From cbe8fce6f9798dfdb9c5575b5b189e7b32c3e814 Mon Sep 17 00:00:00 2001 From: shoutcool Date: Mon, 6 Sep 2021 17:25:40 +0200 Subject: [PATCH 1/2] improved bip39 api --- README.md | 8 +++++--- dist/lamden.js | 33 +++++++++++++++++---------------- src/js/wallet.js | 30 ++++++++++++++++++------------ test/wallet-test.js | 11 +++++++---- 4 files changed, 47 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 28160f5..6b084f7 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ console.log(lamdenWallet) ``` ### Create a new BIP39 / BIP 32 compatible wallet -- **BIP39** = 24 seed phrase +- **BIP39** = 24 word mnemonic - **BIP32** = derivation path ```javascript @@ -49,6 +49,7 @@ console.log(lamdenWallet) sk: 'a6b72cb3d1160c26f9f39a8f1d4a3c7c0da2ac59d193b66ac5f919ec77f28915', vk: '53d016586ce35c5f6ea581cadf4693dd2850621dfad6a2261e8dd311c83e11d5', derivationIndex: 0, + seed: '3626c59ee5bce833a8bf5024645eb10415b39c6f9fd0ff0fb1b00b8ca9fd6ff4b8a0ed7077296cdaff1b955f03318f244dfd3fead404d93f11a3f301c0e3e1c6', mnemonic: 'evidence rifle behave normal duty mean junk chicken salute relief raw chunk region ocean guard swarm taste toy loop ozone spell crumble apart echo' } @@ -59,7 +60,7 @@ console.log(lamdenWallet) - **BIP32** = derivation path ```javascript -const mnemonic = 'evidence rifle behave normal duty mean junk chicken salute relief raw chunk region ocean guard swarm taste toy loop ozone spell crumble apart echo' +const seed = '3626c59ee5bce833a8bf5024645eb10415b39c6f9fd0ff0fb1b00b8ca9fd6ff4b8a0ed7077296cdaff1b955f03318f244dfd3fead404d93f11a3f301c0e3e1c6' const derivationIndex = 0; let lamdenWallet = Lamden.wallet.new_wallet_bip39(mnemonic, derivationIndex) @@ -68,7 +69,8 @@ console.log(lamdenWallet) sk: 'a6b72cb3d1160c26f9f39a8f1d4a3c7c0da2ac59d193b66ac5f919ec77f28915', vk: '53d016586ce35c5f6ea581cadf4693dd2850621dfad6a2261e8dd311c83e11d5', derivationIndex: 0, - mnemonic: 'evidence rifle behave normal duty mean junk chicken salute relief raw chunk region ocean guard swarm taste toy loop ozone spell crumble apart echo' + seed: null, + mnemonic: null } ``` diff --git a/dist/lamden.js b/dist/lamden.js index fd31506..5d5c545 100644 --- a/dist/lamden.js +++ b/dist/lamden.js @@ -2483,26 +2483,26 @@ function new_wallet(seed = null) { * * @param mnemonic 24 word seed phrase * @param derivationIndex bip32 derivation key index - * @returns {{derivationIndex: number, vk: string, sk: string, mnemonic: (string|undefined)}} + * @returns {{derivationIndex: number, vk: string, sk: string, mnemonic: string}} * derivationIndex: bip32 derivation key index * vk: Verify Key (VK) represented as a 64 character hex string * sk: Signing Key (SK) represented as a 64 character hex string - * mnemonic: 24 word seed phrase - + * seed: Bip39 seed phrase (128 characters in hex) + * mnemonic: Bip39 24 words mnemonic */ -function generate_keys_bip39(mnemonic = undefined, derivationIndex = 0) { +function generate_keys_bip39(seed = undefined, derivationIndex = 0) { + let finalSeed; let finalMnemonic; - if (mnemonic !== undefined){ - finalMnemonic = mnemonic; + if (seed !== undefined){ + finalSeed = seed; }else { finalMnemonic = bip39.generateMnemonic(256); + finalSeed = bip39.mnemonicToSeedSync(finalMnemonic).toString('hex'); } - const seed = bip39.mnemonicToSeedSync(finalMnemonic).toString('hex'); - const derivationPath = "m/44'/789'/" + derivationIndex + "'/0'/0'"; - const { key, chainCode } = bip32.derivePath(derivationPath, seed, 0x80000000); + const { key, chainCode } = bip32.derivePath(derivationPath, finalSeed, 0x80000000); const privateKey = key.toString('hex'); const publicKey = bip32.getPublicKey(key, false).toString('hex'); @@ -2515,23 +2515,24 @@ function generate_keys_bip39(mnemonic = undefined, derivationIndex = 0) { sk: privateKey, vk: publicKey, derivationIndex: derivationIndex, - mnemonic: finalMnemonic + seed: seed !== undefined ? null : finalSeed, + mnemonic: seed !== undefined ? null : finalMnemonic, } } /** - * @param Uint8Array(length: 32) seed - * seed: A Uint8Array with a length of 32 to seed the keyPair with. This is advanced behavior and should be - * avoided by everyday users + * @param mnemonic 24 word seed phrase + * @param derivationIndex bip32 derivation key index * * @return {{derivationIndex: number, vk: string, sk: string, mnemonic: (string|undefined)}} { sk, vk, derivationIndex, mnemonic } * sk: Signing Key (SK) represented as a 64 character hex string * vk: Verify Key (VK) represented as a 64 character hex string * derivationIndex: Bip32 derivation index - * mnemonic: 24 word seed phrase (just returned if method was called without existing mnemonic) + * seed: Bip39 seed phrase (128 characters in hex) + * mnemonic: Bip39 24 words mnemonic */ -function new_wallet_bip39(mnemonic = undefined, derivationIndex = 0) { - return generate_keys_bip39(mnemonic, derivationIndex); +function new_wallet_bip39(seed = undefined, derivationIndex = 0) { + return generate_keys_bip39(seed, derivationIndex); } /** diff --git a/src/js/wallet.js b/src/js/wallet.js index 7a39c00..0cdf0b8 100644 --- a/src/js/wallet.js +++ b/src/js/wallet.js @@ -129,22 +129,22 @@ export function new_wallet(seed = null) { * derivationIndex: bip32 derivation key index * vk: Verify Key (VK) represented as a 64 character hex string * sk: Signing Key (SK) represented as a 64 character hex string - * mnemonic: 24 word seed phrase - + * seed: Bip39 seed phrase (128 characters in hex) + * mnemonic: Bip39 24 words mnemonic */ -function generate_keys_bip39(mnemonic = undefined, derivationIndex = 0) { +function generate_keys_bip39(seed = undefined, derivationIndex = 0) { + let finalSeed; let finalMnemonic; - if (mnemonic !== undefined){ - finalMnemonic = mnemonic; + if (seed !== undefined){ + finalSeed = seed; }else { finalMnemonic = bip39.generateMnemonic(256) + finalSeed = bip39.mnemonicToSeedSync(finalMnemonic).toString('hex'); } - const seed = bip39.mnemonicToSeedSync(finalMnemonic).toString('hex'); - const derivationPath = "m/44'/789'/" + derivationIndex + "'/0'/0'"; - const { key, chainCode } = bip32.derivePath(derivationPath, seed, 0x80000000); + const { key, chainCode } = bip32.derivePath(derivationPath, finalSeed, 0x80000000); const privateKey = key.toString('hex'); const publicKey = bip32.getPublicKey(key, false).toString('hex'); @@ -153,11 +153,16 @@ function generate_keys_bip39(mnemonic = undefined, derivationIndex = 0) { throw Error('Bip32 public key does not match with Lamden public key!') } + if (finalMnemonic !== undefined){ + + } + return { sk: privateKey, vk: publicKey, derivationIndex: derivationIndex, - mnemonic: finalMnemonic + seed: seed !== undefined ? null : finalSeed, + mnemonic: seed !== undefined ? null : finalMnemonic, } } @@ -169,10 +174,11 @@ function generate_keys_bip39(mnemonic = undefined, derivationIndex = 0) { * sk: Signing Key (SK) represented as a 64 character hex string * vk: Verify Key (VK) represented as a 64 character hex string * derivationIndex: Bip32 derivation index - * mnemonic: 24 word seed phrase + * seed: Bip39 seed phrase (128 characters in hex) + * mnemonic: Bip39 24 words mnemonic */ -export function new_wallet_bip39(mnemonic = undefined, derivationIndex = 0) { - return generate_keys_bip39(mnemonic, derivationIndex); +export function new_wallet_bip39(seed = undefined, derivationIndex = 0) { + return generate_keys_bip39(seed, derivationIndex); } /** diff --git a/test/wallet-test.js b/test/wallet-test.js index 4924768..a4861c8 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -17,18 +17,21 @@ describe('Test Lamden Wallet methods', () => { expect( validateTypes.isStringWithValue(newWallet.mnemonic) ).to.be( true ) expect( validateTypes.isNumber(newWallet.derivationIndex) ).to.be( true ) expect( newWallet.derivationIndex ).to.be( 0 ) + expect( validateTypes.isStringHex(newWallet.seed) ).to.be( true ) + expect( newWallet.seed.length ).to.be( 128 ) }), - it('creates a bip39 / bip32 compatible lamden keypair from mnemonic', () => { - const mnemonic = 'ripple junk access broom element fitness side example ramp flush model creek nest face rent jacket ahead come short find over family wise comfort' + it('creates a bip39 / bip32 compatible lamden keypair from seed', () => { + const seed = 'd3ad26bd89d54d0c22bb32d34ea9f06c567ba060d8e1518974d807180b886c643bfb7f455bd3db2c767a17c089aab20db97cf0f0184d730b9d20be0c7b6cc6cc' const derivationIndex = 127 - let newWallet = wallet.new_wallet_bip39(mnemonic, derivationIndex); + let newWallet = wallet.new_wallet_bip39(seed, derivationIndex); expect( validateTypes.isStringHex(newWallet.vk) ).to.be( true ) expect( newWallet.vk ).to.be( 'd0d2de909bf7c2be3bafbcb3af0b1c50487b80ba48b5700bff35bb927921c607' ) expect( validateTypes.isStringHex(newWallet.sk) ).to.be( true ) expect( newWallet.sk ).to.be( '86c77748edc039c672cf761d2db1e52d6255b16cd4d626d4b66c67eb224287a8' ) - expect( newWallet.mnemonic ).to.be( mnemonic ) + expect( newWallet.mnemonic ).to.be( null ) + expect( newWallet.seed ).to.be( null ) expect( validateTypes.isNumber(newWallet.derivationIndex) ).to.be( true ) expect( newWallet.derivationIndex ).to.be( 127 ) }) From c15e4813cb7f974a8517d56300032885754c651a Mon Sep 17 00:00:00 2001 From: shoutcool Date: Mon, 6 Sep 2021 17:40:38 +0200 Subject: [PATCH 2/2] fixed doco --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6b084f7..bf7f2fb 100644 --- a/README.md +++ b/README.md @@ -56,13 +56,13 @@ console.log(lamdenWallet) ``` ### Restore a BIP39 / BIP 32 compatible wallet -- **BIP39** = 24 seed phrase +- **BIP39** = 24 word mnemonic - **BIP32** = derivation path ```javascript const seed = '3626c59ee5bce833a8bf5024645eb10415b39c6f9fd0ff0fb1b00b8ca9fd6ff4b8a0ed7077296cdaff1b955f03318f244dfd3fead404d93f11a3f301c0e3e1c6' const derivationIndex = 0; -let lamdenWallet = Lamden.wallet.new_wallet_bip39(mnemonic, derivationIndex) +let lamdenWallet = Lamden.wallet.new_wallet_bip39(seed, derivationIndex) console.log(lamdenWallet) >> {