Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Add fromMnemonic function #192

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions docs/classes/ethereumhdkey.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [publicExtendedKey](ethereumhdkey.md#publicextendedkey)
- [fromExtendedKey](ethereumhdkey.md#fromextendedkey)
- [fromMasterSeed](ethereumhdkey.md#frommasterseed)
- [fromMnemonic](ethereumhdkey.md#frommnemonic)

---

Expand All @@ -36,7 +37,7 @@

⊕ **new EthereumHDKey**(\_hdkey: _`any`_): [EthereumHDKey](ethereumhdkey.md)

_Defined in [hdkey.ts:21](https://github.com/ethereumjs/ethereumjs-wallet/blob/13fb20d/src/hdkey.ts#L21)_
_Defined in [hdkey.ts:27](https://github.com/ethereumjs/ethereumjs-wallet/blob/7b6ac09/src/hdkey.ts#L27)_

**Parameters:**

Expand All @@ -56,7 +57,7 @@ _Defined in [hdkey.ts:21](https://github.com/ethereumjs/ethereumjs-wallet/blob/1

**● \_hdkey**: _`any`_

_Defined in [hdkey.ts:23](https://github.com/ethereumjs/ethereumjs-wallet/blob/13fb20d/src/hdkey.ts#L23)_
_Defined in [hdkey.ts:27](https://github.com/ethereumjs/ethereumjs-wallet/blob/7b6ac09/src/hdkey.ts#L27)_

---

Expand All @@ -68,7 +69,7 @@ _Defined in [hdkey.ts:23](https://github.com/ethereumjs/ethereumjs-wallet/blob/1

▸ **deriveChild**(index: _`number`_): [EthereumHDKey](ethereumhdkey.md)

_Defined in [hdkey.ts:52](https://github.com/ethereumjs/ethereumjs-wallet/blob/13fb20d/src/hdkey.ts#L52)_
_Defined in [hdkey.ts:56](https://github.com/ethereumjs/ethereumjs-wallet/blob/7b6ac09/src/hdkey.ts#L56)_

**Parameters:**

Expand All @@ -86,7 +87,7 @@ _Defined in [hdkey.ts:52](https://github.com/ethereumjs/ethereumjs-wallet/blob/1

▸ **derivePath**(path: _`string`_): [EthereumHDKey](ethereumhdkey.md)

_Defined in [hdkey.ts:45](https://github.com/ethereumjs/ethereumjs-wallet/blob/13fb20d/src/hdkey.ts#L45)_
_Defined in [hdkey.ts:49](https://github.com/ethereumjs/ethereumjs-wallet/blob/7b6ac09/src/hdkey.ts#L49)_

**Parameters:**

Expand All @@ -104,7 +105,7 @@ _Defined in [hdkey.ts:45](https://github.com/ethereumjs/ethereumjs-wallet/blob/1

▸ **getWallet**(): [Wallet](wallet.md)

_Defined in [hdkey.ts:59](https://github.com/ethereumjs/ethereumjs-wallet/blob/13fb20d/src/hdkey.ts#L59)_
_Defined in [hdkey.ts:63](https://github.com/ethereumjs/ethereumjs-wallet/blob/7b6ac09/src/hdkey.ts#L63)_

**Returns:** [Wallet](wallet.md)

Expand All @@ -116,7 +117,7 @@ _Defined in [hdkey.ts:59](https://github.com/ethereumjs/ethereumjs-wallet/blob/1

▸ **privateExtendedKey**(): `Buffer`

_Defined in [hdkey.ts:28](https://github.com/ethereumjs/ethereumjs-wallet/blob/13fb20d/src/hdkey.ts#L28)_
_Defined in [hdkey.ts:32](https://github.com/ethereumjs/ethereumjs-wallet/blob/7b6ac09/src/hdkey.ts#L32)_

**Returns:** `Buffer`

Expand All @@ -128,7 +129,7 @@ _Defined in [hdkey.ts:28](https://github.com/ethereumjs/ethereumjs-wallet/blob/1

▸ **publicExtendedKey**(): `Buffer`

_Defined in [hdkey.ts:38](https://github.com/ethereumjs/ethereumjs-wallet/blob/13fb20d/src/hdkey.ts#L38)_
_Defined in [hdkey.ts:42](https://github.com/ethereumjs/ethereumjs-wallet/blob/7b6ac09/src/hdkey.ts#L42)_

**Returns:** `Buffer`

Expand All @@ -140,7 +141,7 @@ _Defined in [hdkey.ts:38](https://github.com/ethereumjs/ethereumjs-wallet/blob/1

▸ **fromExtendedKey**(base58Key: _`string`_): [EthereumHDKey](ethereumhdkey.md)

_Defined in [hdkey.ts:19](https://github.com/ethereumjs/ethereumjs-wallet/blob/13fb20d/src/hdkey.ts#L19)_
_Defined in [hdkey.ts:23](https://github.com/ethereumjs/ethereumjs-wallet/blob/7b6ac09/src/hdkey.ts#L23)_

**Parameters:**

Expand All @@ -158,7 +159,7 @@ _Defined in [hdkey.ts:19](https://github.com/ethereumjs/ethereumjs-wallet/blob/1

▸ **fromMasterSeed**(seedBuffer: _`Buffer`_): [EthereumHDKey](ethereumhdkey.md)

_Defined in [hdkey.ts:12](https://github.com/ethereumjs/ethereumjs-wallet/blob/13fb20d/src/hdkey.ts#L12)_
_Defined in [hdkey.ts:9](https://github.com/ethereumjs/ethereumjs-wallet/blob/7b6ac09/src/hdkey.ts#L9)_

**Parameters:**

Expand All @@ -169,3 +170,22 @@ _Defined in [hdkey.ts:12](https://github.com/ethereumjs/ethereumjs-wallet/blob/1
**Returns:** [EthereumHDKey](ethereumhdkey.md)

---

<a id="frommnemonic"></a>

### `<Static>` fromMnemonic

▸ **fromMnemonic**(mnemonic: _`string`_, passphrase: _`string`_): [EthereumHDKey](ethereumhdkey.md)

_Defined in [hdkey.ts:16](https://github.com/ethereumjs/ethereumjs-wallet/blob/7b6ac09/src/hdkey.ts#L16)_

**Parameters:**

| Name | Type |
| ---------- | -------- |
| mnemonic | `string` |
| `Optional` passphrase | `string` |

**Returns:** [EthereumHDKey](ethereumhdkey.md)

---
11 changes: 8 additions & 3 deletions src/hdkey.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { HDKey } from 'ethereum-cryptography/hdkey'
import { mnemonicToSeedSync } from 'ethereum-cryptography/bip39'
import Wallet from './index'

export default class EthereumHDKey {
/**
* Creates an instance based on a seed.
*
* For the seed we suggest to use [bip39](https://npmjs.org/package/bip39) to
* create one from a BIP39 mnemonic.
*/
public static fromMasterSeed(seedBuffer: Buffer): EthereumHDKey {
return new EthereumHDKey(HDKey.fromMasterSeed(seedBuffer))
}

/**
* Creates an instance based on BIP39 mnemonic phrases
*/
public static fromMnemonic(mnemonic: string, passphrase?: string): EthereumHDKey {
return EthereumHDKey.fromMasterSeed(Buffer.from(mnemonicToSeedSync(mnemonic, passphrase)))
}

/**
* Create an instance based on a BIP32 extended private or public key.
*/
Expand Down
20 changes: 20 additions & 0 deletions test/hdkey.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const fixtureseed = Buffer.from(
'hex'
)
const fixturehd = EthereumHDKey.fromMasterSeed(fixtureseed)
const fixtureMnemonic = 'awake book subject inch gentle blur grant damage process float month clown'
const fixtureAddress = '0x4dcccf58c6573eb896250b0c9647a40c1673af44'
const fixturePrivateKey = '0xf62a8ea4ab7025d151ccd84981c66278d0d3cd58ff837467cdc51229915a22d1'

describe('.fromMasterSeed()', function () {
it('should work', function () {
Expand All @@ -16,6 +19,23 @@ describe('.fromMasterSeed()', function () {
})
})

describe('.fromMnemonic()', function () {
it('should work', function () {
assert.doesNotThrow(function () {
EthereumHDKey.fromMnemonic(fixtureMnemonic)
})
})

it('should match', function () {
const path = "m/44'/60'/0'/0/0"
const hdWallet = EthereumHDKey.fromMnemonic(fixtureMnemonic)
const wallet = hdWallet.derivePath(path).getWallet()

assert.strictEqual(wallet.getPrivateKeyString(), fixturePrivateKey)
assert.strictEqual(wallet.getAddressString(), fixtureAddress)
})
})

describe('.privateExtendedKey()', function () {
it('should work', function () {
assert.strictEqual(
Expand Down