Skip to content

Commit

Permalink
Merge pull request #30 from shoutcool/add-bip39-and-bip32-support
Browse files Browse the repository at this point in the history
added bip39 and bip32 support
  • Loading branch information
JeffWScott authored Aug 31, 2021
2 parents 46f98c0 + 463f510 commit d0a9c2b
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 2,806 deletions.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,43 @@ console.log(lamdenWallet)
}
```

### Create a new BIP39 / BIP 32 compatible wallet
- **BIP39** = 24 seed phrase
- **BIP32** = derivation path

```javascript
let lamdenWallet = Lamden.wallet.new_wallet_bip39()

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'
}

```

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

```


### Get a public key (vk) from a private key (sk)
Takes the sk as an argument and returns the vk
```javascript
Expand Down Expand Up @@ -316,4 +353,4 @@ tx.sign(null, keystore.wallets[0])

// Send transaction
tx.send()
```
```
59 changes: 59 additions & 0 deletions dist/lamden.js
Original file line number Diff line number Diff line change
Expand Up @@ -2359,6 +2359,8 @@ var utils = /*#__PURE__*/Object.freeze({
});

const nacl = require('tweetnacl');
const bip39 = require('bip39');
const bip32 = require('ed25519-hd-key');

/**
* Create a wallet object for signing and verifying messages
Expand Down Expand Up @@ -2476,6 +2478,62 @@ function new_wallet(seed = null) {
const keys = generate_keys(seed);
return keys_to_format(keys);
}

/**
*
* @param mnemonic 24 word seed phrase
* @param derivationIndex bip32 derivation key index
* @returns {{derivationIndex: number, vk: string, sk: string, mnemonic: (string|undefined)}}
* 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
*/
function generate_keys_bip39(mnemonic = undefined, derivationIndex = 0) {
let finalMnemonic;

if (mnemonic !== undefined){
finalMnemonic = mnemonic;
}else {
finalMnemonic = bip39.generateMnemonic(256);
}

const seed = bip39.mnemonicToSeedSync(finalMnemonic).toString('hex');

const derivationPath = "m/44'/789'/" + derivationIndex + "'/0'/0'";
const { key, chainCode } = bip32.derivePath(derivationPath, seed, 0x80000000);

const privateKey = key.toString('hex');
const publicKey = bip32.getPublicKey(key, false).toString('hex');

if (publicKey !== get_vk(privateKey)){
throw Error('Bip32 public key does not match with Lamden public key!')
}

return {
sk: privateKey,
vk: publicKey,
derivationIndex: derivationIndex,
mnemonic: 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
*
* @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)
*/
function new_wallet_bip39(mnemonic = undefined, derivationIndex = 0) {
return generate_keys_bip39(mnemonic, derivationIndex);
}

/**
* @param String sk
* @param Uint8Array msg
Expand Down Expand Up @@ -2525,6 +2583,7 @@ var wallet = /*#__PURE__*/Object.freeze({
format_to_keys: format_to_keys,
keys_to_format: keys_to_format,
new_wallet: new_wallet,
new_wallet_bip39: new_wallet_bip39,
sign: sign,
verify: verify
});
Expand Down
Loading

0 comments on commit d0a9c2b

Please sign in to comment.