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

Loading…
Cancel
Save