2018-06-25 08:25:12 +02:00
|
|
|
const Buffer = require('safe-buffer').Buffer
|
|
|
|
const bcrypto = require('./crypto')
|
|
|
|
const bscript = require('./script')
|
|
|
|
const bufferutils = require('./bufferutils')
|
|
|
|
const opcodes = require('bitcoin-ops')
|
|
|
|
const typeforce = require('typeforce')
|
|
|
|
const types = require('./types')
|
|
|
|
const varuint = require('varuint-bitcoin')
|
2014-06-04 06:07:29 +02:00
|
|
|
|
2016-07-14 11:50:35 +02:00
|
|
|
function varSliceSize (someScript) {
|
2018-06-25 08:37:45 +02:00
|
|
|
const length = someScript.length
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2017-04-19 06:41:59 +02:00
|
|
|
return varuint.encodingLength(length) + length
|
2016-07-14 11:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function vectorSize (someVector) {
|
2018-06-25 08:37:45 +02:00
|
|
|
const length = someVector.length
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2017-04-19 06:41:59 +02:00
|
|
|
return varuint.encodingLength(length) + someVector.reduce(function (sum, witness) {
|
2016-07-14 11:50:35 +02:00
|
|
|
return sum + varSliceSize(witness)
|
|
|
|
}, 0)
|
|
|
|
}
|
|
|
|
|
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
|
2016-07-14 11:50:35 +02:00
|
|
|
Transaction.ADVANCED_TRANSACTION_MARKER = 0x00
|
|
|
|
Transaction.ADVANCED_TRANSACTION_FLAG = 0x01
|
|
|
|
|
2018-06-25 08:25:12 +02:00
|
|
|
const EMPTY_SCRIPT = Buffer.allocUnsafe(0)
|
|
|
|
const EMPTY_WITNESS = []
|
|
|
|
const ZERO = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
|
|
|
|
const ONE = Buffer.from('0000000000000000000000000000000000000000000000000000000000000001', 'hex')
|
|
|
|
const VALUE_UINT64_MAX = Buffer.from('ffffffffffffffff', 'hex')
|
|
|
|
const BLANK_OUTPUT = {
|
2016-07-14 11:50:35 +02:00
|
|
|
script: EMPTY_SCRIPT,
|
|
|
|
valueBuffer: VALUE_UINT64_MAX
|
|
|
|
}
|
2014-09-15 06:21:01 +02:00
|
|
|
|
2015-08-14 00:27:27 +02:00
|
|
|
Transaction.fromBuffer = function (buffer, __noStrict) {
|
2018-06-25 08:37:45 +02:00
|
|
|
let 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 () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const i = buffer.readUInt32LE(offset)
|
2014-10-24 04:58:32 +02:00
|
|
|
offset += 4
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2016-10-12 16:04:03 +02:00
|
|
|
function readInt32 () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const i = buffer.readInt32LE(offset)
|
2016-10-12 16:04:03 +02:00
|
|
|
offset += 4
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
function readUInt64 () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const i = bufferutils.readUInt64LE(buffer, offset)
|
2014-10-24 04:58:32 +02:00
|
|
|
offset += 8
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
function readVarInt () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const vi = varuint.decode(buffer, offset)
|
2017-04-19 06:41:59 +02:00
|
|
|
offset += varuint.decode.bytes
|
|
|
|
return vi
|
2014-10-24 04:58:32 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 11:50:35 +02:00
|
|
|
function readVarSlice () {
|
2015-08-07 08:30:24 +02:00
|
|
|
return readSlice(readVarInt())
|
2015-02-19 02:30:23 +01:00
|
|
|
}
|
|
|
|
|
2016-07-14 11:50:35 +02:00
|
|
|
function readVector () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const count = readVarInt()
|
|
|
|
const vector = []
|
2016-07-14 11:50:35 +02:00
|
|
|
for (var i = 0; i < count; i++) vector.push(readVarSlice())
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
const tx = new Transaction()
|
2016-10-12 16:04:03 +02:00
|
|
|
tx.version = readInt32()
|
2014-10-24 04:58:32 +02:00
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
const marker = buffer.readUInt8(offset)
|
|
|
|
const flag = buffer.readUInt8(offset + 1)
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
let hasWitnesses = false
|
2016-07-14 11:50:35 +02:00
|
|
|
if (marker === Transaction.ADVANCED_TRANSACTION_MARKER &&
|
|
|
|
flag === Transaction.ADVANCED_TRANSACTION_FLAG) {
|
|
|
|
offset += 2
|
|
|
|
hasWitnesses = true
|
|
|
|
}
|
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
const vinLen = readVarInt()
|
2014-10-24 04:58:32 +02:00
|
|
|
for (var i = 0; i < vinLen; ++i) {
|
2015-09-05 06:28:28 +02:00
|
|
|
tx.ins.push({
|
|
|
|
hash: readSlice(32),
|
|
|
|
index: readUInt32(),
|
2016-07-14 11:50:35 +02:00
|
|
|
script: readVarSlice(),
|
|
|
|
sequence: readUInt32(),
|
|
|
|
witness: EMPTY_WITNESS
|
2015-09-05 06:28:28 +02:00
|
|
|
})
|
2014-10-24 04:58:32 +02:00
|
|
|
}
|
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
const voutLen = readVarInt()
|
2014-10-24 04:58:32 +02:00
|
|
|
for (i = 0; i < voutLen; ++i) {
|
|
|
|
tx.outs.push({
|
|
|
|
value: readUInt64(),
|
2016-07-14 11:50:35 +02:00
|
|
|
script: readVarSlice()
|
2014-10-24 04:58:32 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-14 11:50:35 +02:00
|
|
|
if (hasWitnesses) {
|
|
|
|
for (i = 0; i < vinLen; ++i) {
|
|
|
|
tx.ins[i].witness = readVector()
|
|
|
|
}
|
2016-11-08 13:43:45 +01:00
|
|
|
|
|
|
|
// was this pointless?
|
2016-12-07 13:04:31 +01:00
|
|
|
if (!tx.hasWitnesses()) throw new Error('Transaction has superfluous witness data')
|
2016-07-14 11:50:35 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2017-01-20 21:07:19 +01:00
|
|
|
return Transaction.fromBuffer(Buffer.from(hex, 'hex'))
|
2014-10-24 04:58:32 +02:00
|
|
|
}
|
|
|
|
|
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-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,
|
2016-07-14 11:50:35 +02:00
|
|
|
sequence: sequence,
|
|
|
|
witness: EMPTY_WITNESS
|
2015-03-02 03:31:03 +01:00
|
|
|
}) - 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
|
|
|
|
2016-12-07 13:04:31 +01:00
|
|
|
Transaction.prototype.hasWitnesses = function () {
|
2016-07-14 11:50:35 +02:00
|
|
|
return this.ins.some(function (x) {
|
2016-11-08 13:43:45 +01:00
|
|
|
return x.witness.length !== 0
|
2016-07-14 11:50:35 +02:00
|
|
|
})
|
|
|
|
}
|
2015-03-16 00:50:47 +01:00
|
|
|
|
2017-06-20 13:46:46 +02:00
|
|
|
Transaction.prototype.weight = function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const base = this.__byteLength(false)
|
|
|
|
const total = this.__byteLength(true)
|
2017-06-20 13:46:46 +02:00
|
|
|
return base * 3 + total
|
|
|
|
}
|
|
|
|
|
|
|
|
Transaction.prototype.virtualSize = function () {
|
|
|
|
return Math.ceil(this.weight() / 4)
|
|
|
|
}
|
|
|
|
|
2016-07-14 11:50:35 +02:00
|
|
|
Transaction.prototype.byteLength = function () {
|
2016-11-14 01:37:45 +01:00
|
|
|
return this.__byteLength(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
Transaction.prototype.__byteLength = function (__allowWitness) {
|
2018-06-25 08:37:45 +02:00
|
|
|
const hasWitnesses = __allowWitness && this.hasWitnesses()
|
2015-03-16 00:50:47 +01:00
|
|
|
|
|
|
|
return (
|
2016-07-14 11:50:35 +02:00
|
|
|
(hasWitnesses ? 10 : 8) +
|
2017-04-19 06:41:59 +02:00
|
|
|
varuint.encodingLength(this.ins.length) +
|
|
|
|
varuint.encodingLength(this.outs.length) +
|
2016-07-14 11:50:35 +02:00
|
|
|
this.ins.reduce(function (sum, input) { return sum + 40 + varSliceSize(input.script) }, 0) +
|
|
|
|
this.outs.reduce(function (sum, output) { return sum + 8 + varSliceSize(output.script) }, 0) +
|
|
|
|
(hasWitnesses ? this.ins.reduce(function (sum, input) { return sum + vectorSize(input.witness) }, 0) : 0)
|
2015-03-16 00:50:47 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2014-10-24 04:58:32 +02:00
|
|
|
Transaction.prototype.clone = function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const newTx = new Transaction()
|
2014-10-24 04:58:32 +02:00
|
|
|
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,
|
2016-07-14 11:50:35 +02:00
|
|
|
sequence: txIn.sequence,
|
|
|
|
witness: txIn.witness
|
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
|
|
|
}
|
|
|
|
|
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
|
2018-06-25 08:37:45 +02:00
|
|
|
const 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
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
const 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)
|
2016-12-10 01:41:12 +01:00
|
|
|
txTmp.ins.forEach(function (input, y) {
|
|
|
|
if (y === inIndex) return
|
2016-06-22 05:54:55 +02:00
|
|
|
|
|
|
|
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
|
2018-06-25 08:37:45 +02:00
|
|
|
const buffer = Buffer.allocUnsafe(txTmp.__byteLength(false) + 4)
|
2015-04-28 02:35:32 +02:00
|
|
|
buffer.writeInt32LE(hashType, buffer.length - 4)
|
2016-11-14 01:37:45 +01:00
|
|
|
txTmp.__toBuffer(buffer, 0, false)
|
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
|
|
|
|
2016-11-09 02:30:06 +01:00
|
|
|
Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value, hashType) {
|
2016-07-14 11:50:35 +02:00
|
|
|
typeforce(types.tuple(types.UInt32, types.Buffer, types.Satoshi, types.UInt32), arguments)
|
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
let tbuffer, toffset
|
2016-07-14 11:50:35 +02:00
|
|
|
function writeSlice (slice) { toffset += slice.copy(tbuffer, toffset) }
|
|
|
|
function writeUInt32 (i) { toffset = tbuffer.writeUInt32LE(i, toffset) }
|
|
|
|
function writeUInt64 (i) { toffset = bufferutils.writeUInt64LE(tbuffer, i, toffset) }
|
2017-04-19 06:41:59 +02:00
|
|
|
function writeVarInt (i) {
|
|
|
|
varuint.encode(i, tbuffer, toffset)
|
|
|
|
toffset += varuint.encode.bytes
|
|
|
|
}
|
2016-07-14 11:50:35 +02:00
|
|
|
function writeVarSlice (slice) { writeVarInt(slice.length); writeSlice(slice) }
|
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
let hashOutputs = ZERO
|
|
|
|
let hashPrevouts = ZERO
|
|
|
|
let hashSequence = ZERO
|
2016-11-09 01:54:17 +01:00
|
|
|
|
2016-07-14 11:50:35 +02:00
|
|
|
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY)) {
|
2017-04-19 09:24:58 +02:00
|
|
|
tbuffer = Buffer.allocUnsafe(36 * this.ins.length)
|
2016-07-14 11:50:35 +02:00
|
|
|
toffset = 0
|
|
|
|
|
|
|
|
this.ins.forEach(function (txIn) {
|
|
|
|
writeSlice(txIn.hash)
|
|
|
|
writeUInt32(txIn.index)
|
|
|
|
})
|
|
|
|
|
|
|
|
hashPrevouts = bcrypto.hash256(tbuffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY) &&
|
|
|
|
(hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
|
|
|
|
(hashType & 0x1f) !== Transaction.SIGHASH_NONE) {
|
2017-04-19 09:24:58 +02:00
|
|
|
tbuffer = Buffer.allocUnsafe(4 * this.ins.length)
|
2016-07-14 11:50:35 +02:00
|
|
|
toffset = 0
|
|
|
|
|
|
|
|
this.ins.forEach(function (txIn) {
|
|
|
|
writeUInt32(txIn.sequence)
|
|
|
|
})
|
|
|
|
|
|
|
|
hashSequence = bcrypto.hash256(tbuffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
|
|
|
|
(hashType & 0x1f) !== Transaction.SIGHASH_NONE) {
|
2018-06-25 08:37:45 +02:00
|
|
|
const txOutsSize = this.outs.reduce(function (sum, output) {
|
2016-07-14 11:50:35 +02:00
|
|
|
return sum + 8 + varSliceSize(output.script)
|
|
|
|
}, 0)
|
|
|
|
|
2017-04-19 09:24:58 +02:00
|
|
|
tbuffer = Buffer.allocUnsafe(txOutsSize)
|
2016-07-14 11:50:35 +02:00
|
|
|
toffset = 0
|
|
|
|
|
|
|
|
this.outs.forEach(function (out) {
|
|
|
|
writeUInt64(out.value)
|
|
|
|
writeVarSlice(out.script)
|
|
|
|
})
|
|
|
|
|
|
|
|
hashOutputs = bcrypto.hash256(tbuffer)
|
|
|
|
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE && inIndex < this.outs.length) {
|
2018-06-25 08:37:45 +02:00
|
|
|
const output = this.outs[inIndex]
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2017-04-19 09:24:58 +02:00
|
|
|
tbuffer = Buffer.allocUnsafe(8 + varSliceSize(output.script))
|
2016-07-14 11:50:35 +02:00
|
|
|
toffset = 0
|
|
|
|
writeUInt64(output.value)
|
|
|
|
writeVarSlice(output.script)
|
|
|
|
|
|
|
|
hashOutputs = bcrypto.hash256(tbuffer)
|
|
|
|
}
|
|
|
|
|
2017-04-19 09:24:58 +02:00
|
|
|
tbuffer = Buffer.allocUnsafe(156 + varSliceSize(prevOutScript))
|
2016-07-14 11:50:35 +02:00
|
|
|
toffset = 0
|
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
const input = this.ins[inIndex]
|
2016-07-14 11:50:35 +02:00
|
|
|
writeUInt32(this.version)
|
|
|
|
writeSlice(hashPrevouts)
|
|
|
|
writeSlice(hashSequence)
|
|
|
|
writeSlice(input.hash)
|
|
|
|
writeUInt32(input.index)
|
|
|
|
writeVarSlice(prevOutScript)
|
2016-11-09 02:30:06 +01:00
|
|
|
writeUInt64(value)
|
2016-07-14 11:50:35 +02:00
|
|
|
writeUInt32(input.sequence)
|
|
|
|
writeSlice(hashOutputs)
|
|
|
|
writeUInt32(this.locktime)
|
|
|
|
writeUInt32(hashType)
|
|
|
|
return bcrypto.hash256(tbuffer)
|
|
|
|
}
|
|
|
|
|
2014-06-16 10:52:06 +02:00
|
|
|
Transaction.prototype.getHash = function () {
|
2016-11-14 01:37:45 +01:00
|
|
|
return bcrypto.hash256(this.__toBuffer(undefined, undefined, false))
|
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-12-21 02:52:09 +01:00
|
|
|
return this.getHash().reverse().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-11-14 01:37:45 +01:00
|
|
|
return this.__toBuffer(buffer, initialOffset, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
Transaction.prototype.__toBuffer = function (buffer, initialOffset, __allowWitness) {
|
2017-04-19 09:24:58 +02:00
|
|
|
if (!buffer) buffer = Buffer.allocUnsafe(this.__byteLength(__allowWitness))
|
2013-10-07 21:27:19 +02:00
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
let offset = initialOffset || 0
|
2016-10-09 13:53:41 +02:00
|
|
|
function writeSlice (slice) { offset += slice.copy(buffer, offset) }
|
2016-07-14 11:50:35 +02:00
|
|
|
function writeUInt8 (i) { offset = buffer.writeUInt8(i, offset) }
|
2016-10-09 13:53:41 +02:00
|
|
|
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) }
|
2017-04-19 06:41:59 +02:00
|
|
|
function writeVarInt (i) {
|
|
|
|
varuint.encode(i, buffer, offset)
|
|
|
|
offset += varuint.encode.bytes
|
|
|
|
}
|
2016-07-14 11:50:35 +02:00
|
|
|
function writeVarSlice (slice) { writeVarInt(slice.length); writeSlice(slice) }
|
|
|
|
function writeVector (vector) { writeVarInt(vector.length); vector.forEach(writeVarSlice) }
|
2014-10-16 06:30:12 +02:00
|
|
|
|
2016-10-12 16:04:03 +02:00
|
|
|
writeInt32(this.version)
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
const hasWitnesses = __allowWitness && this.hasWitnesses()
|
2016-07-14 11:50:35 +02:00
|
|
|
|
|
|
|
if (hasWitnesses) {
|
|
|
|
writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER)
|
|
|
|
writeUInt8(Transaction.ADVANCED_TRANSACTION_FLAG)
|
|
|
|
}
|
|
|
|
|
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)
|
2016-07-14 11:50:35 +02:00
|
|
|
writeVarSlice(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)
|
|
|
|
}
|
|
|
|
|
2016-07-14 11:50:35 +02:00
|
|
|
writeVarSlice(txOut.script)
|
2014-10-24 04:58:32 +02:00
|
|
|
})
|
2014-03-23 20:02:31 +01:00
|
|
|
|
2016-11-08 13:43:45 +01:00
|
|
|
if (hasWitnesses) {
|
2016-07-14 11:50:35 +02:00
|
|
|
this.ins.forEach(function (input) {
|
|
|
|
writeVector(input.witness)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-07-14 11:50:35 +02:00
|
|
|
Transaction.prototype.setWitness = function (index, witness) {
|
2016-11-14 03:40:06 +01:00
|
|
|
typeforce(types.tuple(types.Number, [types.Buffer]), arguments)
|
2016-07-14 11:50:35 +02:00
|
|
|
|
|
|
|
this.ins[index].witness = witness
|
|
|
|
}
|
|
|
|
|
2014-05-16 09:12:39 +02:00
|
|
|
module.exports = Transaction
|