From ed30f76207fbb1f6a9f5734c3bd97b6ed2109f00 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 16 Aug 2018 15:02:42 +0200 Subject: [PATCH] Decode accountIds with prefixes (#136) * Decode accountIds with prefixes * Test for extrinsic decoding --- .../src/decode/value/accountId.spec.js | 75 +++++++++++++++++++ .../type-params/src/decode/value/accountId.ts | 31 +++++++- packages/type-params/src/decode/value/u8a.ts | 2 +- .../src/json/extrinsic/decode.spec.js | 27 +++++++ 4 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 packages/type-params/src/decode/value/accountId.spec.js create mode 100644 packages/type-primitives/src/json/extrinsic/decode.spec.js diff --git a/packages/type-params/src/decode/value/accountId.spec.js b/packages/type-params/src/decode/value/accountId.spec.js new file mode 100644 index 0000000000..ffc906a945 --- /dev/null +++ b/packages/type-params/src/decode/value/accountId.spec.js @@ -0,0 +1,75 @@ +// Copyright 2017-2018 @polkadot/params authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import decode from './accountId'; + +describe('AccountId', () => { + it('decodes null as null', () => { + expect( + decode(null) + ).toEqual({ + length: 0, + value: null + }); + }); + + it('encodes without a prefix (storage)', () => { + expect( + decode( + new Uint8Array([ + 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, + 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 + ]), + 'latest', true + ) + ).toEqual({ + length: 32, + value: '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCt72s' + }); + }); + + it('encodes without a prefix (poc-1)', () => { + expect( + decode( + new Uint8Array([ + 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, + 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 + ]), + 'poc-1', false + ) + ).toEqual({ + length: 32, + value: '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCt72s' + }); + }); + + it('decodes with a prefix (255)', () => { + expect( + decode( + new Uint8Array([ + 255, + 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, + 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 + ]), + 'latest', false + ) + ).toEqual({ + length: 33, + value: '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCt72s' + }); + }); + + it('codes with a prefix (1-byte)', () => { + expect( + decode( + new Uint8Array([ + 1, 17 + ]) + ) + ).toEqual({ + length: 2, + value: '0x11' + }); + }); +}); diff --git a/packages/type-params/src/decode/value/accountId.ts b/packages/type-params/src/decode/value/accountId.ts index 5ced50c501..f0c4f1f8ce 100644 --- a/packages/type-params/src/decode/value/accountId.ts +++ b/packages/type-params/src/decode/value/accountId.ts @@ -5,6 +5,8 @@ import { EncodingVersions, Param$Decoded } from '../../types'; import encodeAddress from '@polkadot/util-keyring/address/encode'; +import ExtError from '@polkadot/util/ext/error'; +import u8aToHex from '@polkadot/util/u8a/toHex'; import u8a from './u8a'; @@ -16,10 +18,35 @@ export default function accountId (input: Uint8Array | null | undefined, version } as Param$Decoded; } - const u8aDecoded = u8a(input, 256, (isStorage || version === 'poc-1') ? 0 : 1); + const withoutPrefix = isStorage || version === 'poc-1'; + const offset = withoutPrefix ? 0 : 1; + const length = withoutPrefix + ? 256 + : (() => { + const first = input[0]; + + if (first <= 0xef) { + return 1 * 8; + } else if (first === 0xfc) { + return 2 * 8; + } else if (first === 0xfd) { + return 4 * 8; + } else if (first === 0xfe) { + return 8 * 8; + } else if (first === 0xff) { + return 32 * 8; + } + + throw new ExtError(`Invalid account index byte, 0x${first.toString(16)}`); + })(); + + const u8aDecoded = u8a(input, length, offset); + const publicKey = u8aDecoded.value as Uint8Array; return { length: u8aDecoded.length, - value: encodeAddress(u8aDecoded.value as Uint8Array) + value: length === 256 + ? encodeAddress(publicKey) + : u8aToHex(publicKey) }; } diff --git a/packages/type-params/src/decode/value/u8a.ts b/packages/type-params/src/decode/value/u8a.ts index 847b3e1af3..84b101afa4 100644 --- a/packages/type-params/src/decode/value/u8a.ts +++ b/packages/type-params/src/decode/value/u8a.ts @@ -4,7 +4,7 @@ import { Param$Decoded } from '../../types'; -export default function u8a (input: Uint8Array | null | undefined, bitLength: 256 | 512, offset: number): Param$Decoded { +export default function u8a (input: Uint8Array | null | undefined, bitLength: number, offset: number): Param$Decoded { if (!input) { return { length: 0, diff --git a/packages/type-primitives/src/json/extrinsic/decode.spec.js b/packages/type-primitives/src/json/extrinsic/decode.spec.js new file mode 100644 index 0000000000..018b536ad7 --- /dev/null +++ b/packages/type-primitives/src/json/extrinsic/decode.spec.js @@ -0,0 +1,27 @@ +// Copyright 2017-2018 @polkadot/primitives authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import decode from './decode'; + +const extrinsics = [ + new Uint8Array([ + 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 41, 68, 117, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + ]), + new Uint8Array([ + 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + ]), + new Uint8Array([ + 255, 77, 4, 166, 251, 34, 133, 176, 143, 12, 198, 201, 222, 181, 12, 200, 97, 198, 90, 180, 149, 151, 27, 224, 180, 14, 209, 39, 82, 19, 43, 128, 246, 16, 0, 0, 0, 2, 3, 255, 234, 26, 21, 20, 14, 86, 45, 159, 88, 43, 56, 214, 77, 141, 31, 87, 91, 159, 131, 117, 86, 253, 236, 64, 252, 195, 229, 100, 37, 148, 113, 199, 54, 163, 211, 184, 103, 79, 218, 214, 142, 212, 194, 213, 30, 205, 144, 202, 98, 169, 249, 42, 218, 178, 115, 60, 189, 224, 37, 129, 199, 179, 25, 68, 43, 230, 218, 86, 161, 223, 189, 1, 170, 86, 36, 118, 21, 90, 182, 34, 135, 60, 104, 202, 1, 12, 105, 28, 88, 227, 177, 1, 37, 220, 50, 14 + ]) +]; + +describe('extrinsic', () => { + extrinsics.forEach((extrinsic, index) => { + it(`decodes the extrinsic - ${index}`, () => { + const decoded = decode(extrinsic); + + expect(decoded).toBeDefined(); + }); + }); +});