bip84
Advanced tools
Comparing version 0.2.0 to 0.2.1
{ | ||
"name": "bip84", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "BIP84 - Derives segwit + bech32 addresses from seed, zprv/zpub and vprv/vpub in javascript", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -19,4 +19,4 @@ # BIP84 | ||
console.log('mnemonic:', mnemonic) | ||
console.log('rootpriv:', root.getRootPrivate()) | ||
console.log('rootpub:', root.getRootPublic()) | ||
console.log('rootpriv:', root.getRootPrivateKey()) | ||
console.log('rootpub:', root.getRootPublicKey()) | ||
console.log('\n'); | ||
@@ -27,4 +27,4 @@ | ||
console.log("Account 0, root = m/84'/0'/0'"); | ||
console.log('Account 0 xprv:', account0.getAccountPrivate()) | ||
console.log('Account 0 xpub:', account0.getAccountPublic()) | ||
console.log('Account 0 xprv:', account0.getAccountPrivateKey()) | ||
console.log('Account 0 xpub:', account0.getAccountPublicKey()) | ||
console.log('\n'); | ||
@@ -53,7 +53,7 @@ | ||
console.log("Account 1, root = m/84'/0'/0'"); | ||
console.log('Account 1 xpub:', account1.getAccountPublic()); | ||
console.log("Account 1, root = m/84'/1'/0'"); | ||
console.log('Account 1 xpub:', account1.getAccountPublicKey()); | ||
console.log('\n'); | ||
console.log("Account 1, first receiving address = m/84'/0'/0'/0/0"); | ||
console.log("Account 1, first receiving address = m/84'/1'/0'/0/0"); | ||
console.log('Pubkey:', account1.getPublicKey(0)) | ||
@@ -63,3 +63,3 @@ console.log('Address:', account1.getAddress(0)) | ||
console.log("Account 1, second receiving address = m/84'/0'/0'/0/1"); | ||
console.log("Account 1, second receiving address = m/84'/1'/0'/0/1"); | ||
console.log('Pubkey:', account1.getPublicKey(1)) | ||
@@ -69,3 +69,3 @@ console.log('Address:', account1.getAddress(1)) | ||
console.log("Account 1, first change address = m/84'/0'/0'/1/0"); | ||
console.log("Account 1, first change address = m/84'/1'/0'/1/0"); | ||
console.log('Pubkey:', account1.getPublicKey(0, true)) | ||
@@ -75,3 +75,3 @@ console.log('Address:', account1.getAddress(0, true)) | ||
console.log("Account 1, second change address = m/84'/0'/0'/1/1"); | ||
console.log("Account 1, second change address = m/84'/1'/0'/1/1"); | ||
console.log('Pubkey:', account1.getPublicKey(1, true)) | ||
@@ -86,8 +86,2 @@ console.log('Address:', account1.getAddress(1, true)) | ||
## Donate | ||
![alt text](https://chainflyer.bitflyer.jp/Address/QR/1NzRXQa3gL1kAVZZGMjedUqM3Z3MSDFyv6 "Donate") | ||
1NzRXQa3gL1kAVZZGMjedUqM3Z3MSDFyv6 | ||
## License terms | ||
@@ -94,0 +88,0 @@ |
136
src/index.js
const bjs = require('bitcoinjs-lib') | ||
, b58 = require('bs58check') | ||
, bip39 = require('bip39') | ||
, bitcoinPubTypes = { mainnet: { zprv: '04b2430c', zpub: '04b24746'}, testnet: { vprv: '045f18bc', vpub: '045f1cf6'} } | ||
, bitcoinNetworks = { mainnet: bjs.networks.bitcoin, testnet: bjs.networks.testnet } | ||
const bitcoinPubTypes = { zprv: '04b2430c', zpub: '04b24746'} | ||
const bitcoinTestnetPubTypes = { vprv: '045f18bc', vpub: '045f1cf6'} | ||
/** | ||
@@ -12,23 +11,19 @@ * Constructor | ||
* @param {string} seed | ||
* @param {boolean} isTestnet | ||
* @param {number} slip44 | ||
* @param {object} pubTypes | ||
* @param {object} network | ||
* @param {number} slip44 | ||
* @param {boolean} testnet | ||
*/ | ||
function fromSeed(seed, network, slip44, pub_types, testnet) { | ||
function fromSeed(seed, isTestnet, slip44, pubTypes, network) { | ||
this.seed = bip39.mnemonicToSeedSync(seed) | ||
this.isTestnet = testnet === true | ||
this.slip44 = slip44 ? slip44 : 0 | ||
this.pub_types = pub_types || { mainnet: bitcoinPubTypes, testnet: bitcoinTestnetPubTypes } | ||
if (network) { | ||
this.network = network // assume to be bjs.network type | ||
} else { | ||
this.network = testnet ? bjs.networks.testnet : bjs.networks.bitcoin | ||
} | ||
this.isTestnet = isTestnet === true | ||
this.slip44 = this.isTestnet ? 1 : slip44 ? slip44 : 0 // 0 is for Bitcoin and 1 is testnet for all coins | ||
this.pubTypes = pubTypes || bitcoinPubTypes | ||
this.network = network || this.isTestnet ? bitcoinNetworks.testnet : bitcoinNetworks.bitcoin | ||
} | ||
fromSeed.prototype.getRootPrivate = function () { | ||
fromSeed.prototype.getRootPrivateKey = function () { | ||
let masterPrv = this.isTestnet ? | ||
vprv(bjs.bip32.fromSeed(this.seed, this.network).toBase58(), this.pub_types.testnet.vprv) : | ||
zprv(bjs.bip32.fromSeed(this.seed, this.network).toBase58(), this.pub_types.mainnet.zprv) | ||
vprv(bjs.bip32.fromSeed(this.seed, this.network).toBase58(), this.pubTypes.testnet.vprv) : | ||
zprv(bjs.bip32.fromSeed(this.seed, this.network).toBase58(), this.pubTypes.mainnet.zprv) | ||
@@ -38,6 +33,6 @@ return masterPrv | ||
fromSeed.prototype.getRootPublic = function () { | ||
fromSeed.prototype.getRootPublicKey = function () { | ||
let masterPub = this.isTestnet ? | ||
vpub(bjs.bip32.fromSeed(this.seed, this.network).neutered().toBase58(), this.pub_types.testnet.vpub) : | ||
zpub(bjs.bip32.fromSeed(this.seed, this.network).neutered().toBase58(), this.pub_types.mainnet.zpub) | ||
vpub(bjs.bip32.fromSeed(this.seed, this.network).neutered().toBase58(), this.pubTypes.testnet.vpub) : | ||
zpub(bjs.bip32.fromSeed(this.seed, this.network).neutered().toBase58(), this.pubTypes.mainnet.zpub) | ||
@@ -50,4 +45,4 @@ return masterPub | ||
let masterPrv = this.isTestnet ? | ||
vprv(bjs.bip32.fromSeed(this.seed, this.network).derivePath(keypath).toBase58(), this.pub_types.testnet.vprv) : | ||
zprv(bjs.bip32.fromSeed(this.seed, this.network).derivePath(keypath).toBase58(), this.pub_types.mainnet.zprv) | ||
vprv(bjs.bip32.fromSeed(this.seed, this.network).derivePath(keypath).toBase58(), this.pubTypes.testnet.vprv) : | ||
zprv(bjs.bip32.fromSeed(this.seed, this.network).derivePath(keypath).toBase58(), this.pubTypes.mainnet.zprv) | ||
@@ -59,13 +54,10 @@ return masterPrv | ||
* Constructor | ||
* Create key pairs from a private master key. | ||
* Create key pairs from a private master key of mainnet and testnet. | ||
* @param {string} zprv/vprv | ||
* @param {object} networks | ||
* @param {object} pub_types | ||
* @param {boolean} testnet | ||
* @param {object} pubTypes | ||
*/ | ||
function fromZPrv(zprv, networks, pub_types, testnet) { | ||
this.isTestnet = testnet === true | ||
this.pub_types = pub_types || { mainnet: bitcoinPubTypes, testnet: bitcoinTestnetPubTypes } | ||
this.networks = networks || { mainnet: bjs.networks.bitcoin, testnet: bjs.networks.testnet } | ||
this.network = undefined | ||
function fromZPrv(zprv, networks, pubTypes) { | ||
this.pubTypes = pubTypes || bitcoinPubTypes | ||
this.networks = networks || bitcoinNetworks | ||
this.zprv = this.toNode(zprv) | ||
@@ -80,7 +72,7 @@ } | ||
if (!Object.values(this.pub_types.mainnet).includes(version.toString('hex')) && !Object.values(this.pub_types.testnet).includes(version.toString('hex'))) { | ||
if (!Object.values(this.pubTypes.mainnet).includes(version.toString('hex')) && !Object.values(this.pubTypes.testnet).includes(version.toString('hex'))) { | ||
throw new Error('prefix is not supported') | ||
} | ||
if (Object.values(this.pub_types.mainnet).includes(version.toString('hex'))) { | ||
if (Object.values(this.pubTypes.mainnet).includes(version.toString('hex'))) { | ||
const buf = Buffer.allocUnsafe(4) | ||
@@ -93,6 +85,6 @@ buf.writeInt32BE(this.networks.mainnet.bip32.private, 0) | ||
if (Object.values(this.pub_types.testnet).includes(version.toString('hex'))) { | ||
if (Object.values(this.pubTypes.testnet).includes(version.toString('hex'))) { | ||
const buf = Buffer.allocUnsafe(4) | ||
buf.writeInt32BE(this.networks.testnet.bip32.private, 0) | ||
buffer = Buffer.concat([buf, key]) // xprv | ||
buffer = Buffer.concat([buf, key]) // vprv | ||
this.network = this.networks.testnet | ||
@@ -105,6 +97,7 @@ this.isTestnet = true | ||
fromZPrv.prototype.getAccountPrivate = function () { | ||
fromZPrv.prototype.getAccountPrivateKey = function () { | ||
let pub = bjs.bip32.fromBase58(this.zprv, this.network).toBase58() | ||
let masterPrv = this.isTestnet ? | ||
vprv(bjs.bip32.fromBase58(this.zprv, this.network).toBase58(), this.pub_types.testnet.vprv) : | ||
zprv(bjs.bip32.fromBase58(this.zprv, this.network).toBase58(), this.pub_types.mainnet.zprv) | ||
vprv(pub, this.pubTypes.testnet.vprv) : | ||
zprv(pub, this.pubTypes.mainnet.zprv) | ||
@@ -114,6 +107,7 @@ return masterPrv | ||
fromZPrv.prototype.getAccountPublic = function () { | ||
let masterPub = this.isTestnet ? | ||
vpub(bjs.bip32.fromBase58(this.zprv, this.network).neutered().toBase58(), this.pub_types.testnet.vpub) : | ||
zpub(bjs.bip32.fromBase58(this.zprv, this.network).neutered().toBase58(), this.pub_types.mainnet.zpub) | ||
fromZPrv.prototype.getAccountPublicKey = function () { | ||
let pub = bjs.bip32.fromBase58(this.zprv, this.network).neutered().toBase58() | ||
, masterPub = this.isTestnet ? | ||
vpub(pub, this.pubTypes.testnet.vpub) : | ||
zpub(pub, this.pubTypes.mainnet.zpub) | ||
@@ -124,5 +118,3 @@ return masterPub | ||
fromZPrv.prototype.getPrivateKey = function (index, isChange) { | ||
isChange = isChange !== true ? false : true | ||
let change = isChange !== true ? 0 : 1 | ||
let change = isChange === true ? 1 : 0 | ||
, prvkey = bjs.bip32.fromBase58(this.zprv, this.network).derive(change).derive(index) | ||
@@ -134,5 +126,3 @@ | ||
fromZPrv.prototype.getPublicKey = function (index, isChange) { | ||
isChange = isChange !== true ? false : true | ||
let change = isChange !== true ? 0 : 1 | ||
let change = isChange === true ? 1 : 0 | ||
, prvkey = bjs.bip32.fromBase58(this.zprv, this.network).derive(change).derive(index) | ||
@@ -144,5 +134,3 @@ | ||
fromZPrv.prototype.getAddress = function (index, isChange) { | ||
isChange = isChange !== true ? false : true | ||
let change = isChange !== true ? 0 : 1 | ||
let change = isChange === true ? 1 : 0 | ||
, pubkey = bjs.bip32.fromBase58(this.zprv, this.network).derive(change).derive(index).publicKey | ||
@@ -159,5 +147,3 @@ | ||
fromZPrv.prototype.getKeypair = function (index, isChange) { | ||
isChange = isChange !== true ? false : true | ||
let change = isChange !== true ? 0 : 1 | ||
let change = isChange === true ? 1 : 0 | ||
, prvkey = bjs.bip32.fromBase58(this.zprv, this.network).derive(change).derive(index) | ||
@@ -170,13 +156,10 @@ | ||
* Constructor | ||
* Create public keys and addresses from a public master key. | ||
* Create public keys and addresses from a public master key of mainnet and testnet. | ||
* @param {string} zpub/vpub | ||
* @param {object} pubTypes | ||
* @param {object} networks | ||
* @param {object} pub_types | ||
* @param {boolean} testnet | ||
*/ | ||
function fromZPub(zpub, networks, pub_types, testnet) { | ||
this.isTestnet = testnet === true | ||
this.pub_types = pub_types || { mainnet: bitcoinPubTypes, testnet: bitcoinTestnetPubTypes } | ||
this.networks = networks || { mainnet: bjs.networks.bitcoin, testnet: bjs.networks.testnet } | ||
this.network = undefined | ||
function fromZPub(zpub, pubTypes, networks) { | ||
this.pubTypes = pubTypes || bitcoinPubTypes | ||
this.networks = networks || bitcoinNetworks | ||
this.zpub = this.toNode(zpub) | ||
@@ -191,10 +174,10 @@ } | ||
if (!Object.values(this.pub_types.mainnet).includes(version.toString('hex')) && !Object.values(this.pub_types.testnet).includes(version.toString('hex'))) { | ||
if (!Object.values(this.pubTypes.mainnet).includes(version.toString('hex')) && !Object.values(this.pubTypes.testnet).includes(version.toString('hex'))) { | ||
throw new Error('prefix is not supported') | ||
} | ||
if (Object.values(this.pub_types.mainnet).includes(version.toString('hex'))) { | ||
if (Object.values(this.pubTypes.mainnet).includes(version.toString('hex'))) { | ||
const buf = Buffer.allocUnsafe(4) | ||
buf.writeInt32BE(this.networks.mainnet.bip32.public, 0) | ||
buffer = Buffer.concat([buf, key]) // xprv | ||
buffer = Buffer.concat([buf, key]) // xpub | ||
this.network = this.networks.mainnet | ||
@@ -204,6 +187,6 @@ this.isTestnet = false | ||
if (Object.values(this.pub_types.testnet).includes(version.toString('hex'))) { | ||
if (Object.values(this.pubTypes.testnet).includes(version.toString('hex'))) { | ||
const buf = Buffer.allocUnsafe(4) | ||
buf.writeInt32BE(this.networks.testnet.bip32.public, 0) | ||
buffer = Buffer.concat([buf, key]) // xprv | ||
buffer = Buffer.concat([buf, key]) // vpub | ||
this.network = this.networks.testnet | ||
@@ -216,6 +199,7 @@ this.isTestnet = true | ||
fromZPub.prototype.getAccountPublic = function () { | ||
fromZPub.prototype.getAccountPublicKey = function () { | ||
let pub = bjs.bip32.fromBase58(this.zpub, this.network).neutered().toBase58() | ||
let masterPub = this.isTestnet ? | ||
vpub(bjs.bip32.fromBase58(this.zpub, this.network).neutered().toBase58(), this.pub_types.testnet.vpub) : | ||
zpub(bjs.bip32.fromBase58(this.zpub, this.network).neutered().toBase58(), this.pub_types.mainnet.zpub) | ||
vpub(pub, this.pubTypes.testnet.vpub) : | ||
zpub(pubt , this.pubTypes.mainnet.zpub) | ||
@@ -226,5 +210,3 @@ return masterPub | ||
fromZPub.prototype.getPublicKey = function (index, isChange) { | ||
isChange = isChange !== true ? false : true | ||
let change = isChange !== true ? 0 : 1 | ||
let change = isChange === true ? 1 : 0 | ||
, zpub = bjs.bip32.fromBase58(this.zpub, this.network).derive(change).derive(index) | ||
@@ -236,5 +218,3 @@ | ||
fromZPub.prototype.getAddress = function (index, isChange) { | ||
isChange = isChange !== true ? false : true | ||
let change = isChange !== true ? 0 : 1 | ||
let change = isChange === true ? 1 : 0 | ||
, pubkey = bjs.bip32.fromBase58(this.zpub, this.network).derive(change).derive(index).publicKey | ||
@@ -251,5 +231,3 @@ | ||
fromZPub.prototype.getPayment = function (index, isChange) { | ||
isChange = isChange !== true ? false : true | ||
let change = isChange !== true ? 0 : 1 | ||
let change = isChange === true ? 1 : 0 | ||
, pubkey = bjs.bip32.fromBase58(this.zpub, this.network).derive(change).derive(index).publicKey | ||
@@ -256,0 +234,0 @@ |
@@ -32,9 +32,9 @@ const BIP84 = require('../src/index'); | ||
it("Generates correct rootPublic and rootPrivate", () => { | ||
expect(data.root.getRootPrivate()).toEqual('zprvAWgYBBk7JR8Gjrh4UJQ2uJdG1r3WNRRfURiABBE3RvMXYSrRJL62XuezvGdPvG6GFBZduosCc1YP5wixPox7zhZLfiUm8aunE96BBa4Kei5'); | ||
expect(data.root.getRootPublic()).toEqual('zpub6jftahH18ngZxLmXaKw3GSZzZsszmt9WqedkyZdezFtWRFBZqsQH5hyUmb4pCEeZGmVfQuP5bedXTB8is6fTv19U1GQRyQUKQGUTzyHACMF'); | ||
expect(data.root.getRootPrivateKey()).toEqual('zprvAWgYBBk7JR8Gjrh4UJQ2uJdG1r3WNRRfURiABBE3RvMXYSrRJL62XuezvGdPvG6GFBZduosCc1YP5wixPox7zhZLfiUm8aunE96BBa4Kei5'); | ||
expect(data.root.getRootPublicKey()).toEqual('zpub6jftahH18ngZxLmXaKw3GSZzZsszmt9WqedkyZdezFtWRFBZqsQH5hyUmb4pCEeZGmVfQuP5bedXTB8is6fTv19U1GQRyQUKQGUTzyHACMF'); | ||
}); | ||
it("Generates correct root = m/84'/0'/0'", () => { | ||
expect(data.account0.getAccountPrivate()).toEqual('zprvAdG4iTXWBoARxkkzNpNh8r6Qag3irQB8PzEMkAFeTRXxHpbF9z4QgEvBRmfvqWvGp42t42nvgGpNgYSJA9iefm1yYNZKEm7z6qUWCroSQnE'); | ||
expect(data.account0.getAccountPublic()).toEqual('zpub6rFR7y4Q2AijBEqTUquhVz398htDFrtymD9xYYfG1m4wAcvPhXNfE3EfH1r1ADqtfSdVCToUG868RvUUkgDKf31mGDtKsAYz2oz2AGutZYs'); | ||
expect(data.account0.getAccountPrivateKey()).toEqual('zprvAdG4iTXWBoARxkkzNpNh8r6Qag3irQB8PzEMkAFeTRXxHpbF9z4QgEvBRmfvqWvGp42t42nvgGpNgYSJA9iefm1yYNZKEm7z6qUWCroSQnE'); | ||
expect(data.account0.getAccountPublicKey()).toEqual('zpub6rFR7y4Q2AijBEqTUquhVz398htDFrtymD9xYYfG1m4wAcvPhXNfE3EfH1r1ADqtfSdVCToUG868RvUUkgDKf31mGDtKsAYz2oz2AGutZYs'); | ||
}); | ||
@@ -66,7 +66,7 @@ | ||
it("Generates correct root = m/84'/0'/0'", () => { | ||
expect(account1.getAccountPublic()).toEqual('vpub5Vm8JiyeMgCWT2SqgFkoJyaovNQH8RCF3wAUKCrFAfRdVujdYubBrYUGtggtabj71XxvUQuS5r9AgT4VhGvax9gXEpdi9XBg7jHnvm1WDii'); | ||
it("Generates correct root = m/84'/1'/0'", () => { | ||
expect(account1.getAccountPublicKey()).toEqual('vpub5Vm8JiyeMgCWT2SqgFkoJyaovNQH8RCF3wAUKCrFAfRdVujdYubBrYUGtggtabj71XxvUQuS5r9AgT4VhGvax9gXEpdi9XBg7jHnvm1WDii'); | ||
}); | ||
it("Generates correct first receiving address = m/84'/0'/0'/0/0", () => { | ||
it("Generates correct first receiving address = m/84'/1'/0'/0/0", () => { | ||
expect(account1.getPublicKey(0)).toEqual('02176cd6a3d74e3a4f3f9c4fe517291fae7654709db13e97f41765fd6e7e1406bb'); | ||
@@ -76,3 +76,3 @@ expect(account1.getAddress(0)).toEqual('tb1qxdz5xktump2xt0832tgqnlhf48jrarulddvaym'); | ||
it("Generates correct second receiving address = m/84'/0'/0'/0/1", () => { | ||
it("Generates correct second receiving address = m/84'/1'/0'/0/1", () => { | ||
expect(account1.getPublicKey(1)).toEqual('03b3ca0a9f89350bbd0bbaca3e9701d71e86ae29582a9572a32404ecf4350f3964'); | ||
@@ -82,3 +82,3 @@ expect(account1.getAddress(1)).toEqual('tb1q65v0jm4v49g33pys6wsv6enrl98vscpkr56zz9'); | ||
it("Generates correct first change address = m/84'/0'/0'/1/0", () => { | ||
it("Generates correct first change address = m/84'/1'/0'/1/0", () => { | ||
expect(account1.getPublicKey(0, true)).toEqual('03fb2a8afb6ff7d628d0742601e5016c01d387a0103fff548bbfbfe06a1bfb85f9'); | ||
@@ -88,3 +88,3 @@ expect(account1.getAddress(0, true)).toEqual('tb1qexuaj40zxzmyqsruv0ed2lw4mhtz0mvc4cvlhd'); | ||
it("Generates correct second change address = m/84'/0'/0'/1/1", () => { | ||
it("Generates correct second change address = m/84'/1'/0'/1/1", () => { | ||
expect(account1.getPublicKey(1, true)).toEqual('03e46cb4676c6259d46f39e1ba53f7170309ea818d6b4d2fc14f959812087f5f5f'); | ||
@@ -91,0 +91,0 @@ expect(account1.getAddress(1, true)).toEqual('tb1q97cd5cd3jp43rfcfuuv3j97t5pve7qg3eeljq8'); |
@@ -8,4 +8,4 @@ const BIP84 = require('../src/index') | ||
console.log('mnemonic:', mnemonic) | ||
console.log('rootpriv:', root.getRootPrivate()) | ||
console.log('rootpub:', root.getRootPublic()) | ||
console.log('rootpriv:', root.getRootPrivateKey()) | ||
console.log('rootpub:', root.getRootPublicKey()) | ||
console.log('\n'); | ||
@@ -16,4 +16,4 @@ | ||
console.log("Account 0, root = m/84'/0'/0'"); | ||
console.log('Account 0 xprv:', account0.getAccountPrivate()) | ||
console.log('Account 0 xpub:', account0.getAccountPublic()) | ||
console.log('Account 0 xprv:', account0.getAccountPrivateKey()) | ||
console.log('Account 0 xpub:', account0.getAccountPublicKey()) | ||
console.log('\n'); | ||
@@ -43,3 +43,3 @@ | ||
console.log("Account 1, root = m/84'/0'/0'"); | ||
console.log('Account 1 xpub:', account1.getAccountPublic()); | ||
console.log('Account 1 xpub:', account1.getAccountPublicKey()); | ||
console.log('\n'); | ||
@@ -46,0 +46,0 @@ |
20985
391
87