Browse Source

Merge pull request #1293 from bitcoinjs/revertTxES6

Revert "Merge pull request #1086 from bitcoinjs/refactorTransaction"
v4
Jonathan Underwood 6 years ago
committed by GitHub
parent
commit
293116b20f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 769
      src/transaction.js

769
src/transaction.js

@ -16,11 +16,26 @@ function varSliceSize (someScript) {
function vectorSize (someVector) {
const length = someVector.length
return varuint.encodingLength(length) + someVector.reduce((sum, witness) => {
return varuint.encodingLength(length) + someVector.reduce(function (sum, witness) {
return sum + varSliceSize(witness)
}, 0)
}
function Transaction () {
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
Transaction.ADVANCED_TRANSACTION_MARKER = 0x00
Transaction.ADVANCED_TRANSACTION_FLAG = 0x01
const EMPTY_SCRIPT = Buffer.allocUnsafe(0)
const EMPTY_WITNESS = []
const ZERO = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
@ -31,525 +46,447 @@ const BLANK_OUTPUT = {
valueBuffer: VALUE_UINT64_MAX
}
class Transaction {
constructor () {
this.version = 1
this.locktime = 0
this.ins = []
this.outs = []
Transaction.fromBuffer = function (buffer, __noStrict) {
let offset = 0
function readSlice (n) {
offset += n
return buffer.slice(offset - n, offset)
}
static get DEFAULT_SEQUENCE () {
return 0xffffffff
}
static get SIGHASH_ALL () {
return 0x01
}
static get SIGHASH_NONE () {
return 0x02
}
static get SIGHASH_SINGLE () {
return 0x03
}
static get SIGHASH_ANYONECANPAY () {
return 0x80
}
static get ADVANCED_TRANSACTION_MARKER () {
return 0x00
}
static get ADVANCED_TRANSACTION_FLAG () {
return 0x01
function readUInt32 () {
const i = buffer.readUInt32LE(offset)
offset += 4
return i
}
isCoinbase () {
return this.ins.length === 1 && Transaction.isCoinbaseHash(this.ins[0].hash)
function readInt32 () {
const i = buffer.readInt32LE(offset)
offset += 4
return i
}
addInput (hash, index, sequence, scriptSig) {
typeforce(types.tuple(
types.Hash256bit,
types.UInt32,
types.maybe(types.UInt32),
types.maybe(types.Buffer)
), arguments)
if (types.Null(sequence)) {
sequence = Transaction.DEFAULT_SEQUENCE
}
// Add the input and return the input's index
return (this.ins.push({
hash: hash,
index: index,
script: scriptSig || EMPTY_SCRIPT,
sequence: sequence,
witness: EMPTY_WITNESS
}) - 1)
function readUInt64 () {
const i = bufferutils.readUInt64LE(buffer, offset)
offset += 8
return i
}
addOutput (scriptPubKey, value) {
typeforce(types.tuple(types.Buffer, types.Satoshi), arguments)
// Add the output and return the output's index
return (this.outs.push({
script: scriptPubKey,
value: value
}) - 1)
function readVarInt () {
const vi = varuint.decode(buffer, offset)
offset += varuint.decode.bytes
return vi
}
hasWitnesses () {
return this.ins.some((x) => {
return x.witness.length !== 0
})
function readVarSlice () {
return readSlice(readVarInt())
}
weight () {
const base = this.__byteLength(false)
const total = this.__byteLength(true)
return base * 3 + total
function readVector () {
const count = readVarInt()
const vector = []
for (var i = 0; i < count; i++) vector.push(readVarSlice())
return vector
}
virtualSize () {
return Math.ceil(this.weight() / 4)
}
const tx = new Transaction()
tx.version = readInt32()
byteLength () {
return this.__byteLength(true)
}
const marker = buffer.readUInt8(offset)
const flag = buffer.readUInt8(offset + 1)
__byteLength (__allowWitness) {
const hasWitnesses = __allowWitness && this.hasWitnesses()
return (
(hasWitnesses ? 10 : 8) +
varuint.encodingLength(this.ins.length) +
varuint.encodingLength(this.outs.length) +
this.ins.reduce((sum, input) => {
return sum + 40 + varSliceSize(input.script)
}, 0) +
this.outs.reduce((sum, output) => {
return sum + 8 + varSliceSize(output.script)
}, 0) +
(hasWitnesses ? this.ins.reduce((sum, input) => {
return sum + vectorSize(input.witness)
}, 0) : 0)
)
let hasWitnesses = false
if (marker === Transaction.ADVANCED_TRANSACTION_MARKER &&
flag === Transaction.ADVANCED_TRANSACTION_FLAG) {
offset += 2
hasWitnesses = true
}
clone () {
const newTx = new Transaction()
newTx.version = this.version
newTx.locktime = this.locktime
newTx.ins = this.ins.map((txIn) => {
return {
hash: txIn.hash,
index: txIn.index,
script: txIn.script,
sequence: txIn.sequence,
witness: txIn.witness
}
const vinLen = readVarInt()
for (var i = 0; i < vinLen; ++i) {
tx.ins.push({
hash: readSlice(32),
index: readUInt32(),
script: readVarSlice(),
sequence: readUInt32(),
witness: EMPTY_WITNESS
})
}
newTx.outs = this.outs.map((txOut) => {
return {
script: txOut.script,
value: txOut.value
}
const voutLen = readVarInt()
for (i = 0; i < voutLen; ++i) {
tx.outs.push({
value: readUInt64(),
script: readVarSlice()
})
return newTx
}
/**
* 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.
*/
hashForSignature (inIndex, prevOutScript, hashType) {
typeforce(types.tuple(types.UInt32, types.Buffer, /* 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
// ignore OP_CODESEPARATOR
const ourScript = bscript.compile(bscript.decompile(prevOutScript).filter((x) => {
return x !== opcodes.OP_CODESEPARATOR
}))
const txTmp = this.clone()
// SIGHASH_NONE: ignore all outputs? (wildcard payee)
if ((hashType & 0x1f) === Transaction.SIGHASH_NONE) {
txTmp.outs = []
// ignore sequence numbers (except at inIndex)
txTmp.ins.forEach((input, i) => {
if (i === inIndex) return
input.sequence = 0
})
// SIGHASH_SINGLE: ignore all outputs, except at the same index?
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE) {
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L60
if (inIndex >= this.outs.length) return ONE
// truncate outputs after
txTmp.outs.length = inIndex + 1
// "blank" outputs before
for (var i = 0; i < inIndex; i++) {
txTmp.outs[i] = BLANK_OUTPUT
}
// ignore sequence numbers (except at inIndex)
txTmp.ins.forEach((input, y) => {
if (y === inIndex) return
input.sequence = 0
})
}
// SIGHASH_ANYONECANPAY: ignore inputs entirely?
if (hashType & Transaction.SIGHASH_ANYONECANPAY) {
txTmp.ins = [txTmp.ins[inIndex]]
txTmp.ins[0].script = ourScript
// SIGHASH_ALL: only ignore input scripts
} else {
// "blank" others input scripts
txTmp.ins.forEach((input) => {
input.script = EMPTY_SCRIPT
})
txTmp.ins[inIndex].script = ourScript
if (hasWitnesses) {
for (i = 0; i < vinLen; ++i) {
tx.ins[i].witness = readVector()
}
// serialize and hash
const buffer = Buffer.allocUnsafe(txTmp.__byteLength(false) + 4)
buffer.writeInt32LE(hashType, buffer.length - 4)
txTmp.__toBuffer(buffer, 0, false)
return bcrypto.hash256(buffer)
// was this pointless?
if (!tx.hasWitnesses()) throw new Error('Transaction has superfluous witness data')
}
hashForWitnessV0 (inIndex, prevOutScript, value, hashType) {
typeforce(types.tuple(types.UInt32, types.Buffer, types.Satoshi, types.UInt32), arguments)
let tbuffer, toffset
tx.locktime = readUInt32()
function writeSlice (slice) {
toffset += slice.copy(tbuffer, toffset)
}
if (__noStrict) return tx
if (offset !== buffer.length) throw new Error('Transaction has unexpected data')
function writeUInt32 (i) {
toffset = tbuffer.writeUInt32LE(i, toffset)
}
return tx
}
function writeUInt64 (i) {
toffset = bufferutils.writeUInt64LE(tbuffer, i, toffset)
}
Transaction.fromHex = function (hex) {
return Transaction.fromBuffer(Buffer.from(hex, 'hex'))
}
function writeVarInt (i) {
varuint.encode(i, tbuffer, toffset)
toffset += varuint.encode.bytes
}
Transaction.isCoinbaseHash = function (buffer) {
typeforce(types.Hash256bit, buffer)
for (var i = 0; i < 32; ++i) {
if (buffer[i] !== 0) return false
}
return true
}
function writeVarSlice (slice) {
writeVarInt(slice.length)
writeSlice(slice)
}
Transaction.prototype.isCoinbase = function () {
return this.ins.length === 1 && Transaction.isCoinbaseHash(this.ins[0].hash)
}
let hashOutputs = ZERO
let hashPrevouts = ZERO
let hashSequence = ZERO
Transaction.prototype.addInput = function (hash, index, sequence, scriptSig) {
typeforce(types.tuple(
types.Hash256bit,
types.UInt32,
types.maybe(types.UInt32),
types.maybe(types.Buffer)
), arguments)
if (types.Null(sequence)) {
sequence = Transaction.DEFAULT_SEQUENCE
}
// Add the input and return the input's index
return (this.ins.push({
hash: hash,
index: index,
script: scriptSig || EMPTY_SCRIPT,
sequence: sequence,
witness: EMPTY_WITNESS
}) - 1)
}
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY)) {
tbuffer = Buffer.allocUnsafe(36 * this.ins.length)
toffset = 0
Transaction.prototype.addOutput = function (scriptPubKey, value) {
typeforce(types.tuple(types.Buffer, types.Satoshi), arguments)
this.ins.forEach((txIn) => {
writeSlice(txIn.hash)
writeUInt32(txIn.index)
})
// Add the output and return the output's index
return (this.outs.push({
script: scriptPubKey,
value: value
}) - 1)
}
hashPrevouts = bcrypto.hash256(tbuffer)
}
Transaction.prototype.hasWitnesses = function () {
return this.ins.some(function (x) {
return x.witness.length !== 0
})
}
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY) &&
(hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
(hashType & 0x1f) !== Transaction.SIGHASH_NONE) {
tbuffer = Buffer.allocUnsafe(4 * this.ins.length)
toffset = 0
Transaction.prototype.weight = function () {
const base = this.__byteLength(false)
const total = this.__byteLength(true)
return base * 3 + total
}
this.ins.forEach((txIn) => {
writeUInt32(txIn.sequence)
})
Transaction.prototype.virtualSize = function () {
return Math.ceil(this.weight() / 4)
}
hashSequence = bcrypto.hash256(tbuffer)
}
Transaction.prototype.byteLength = function () {
return this.__byteLength(true)
}
if ((hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
(hashType & 0x1f) !== Transaction.SIGHASH_NONE) {
const txOutsSize = this.outs.reduce((sum, output) => {
return sum + 8 + varSliceSize(output.script)
}, 0)
Transaction.prototype.__byteLength = function (__allowWitness) {
const hasWitnesses = __allowWitness && this.hasWitnesses()
return (
(hasWitnesses ? 10 : 8) +
varuint.encodingLength(this.ins.length) +
varuint.encodingLength(this.outs.length) +
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)
)
}
tbuffer = Buffer.allocUnsafe(txOutsSize)
toffset = 0
Transaction.prototype.clone = function () {
const newTx = new Transaction()
newTx.version = this.version
newTx.locktime = this.locktime
newTx.ins = this.ins.map(function (txIn) {
return {
hash: txIn.hash,
index: txIn.index,
script: txIn.script,
sequence: txIn.sequence,
witness: txIn.witness
}
})
this.outs.forEach((out) => {
writeUInt64(out.value)
writeVarSlice(out.script)
})
newTx.outs = this.outs.map(function (txOut) {
return {
script: txOut.script,
value: txOut.value
}
})
hashOutputs = bcrypto.hash256(tbuffer)
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE && inIndex < this.outs.length) {
const output = this.outs[inIndex]
return newTx
}
tbuffer = Buffer.allocUnsafe(8 + varSliceSize(output.script))
toffset = 0
writeUInt64(output.value)
writeVarSlice(output.script)
/**
* 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.
*/
Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashType) {
typeforce(types.tuple(types.UInt32, types.Buffer, /* types.UInt8 */ types.Number), arguments)
hashOutputs = bcrypto.hash256(tbuffer)
}
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L29
if (inIndex >= this.ins.length) return ONE
tbuffer = Buffer.allocUnsafe(156 + varSliceSize(prevOutScript))
toffset = 0
// ignore OP_CODESEPARATOR
const ourScript = bscript.compile(bscript.decompile(prevOutScript).filter(function (x) {
return x !== opcodes.OP_CODESEPARATOR
}))
const input = this.ins[inIndex]
writeUInt32(this.version)
writeSlice(hashPrevouts)
writeSlice(hashSequence)
writeSlice(input.hash)
writeUInt32(input.index)
writeVarSlice(prevOutScript)
writeUInt64(value)
writeUInt32(input.sequence)
writeSlice(hashOutputs)
writeUInt32(this.locktime)
writeUInt32(hashType)
return bcrypto.hash256(tbuffer)
}
const txTmp = this.clone()
getHash () {
return bcrypto.hash256(this.__toBuffer(undefined, undefined, false))
}
// SIGHASH_NONE: ignore all outputs? (wildcard payee)
if ((hashType & 0x1f) === Transaction.SIGHASH_NONE) {
txTmp.outs = []
getId () {
// transaction hash's are displayed in reverse order
return this.getHash().reverse().toString('hex')
}
// ignore sequence numbers (except at inIndex)
txTmp.ins.forEach(function (input, i) {
if (i === inIndex) return
toBuffer (buffer, initialOffset) {
return this.__toBuffer(buffer, initialOffset, true)
}
input.sequence = 0
})
__toBuffer (buffer, initialOffset, __allowWitness) {
if (!buffer) buffer = Buffer.allocUnsafe(this.__byteLength(__allowWitness))
// SIGHASH_SINGLE: ignore all outputs, except at the same index?
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE) {
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L60
if (inIndex >= this.outs.length) return ONE
let offset = initialOffset || 0
// truncate outputs after
txTmp.outs.length = inIndex + 1
function writeSlice (slice) {
offset += slice.copy(buffer, offset)
// "blank" outputs before
for (var i = 0; i < inIndex; i++) {
txTmp.outs[i] = BLANK_OUTPUT
}
function writeUInt8 (i) {
offset = buffer.writeUInt8(i, offset)
}
// ignore sequence numbers (except at inIndex)
txTmp.ins.forEach(function (input, y) {
if (y === inIndex) return
function writeUInt32 (i) {
offset = buffer.writeUInt32LE(i, offset)
}
input.sequence = 0
})
}
function writeInt32 (i) {
offset = buffer.writeInt32LE(i, offset)
}
// SIGHASH_ANYONECANPAY: ignore inputs entirely?
if (hashType & Transaction.SIGHASH_ANYONECANPAY) {
txTmp.ins = [txTmp.ins[inIndex]]
txTmp.ins[0].script = ourScript
function writeUInt64 (i) {
offset = bufferutils.writeUInt64LE(buffer, i, offset)
}
// 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
}
function writeVarInt (i) {
varuint.encode(i, buffer, offset)
offset += varuint.encode.bytes
}
// serialize and hash
const buffer = Buffer.allocUnsafe(txTmp.__byteLength(false) + 4)
buffer.writeInt32LE(hashType, buffer.length - 4)
txTmp.__toBuffer(buffer, 0, false)
function writeVarSlice (slice) {
writeVarInt(slice.length)
writeSlice(slice)
}
return bcrypto.hash256(buffer)
}
function writeVector (vector) {
writeVarInt(vector.length)
vector.forEach(writeVarSlice)
}
Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value, hashType) {
typeforce(types.tuple(types.UInt32, types.Buffer, types.Satoshi, types.UInt32), arguments)
writeInt32(this.version)
let tbuffer, toffset
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) }
function writeVarInt (i) {
varuint.encode(i, tbuffer, toffset)
toffset += varuint.encode.bytes
}
function writeVarSlice (slice) { writeVarInt(slice.length); writeSlice(slice) }
const hasWitnesses = __allowWitness && this.hasWitnesses()
let hashOutputs = ZERO
let hashPrevouts = ZERO
let hashSequence = ZERO
if (hasWitnesses) {
writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER)
writeUInt8(Transaction.ADVANCED_TRANSACTION_FLAG)
}
writeVarInt(this.ins.length)
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY)) {
tbuffer = Buffer.allocUnsafe(36 * this.ins.length)
toffset = 0
this.ins.forEach((txIn) => {
this.ins.forEach(function (txIn) {
writeSlice(txIn.hash)
writeUInt32(txIn.index)
writeVarSlice(txIn.script)
writeUInt32(txIn.sequence)
})
writeVarInt(this.outs.length)
this.outs.forEach((txOut) => {
if (!txOut.valueBuffer) {
writeUInt64(txOut.value)
} else {
writeSlice(txOut.valueBuffer)
}
writeVarSlice(txOut.script)
})
hashPrevouts = bcrypto.hash256(tbuffer)
}
if (hasWitnesses) {
this.ins.forEach((input) => {
writeVector(input.witness)
})
}
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY) &&
(hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
(hashType & 0x1f) !== Transaction.SIGHASH_NONE) {
tbuffer = Buffer.allocUnsafe(4 * this.ins.length)
toffset = 0
writeUInt32(this.locktime)
this.ins.forEach(function (txIn) {
writeUInt32(txIn.sequence)
})
// avoid slicing unless necessary
if (initialOffset !== undefined) return buffer.slice(initialOffset, offset)
return buffer
hashSequence = bcrypto.hash256(tbuffer)
}
toHex () {
return this.toBuffer().toString('hex')
}
if ((hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
(hashType & 0x1f) !== Transaction.SIGHASH_NONE) {
const txOutsSize = this.outs.reduce(function (sum, output) {
return sum + 8 + varSliceSize(output.script)
}, 0)
setInputScript (index, scriptSig) {
typeforce(types.tuple(types.Number, types.Buffer), arguments)
tbuffer = Buffer.allocUnsafe(txOutsSize)
toffset = 0
this.ins[index].script = scriptSig
}
this.outs.forEach(function (out) {
writeUInt64(out.value)
writeVarSlice(out.script)
})
setWitness (index, witness) {
typeforce(types.tuple(types.Number, [types.Buffer]), arguments)
hashOutputs = bcrypto.hash256(tbuffer)
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE && inIndex < this.outs.length) {
const output = this.outs[inIndex]
this.ins[index].witness = witness
}
tbuffer = Buffer.allocUnsafe(8 + varSliceSize(output.script))
toffset = 0
writeUInt64(output.value)
writeVarSlice(output.script)
hashOutputs = bcrypto.hash256(tbuffer)
}
tbuffer = Buffer.allocUnsafe(156 + varSliceSize(prevOutScript))
toffset = 0
const input = this.ins[inIndex]
writeUInt32(this.version)
writeSlice(hashPrevouts)
writeSlice(hashSequence)
writeSlice(input.hash)
writeUInt32(input.index)
writeVarSlice(prevOutScript)
writeUInt64(value)
writeUInt32(input.sequence)
writeSlice(hashOutputs)
writeUInt32(this.locktime)
writeUInt32(hashType)
return bcrypto.hash256(tbuffer)
}
Transaction.fromBuffer = (buffer, __noStrict) => {
let offset = 0
Transaction.prototype.getHash = function () {
return bcrypto.hash256(this.__toBuffer(undefined, undefined, false))
}
function readSlice (n) {
offset += n
return buffer.slice(offset - n, offset)
}
Transaction.prototype.getId = function () {
// transaction hash's are displayed in reverse order
return this.getHash().reverse().toString('hex')
}
function readUInt32 () {
const i = buffer.readUInt32LE(offset)
offset += 4
return i
}
Transaction.prototype.toBuffer = function (buffer, initialOffset) {
return this.__toBuffer(buffer, initialOffset, true)
}
function readInt32 () {
const i = buffer.readInt32LE(offset)
offset += 4
return i
}
Transaction.prototype.__toBuffer = function (buffer, initialOffset, __allowWitness) {
if (!buffer) buffer = Buffer.allocUnsafe(this.__byteLength(__allowWitness))
function readUInt64 () {
const i = bufferutils.readUInt64LE(buffer, offset)
offset += 8
return i
let offset = initialOffset || 0
function writeSlice (slice) { offset += slice.copy(buffer, offset) }
function writeUInt8 (i) { offset = buffer.writeUInt8(i, offset) }
function writeUInt32 (i) { offset = buffer.writeUInt32LE(i, offset) }
function writeInt32 (i) { offset = buffer.writeInt32LE(i, offset) }
function writeUInt64 (i) { offset = bufferutils.writeUInt64LE(buffer, i, offset) }
function writeVarInt (i) {
varuint.encode(i, buffer, offset)
offset += varuint.encode.bytes
}
function writeVarSlice (slice) { writeVarInt(slice.length); writeSlice(slice) }
function writeVector (vector) { writeVarInt(vector.length); vector.forEach(writeVarSlice) }
function readVarInt () {
const vi = varuint.decode(buffer, offset)
offset += varuint.decode.bytes
return vi
}
writeInt32(this.version)
function readVarSlice () {
return readSlice(readVarInt())
}
const hasWitnesses = __allowWitness && this.hasWitnesses()
function readVector () {
const count = readVarInt()
const vector = []
for (var i = 0; i < count; i++) vector.push(readVarSlice())
return vector
if (hasWitnesses) {
writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER)
writeUInt8(Transaction.ADVANCED_TRANSACTION_FLAG)
}
const tx = new Transaction()
tx.version = readInt32()
writeVarInt(this.ins.length)
const marker = buffer.readUInt8(offset)
const flag = buffer.readUInt8(offset + 1)
this.ins.forEach(function (txIn) {
writeSlice(txIn.hash)
writeUInt32(txIn.index)
writeVarSlice(txIn.script)
writeUInt32(txIn.sequence)
})
let hasWitnesses = false
if (marker === Transaction.ADVANCED_TRANSACTION_MARKER &&
flag === Transaction.ADVANCED_TRANSACTION_FLAG) {
offset += 2
hasWitnesses = true
}
writeVarInt(this.outs.length)
this.outs.forEach(function (txOut) {
if (!txOut.valueBuffer) {
writeUInt64(txOut.value)
} else {
writeSlice(txOut.valueBuffer)
}
const vinLen = readVarInt()
for (var i = 0; i < vinLen; ++i) {
tx.ins.push({
hash: readSlice(32),
index: readUInt32(),
script: readVarSlice(),
sequence: readUInt32(),
witness: EMPTY_WITNESS
})
}
writeVarSlice(txOut.script)
})
const voutLen = readVarInt()
for (i = 0; i < voutLen; ++i) {
tx.outs.push({
value: readUInt64(),
script: readVarSlice()
if (hasWitnesses) {
this.ins.forEach(function (input) {
writeVector(input.witness)
})
}
if (hasWitnesses) {
for (i = 0; i < vinLen; ++i) {
tx.ins[i].witness = readVector()
}
writeUInt32(this.locktime)
// was this pointless?
if (!tx.hasWitnesses()) throw new Error('Transaction has superfluous witness data')
}
// avoid slicing unless necessary
if (initialOffset !== undefined) return buffer.slice(initialOffset, offset)
return buffer
}
tx.locktime = readUInt32()
Transaction.prototype.toHex = function () {
return this.toBuffer().toString('hex')
}
if (__noStrict) return tx
if (offset !== buffer.length) throw new Error('Transaction has unexpected data')
Transaction.prototype.setInputScript = function (index, scriptSig) {
typeforce(types.tuple(types.Number, types.Buffer), arguments)
return tx
this.ins[index].script = scriptSig
}
Transaction.fromHex = (hex) => {
return Transaction.fromBuffer(Buffer.from(hex, 'hex'))
}
Transaction.prototype.setWitness = function (index, witness) {
typeforce(types.tuple(types.Number, [types.Buffer]), arguments)
Transaction.isCoinbaseHash = (buffer) => {
typeforce(types.Hash256bit, buffer)
for (var i = 0; i < 32; ++i) {
if (buffer[i] !== 0) return false
}
return true
this.ins[index].witness = witness
}
module.exports = Transaction

Loading…
Cancel
Save