bitcoinjs-lib/src/transaction.js

402 lines
9.9 KiB
JavaScript
Raw Normal View History

// FIXME: To all ye that enter here, be weary of Buffers, Arrays and Hex interchanging between the outpoints
2014-04-17 11:08:16 +02:00
var assert = require('assert')
2014-05-13 08:38:13 +02:00
var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
2014-04-08 19:58:55 +02:00
var ecdsa = require('./ecdsa')
var opcodes = require('./opcodes')
2014-06-13 01:58:52 +02:00
var scripts = require('./scripts')
2014-06-04 06:07:29 +02:00
var Address = require('./address')
var ECKey = require('./eckey')
var Script = require('./script')
2014-06-04 06:07:29 +02:00
var DEFAULT_SEQUENCE = 0xffffffff
2014-05-19 00:49:41 +02:00
var SIGHASH_ALL = 0x01
var SIGHASH_NONE = 0x02
var SIGHASH_SINGLE = 0x03
var SIGHASH_ANYONECANPAY = 0x80
2014-05-13 08:38:13 +02:00
function Transaction(doc) {
2014-03-31 05:47:47 +02:00
if (!(this instanceof Transaction)) { return new Transaction(doc) }
this.version = 1
this.locktime = 0
this.ins = []
this.outs = []
if (doc) {
if (doc.hash) this.hash = doc.hash;
if (doc.version) this.version = doc.version;
if (doc.locktime) this.locktime = doc.locktime;
if (doc.ins && doc.ins.length) {
this.ins = doc.ins.map(function(input) {
return new TransactionIn(input)
})
2014-03-31 05:47:47 +02:00
}
if (doc.outs && doc.outs.length) {
this.outs = doc.outs.map(function(output) {
return new TransactionOut(output)
})
2014-03-31 05:47:47 +02:00
}
this.hash = this.hash || this.getHash()
}
}
/**
* Create a new txin.
*
2013-10-21 20:07:38 +02:00
* Can be called with any of:
*
2014-03-17 10:49:37 +01:00
* - An existing TransactionIn object
2013-10-21 20:07:38 +02:00
* - A transaction and an index
* - A transaction hash and an index
*
* Note that this method does not sign the created input.
*/
Transaction.prototype.addInput = function (tx, outIndex) {
2014-03-31 05:47:47 +02:00
if (arguments[0] instanceof TransactionIn) {
this.ins.push(arguments[0])
return
2014-03-31 05:47:47 +02:00
}
var hash = typeof tx === "string" ? tx : tx.hash
this.ins.push(new TransactionIn({
outpoint: {
hash: hash,
index: outIndex
},
2014-06-12 04:48:04 +02:00
script: Script.EMPTY
}))
2014-03-31 05:47:47 +02:00
}
/**
* Create a new txout.
*
* Can be called with:
*
* i) An existing TransactionOut object
* ii) An address object or a string address, and a value
*
*/
Transaction.prototype.addOutput = function (address, value) {
2014-03-31 05:47:47 +02:00
if (arguments[0] instanceof TransactionOut) {
this.outs.push(arguments[0])
return
}
2014-03-23 20:01:33 +01:00
if (typeof address === 'string') {
address = Address.fromBase58Check(address)
}
2014-03-31 05:47:47 +02:00
this.outs.push(new TransactionOut({
value: value,
script: address.toOutputScript(),
address: address // TODO: Remove me
2014-03-31 05:47:47 +02:00
}))
}
Transaction.prototype.toBuffer = function () {
var txInSize = this.ins.reduce(function(a, x) {
2014-05-13 08:38:13 +02:00
return a + (40 + bufferutils.varIntSize(x.script.buffer.length) + x.script.buffer.length)
}, 0)
var txOutSize = this.outs.reduce(function(a, x) {
2014-05-13 08:38:13 +02:00
return a + (8 + bufferutils.varIntSize(x.script.buffer.length) + x.script.buffer.length)
}, 0)
var buffer = new Buffer(
8 +
2014-05-13 08:38:13 +02:00
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) {
2014-05-13 08:38:13 +02:00
bufferutils.writeUInt64LE(buffer, i, offset)
offset += 8
}
2014-05-04 23:44:51 +02:00
function writeVarInt(i) {
2014-05-13 08:38:13 +02:00
var n = bufferutils.writeVarInt(buffer, i, offset)
offset += n
}
2013-03-02 18:00:14 +01:00
writeUInt32(this.version)
2014-05-04 23:44:51 +02:00
writeVarInt(this.ins.length)
2013-03-02 18:00:14 +01:00
2014-06-04 06:07:29 +02:00
this.ins.forEach(function(txin) {
2014-05-04 00:28:31 +02:00
var hash = new Buffer(txin.outpoint.hash, 'hex') // FIXME: Performance: convert on tx.addInput instead
2014-03-23 20:01:33 +01:00
2014-05-02 23:55:47 +02:00
// TxHash hex is big-endian, we need little-endian
Array.prototype.reverse.call(hash)
writeSlice(hash)
writeUInt32(txin.outpoint.index)
2014-05-04 23:44:51 +02:00
writeVarInt(txin.script.buffer.length)
writeSlice(txin.script.buffer)
writeUInt32(txin.sequence)
})
2014-05-04 23:44:51 +02:00
writeVarInt(this.outs.length)
2014-03-31 05:47:47 +02:00
this.outs.forEach(function(txout) {
writeUInt64(txout.value)
2014-05-04 23:44:51 +02:00
writeVarInt(txout.script.buffer.length)
writeSlice(txout.script.buffer)
2014-03-31 05:47:47 +02:00
})
2014-03-23 20:01:33 +01:00
writeUInt32(this.locktime)
assert.equal(offset, buffer.length, 'Invalid transaction object')
return buffer
2014-03-31 05:47:47 +02:00
}
Transaction.prototype.toHex = function() {
return this.toBuffer().toString('hex')
}
/**
* 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, serializes and finally hashes the result. This hash can then be
* used to sign the transaction input in question.
*/
Transaction.prototype.hashForSignature = function(prevOutScript, inIndex, hashType) {
assert(inIndex >= 0, 'Invalid vin index')
assert(inIndex < this.ins.length, 'Invalid vin index')
assert(prevOutScript instanceof Script, 'Invalid Script object')
var txTmp = this.clone()
var hashScript = prevOutScript.without(opcodes.OP_CODESEPARATOR)
// Blank out other inputs' signatures
txTmp.ins.forEach(function(txin) {
2014-06-12 04:48:04 +02:00
txin.script = Script.EMPTY
2014-03-31 05:47:47 +02:00
})
txTmp.ins[inIndex].script = hashScript
var hashTypeModifier = hashType & 0x1f
if (hashTypeModifier === SIGHASH_NONE) {
assert(false, 'SIGHASH_NONE not yet supported')
} else if (hashTypeModifier === SIGHASH_SINGLE) {
assert(false, 'SIGHASH_SINGLE not yet supported')
2014-03-21 03:15:15 +01:00
}
if (hashType & SIGHASH_ANYONECANPAY) {
assert(false, 'SIGHASH_ANYONECANPAY not yet supported')
2014-03-21 03:15:15 +01:00
}
var hashTypeBuffer = new Buffer(4)
hashTypeBuffer.writeInt32LE(hashType, 0)
2014-03-23 20:01:33 +01:00
var buffer = Buffer.concat([txTmp.toBuffer(), hashTypeBuffer])
return crypto.hash256(buffer)
2014-03-31 05:47:47 +02:00
}
2014-05-02 23:55:47 +02:00
Transaction.prototype.getHash = function () {
var buffer = crypto.hash256(this.toBuffer())
2014-05-02 23:55:47 +02:00
// Big-endian is used for TxHash
Array.prototype.reverse.call(buffer)
return buffer.toString('hex')
2014-03-31 05:47:47 +02:00
}
Transaction.prototype.clone = function () {
2014-03-31 05:47:47 +02:00
var newTx = new Transaction()
newTx.version = this.version
newTx.locktime = this.locktime
this.ins.forEach(function(txin) {
2014-03-31 05:47:47 +02:00
newTx.addInput(txin.clone())
})
this.outs.forEach(function(txout) {
2014-03-31 05:47:47 +02:00
newTx.addOutput(txout.clone())
})
2014-03-31 05:47:47 +02:00
return newTx
}
Transaction.fromBuffer = function(buffer) {
// Copy because we mutate (reverse TxOutHashs)
buffer = new Buffer(buffer)
var offset = 0
function readSlice(n) {
offset += n
return buffer.slice(offset - n, offset)
2014-03-31 05:47:47 +02:00
}
function readUInt32() {
var i = buffer.readUInt32LE(offset)
offset += 4
return i
2014-03-31 05:47:47 +02:00
}
function readUInt64() {
2014-05-13 08:38:13 +02:00
var i = bufferutils.readUInt64LE(buffer, offset)
offset += 8
return i
2014-03-31 05:47:47 +02:00
}
2014-05-04 23:44:51 +02:00
function readVarInt() {
2014-05-13 08:38:13 +02:00
var vi = bufferutils.readVarInt(buffer, offset)
offset += vi.size
return vi.number
2014-03-31 05:47:47 +02:00
}
var ins = []
var outs = []
var version = readUInt32()
2014-05-04 23:44:51 +02:00
var vinLen = readVarInt()
for (var i = 0; i < vinLen; ++i) {
var hash = readSlice(32)
2014-05-02 23:55:47 +02:00
// TxHash is little-endian, we want big-endian hex
2014-05-01 22:25:57 +02:00
Array.prototype.reverse.call(hash)
var vout = readUInt32()
2014-05-04 23:44:51 +02:00
var scriptLen = readVarInt()
var script = readSlice(scriptLen)
var sequence = readUInt32()
ins.push(new TransactionIn({
2014-03-31 05:47:47 +02:00
outpoint: {
hash: hash.toString('hex'),
index: vout
2014-03-31 05:47:47 +02:00
},
script: Script.fromBuffer(script),
sequence: sequence
}))
2014-03-31 05:47:47 +02:00
}
2014-03-23 20:02:31 +01:00
2014-05-04 23:44:51 +02:00
var voutLen = readVarInt()
for (i = 0; i < voutLen; ++i) {
var value = readUInt64()
2014-05-04 23:44:51 +02:00
var scriptLen = readVarInt()
var script = readSlice(scriptLen)
outs.push(new TransactionOut({
value: value,
script: Script.fromBuffer(script)
}))
2014-03-31 05:47:47 +02:00
}
2014-03-23 20:02:31 +01:00
var locktime = readUInt32()
assert.equal(offset, buffer.length, 'Invalid transaction')
return new Transaction({
version: version,
ins: ins,
outs: outs,
locktime: locktime
})
}
Transaction.fromHex = function(hex) {
return Transaction.fromBuffer(new Buffer(hex, 'hex'))
}
/**
* Signs a pubKeyHash output at some index with the given key
*/
Transaction.prototype.sign = function(index, key, type) {
var prevOutScript = key.pub.getAddress().toOutputScript()
var signature = this.signInput(index, prevOutScript, key, type)
// FIXME: Assumed prior TX was pay-to-pubkey-hash
2014-06-13 01:58:52 +02:00
var scriptSig = scripts.pubKeyHashInput(signature, key.pub)
this.setInputScript(index, scriptSig)
}
Transaction.prototype.signInput = function(index, prevOutScript, key, type) {
2014-03-31 05:47:47 +02:00
type = type || SIGHASH_ALL
assert(key instanceof ECKey, 'Invalid private key')
var hash = this.hashForSignature(prevOutScript, index, type)
2014-05-24 08:25:38 +02:00
var signature = key.sign(hash)
var DERencoded = ecdsa.serializeSig(signature)
2014-05-10 14:38:05 +02:00
return Buffer.concat([
2014-05-24 08:25:38 +02:00
new Buffer(DERencoded),
2014-05-10 14:38:05 +02:00
new Buffer([type])
])
}
Transaction.prototype.setInputScript = function(index, script) {
this.ins[index].script = script
2013-11-02 11:20:09 +01:00
}
// FIXME: should probably be validateInput(index, pub)
Transaction.prototype.validateInput = function(index, script, pub, DERsig) {
2014-05-10 14:38:05 +02:00
var type = DERsig.readUInt8(DERsig.length - 1)
DERsig = DERsig.slice(0, -1)
var hash = this.hashForSignature(script, index, type)
2014-05-10 14:38:05 +02:00
var sig = ecdsa.parseSig(DERsig)
return pub.verify(hash, sig)
}
2014-03-22 16:48:21 +01:00
Transaction.feePerKb = 20000
Transaction.prototype.estimateFee = function(feePerKb){
var uncompressedInSize = 180
var outSize = 34
var fixedPadding = 34
2014-03-26 01:37:07 +01:00
2014-03-31 05:47:47 +02:00
if(feePerKb == undefined) feePerKb = Transaction.feePerKb;
var size = this.ins.length * uncompressedInSize + this.outs.length * outSize + fixedPadding
2014-03-22 16:48:21 +01:00
return feePerKb * Math.ceil(size / 1000)
2014-03-22 16:48:21 +01:00
}
function TransactionIn(data) {
assert(data.outpoint && data.script, 'Invalid TxIn parameters')
this.outpoint = data.outpoint
this.script = data.script
this.sequence = data.sequence == undefined ? DEFAULT_SEQUENCE : data.sequence
2014-03-31 05:47:47 +02:00
}
TransactionIn.prototype.clone = function () {
2014-03-31 05:47:47 +02:00
return new TransactionIn({
outpoint: {
hash: this.outpoint.hash,
index: this.outpoint.index
},
2014-06-12 08:45:53 +02:00
script: this.script,
2014-03-31 05:47:47 +02:00
sequence: this.sequence
})
}
function TransactionOut(data) {
this.script = data.script
this.value = data.value
this.address = data.address
2014-03-31 05:47:47 +02:00
}
2014-03-23 14:34:52 +01:00
TransactionOut.prototype.clone = function() {
return new TransactionOut({
2014-06-12 08:45:53 +02:00
script: this.script,
value: this.value,
address: this.address
2014-03-31 05:47:47 +02:00
})
2014-03-23 14:34:52 +01:00
}
2014-05-16 09:12:39 +02:00
module.exports = Transaction