From 04bbf28a57bad5309defa1d32feb4ac8e468f6be Mon Sep 17 00:00:00 2001 From: Antoine Estienne Date: Mon, 1 Mar 2021 17:06:12 +0100 Subject: [PATCH] Jlm test from seed (#909) * added gerald test address * add test for fromSeed * Update packages/util-crypto/src/secp256k1/keypair/fromSeed.spec.ts Co-authored-by: Jaco Greeff * coorected createfromuri to accept eth priv key * Update packages/keyring/src/keyring.ts Co-authored-by: Jaco Greeff * add isPhraeHex Co-authored-by: Jaco Greeff --- packages/keyring/src/index.spec.ts | 17 +++++++++++++++++ packages/keyring/src/keyring.ts | 9 ++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/keyring/src/index.spec.ts b/packages/keyring/src/index.spec.ts index 26aebf290..2ff56620e 100644 --- a/packages/keyring/src/index.spec.ts +++ b/packages/keyring/src/index.spec.ts @@ -306,6 +306,7 @@ describe('keypair', (): void => { describe('ethereum', (): void => { // combine mnemonic with derivation path const PHRASE = 'seed sock milk update focus rotate barely fade car face mechanic mercy' + '/m/44\'/60\'/0\'/0/0'; + const PRIV_KEY_ONE = '0x070dc3117300011918e26b02176945cc15c3d548cf49fd8418d97f93af699e46'; const ETH_ADDRESS_ONE = '0x31ea8795EE32D782C8ff41a5C68Dcbf0F5B27f6d'; let keyring: Keyring; @@ -314,6 +315,22 @@ describe('keypair', (): void => { keyring = new Keyring({ type: 'ethereum' }); }); + it('creates with dev phrase from the private key', (): void => { + const pair = keyring.addFromSeed(hexToU8a(PRIV_KEY_ONE)); + + expect( + pair.address + ).toEqual(ETH_ADDRESS_ONE); + }); + + it('creates with dev phrase from the private key in createFromUri', (): void => { + const pair = keyring.createFromUri(PRIV_KEY_ONE); + + expect( + pair.address + ).toEqual(ETH_ADDRESS_ONE); + }); + it('creates with dev phrase with derivation path specified', (): void => { const pair = keyring.createFromUri(PHRASE); diff --git a/packages/keyring/src/keyring.ts b/packages/keyring/src/keyring.ts index 5f7d3f902..9cd6c706c 100644 --- a/packages/keyring/src/keyring.ts +++ b/packages/keyring/src/keyring.ts @@ -175,11 +175,12 @@ export class Keyring implements KeyringInstance { : _suri; const { derivePath, password, path, phrase } = keyExtractSuri(suri); let seed: Uint8Array; + const isPhraseHex = isHex(phrase, 256); - if (isHex(phrase, 256)) { + if (isPhraseHex) { seed = hexToU8a(phrase); } else { - const str = phrase as string; + const str = phrase; const parts = str.split(' '); if ([12, 15, 18, 21, 24].includes(parts.length)) { @@ -194,7 +195,9 @@ export class Keyring implements KeyringInstance { } const derived = type === 'ethereum' - ? hdEthereum(seed, derivePath.substring(1)) + ? isPhraseHex + ? keypairFromSeed[type](seed) // for eth, if the private key is provided as suri, it must be derived only once + : hdEthereum(seed, derivePath.substring(1)) : keyFromPath(keypairFromSeed[type](seed), path, type); return createPair({ toSS58: this.encodeAddress, type }, derived, meta, null);