Skip to content

Commit

Permalink
refactoring browser code
Browse files Browse the repository at this point in the history
  • Loading branch information
maraoz committed Mar 11, 2014
1 parent 272378f commit 76cf425
Show file tree
Hide file tree
Showing 18 changed files with 36 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Key.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 0 additions & 2 deletions browser/bitcoin.js

This file was deleted.

1 change: 0 additions & 1 deletion browser/browser.js

This file was deleted.

6 changes: 5 additions & 1 deletion browser/concat.sh
Original file line number Diff line number Diff line change
@@ -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 ../

61 changes: 24 additions & 37 deletions browser/bitcoinjs-lib.js → browser/vendor.js
Original file line number Diff line number Diff line change
@@ -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/
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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]));
}
},

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@


if ('undefined' === typeof window) window = this;
Bitcoin = {};
if (typeof navigator === 'undefined') {
var navigator = {};
navigator.appName = 'NodeJS';

}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}


Expand All @@ -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');
Expand Down

0 comments on commit 76cf425

Please sign in to comment.