Browse Source

Move BigNumber to internal

...no longer relies on Manuel's repo hostig a version of "bignum" that actually
contained bignumber.js. This moves bignumber.js internally and removes bignum
from the browser build process. Also adds a bitcore.Bignum that links to the
right thing. In node, browser.Bignum is require('bignum'). And in the browser,
bitcore.Bignum is is now Bignumber.js (same as before ... except bignumber.js
is now inside bitcore).
patch-2
Ryan X. Charles 11 years ago
parent
commit
7f348ca0bd
  1. 18
      bitcore.js
  2. 6
      browser/bignum_config.js
  3. 8
      browser/build.js
  4. 5
      lib/BIP32.js
  5. 116
      lib/Base58.js
  6. 5
      lib/Bignum.js
  7. 2
      lib/Block.js
  8. 2
      lib/Curve.js
  9. 2
      lib/Electrum.js
  10. 2
      lib/ScriptInterpreter.js
  11. 2
      lib/Transaction.js
  12. 2
      lib/TransactionBuilder.js
  13. 2121
      lib/browser/Bignum.js
  14. 2
      lib/browser/Key.js
  15. 2
      lib/browser/Point.js
  16. 2
      lib/node/Point.js
  17. 2
      package.json
  18. 49
      test/test.Base58.js
  19. 2
      test/test.Curve.js
  20. 2
      test/test.Key.js
  21. 2
      test/test.Point.js
  22. 8
      test/test.misc.js
  23. 2
      util/EncodedData.js
  24. 2
      util/VersionedData.js
  25. 2
      util/util.js

18
bitcore.js

@ -10,8 +10,20 @@ var requireWhenAccessed = function(name, file) {
Object.defineProperty(module.exports, name, {get: function() {return require(file)}}); Object.defineProperty(module.exports, name, {get: function() {return require(file)}});
}; };
requireWhenAccessed('bignum', 'bignum'); requireWhenAccessed('Bignum', './lib/Bignum');
requireWhenAccessed('base58', 'base58-native'); /*
Object.defineProperty(module.exports, 'bignum', {get: function() {
console.log('bignum (with a lower-case "b") is deprecated. Use bitcore.Bignum (capital "B") instead.');
return require('./lib/Bignum');
}});
*/
requireWhenAccessed('Base58', './lib/Base58');
/*
Object.defineProperty(module.exports, 'base58', {get: function() {
console.log('base58 (with a lower-case "b") is deprecated. Use bitcore.Base58 (capital "B") instead.');
return require('./lib/Base58');
}});
*/
requireWhenAccessed('bufferput', 'bufferput'); requireWhenAccessed('bufferput', 'bufferput');
requireWhenAccessed('buffertools', 'buffertools'); requireWhenAccessed('buffertools', 'buffertools');
requireWhenAccessed('Buffers.monkey', './patches/Buffers.monkey'); requireWhenAccessed('Buffers.monkey', './patches/Buffers.monkey');
@ -56,6 +68,6 @@ module.exports.Buffer = Buffer;
if (typeof process.versions === 'undefined') { if (typeof process.versions === 'undefined') {
// Browser specific // Browser specific
module.exports.bignum.config({EXPONENTIAL_AT: 9999999, DECIMAL_PLACES: 0, ROUNDING_MODE: 1}); module.exports.Bignum.config({EXPONENTIAL_AT: 9999999, DECIMAL_PLACES: 0, ROUNDING_MODE: 1});
} }

6
browser/bignum_config.js

@ -1,6 +0,0 @@
require('bignum').config({
EXPONENTIAL_AT: 9999999,
DECIMAL_PLACES: 0,
ROUNDING_MODE: 1,
});

8
browser/build.js

