|
|
@ -37,14 +37,14 @@ BIP32.prototype.fromRandom = function(network) { |
|
|
|
BIP32.prototype.fromString = function(str) { |
|
|
|
var decoded = base58.decode(str); |
|
|
|
if (decoded.length != 82) |
|
|
|
throw new Error('bip32: Not enough data, expected 82 and received ' + decoded.length); |
|
|
|
throw new Error('gcNot 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('bip32: Invalid checksum'); |
|
|
|
throw new Error('gcInvalid checksum'); |
|
|
|
|
|
|
|
if (bytes !== undefined && bytes !== null) |
|
|
|
this.initFromBytes(bytes); |
|
|
@ -55,11 +55,11 @@ BIP32.prototype.fromSeed = function(bytes, network) { |
|
|
|
network = 'mainnet'; |
|
|
|
|
|
|
|
if (!Buffer.isBuffer(bytes)) |
|
|
|
throw new Error('bip32: bytes must be a buffer'); |
|
|
|
throw new Error('gcbytes must be a buffer'); |
|
|
|
if (bytes.length < 128 / 8) |
|
|
|
throw new Error('bip32: Need more than 128 bytes of entropy'); |
|
|
|
throw new Error('gcNeed more than 128 bytes of entropy'); |
|
|
|
if (bytes.length > 512 / 8) |
|
|
|
throw new Error('bip32: More than 512 bytes of entropy is nonstandard'); |
|
|
|
throw new Error('gcMore than 512 bytes of entropy is nonstandard'); |
|
|
|
var hash = Hash.sha512hmac(bytes, new Buffer('Bitcoin seed')); |
|
|
|
|
|
|
|
this.depth = 0x00; |
|
|
@ -82,7 +82,7 @@ BIP32.prototype.fromSeed = function(bytes, network) { |
|
|
|
BIP32.prototype.initFromBytes = function(bytes) { |
|
|
|
// Both pub and private extended keys are 78 bytes
|
|
|
|
if (bytes.length != 78) |
|
|
|
throw new Error('bip32: not enough data'); |
|
|
|
throw new Error('gcnot enough data'); |
|
|
|
|
|
|
|
this.version = u32(bytes.slice(0, 4)); |
|
|
|
this.depth = u8(bytes.slice(4, 5)); |
|
|
@ -112,7 +112,7 @@ BIP32.prototype.initFromBytes = function(bytes) { |
|
|
|
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer()); |
|
|
|
this.hasPrivateKey = false; |
|
|
|
} else { |
|
|
|
throw new Error('bip32: Invalid key'); |
|
|
|
throw new Error('gcInvalid key'); |
|
|
|
} |
|
|
|
|
|
|
|
this.buildExtendedPublicKey(); |
|
|
@ -133,7 +133,7 @@ BIP32.prototype.buildExtendedPublicKey = function() { |
|
|
|
v = constants.testnet.bip32pubkey; |
|
|
|
break; |
|
|
|
default: |
|
|
|
throw new Error('bip32: Unknown version'); |
|
|
|
throw new Error('gcUnknown version'); |
|
|
|
} |
|
|
|
|
|
|
|
// Version
|
|
|
@ -164,7 +164,7 @@ BIP32.prototype.extendedPublicKeyString = function(format) { |
|
|
|
} else if (format === 'hex') { |
|
|
|
return this.extendedPublicKey.toString('hex');; |
|
|
|
} else { |
|
|
|
throw new Error('bip32: bad format'); |
|
|
|
throw new Error('gcbad format'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -200,7 +200,7 @@ BIP32.prototype.extendedPrivateKeyString = function(format) { |
|
|
|
} else if (format === 'hex') { |
|
|
|
return this.extendedPrivateKey.toString('hex'); |
|
|
|
} else { |
|
|
|
throw new Error('bip32: bad format'); |
|
|
|
throw new Error('gcbad format'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -217,12 +217,12 @@ BIP32.prototype.derive = function(path) { |
|
|
|
var c = e[i]; |
|
|
|
|
|
|
|
if (i == 0) { |
|
|
|
if (c != 'm') throw new Error('bip32: invalid path'); |
|
|
|
if (c != 'm') throw new Error('gcinvalid path'); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
if (parseInt(c.replace("'", "")).toString() !== c.replace("'", "")) |
|
|
|
throw new Error('bip32: invalid path'); |
|
|
|
throw new Error('gcinvalid path'); |
|
|
|
|
|
|
|
var usePrivate = (c.length > 1) && (c[c.length - 1] == '\''); |
|
|
|
var childIndex = parseInt(usePrivate ? c.slice(0, c.length - 1) : c) & 0x7fffffff; |
|
|
@ -238,7 +238,7 @@ BIP32.prototype.derive = function(path) { |
|
|
|
|
|
|
|
BIP32.prototype.deriveChild = function(i) { |
|
|
|
if (typeof i !== 'number') |
|
|
|
throw new Error('bip32: i must be a number'); |
|
|
|
throw new Error('gci must be a number'); |
|
|
|
|
|
|
|
var ib = []; |
|
|
|
ib.push((i >> 24) & 0xff); |
|
|
@ -254,7 +254,7 @@ BIP32.prototype.deriveChild = function(i) { |
|
|
|
this.version == constants.testnet.bip32privkey); |
|
|
|
|
|
|
|
if (usePrivate && (!this.hasPrivateKey || !isPrivate)) |
|
|
|
throw new Error('bip32: Cannot do private key derivation without private key'); |
|
|
|
throw new Error('gcCannot do private key derivation without private key'); |
|
|
|
|
|
|
|
var ret = null; |
|
|
|
if (this.hasPrivateKey) { |
|
|
@ -329,7 +329,7 @@ BIP32.prototype.toString = function() { |
|
|
|
|
|
|
|
function uint(f, size) { |
|
|
|
if (f.length < size) |
|
|
|
throw new Error('bip32: not enough data'); |
|
|
|
throw new Error('gcnot enough data'); |
|
|
|
var n = 0; |
|
|
|
for (var i = 0; i < size; i++) { |
|
|
|
n *= 256; |
|
|
|