bip84
Advanced tools
Comparing version 0.1.7 to 0.1.8
{ | ||
"name": "bip84", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "BIP84 - Derives segwit + bech32 addresses from seed, zprv/zpub and vprv/vpub in javascript", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -5,2 +5,13 @@ const bjs = require('bitcoinjs-lib') | ||
const pub_types = [ | ||
'04b2430c', // zprv | ||
'04b24746', // zpub | ||
] | ||
// testnet | ||
const pub_types_testnet = [ | ||
'045f18bc', // vprv | ||
'045f1cf6', // vpub | ||
] | ||
function fromSeed(seed, network) { | ||
@@ -37,15 +48,10 @@ this.seed = bip39.mnemonicToSeedSync(seed) | ||
function toNode(pub) { | ||
var pub_types = [ | ||
'04b2430c', // zprv | ||
'04b24746', // zpub | ||
] | ||
function fromZPrv(zprv) { | ||
this.network = undefined | ||
this.isTestnet = undefined | ||
this.zprv = this.toNode(zprv) | ||
} | ||
// testnet | ||
var pub_types_testnet = [ | ||
'045f18bc', // vprv | ||
'045f1cf6', // vpub | ||
] | ||
let payload = b58.decode(pub) | ||
fromZPrv.prototype.toNode = function (zprv) { | ||
let payload = b58.decode(zprv) | ||
, version = payload.slice(0, 4) | ||
@@ -63,2 +69,5 @@ , key = payload.slice(4) | ||
Buffer.concat([Buffer.from('0488b21e','hex'), key]) // xpub | ||
this.network = bjs.networks.bitcoin | ||
this.isTestnet = false | ||
} | ||
@@ -70,2 +79,5 @@ | ||
Buffer.concat([Buffer.from('043587cf','hex'), key]) // tpub | ||
this.network = bjs.networks.testnet | ||
this.isTestnet = true | ||
} | ||
@@ -76,8 +88,2 @@ | ||
function fromZPrv(zprv, network) { | ||
this.zprv = toNode(zprv) | ||
this.isTestnet = network === true ? true : false | ||
this.network = network === true ? bjs.networks.testnet : bjs.networks.bitcoin | ||
} | ||
fromZPrv.prototype.getAccountPrivate = function () { | ||
@@ -131,8 +137,39 @@ let masterPrv = this.isTestnet ? | ||
function fromZPub(zpub, network) { | ||
this.zpub = toNode(zpub) | ||
this.isTestnet = network === true ? true : false | ||
this.network = network === true ? bjs.networks.testnet : bjs.networks.bitcoin | ||
function fromZPub(zpub) { | ||
this.network = undefined | ||
this.isTestnet = undefined | ||
this.zpub = this.toNode(zpub) | ||
} | ||
fromZPub.prototype.toNode = function (zpub) { | ||
let payload = b58.decode(zpub) | ||
, version = payload.slice(0, 4) | ||
, key = payload.slice(4) | ||
, buffer = undefined | ||
if (!pub_types.includes(version.toString('hex')) && !pub_types_testnet.includes(version.toString('hex'))) { | ||
throw new Error('prefix is not supported') | ||
} | ||
if (pub_types.includes(version.toString('hex'))) { | ||
buffer = version.toString('hex') === '04b2430c' ? | ||
Buffer.concat([Buffer.from('0488ade4','hex'), key]) : // xprv | ||
Buffer.concat([Buffer.from('0488b21e','hex'), key]) // xpub | ||
this.network = bjs.networks.bitcoin | ||
this.isTestnet = false | ||
} | ||
if (pub_types_testnet.includes(version.toString('hex'))) { | ||
buffer = version.toString('hex') === '045f18bc' ? | ||
Buffer.concat([Buffer.from('04358394','hex'), key]) : // tprv | ||
Buffer.concat([Buffer.from('043587cf','hex'), key]) // tpub | ||
this.network = bjs.networks.testnet | ||
this.isTestnet = true | ||
} | ||
return b58.encode(buffer) | ||
} | ||
fromZPub.prototype.getAccountPublic = function () { | ||
@@ -139,0 +176,0 @@ let masterPub = this.isTestnet ? |
@@ -38,3 +38,3 @@ const BIP84 = require('../src/index') | ||
var zpub = 'vpub5Vm8JiyeMgCWT2SqgFkoJyaovNQH8RCF3wAUKCrFAfRdVujdYubBrYUGtggtabj71XxvUQuS5r9AgT4VhGvax9gXEpdi9XBg7jHnvm1WDii' | ||
var account1 = new BIP84.fromZPub(zpub, true) | ||
var account1 = new BIP84.fromZPub(zpub) | ||
@@ -41,0 +41,0 @@ console.log("Account 1, root = m/84'/0'/0'"); |
21372
446