Files
self/common/tests/scope.test.ts
Evi Nova 111d513093 Fix/auto convert sc endpoint to lowerpoint (#1760)
* fix: automatically lowercase smart contract end points

To avoid getting scopeMismatch() errors which were caused by ethers.js returning checksummed addresses (mixed case, including upper case) by default. Forces to lowercase to match logic in on chain _calculateScope() function

* fix: auto lowercase for Go SDK

* chore: yarn prettier
2026-02-20 02:04:18 -08:00

111 lines
3.7 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
bigIntToString,
formatEndpoint,
hashEndpointWithScope,
stringToBigInt,
} from '../src/utils/scope.js';
describe('Scope Utilities', () => {
describe('formatEndpoint', () => {
const testCases = [
{ input: 'https://example.com/path', expected: 'example.com' },
{ input: 'http://subdomain.example.org/path?query=1', expected: 'subdomain.example.org' },
{ input: 'example.net', expected: 'example.net' },
{ input: '', expected: '' },
{ input: 'https://multiple.dots.in.domain.com', expected: 'multiple.dots.in.domain.com' },
{
input: 'https://multiple.dots.in.domain.com.that.exceeds.25.chars/path?query=1',
expected: 'multiple.dots.in.domain.com.that.exceeds.25.chars',
},
{
input: '0x37F5CB8cB1f6B00aa768D8aA99F1A9289802A968',
expected: '0x37f5cb8cb1f6b00aa768d8aa99f1a9289802a968',
},
];
testCases.forEach(({ input, expected }) => {
it(`should format "${input}" to "${expected}"`, () => {
const result = formatEndpoint(input);
expect(result).toBe(expected);
});
});
});
describe('hashEndpointWithScope', () => {
it('should hash endpoint and scope correctly', () => {
const endpoint = 'https://example.com';
const scope = 'scope1';
const result = hashEndpointWithScope(endpoint, scope);
expect(typeof result).toBe('string');
expect(Number.isNaN(Number(result))).toBe(false);
});
it('should produce different hashes for different endpoints with same scope', () => {
const scope = 'scope1';
const hash1 = hashEndpointWithScope('https://example.com', scope);
const hash2 = hashEndpointWithScope('https://different.com', scope);
expect(hash1).not.toBe(hash2);
});
it('should produce different hashes for different scopes with same endpoint', () => {
const endpoint =
'https://example-endpoint-that-exceeds-31-but-is-not-too-long-to-be-a-valid-domain-as-the-max-is-496-chars.com';
const hash1 = hashEndpointWithScope(endpoint, 'scope1');
const hash2 = hashEndpointWithScope(endpoint, 'scope2');
expect(hash1).not.toBe(hash2);
});
it('should hash 0x37F5CB8cB1f6B00aa768D8aA99F1A9289802A968 correctly', () => {
const hash = hashEndpointWithScope('0x37F5CB8cB1f6B00aa768D8aA99F1A9289802A968', 'scope1');
expect(hash).toBe(
'17320225058247886741210754832666102619008901426838628632042987297713059140253'
);
});
it('should produce the same hash for checksummed and lowercase addresses', () => {
const checksummed = hashEndpointWithScope(
'0x37F5CB8cB1f6B00aa768D8aA99F1A9289802A968',
'scope1'
);
const lowercase = hashEndpointWithScope(
'0x37f5cb8cb1f6b00aa768d8aa99f1a9289802a968',
'scope1'
);
expect(checksummed).toBe(lowercase);
});
});
describe('stringToBigInt', () => {
it('should convert various strings to bigint correctly', () => {
const testCases = [
'hello-world',
'test123',
'UPPERCASE',
'mixed_CASE_123',
'symbols!@#$%',
'short',
'',
'a',
'12345',
'exactly-31-characters-in-length',
];
for (const str of testCases) {
const result = stringToBigInt(str);
const roundTrip = bigIntToString(result);
expect(roundTrip).toBe(str);
}
});
it('should throw an error for strings longer than 25 characters', () => {
const longScope = '1234567890123456789012345678901212983719283719283791287391287312379123798';
expect(() => stringToBigInt(longScope)).toThrow(
'Resulting BigInt exceeds maximum size of 31 bytes'
);
});
});
});