diff --git a/Key.js b/Key.js index 550c40f..48f661b 100644 --- a/Key.js +++ b/Key.js @@ -5,7 +5,7 @@ if (process.versions) { module.exports = require('bindings')('KeyModule'); } else { // pure js version - var ECKey = require('./browser/bitcoinjs-lib.js').ECKey; + var ECKey = require('./browser/vendor.js').ECKey; var buffertools = require('buffertools'); var bufferToArray = function(buffer) { diff --git a/browser/bitcoin.js b/browser/bitcoin.js deleted file mode 100644 index 49e3ae4..0000000 --- a/browser/bitcoin.js +++ /dev/null @@ -1,2 +0,0 @@ -Bitcoin = {}; - diff --git a/browser/browser.js b/browser/browser.js deleted file mode 100644 index d1db04b..0000000 --- a/browser/browser.js +++ /dev/null @@ -1 +0,0 @@ -if ('undefined' === typeof window) window = this; diff --git a/browser/concat.sh b/browser/concat.sh index 21582e1..97036fc 100755 --- a/browser/concat.sh +++ b/browser/concat.sh @@ -1,3 +1,7 @@ #! /bin/bash -cat browser.js crypto.js ripemd160.js bitcoin.js navigator-adapter.js jsbn.js jsbn2.js prng4.js util.js rng.js ec.js sec.js ecdsa.js eckey.js > bitcoinjs-lib.js +cd vendor/ +cat browser-adapter.js crypto.js ripemd160.js jsbn.js jsbn2.js prng4.js util.js rng.js ec.js sec.js ecdsa.js eckey.js > vendor.js +mv vendor.js ../ +cd ../ + diff --git a/browser/bitcoinjs-lib.js b/browser/vendor.js similarity index 98% rename from browser/bitcoinjs-lib.js rename to browser/vendor.js index 2da8ab0..c6f7c4a 100644 --- a/browser/bitcoinjs-lib.js +++ b/browser/vendor.js @@ -1,4 +1,9 @@ if ('undefined' === typeof window) window = this; +Bitcoin = {}; +if (typeof navigator === 'undefined') { + var navigator = {}; + navigator.appName = 'NodeJS'; +} /*! * Crypto-JS v2.0.0 * http://code.google.com/p/crypto-js/ @@ -189,15 +194,7 @@ e[a>>>5]|=128<<24-a%32;e[(a+64>>>9<<4)+14]=(b<<8|b>>>24)&16711935|(b<<24|b>>>8)& module.exports.RIPEMD160 = CryptoJS.RIPEMD160; -Bitcoin = {}; - - - -if (typeof navigator === 'undefined') { - var navigator = {}; - navigator.appName = 'NodeJS'; - -} +module.exports.WordArray = CryptoJS.lib.WordArray; // Copyright (c) 2005 Tom Wu // All Rights Reserved. // See "LICENSE" for details. @@ -1605,15 +1602,16 @@ Bitcoin.Util = { if (i < 0xfd) { // unsigned char return [i]; - } else if (i <= 1<<16) { + } else if (i < 0x10000) { // unsigned short (LE) - return [0xfd, i >>> 8, i & 255]; - } else if (i <= 1<<32) { + return [0xfd, i & 255 , i >>> 8]; + } else if (i < 0x100000000) { // unsigned int (LE) - return [0xfe].concat(Crypto.util.wordsToBytes([i])); + return [0xfe].concat(Crypto.util.wordsToBytes([i]).reverse()); } else { + throw 'quadword not implemented' // unsigned long long (LE) - return [0xff].concat(Crypto.util.wordsToBytes([i >>> 32, i])); + //return [0xff].concat(Crypto.util.wordsToBytes([i >>> 32, i])); } }, @@ -2286,37 +2284,26 @@ ECPointFp.prototype.getEncoded = function (compressed) { return enc; }; -ECPointFp.decodeFrom = function (ecparams, enc) { +ECPointFp.decodeFrom = function (curve, enc) { var type = enc[0]; var dataLen = enc.length-1; // Extract x and y as byte arrays - if (type == 4) { - var xBa = enc.slice(1, 1 + dataLen/2), - yBa = enc.slice(1 + dataLen/2, 1 + dataLen), - x = BigInteger.fromByteArrayUnsigned(xBa), - y = BigInteger.fromByteArrayUnsigned(yBa); - } - else { - var xBa = enc.slice(1), - x = BigInteger.fromByteArrayUnsigned(xBa), - p = ecparams.getQ(), - 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 BigInteger('2')).toString() != ''+(type % 2)) { - y = p.subtract(y) - } - } + var xBa = enc.slice(1, 1 + dataLen/2); + var yBa = enc.slice(1 + dataLen/2, 1 + dataLen); + + // Prepend zero byte to prevent interpretation as negative integer + xBa.unshift(0); + yBa.unshift(0); + + // Convert to BigIntegers + var x = new BigInteger(xBa); + var y = new BigInteger(yBa); // Return point - return new ECPointFp(ecparams, - ecparams.fromBigInteger(x), - ecparams.fromBigInteger(y)); + return new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y)); }; - ECPointFp.prototype.add2D = function (b) { if(this.isInfinity()) return b; if(b.isInfinity()) return this; diff --git a/browser/navigator-adapter.js b/browser/vendor/browser-adapter.js similarity index 60% rename from browser/navigator-adapter.js rename to browser/vendor/browser-adapter.js index b1eb29d..7da083e 100644 --- a/browser/navigator-adapter.js +++ b/browser/vendor/browser-adapter.js @@ -1,7 +1,6 @@ - - +if ('undefined' === typeof window) window = this; +Bitcoin = {}; if (typeof navigator === 'undefined') { var navigator = {}; navigator.appName = 'NodeJS'; - } diff --git a/browser/crypto.js b/browser/vendor/crypto.js similarity index 100% rename from browser/crypto.js rename to browser/vendor/crypto.js diff --git a/browser/ec.js b/browser/vendor/ec.js similarity index 100% rename from browser/ec.js rename to browser/vendor/ec.js diff --git a/browser/ecdsa.js b/browser/vendor/ecdsa.js similarity index 100% rename from browser/ecdsa.js rename to browser/vendor/ecdsa.js diff --git a/browser/eckey.js b/browser/vendor/eckey.js similarity index 100% rename from browser/eckey.js rename to browser/vendor/eckey.js diff --git a/browser/jsbn.js b/browser/vendor/jsbn.js similarity index 100% rename from browser/jsbn.js rename to browser/vendor/jsbn.js diff --git a/browser/jsbn2.js b/browser/vendor/jsbn2.js similarity index 100% rename from browser/jsbn2.js rename to browser/vendor/jsbn2.js diff --git a/browser/prng4.js b/browser/vendor/prng4.js similarity index 100% rename from browser/prng4.js rename to browser/vendor/prng4.js diff --git a/browser/ripemd160.js b/browser/vendor/ripemd160.js similarity index 100% rename from browser/ripemd160.js rename to browser/vendor/ripemd160.js diff --git a/browser/rng.js b/browser/vendor/rng.js similarity index 100% rename from browser/rng.js rename to browser/vendor/rng.js diff --git a/browser/sec.js b/browser/vendor/sec.js similarity index 100% rename from browser/sec.js rename to browser/vendor/sec.js diff --git a/browser/util.js b/browser/vendor/util.js similarity index 100% rename from browser/util.js rename to browser/vendor/util.js diff --git a/util/util.js b/util/util.js index 8a01292..44b43c8 100644 --- a/util/util.js +++ b/util/util.js @@ -4,10 +4,10 @@ var bignum = require('bignum'); var Binary = require('binary'); var Put = require('bufferput'); var buffertools = require('buffertools'); -var bjs; +var browser; if (!process.versions) { // browser version - bjs = require('../browser/bitcoinjs-lib.js'); + browser = require('../browser/vendor.js'); } @@ -17,8 +17,8 @@ var sha256 = exports.sha256 = function (data) { var ripe160 = exports.ripe160 = function (data) { if (!process.versions) { - var RIPEMD160 = bjs.RIPEMD160; - var WordArray = bjs.WordArray; + var RIPEMD160 = browser.RIPEMD160; + var WordArray = browser.WordArray; data = data.toString(); var result = RIPEMD160(data) + ''; return new Buffer(result, 'hex');