diff --git a/src/bip32.js b/src/bip32.js index 0b83ac9..fb4c80d 100644 --- a/src/bip32.js +++ b/src/bip32.js @@ -1,22 +1,19 @@ -var Script = require('./script'), - util = require('./util'), +var util = require('./util'), + Address = require('./address'), conv = require('./convert'), ECKey = require('./eckey').ECKey, ECPubKey = require('./eckey').ECPubKey, base58 = require('./base58'), - Crypto = require('./crypto-js/crypto'), - ECPointFp = require('./jsbn/ec').ECPointFp, - sec = require('./jsbn/sec'), - ecparams = sec("secp256k1"); + Crypto = require('./crypto-js/crypto'); var BIP32key = function(opts) { if (!opts) opts = {} - if (typeof opts == "string") { + if (typeof opts == 'string') { try { - opts = BIP32key.prototype.deserialize(opts); + opts = BIP32key.deserialize(opts); } catch(e) { - opts = BIP32key.prototype.fromMasterKey(opts); + opts = BIP32key.fromMasterKey(opts); } } this.vbytes = opts.vbytes; @@ -32,14 +29,14 @@ var BIP32key = function(opts) { var PRIVDERIV = BIP32key.PRIVDERIV = '\x04\x88\xAD\xE4' var PUBDERIV = BIP32key.PUBDERIV = '\x04\x88\xB2\x1E' -BIP32key.prototype.deserialize = function(str) { +BIP32key.deserialize = function(str) { var bytes = base58.decode(str) var front = bytes.slice(0,bytes.length-4), back = bytes.slice(bytes.length-4); var checksum = Crypto.SHA256(Crypto.SHA256(front,{asBytes: true}), {asBytes: true}) .slice(0,4); - if (""+checksum != ""+back) { - throw new Error("Checksum failed"); + if ('' + checksum != '' + back) { + throw new Error('Checksum failed'); } var type = conv.bytesToString(bytes.slice(0,4)) == PRIVDERIV ? 'priv' : 'pub'; return new BIP32key({ @@ -80,7 +77,7 @@ BIP32key.prototype.ckd = function(i) { blob = [0].concat(priv.slice(0,32),util.numToBytes(i,4).reverse()) } else blob = pub.concat(util.numToBytes(i,4).reverse()) - + I = Crypto.HMAC(Crypto.SHA512,blob,this.chaincode,{ asBytes: true }) if (this.type == 'priv') { @@ -119,8 +116,8 @@ BIP32key.prototype.privtopub = BIP32key.prototype.getPub = function() { }) } -BIP32key.prototype.fromMasterKey = function(seed) { - var I = Bitcoin.Crypto.HMAC(Bitcoin.Crypto.SHA512,seed,"Bitcoin seed",{ asBytes: true }) +BIP32key.fromMasterKey = function(seed) { + var I = Crypto.HMAC(Crypto.SHA512,seed, 'Bitcoin seed' , { asBytes: true }) return new BIP32key({ vbytes: conv.stringToBytes(PRIVDERIV), type: 'priv', @@ -134,4 +131,8 @@ BIP32key.prototype.fromMasterKey = function(seed) { BIP32key.prototype.getKey = function() { return this.key } +BIP32key.prototype.bitcoinAddress = function() { + return new Address(util.sha256ripe160(this.getPub().key.export('bytes'))) +} + module.exports = BIP32key; diff --git a/src/ecdsa.js b/src/ecdsa.js index 53770df..1c1e3b8 100644 --- a/src/ecdsa.js +++ b/src/ecdsa.js @@ -3,6 +3,7 @@ var util = require('./util'); var SecureRandom = require('./jsbn/rng'); var BigInteger = require('./jsbn/jsbn'); var conv = require('./convert') +var Crypto = require('./crypto-js/crypto.js') var ECPointFp = require('./jsbn/ec').ECPointFp; @@ -42,12 +43,12 @@ function deterministicGenerateK(hash,key) { var k = []; for (var i = 0;i < 32;i++) v.push(1); for (var i = 0;i < 32;i++) k.push(0); - k = Bitcoin.Crypto.HMAC(Bitcoin.Crypto.SHA256,v.concat([0]).concat(key).concat(hash),k,{ asBytes: true }) - v = Bitcoin.Crypto.HMAC(Bitcoin.Crypto.SHA256,v,k,{ asBytes: true }) - k = Bitcoin.Crypto.HMAC(Bitcoin.Crypto.SHA256,v.concat([1]).concat(key).concat(hash),k,{ asBytes: true }) - v = Bitcoin.Crypto.HMAC(Bitcoin.Crypto.SHA256,v,k,{ asBytes: true }) - v = Bitcoin.Crypto.HMAC(Bitcoin.Crypto.SHA256,v,k,{ asBytes: true }) - return Bitcoin.BigInteger.fromByteArrayUnsigned(v); + k = Crypto.HMAC(Crypto.SHA256,v.concat([0]).concat(key).concat(hash),k,{ asBytes: true }) + v = Crypto.HMAC(Crypto.SHA256,v,k,{ asBytes: true }) + k = Crypto.HMAC(Crypto.SHA256,v.concat([1]).concat(key).concat(hash),k,{ asBytes: true }) + v = Crypto.HMAC(Crypto.SHA256,v,k,{ asBytes: true }) + v = Crypto.HMAC(Crypto.SHA256,v,k,{ asBytes: true }) + return BigInteger.fromByteArrayUnsigned(v); } var ECDSA = { diff --git a/src/jsbn/ec.js b/src/jsbn/ec.js index 0bfc111..a7642e8 100644 --- a/src/jsbn/ec.js +++ b/src/jsbn/ec.js @@ -376,11 +376,11 @@ ECPointFp.decodeFrom = function (ecparams, enc) { var xBa = enc.slice(1), x = BigInteger.fromByteArrayUnsigned(xBa), p = ecparams.getQ(), - xCubedPlus7 = x.multiply(x).multiply(x).add(new Bitcoin.BigInteger('7')).mod(p), - pPlus1Over4 = p.add(new Bitcoin.BigInteger('1')) - .divide(new Bitcoin.BigInteger('4')), + xCubedPlus7 = x.multiply(x).multiply(x).add(new BigInteger('7')).mod(p), + pPlus1Over4 = p.add(new BigInteger('1')) + .divide(new BigInteger('4')), y = xCubedPlus7.modPow(pPlus1Over4,p); - if (y.mod(new Bitcoin.BigInteger('2')).toString() != ''+(type % 2)) { + if (y.mod(new BigInteger('2')).toString() != ''+(type % 2)) { y = p.subtract(y) } }