Decode accountIds with prefixes (#136)

* Decode accountIds with prefixes

* Test for extrinsic decoding
This commit is contained in:
Jaco Greeff
2018-08-16 15:02:42 +02:00
committed by GitHub
parent c17ba5c952
commit ed30f76207
4 changed files with 132 additions and 3 deletions
@@ -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'
});
});
});
@@ -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)
};
}
+1 -1
View File
@@ -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,