diff --git a/lib/bcoin.js b/lib/bcoin.js index 7b64c3c2..a638a1af 100644 --- a/lib/bcoin.js +++ b/lib/bcoin.js @@ -1,11 +1,7 @@ var bcoin = exports; var elliptic = require('elliptic'); -if (elliptic.ec) { - bcoin.ecdsa = elliptic.ec('secp256k1'); -} else { - bcoin.ecdsa = elliptic.ecdsa(elliptic.nist.secp256k1); -} +bcoin.ecdsa = elliptic.ec('secp256k1'); bcoin.utils = require('./bcoin/utils'); bcoin.bloom = require('./bcoin/bloom'); bcoin.protocol = require('./bcoin/protocol'); diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index 8a77237b..6e67693f 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -62,16 +62,7 @@ var crypto = require('crypto'); var english = require('../../etc/english.json'); -var ec; -if (!elliptic.curves) { - ec = elliptic.nist.secp256k1; - ec.curve.g = ec.g; - ec.curve.n = ec.n; -} else { - ec = elliptic.curves.secp256k1; -} -var ecPoint = ec.curve.point.bind(ec.curve); -var ecPointFromX = ec.curve.pointFromX.bind(ec.curve); +var ec = elliptic.curves.secp256k1; /** * HD Seeds @@ -333,6 +324,8 @@ HDPriv.prototype.derive = function(index, hard) { var leftPart = new bn(hash.slice(0, 32)); var chainCode = hash.slice(32, 64); + // XXX This causes a call stack overflow with bn.js@4.0.5 and elliptic@3.0.3 + // Example: new bn(0).mod(ec.curve.n) var privateKey = leftPart.add(new bn(this.privateKey)).mod(ec.curve.n).toArray(); return new HDPriv({ diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index ea1909fa..b1243ba5 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -242,7 +242,7 @@ TX.prototype.out = function out(output, value) { var keys = output.keys || output.address; if (keys === output.address) { keys = keys.map(function(address) { - return bcoin.wallet.addr2hash(address); + return bcoin.wallet.addr2hash(address, 'normal'); }); } keys = keys.map(function(key) { @@ -275,7 +275,7 @@ TX.prototype.out = function out(output, value) { script = [ 'dup', 'hash160', - bcoin.wallet.addr2hash(output.address), + bcoin.wallet.addr2hash(output.address, 'normal'), 'eqverify', 'checksig' ]; @@ -383,7 +383,7 @@ TX.prototype.inputAddrs = function inputAddrs() { }).map(function(input) { var pub = input.script[1]; var hash = utils.ripesha(pub); - return bcoin.wallet.hash2addr(hash); + return bcoin.wallet.hash2addr(hash, 'normal'); }); }; diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 78309e2f..6f1eb0f6 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -46,18 +46,14 @@ function Wallet(options, passphrase) { entropy: hash.sha256().update(options.passphrase).digest() }); } else if (options.priv || options.pub) { - this.key = bcoin.ecdsa.keyPair(options.priv || options.pub, 'hex'); + this.key = bcoin.ecdsa.keyPair({ + priv: options.priv, + pub: options.pub + }); } else { this.key = bcoin.ecdsa.genKeyPair(); } - this.prefix = 'bt/' + this.getAddress() + '/'; - this.tx = new bcoin.txPool(this); - - // Just a constants, actually - this.fee = 10000; - this.dust = 5460; - this.addressType = options.addressType || 'normal'; // Multisig @@ -82,6 +78,13 @@ function Wallet(options, passphrase) { throw new Error(this.m + ' public keys required'); } + this.prefix = 'bt/' + this.getAddress() + '/'; + this.tx = new bcoin.txPool(this); + + // Just a constants, actually + this.fee = 10000; + this.dust = 5460; + this._init(); } inherits(Wallet, EventEmitter); @@ -165,13 +168,13 @@ Wallet.prototype.getHash = function getHash() { }; Wallet.prototype.getAddress = function getAddress() { - return Wallet.hash2addr(this.getHash()); + return Wallet.hash2addr(this.getHash(), this.addressType); }; Wallet.hash2addr = function hash2addr(hash, version) { hash = utils.toArray(hash, 'hex'); - version = constants.addr[version || this.addressType]; + version = constants.addr[version || 'normal']; hash = [ version ].concat(hash); var addr = hash.concat(utils.checksum(hash)); @@ -182,7 +185,7 @@ Wallet.addr2hash = function addr2hash(addr, version) { if (!Array.isArray(addr)) addr = utils.fromBase58(addr); - version = constants.addr[version || this.addressType]; + version = constants.addr[version || 'normal']; if (addr.length !== 25) return []; diff --git a/package.json b/package.json index 26759a4d..9037ca85 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "dependencies": { "async": "^0.8.0", "bn.js": "^0.10.0", - "elliptic": "^0.14.1", + "elliptic": "^3.0.3", "hash.js": "^0.2.0", "inherits": "^2.0.1" },