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