From 05f475485785bafd2a5e32682a2c0ee525118380 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sat, 5 May 2018 20:09:14 +0200 Subject: [PATCH] Add keyring publicKey <-> address (#122) * Add keyring publicKey <-> address * Add isObservable utility --- flow-typed/bs58.js | 8 ++++ package.json | 2 +- packages/util-keyring/package.json | 3 +- packages/util-keyring/src/address/decode.js | 24 ++++++++++++ .../util-keyring/src/address/decode.spec.js | 38 +++++++++++++++++++ packages/util-keyring/src/address/encode.js | 30 +++++++++++++++ .../util-keyring/src/address/encode.spec.js | 23 +++++++++++ packages/util-keyring/src/address/index.js | 12 ++++++ packages/util-keyring/src/index.js | 5 +++ packages/util-keyring/src/pair/index.js | 5 +++ packages/util-keyring/src/pair/types.js | 1 + packages/util-keyring/src/testingPairs.js | 5 ++- packages/util-keyring/src/types.js | 2 + packages/util/package.json | 2 +- packages/util/src/is/bn.js | 8 ++-- packages/util/src/is/bn.spec.js | 6 +-- packages/util/src/is/index.js | 6 ++- packages/util/src/is/observable.js | 23 +++++++++++ packages/util/src/is/observable.spec.js | 25 ++++++++++++ yarn.lock | 32 +++++++++++----- 20 files changed, 236 insertions(+), 24 deletions(-) create mode 100644 flow-typed/bs58.js create mode 100644 packages/util-keyring/src/address/decode.js create mode 100644 packages/util-keyring/src/address/decode.spec.js create mode 100644 packages/util-keyring/src/address/encode.js create mode 100644 packages/util-keyring/src/address/encode.spec.js create mode 100644 packages/util-keyring/src/address/index.js create mode 100644 packages/util/src/is/observable.js create mode 100644 packages/util/src/is/observable.spec.js diff --git a/flow-typed/bs58.js b/flow-typed/bs58.js new file mode 100644 index 000000000..0d82b12e2 --- /dev/null +++ b/flow-typed/bs58.js @@ -0,0 +1,8 @@ +// @flow + +declare module 'bs58' { + declare module.exports: { + decode: (input: string) => Buffer, + encode: (input: Buffer | Array) => string + } +} diff --git a/package.json b/package.json index 1d8cc4da8..284576e4c 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "test": "jest --coverage" }, "devDependencies": { - "@polkadot/dev": "^0.17.12", + "@polkadot/dev": "^0.17.13", "lerna": "^2.5.1" } } diff --git a/packages/util-keyring/package.json b/packages/util-keyring/package.json index f9413cdc7..6a99167e8 100644 --- a/packages/util-keyring/package.json +++ b/packages/util-keyring/package.json @@ -33,6 +33,7 @@ "dependencies": { "@polkadot/util": "^0.19.4", "@polkadot/util-crypto": "^0.19.4", - "babel-runtime": "^6.26.0" + "babel-runtime": "^6.26.0", + "bs58": "^4.0.1" } } diff --git a/packages/util-keyring/src/address/decode.js b/packages/util-keyring/src/address/decode.js new file mode 100644 index 000000000..76f9dc6c1 --- /dev/null +++ b/packages/util-keyring/src/address/decode.js @@ -0,0 +1,24 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. +// @flow + +// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L6 + +const bs58 = require('bs58'); +const assert = require('@polkadot/util/assert'); +const bufferToU8a = require('@polkadot/util/buffer/toU8a'); +const blake2b = require('@polkadot/util-crypto/blake2/asU8a512'); + +module.exports = function decode (encoded: string): Uint8Array { + const decoded = bufferToU8a(bs58.decode(encoded)); + + assert(decoded[0] === 42, 'Invalid decoded address prefix'); + assert(decoded.length === 32 + 1 + 2, 'Invalid decoded address length'); + + const hash = blake2b(decoded.subarray(0, 33)); + + assert(decoded[33] === hash[0] && decoded[34] === hash[1], 'Invalid decoded address checksum'); + + return decoded.slice(1, 33); +}; diff --git a/packages/util-keyring/src/address/decode.spec.js b/packages/util-keyring/src/address/decode.spec.js new file mode 100644 index 000000000..40958ff72 --- /dev/null +++ b/packages/util-keyring/src/address/decode.spec.js @@ -0,0 +1,38 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. +// @flow + +const keyring = require('../testingPairs')(); +const decode = require('./decode'); + +describe('decode', () => { + it('decodes an address', () => { + expect( + decode('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ') + ).toEqual( + keyring.alice.publicKey() + ); + }); + + it('fails when prefix is invalid', () => { + expect( + () => decode('BXHKtJjKdXjvbvTg6qK275sgcKKq8dnU38MQQAiR3LN2stP') + ).toThrow(/address prefix/); + }); + + it('fails when length is invalid', () => { + expect( + () => decode('y9EMHt34JJo4rWLSaxoLGdYXvjgSXEd4zHUnQgfNzwES8b') + ).toThrow(/address length/); + }); + + it('fails when the checksum does not match', () => { + expect( + () => decode('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMa9cj') + ).toThrow(/address checksum/); + expect( + () => decode('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDwU') + ).toThrow(/address checksum/); + }); +}); diff --git a/packages/util-keyring/src/address/encode.js b/packages/util-keyring/src/address/encode.js new file mode 100644 index 000000000..1ab64ad0c --- /dev/null +++ b/packages/util-keyring/src/address/encode.js @@ -0,0 +1,30 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. +// @flow + +// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L34 + +const bs58 = require('bs58'); +const assert = require('@polkadot/util/assert'); +const u8aConcat = require('@polkadot/util/u8a/concat'); +const u8aToBuffer = require('@polkadot/util/u8a/toBuffer'); +const u8aToU8a = require('@polkadot/util/u8a/toU8a'); +const blake2b = require('@polkadot/util-crypto/blake2/asU8a512'); + +const PREFIX = new Uint8Array([42]); + +module.exports = function encode (_publicKey: Uint8Array | string): string { + const publicKey = u8aToU8a(_publicKey); + + assert(publicKey.length === 32, `Expected a valid publicKey to convert`); + + const input = u8aConcat(PREFIX, publicKey); + const hash = blake2b(input); + + return bs58.encode( + u8aToBuffer( + u8aConcat(input, hash.subarray(0, 2)) + ) + ); +}; diff --git a/packages/util-keyring/src/address/encode.spec.js b/packages/util-keyring/src/address/encode.spec.js new file mode 100644 index 000000000..e31c4be66 --- /dev/null +++ b/packages/util-keyring/src/address/encode.spec.js @@ -0,0 +1,23 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. +// @flow + +const keyring = require('../testingPairs')(); +const encode = require('./encode'); + +describe('encode', () => { + it('encodes an address to a valid value', () => { + expect( + keyring.alice.address() + ).toEqual('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ'); + }); + + it('fails when non-valid publicKey provided', () => { + expect( + () => encode( + keyring.alice.publicKey().slice(0, 30) + ) + ).toThrow(/Expected a valid publicKey/); + }); +}); diff --git a/packages/util-keyring/src/address/index.js b/packages/util-keyring/src/address/index.js new file mode 100644 index 000000000..b568a2719 --- /dev/null +++ b/packages/util-keyring/src/address/index.js @@ -0,0 +1,12 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. +// @flow + +const decodeAddress = require('./decode'); +const encodeAddress = require('./encode'); + +module.exports = { + decodeAddress, + encodeAddress +}; diff --git a/packages/util-keyring/src/index.js b/packages/util-keyring/src/index.js index ab91d3b62..2c3c58c68 100644 --- a/packages/util-keyring/src/index.js +++ b/packages/util-keyring/src/index.js @@ -7,6 +7,7 @@ import type { KeyringInstance, KeyringPair } from './types'; const naclKeypairFromSeed = require('@polkadot/util-crypto/nacl/keypair/fromSeed'); +const addressDecode = require('./address/decode'); const decrypt = require('./decrypt'); const encrypt = require('./encrypt'); const createPair = require('./pair'); @@ -22,6 +23,10 @@ module.exports = function keyring (): KeyringInstance { pairs.add(decrypt(encrypted, passphrase)), encrypt: (publicKey: Uint8Array, passphrase: Uint8Array | string): Uint8Array => encrypt(pairs.get(publicKey), passphrase), + getAddress: (address: string): KeyringPair => + pairs.get( + addressDecode(address) + ), getPair: (publicKey: Uint8Array): KeyringPair => pairs.get(publicKey), getPairs: pairs.all, diff --git a/packages/util-keyring/src/pair/index.js b/packages/util-keyring/src/pair/index.js index 91eba76fd..f22e9ed92 100644 --- a/packages/util-keyring/src/pair/index.js +++ b/packages/util-keyring/src/pair/index.js @@ -10,6 +10,7 @@ import type { PairState } from './types'; const naclSign = require('@polkadot/util-crypto/nacl/sign'); const naclVerify = require('@polkadot/util-crypto/nacl/verify'); +const encodeAddress = require('../address/encode'); const decode = require('./decode'); const encode = require('./encode'); const getMeta = require('./getMeta'); @@ -17,15 +18,19 @@ const setMeta = require('./setMeta'); module.exports = function pair ({ publicKey, secretKey }: KeypairType): KeyringPair { const state: PairState = { + address: encodeAddress(publicKey), meta: {} }; return { + address: (): string => + state.address, decodePkcs8: (encoded: Uint8Array, passphrase?: Uint8Array | string): void => { const decoded = decode(encoded, passphrase); publicKey = decoded.publicKey; secretKey = decoded.secretKey; + state.address = encodeAddress(publicKey); }, encodePkcs8: (passphrase?: Uint8Array | string): Uint8Array => encode(secretKey, passphrase), diff --git a/packages/util-keyring/src/pair/types.js b/packages/util-keyring/src/pair/types.js index 5679e08e7..0790b0441 100644 --- a/packages/util-keyring/src/pair/types.js +++ b/packages/util-keyring/src/pair/types.js @@ -6,5 +6,6 @@ import type { KeyringPair$Meta } from '../types'; export type PairState = { + address: string, meta: KeyringPair$Meta }; diff --git a/packages/util-keyring/src/testingPairs.js b/packages/util-keyring/src/testingPairs.js index 8c17ee290..618e6ab06 100644 --- a/packages/util-keyring/src/testingPairs.js +++ b/packages/util-keyring/src/testingPairs.js @@ -3,9 +3,10 @@ // of the ISC license. See the LICENSE file for details. // @flow -import type { KeyringPair } from './types'; +import type { KeyringPair, KeyringInstance } from './types'; type TestKeyringMap = { + _keyring: KeyringInstance, [string]: KeyringPair }; @@ -21,5 +22,5 @@ module.exports = function testKeyringPairs (): TestKeyringMap { result[(name: string)] = pair; return result; - }, {}); + }, { _keyring: keyring }); }; diff --git a/packages/util-keyring/src/types.js b/packages/util-keyring/src/types.js index 06b853897..00e840864 100644 --- a/packages/util-keyring/src/types.js +++ b/packages/util-keyring/src/types.js @@ -9,6 +9,7 @@ export type KeyringPair$Meta = { } export type KeyringPair = { + address: () => string, decodePkcs8: (encoded: Uint8Array, passphrase: Uint8Array | string) => void, encodePkcs8: (passphrase: Uint8Array | string) => Uint8Array, getMeta: () => KeyringPair$Meta, @@ -28,6 +29,7 @@ export type KeyringInstance = { addFromSeed (seed: Uint8Array): KeyringPair, decrypt (encrypted: Uint8Array, secret: Uint8Array | string): KeyringPair, encrypt (publicKey: Uint8Array, secret: Uint8Array | string): Uint8Array, + getAddress (address: string): KeyringPair, getPair (publicKey: Uint8Array): KeyringPair, getPairs (): Array, getPublicKeys (): Array diff --git a/packages/util/package.json b/packages/util/package.json index a9d324565..8277faa60 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -29,7 +29,7 @@ "test": "echo \"Tests only available from root wrapper\"" }, "devDependencies": { - "@polkadot/api-jsonrpc": "^0.9.5" + "@polkadot/api-jsonrpc": "^0.9.15" }, "dependencies": { "babel-runtime": "^6.26.0", diff --git a/packages/util/src/is/bn.js b/packages/util/src/is/bn.js index 3aa4a3262..bff8a90c8 100644 --- a/packages/util/src/is/bn.js +++ b/packages/util/src/is/bn.js @@ -6,17 +6,17 @@ const BN = require('bn.js'); /** - @name isBN + @name isBn @signature isBN (value: mixed): boolean @summary Tests for a `BN` object instance. @description Checks to see if the input object is an instance of `BN` (bn.js). @example import BN from 'bn.js'; - import { isBN } from '@polkadot/util'; + import { isBn } from '@polkadot/util'; - console.log('isBN', isBN(new BN(1))); // => true + console.log('isBn', isBn(new BN(1))); // => true */ -module.exports = function isBN (value: mixed): boolean { +module.exports = function isBn (value: mixed): boolean { return BN.isBN(value); }; diff --git a/packages/util/src/is/bn.spec.js b/packages/util/src/is/bn.spec.js index e0c3e4a0b..2ec0b4eca 100644 --- a/packages/util/src/is/bn.spec.js +++ b/packages/util/src/is/bn.spec.js @@ -4,18 +4,18 @@ const BN = require('bn.js'); -const { isBN } = require('./index'); +const { isBn } = require('./index'); describe('isBN', () => { it('returns true when a BN value', () => { expect( - isBN(new BN(123)) + isBn(new BN(123)) ).toEqual(true); }); it('returns false on non-BN values', () => { expect( - isBN(0) + isBn(0) ).toEqual(false); }); }); diff --git a/packages/util/src/is/index.js b/packages/util/src/is/index.js index 56e6b9662..a5f0e680c 100644 --- a/packages/util/src/is/index.js +++ b/packages/util/src/is/index.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const isBN = require('./bn'); +const isBn = require('./bn'); const isBuffer = require('./buffer'); const isBoolean = require('./boolean'); const isError = require('./error'); @@ -14,6 +14,7 @@ const isIp = require('./ip'); const isNull = require('./null'); const isNumber = require('./number'); const isObject = require('./object'); +const isObservable = require('./observable'); const isString = require('./string'); const isU8a = require('./u8a'); const isUndefined = require('./undefined'); @@ -22,7 +23,7 @@ const isUndefined = require('./undefined'); @summary Type checking utilities */ module.exports = { - isBN, + isBn, isBoolean, isBuffer, isError, @@ -31,6 +32,7 @@ module.exports = { isInstanceOf, isIp, isObject, + isObservable, isNull, isNumber, isString, diff --git a/packages/util/src/is/observable.js b/packages/util/src/is/observable.js new file mode 100644 index 000000000..df1472a11 --- /dev/null +++ b/packages/util/src/is/observable.js @@ -0,0 +1,23 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. +// @flow + +const isFunction = require('./function'); +const isObject = require('./object'); + +/** + @name isBObservable + @signature isObservable (value: mixed): boolean + @summary Tests for a `Observable` object instance. + @description + Checks to see if the input object is an instance of `BN` (bn.js). + @example + import { isObservable } from '@polkadot/util'; + + console.log('isObservable', isObservable(...)); +*/ +module.exports = function isObservable (value: mixed): boolean { + // $FlowFixMe check for possibly non-existent .next + return isObject(value) && isFunction(value.next); +}; diff --git a/packages/util/src/is/observable.spec.js b/packages/util/src/is/observable.spec.js new file mode 100644 index 000000000..9ae970152 --- /dev/null +++ b/packages/util/src/is/observable.spec.js @@ -0,0 +1,25 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +const { isObservable } = require('./index'); + +describe('isObservable', () => { + it('returns true on valid observables', () => { + expect( + isObservable({ next: () => true }) + ).toEqual(true); + }); + + it('returns false on invalid objects', () => { + expect( + isObservable('notAnObservable') + ).toEqual(false); + }); + + it('returns false on invalid next functions', () => { + expect( + isObservable({ next: true }) + ).toEqual(false); + }); +}); diff --git a/yarn.lock b/yarn.lock index f242b1dd5..e2e85040f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -79,15 +79,15 @@ lodash "^4.2.0" to-fast-properties "^2.0.0" -"@polkadot/api-jsonrpc@^0.9.5": - version "0.9.5" - resolved "https://registry.yarnpkg.com/@polkadot/api-jsonrpc/-/api-jsonrpc-0.9.5.tgz#67cd9568ea25b7c1b7f1d1f3b0b7faa9f733ad25" +"@polkadot/api-jsonrpc@^0.9.15": + version "0.9.15" + resolved "https://registry.yarnpkg.com/@polkadot/api-jsonrpc/-/api-jsonrpc-0.9.15.tgz#e210f5ce82d7c41c11ddf3cd83e521ede9f1ddae" dependencies: babel-runtime "^6.26.0" -"@polkadot/dev@^0.17.12": - version "0.17.12" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.17.12.tgz#90d0d302681c5eae4683eea75949548a99e52831" +"@polkadot/dev@^0.17.13": + version "0.17.13" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.17.13.tgz#cd5690d4c45abcc1e4b2624944c79229a82930e2" dependencies: babel-cli "^6.26.0" babel-core "^6.26.0" @@ -112,7 +112,7 @@ eslint-plugin-promise "^3.6.0" eslint-plugin-react "^7.7.0" eslint-plugin-standard "^3.0.1" - flow-bin "^0.70.0" + flow-bin "^0.71.0" flow-copy-source "^1.3.0" jest "^22.4.2" makeshift "^1.1.0" @@ -970,6 +970,12 @@ balanced-match@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" +base-x@^3.0.2: + version "3.0.4" + resolved "https://registry.yarnpkg.com/base-x/-/base-x-3.0.4.tgz#94c1788736da065edb1d68808869e357c977fa77" + dependencies: + safe-buffer "^5.0.1" + base@^0.11.1: version "0.11.2" resolved "https://registry.yarnpkg.com/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f" @@ -1086,6 +1092,12 @@ browserslist@^3.0.0: caniuse-lite "^1.0.30000809" electron-to-chromium "^1.3.33" +bs58@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/bs58/-/bs58-4.0.1.tgz#be161e76c354f6f788ae4071f63f34e8c4f0a42a" + dependencies: + base-x "^3.0.2" + bser@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/bser/-/bser-2.0.0.tgz#9ac78d3ed5d915804fd87acb158bc797147a1719" @@ -2362,9 +2374,9 @@ flat-cache@^1.2.1: graceful-fs "^4.1.2" write "^0.2.1" -flow-bin@^0.70.0: - version "0.70.0" - resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.70.0.tgz#080ae83a997f2b4ddb3dc2649bf13336825292b5" +flow-bin@^0.71.0: + version "0.71.0" + resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.71.0.tgz#fd1b27a6458c3ebaa5cb811853182ed631918b70" flow-copy-source@^1.3.0: version "1.3.0"