refactoring browser code

This commit is contained in:
Manuel Araoz 2014-03-11 12:32:38 -03:00
parent 272378f43a
commit 76cf42506c
18 changed files with 36 additions and 49 deletions

2
Key.js
View File

@ -5,7 +5,7 @@ if (process.versions) {
module.exports = require('bindings')('KeyModule'); module.exports = require('bindings')('KeyModule');
} else { } else {
// pure js version // pure js version
var ECKey = require('./browser/bitcoinjs-lib.js').ECKey; var ECKey = require('./browser/vendor.js').ECKey;
var buffertools = require('buffertools'); var buffertools = require('buffertools');
var bufferToArray = function(buffer) { var bufferToArray = function(buffer) {

View File

@ -1,2 +0,0 @@
Bitcoin = {};

View File

@ -1 +0,0 @@
if ('undefined' === typeof window) window = this;

View File

@ -1,3 +1,7 @@
#! /bin/bash #! /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 ../

View File

@ -1,4 +1,9 @@
if ('undefined' === typeof window) window = this; if ('undefined' === typeof window) window = this;
Bitcoin = {};
if (typeof navigator === 'undefined') {
var navigator = {};
navigator.appName = 'NodeJS';
}
/*! /*!
* Crypto-JS v2.0.0 * Crypto-JS v2.0.0
* http://code.google.com/p/crypto-js/ * 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; module.exports.RIPEMD160 = CryptoJS.RIPEMD160;
Bitcoin = {}; module.exports.WordArray = CryptoJS.lib.WordArray;
if (typeof navigator === 'undefined') {
var navigator = {};
navigator.appName = 'NodeJS';
}
// Copyright (c) 2005 Tom Wu // Copyright (c) 2005 Tom Wu
// All Rights Reserved. // All Rights Reserved.
// See "LICENSE" for details. // See "LICENSE" for details.
@ -1605,15 +1602,16 @@ Bitcoin.Util = {
if (i < 0xfd) { if (i < 0xfd) {
// unsigned char // unsigned char
return [i]; return [i];
} else if (i <= 1<<16) { } else if (i < 0x10000) {
// unsigned short (LE) // unsigned short (LE)
return [0xfd, i >>> 8, i & 255]; return [0xfd, i & 255 , i >>> 8];
} else if (i <= 1<<32) { } else if (i < 0x100000000) {
// unsigned int (LE) // unsigned int (LE)
return [0xfe].concat(Crypto.util.wordsToBytes([i])); return [0xfe].concat(Crypto.util.wordsToBytes([i]).reverse());
} else { } else {
throw 'quadword not implemented'
// unsigned long long (LE) // 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; return enc;
}; };
ECPointFp.decodeFrom = function (ecparams, enc) { ECPointFp.decodeFrom = function (curve, enc) {
var type = enc[0]; var type = enc[0];
var dataLen = enc.length-1; var dataLen = enc.length-1;
// Extract x and y as byte arrays // Extract x and y as byte arrays
if (type == 4) { var xBa = enc.slice(1, 1 + dataLen/2);
var xBa = enc.slice(1, 1 + dataLen/2), var yBa = enc.slice(1 + dataLen/2, 1 + dataLen);
yBa = enc.slice(1 + dataLen/2, 1 + dataLen),
x = BigInteger.fromByteArrayUnsigned(xBa), // Prepend zero byte to prevent interpretation as negative integer
y = BigInteger.fromByteArrayUnsigned(yBa); xBa.unshift(0);
} yBa.unshift(0);
else {
var xBa = enc.slice(1), // Convert to BigIntegers
x = BigInteger.fromByteArrayUnsigned(xBa), var x = new BigInteger(xBa);
p = ecparams.getQ(), var y = new BigInteger(yBa);
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)
}
}
// Return point // Return point
return new ECPointFp(ecparams, return new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y));
ecparams.fromBigInteger(x),
ecparams.fromBigInteger(y));
}; };
ECPointFp.prototype.add2D = function (b) { ECPointFp.prototype.add2D = function (b) {
if(this.isInfinity()) return b; if(this.isInfinity()) return b;
if(b.isInfinity()) return this; if(b.isInfinity()) return this;

View File

@ -1,7 +1,6 @@
if ('undefined' === typeof window) window = this;
Bitcoin = {};
if (typeof navigator === 'undefined') { if (typeof navigator === 'undefined') {
var navigator = {}; var navigator = {};
navigator.appName = 'NodeJS'; navigator.appName = 'NodeJS';
} }

View File

@ -4,10 +4,10 @@ var bignum = require('bignum');
var Binary = require('binary'); var Binary = require('binary');
var Put = require('bufferput'); var Put = require('bufferput');
var buffertools = require('buffertools'); var buffertools = require('buffertools');
var bjs; var browser;
if (!process.versions) { if (!process.versions) {
// browser version // 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) { var ripe160 = exports.ripe160 = function (data) {
if (!process.versions) { if (!process.versions) {
var RIPEMD160 = bjs.RIPEMD160; var RIPEMD160 = browser.RIPEMD160;
var WordArray = bjs.WordArray; var WordArray = browser.WordArray;
data = data.toString(); data = data.toString();
var result = RIPEMD160(data) + ''; var result = RIPEMD160(data) + '';
return new Buffer(result, 'hex'); return new Buffer(result, 'hex');