|
|
@ -1,7 +1,7 @@ |
|
|
|
'use strict'; |
|
|
|
|
|
|
|
var Base58Check = require('./protocol/base58check'); |
|
|
|
var constants = require('./protocol/constants'); |
|
|
|
var Base58Check = require('./encoding/base58check'); |
|
|
|
var networks = require('./networks'); |
|
|
|
var Hash = require('./crypto/hash'); |
|
|
|
var Point = require('./crypto/point'); |
|
|
|
var Random = require('./crypto/random'); |
|
|
@ -38,7 +38,7 @@ BIP32.prototype.set = function(obj) { |
|
|
|
BIP32.prototype.fromRandom = function(networkstr) { |
|
|
|
if (!networkstr) |
|
|
|
networkstr = 'mainnet'; |
|
|
|
this.version = constants[networkstr].bip32privkey; |
|
|
|
this.version = networks[networkstr].bip32privkey; |
|
|
|
this.depth = 0x00; |
|
|
|
this.parentfingerprint = new Buffer([0, 0, 0, 0]); |
|
|
|
this.childindex = new Buffer([0, 0, 0, 0]); |
|
|
@ -72,7 +72,7 @@ BIP32.prototype.fromSeed = function(bytes, networkstr) { |
|
|
|
this.parentfingerprint = new Buffer([0, 0, 0, 0]); |
|
|
|
this.childindex = new Buffer([0, 0, 0, 0]); |
|
|
|
this.chaincode = hash.slice(32, 64); |
|
|
|
this.version = constants[networkstr].bip32privkey; |
|
|
|
this.version = networks[networkstr].bip32privkey; |
|
|
|
this.keypair = new Keypair(); |
|
|
|
this.keypair.privkey = new Privkey({bn: BN().fromBuffer(hash.slice(0, 32))}); |
|
|
|
this.keypair.privkey2pubkey(); |
|
|
@ -99,12 +99,12 @@ BIP32.prototype.initFromBytes = function(bytes) { |
|
|
|
var keyBytes = bytes.slice(45, 78); |
|
|
|
|
|
|
|
var isPrivate = |
|
|
|
(this.version == constants.mainnet.bip32privkey || |
|
|
|
this.version == constants.testnet.bip32privkey); |
|
|
|
(this.version == networks.mainnet.bip32privkey || |
|
|
|
this.version == networks.testnet.bip32privkey); |
|
|
|
|
|
|
|
var isPublic = |
|
|
|
(this.version == constants.mainnet.bip32pubkey || |
|
|
|
this.version == constants.testnet.bip32pubkey); |
|
|
|
(this.version == networks.mainnet.bip32pubkey || |
|
|
|
this.version == networks.testnet.bip32pubkey); |
|
|
|
|
|
|
|
if (isPrivate && keyBytes[0] == 0) { |
|
|
|
this.keypair = new Keypair(); |
|
|
@ -130,13 +130,13 @@ BIP32.prototype.buildxpubkey = function() { |
|
|
|
|
|
|
|
var v = null; |
|
|
|
switch (this.version) { |
|
|
|
case constants.mainnet.bip32pubkey: |
|
|
|
case constants.mainnet.bip32privkey: |
|
|
|
v = constants.mainnet.bip32pubkey; |
|
|
|
case networks.mainnet.bip32pubkey: |
|
|
|
case networks.mainnet.bip32privkey: |
|
|
|
v = networks.mainnet.bip32pubkey; |
|
|
|
break; |
|
|
|
case constants.testnet.bip32pubkey: |
|
|
|
case constants.testnet.bip32privkey: |
|
|
|
v = constants.testnet.bip32pubkey; |
|
|
|
case networks.testnet.bip32pubkey: |
|
|
|
case networks.testnet.bip32privkey: |
|
|
|
v = networks.testnet.bip32pubkey; |
|
|
|
break; |
|
|
|
default: |
|
|
|
throw new Error('Unknown version'); |
|
|
@ -248,8 +248,8 @@ BIP32.prototype.deriveChild = function(i) { |
|
|
|
var usePrivate = (i & 0x80000000) != 0; |
|
|
|
|
|
|
|
var isPrivate = |
|
|
|
(this.version == constants.mainnet.bip32privkey || |
|
|
|
this.version == constants.testnet.bip32privkey); |
|
|
|
(this.version == networks.mainnet.bip32privkey || |
|
|
|
this.version == networks.testnet.bip32privkey); |
|
|
|
|
|
|
|
if (usePrivate && (!this.hasprivkey || !isPrivate)) |
|
|
|
throw new Error('Cannot do private key derivation without private key'); |
|
|
@ -316,8 +316,8 @@ BIP32.prototype.deriveChild = function(i) { |
|
|
|
|
|
|
|
BIP32.prototype.toString = function() { |
|
|
|
var isPrivate = |
|
|
|
(this.version == constants.mainnet.bip32privkey || |
|
|
|
this.version == constants.testnet.bip32privkey); |
|
|
|
(this.version == networks.mainnet.bip32privkey || |
|
|
|
this.version == networks.testnet.bip32privkey); |
|
|
|
|
|
|
|
if (isPrivate) |
|
|
|
return this.xprivkeyString(); |
|
|
|