bring formatting in line with bitcore standards
This commit is contained in:
parent
fa47ee9984
commit
31a024a20b
70
BIP32.js
70
BIP32.js
|
@ -1,3 +1,6 @@
|
|||
var EncodedData = require('./util/EncodedData');
|
||||
var base58 = imports.base58 || require('base58-native').base58Check;
|
||||
|
||||
var BITCOIN_MAINNET_PUBLIC = 0x0488b21e;
|
||||
var BITCOIN_MAINNET_PRIVATE = 0x0488ade4;
|
||||
var BITCOIN_TESTNET_PUBLIC = 0x043587cf;
|
||||
|
@ -13,29 +16,30 @@ var LITECOIN_TESTNET_PRIVATE = 0x0436ef7d;
|
|||
|
||||
var BIP32 = function(bytes) {
|
||||
// decode base58
|
||||
if( typeof bytes === "string" ) {
|
||||
var decoded = Bitcoin.Base58.decode(bytes);
|
||||
if( decoded.length != 82 ) throw new Error("Not enough data");
|
||||
if (typeof bytes === "string") {
|
||||
var decoded = base58.decode(bytes);
|
||||
if (decoded.length != 82)
|
||||
throw new Error("Not enough data");
|
||||
var checksum = decoded.slice(78, 82);
|
||||
bytes = decoded.slice(0, 78);
|
||||
|
||||
var hash = Crypto.SHA256( Crypto.SHA256( bytes, { asBytes: true } ), { asBytes: true } );
|
||||
var hash = Crypto.SHA256(Crypto.SHA256(bytes, {asBytes: true}), {asBytes: true});
|
||||
|
||||
if( hash[0] != checksum[0] || hash[1] != checksum[1] || hash[2] != checksum[2] || hash[3] != checksum[3] ) {
|
||||
if (hash[0] != checksum[0] || hash[1] != checksum[1] || hash[2] != checksum[2] || hash[3] != checksum[3]) {
|
||||
throw new Error("Invalid checksum");
|
||||
}
|
||||
}
|
||||
|
||||
if( bytes !== undefined )
|
||||
if (bytes !== undefined)
|
||||
this.init_from_bytes(bytes);
|
||||
}
|
||||
|
||||
BIP32.prototype.init_from_bytes = function(bytes) {
|
||||
// Both pub and private extended keys are 78 bytes
|
||||
if( bytes.length != 78 ) throw new Error("not enough data");
|
||||
if(bytes.length != 78) throw new Error("not enough data");
|
||||
|
||||
this.version = u32(bytes.slice(0, 4));
|
||||
this.depth = u8 (bytes.slice(4, 5));
|
||||
this.depth = u8(bytes.slice(4, 5));
|
||||
this.parent_fingerprint = bytes.slice(5, 9);
|
||||
this.child_index = u32(bytes.slice(9, 13));
|
||||
this.chain_code = bytes.slice(13, 45);
|
||||
|
@ -58,7 +62,7 @@ BIP32.prototype.init_from_bytes = function(bytes) {
|
|||
this.version == LITECOIN_MAINNET_PUBLIC ||
|
||||
this.version == LITECOIN_TESTNET_PUBLIC );
|
||||
|
||||
if( is_private && key_bytes[0] == 0 ) {
|
||||
if (is_private && key_bytes[0] == 0) {
|
||||
this.eckey = new Bitcoin.ECKey(key_bytes.slice(1, 33));
|
||||
this.eckey.setCompressed(true);
|
||||
|
||||
|
@ -67,7 +71,7 @@ BIP32.prototype.init_from_bytes = function(bytes) {
|
|||
this.eckey.pub = pt;
|
||||
this.eckey.pubKeyHash = Bitcoin.Util.sha256ripe160(this.eckey.pub.getEncoded(true));
|
||||
this.has_private_key = true;
|
||||
} else if( is_public && (key_bytes[0] == 0x02 || key_bytes[0] == 0x03) ) {
|
||||
} else if (is_public && (key_bytes[0] == 0x02 || key_bytes[0] == 0x03)) {
|
||||
this.eckey = new Bitcoin.ECKey();
|
||||
this.eckey.pub = decompress_pubkey(key_bytes);
|
||||
this.eckey.pubKeyHash = Bitcoin.Util.sha256ripe160(this.eckey.pub.getEncoded(true));
|
||||
|
@ -140,12 +144,12 @@ BIP32.prototype.build_extended_public_key = function() {
|
|||
}
|
||||
|
||||
BIP32.prototype.extended_public_key_string = function(format) {
|
||||
if( format === undefined || format === "base58" ) {
|
||||
var hash = Crypto.SHA256( Crypto.SHA256( this.extended_public_key, { asBytes: true } ), { asBytes: true } );
|
||||
if (format === undefined || format === "base58") {
|
||||
var hash = Crypto.SHA256(Crypto.SHA256(this.extended_public_key, {asBytes: true} ), {asBytes: true});
|
||||
var checksum = hash.slice(0, 4);
|
||||
var data = this.extended_public_key.concat(checksum);
|
||||
return Bitcoin.Base58.encode(data);
|
||||
} else if( format === "hex" ) {
|
||||
} else if (format === "hex") {
|
||||
return Crypto.util.bytesToHex(this.extended_public_key);
|
||||
} else {
|
||||
throw new Error("bad format");
|
||||
|
@ -153,7 +157,7 @@ BIP32.prototype.extended_public_key_string = function(format) {
|
|||
}
|
||||
|
||||
BIP32.prototype.build_extended_private_key = function() {
|
||||
if( !this.has_private_key ) return;
|
||||
if (!this.has_private_key) return;
|
||||
this.extended_private_key = [];
|
||||
|
||||
var v = this.version;
|
||||
|
@ -185,8 +189,8 @@ BIP32.prototype.build_extended_private_key = function() {
|
|||
}
|
||||
|
||||
BIP32.prototype.extended_private_key_string = function(format) {
|
||||
if( format === undefined || format === "base58" ) {
|
||||
var hash = Crypto.SHA256( Crypto.SHA256( this.extended_private_key, { asBytes: true } ), { asBytes: true } );
|
||||
if (format === undefined || format === "base58") {
|
||||
var hash = Crypto.SHA256(Crypto.SHA256(this.extended_private_key, {asBytes: true}), {asBytes: true});
|
||||
var checksum = hash.slice(0, 4);
|
||||
var data = this.extended_private_key.concat(checksum);
|
||||
return Bitcoin.Base58.encode(data);
|
||||
|
@ -202,14 +206,15 @@ BIP32.prototype.derive = function(path) {
|
|||
var e = path.split('/');
|
||||
|
||||
// Special cases:
|
||||
if( path == 'm' || path == 'M' || path == 'm\'' || path == 'M\'' ) return this;
|
||||
if (path == 'm' || path == 'M' || path == 'm\'' || path == 'M\'')
|
||||
return this;
|
||||
|
||||
var bip32 = this;
|
||||
for( var i in e ) {
|
||||
for (var i in e) {
|
||||
var c = e[i];
|
||||
|
||||
if( i == 0 ) {
|
||||
if( c != 'm' ) throw new Error("invalid path");
|
||||
if (i == 0 ) {
|
||||
if (c != 'm') throw new Error("invalid path");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -227,10 +232,10 @@ BIP32.prototype.derive = function(path) {
|
|||
|
||||
BIP32.prototype.derive_child = function(i) {
|
||||
var ib = [];
|
||||
ib.push( (i >> 24) & 0xff );
|
||||
ib.push( (i >> 16) & 0xff );
|
||||
ib.push( (i >> 8) & 0xff );
|
||||
ib.push( i & 0xff );
|
||||
ib.push((i >> 24) & 0xff);
|
||||
ib.push((i >> 16) & 0xff);
|
||||
ib.push((i >> 8) & 0xff);
|
||||
ib.push(i & 0xff );
|
||||
|
||||
var use_private = (i & 0x80000000) != 0;
|
||||
var ecparams = getSECCurveByName("secp256k1");
|
||||
|
@ -243,13 +248,14 @@ BIP32.prototype.derive_child = function(i) {
|
|||
this.version == LITECOIN_MAINNET_PRIVATE ||
|
||||
this.version == LITECOIN_TESTNET_PRIVATE);
|
||||
|
||||
if( use_private && (!this.has_private_key || !is_private) ) throw new Error("Cannot do private key derivation without private key");
|
||||
if (use_private && (!this.has_private_key || !is_private))
|
||||
throw new Error("Cannot do private key derivation without private key");
|
||||
|
||||
var ret = null;
|
||||
if( this.has_private_key ) {
|
||||
if (this.has_private_key) {
|
||||
var data = null;
|
||||
|
||||
if( use_private ) {
|
||||
if (use_private) {
|
||||
data = [0].concat(this.eckey.priv.toByteArrayUnsigned()).concat(ib);
|
||||
} else {
|
||||
data = this.eckey.pub.getEncoded(true).concat(ib);
|
||||
|
@ -315,10 +321,10 @@ function uint(f, size) {
|
|||
return n;
|
||||
}
|
||||
|
||||
function u8(f) { return uint(f,1); }
|
||||
function u16(f) { return uint(f,2); }
|
||||
function u32(f) { return uint(f,4); }
|
||||
function u64(f) { return uint(f,8); }
|
||||
function u8(f) {return uint(f,1);}
|
||||
function u16(f) {return uint(f,2);}
|
||||
function u32(f) {return uint(f,4);}
|
||||
function u64(f) {return uint(f,8);}
|
||||
|
||||
function decompress_pubkey(key_bytes) {
|
||||
var y_bit = u8(key_bytes.slice(0, 1)) & 0x01;
|
||||
|
@ -341,7 +347,7 @@ function decompress_pubkey(key_bytes) {
|
|||
var y = tmp.modSqrt(p);
|
||||
|
||||
// flip sign if we need to
|
||||
if( (y[0] & 0x01) != y_bit ) {
|
||||
if ((y[0] & 0x01) != y_bit) {
|
||||
y = y.multiply(new BigInteger("-1")).mod(p);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue