Replaced bigints with plain integers for transaction values

This commit is contained in:
vub 2013-10-14 07:41:21 -04:00
parent c20dee5908
commit 7bfd72c8c4
4 changed files with 51 additions and 84 deletions

2
bitcoinjs-min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -32,7 +32,7 @@
buffer = buffer.concat(numToVarInt(tx.outs.length)); buffer = buffer.concat(numToVarInt(tx.outs.length));
for (var i = 0; i < tx.outs.length; i++) { for (var i = 0; i < tx.outs.length; i++) {
var txout = tx.outs[i]; var txout = tx.outs[i];
var valueHex = (new BigInteger(txout.value, 10)).toString(16); var valueHex = txout.value.toString(16);
while (valueHex.length < 16) valueHex = "0" + valueHex; while (valueHex.length < 16) valueHex = "0" + valueHex;
buffer = buffer.concat(Crypto.util.hexToBytes(valueHex)); buffer = buffer.concat(Crypto.util.hexToBytes(valueHex));
var scriptBytes = Crypto.util.hexToBytes(txout.script); var scriptBytes = Crypto.util.hexToBytes(txout.script);

View file

@ -85,16 +85,8 @@ Transaction.prototype.addInput = function (tx, outIndex) {
Transaction.prototype.addOutput = function (address, value) { Transaction.prototype.addOutput = function (address, value) {
if (arguments[0] instanceof TransactionOut) { if (arguments[0] instanceof TransactionOut) {
this.outs.push(arguments[0]); this.outs.push(arguments[0]);
} else {
if (value instanceof BigInteger) {
value = value.toByteArrayUnsigned().reverse();
while (value.length < 8) value.push(0);
} else if (typeof value == "number") {
value = util.numToBytes(value,8);
} else if (util.isArray(value)) {
// Nothing to do
} }
else {
this.outs.push(new TransactionOut({ this.outs.push(new TransactionOut({
value: value, value: value,
script: Script.createOutputScript(address) script: Script.createOutputScript(address)
@ -145,7 +137,7 @@ Transaction.prototype.serialize = function ()
buffer = buffer.concat(util.numToVarInt(this.outs.length)); buffer = buffer.concat(util.numToVarInt(this.outs.length));
for (var i = 0; i < this.outs.length; i++) { for (var i = 0; i < this.outs.length; i++) {
var txout = this.outs[i]; var txout = this.outs[i];
buffer = buffer.concat(txout.value); buffer = buffer.concat(util.numToBytes(txout.value),8);
var scriptBytes = txout.script.buffer; var scriptBytes = txout.script.buffer;
buffer = buffer.concat(util.numToVarInt(scriptBytes.length)); buffer = buffer.concat(util.numToVarInt(scriptBytes.length));
buffer = buffer.concat(scriptBytes); buffer = buffer.concat(scriptBytes);
@ -304,7 +296,7 @@ Transaction.prototype.analyze = function (wallet) {
analysis.impact = impact; analysis.impact = impact;
if (impact.sign > 0 && impact.value.compareTo(BigInteger.ZERO) > 0) { if (impact.sign > 0 && impact.value > 0) {
analysis.type = 'recv'; analysis.type = 'recv';
analysis.addr = new Address(firstMeRecvHash); analysis.addr = new Address(firstMeRecvHash);
} else if (allFromMe && allToMe) { } else if (allFromMe && allToMe) {
@ -356,12 +348,7 @@ Transaction.prototype.getDescription = function (wallet) {
* Get the total amount of a transaction's outputs. * Get the total amount of a transaction's outputs.
*/ */
Transaction.prototype.getTotalOutValue = function () { Transaction.prototype.getTotalOutValue = function () {
var totalValue = BigInteger.ZERO; return this.outs.reduce(function(t,o) { return t + o.value },0);
for (var j = 0; j < this.outs.length; j++) {
var txout = this.outs[j];
totalValue = totalValue.add(util.valueToBigInt(txout.value));
}
return totalValue;
}; };
/** /**
@ -384,39 +371,31 @@ Transaction.prototype.getTotalOutValue = function () {
* @returns Object Impact on wallet * @returns Object Impact on wallet
*/ */
Transaction.prototype.calcImpact = function (wallet) { Transaction.prototype.calcImpact = function (wallet) {
if (!(wallet instanceof Wallet)) return BigInteger.ZERO; if (!(wallet instanceof Wallet)) return 0;
// Calculate credit to us from all outputs // Calculate credit to us from all outputs
var valueOut = BigInteger.ZERO; var valueOut = this.outs.filter(function(o) {
for (var j = 0; j < this.outs.length; j++) { return wallet.hasHash(conv.bytesToHex(o.script.simpleOutPubKeyHash()));
var txout = this.outs[j]; })
var hash = conv.bytesToHex(txout.script.simpleOutPubKeyHash()); .reduce(function(t,o) { return t+o.value },0);
if (wallet.hasHash(hash)) {
valueOut = valueOut.add(util.valueToBigInt(txout.value));
}
}
// Calculate debit to us from all ins var valueIn = this.ins.filter(function(i) {
var valueIn = BigInteger.ZERO; return wallet.hasHash(conv.bytesToHex(i.script.simpleInPubKeyHash()))
for (var j = 0; j < this.ins.length; j++) { && wallet.txIndex[i.outpoint.hash];
var txin = this.ins[j]; })
var hash = conv.bytesToHex(txin.script.simpleInPubKeyHash()); .reduce(function(t,i) {
if (wallet.hasHash(hash)) { return t + wallet.txIndex[i.outpoint.hash].outs[i.outpoint.index].value
var fromTx = wallet.txIndex[txin.outpoint.hash]; },0);
if (fromTx) {
valueIn = valueIn.add(conv.valueToBigInt(fromTx.outs[txin.outpoint.index].value)); if (valueOut > valueIn) {
}
}
}
if (valueOut.compareTo(valueIn) >= 0) {
return { return {
sign: 1, sign: 1,
value: valueOut.subtract(valueIn) value: valueOut - valueIn
}; };
} else { } else {
return { return {
sign: -1, sign: -1,
value: valueIn.subtract(valueOut) value: valueIn - valueOut
}; };
} }
}; };
@ -466,7 +445,7 @@ Transaction.deserialize = function(buffer) {
var outs = readVarInt(); var outs = readVarInt();
for (var i = 0; i < outs; i++) { for (var i = 0; i < outs; i++) {
obj.outs.push({ obj.outs.push({
value: readBytes(8), value: util.bytesToNum(readBytes(8)),
script: new Script(readVarString()) script: new Script(readVarString())
}); });
} }
@ -541,33 +520,26 @@ TransactionIn.prototype.clone = function ()
return newTxin; return newTxin;
}; };
var TransactionOut = function (data) var TransactionOut = function (data) {
{ this.script =
if (data.script instanceof Script) { data.script instanceof Script ? data.script
this.script = data.script; : util.isArray(data.script) ? new Script(data.script)
} else { : typeof data.script == "string" ? new Script(conv.hexToBytes(data.script))
if (data.scriptPubKey) { : data.scriptPubKey ? Script.fromScriptSig(data.scriptPubKey)
this.script = Script.fromScriptSig(data.scriptPubKey); : new Script();
}
else {
this.script = new Script(data.script);
}
}
if (util.isArray(data.value)) { this.value =
this.value = data.value; util.isArray(data.value) ? util.bytesToNum(data.value)
} else if ("string" == typeof data.value || "number" == typeof data.value) { : "string" == typeof data.value ? parseInt(data.value)
var valueHex = (new BigInteger(""+data.value, 10)).toString(16); : data.value instanceof BigInteger ? parseInt(data.value.toString())
while (valueHex.length < 16) valueHex = "0" + valueHex; : data.value;
this.value = conv.hexToBytes(valueHex);
}
}; };
TransactionOut.prototype.clone = function () TransactionOut.prototype.clone = function ()
{ {
var newTxout = new TransactionOut({ var newTxout = new TransactionOut({
script: this.script.clone(), script: this.script.clone(),
value: this.value.slice(0) value: this.value
}); });
return newTxout; return newTxout;
}; };

View file

@ -264,32 +264,27 @@ Wallet.prototype.process = function (tx) {
}; };
Wallet.prototype.getBalance = function () { Wallet.prototype.getBalance = function () {
var balance = BigInteger.valueOf(0); return this.unspentOuts.reduce(function(t,o) { return t + o.out.value },0);
for (var i = 0; i < this.unspentOuts.length; i++) {
var txout = this.unspentOuts[i].out;
balance = balance.add(util.valueToBigInt(txout.value));
}
return balance;
}; };
Wallet.prototype.createSend = function (address, sendValue, feeValue) { Wallet.prototype.createSend = function (address, sendValue, feeValue) {
var selectedOuts = []; var selectedOuts = [];
var txValue = sendValue.add(feeValue); var txValue = sendValue + feeValue;
var availableValue = BigInteger.ZERO; var availableValue = 0;
var i; var i;
for (i = 0; i < this.unspentOuts.length; i++) { for (i = 0; i < this.unspentOuts.length; i++) {
var txout = this.unspentOuts[i]; var txout = this.unspentOuts[i];
selectedOuts.push(txout); selectedOuts.push(txout);
availableValue = availableValue.add(util.valueToBigInt(txout.out.value)); availableValue += txout.out.value;
if (availableValue.compareTo(txValue) >= 0) break; if (availableValue >= txValue) break;
} }
if (availableValue.compareTo(txValue) < 0) { if (availableValue < txValue) {
throw new Error('Insufficient funds.'); throw new Error('Insufficient funds.');
} }
var changeValue = availableValue.subtract(txValue); var changeValue = availableValue - txValue;
var sendTx = new Transaction(); var sendTx = new Transaction();
@ -298,7 +293,7 @@ Wallet.prototype.createSend = function (address, sendValue, feeValue) {
} }
sendTx.addOutput(address, sendValue); sendTx.addOutput(address, sendValue);
if (changeValue.compareTo(BigInteger.ZERO) > 0) { if (changeValue > 0) {
sendTx.addOutput(this.getNextAddress(), changeValue); sendTx.addOutput(this.getNextAddress(), changeValue);
} }