2016-11-25 03:46:36 +01:00
|
|
|
/* global describe, it */
|
|
|
|
|
2018-06-25 08:24:37 +02:00
|
|
|
const assert = require('assert')
|
|
|
|
const bip32 = require('bip32')
|
|
|
|
const bip39 = require('bip39')
|
|
|
|
const bitcoin = require('../../')
|
2016-11-25 03:46:36 +01:00
|
|
|
|
2018-05-22 09:43:25 +02:00
|
|
|
function getAddress (node, network) {
|
2018-06-27 05:54:47 +02:00
|
|
|
return bitcoin.payments.p2pkh({ pubkey: node.publicKey, network }).address
|
2018-05-15 14:17:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 03:46:36 +01:00
|
|
|
describe('bitcoinjs-lib (BIP32)', function () {
|
2017-03-29 04:59:42 +02:00
|
|
|
it('can import a BIP32 testnet xpriv and export to WIF', function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const xpriv = 'tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK'
|
|
|
|
const node = bip32.fromBase58(xpriv, bitcoin.networks.testnet)
|
2017-03-29 04:59:42 +02:00
|
|
|
|
2018-05-15 14:17:46 +02:00
|
|
|
assert.equal(node.toWIF(), 'cQfoY67cetFNunmBUX5wJiw3VNoYx3gG9U9CAofKE6BfiV1fSRw7')
|
2017-03-29 04:59:42 +02:00
|
|
|
})
|
|
|
|
|
2017-08-09 11:08:39 +02:00
|
|
|
it('can export a BIP32 xpriv, then import it', function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost'
|
|
|
|
const seed = bip39.mnemonicToSeed(mnemonic)
|
|
|
|
const node = bip32.fromSeed(seed)
|
|
|
|
const string = node.toBase58()
|
|
|
|
const restored = bip32.fromBase58(string)
|
2017-08-09 11:08:39 +02:00
|
|
|
|
2018-05-15 14:17:46 +02:00
|
|
|
assert.equal(getAddress(node), getAddress(restored)) // same public key
|
|
|
|
assert.equal(node.toWIF(), restored.toWIF()) // same private key
|
2017-08-09 11:08:39 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('can export a BIP32 xpub', function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost'
|
|
|
|
const seed = bip39.mnemonicToSeed(mnemonic)
|
|
|
|
const node = bip32.fromSeed(seed)
|
|
|
|
const string = node.neutered().toBase58()
|
2017-08-09 11:08:39 +02:00
|
|
|
|
|
|
|
assert.equal(string, 'xpub661MyMwAqRbcGhVeaVfEBA25e3cP9DsJQZoE8iep5fZSxy3TnPBNBgWnMZx56oreNc48ZoTkQfatNJ9VWnQ7ZcLZcVStpaXLTeG8bGrzX3n')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('can create a BIP32, bitcoin, account 0, external address', function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const path = "m/0'/0/0"
|
|
|
|
const root = bip32.fromSeed(Buffer.from('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd', 'hex'))
|
2017-01-03 07:00:23 +01:00
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
const child1 = root.derivePath(path)
|
2017-01-03 07:00:23 +01:00
|
|
|
|
|
|
|
// option 2, manually
|
2018-06-25 08:37:45 +02:00
|
|
|
const child1b = root.deriveHardened(0)
|
2017-01-03 07:00:23 +01:00
|
|
|
.derive(0)
|
|
|
|
.derive(0)
|
|
|
|
|
2018-05-15 14:17:46 +02:00
|
|
|
assert.equal(getAddress(child1), '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7')
|
|
|
|
assert.equal(getAddress(child1b), '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7')
|
2017-01-03 07:00:23 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('can create a BIP44, bitcoin, account 0, external address', function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const root = bip32.fromSeed(Buffer.from('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd', 'hex'))
|
2017-01-03 07:00:23 +01:00
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
const child1 = root.derivePath("m/44'/0'/0'/0/0")
|
2017-01-03 07:00:23 +01:00
|
|
|
|
|
|
|
// option 2, manually
|
2018-06-25 08:37:45 +02:00
|
|
|
const child1b = root.deriveHardened(44)
|
2017-01-03 07:00:23 +01:00
|
|
|
.deriveHardened(0)
|
|
|
|
.deriveHardened(0)
|
|
|
|
.derive(0)
|
|
|
|
.derive(0)
|
|
|
|
|
2018-05-15 14:17:46 +02:00
|
|
|
assert.equal(getAddress(child1), '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au')
|
|
|
|
assert.equal(getAddress(child1b), '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au')
|
2017-01-03 07:00:23 +01:00
|
|
|
})
|
|
|
|
|
2017-08-06 00:28:59 +02:00
|
|
|
it('can create a BIP49, bitcoin testnet, account 0, external address', function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const mnemonic = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'
|
|
|
|
const seed = bip39.mnemonicToSeed(mnemonic)
|
|
|
|
const root = bip32.fromSeed(seed)
|
2017-08-06 00:28:59 +02:00
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
const path = "m/49'/1'/0'/0/0"
|
|
|
|
const child = root.derivePath(path)
|
2017-08-06 00:28:59 +02:00
|
|
|
|
2018-07-11 08:19:32 +02:00
|
|
|
const { address } = bitcoin.payments.p2sh({
|
|
|
|
redeem: bitcoin.payments.p2wpkh({ pubkey: child.publicKey, network: bitcoin.networks.testnet }),
|
|
|
|
network: bitcoin.networks.testnet
|
|
|
|
})
|
2017-08-06 00:28:59 +02:00
|
|
|
assert.equal(address, '2Mww8dCYPUpKHofjgcXcBCEGmniw9CoaiD2')
|
|
|
|
})
|
|
|
|
|
2017-08-09 11:08:39 +02:00
|
|
|
it('can use BIP39 to generate BIP32 addresses', function () {
|
2018-05-29 03:37:03 +02:00
|
|
|
// var mnemonic = bip39.generateMnemonic()
|
2018-06-25 08:37:45 +02:00
|
|
|
const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost'
|
2017-04-19 15:10:30 +02:00
|
|
|
assert(bip39.validateMnemonic(mnemonic))
|
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
const seed = bip39.mnemonicToSeed(mnemonic)
|
|
|
|
const root = bip32.fromSeed(seed)
|
2017-04-19 15:10:30 +02:00
|
|
|
|
2017-08-09 11:08:39 +02:00
|
|
|
// receive addresses
|
2018-05-15 14:17:46 +02:00
|
|
|
assert.strictEqual(getAddress(root.derivePath("m/0'/0/0")), '1AVQHbGuES57wD68AJi7Gcobc3RZrfYWTC')
|
|
|
|
assert.strictEqual(getAddress(root.derivePath("m/0'/0/1")), '1Ad6nsmqDzbQo5a822C9bkvAfrYv9mc1JL')
|
2017-04-19 15:10:30 +02:00
|
|
|
|
2017-08-09 11:08:39 +02:00
|
|
|
// change addresses
|
2018-05-15 14:17:46 +02:00
|
|
|
assert.strictEqual(getAddress(root.derivePath("m/0'/1/0")), '1349KVc5NgedaK7DvuD4xDFxL86QN1Hvdn')
|
|
|
|
assert.strictEqual(getAddress(root.derivePath("m/0'/1/1")), '1EAvj4edpsWcSer3duybAd4KiR4bCJW5J6')
|
2017-04-19 15:10:30 +02:00
|
|
|
})
|
2016-11-25 03:46:36 +01:00
|
|
|
})
|