Utility function to extract from path only (#376)
* Utility function to extract from path only * Add createFromUri
This commit is contained in:
@@ -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 `<secret>/<soft-key>//<hard-key>///<password>` (the `///password` may be omitted, and `/<soft-key>` and `//<hard-key>` 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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<KeyringPair>;
|
||||
getPublicKeys (): Array<Uint8Array>;
|
||||
|
||||
@@ -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/);
|
||||
});
|
||||
});
|
||||
@@ -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<string>,
|
||||
path: Array<DeriveJunction>
|
||||
};
|
||||
|
||||
/**
|
||||
* @description Extract derivation juntions from the supplied path
|
||||
*/
|
||||
export default function keyExtractPath (derivePath: string): ExtractResult {
|
||||
const parts = derivePath.match(RE_JUNCTION);
|
||||
const path: Array<DeriveJunction> = [];
|
||||
|
||||
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
|
||||
};
|
||||
}
|
||||
+19
-13
@@ -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]) }
|
||||
+5
-12
@@ -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<DeriveJunction>,
|
||||
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 `<secret>/<soft-key>//<hard-key>///<password>` (the `///password` may be omitted, and `/<soft-key>` and `//<hard-key>` 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<any>;
|
||||
const parts = junctions.match(RE_JUNCTION);
|
||||
const path: Array<DeriveJunction> = [];
|
||||
|
||||
if (parts) {
|
||||
parts.forEach((value: string) => {
|
||||
path.push(DeriveJunction.from(value.substr(1)));
|
||||
});
|
||||
}
|
||||
const [, phrase, , derivePath, , , password] = matches as Array<string>;
|
||||
const { path } = keyExtractPath(derivePath);
|
||||
|
||||
return {
|
||||
password,
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user