diff --git a/package.json b/package.json index 387170d1f..1ee58bcfd 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "test": "jest --coverage" }, "devDependencies": { - "@polkadot/dev": "^0.20.21", + "@polkadot/dev": "^0.20.24", "@polkadot/ts": "^0.1.30" } } diff --git a/packages/keyring/src/address/decode.spec.js b/packages/keyring/src/address/decode.spec.js index ff8fc214d..d9ce34dec 100644 --- a/packages/keyring/src/address/decode.spec.js +++ b/packages/keyring/src/address/decode.spec.js @@ -33,6 +33,36 @@ describe('decode', () => { ); }); + it('decodes a short address', () => { + expect( + decode('F7L6') + ).toEqual(new Uint8Array([1])); + }); + + it('decodes a 1-byte accountId (with prefix)', () => { + expect( + decode('Pqt7', 68) + ).toEqual(new Uint8Array([1])); + }); + + it('decodes a 2-byte accountId', () => { + expect( + decode('2jpAJz', 68) + ).toEqual(new Uint8Array([0, 1])); + }); + + it('encodes a 4-byte address', () => { + expect( + decode('as7QnGQ7', 68) + ).toEqual(new Uint8Array([1, 2, 3, 4])); + }); + + it('decodes a 8-byte accountId', () => { + expect( + decode('4q7qY5RBG7Z4xX', 68) + ).toEqual(new Uint8Array([42, 44, 10, 0, 0, 0, 0, 0])); + }); + it('fails when prefix is invalid', () => { expect( () => decode('BXHKtJjKdXjvbvTg6qK275sgcKKq8dnU38MQQAiR3LN2stP') diff --git a/packages/keyring/src/address/decode.ts b/packages/keyring/src/address/decode.ts index 43c5c91e7..afb19f813 100644 --- a/packages/keyring/src/address/decode.ts +++ b/packages/keyring/src/address/decode.ts @@ -2,6 +2,8 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. +import { Prefix } from './types'; + // Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L6 import bs58 from 'bs58'; @@ -13,7 +15,9 @@ import isU8a from '@polkadot/util/is/u8a'; import u8aToU8a from '@polkadot/util/u8a/toU8a'; import blake2b from '@polkadot/util-crypto/blake2/asU8a'; -export default function decode (encoded: string | Uint8Array): Uint8Array { +import defaults from './defaults'; + +export default function decode (encoded: string | Uint8Array, prefix: Prefix = defaults.prefix): Uint8Array { if (isU8a(encoded) || isHex(encoded)) { return u8aToU8a(encoded); } @@ -22,12 +26,21 @@ export default function decode (encoded: string | Uint8Array): Uint8Array { const error = (message: string) => `Decoding ${encoded}: ${message}`; - assert(decoded[0] === 42, error('Invalid decoded address prefix')); - assert(decoded.length === 32 + 1 + 2, error('Invalid decoded address length')); + assert(decoded[0] === prefix, error('Invalid decoded address prefix')); + assert(defaults.allowedEncodedLengths.includes(decoded.length), error('Invalid decoded address length')); - const hash = blake2b(decoded.subarray(0, 33), 512); + const isPublicKey = decoded.length === 35; - assert(decoded[33] === hash[0] && decoded[34] === hash[1], error(' Invalid decoded address checksum')); + // non-publicKeys has 1 byte checksums, else default to 2 + const endPos = decoded.length - (isPublicKey ? 2 : 1); - return decoded.slice(1, 33); + // calculate the hash and do the checksum byte checks + const hash = blake2b(decoded.subarray(0, endPos), 512); + const checks = isPublicKey + ? decoded[decoded.length - 2] === hash[0] && decoded[decoded.length - 1] === hash[1] + : decoded[decoded.length - 1] === hash[0]; + + assert(checks, error('Invalid decoded address checksum')); + + return decoded.slice(1, endPos); } diff --git a/packages/keyring/src/address/defaults.ts b/packages/keyring/src/address/defaults.ts new file mode 100644 index 000000000..59cab5223 --- /dev/null +++ b/packages/keyring/src/address/defaults.ts @@ -0,0 +1,12 @@ +// Copyright 2017-2018 @polkadot/util-keyring authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import { Prefix } from './types'; + +export default { + allowedDecodedLengths: [1, 2, 4, 8, 32], + // publicKey has prefix + 2 checksum bytes, short only prefix + 1 checksum byte + allowedEncodedLengths: [3, 4, 6, 10, 35], + prefix: 42 as Prefix +}; diff --git a/packages/keyring/src/address/encode.spec.js b/packages/keyring/src/address/encode.spec.js index bb46e43ae..74187a531 100644 --- a/packages/keyring/src/address/encode.spec.js +++ b/packages/keyring/src/address/encode.spec.js @@ -20,6 +20,46 @@ describe('encode', () => { () => encode( keyring.alice.publicKey().slice(0, 30) ) - ).toThrow(/Expected a valid publicKey/); + ).toThrow(/Expected a valid key/); + }); + + it('encodes a 1-byte address', () => { + expect( + encode( + new Uint8Array([1]) + ) + ).toEqual('F7L6'); + }); + + it('encodes a 1-byte address (with prefix)', () => { + expect( + encode( + new Uint8Array([1]), 68 + ) + ).toEqual('Pqt7'); + }); + + it('encodes a 2-byte address', () => { + expect( + encode( + new Uint8Array([0, 1]), 68 + ) + ).toEqual('2jpAJz'); + }); + + it('encodes a 4-byte address', () => { + expect( + encode( + new Uint8Array([1, 2, 3, 4]), 68 + ) + ).toEqual('as7QnGQ7'); + }); + + it('enodes a 8-byte address', () => { + expect( + encode( + new Uint8Array([42, 44, 10, 0, 0, 0, 0, 0]), 68 + ) + ).toEqual('4q7qY5RBG7Z4xX'); }); }); diff --git a/packages/keyring/src/address/encode.ts b/packages/keyring/src/address/encode.ts index 4af1d5492..31a75be53 100644 --- a/packages/keyring/src/address/encode.ts +++ b/packages/keyring/src/address/encode.ts @@ -2,6 +2,8 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. +import { Prefix } from './types'; + // Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L34 import bs58 from 'bs58'; @@ -12,19 +14,22 @@ import u8aToBuffer from '@polkadot/util/u8a/toBuffer'; import u8aToU8a from '@polkadot/util/u8a/toU8a'; import blake2b from '@polkadot/util-crypto/blake2/asU8a'; -const PREFIX = new Uint8Array([42]); +import defaults from './defaults'; -export default function encode (_publicKey: Uint8Array | string): string { - const publicKey = u8aToU8a(_publicKey); +export default function encode (_key: Uint8Array | string, prefix: Prefix = defaults.prefix): string { + const key = u8aToU8a(_key); - assert(publicKey.length === 32, `Expected a valid publicKey to convert`); + assert(defaults.allowedDecodedLengths.includes(key.length), `Expected a valid key to convert, with length ${defaults.allowedDecodedLengths}`); - const input = u8aConcat(PREFIX, publicKey); + const isPublicKey = key.length === 32; + + // generate an input with the prefix and calculate hash + const input = u8aConcat(new Uint8Array([prefix]), key); const hash = blake2b(input, 512); return bs58.encode( u8aToBuffer( - u8aConcat(input, hash.subarray(0, 2)) + u8aConcat(input, hash.subarray(0, isPublicKey ? 2 : 1)) ) ); } diff --git a/packages/keyring/src/address/types.d.ts b/packages/keyring/src/address/types.d.ts new file mode 100644 index 000000000..2cabaa40d --- /dev/null +++ b/packages/keyring/src/address/types.d.ts @@ -0,0 +1,5 @@ +// Copyright 2017-2018 @polkadot/util-keyring authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +export type Prefix = 0 | 1 | 42 | 43 | 68 | 69; diff --git a/yarn.lock b/yarn.lock index 734415d1b..40dba9bb8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1164,9 +1164,9 @@ version "1.1.0" resolved "https://registry.yarnpkg.com/@nodelib/fs.stat/-/fs.stat-1.1.0.tgz#50c1e2260ac0ed9439a181de3725a0168d59c48a" -"@polkadot/dev@^0.20.21": - version "0.20.21" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.20.21.tgz#81d707d5aeb4b32e22fea1e1653ab238c77eefeb" +"@polkadot/dev@^0.20.24": + version "0.20.24" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.20.24.tgz#e88af950aa4e0e1a549d0a4860b2b3f8d63ce723" dependencies: "@babel/cli" "^7.0.0" "@babel/core" "^7.0.0"