From 228a2c1879d37997f8c622f17b8ecc0716423e18 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 3 Jan 2017 17:00:23 +1100 Subject: [PATCH] tests/README: add BIP32/BIP44 derivation examples --- README.md | 2 ++ test/integration/bip32.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/README.md b/README.md index 32da609..e5713a7 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,8 @@ The below examples are implemented as integration tests, they should be very eas - [Spend from a 2-of-4 multisig P2SH address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/d853806/test/integration/multisig.js#L25) - [Generate a single-key stealth address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/d853806/test/integration/stealth.js) - [Generate a dual-key stealth address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/d853806/test/integration/stealth.js) +- [Create a BIP32 wallet external address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/d853806/test/integration/bip32.js) +- [Create a BIP44, bitcoin, account 0, external address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/d853806/test/integration/bip32.js) - [Recover a BIP32 parent private key from the parent public key and a derived non-hardened child private key](https://github.com/bitcoinjs/bitcoinjs-lib/blob/d853806/test/integration/bip32.js) - [Recover a Private key from duplicate R values in a signature](https://github.com/bitcoinjs/bitcoinjs-lib/blob/d853806/test/integration/crypto.js) - [Create a CLTV locked transaction where the expiry is past](https://github.com/bitcoinjs/bitcoinjs-lib/blob/d853806/test/integration/cltv.js#L36) diff --git a/test/integration/bip32.js b/test/integration/bip32.js index 552c383..9f5b9a9 100644 --- a/test/integration/bip32.js +++ b/test/integration/bip32.js @@ -9,6 +9,38 @@ var ecurve = require('ecurve') var secp256k1 = ecurve.getCurveByName('secp256k1') describe('bitcoinjs-lib (BIP32)', function () { + it('can create a BIP32 wallet external address', function () { + var path = "m/0'/0/0" + var root = bitcoin.HDNode.fromSeedHex('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd') + + var child1 = root.derivePath(path) + + // option 2, manually + var child2 = root.deriveHardened(0) + .derive(0) + .derive(0) + + assert.equal(child1.getAddress(), '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7') + assert.equal(child2.getAddress(), '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7') + }) + + it('can create a BIP44, bitcoin, account 0, external address', function () { + var path = "m/44'/0'/0'/0/0" + var root = bitcoin.HDNode.fromSeedHex('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd') + + var child1 = root.derivePath(path) + + // option 2, manually + var child2 = root.deriveHardened(44) + .deriveHardened(0) + .deriveHardened(0) + .derive(0) + .derive(0) + + assert.equal(child1.getAddress(), '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au') + assert.equal(child2.getAddress(), '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au') + }) + it('can recover a BIP32 parent private key from the parent public key, and a derived, non-hardened child private key', function () { function recoverParent (master, child) { assert(!master.keyPair.d, 'You already have the parent private key')