From 2d6064f8d16e73a2fee2c013cc9467677c8434b9 Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Wed, 19 Apr 2017 23:10:30 +1000
Subject: [PATCH] README: add examples for BIP39 wallet path derivation

---
 README.md                 |  3 ++-
 package.json              |  1 +
 test/integration/bip32.js | 16 ++++++++++++++++
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index fba5665..4e46df0 100644
--- a/README.md
+++ b/README.md
@@ -82,7 +82,7 @@ Example:
 uglifyjs ... --mangle --reserved 'Array,BigInteger,Boolean,ECPair,Function,Number,Point'
 ```
 
-**NOTE**: If you expect this library to run on an iOS 10 device, ensure that you are using [buffer@5.0.5](https://www.npmjs.com/package/buffer) or greater. 
+**NOTE**: If you expect this library to run on an iOS 10 device, ensure that you are using [buffer@5.0.5](https://www.npmjs.com/package/buffer) or greater.
 
 ### Flow
 
@@ -116,6 +116,7 @@ The below examples are implemented as integration tests, they should be very eas
 - [Create a CLTV locked transaction where the expiry is past](https://github.com/bitcoinjs/bitcoinjs-lib/blob/d853806/test/integration/cltv.js#L36)
 - [Create a CLTV locked transaction where the parties bypass the expiry](https://github.com/bitcoinjs/bitcoinjs-lib/blob/d853806/test/integration/cltv.js#L70)
 - [Create a CLTV locked transaction which fails due to expiry in the future](https://github.com/bitcoinjs/bitcoinjs-lib/blob/d853806/test/integration/cltv.js#L102)
+- [Use BIP39 to generate a BIP32 wallet address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/14f983b/test/integration/bip32.js)
 
 If you have a use case that you feel could be listed here, please [ask for it](https://github.com/bitcoinjs/bitcoinjs-lib/issues/new)!
 
diff --git a/package.json b/package.json
index 822fd0c..06be44d 100644
--- a/package.json
+++ b/package.json
@@ -67,6 +67,7 @@
   },
   "devDependencies": {
     "async": "^2.0.1",
+    "bip39": "^2.3.0",
     "bs58": "^4.0.0",
     "cb-http-client": "^0.2.0",
     "coinselect": "^3.1.1",
diff --git a/test/integration/bip32.js b/test/integration/bip32.js
index 25db93d..f86fc9b 100644
--- a/test/integration/bip32.js
+++ b/test/integration/bip32.js
@@ -2,6 +2,7 @@
 
 var assert = require('assert')
 var bigi = require('bigi')
+var bip39 = require('bip39')
 var bitcoin = require('../../')
 var crypto = require('crypto')
 
@@ -94,4 +95,19 @@ describe('bitcoinjs-lib (BIP32)', function () {
     var recovered = recoverParent(neuteredMaster, child)
     assert.strictEqual(recovered.toBase58(), master.toBase58())
   })
+
+  it('can use BIP39 to generate BIP32 wallet address', function () {
+//     var mnemonic = bip39.generateMnemonic()
+    var mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost'
+    assert(bip39.validateMnemonic(mnemonic))
+
+    var seed = bip39.mnemonicToSeed(mnemonic)
+    var root = bitcoin.HDNode.fromSeedBuffer(seed)
+
+    // 1st receive address
+    assert.strictEqual(root.derivePath("m/0'/0/0").getAddress(), '1AVQHbGuES57wD68AJi7Gcobc3RZrfYWTC')
+
+    // 1st change address
+    assert.strictEqual(root.derivePath("m/0'/1/0").getAddress(), '1349KVc5NgedaK7DvuD4xDFxL86QN1Hvdn')
+  })
 })