|
|
@ -28,6 +28,12 @@ |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Turn transaction data into Transaction objects. |
|
|
|
* |
|
|
|
* Takes an array of plain JavaScript objects containing transaction data and |
|
|
|
* returns an array of Transaction objects. |
|
|
|
*/ |
|
|
|
Transaction.objectify = function (txs) { |
|
|
|
var objs = []; |
|
|
|
for (var i = 0; i < txs.length; i++) { |
|
|
@ -36,6 +42,16 @@ |
|
|
|
return objs; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Create a new txin. |
|
|
|
* |
|
|
|
* Can be called with an existing TransactionIn object to add it to the |
|
|
|
* transaction. Or it can be called with a Transaction object and an integer |
|
|
|
* output index, in which case a new TransactionIn object pointing to the |
|
|
|
* referenced output will be created. |
|
|
|
* |
|
|
|
* Note that this method does not sign the created input. |
|
|
|
*/ |
|
|
|
Transaction.prototype.addInput = function (tx, outIndex) { |
|
|
|
if (arguments[0] instanceof TransactionIn) { |
|
|
|
this.ins.push(arguments[0]); |
|
|
@ -51,6 +67,14 @@ |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Create a new txout. |
|
|
|
* |
|
|
|
* Can be called with an existing TransactionOut object to add it to the |
|
|
|
* transaction. Or it can be called with an Address object and a BigInteger |
|
|
|
* for the amount, in which case a new TransactionOut object with those |
|
|
|
* values will be created. |
|
|
|
*/ |
|
|
|
Transaction.prototype.addOutput = function (address, value) { |
|
|
|
if (arguments[0] instanceof TransactionOut) { |
|
|
|
this.outs.push(arguments[0]); |
|
|
@ -69,6 +93,13 @@ |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Serialize this transaction. |
|
|
|
* |
|
|
|
* Returns the transaction as a byte array in the standard Bitcoin binary |
|
|
|
* format. This method is byte-perfect, i.e. the resulting byte array can |
|
|
|
* be hashed to get the transaction's standard Bitcoin hash. |
|
|
|
*/ |
|
|
|
Transaction.prototype.serialize = function () |
|
|
|
{ |
|
|
|
var buffer = []; |
|
|
@ -103,12 +134,22 @@ |
|
|
|
var SIGHASH_SINGLE = 3; |
|
|
|
var SIGHASH_ANYONECANPAY = 80; |
|
|
|
|
|
|
|
Transaction.prototype.hashTransactionForSignature = function (connectedScript, inIndex, hashType) |
|
|
|
/** |
|
|
|
* Hash transaction for signing a specific input. |
|
|
|
* |
|
|
|
* Bitcoin uses a different hash for each signed transaction input. This |
|
|
|
* method copies the transaction, makes the necessary changes based on the |
|
|
|
* hashType, serializes and finally hashes the result. This hash can then be |
|
|
|
* used to sign the transaction input in question. |
|
|
|
*/ |
|
|
|
Transaction.prototype.hashTransactionForSignature = |
|
|
|
function (connectedScript, inIndex, hashType) |
|
|
|
{ |
|
|
|
var txTmp = this.clone(); |
|
|
|
|
|
|
|
// In case concatenating two scripts ends up with two codeseparators,
|
|
|
|
// or an extra one at the end, this prevents all those possible incompatibilities.
|
|
|
|
// or an extra one at the end, this prevents all those possible
|
|
|
|
// incompatibilities.
|
|
|
|
/*scriptCode = scriptCode.filter(function (val) { |
|
|
|
return val !== OP_CODESEPARATOR; |
|
|
|
});*/ |
|
|
@ -151,12 +192,18 @@ |
|
|
|
return Crypto.SHA256(hash1, {asBytes: true}); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Calculate and return the transaction's hash. |
|
|
|
*/ |
|
|
|
Transaction.prototype.getHash = function () |
|
|
|
{ |
|
|
|
var buffer = this.serialize(); |
|
|
|
return Crypto.SHA256(Crypto.SHA256(buffer, {asBytes: true}), {asBytes: true}); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Create a copy of this transaction object. |
|
|
|
*/ |
|
|
|
Transaction.prototype.clone = function () |
|
|
|
{ |
|
|
|
var newTx = new Transaction(); |
|
|
@ -175,6 +222,28 @@ |
|
|
|
|
|
|
|
/** |
|
|
|
* Analyze how this transaction affects a wallet. |
|
|
|
* |
|
|
|
* Returns an object with properties 'impact', 'type' and 'addr'. |
|
|
|
* |
|
|
|
* 'impact' is an object, see Transaction#calcImpact. |
|
|
|
* |
|
|
|
* 'type' can be one of the following: |
|
|
|
* |
|
|
|
* recv: |
|
|
|
* This is an incoming transaction, the wallet received money. |
|
|
|
* 'addr' contains the first address in the wallet that receives money |
|
|
|
* from this transaction. |
|
|
|
* |
|
|
|
* self: |
|
|
|
* This is an internal transaction, money was sent within the wallet. |
|
|
|
* 'addr' is undefined. |
|
|
|
* |
|
|
|
* sent: |
|
|
|
* This is an outgoing transaction, money was sent out from the wallet. |
|
|
|
* 'addr' contains the first external address, i.e. the recipient. |
|
|
|
* |
|
|
|
* other: |
|
|
|
* This method was unable to detect what the transaction does. Either it |
|
|
|
*/ |
|
|
|
Transaction.prototype.analyze = function (wallet) { |
|
|
|
if (!(wallet instanceof Bitcoin.Wallet)) return null; |
|
|
@ -217,6 +286,9 @@ |
|
|
|
analysis.type = 'self'; |
|
|
|
} else if (allFromMe) { |
|
|
|
analysis.type = 'sent'; |
|
|
|
// TODO: Right now, firstRecvHash is the first output, which - if the
|
|
|
|
// transaction was not generated by this library could be the
|
|
|
|
// change address.
|
|
|
|
analysis.addr = new Bitcoin.Address(firstRecvHash); |
|
|
|
} else { |
|
|
|
analysis.type = "other"; |
|
|
@ -225,6 +297,12 @@ |
|
|
|
return analysis; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Get a human-readable version of the data returned by Transaction#analyze. |
|
|
|
* |
|
|
|
* This is merely a convenience function. Clients should consider implementing |
|
|
|
* this themselves based on their UI, I18N, etc. |
|
|
|
*/ |
|
|
|
Transaction.prototype.getDescription = function (wallet) { |
|
|
|
var analysis = this.analyze(wallet); |
|
|
|
|
|
|
@ -249,7 +327,10 @@ |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
Transaction.prototype.getTotalValue = function () { |
|
|
|
/** |
|
|
|
* Get the total amount of a transaction's outputs. |
|
|
|
*/ |
|
|
|
Transaction.prototype.getTotalOutValue = function () { |
|
|
|
var totalValue = BigInteger.ZERO; |
|
|
|
for (var j = 0; j < this.outs.length; j++) { |
|
|
|
var txout = this.outs[j]; |
|
|
@ -258,6 +339,13 @@ |
|
|
|
return totalValue; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Old name for Transaction#getTotalOutValue. |
|
|
|
* |
|
|
|
* @deprecated |
|
|
|
*/ |
|
|
|
Transaction.prototype.getTotalValue = Transaction.prototype.getTotalOutValue; |
|
|
|
|
|
|
|
/** |
|
|
|
* Calculates the impact a transaction has on this wallet. |
|
|
|
* |
|
|
|