Encode & decode short addresses (#221)

* Encode & decode short addresses

* Adjust short addresses short checksum

* Make prefix coinfigurable as an input

* Expand tests, cleanup short encoding
This commit is contained in:
Jaco Greeff
2018-10-13 00:03:48 +02:00
committed by GitHub
parent c07f3a5f5c
commit 400c6c5685
8 changed files with 122 additions and 17 deletions
+1 -1
View File
@@ -20,7 +20,7 @@
"test": "jest --coverage"
},
"devDependencies": {
"@polkadot/dev": "^0.20.21",
"@polkadot/dev": "^0.20.24",
"@polkadot/ts": "^0.1.30"
}
}
@@ -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')
+19 -6
View File
@@ -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);
}
+12
View File
@@ -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
};
+41 -1
View File
@@ -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');
});
});
+11 -6
View File
@@ -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))
)
);
}
+5
View File
@@ -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;
+3 -3
View File
@@ -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"