Skip to content

Commit

Permalink
replace bignumber.js with bn.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan X. Charles committed Jul 4, 2014
1 parent e485613 commit 2fff555
Show file tree
Hide file tree
Showing 13 changed files with 321 additions and 2,206 deletions.
246 changes: 135 additions & 111 deletions browser/bundle.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions lib/Base58.js
Expand Up @@ -24,9 +24,9 @@ var base58 = {
str = new Buffer(buf.length << 1);
}
var i = str.length - 1;
while (x.gt(0)) {
r = x.mod(58);
x = x.div(58);
while (x.gt(new bignum(0))) {
r = x.mod(new bignum(58));
x = x.div(new bignum(58));
str[i] = ALPHABET_BUF[r.toNumber()];
i--;
}
Expand All @@ -44,10 +44,10 @@ var base58 = {

decode: function(str) {
if (str.length == 0) return zerobuf;
var answer = bignum(0);
var answer = new bignum(0);
for (var i = 0; i < str.length; i++) {
answer = answer.mul(58);
answer = answer.add(ALPHABET_INV[str[i]]);
answer = answer.mul(new bignum(58));
answer = answer.add(new bignum(ALPHABET_INV[str[i]]));
};
var i = 0;
while (i < str.length && str[i] == ALPHABET_ZERO) {
Expand Down
12 changes: 8 additions & 4 deletions lib/Block.js
Expand Up @@ -14,7 +14,9 @@ var COINBASE_OP = Transaction.COINBASE_OP;
var VerificationError = imports.VerificationError || require('../util/error').VerificationError;
var BlockRules = {
maxTimeOffset: 2 * 60 * 60, // How far block timestamps can be into the future
largestHash: Bignum(2).pow(256)
//largestHash: (new Bignum(2)).pow(256)
//largestHash: new Bignum('115792089237316195423570985008687907853269984665640564039457584007913129639936') // = 2^256
largestHash: new Bignum('10000000000000000000000000000000000000000000000000000000000000000', 16)
};

function Block(data) {
Expand Down Expand Up @@ -115,7 +117,7 @@ Block.prototype.checkProofOfWork = function checkProofOfWork() {
*/
Block.prototype.getWork = function getWork() {
var target = util.decodeDiffBits(this.bits, true);
return BlockRules.largestHash.div(target.add(1));
return BlockRules.largestHash.div(target.add(new Bignum(1)));
};

Block.prototype.checkTimestamp = function checkTimestamp() {
Expand Down Expand Up @@ -224,8 +226,10 @@ Block.prototype.checkBlock = function checkBlock(txs) {
};

Block.getBlockValue = function getBlockValue(height) {
var subsidy = Bignum(50).mul(util.COIN);
subsidy = subsidy.div(Bignum(2).pow(Math.floor(height / 210000)));
var subsidy = 50 * util.COIN;
subsidy = subsidy / (Math.pow(2, Math.floor(height / 210000)));
subsidy = Math.floor(subsidy);
subsidy = new Bignum(subsidy);
return subsidy;
};

Expand Down
38 changes: 19 additions & 19 deletions lib/ScriptInterpreter.js
Expand Up @@ -243,7 +243,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,

case Opcode.map.OP_DEPTH:
// -- stacksize
var value = bignum(this.stack.length);
var value = new bignum(this.stack.length);
this.stack.push(intToBufferSM(value));
break;

Expand Down Expand Up @@ -352,7 +352,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,

case Opcode.map.OP_SIZE:
// (in -- in size)
var value = bignum(this.stackTop().length);
var value = new bignum(this.stackTop().length);
this.stack.push(intToBufferSM(value));
break;

Expand Down Expand Up @@ -428,16 +428,16 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
var num = bufferSMToInt(this.stackTop());
switch (opcode) {
case Opcode.map.OP_1ADD:
num = num.add(bignum(1));
num = num.add(new bignum(1));
break;
case Opcode.map.OP_1SUB:
num = num.sub(bignum(1));
num = num.sub(new bignum(1));
break;
case Opcode.map.OP_2MUL:
num = num.mul(bignum(2));
num = num.mul(new bignum(2));
break;
case Opcode.map.OP_2DIV:
num = num.div(bignum(2));
num = num.div(new bignum(2));
break;
case Opcode.map.OP_NEGATE:
num = num.neg();
Expand All @@ -446,10 +446,10 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
num = num.abs();
break;
case Opcode.map.OP_NOT:
num = bignum(num.cmp(0) == 0 ? 1 : 0);
num = new bignum(num.cmp(new bignum(0)) == 0 ? 1 : 0);
break;
case Opcode.map.OP_0NOTEQUAL:
num = bignum(num.cmp(0) == 0 ? 0 : 1);
num = new bignum(num.cmp(new bignum(0)) == 0 ? 0 : 1);
break;
}
this.stack[this.stack.length - 1] = intToBufferSM(num);
Expand Down Expand Up @@ -495,51 +495,51 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
break;

case Opcode.map.OP_LSHIFT:
if (v2.cmp(0) < 0 || v2.cmp(2048) > 0) {
if (v2.cmp(new bignum(0)) < 0 || v2.cmp(new bignum(2048)) > 0) {
throw new Error("OP_LSHIFT parameter out of bounds");
}
num = v1.shiftLeft(v2);
break;

case Opcode.map.OP_RSHIFT:
if (v2.cmp(0) < 0 || v2.cmp(2048) > 0) {
if (v2.cmp(new bignum(0)) < 0 || v2.cmp(new bignum(2048)) > 0) {
throw new Error("OP_RSHIFT parameter out of bounds");
}
num = v1.shiftRight(v2);
break;

case Opcode.map.OP_BOOLAND:
num = bignum((v1.cmp(0) != 0 && v2.cmp(0) != 0) ? 1 : 0);
num = new bignum((v1.cmp(new bignum(0)) != 0 && v2.cmp(new bignum(0)) != 0) ? 1 : 0);
break;

case Opcode.map.OP_BOOLOR:
num = bignum((v1.cmp(0) != 0 || v2.cmp(0) != 0) ? 1 : 0);
num = new bignum((v1.cmp(new bignum(0)) != 0 || v2.cmp(new bignum(0)) != 0) ? 1 : 0);
break;

case Opcode.map.OP_NUMEQUAL:
case Opcode.map.OP_NUMEQUALVERIFY:
num = bignum(v1.cmp(v2) == 0 ? 1 : 0);
num = new bignum(v1.cmp(v2) == 0 ? 1 : 0);
break;

case Opcode.map.OP_NUMNOTEQUAL:
;
num = bignum(v1.cmp(v2) != 0 ? 1 : 0);
num = new bignum(v1.cmp(v2) != 0 ? 1 : 0);
break;

case Opcode.map.OP_LESSTHAN:
num = bignum(v1.lt(v2) ? 1 : 0);
num = new bignum(v1.lt(v2) ? 1 : 0);
break;

case Opcode.map.OP_GREATERTHAN:
num = bignum(v1.gt(v2) ? 1 : 0);
num = new bignum(v1.gt(v2) ? 1 : 0);
break;

case Opcode.map.OP_LESSTHANOREQUAL:
num = bignum(v1.gt(v2) ? 0 : 1);
num = new bignum(v1.gt(v2) ? 0 : 1);
break;

case Opcode.map.OP_GREATERTHANOREQUAL:
num = bignum(v1.lt(v2) ? 0 : 1);
num = new bignum(v1.lt(v2) ? 0 : 1);
break;

case Opcode.map.OP_MIN:
Expand Down Expand Up @@ -836,7 +836,7 @@ ScriptInterpreter.prototype.getPrimitiveStack = function getPrimitiveStack() {
return buffertools.toHex(chunk.slice(0));
}
var num = bufferSMToInt(chunk);
if (num.cmp(-128) >= 0 && num.cmp(127) <= 0) {
if (num.cmp(new bignum(-128)) >= 0 && num.cmp(new bignum(127)) <= 0) {
return num.toNumber();
} else {
return buffertools.toHex(chunk.slice(0));
Expand Down
2 changes: 1 addition & 1 deletion lib/Transaction.js
Expand Up @@ -513,7 +513,7 @@ Transaction.prototype.fromObj = function fromObj(obj) {
var addr = new Address(addrStr);
var script = Script.createPubKeyHashOut(addr.payload());

var valueNum = bignum(obj.outputs[addrStr]);
var valueNum = new bignum(obj.outputs[addrStr]);
var value = util.bigIntToValue(valueNum);

var txout = new TransactionOut();
Expand Down
26 changes: 13 additions & 13 deletions lib/TransactionBuilder.js
Expand Up @@ -232,7 +232,7 @@ TransactionBuilder.prototype._selectUnspent = function(neededAmountSat) {
if (this.spendUnconfirmed) minConfirmationSteps.push(0);

var sel = [],
totalSat = bignum(0),
totalSat = new bignum(0),
fulfill = false,
maxConfirmations = null,
l = this.utxos.length;
Expand All @@ -247,9 +247,9 @@ TransactionBuilder.prototype._selectUnspent = function(neededAmountSat) {
continue;

var sat = u.amountSat || util.parseValue(u.amount);
totalSat = totalSat.add(sat);
totalSat = totalSat.add(new bignum(sat));
sel.push(u);
if (totalSat.cmp(neededAmountSat) >= 0) {
if (totalSat.cmp(new bignum(neededAmountSat)) >= 0) {
fulfill = true;
break;
}
Expand All @@ -269,11 +269,11 @@ TransactionBuilder.prototype._selectUnspent = function(neededAmountSat) {
TransactionBuilder.prototype._setInputs = function(txobj) {
var ins = this.selectedUtxos;
var l = ins.length;
var valueInSat = bignum(0);
var valueInSat = new bignum(0);

txobj.ins = [];
for (var i = 0; i < l; i++) {
valueInSat = valueInSat.add(util.parseValue(ins[i].amount));
valueInSat = valueInSat.add(new bignum(util.parseValue(ins[i].amount)));

var txin = {};
txin.s = util.EMPTY_BUFFER;
Expand All @@ -298,9 +298,9 @@ TransactionBuilder.prototype._setFee = function(feeSat) {
throw new Error('valueOutSat undefined');


var valueOutSat = this.valueOutSat.add(feeSat);
var valueOutSat = this.valueOutSat.add(new bignum(feeSat));

if (this.valueInSat.cmp(valueOutSat) < 0) {
if (this.valueInSat.cmp(new bignum(valueOutSat)) < 0) {
var inv = this.valueInSat.toString();
var ouv = valueOutSat.toString();
throw new Error('transaction input amount is less than outputs: ' +
Expand All @@ -317,16 +317,16 @@ TransactionBuilder.prototype._setRemainder = function(txobj, remainderIndex) {
throw new Error('valueInSat / valueOutSat undefined');

/* add remainder (without modifying outs[]) */
var remainderSat = this.valueInSat.sub(this.valueOutSat).sub(this.feeSat);
var remainderSat = this.valueInSat.sub(new bignum(this.valueOutSat)).sub(new bignum(this.feeSat));
var l = txobj.outs.length;
this.remainderSat = bignum(0);
this.remainderSat = new bignum(0);

/*remove old remainder? */
if (l > remainderIndex) {
txobj.outs.pop();
}

if (remainderSat.cmp(0) > 0) {
if (remainderSat.cmp(new bignum(0)) > 0) {
var remainderOut = this.remainderOut || this.selectedUtxos[0];
var value = util.bigIntToValue(remainderSat);
var script = TransactionBuilder._scriptForOut(remainderOut);
Expand All @@ -353,7 +353,7 @@ TransactionBuilder.prototype._setFeeAndRemainder = function(txobj) {
var feeSat = this.givenFeeSat ?
this.givenFeeSat : maxSizeK * FEE_PER_1000B_SAT;

var neededAmountSat = this.valueOutSat.add(feeSat);
var neededAmountSat = this.valueOutSat.add(new bignum(feeSat));

this._selectUnspent(neededAmountSat)
._setInputs(txobj)
Expand Down Expand Up @@ -382,7 +382,7 @@ TransactionBuilder.prototype._setFeeAndRemainder = function(txobj) {
//

TransactionBuilder.prototype.setOutputs = function(outs) {
var valueOutSat = bignum(0);
var valueOutSat = new bignum(0);

var txobj = {};
txobj.version = 1;
Expand All @@ -402,7 +402,7 @@ TransactionBuilder.prototype.setOutputs = function(outs) {
txobj.outs.push(txout);

var sat = outs[i].amountSat || util.parseValue(outs[i].amount);
valueOutSat = valueOutSat.add(sat);
valueOutSat = valueOutSat.add(new bignum(sat));
}

this.valueOutSat = valueOutSat;
Expand Down

0 comments on commit 2fff555

Please sign in to comment.