Skip to content

Commit

Permalink
Merge branch 'improve-bip39-api'
Browse files Browse the repository at this point in the history
  • Loading branch information
Dapiguabc committed Nov 26, 2021
2 parents 0c0937e + c15e481 commit 95fdaf8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 49 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -49,26 +49,28 @@ 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'
}

```

### Restore a BIP39 / BIP 32 compatible wallet
- **BIP39** = 24 seed phrase
- **BIP39** = 24 word mnemonic
- **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)
let lamdenWallet = Lamden.wallet.new_wallet_bip39(seed, derivationIndex)

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
}

```
Expand Down
50 changes: 28 additions & 22 deletions src/js/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,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) {
let finalMnemonic;
function generate_keys_bip39(seed = undefined, derivationIndex = 0) {
let finalSeed;
let finalMnemonic;

if (mnemonic !== undefined) {
finalMnemonic = mnemonic;
} else {
finalMnemonic = bip39.generateMnemonic(256);
}
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 derivationPath = "m/44'/789'/" + derivationIndex + "'/0'/0'";
const { key, chainCode } = bip32.derivePath(derivationPath, finalSeed, 0x80000000);

const privateKey = key.toString("hex");
const publicKey = bip32.getPublicKey(key, false).toString("hex");
Expand All @@ -151,12 +151,17 @@ function generate_keys_bip39(mnemonic = undefined, derivationIndex = 0) {
throw Error("Bip32 public key does not match with Lamden public key!");
}

return {
sk: privateKey,
vk: publicKey,
derivationIndex: derivationIndex,
mnemonic: finalMnemonic,
};
if (finalMnemonic !== undefined){

}

return {
sk: privateKey,
vk: publicKey,
derivationIndex: derivationIndex,
seed: seed !== undefined ? null : finalSeed,
mnemonic: seed !== undefined ? null : finalMnemonic,
}
}

/**
Expand All @@ -167,10 +172,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);
}

/**
Expand Down
46 changes: 24 additions & 22 deletions test/wallet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,32 @@ describe("Test Lamden Wallet methods", () => {
const derivationIndex = 127;
let newWallet = wallet.new_wallet_bip39(mnemonic, 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(validateTypes.isNumber(newWallet.derivationIndex)).to.be(true);
expect(newWallet.derivationIndex).to.be(127);
});
});
expect( validateTypes.isStringHex(newWallet.vk) ).to.be( true )
expect( newWallet.vk.length ).to.be( 64 )
expect( validateTypes.isStringHex(newWallet.sk) ).to.be( true )
expect( newWallet.sk.length ).to.be( 64 )
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 )
}),

context("wallet.new_wallet(): ", () => {
it("creates a lamden keypair", () => {
let newWallet = wallet.new_wallet();
it('creates a bip39 / bip32 compatible lamden keypair from seed', () => {
const seed = 'd3ad26bd89d54d0c22bb32d34ea9f06c567ba060d8e1518974d807180b886c643bfb7f455bd3db2c767a17c089aab20db97cf0f0184d730b9d20be0c7b6cc6cc'
const derivationIndex = 127
let newWallet = wallet.new_wallet_bip39(seed, derivationIndex);

expect(validateTypes.isStringHex(newWallet.vk)).to.be(true);
expect(newWallet.vk.length).to.be(64);
expect(validateTypes.isStringHex(newWallet.sk)).to.be(true);
expect(newWallet.sk.length).to.be(64);
});
});
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( null )
expect( newWallet.seed ).to.be( null )
expect( validateTypes.isNumber(newWallet.derivationIndex) ).to.be( true )
expect( newWallet.derivationIndex ).to.be( 127 )
})
})

context("wallet.get_vk(): ", () => {
it("can create a vk from an sk", () => {
Expand Down

0 comments on commit 95fdaf8

Please sign in to comment.