From 43923e3201c8247b8c49ff722be93d00efc57079 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 21 Mar 2016 21:57:20 -0700 Subject: [PATCH] use bn.toBuffer. --- lib/bcoin/chainblock.js | 2 +- lib/bcoin/ec.js | 2 +- lib/bcoin/hd.js | 4 ++-- lib/bcoin/miner.js | 4 ++-- lib/bcoin/output.js | 2 -- lib/bcoin/script.js | 2 +- lib/bcoin/utils.js | 6 ++++++ lib/bcoin/workers.js | 2 +- 8 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/bcoin/chainblock.js b/lib/bcoin/chainblock.js index 365260be..e2d00600 100644 --- a/lib/bcoin/chainblock.js +++ b/lib/bcoin/chainblock.js @@ -252,7 +252,7 @@ ChainBlock.prototype.toRaw = function toRaw() { p.writeU32(this.bits); p.writeU32(this.nonce); p.writeU32(this.height); - p.writeBytes(new Buffer(this.chainwork.toArray('le', 32))); + p.writeBytes(this.chainwork.toBuffer('le', 32)); return p.render(); }; diff --git a/lib/bcoin/ec.js b/lib/bcoin/ec.js index 164d172a..4bc303d6 100644 --- a/lib/bcoin/ec.js +++ b/lib/bcoin/ec.js @@ -37,7 +37,7 @@ ec.generatePrivateKey = function generatePrivateKey() { } while (!secp256k1.privateKeyVerify(priv)); } else { key = ec.elliptic.genKeyPair(); - priv = new Buffer(key.getPrivate().toArray('be', 32)); + priv = key.getPrivate().toBuffer('be', 32); } return priv; diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index fe5e8a42..0a72666b 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -579,10 +579,10 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) { leftPart = new bn(hash.slice(0, 32)); chainCode = hash.slice(32, 64); - privateKey = new Buffer(leftPart + privateKey = leftPart .add(new bn(this.privateKey)) .mod(ec.elliptic.curve.n) - .toArray('be', 32)); + .toBuffer('be', 32); if (!this.fingerPrint) { this.fingerPrint = utils.ripesha(this.publicKey) diff --git a/lib/bcoin/miner.js b/lib/bcoin/miner.js index dbe6ca11..1efe3270 100644 --- a/lib/bcoin/miner.js +++ b/lib/bcoin/miner.js @@ -226,7 +226,7 @@ Miner.prototype.createBlock = function createBlock() { // of ours. This isn't really // necessary nowdays due to bip34 // (used above). - new Buffer(utils.nonce().toArray()), + utils.nonce().toBuffer(), // Let the world know this little // miner succeeded. new Buffer(this.coinbaseFlags || 'mined by bcoin', 'ascii') @@ -276,7 +276,7 @@ Miner.prototype.updateCoinbase = function updateCoinbase(block) { if (!block) block = this.block; - coinbase.inputs[0].script[1] = new Buffer(block.extraNonce.toArray()); + coinbase.inputs[0].script[1] = block.extraNonce.toBuffer(); coinbase.outputs[0].value = reward.add(this.fee); }; diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index f8dcd83b..f3a9cd85 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -38,8 +38,6 @@ function Output(options, tx) { // Numbers, do not allow negative values. assert(typeof value !== 'number'); assert(!this.value.isNeg()); - assert(this.value.bitLength() <= 63); - assert(!(this.value.toArray('be', 8)[0] & 0x80)); } Output.prototype.__defineGetter__('type', function() { diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 46ebe358..dd61ee72 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -1188,7 +1188,7 @@ Script.array = function(value) { if (value.cmpn(0) === 0) return new Buffer([]); - return new Buffer(value.toArray('le')); + return value.toBuffer('le'); }; Script.prototype.removeData = function removeData(data) { diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 47ca2ebd..890a22fb 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -1912,3 +1912,9 @@ SyncBatch.prototype.clear = function clear() { }; utils.SyncBatch = SyncBatch; + +if (utils.isBrowser) { + bn.prototype.toBuffer = function toBuffer(order, size) { + return new Buffer(this.toArray(order, size)); + }; +} diff --git a/lib/bcoin/workers.js b/lib/bcoin/workers.js index df4b4f2b..7e1cb926 100644 --- a/lib/bcoin/workers.js +++ b/lib/bcoin/workers.js @@ -209,7 +209,7 @@ function createBody(name, items) { } else if (item instanceof bcoin.coin) { p.writeU8(8); p.writeVarBytes(item.toExtended()); - } else if (item instanceof bn) + } else if (item instanceof bn) { p.writeU8(9); p.writeVarBytes(item.toBuffer()); } else if (Buffer.isBuffer(item)) {