From f6d05e0553b8f64fb9c94d7007ca9539d5a784af Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 21 Mar 2019 10:51:02 +0100 Subject: [PATCH] Utility function to extract from path only (#376) * Utility function to extract from path only * Add createFromUri --- packages/keyring/src/index.ts | 17 +++++-- packages/keyring/src/types.ts | 1 + .../util-crypto/src/key/extractPath.spec.ts | 47 +++++++++++++++++++ packages/util-crypto/src/key/extractPath.ts | 36 ++++++++++++++ .../{extract.spec.ts => extractSuri.spec.ts} | 32 ++++++++----- .../src/key/{extract.ts => extractSuri.ts} | 17 ++----- packages/util-crypto/src/key/index.ts | 3 +- 7 files changed, 124 insertions(+), 29 deletions(-) create mode 100644 packages/util-crypto/src/key/extractPath.spec.ts create mode 100644 packages/util-crypto/src/key/extractPath.ts rename packages/util-crypto/src/key/{extract.spec.ts => extractSuri.spec.ts} (83%) rename packages/util-crypto/src/key/{extract.ts => extractSuri.ts} (67%) diff --git a/packages/keyring/src/index.ts b/packages/keyring/src/index.ts index 819e0d26e..9eee25735 100644 --- a/packages/keyring/src/index.ts +++ b/packages/keyring/src/index.ts @@ -6,7 +6,7 @@ import { KeypairType } from '@polkadot/util-crypto/types'; import { KeyringInstance, KeyringPair, KeyringPair$Json, KeyringPair$Meta, KeyringOptions } from './types'; import { assert, hexToU8a, isNumber, isHex, stringToU8a } from '@polkadot/util'; -import { keyExtract, mnemonicToSeed , naclKeypairFromSeed as naclFromSeed, schnorrkelKeypairFromSeed as schnorrkelFromSeed, mnemonicToMiniSecret, keyFromPath } from '@polkadot/util-crypto'; +import { keyExtractSuri, mnemonicToSeed , naclKeypairFromSeed as naclFromSeed, schnorrkelKeypairFromSeed as schnorrkelFromSeed, mnemonicToMiniSecret, keyFromPath } from '@polkadot/util-crypto'; import { decodeAddress, encodeAddress, setAddressPrefix } from './address'; import createPair from './pair'; @@ -135,7 +135,18 @@ export default class Keyring implements KeyringInstance { * @description Extracts the phrase, path and password from a SURI format for specifying secret keys `//////` (the `///password` may be omitted, and `/` and `//` maybe repeated and mixed). The secret can be a hex string, mnemonic phrase or a string (to be padded) */ addFromUri (suri: string, meta: KeyringPair$Meta = {}, type: KeypairType = this.type): KeyringPair { - const { password, phrase, path } = keyExtract(suri); + return this.addPair( + this.createFromUri(suri, meta, type) + ); + } + + /** + * @name createFromUri + * @summry Creates a Keypair from an suri + * @description This creates a pair from the suri, but does not add it to the keyring + */ + createFromUri (suri: string, meta: KeyringPair$Meta = {}, type: KeypairType = this.type): KeyringPair { + const { password, phrase, path } = keyExtractSuri(suri); let seed; if (isHex(phrase, 256)) { @@ -156,7 +167,7 @@ export default class Keyring implements KeyringInstance { : naclFromSeed(seed); const derived = keyFromPath(keypair, path, type); - return this.addPair(createPair(type, derived, meta, null)); + return createPair(type, derived, meta, null); } /** diff --git a/packages/keyring/src/types.ts b/packages/keyring/src/types.ts index e88d8511a..4cb2ad3b5 100644 --- a/packages/keyring/src/types.ts +++ b/packages/keyring/src/types.ts @@ -67,6 +67,7 @@ export interface KeyringInstance { addFromMnemonic (mnemonic: string, meta?: KeyringPair$Meta, type?: KeypairType): KeyringPair; addFromSeed (seed: Uint8Array, meta?: KeyringPair$Meta, type?: KeypairType): KeyringPair; addFromUri (suri: string, meta?: KeyringPair$Meta, type?: KeypairType): KeyringPair; + createFromUri (suri: string, meta?: KeyringPair$Meta, type?: KeypairType): KeyringPair; getPair (address: string | Uint8Array): KeyringPair; getPairs (): Array; getPublicKeys (): Array; diff --git a/packages/util-crypto/src/key/extractPath.spec.ts b/packages/util-crypto/src/key/extractPath.spec.ts new file mode 100644 index 000000000..6e4acd064 --- /dev/null +++ b/packages/util-crypto/src/key/extractPath.spec.ts @@ -0,0 +1,47 @@ +// Copyright 2017-2019 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import keyExtractPath from './extractPath'; + +describe('keyExtractPath', () => { + it('extracts properly from soft', () => { + expect( + keyExtractPath('/1') + ).toEqual({ + parts: ['/1'], + path: [ + { _isHard: false, _chainCode: Uint8Array.from([1, 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]) } + ] + }); + }); + + it('extracts properly from hard', () => { + expect( + keyExtractPath('//1') + ).toEqual({ + parts: ['//1'], + path: [ + { _isHard: true, _chainCode: Uint8Array.from([1, 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]) } + ] + }); + }); + + it('extracts properly from hard/soft', () => { + expect( + keyExtractPath('//1/2') + ).toEqual({ + parts: ['//1', '/2'], + path: [ + { _isHard: true, _chainCode: Uint8Array.from([1, 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]) }, + { _isHard: false, _chainCode: Uint8Array.from([2, 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]) } + ] + }); + }); + + it('does not extract from invalid paths', () => { + expect( + () => keyExtractPath('1/2') + ).toThrow(/does not match input/); + }); +}); diff --git a/packages/util-crypto/src/key/extractPath.ts b/packages/util-crypto/src/key/extractPath.ts new file mode 100644 index 000000000..92f3a34fe --- /dev/null +++ b/packages/util-crypto/src/key/extractPath.ts @@ -0,0 +1,36 @@ +// Copyright 2017-2019 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import DeriveJunction from './DeriveJunction'; +import { assert } from '@polkadot/util'; + +const RE_JUNCTION = /\/(\/?)([^\/]+)/g; + +type ExtractResult = { + parts: null | Array, + path: Array +}; + +/** + * @description Extract derivation juntions from the supplied path + */ +export default function keyExtractPath (derivePath: string): ExtractResult { + const parts = derivePath.match(RE_JUNCTION); + const path: Array = []; + + if (parts) { + const constructed = parts.join(''); + + assert(constructed === derivePath, `Re-constructed path "${constructed}" does not match input`); + + parts.forEach((value: string) => { + path.push(DeriveJunction.from(value.substr(1))); + }); + } + + return { + parts, + path + }; +} diff --git a/packages/util-crypto/src/key/extract.spec.ts b/packages/util-crypto/src/key/extractSuri.spec.ts similarity index 83% rename from packages/util-crypto/src/key/extract.spec.ts rename to packages/util-crypto/src/key/extractSuri.spec.ts index c8c7a5b69..509f059dd 100644 --- a/packages/util-crypto/src/key/extract.spec.ts +++ b/packages/util-crypto/src/key/extractSuri.spec.ts @@ -2,12 +2,18 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import keyExtract from './extract'; +import keyExtractSuri from './extractSuri'; + +describe('keyExtractSuri', () => { + it('does not extract from invalid suri', () => { + expect( + () => keyExtractSuri('//2') + ).toThrow(/to a secret URI/); + }); -describe('keyExtract', () => { it('derives on "hello world"', () => { expect( - keyExtract('hello world') + keyExtractSuri('hello world') ).toEqual({ path: [], phrase: 'hello world' @@ -16,7 +22,7 @@ describe('keyExtract', () => { it('derives on "hello world/1', () => { expect( - keyExtract('hello world/1') + keyExtractSuri('hello world/1') ).toEqual({ path: [ { _isHard: false, _chainCode: Uint8Array.from([1, 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]) } @@ -27,7 +33,7 @@ describe('keyExtract', () => { it('derives on "hello world/DOT', () => { expect( - keyExtract('hello world/DOT') + keyExtractSuri('hello world/DOT') ).toEqual({ path: [ { _isHard: false, _chainCode: Uint8Array.from([12, 68, 79, 84, 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]) } @@ -38,7 +44,7 @@ describe('keyExtract', () => { it('derives on "hello world//1', () => { expect( - keyExtract('hello world//1') + keyExtractSuri('hello world//1') ).toEqual({ path: [ { _isHard: true, _chainCode: Uint8Array.from([1, 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]) } @@ -49,7 +55,7 @@ describe('keyExtract', () => { it('derives on "hello world//DOT', () => { expect( - keyExtract('hello world//DOT') + keyExtractSuri('hello world//DOT') ).toEqual({ path: [ { _isHard: true, _chainCode: Uint8Array.from([12, 68, 79, 84, 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]) } @@ -60,7 +66,7 @@ describe('keyExtract', () => { it('derives on "hello world//1/DOT', () => { expect( - keyExtract('hello world//1/DOT') + keyExtractSuri('hello world//1/DOT') ).toEqual({ path: [ { _isHard: true, _chainCode: Uint8Array.from([1, 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]) }, @@ -72,7 +78,7 @@ describe('keyExtract', () => { it('derives on "hello world//DOT/1', () => { expect( - keyExtract('hello world//DOT/1') + keyExtractSuri('hello world//DOT/1') ).toEqual({ path: [ { _isHard: true, _chainCode: Uint8Array.from([12, 68, 79, 84, 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]) }, @@ -84,7 +90,7 @@ describe('keyExtract', () => { it('derives on "hello world///password"', () => { expect( - keyExtract('hello world///password') + keyExtractSuri('hello world///password') ).toEqual({ password: 'password', path: [], @@ -94,7 +100,7 @@ describe('keyExtract', () => { it('derives on "hello world//1/DOT///password"', () => { expect( - keyExtract('hello world//1/DOT///password') + keyExtractSuri('hello world//1/DOT///password') ).toEqual({ path: [ { _isHard: true, _chainCode: Uint8Array.from([1, 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]) }, @@ -107,7 +113,7 @@ describe('keyExtract', () => { it('derives on "hello world/1//DOT///password"', () => { expect( - keyExtract('hello world/1//DOT///password') + keyExtractSuri('hello world/1//DOT///password') ).toEqual({ path: [ { _isHard: false, _chainCode: Uint8Array.from([1, 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]) }, @@ -120,7 +126,7 @@ describe('keyExtract', () => { it('derives on actual Alice', () => { expect( - keyExtract('bottom drive obey lake curtain smoke basket hold race lonely fit walk//Alice') + keyExtractSuri('bottom drive obey lake curtain smoke basket hold race lonely fit walk//Alice') ).toEqual({ path: [ { _isHard: true, _chainCode: Uint8Array.from([20, 65, 108, 105, 99, 101, 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]) } diff --git a/packages/util-crypto/src/key/extract.ts b/packages/util-crypto/src/key/extractSuri.ts similarity index 67% rename from packages/util-crypto/src/key/extract.ts rename to packages/util-crypto/src/key/extractSuri.ts index cd6f41f0c..cfb800d9f 100644 --- a/packages/util-crypto/src/key/extract.ts +++ b/packages/util-crypto/src/key/extractSuri.ts @@ -5,33 +5,26 @@ import { assert, isNull } from '@polkadot/util'; import DeriveJunction from './DeriveJunction'; +import keyExtractPath from './extractPath'; -type DeriveResult = { +type ExtractResult = { password?: string, path: Array, phrase: string }; const RE_CAPTURE = /^(\w+( \w+)*)((\/\/?[^\/]+)*)(\/\/\/(.*))?$/; -const RE_JUNCTION = /\/(\/?)([^\/]+)/g; /** * @description Extracts the phrase, path and password from a SURI format for specifying secret keys `//////` (the `///password` may be omitted, and `/` and `//` maybe repeated and mixed). */ -export default function keyExtract (suri: string): DeriveResult { +export default function keyExtract (suri: string): ExtractResult { const matches = suri.match(RE_CAPTURE); assert(!isNull(matches), `Unable to match '${suri}' to a secret URI`); - const [, phrase, , junctions, , , password] = matches as Array; - const parts = junctions.match(RE_JUNCTION); - const path: Array = []; - - if (parts) { - parts.forEach((value: string) => { - path.push(DeriveJunction.from(value.substr(1))); - }); - } + const [, phrase, , derivePath, , , password] = matches as Array; + const { path } = keyExtractPath(derivePath); return { password, diff --git a/packages/util-crypto/src/key/index.ts b/packages/util-crypto/src/key/index.ts index 9db936f53..9dd50ec3d 100644 --- a/packages/util-crypto/src/key/index.ts +++ b/packages/util-crypto/src/key/index.ts @@ -5,7 +5,8 @@ /** * @summary Create keys from paths, seeds and password */ -export { default as keyExtract } from './extract'; +export { default as keyExtractPath } from './extractPath'; +export { default as keyExtractSuri } from './extractSuri'; export { default as keyFromPath } from './fromPath'; export { default as keyHdkdEd25519 } from './hdkdEd25519'; export { default as keyHdkdSr25519 } from './hdkdEd25519';