bitcoinjs-lib/src/transaction.js

331 lines
7.9 KiB
JavaScript
Raw Normal View History

2014-05-13 08:38:13 +02:00
var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
2015-08-11 09:01:47 +02:00
var typeforce = require('typeforce')
var types = require('./types')
var opcodes = require('./opcodes')
var Script = require('./script')
2014-06-04 06:07:29 +02:00
2015-02-23 00:36:57 +01:00
function Transaction () {
2014-03-31 05:47:47 +02:00
this.version = 1
this.locktime = 0
this.ins = []
this.outs = []
}
Transaction.DEFAULT_SEQUENCE = 0xffffffff
Transaction.SIGHASH_ALL = 0x01
Transaction.SIGHASH_NONE = 0x02
Transaction.SIGHASH_SINGLE = 0x03
Transaction.SIGHASH_ANYONECANPAY = 0x80
2015-08-11 09:09:34 +02:00
Transaction.fromBuffer = function (buffer, __disableExcess) {
var offset = 0
2015-02-23 00:36:57 +01:00
function readSlice (n) {
offset += n
return buffer.slice(offset - n, offset)
}
2015-02-23 00:36:57 +01:00
function readUInt32 () {
var i = buffer.readUInt32LE(offset)
offset += 4
return i
}
2015-02-23 00:36:57 +01:00
function readUInt64 () {
var i = bufferutils.readUInt64LE(buffer, offset)
offset += 8
return i
}
2015-02-23 00:36:57 +01:00
function readVarInt () {
var vi = bufferutils.readVarInt(buffer, offset)
offset += vi.size
return vi.number
}
2015-02-23 00:36:57 +01:00
function readScript () {
return Script.fromBuffer(readSlice(readVarInt()))
}
2015-02-23 00:36:57 +01:00
function readGenerationScript () {
return new Script(readSlice(readVarInt()), [])
}
var tx = new Transaction()
tx.version = readUInt32()
var vinLen = readVarInt()
for (var i = 0; i < vinLen; ++i) {
var hash = readSlice(32)
if (Transaction.isCoinbaseHash(hash)) {
tx.ins.push({
hash: hash,
index: readUInt32(),
script: readGenerationScript(),
sequence: readUInt32()
})
} else {
tx.ins.push({
hash: hash,
index: readUInt32(),
script: readScript(),
sequence: readUInt32()
})
}
}
var voutLen = readVarInt()
for (i = 0; i < voutLen; ++i) {
tx.outs.push({
value: readUInt64(),
script: readScript()
})
}
tx.locktime = readUInt32()
2015-08-11 09:09:34 +02:00
if (!__disableExcess) {
if (offset !== buffer.length) throw new Error('Transaction has unexpected data')
}
return tx
}
2015-02-23 00:36:57 +01:00
Transaction.fromHex = function (hex) {
return Transaction.fromBuffer(new Buffer(hex, 'hex'))
}
2015-02-23 00:36:57 +01:00
Transaction.isCoinbaseHash = function (buffer) {
return Array.prototype.every.call(buffer, function (x) {
return x === 0
})
}
2015-02-23 00:36:57 +01:00
Transaction.prototype.addInput = function (hash, index, sequence, script) {
2015-08-11 09:01:47 +02:00
typeforce(types.tuple(
types.Hash256bit,
types.UInt32,
types.maybe(types.UInt32),
types.maybe(types.Script)
), arguments)
if (types.Null(sequence)) {
sequence = Transaction.DEFAULT_SEQUENCE
}
2014-12-23 05:08:20 +01:00
script = script || Script.EMPTY
// Add the input and return the input's index
2014-06-15 07:28:20 +02:00
return (this.ins.push({
hash: hash,
index: index,
script: script,
sequence: sequence
}) - 1)
2014-03-31 05:47:47 +02:00
}
2015-02-23 00:36:57 +01:00
Transaction.prototype.addOutput = function (scriptPubKey, value) {
2015-08-11 09:01:47 +02:00
typeforce(types.tuple(types.Script, types.UInt53), arguments)
// Add the output and return the output's index
2014-06-15 07:28:20 +02:00
return (this.outs.push({
script: scriptPubKey,
value: value
}) - 1)
2014-03-31 05:47:47 +02:00
}
Transaction.prototype.byteLength = function () {
function scriptSize (script) {
var length = script.buffer.length
return bufferutils.varIntSize(length) + length
}
return (
8 +
bufferutils.varIntSize(this.ins.length) +
bufferutils.varIntSize(this.outs.length) +
this.ins.reduce(function (sum, input) { return sum + 40 + scriptSize(input.script) }, 0) +
this.outs.reduce(function (sum, output) { return sum + 8 + scriptSize(output.script) }, 0)
)
}
Transaction.prototype.clone = function () {
var newTx = new Transaction()
newTx.version = this.version
newTx.locktime = this.locktime
2013-03-02 18:00:14 +01:00
2015-02-23 00:36:57 +01:00
newTx.ins = this.ins.map(function (txIn) {
return {
2014-12-02 04:20:04 +01:00
hash: txIn.hash,
index: txIn.index,
script: txIn.script,
sequence: txIn.sequence
}
})
2015-02-23 00:36:57 +01:00
newTx.outs = this.outs.map(function (txOut) {
return {
2014-12-02 04:20:04 +01:00
script: txOut.script,
value: txOut.value
}
2014-03-31 05:47:47 +02:00
})
2014-03-23 20:01:33 +01:00
return newTx
}
var ONE = new Buffer('0000000000000000000000000000000000000000000000000000000000000001', 'hex')
/**
* 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, and then hashes the result.
* This hash can then be used to sign the provided transaction input.
*/
2015-02-23 00:36:57 +01:00
Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashType) {
2015-08-11 09:01:47 +02:00
typeforce(types.tuple(types.UInt32, types.Script, /* types.UInt8 */ types.Number), arguments)
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L29
if (inIndex >= this.ins.length) return ONE
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.
var hashScript = prevOutScript.without(opcodes.OP_CODESEPARATOR)
var i
// blank out other inputs' signatures
txTmp.ins.forEach(function (input) { input.script = Script.EMPTY })
txTmp.ins[inIndex].script = hashScript
// blank out some of the inputs
if ((hashType & 0x1f) === Transaction.SIGHASH_NONE) {
// wildcard payee
txTmp.outs = []
// let the others update at will
txTmp.ins.forEach(function (input, i) {
if (i !== inIndex) {
input.sequence = 0
}
})
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE) {
var nOut = inIndex
2015-04-28 03:08:23 +02:00
// only lock-in the txOut payee at same index as txIn
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L60
if (nOut >= this.outs.length) return ONE
txTmp.outs = txTmp.outs.slice(0, nOut + 1)
2015-02-23 00:36:57 +01:00
// blank all other outputs (clear scriptPubKey, value === -1)
var stubOut = {
script: Script.EMPTY,
valueBuffer: new Buffer('ffffffffffffffff', 'hex')
}
for (i = 0; i < nOut; i++) {
txTmp.outs[i] = stubOut
}
// let the others update at will
txTmp.ins.forEach(function (input, i) {
if (i !== inIndex) {
input.sequence = 0
}
})
2014-03-21 03:15:15 +01:00
}
// blank out other inputs completely, not recommended for open transactions
if (hashType & Transaction.SIGHASH_ANYONECANPAY) {
txTmp.ins[0] = txTmp.ins[inIndex]
txTmp.ins = txTmp.ins.slice(0, 1)
2014-03-21 03:15:15 +01:00
}
// serialize and hash
var buffer = new Buffer(txTmp.byteLength() + 4)
buffer.writeInt32LE(hashType, buffer.length - 4)
txTmp.toBuffer().copy(buffer, 0)
2014-03-23 20:01:33 +01:00
return crypto.hash256(buffer)
2014-03-31 05:47:47 +02:00
}
Transaction.prototype.getHash = function () {
return crypto.hash256(this.toBuffer())
}
Transaction.prototype.getId = function () {
2014-08-17 09:24:48 +02:00
// TxHash is little-endian, we need big-endian
return bufferutils.reverse(this.getHash()).toString('hex')
2014-03-31 05:47:47 +02:00
}
Transaction.prototype.toBuffer = function () {
var buffer = new Buffer(this.byteLength())
var offset = 0
2015-02-23 00:36:57 +01:00
function writeSlice (slice) {
slice.copy(buffer, offset)
offset += slice.length
2014-03-31 05:47:47 +02:00
}
2015-02-23 00:36:57 +01:00
function writeUInt32 (i) {
buffer.writeUInt32LE(i, offset)
offset += 4
2014-03-31 05:47:47 +02:00
}
2015-02-23 00:36:57 +01:00
function writeUInt64 (i) {
bufferutils.writeUInt64LE(buffer, i, offset)
offset += 8
2014-03-31 05:47:47 +02:00
}
2015-02-23 00:36:57 +01:00
function writeVarInt (i) {
var n = bufferutils.writeVarInt(buffer, i, offset)
offset += n
}
writeUInt32(this.version)
writeVarInt(this.ins.length)
2015-02-23 00:36:57 +01:00
this.ins.forEach(function (txIn) {
2014-12-02 04:20:04 +01:00
writeSlice(txIn.hash)
writeUInt32(txIn.index)
writeVarInt(txIn.script.buffer.length)
writeSlice(txIn.script.buffer)
writeUInt32(txIn.sequence)
})
2014-03-23 20:02:31 +01:00
writeVarInt(this.outs.length)
2015-02-23 00:36:57 +01:00
this.outs.forEach(function (txOut) {
if (!txOut.valueBuffer) {
writeUInt64(txOut.value)
} else {
writeSlice(txOut.valueBuffer)
}
2014-12-02 04:20:04 +01:00
writeVarInt(txOut.script.buffer.length)
writeSlice(txOut.script.buffer)
})
2014-03-23 20:02:31 +01:00
writeUInt32(this.locktime)
return buffer
}
2015-02-23 00:36:57 +01:00
Transaction.prototype.toHex = function () {
return this.toBuffer().toString('hex')
}
2015-02-23 00:36:57 +01:00
Transaction.prototype.setInputScript = function (index, script) {
2015-08-11 09:01:47 +02:00
typeforce(types.tuple(types.Number, types.Script), arguments)
this.ins[index].script = script
}
2014-05-16 09:12:39 +02:00
module.exports = Transaction