|
|
@ -35,14 +35,14 @@ BIP32.prototype.fromRandom = function(network) { |
|
|
|
BIP32.prototype.fromString = function(str) { |
|
|
|
var decoded = base58.decode(str); |
|
|
|
if (decoded.length != 82) |
|
|
|
throw new Error('Not enough data, expected 82 and received ' + decoded.length); |
|
|
|
throw new Error('bip32: Not enough data, expected 82 and received ' + decoded.length); |
|
|
|
var checksum = decoded.slice(78, 82); |
|
|
|
var bytes = decoded.slice(0, 78); |
|
|
|
|
|
|
|
var hash = Hash.sha256sha256(bytes); |
|
|
|
|
|
|
|
if (hash[0] != checksum[0] || hash[1] != checksum[1] || hash[2] != checksum[2] || hash[3] != checksum[3]) |
|
|
|
throw new Error('Invalid checksum'); |
|
|
|
throw new Error('bip32: Invalid checksum'); |
|
|
|
|
|
|
|
if (bytes !== undefined && bytes !== null) |
|
|
|
this.initFromBytes(bytes); |
|
|
@ -110,7 +110,7 @@ BIP32.prototype.initFromBytes = function(bytes) { |
|
|
|
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer()); |
|
|
|
this.hasPrivateKey = false; |
|
|
|
} else { |
|
|
|
throw new Error('Invalid key'); |
|
|
|
throw new Error('bip32: Invalid key'); |
|
|
|
} |
|
|
|
|
|
|
|
this.buildExtendedPublicKey(); |
|
|
@ -162,7 +162,7 @@ BIP32.prototype.extendedPublicKeyString = function(format) { |
|
|
|
} else if (format === 'hex') { |
|
|
|
return this.extendedPublicKey.toString('hex');; |
|
|
|
} else { |
|
|
|
throw new Error('bad format'); |
|
|
|
throw new Error('bip32: bad format'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -198,7 +198,7 @@ BIP32.prototype.extendedPrivateKeyString = function(format) { |
|
|
|
} else if (format === 'hex') { |
|
|
|
return this.extendedPrivateKey.toString('hex'); |
|
|
|
} else { |
|
|
|
throw new Error('bad format'); |
|
|
|
throw new Error('bip32: bad format'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -235,6 +235,9 @@ BIP32.prototype.derive = function(path) { |
|
|
|
} |
|
|
|
|
|
|
|
BIP32.prototype.deriveChild = function(i) { |
|
|
|
if (typeof i !== 'number') |
|
|
|
throw new Error('bip32: i must be a number'); |
|
|
|
|
|
|
|
var ib = []; |
|
|
|
ib.push((i >> 24) & 0xff); |
|
|
|
ib.push((i >> 16) & 0xff); |
|
|
@ -249,7 +252,7 @@ BIP32.prototype.deriveChild = function(i) { |
|
|
|
this.version == constants.testnet.bip32privkey); |
|
|
|
|
|
|
|
if (usePrivate && (!this.hasPrivateKey || !isPrivate)) |
|
|
|
throw new Error('Cannot do private key derivation without private key'); |
|
|
|
throw new Error('bip32: Cannot do private key derivation without private key'); |
|
|
|
|
|
|
|
var ret = null; |
|
|
|
if (this.hasPrivateKey) { |
|
|
@ -324,7 +327,7 @@ BIP32.prototype.toString = function() { |
|
|
|
|
|
|
|
function uint(f, size) { |
|
|
|
if (f.length < size) |
|
|
|
throw new Error('not enough data'); |
|
|
|
throw new Error('bip32: not enough data'); |
|
|
|
var n = 0; |
|
|
|
for (var i = 0; i < size; i++) { |
|
|
|
n *= 256; |
|
|
|