bitcoinjs-lib/src/transaction.js

347 lines
9 KiB
JavaScript
Raw Normal View History

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')
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 ECSignature = require('./ecsignature')
var Script = require('./script')
2014-06-04 06:07:29 +02:00
Transaction.DEFAULT_SEQUENCE = 0xffffffff
Transaction.SIGHASH_ALL = 0x01
Transaction.SIGHASH_NONE = 0x02
Transaction.SIGHASH_SINGLE = 0x03
Transaction.SIGHASH_ANYONECANPAY = 0x80
function Transaction() {
2014-03-31 05:47:47 +02:00
this.version = 1
this.locktime = 0
this.ins = []
this.outs = []
}
/**
* Create a new txin.
*
2013-10-21 20:07:38 +02:00
* Can be called with any of:
*
* - 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, index, sequence) {
if (sequence == undefined) sequence = Transaction.DEFAULT_SEQUENCE
var hash
if (typeof tx === 'string') {
hash = new Buffer(tx, 'hex')
// TxId hex is big-endian, we need little-endian
Array.prototype.reverse.call(hash)
} else if (tx instanceof Transaction) {
hash = tx.getHash()
} else {
hash = tx
}
assert(Buffer.isBuffer(hash), 'Expected Transaction, txId or txHash, got ' + tx)
assert.equal(hash.length, 32, 'Expected hash length of 32, got ' + hash.length)
assert.equal(typeof index, 'number', 'Expected number index, got ' + index)
// Add the input and return the input's index
2014-06-15 07:28:20 +02:00
return (this.ins.push({
2014-06-16 06:08:43 +02:00
hash: hash,
index: index,
2014-06-15 07:28:20 +02:00
script: Script.EMPTY,
sequence: sequence
2014-06-15 07:28:20 +02:00
}) - 1)
2014-03-31 05:47:47 +02:00
}
/**
* Create a new txout.
*
* Can be called with:
*
* - A base58 address string and a value
* - An Address object and a value
* - A scriptPubKey Script and a value
*/
Transaction.prototype.addOutput = function(scriptPubKey, value) {
// Attempt to get a valid address if it's a base58 address string
if (typeof scriptPubKey === 'string') {
scriptPubKey = Address.fromBase58Check(scriptPubKey)
}
// Attempt to get a valid script if it's an Address object
if (scriptPubKey instanceof Address) {
scriptPubKey = scriptPubKey.toOutputScript()
}
assert(scriptPubKey instanceof Script, 'Expected Address or Script, got ' + scriptPubKey)
assert.equal(typeof value, 'number', 'Expected number value, got ' + value)
// Add the output and return the output's index
2014-06-15 07:28:20 +02:00
return (this.outs.push({
script: scriptPubKey,
value: value
2014-06-15 07:28:20 +02:00
}) - 1)
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-15 17:27:05 +02:00
this.ins.forEach(function(txin) {
2014-06-16 06:08:43 +02:00
writeSlice(txin.hash)
writeUInt32(txin.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)
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(inIndex, prevOutScript, hashType) {
// FIXME: remove in 2.x.y
if (arguments[0] instanceof Script) {
console.warn('hashForSignature(prevOutScript, inIndex, ...) has been deprecated. Use hashForSignature(inIndex, prevOutScript, ...)')
// swap the arguments (must be stored in tmp, arguments is special)
var tmp = arguments[0]
inIndex = arguments[1]
prevOutScript = tmp
}
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 === Transaction.SIGHASH_NONE) {
assert(false, 'SIGHASH_NONE not yet supported')
} else if (hashTypeModifier === Transaction.SIGHASH_SINGLE) {
assert(false, 'SIGHASH_SINGLE not yet supported')
2014-03-21 03:15:15 +01:00
}
if (hashType & Transaction.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
}
Transaction.prototype.getHash = function () {
return crypto.hash256(this.toBuffer())
}
Transaction.prototype.getId = function () {
var buffer = this.getHash()
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
newTx.ins = this.ins.map(function(txin) {
2014-06-15 07:28:20 +02:00
return {
2014-06-16 06:08:43 +02:00
hash: txin.hash,
index: txin.index,
2014-06-15 07:28:20 +02:00
script: txin.script,
sequence: txin.sequence
}
2014-03-31 05:47:47 +02:00
})
newTx.outs = this.outs.map(function(txout) {
2014-06-15 07:28:20 +02:00
return {
script: txout.script,
value: txout.value
}
2014-03-31 05:47:47 +02:00
})
2014-03-31 05:47:47 +02:00
return newTx
}
Transaction.fromBuffer = function(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 tx = new Transaction()
tx.version = readUInt32()
2014-05-04 23:44:51 +02:00
var vinLen = readVarInt()
for (var i = 0; i < vinLen; ++i) {
var hash = readSlice(32)
var vout = readUInt32()
2014-05-04 23:44:51 +02:00
var scriptLen = readVarInt()
var script = readSlice(scriptLen)
var sequence = readUInt32()
2014-06-15 07:28:20 +02:00
tx.ins.push({
2014-06-16 06:08:43 +02:00
hash: hash,
index: vout,
script: Script.fromBuffer(script),
sequence: sequence
2014-06-15 07:28:20 +02:00
})
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)
2014-06-15 07:28:20 +02:00
tx.outs.push({
value: value,
script: Script.fromBuffer(script)
2014-06-15 07:28:20 +02:00
})
2014-03-31 05:47:47 +02:00
}
2014-03-23 20:02:31 +01:00
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'))
}
Transaction.prototype.setInputScript = function(index, script) {
this.ins[index].script = script
}
// FIXME: remove in 2.x.y
Transaction.prototype.sign = function(index, privKey, hashType) {
console.warn("Transaction.prototype.sign is deprecated. Use TransactionBuilder instead.")
var prevOutScript = privKey.pub.getAddress().toOutputScript()
var signature = this.signInput(index, prevOutScript, privKey, hashType)
var scriptSig = scripts.pubKeyHashInput(signature, privKey.pub)
this.setInputScript(index, scriptSig)
}
// FIXME: remove in 2.x.y
Transaction.prototype.signInput = function(index, prevOutScript, privKey, hashType) {
console.warn("Transaction.prototype.signInput is deprecated. Use TransactionBuilder instead.")
hashType = hashType || Transaction.SIGHASH_ALL
var hash = this.hashForSignature(index, prevOutScript, hashType)
var signature = privKey.sign(hash)
2014-05-10 14:38:05 +02:00
return signature.toScriptSignature(hashType)
}
// FIXME: remove in 2.x.y
Transaction.prototype.validateInput = function(index, prevOutScript, pubKey, buffer) {
console.warn("Transaction.prototype.validateInput is deprecated. Use TransactionBuilder instead.")
var parsed = ECSignature.parseScriptSignature(buffer)
var hash = this.hashForSignature(index, prevOutScript, parsed.hashType)
2014-05-10 14:38:05 +02:00
return pubKey.verify(hash, parsed.signature)
}
2014-05-16 09:12:39 +02:00
module.exports = Transaction