@ -24,6 +24,8 @@ var pack = function (params) {
var modules = [ var modules = [
'lib/Address', 'lib/Address',
'lib/Base58',
'lib/Bignum',
'lib/BIP32', 'lib/BIP32',
'lib/Block', 'lib/Block',
'lib/Bloom', 'lib/Bloom',
@ -94,18 +96,12 @@ var createBitcore = function(opts) {
}; };
var b = browserify(bopts); var b = browserify(bopts);
b.require(opts.dir + 'browserify-bignum/bignumber.js', {
expose: 'bignum'
});
b.require(opts.dir + 'browserify-buffertools/buffertools.js', { b.require(opts.dir + 'browserify-buffertools/buffertools.js', {
expose: 'buffertools' expose: 'buffertools'
}); });
b.require(opts.dir + 'bufferput', { b.require(opts.dir + 'bufferput', {
expose: 'bufferput' expose: 'bufferput'
}); });
b.require(opts.dir + 'base58-native', {
expose: 'base58-native'
});
b.require(opts.dir + 'buffers', { b.require(opts.dir + 'buffers', {
expose: 'buffers' expose: 'buffers'
}); });

5
lib/BIP32.js

@ -1,11 +1,10 @@
var imports = require('soop').imports(); var imports = require('soop').imports();
var base58 = imports.base58 || require('base58-native').base58; var base58 = imports.base58 || require('./Base58').base58;
var coinUtil = imports.coinUtil || require('../util'); var coinUtil = imports.coinUtil || require('../util');
var Key = imports.Key || require('./Key'); var Key = imports.Key || require('./Key');
var Point = imports.Point || require('./Point'); var Point = imports.Point || require('./Point');
var SecureRandom = imports.SecureRandom || require('./SecureRandom'); var SecureRandom = imports.SecureRandom || require('./SecureRandom');
var bignum = imports.bignum || require('bignum'); var bignum = imports.bignum || require('./Bignum');
var crypto = require('crypto');
var networks = require('../networks'); var networks = require('../networks');
var secp256k1_n = new bignum("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16); var secp256k1_n = new bignum("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16);

116
lib/Base58.js

@ -0,0 +1,116 @@
var crypto = require('crypto');
var bignum = require('./Bignum');
var globalBuffer = new Buffer(1024);
var zerobuf = new Buffer(0);
var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
var ALPHABET_ZERO = ALPHABET[0];
var ALPHABET_BUF = new Buffer(ALPHABET, 'ascii');
var ALPHABET_INV = {};
for(var i=0; i < ALPHABET.length; i++) {
ALPHABET_INV[ALPHABET[i]] = i;
};
// Vanilla Base58 Encoding
var base58 = {
encode: function(buf) {
var str;
var x = bignum.fromBuffer(buf);
var r;
if(buf.length < 512) {
str = globalBuffer;
} else {
str = new Buffer(buf.length << 1);
}
var i = str.length - 1;
while(x.gt(0)) {
r = x.mod(58);
x = x.div(58);
str[i] = ALPHABET_BUF[r.toNumber()];
i--;
}
// deal with leading zeros
var j=0;
while(buf[j] == 0) {
str[i] = ALPHABET_BUF[0];
j++; i--;
}
return str.slice(i+1,str.length).toString('ascii');
},
decode: function(str) {
if(str.length == 0) return zerobuf;
var answer = bignum(0);
for(var i=0; i<str.length; i++) {
answer.mul(58)
answer = answer.mul(58);
answer = answer.add(ALPHABET_INV[str[i]]);
};
var i = 0;
while(i < str.length && str[i] == ALPHABET_ZERO) {
i++;
}
if(i > 0) {
var zb = new Buffer(i);
zb.fill(0);
if(i == str.length) return zb;
answer = answer.toBuffer();
return Buffer.concat([zb, answer], i+answer.length);
} else {
return answer.toBuffer();
}
},
};
// Base58Check Encoding
function sha256(data) {
return new Buffer(crypto.createHash('sha256').update(data).digest('binary'), 'binary');
};
function doubleSHA256(data) {
return sha256(sha256(data));
};
var base58Check = {
encode: function(buf) {
var checkedBuf = new Buffer(buf.length + 4);
var hash = doubleSHA256(buf);
buf.copy(checkedBuf);
hash.copy(checkedBuf, buf.length);
return base58.encode(checkedBuf);
},
decode: function(s) {
var buf = base58.decode(s);
if (buf.length < 4) {
throw new Error("invalid input: too short");
}
var data = buf.slice(0, -4);
var csum = buf.slice(-4);
var hash = doubleSHA256(data);
var hash4 = hash.slice(0, 4);
if (csum.toString() != hash4.toString()) {
throw new Error("checksum mismatch");
}
return data;
},
};
// if you frequently do base58 encodings with data larger
// than 512 bytes, you can use this method to expand the
// size of the reusable buffer
exports.setBuffer = function(buf) {
globalBuffer = buf;
};
exports.base58 = base58;
exports.base58Check = base58Check;
exports.encode = base58.encode;
exports.decode = base58.decode;

5
lib/Bignum.js

@ -0,0 +1,5 @@
if (process.versions) {
module.exports = require('bignum');
return;
}
module.exports = require('./browser/Bignum');

2
lib/Block.js

@ -3,7 +3,7 @@ var imports = require('soop').imports();
var util = imports.util || require('../util'); var util = imports.util || require('../util');
var Debug1 = imports.Debug1 || function() {}; var Debug1 = imports.Debug1 || function() {};
var Script = imports.Script || require('./Script'); var Script = imports.Script || require('./Script');
var Bignum = imports.Bignum || require('bignum'); var Bignum = imports.Bignum || require('./Bignum');
var Binary = imports.Binary || require('binary'); var Binary = imports.Binary || require('binary');
var Step = imports.Step || require('step'); var Step = imports.Step || require('step');
var buffertools = imports.buffertools || require('buffertools'); var buffertools = imports.buffertools || require('buffertools');

2
lib/Curve.js

@ -1,6 +1,6 @@
"use strict"; "use strict";
var imports = require('soop'); var imports = require('soop');
var bignum = imports.bignum || require('bignum'); var bignum = imports.bignum || require('./Bignum');
var Point = imports.Point || require('./Point'); var Point = imports.Point || require('./Point');
var n = bignum.fromBuffer(new Buffer("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 'hex'), {size: 32}); var n = bignum.fromBuffer(new Buffer("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 'hex'), {size: 32});

2
lib/Electrum.js

@ -2,7 +2,7 @@ var Key = require('./Key'),
Point = require('./Point'), Point = require('./Point'),
twoSha256 = require('../util').twoSha256, twoSha256 = require('../util').twoSha256,
buffertools = require('buffertools'), buffertools = require('buffertools'),
bignum = require('bignum'); bignum = require('./Bignum');
/** /**
* Pre-BIP32 Electrum public key derivation (electrum <2.0) * Pre-BIP32 Electrum public key derivation (electrum <2.0)

2
lib/ScriptInterpreter.js

@ -4,7 +4,7 @@ var log = imports.log || require('../util/log');
var util = imports.util || require('../util'); var util = imports.util || require('../util');
var Opcode = imports.Opcode || require('./Opcode'); var Opcode = imports.Opcode || require('./Opcode');
var buffertools = imports.buffertools || require('buffertools'); var buffertools = imports.buffertools || require('buffertools');
var bignum = imports.bignum || require('bignum'); var bignum = imports.bignum || require('./Bignum');
var Util = imports.Util || require('../util'); var Util = imports.Util || require('../util');
var Script = require('./Script'); var Script = require('./Script');
var Key = require('./Key'); var Key = require('./Key');

2
lib/Transaction.js

@ -5,7 +5,7 @@ var Address = imports.Address || require('./Address');
var Script = imports.Script || require('./Script'); var Script = imports.Script || require('./Script');
var ScriptInterpreter = imports.ScriptInterpreter || require('./ScriptInterpreter'); var ScriptInterpreter = imports.ScriptInterpreter || require('./ScriptInterpreter');
var util = imports.util || require('../util'); var util = imports.util || require('../util');
var bignum = imports.bignum || require('bignum'); var bignum = imports.bignum || require('./Bignum');
var Put = imports.Put || require('bufferput'); var Put = imports.Put || require('bufferput');
var Parser = imports.Parser || require('../util/BinaryParser'); var Parser = imports.Parser || require('../util/BinaryParser');
var Step = imports.Step || require('step'); var Step = imports.Step || require('step');

2
lib/TransactionBuilder.js

@ -83,7 +83,7 @@ var imports = require('soop').imports();
var Address = imports.Address || require('./Address'); var Address = imports.Address || require('./Address');
var Script = imports.Script || require('./Script'); var Script = imports.Script || require('./Script');
var util = imports.util || require('../util'); var util = imports.util || require('../util');
var bignum = imports.bignum || require('bignum'); var bignum = imports.bignum || require('./Bignum');
var buffertools = imports.buffertools || require('buffertools'); var buffertools = imports.buffertools || require('buffertools');
var networks = imports.networks || require('../networks'); var networks = imports.networks || require('../networks');
var WalletKey = imports.WalletKey || require('./WalletKey'); var WalletKey = imports.WalletKey || require('./WalletKey');

2121
lib/browser/Bignum.js

File diff suppressed because it is too large

2
lib/browser/Key.js

@ -1,7 +1,7 @@
var ECKey = require('../../browser/vendor-bundle.js').ECKey; var ECKey = require('../../browser/vendor-bundle.js').ECKey;
var SecureRandom = require('../SecureRandom'); var SecureRandom = require('../SecureRandom');
var Curve = require('../Curve'); var Curve = require('../Curve');
var bignum = require('bignum'); var bignum = require('../Bignum');
var Key = function() { var Key = function() {
this._pub = null; this._pub = null;

2
lib/browser/Point.js

@ -2,7 +2,7 @@
var imports = require('soop').imports(); var imports = require('soop').imports();
var Key = imports.Key || require('./Key'); var Key = imports.Key || require('./Key');
var bignum = imports.bignum || require('bignum'); var bignum = imports.bignum || require('../Bignum');
var assert = require('assert'); var assert = require('assert');
var ECPointFp = require('../../browser/vendor-bundle.js').ECPointFp; var ECPointFp = require('../../browser/vendor-bundle.js').ECPointFp;
var ECFieldElementFp = require('../../browser/vendor-bundle.js').ECFieldElementFp; var ECFieldElementFp = require('../../browser/vendor-bundle.js').ECFieldElementFp;

2
lib/node/Point.js

@ -1,7 +1,7 @@
"use strict"; "use strict";
var imports = require('soop').imports(); var imports = require('soop').imports();
var bignum = imports.bignum || require('bignum'); var bignum = imports.bignum || require('../Bignum');
var CPPKey = imports.CPPKey || require('bindings')('KeyModule').Key; var CPPKey = imports.CPPKey || require('bindings')('KeyModule').Key;
var assert = require('assert'); var assert = require('assert');

2
package.json

@ -54,7 +54,6 @@
"dependencies": { "dependencies": {
"jssha": "=1.5.0", "jssha": "=1.5.0",
"soop": "=0.1.5", "soop": "=0.1.5",
"base58-native": "=0.1.3",
"bindings": "=1.1.1", "bindings": "=1.1.1",
"bufferput": "git://github.com/bitpay/node-bufferput.git", "bufferput": "git://github.com/bitpay/node-bufferput.git",
"bignum": "=0.6.2", "bignum": "=0.6.2",
@ -65,7 +64,6 @@
"browserify": "=3.40.0", "browserify": "=3.40.0",
"browser-pack": "=2.0.1", "browser-pack": "=2.0.1",
"commander": "=2.1.0", "commander": "=2.1.0",
"browserify-bignum": "git://github.com/maraoz/browserify-bignum.git",
"browserify-buffertools": "git://github.com/maraoz/browserify-buffertools.git", "browserify-buffertools": "git://github.com/maraoz/browserify-buffertools.git",
"socks5-client": "~0.3.6", "socks5-client": "~0.3.6",
"brfs": "=1.0.0", "brfs": "=1.0.0",

49
test/test.Base58.js

@ -0,0 +1,49 @@
var assert = require('assert');
var base58 = require('../lib/Base58').base58;
var base58Check = require('../lib/Base58').base58Check;
var testData = [
["61", "2g", "C2dGTwc"],
["626262", "a3gV", "4jF5uERJAK"],
["636363", "aPEr", "4mT4krqUYJ"],
["73696d706c792061206c6f6e6720737472696e67", "2cFupjhnEsSn59qHXstmK2ffpLv2", "BXF1HuEUCqeVzZdrKeJjG74rjeXxqJ7dW"],
["00eb15231dfceb60925886b67d065299925915aeb172c06647", "1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L", "13REmUhe2ckUKy1FvM7AMCdtyYq831yxM3QeyEu4"],
["516b6fcd0f", "ABnLTmg", "237LSrY9NUUas"],
["bf4f89001e670274dd", "3SEo3LWLoPntC", "GwDDDeduj1jpykc27e"],
["572e4794", "3EFU7m", "FamExfqCeza"],
["ecac89cad93923c02321", "EJDM8drfXA6uyA", "2W1Yd5Zu6WGyKVtHGMrH"],
["10c8511e", "Rt5zm", "3op3iuGMmhs"],
["00000000000000000000", "1111111111", "111111111146Momb"],
["", "", "3QJmnh"]
];
//suite('basic');
test('allData', function() {
base58.encodeTest = function(raw, b58str) {
assert.equal(base58.encode(raw), b58str);
};
base58.decodeTest = function(raw, b58str) {
assert.equal(raw.toString('hex'), base58.decode(b58str).toString('hex'));
};
base58Check.encodeTest = function(raw, b58str) {
assert.equal(base58Check.encode(raw), b58str);
};
base58Check.decodeTest = function(raw, b58str) {
assert.equal(raw.toString('hex'), base58Check.decode(b58str).toString('hex'));
};
testData.forEach(function(datum) {
var raw = new Buffer(datum[0], 'hex');
var b58 = datum[1];
var b58Check = datum[2];
base58.encodeTest(raw, b58);
base58.decodeTest(raw, b58);
base58Check.encodeTest(raw, b58Check);
base58Check.decodeTest(raw, b58Check);
});
});

2
test/test.Curve.js

@ -4,7 +4,7 @@ var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore'); var bitcore = bitcore || require('../bitcore');
var coinUtil = coinUtil || bitcore.util; var coinUtil = coinUtil || bitcore.util;
var buffertools = require('buffertools'); var buffertools = require('buffertools');
var bignum = require('bignum'); var bignum = bitcore.Bignum;
var should = chai.should(); var should = chai.should();
var assert = chai.assert; var assert = chai.assert;

2
test/test.Key.js

@ -8,7 +8,7 @@ var assert = chai.assert;
var Key = bitcore.Key; var Key = bitcore.Key;
var Point = bitcore.Point; var Point = bitcore.Point;
var bignum = require('bignum'); var bignum = bitcore.Bignum;
describe('Key', function() { describe('Key', function() {
it('should initialize the main object', function() { it('should initialize the main object', function() {

2
test/test.Point.js

@ -4,7 +4,7 @@ var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore'); var bitcore = bitcore || require('../bitcore');
var coinUtil = coinUtil || bitcore.util; var coinUtil = coinUtil || bitcore.util;
var buffertools = require('buffertools'); var buffertools = require('buffertools');
var bignum = require('bignum'); var bignum = bitcore.Bignum;
var should = chai.should(); var should = chai.should();
var assert = chai.assert; var assert = chai.assert;

8
test/test.misc.js

@ -9,8 +9,8 @@ var should = chai.should();
var testdata = testdata || require('./testdata'); var testdata = testdata || require('./testdata');
var bignum = bitcore.bignum; var bignum = bitcore.Bignum;
var base58 = bitcore.base58; var base58 = bitcore.Base58;
var base58Check = base58.base58Check; var base58Check = base58.base58Check;
var Address = bitcore.Address; var Address = bitcore.Address;
@ -81,7 +81,7 @@ describe('Miscelaneous stuff', function() {
}); });
// bignum // bignum
it('should initialze the bignum object', function() { it('should initialze the bignum object', function() {
should.exist(bitcore.bignum); should.exist(bitcore.Bignum);
}); });
it('should create a bignum from string', function() { it('should create a bignum from string', function() {
var n = bignum('9832087987979879879879879879879879879879879879'); var n = bignum('9832087987979879879879879879879879879879879879');
@ -96,7 +96,7 @@ describe('Miscelaneous stuff', function() {
// base58 // base58
it('should initialze the base58 object', function() { it('should initialze the base58 object', function() {
should.exist(bitcore.base58); should.exist(bitcore.Base58);
}); });
it('should obtain the same string in base58 roundtrip', function() { it('should obtain the same string in base58 roundtrip', function() {
var m = 'mqqa8xSMVDyf9QxihGnPtap6Mh6qemUkcu'; var m = 'mqqa8xSMVDyf9QxihGnPtap6Mh6qemUkcu';

2
util/EncodedData.js

@ -1,5 +1,5 @@
var imports = require('soop').imports(); var imports = require('soop').imports();
var base58 = imports.base58 || require('base58-native').base58Check; var base58 = imports.base58 || require('../lib/Base58').base58Check;
// Constructor. Takes the following forms: // Constructor. Takes the following forms:

2
util/VersionedData.js

@ -1,5 +1,5 @@
var imports = require('soop').imports(); var imports = require('soop').imports();
var base58 = imports.base58 || require('base58-native').base58Check; var base58 = imports.base58 || require('../lib/Base58').base58Check;
var parent = imports.parent || require('./EncodedData'); var parent = imports.parent || require('./EncodedData');

2
util/util.js

@ -1,5 +1,5 @@
var crypto = require('crypto'); var crypto = require('crypto');
var bignum = require('bignum'); var bignum = require('../lib/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');

Loading…
Cancel
Save