diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e5f2e7786..bae354f951 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Changes: - Fix generate of well-known storage keys (e.g. `:code`) - Optimize metadata unknown type validation - Allow metadata creation for `Map` or `Record` +- Added explicit double-byte ss58 `AccountId` encoding test - Update to latest Substrate metadata diff --git a/packages/types/src/generic/AccountId.spec.ts b/packages/types/src/generic/AccountId.spec.ts index 4003577a6f..8ecc214473 100644 --- a/packages/types/src/generic/AccountId.spec.ts +++ b/packages/types/src/generic/AccountId.spec.ts @@ -28,7 +28,9 @@ describe('AccountId', (): void => { it('allows decoding from null', (): void => { expect( - registry.createType('AccountId', null).eq('5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM') + registry + .createType('AccountId', null) + .eq('5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM') ).toBe(true); }); }); @@ -36,9 +38,11 @@ describe('AccountId', (): void => { describe('decoding', (): void => { const testDecode = (type: string, input: Uint8Array | string | AccountId, expected: string): void => it(`can decode from ${type}`, (): void => { - const a = registry.createType('AccountId', input); - - expect(a.toString()).toBe(expected); + expect( + registry + .createType('AccountId', input) + .toString() + ).toBe(expected); }); it('fails with non-32-byte lengths', (): void => { @@ -75,9 +79,9 @@ describe('AccountId', (): void => { describe('encoding', (): void => { const testEncode = (to: 'toHex' | 'toJSON' | 'toString' | 'toU8a', expected: Uint8Array | string, input = '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'): void => it(`can encode ${to}`, (): void => { - const a = registry.createType('AccountId', input); - - expect(a[to]()).toEqual(expected); + expect( + registry.createType('AccountId', input)[to]() + ).toEqual(expected); }); testEncode('toHex', '0x0102030405060708010203040506070801020304050607080102030405060708'); @@ -90,12 +94,34 @@ describe('AccountId', (): void => { ])); it('decodes to a non-empty value', (): void => { - expect(registry.createType('AccountId', 'FVU8T5yaAssHAUwo3PnKso7fqNthEqJZGCZgJZSR9cKjWAf').isEmpty).toBe(false); + expect( + registry + .createType('AccountId', 'FVU8T5yaAssHAUwo3PnKso7fqNthEqJZGCZgJZSR9cKjWAf') + .isEmpty + ).toBe(false); + }); + + it('encodes a 2-byte key correctly', (): void => { + const registry = new TypeRegistry(); + + registry.setChainProperties( + registry.createType('ChainProperties', { + ss58Format: 252 + }) + ); + + expect( + registry + .createType('AccountId', '0x5c64f1151f0ce4358c27238fb20c88e7c899825436f565410724c8c2c5add869') + .toString() + ).toEqual('xw5g1Eec8LT99pZLZMaTWwrwvNtfM6vrSuZeVbtEszCDUwByg'); }); }); describe('storage decoding', (): void => { it('has the correct entries', (): void => { + const registry = new TypeRegistry(); + registry.setChainProperties( registry.createType('ChainProperties', { ss58Format: 2 @@ -103,9 +129,12 @@ describe('AccountId', (): void => { ); const data = registry.createType('StorageData', jsonVec.params.result.changes[0][1]); - const list = registry.createType('Vec', data).map((accountId) => accountId.toString()); - expect(list).toEqual([ + expect( + registry + .createType('Vec', data) + .map((accountId) => accountId.toString()) + ).toEqual([ 'FXmrFbdg2VdG1NttGTx1S6Czbv2SQ7PaD2PfryGBWWgt52i', 'ELSnAoxwXaRP5i4RPjs6m1K3AGVCfXpt8GjTcrgtxvrAuZv', 'EaoT1Mn6H3N9Ui3WdiLGTdKgjGChbrpLfnNV5zSLJLhZvgj', diff --git a/packages/types/src/generic/AccountId.ts b/packages/types/src/generic/AccountId.ts index d9fc8da4d3..601c7462d6 100644 --- a/packages/types/src/generic/AccountId.ts +++ b/packages/types/src/generic/AccountId.ts @@ -14,13 +14,13 @@ function decodeAccountId (value?: AnyU8a | AnyString): Uint8Array { return new Uint8Array(); } else if (isU8a(value) || Array.isArray(value)) { return u8aToU8a(value); - } else if (isHex(value)) { - return hexToU8a(value.toString()); } else if (isString(value)) { - return decodeAddress((value as string).toString()); + return isHex(value) + ? hexToU8a(value.toString()) + : decodeAddress((value as string).toString()); } - throw new Error('Unknown type passed to AccountId constructor'); + throw new Error(`Unknown type passed to AccountId constructor, found typeof ${typeof value}`); } /**