Transaction: re-order functions for consistency

This commit is contained in:
Daniel Cousens 2014-10-24 13:58:32 +11:00
parent 27e69ba716
commit a3a4d2a0d9

View file

@ -22,6 +22,66 @@ Transaction.SIGHASH_NONE = 0x02
Transaction.SIGHASH_SINGLE = 0x03 Transaction.SIGHASH_SINGLE = 0x03
Transaction.SIGHASH_ANYONECANPAY = 0x80 Transaction.SIGHASH_ANYONECANPAY = 0x80
Transaction.fromBuffer = function(buffer) {
var offset = 0
function readSlice(n) {
offset += n
return buffer.slice(offset - n, offset)
}
function readUInt32() {
var i = buffer.readUInt32LE(offset)
offset += 4
return i
}
function readUInt64() {
var i = bufferutils.readUInt64LE(buffer, offset)
offset += 8
return i
}
function readVarInt() {
var vi = bufferutils.readVarInt(buffer, offset)
offset += vi.size
return vi.number
}
function readScript() {
return Script.fromBuffer(readSlice(readVarInt()))
}
var tx = new Transaction()
tx.version = readUInt32()
var vinLen = readVarInt()
for (var i = 0; i < vinLen; ++i) {
tx.ins.push({
hash: readSlice(32),
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()
assert.equal(offset, buffer.length, 'Transaction has unexpected data')
return tx
}
Transaction.fromHex = function(hex) {
return Transaction.fromBuffer(new Buffer(hex, 'hex'))
}
/** /**
* Create a new txin. * Create a new txin.
* *
@ -91,69 +151,28 @@ Transaction.prototype.addOutput = function(scriptPubKey, value) {
}) - 1) }) - 1)
} }
Transaction.prototype.toBuffer = function () { Transaction.prototype.clone = function () {
var txInSize = this.ins.reduce(function(a, x) { var newTx = new Transaction()
return a + (40 + bufferutils.varIntSize(x.script.buffer.length) + x.script.buffer.length) newTx.version = this.version
}, 0) newTx.locktime = this.locktime
var txOutSize = this.outs.reduce(function(a, x) { newTx.ins = this.ins.map(function(txin) {
return a + (8 + bufferutils.varIntSize(x.script.buffer.length) + x.script.buffer.length) return {
}, 0) hash: txin.hash,
index: txin.index,
var buffer = new Buffer( script: txin.script,
8 + sequence: txin.sequence
bufferutils.varIntSize(this.ins.length) + }
bufferutils.varIntSize(this.outs.length) +
txInSize +
txOutSize
)
var offset = 0
function writeSlice(slice) {
slice.copy(buffer, offset)
offset += slice.length
}
function writeUInt32(i) {
buffer.writeUInt32LE(i, offset)
offset += 4
}
function writeUInt64(i) {
bufferutils.writeUInt64LE(buffer, i, offset)
offset += 8
}
function writeVarInt(i) {
var n = bufferutils.writeVarInt(buffer, i, offset)
offset += n
}
writeUInt32(this.version)
writeVarInt(this.ins.length)
this.ins.forEach(function(txin) {
writeSlice(txin.hash)
writeUInt32(txin.index)
writeVarInt(txin.script.buffer.length)
writeSlice(txin.script.buffer)
writeUInt32(txin.sequence)
}) })
writeVarInt(this.outs.length) newTx.outs = this.outs.map(function(txout) {
this.outs.forEach(function(txout) { return {
writeUInt64(txout.value) script: txout.script,
writeVarInt(txout.script.buffer.length) value: txout.value
writeSlice(txout.script.buffer) }
}) })
writeUInt32(this.locktime) return newTx
return buffer
}
Transaction.prototype.toHex = function() {
return this.toBuffer().toString('hex')
} }
/** /**
@ -220,88 +239,69 @@ Transaction.prototype.getId = function () {
return bufferutils.reverse(this.getHash()).toString('hex') return bufferutils.reverse(this.getHash()).toString('hex')
} }
Transaction.prototype.clone = function () { Transaction.prototype.toBuffer = function () {
var newTx = new Transaction() var txInSize = this.ins.reduce(function(a, x) {
newTx.version = this.version return a + (40 + bufferutils.varIntSize(x.script.buffer.length) + x.script.buffer.length)
newTx.locktime = this.locktime }, 0)
newTx.ins = this.ins.map(function(txin) { var txOutSize = this.outs.reduce(function(a, x) {
return { return a + (8 + bufferutils.varIntSize(x.script.buffer.length) + x.script.buffer.length)
hash: txin.hash, }, 0)
index: txin.index,
script: txin.script,
sequence: txin.sequence
}
})
newTx.outs = this.outs.map(function(txout) { var buffer = new Buffer(
return { 8 +
script: txout.script, bufferutils.varIntSize(this.ins.length) +
value: txout.value bufferutils.varIntSize(this.outs.length) +
} txInSize +
}) txOutSize
)
return newTx
}
Transaction.fromBuffer = function(buffer) {
var offset = 0 var offset = 0
function readSlice(n) { function writeSlice(slice) {
offset += n slice.copy(buffer, offset)
return buffer.slice(offset - n, offset) offset += slice.length
} }
function readUInt32() { function writeUInt32(i) {
var i = buffer.readUInt32LE(offset) buffer.writeUInt32LE(i, offset)
offset += 4 offset += 4
return i
} }
function readUInt64() { function writeUInt64(i) {
var i = bufferutils.readUInt64LE(buffer, offset) bufferutils.writeUInt64LE(buffer, i, offset)
offset += 8 offset += 8
return i
} }
function readVarInt() { function writeVarInt(i) {
var vi = bufferutils.readVarInt(buffer, offset) var n = bufferutils.writeVarInt(buffer, i, offset)
offset += vi.size offset += n
return vi.number
} }
function readScript() { writeUInt32(this.version)
return Script.fromBuffer(readSlice(readVarInt())) writeVarInt(this.ins.length)
}
var tx = new Transaction() this.ins.forEach(function(txin) {
tx.version = readUInt32() writeSlice(txin.hash)
writeUInt32(txin.index)
writeVarInt(txin.script.buffer.length)
writeSlice(txin.script.buffer)
writeUInt32(txin.sequence)
})
var vinLen = readVarInt() writeVarInt(this.outs.length)
for (var i = 0; i < vinLen; ++i) { this.outs.forEach(function(txout) {
tx.ins.push({ writeUInt64(txout.value)
hash: readSlice(32), writeVarInt(txout.script.buffer.length)
index: readUInt32(), writeSlice(txout.script.buffer)
script: readScript(), })
sequence: readUInt32()
})
}
var voutLen = readVarInt() writeUInt32(this.locktime)
for (i = 0; i < voutLen; ++i) {
tx.outs.push({
value: readUInt64(),
script: readScript(),
})
}
tx.locktime = readUInt32() return buffer
assert.equal(offset, buffer.length, 'Transaction has unexpected data')
return tx
} }
Transaction.fromHex = function(hex) { Transaction.prototype.toHex = function() {
return Transaction.fromBuffer(new Buffer(hex, 'hex')) return this.toBuffer().toString('hex')
} }
Transaction.prototype.setInputScript = function(index, script) { Transaction.prototype.setInputScript = function(index, script) {