From af1f121fd4066ecf6eb60fee81b78165c600fa43 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Sat, 3 May 2014 10:04:54 +0800 Subject: [PATCH] upgrade bigi & remove monkey patching --- package.json | 2 +- src/base58.js | 2 +- src/bigi.js | 38 -------------------------------------- src/ec.js | 2 +- src/ecdsa.js | 4 +++- src/eckey.js | 2 +- src/hdwallet.js | 2 +- src/index.js | 1 - src/sec.js | 2 +- src/transaction.js | 2 +- test/bigi.js | 46 ---------------------------------------------- test/ec.js | 2 +- test/ecdsa.js | 2 +- 13 files changed, 12 insertions(+), 95 deletions(-) delete mode 100644 src/bigi.js delete mode 100644 test/bigi.js diff --git a/package.json b/package.json index 1734e24..cd84045 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "compile": "./node_modules/.bin/browserify ./src/index.js -s Bitcoin | ./node_modules/.bin/uglifyjs > bitcoinjs-min.js" }, "dependencies": { - "bigi": "0.2.0", + "bigi": "1.0.0", "crypto-js": "3.1.2-3", "secure-random": "0.2.1" } diff --git a/src/base58.js b/src/base58.js index f6f86a1..588ac84 100644 --- a/src/base58.js +++ b/src/base58.js @@ -5,7 +5,7 @@ // Merged Buffer refactorings from base58-native by Stephen Pair // Copyright (c) 2013 BitPay Inc -var BigInteger = require('./bigi') +var BigInteger = require('bigi') var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' var ALPHABET_BUF = new Buffer(ALPHABET, 'ascii') diff --git a/src/bigi.js b/src/bigi.js deleted file mode 100644 index 118db69..0000000 --- a/src/bigi.js +++ /dev/null @@ -1,38 +0,0 @@ -var assert = require('assert') -var BigInteger = require('bigi') - -// Import operations -BigInteger.fromHex = function(hex) { - var buffer = new Buffer(hex, 'hex') - assert.equal(buffer.length, Buffer.byteLength(hex) / 2) - - return BigInteger.fromBuffer(buffer) -} - -BigInteger.fromBuffer = function(buffer) { - assert(Array.isArray(buffer) || Buffer.isBuffer(buffer)) // FIXME: Transitionary - - // FIXME: Transitionary - if (Buffer.isBuffer(buffer)) { - buffer = Array.prototype.slice.call(buffer) - } - - return BigInteger.fromByteArrayUnsigned(buffer) -} - -// Export operations -BigInteger.prototype.toBuffer = function(s) { - if (s != undefined) assert(Number.isFinite(s)) - - var buffer = new Buffer(this.toByteArrayUnsigned()) - var padded = new Buffer(s - buffer.length) - padded.fill(0) - - return Buffer.concat([padded, buffer], s) -} - -BigInteger.prototype.toHex = function(s) { - return this.toBuffer(s).toString('hex') -} - -module.exports = BigInteger diff --git a/src/ec.js b/src/ec.js index d06bf83..9f450f8 100644 --- a/src/ec.js +++ b/src/ec.js @@ -2,7 +2,7 @@ // Ported loosely from BouncyCastle's Java EC code // Only Fp curves implemented for now -var BigInteger = require('./bigi') +var BigInteger = require('bigi') function ECFieldElementFp(q,x) { this.x = x; diff --git a/src/ecdsa.js b/src/ecdsa.js index 6175aae..451ba4b 100644 --- a/src/ecdsa.js +++ b/src/ecdsa.js @@ -3,7 +3,7 @@ var crypto = require('crypto') var sec = require('./sec') var ecparams = sec("secp256k1") -var BigInteger = require('./bigi') +var BigInteger = require('bigi') var ECPointFp = require('./ec').ECPointFp var P_OVER_FOUR = null @@ -178,6 +178,8 @@ var ecdsa = { * } */ parseSig: function (sig) { + if(Array.isArray(sig)) sig = new Buffer(sig); + var cursor if (sig[0] != 0x30) { throw new Error("Signature not a valid DERSequence") diff --git a/src/eckey.js b/src/eckey.js index bb2b3a0..87d2934 100644 --- a/src/eckey.js +++ b/src/eckey.js @@ -10,7 +10,7 @@ var crypto = require('./crypto') var sec = require('./sec') var ecparams = sec('secp256k1') -var BigInteger = require('./bigi') +var BigInteger = require('bigi') var ECPointFp = require('./ec').ECPointFp function ECKey(D, compressed) { diff --git a/src/hdwallet.js b/src/hdwallet.js index 0840723..7a475cf 100644 --- a/src/hdwallet.js +++ b/src/hdwallet.js @@ -3,7 +3,7 @@ var base58 = require('./base58') var convert = require('./convert') var Address = require('./address') -var BigInteger = require('./bigi') +var BigInteger = require('bigi') var CJS = require('crypto-js') var crypto = require('./crypto') var ECKey = require('./eckey').ECKey diff --git a/src/index.js b/src/index.js index 81414e2..8d645d7 100644 --- a/src/index.js +++ b/src/index.js @@ -6,7 +6,6 @@ module.exports = { Address: require('./address'), base58: require('./base58'), base58check: require('./base58check'), - BigInteger: require('./bigi'), convert: require('./convert'), crypto: require('./crypto'), ec: ec, diff --git a/src/sec.js b/src/sec.js index 6aa9e6f..79bc51a 100644 --- a/src/sec.js +++ b/src/sec.js @@ -1,6 +1,6 @@ // Named EC curves -var BigInteger = require('./bigi') +var BigInteger = require('bigi') var ECCurveFp = require('./ec') var ECPointFp = ECCurveFp.ECPointFp diff --git a/src/transaction.js b/src/transaction.js index f346125..69b818b 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -2,7 +2,7 @@ var assert = require('assert') var Address = require('./address') -var BigInteger = require('./bigi') +var BigInteger = require('bigi') var Script = require('./script') var convert = require('./convert') var crypto = require('./crypto') diff --git a/test/bigi.js b/test/bigi.js deleted file mode 100644 index 4df4aad..0000000 --- a/test/bigi.js +++ /dev/null @@ -1,46 +0,0 @@ -var assert = require('assert') -var BigInteger = require('../').BigInteger - -var fixtures = require('./fixtures/bigi') - -describe('BigInteger', function() { - describe('fromBuffer/fromHex', function() { - it('should match the test vectors', function() { - fixtures.valid.forEach(function(f) { - assert.equal(BigInteger.fromHex(f.hex).toString(), f.dec) - assert.equal(BigInteger.fromHex(f.hexPadded).toString(), f.dec) - }) - }) - - fixtures.invalid.forEach(function(f) { - it('throws on ' + f.description, function() { - assert.throws(function() { - BigInteger.fromHex(f.string) - }) - }) - }) - }) - - describe('toBuffer/toHex', function() { - it('should match the test vectors', function() { - fixtures.valid.forEach(function(f) { - var bi = new BigInteger(f.dec) - - assert.equal(bi.toHex(), f.hex) - assert.equal(bi.toHex(32), f.hexPadded) - }) - }) - - it('throws on non-finite padding value', function() { - var bi = new BigInteger('1') - - assert.throws(function() { bi.toHex({}) }) - assert.throws(function() { bi.toHex([]) }) - assert.throws(function() { bi.toHex('') }) - assert.throws(function() { bi.toHex(0 / 0) }) - assert.throws(function() { bi.toHex(1 / 0) }) - }) - }) -}) - -module.exports = BigInteger diff --git a/test/ec.js b/test/ec.js index dc74cf3..c9fc3c8 100644 --- a/test/ec.js +++ b/test/ec.js @@ -3,7 +3,7 @@ var assert = require('assert') var sec = require('../').sec var ecparams = sec('secp256k1') -var BigInteger = require('..').BigInteger +var BigInteger = require('bigi') var ECPointFp = require('../').ECPointFp describe('ec', function() { diff --git a/test/ecdsa.js b/test/ecdsa.js index 1516087..fedeb6c 100644 --- a/test/ecdsa.js +++ b/test/ecdsa.js @@ -4,7 +4,7 @@ var ecdsa = require('..').ecdsa var sec = require('..').sec var ecparams = sec("secp256k1") -var BigInteger = require('..').BigInteger +var BigInteger = require('bigi') var ECKey = require('..').ECKey var ECPubKey = require('..').ECPubKey var Message = require('..').Message