2015-08-20 05:37:19 +02:00
|
|
|
var bcrypto = require('./crypto')
|
|
|
|
var bscript = require('./script')
|
2015-09-25 09:38:48 +02:00
|
|
|
var bufferutils = require('./bufferutils')
|
2016-03-09 11:58:40 +01:00
|
|
|
var bufferReverse = require('buffer-reverse')
|
2016-06-09 00:00:37 +02:00
|
|
|
var opcodes = require('./opcodes.json')
|
2015-08-11 09:01:47 +02:00
|
|
|
var typeforce = require('typeforce')
|
|
|
|
var types = require('./types')
|
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 = []
|
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-09-15 06:21:01 +02:00
|
|
|
Transaction.DEFAULT_SEQUENCE = 0xffffffff
|
|
|
|
Transaction.SIGHASH_ALL = 0x01
|
|
|
|
Transaction.SIGHASH_NONE = 0x02
|
|
|
|
Transaction.SIGHASH_SINGLE = 0x03
|
|
|
|
Transaction.SIGHASH_ANYONECANPAY = 0x80
|
|
|
|
|
2015-08-14 00:27:27 +02:00
|
|
|
Transaction.fromBuffer = function (buffer, __noStrict) {
|
2014-10-24 04:58:32 +02:00
|
|
|
var offset = 0
|
2015-02-23 00:36:57 +01:00
|
|
|
function readSlice (n) {
|
2014-10-24 04:58:32 +02:00
|
|
|
offset += n
|
|
|
|
return buffer.slice(offset - n, offset)
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
function readUInt32 () {
|
2014-10-24 04:58:32 +02:00
|
|
|
var i = buffer.readUInt32LE(offset)
|
|
|
|
offset += 4
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2016-10-12 16:04:03 +02:00
|
|
|
function readInt32 () {
|
|
|
|
var i = buffer.readInt32LE(offset)
|
|
|
|
offset += 4
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
function readUInt64 () {
|
2014-10-24 04:58:32 +02:00
|
|
|
var i = bufferutils.readUInt64LE(buffer, offset)
|
|
|
|
offset += 8
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
function readVarInt () {
|
2014-10-24 04:58:32 +02:00
|
|
|
var vi = bufferutils.readVarInt(buffer, offset)
|
|
|
|
offset += vi.size
|
|
|
|
return vi.number
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
function readScript () {
|
2015-08-07 08:30:24 +02:00
|
|
|
return readSlice(readVarInt())
|
2015-02-19 02:30:23 +01:00
|
|
|
}
|
|
|
|
|
2014-10-24 04:58:32 +02:00
|
|
|
var tx = new Transaction()
|
2016-10-12 16:04:03 +02:00
|
|
|
tx.version = readInt32()
|
2014-10-24 04:58:32 +02:00
|
|
|
|
|
|
|
var vinLen = readVarInt()
|
|
|
|
for (var i = 0; i < vinLen; ++i) {
|
2015-09-05 06:28:28 +02:00
|
|
|
tx.ins.push({
|
|
|
|
hash: readSlice(32),
|
|
|
|
index: readUInt32(),
|
|
|
|
script: readScript(),
|
|
|
|
sequence: readUInt32()
|
|
|
|
})
|
2014-10-24 04:58:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var voutLen = readVarInt()
|
|
|
|
for (i = 0; i < voutLen; ++i) {
|
|
|
|
tx.outs.push({
|
|
|
|
value: readUInt64(),
|
2015-02-19 02:11:37 +01:00
|
|
|
script: readScript()
|
2014-10-24 04:58:32 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.locktime = readUInt32()
|
2015-02-19 02:03:41 +01:00
|
|
|
|
2015-08-14 00:27:27 +02:00
|
|
|
if (__noStrict) return tx
|
|
|
|
if (offset !== buffer.length) throw new Error('Transaction has unexpected data')
|
2014-10-24 04:58:32 +02:00
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
Transaction.fromHex = function (hex) {
|
2014-10-24 04:58:32 +02:00
|
|
|
return Transaction.fromBuffer(new Buffer(hex, 'hex'))
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
Transaction.isCoinbaseHash = function (buffer) {
|
2016-05-03 14:45:43 +02:00
|
|
|
typeforce(types.Hash256bit, buffer)
|
|
|
|
for (var i = 0; i < 32; ++i) {
|
|
|
|
if (buffer[i] !== 0) return false
|
|
|
|
}
|
|
|
|
return true
|
2015-02-19 02:04:37 +01:00
|
|
|
}
|
|
|
|
|
2016-04-07 15:52:39 +02:00
|
|
|
Transaction.prototype.isCoinbase = function () {
|
|
|
|
return this.ins.length === 1 && Transaction.isCoinbaseHash(this.ins[0].hash)
|
|
|
|
}
|
|
|
|
|
2015-08-18 01:04:07 +02:00
|
|
|
var EMPTY_SCRIPT = new Buffer(0)
|
2015-08-07 08:30:24 +02:00
|
|
|
|
2015-08-20 05:31:29 +02:00
|
|
|
Transaction.prototype.addInput = function (hash, index, sequence, scriptSig) {
|
2015-08-11 09:01:47 +02:00
|
|
|
typeforce(types.tuple(
|
|
|
|
types.Hash256bit,
|
|
|
|
types.UInt32,
|
|
|
|
types.maybe(types.UInt32),
|
2015-08-07 08:30:24 +02:00
|
|
|
types.maybe(types.Buffer)
|
2015-08-11 09:01:47 +02:00
|
|
|
), arguments)
|
|
|
|
|
|
|
|
if (types.Null(sequence)) {
|
2014-12-12 21:28:26 +01:00
|
|
|
sequence = Transaction.DEFAULT_SEQUENCE
|
|
|
|
}
|
2014-12-23 05:08:20 +01:00
|
|
|
|
2014-08-20 02:37:24 +02:00
|
|
|
// Add the input and return the input's index
|
2014-06-15 07:28:20 +02:00
|
|
|
return (this.ins.push({
|
2015-03-02 03:31:03 +01:00
|
|
|
hash: hash,
|
|
|
|
index: index,
|
2015-08-20 05:31:29 +02:00
|
|
|
script: scriptSig || EMPTY_SCRIPT,
|
2015-03-02 03:31:03 +01:00
|
|
|
sequence: sequence
|
|
|
|
}) - 1)
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
Transaction.prototype.addOutput = function (scriptPubKey, value) {
|
2016-10-07 03:31:02 +02:00
|
|
|
typeforce(types.tuple(types.Buffer, types.Satoshi), arguments)
|
2014-07-11 11:18:27 +02:00
|
|
|
|
2014-08-20 02:37:24 +02:00
|
|
|
// Add the output and return the output's index
|
2014-06-15 07:28:20 +02:00
|
|
|
return (this.outs.push({
|
2015-03-02 03:31:03 +01:00
|
|
|
script: scriptPubKey,
|
|
|
|
value: value
|
|
|
|
}) - 1)
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2015-03-16 00:50:47 +01:00
|
|
|
Transaction.prototype.byteLength = function () {
|
2015-08-20 05:31:29 +02:00
|
|
|
function scriptSize (someScript) {
|
|
|
|
var length = someScript.length
|
2015-03-16 00:50:47 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2014-10-24 04:58:32 +02:00
|
|
|
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) {
|
2014-10-24 04:58:32 +02:00
|
|
|
return {
|
2014-12-02 04:20:04 +01:00
|
|
|
hash: txIn.hash,
|
|
|
|
index: txIn.index,
|
|
|
|
script: txIn.script,
|
|
|
|
sequence: txIn.sequence
|
2014-10-24 04:58:32 +02:00
|
|
|
}
|
2014-05-01 22:36:21 +02:00
|
|
|
})
|
2014-03-23 20:03:58 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
newTx.outs = this.outs.map(function (txOut) {
|
2014-10-24 04:58:32 +02:00
|
|
|
return {
|
2014-12-02 04:20:04 +01:00
|
|
|
script: txOut.script,
|
|
|
|
value: txOut.value
|
2014-10-24 04:58:32 +02:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-03-23 20:01:33 +01:00
|
|
|
|
2014-10-24 04:58:32 +02:00
|
|
|
return newTx
|
2013-11-20 19:00:49 +01:00
|
|
|
}
|
|
|
|
|
2015-08-10 11:12:42 +02:00
|
|
|
var ONE = new Buffer('0000000000000000000000000000000000000000000000000000000000000001', 'hex')
|
2015-08-18 01:04:07 +02:00
|
|
|
var VALUE_UINT64_MAX = new Buffer('ffffffffffffffff', 'hex')
|
2016-06-22 05:54:55 +02:00
|
|
|
var BLANK_OUTPUT = {
|
|
|
|
script: EMPTY_SCRIPT,
|
|
|
|
valueBuffer: VALUE_UINT64_MAX
|
|
|
|
}
|
2015-04-28 02:35:32 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
/**
|
|
|
|
* Hash transaction for signing a specific input.
|
|
|
|
*
|
2015-03-02 07:18:56 +01:00
|
|
|
* 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.
|
2013-02-17 06:39:15 +01:00
|
|
|
*/
|
2015-02-23 00:36:57 +01:00
|
|
|
Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashType) {
|
2015-08-07 08:30:24 +02:00
|
|
|
typeforce(types.tuple(types.UInt32, types.Buffer, /* types.UInt8 */ types.Number), arguments)
|
2015-04-28 02:35:32 +02:00
|
|
|
|
|
|
|
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L29
|
2015-08-10 11:12:42 +02:00
|
|
|
if (inIndex >= this.ins.length) return ONE
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2016-06-22 05:54:55 +02:00
|
|
|
// ignore OP_CODESEPARATOR
|
|
|
|
var ourScript = bscript.compile(bscript.decompile(prevOutScript).filter(function (x) {
|
2015-08-18 02:17:04 +02:00
|
|
|
return x !== opcodes.OP_CODESEPARATOR
|
|
|
|
}))
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2016-06-22 05:54:55 +02:00
|
|
|
var txTmp = this.clone()
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2016-06-22 05:54:55 +02:00
|
|
|
// SIGHASH_NONE: ignore all outputs? (wildcard payee)
|
2015-04-28 02:35:32 +02:00
|
|
|
if ((hashType & 0x1f) === Transaction.SIGHASH_NONE) {
|
|
|
|
txTmp.outs = []
|
|
|
|
|
2016-06-22 05:54:55 +02:00
|
|
|
// ignore sequence numbers (except at inIndex)
|
2015-04-28 02:35:32 +02:00
|
|
|
txTmp.ins.forEach(function (input, i) {
|
2016-06-22 05:54:55 +02:00
|
|
|
if (i === inIndex) return
|
|
|
|
|
|
|
|
input.sequence = 0
|
2015-04-28 02:35:32 +02:00
|
|
|
})
|
|
|
|
|
2016-06-22 05:54:55 +02:00
|
|
|
// SIGHASH_SINGLE: ignore all outputs, except at the same index?
|
|
|
|
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE) {
|
2015-04-28 02:35:32 +02:00
|
|
|
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L60
|
2016-06-22 05:54:55 +02:00
|
|
|
if (inIndex >= this.outs.length) return ONE
|
2015-04-28 02:35:32 +02:00
|
|
|
|
2016-06-22 05:54:55 +02:00
|
|
|
// truncate outputs after
|
|
|
|
txTmp.outs.length = inIndex + 1
|
2015-02-23 00:36:57 +01:00
|
|
|
|
2016-06-22 05:54:55 +02:00
|
|
|
// "blank" outputs before
|
|
|
|
for (var i = 0; i < inIndex; i++) {
|
|
|
|
txTmp.outs[i] = BLANK_OUTPUT
|
2015-04-28 02:35:32 +02:00
|
|
|
}
|
|
|
|
|
2016-06-22 05:54:55 +02:00
|
|
|
// ignore sequence numbers (except at inIndex)
|
2015-04-28 02:35:32 +02:00
|
|
|
txTmp.ins.forEach(function (input, i) {
|
2016-06-22 05:54:55 +02:00
|
|
|
if (i === inIndex) return
|
|
|
|
|
|
|
|
input.sequence = 0
|
2015-04-28 02:35:32 +02:00
|
|
|
})
|
2014-03-21 03:15:15 +01:00
|
|
|
}
|
|
|
|
|
2016-06-22 05:54:55 +02:00
|
|
|
// SIGHASH_ANYONECANPAY: ignore inputs entirely?
|
2014-06-16 09:31:01 +02:00
|
|
|
if (hashType & Transaction.SIGHASH_ANYONECANPAY) {
|
2016-06-22 05:54:55 +02:00
|
|
|
txTmp.ins = [txTmp.ins[inIndex]]
|
|
|
|
txTmp.ins[0].script = ourScript
|
|
|
|
|
|
|
|
// SIGHASH_ALL: only ignore input scripts
|
|
|
|
} else {
|
|
|
|
// "blank" others input scripts
|
|
|
|
txTmp.ins.forEach(function (input) { input.script = EMPTY_SCRIPT })
|
|
|
|
txTmp.ins[inIndex].script = ourScript
|
2014-03-21 03:15:15 +01:00
|
|
|
}
|
|
|
|
|
2015-04-28 02:35:32 +02:00
|
|
|
// serialize and hash
|
|
|
|
var buffer = new Buffer(txTmp.byteLength() + 4)
|
|
|
|
buffer.writeInt32LE(hashType, buffer.length - 4)
|
2016-07-12 04:31:54 +02:00
|
|
|
txTmp.toBuffer(buffer, 0)
|
2014-03-23 20:01:33 +01:00
|
|
|
|
2015-08-20 05:37:19 +02:00
|
|
|
return bcrypto.hash256(buffer)
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-06-16 10:52:06 +02:00
|
|
|
Transaction.prototype.getHash = function () {
|
2015-08-20 05:37:19 +02:00
|
|
|
return bcrypto.hash256(this.toBuffer())
|
2014-06-16 10:52:06 +02:00
|
|
|
}
|
|
|
|
|
2014-05-20 06:07:22 +02:00
|
|
|
Transaction.prototype.getId = function () {
|
2015-09-14 07:03:35 +02:00
|
|
|
// transaction hash's are displayed in reverse order
|
2016-03-09 11:58:40 +01:00
|
|
|
return bufferReverse(this.getHash()).toString('hex')
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2016-08-10 03:43:36 +02:00
|
|
|
Transaction.prototype.toBuffer = function (buffer, initialOffset) {
|
2016-07-12 04:31:54 +02:00
|
|
|
if (!buffer) buffer = new Buffer(this.byteLength())
|
2013-10-07 21:27:19 +02:00
|
|
|
|
2016-08-10 03:43:36 +02:00
|
|
|
var offset = initialOffset || 0
|
2016-10-09 13:53:41 +02:00
|
|
|
function writeSlice (slice) { offset += slice.copy(buffer, offset) }
|
|
|
|
function writeUInt32 (i) { offset = buffer.writeUInt32LE(i, offset) }
|
2016-10-12 16:04:03 +02:00
|
|
|
function writeInt32 (i) { offset = buffer.writeInt32LE(i, offset) }
|
2016-10-09 13:53:41 +02:00
|
|
|
function writeUInt64 (i) { offset = bufferutils.writeUInt64LE(buffer, i, offset) }
|
|
|
|
function writeVarInt (i) { offset += bufferutils.writeVarInt(buffer, i, offset) }
|
2014-10-16 06:30:12 +02:00
|
|
|
|
2016-10-12 16:04:03 +02:00
|
|
|
writeInt32(this.version)
|
2014-10-24 04:58:32 +02:00
|
|
|
writeVarInt(this.ins.length)
|
2014-05-01 22:36:21 +02:00
|
|
|
|
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)
|
2015-08-07 08:30:24 +02:00
|
|
|
writeVarInt(txIn.script.length)
|
|
|
|
writeSlice(txIn.script)
|
2014-12-02 04:20:04 +01:00
|
|
|
writeUInt32(txIn.sequence)
|
2014-10-24 04:58:32 +02:00
|
|
|
})
|
2014-03-23 20:02:31 +01:00
|
|
|
|
2014-10-24 04:58:32 +02:00
|
|
|
writeVarInt(this.outs.length)
|
2015-02-23 00:36:57 +01:00
|
|
|
this.outs.forEach(function (txOut) {
|
2015-04-28 02:35:32 +02:00
|
|
|
if (!txOut.valueBuffer) {
|
|
|
|
writeUInt64(txOut.value)
|
|
|
|
} else {
|
|
|
|
writeSlice(txOut.valueBuffer)
|
|
|
|
}
|
|
|
|
|
2015-08-07 08:30:24 +02:00
|
|
|
writeVarInt(txOut.script.length)
|
|
|
|
writeSlice(txOut.script)
|
2014-10-24 04:58:32 +02:00
|
|
|
})
|
2014-03-23 20:02:31 +01:00
|
|
|
|
2014-10-24 04:58:32 +02:00
|
|
|
writeUInt32(this.locktime)
|
2014-03-09 06:46:20 +01:00
|
|
|
|
2016-08-10 03:43:36 +02:00
|
|
|
// avoid slicing unless necessary
|
|
|
|
if (initialOffset !== undefined) return buffer.slice(initialOffset, offset)
|
|
|
|
|
2014-10-24 04:58:32 +02:00
|
|
|
return buffer
|
2013-10-07 14:21:00 +02:00
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
Transaction.prototype.toHex = function () {
|
2014-10-24 04:58:32 +02:00
|
|
|
return this.toBuffer().toString('hex')
|
2014-05-08 02:44:35 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 05:31:29 +02:00
|
|
|
Transaction.prototype.setInputScript = function (index, scriptSig) {
|
2015-08-07 08:30:24 +02:00
|
|
|
typeforce(types.tuple(types.Number, types.Buffer), arguments)
|
2015-02-05 04:33:31 +01:00
|
|
|
|
2015-08-20 05:31:29 +02:00
|
|
|
this.ins[index].script = scriptSig
|
2014-07-25 08:11:45 +02:00
|
|
|
}
|
|
|
|
|
2014-05-16 09:12:39 +02:00
|
|
|
module.exports = Transaction
|