2014-04-17 11:08:16 +02:00
|
|
|
var assert = require('assert')
|
2014-05-13 08:38:13 +02:00
|
|
|
var bufferutils = require('./bufferutils')
|
2014-04-08 14:13:03 +02:00
|
|
|
var crypto = require('./crypto')
|
2014-04-08 19:58:55 +02:00
|
|
|
var ecdsa = require('./ecdsa')
|
2014-05-28 17:20:13 +02:00
|
|
|
var opcodes = require('./opcodes')
|
2014-06-13 01:58:52 +02:00
|
|
|
var scripts = require('./scripts')
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-06-04 06:07:29 +02:00
|
|
|
var Address = require('./address')
|
|
|
|
var ECKey = require('./eckey')
|
2014-06-12 13:14:22 +02:00
|
|
|
var Script = require('./script')
|
2014-06-04 06:07:29 +02:00
|
|
|
|
2014-05-28 07:13:45 +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-28 07:13:45 +02:00
|
|
|
|
2014-05-21 03:41:25 +02:00
|
|
|
function Transaction() {
|
2014-03-31 05:47:47 +02:00
|
|
|
this.version = 1
|
|
|
|
this.locktime = 0
|
|
|
|
this.ins = []
|
|
|
|
this.outs = []
|
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2013-02-17 06:39:15 +01:00
|
|
|
*
|
|
|
|
* Note that this method does not sign the created input.
|
|
|
|
*/
|
2014-06-13 08:44:02 +02:00
|
|
|
Transaction.prototype.addInput = function(tx, index) {
|
2014-05-20 06:07:22 +02:00
|
|
|
var hash
|
|
|
|
|
|
|
|
if (typeof tx === 'string') {
|
2014-06-14 16:03:17 +02:00
|
|
|
hash = new Buffer(tx, 'hex')
|
2014-06-16 07:44:27 +02:00
|
|
|
assert.equal(hash.length, 32, 'Expected Transaction or string, got ' + tx)
|
2014-06-14 16:03:17 +02:00
|
|
|
|
|
|
|
// TxHash hex is big-endian, we need little-endian
|
|
|
|
Array.prototype.reverse.call(hash)
|
2014-05-20 06:07:22 +02:00
|
|
|
|
|
|
|
} else {
|
2014-06-16 07:44:27 +02:00
|
|
|
assert(tx instanceof Transaction, 'Expected Transaction or string, got ' + tx)
|
2014-06-14 16:03:17 +02:00
|
|
|
hash = crypto.hash256(tx.toBuffer())
|
|
|
|
|
2014-05-20 06:07:22 +02:00
|
|
|
}
|
2014-05-08 00:48:09 +02:00
|
|
|
|
2014-06-13 08:44:02 +02:00
|
|
|
assert.equal(typeof index, 'number', 'Expected number index, got ' + 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: DEFAULT_SEQUENCE
|
|
|
|
}) - 1)
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new txout.
|
|
|
|
*
|
2013-11-02 11:51:27 +01:00
|
|
|
* Can be called with:
|
|
|
|
*
|
2014-05-19 06:14:07 +02:00
|
|
|
* - A base58 address string and a value
|
2014-05-19 06:55:54 +02:00
|
|
|
* - An Address object and a value
|
|
|
|
* - A scriptPubKey Script and a value
|
2013-02-17 06:39:15 +01:00
|
|
|
*/
|
2014-05-19 06:55:54 +02:00
|
|
|
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) {
|
|
|
|
var address = scriptPubKey
|
|
|
|
|
|
|
|
scriptPubKey = address.toOutputScript()
|
2014-04-19 20:44:30 +02:00
|
|
|
}
|
2014-04-17 15:31:45 +02:00
|
|
|
|
2014-06-15 07:28:20 +02:00
|
|
|
return (this.outs.push({
|
2014-05-19 06:55:54 +02:00
|
|
|
script: scriptPubKey,
|
2014-03-31 05:47:47 +02:00
|
|
|
value: value,
|
2014-06-15 07:28:20 +02:00
|
|
|
}) - 1)
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-05-08 02:44:35 +02:00
|
|
|
Transaction.prototype.toBuffer = function () {
|
2014-05-01 22:36:21 +02:00
|
|
|
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)
|
2014-05-01 22:36:21 +02:00
|
|
|
}, 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)
|
2014-05-01 22:36:21 +02:00
|
|
|
}, 0)
|
|
|
|
|
|
|
|
var buffer = new Buffer(
|
|
|
|
8 +
|
2014-05-13 08:38:13 +02:00
|
|
|
bufferutils.varIntSize(this.ins.length) +
|
|
|
|
bufferutils.varIntSize(this.outs.length) +
|
2014-05-01 22:36:21 +02:00
|
|
|
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)
|
2014-05-01 22:36:21 +02:00
|
|
|
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)
|
2014-05-01 22:36:21 +02:00
|
|
|
offset += n
|
|
|
|
}
|
2013-03-02 18:00:14 +01:00
|
|
|
|
2014-05-01 22:36:21 +02: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)
|
2014-05-01 22:36:21 +02:00
|
|
|
writeSlice(txin.script.buffer)
|
|
|
|
writeUInt32(txin.sequence)
|
|
|
|
})
|
2014-03-23 20:03:58 +01:00
|
|
|
|
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) {
|
2014-05-01 22:36:21 +02:00
|
|
|
writeUInt64(txout.value)
|
2014-05-04 23:44:51 +02:00
|
|
|
writeVarInt(txout.script.buffer.length)
|
2014-05-01 22:36:21 +02:00
|
|
|
writeSlice(txout.script.buffer)
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-03-23 20:01:33 +01:00
|
|
|
|
2014-05-01 22:36:21 +02:00
|
|
|
writeUInt32(this.locktime)
|
|
|
|
assert.equal(offset, buffer.length, 'Invalid transaction object')
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-05-01 22:36:21 +02:00
|
|
|
return buffer
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-05-08 02:44:35 +02:00
|
|
|
Transaction.prototype.toHex = function() {
|
|
|
|
return this.toBuffer().toString('hex')
|
2013-11-20 19:00:49 +01:00
|
|
|
}
|
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-06-13 03:30:07 +02:00
|
|
|
Transaction.prototype.hashForSignature = function(prevOutScript, inIndex, hashType) {
|
2014-05-28 17:20:13 +02:00
|
|
|
assert(inIndex >= 0, 'Invalid vin index')
|
|
|
|
assert(inIndex < this.ins.length, 'Invalid vin index')
|
2014-06-13 03:30:07 +02:00
|
|
|
assert(prevOutScript instanceof Script, 'Invalid Script object')
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-05-28 17:20:13 +02:00
|
|
|
var txTmp = this.clone()
|
2014-06-13 03:30:07 +02:00
|
|
|
var hashScript = prevOutScript.without(opcodes.OP_CODESEPARATOR)
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
|
|
// Blank out other inputs' signatures
|
2014-03-23 20:03:58 +01:00
|
|
|
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
|
|
|
})
|
2014-05-28 17:20:13 +02:00
|
|
|
txTmp.ins[inIndex].script = hashScript
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-05-28 17:20:13 +02:00
|
|
|
var hashTypeModifier = hashType & 0x1f
|
|
|
|
if (hashTypeModifier === SIGHASH_NONE) {
|
|
|
|
assert(false, 'SIGHASH_NONE not yet supported')
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-05-28 17:20:13 +02:00
|
|
|
} else if (hashTypeModifier === SIGHASH_SINGLE) {
|
|
|
|
assert(false, 'SIGHASH_SINGLE not yet supported')
|
2014-03-21 03:15:15 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hashType & SIGHASH_ANYONECANPAY) {
|
2014-05-28 17:20:13 +02:00
|
|
|
assert(false, 'SIGHASH_ANYONECANPAY not yet supported')
|
2014-03-21 03:15:15 +01:00
|
|
|
}
|
|
|
|
|
2014-05-28 17:20:13 +02:00
|
|
|
var hashTypeBuffer = new Buffer(4)
|
|
|
|
hashTypeBuffer.writeInt32LE(hashType, 0)
|
2014-03-23 20:01:33 +01:00
|
|
|
|
2014-05-28 17:20:13 +02:00
|
|
|
var buffer = Buffer.concat([txTmp.toBuffer(), hashTypeBuffer])
|
2014-04-08 14:13:03 +02:00
|
|
|
return crypto.hash256(buffer)
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-05-20 06:07:22 +02:00
|
|
|
Transaction.prototype.getId = function () {
|
2014-05-08 02:44:35 +02:00
|
|
|
var buffer = crypto.hash256(this.toBuffer())
|
2014-04-08 14:13:03 +02:00
|
|
|
|
2014-05-02 23:55:47 +02:00
|
|
|
// Big-endian is used for TxHash
|
2014-05-01 22:36:21 +02:00
|
|
|
Array.prototype.reverse.call(buffer)
|
|
|
|
|
|
|
|
return buffer.toString('hex')
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-06-13 03:30:07 +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
|
2014-03-23 20:03:58 +01:00
|
|
|
|
2014-05-19 06:14:07 +02:00
|
|
|
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
|
|
|
})
|
2014-03-23 20:03:58 +01:00
|
|
|
|
2014-05-19 06:14:07 +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
|
|
|
})
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
return newTx
|
|
|
|
}
|
2013-10-07 21:27:19 +02:00
|
|
|
|
2014-05-08 02:44:35 +02:00
|
|
|
Transaction.fromBuffer = function(buffer) {
|
2014-05-01 22:36:21 +02:00
|
|
|
var offset = 0
|
|
|
|
function readSlice(n) {
|
|
|
|
offset += n
|
|
|
|
return buffer.slice(offset - n, offset)
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2014-05-01 22:36:21 +02:00
|
|
|
function readUInt32() {
|
|
|
|
var i = buffer.readUInt32LE(offset)
|
|
|
|
offset += 4
|
|
|
|
return i
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2014-05-01 22:36:21 +02:00
|
|
|
function readUInt64() {
|
2014-05-13 08:38:13 +02:00
|
|
|
var i = bufferutils.readUInt64LE(buffer, offset)
|
2014-05-01 22:36:21 +02:00
|
|
|
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)
|
2014-05-01 22:36:21 +02:00
|
|
|
offset += vi.size
|
|
|
|
return vi.number
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
|
|
|
|
2014-05-21 03:41:25 +02:00
|
|
|
var tx = new Transaction()
|
|
|
|
tx.version = readUInt32()
|
2014-05-01 22:36:21 +02:00
|
|
|
|
2014-05-04 23:44:51 +02:00
|
|
|
var vinLen = readVarInt()
|
2014-05-01 22:36:21 +02:00
|
|
|
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()
|
2014-05-01 22:36:21 +02:00
|
|
|
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,
|
2014-05-01 22:36:21 +02:00
|
|
|
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()
|
2014-05-01 22:36:21 +02:00
|
|
|
for (i = 0; i < voutLen; ++i) {
|
|
|
|
var value = readUInt64()
|
2014-05-04 23:44:51 +02:00
|
|
|
var scriptLen = readVarInt()
|
2014-05-01 22:36:21 +02:00
|
|
|
var script = readSlice(scriptLen)
|
|
|
|
|
2014-06-15 07:28:20 +02:00
|
|
|
tx.outs.push({
|
2014-05-01 22:36:21 +02:00
|
|
|
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
|
|
|
|
2014-05-21 03:41:25 +02:00
|
|
|
tx.locktime = readUInt32()
|
2014-05-01 22:36:21 +02:00
|
|
|
assert.equal(offset, buffer.length, 'Invalid transaction')
|
2014-03-09 06:46:20 +01:00
|
|
|
|
2014-05-21 03:41:25 +02:00
|
|
|
return tx
|
2013-10-07 14:21:00 +02:00
|
|
|
}
|
|
|
|
|
2014-05-08 02:44:35 +02:00
|
|
|
Transaction.fromHex = function(hex) {
|
|
|
|
return Transaction.fromBuffer(new Buffer(hex, 'hex'))
|
|
|
|
}
|
|
|
|
|
2013-10-07 21:27:19 +02:00
|
|
|
/**
|
2014-06-13 03:30:07 +02:00
|
|
|
* Signs a pubKeyHash output at some index with the given key
|
2013-10-07 21:27:19 +02:00
|
|
|
*/
|
2014-06-14 17:08:52 +02:00
|
|
|
Transaction.prototype.sign = function(index, privKey, hashType) {
|
|
|
|
var prevOutScript = privKey.pub.getAddress().toOutputScript()
|
|
|
|
var signature = this.signInput(index, prevOutScript, privKey, hashType)
|
2014-04-08 14:13:03 +02:00
|
|
|
|
2014-05-06 08:52:31 +02:00
|
|
|
// FIXME: Assumed prior TX was pay-to-pubkey-hash
|
2014-06-14 17:08:52 +02:00
|
|
|
var scriptSig = scripts.pubKeyHashInput(signature, privKey.pub)
|
2014-06-13 03:30:07 +02:00
|
|
|
this.setInputScript(index, scriptSig)
|
2013-10-07 21:27:19 +02:00
|
|
|
}
|
|
|
|
|
2014-06-14 17:08:52 +02:00
|
|
|
Transaction.prototype.signInput = function(index, prevOutScript, privKey, hashType) {
|
2014-06-13 08:25:41 +02:00
|
|
|
hashType = hashType || SIGHASH_ALL
|
2014-06-14 17:08:52 +02:00
|
|
|
assert(privKey instanceof ECKey, 'Expected ECKey, got ' + privKey)
|
2014-04-25 19:51:01 +02:00
|
|
|
|
2014-06-13 08:25:41 +02:00
|
|
|
var hash = this.hashForSignature(prevOutScript, index, hashType)
|
2014-06-14 17:08:52 +02:00
|
|
|
var signature = privKey.sign(hash)
|
2014-05-24 08:25:38 +02:00
|
|
|
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-06-13 08:25:41 +02:00
|
|
|
new Buffer([hashType])
|
2014-05-10 14:38:05 +02:00
|
|
|
])
|
2013-10-07 21:27:19 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 03:30:07 +02:00
|
|
|
Transaction.prototype.setInputScript = function(index, script) {
|
2014-04-22 21:31:24 +02:00
|
|
|
this.ins[index].script = script
|
2013-11-02 11:20:09 +01:00
|
|
|
}
|
|
|
|
|
2014-05-24 07:29:23 +02:00
|
|
|
// FIXME: could be validateInput(index, prevTxOut, pub)
|
|
|
|
Transaction.prototype.validateInput = function(index, prevOutScript, pubKey, DERsig) {
|
2014-05-10 14:38:05 +02:00
|
|
|
var type = DERsig.readUInt8(DERsig.length - 1)
|
|
|
|
DERsig = DERsig.slice(0, -1)
|
|
|
|
|
2014-05-24 07:29:23 +02:00
|
|
|
var hash = this.hashForSignature(prevOutScript, index, type)
|
|
|
|
var signature = ecdsa.parseSig(DERsig)
|
2014-04-22 21:33:11 +02:00
|
|
|
|
2014-05-24 07:29:23 +02:00
|
|
|
return pubKey.verify(hash, signature)
|
2013-10-08 08:55:52 +02:00
|
|
|
}
|
2013-10-07 21:27:19 +02:00
|
|
|
|
2014-05-16 09:12:39 +02:00
|
|
|
module.exports = Transaction
|