2014-04-08 14:13:03 +02:00
|
|
|
// 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-04-08 14:13:03 +02:00
|
|
|
var Address = require('./address')
|
2014-05-03 04:04:54 +02:00
|
|
|
var BigInteger = require('bigi')
|
2014-05-13 08:38:13 +02:00
|
|
|
var bufferutils = require('./bufferutils')
|
2014-03-31 05:47:47 +02:00
|
|
|
var Script = require('./script')
|
|
|
|
var convert = require('./convert')
|
2014-04-08 14:13:03 +02:00
|
|
|
var crypto = require('./crypto')
|
2014-05-13 08:44:29 +02:00
|
|
|
var ECKey = require('./eckey')
|
2014-04-08 19:58:55 +02:00
|
|
|
var ecdsa = require('./ecdsa')
|
2013-02-17 06:39:15 +01:00
|
|
|
|
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 = []
|
2014-05-01 22:08:45 +02:00
|
|
|
this.defaultSequence = 0xffffffff
|
2014-03-31 05:47:47 +02:00
|
|
|
|
|
|
|
if (doc) {
|
|
|
|
if (typeof doc == "string" || Array.isArray(doc)) {
|
2014-05-08 02:44:35 +02:00
|
|
|
doc = Transaction.fromBuffer(doc)
|
2012-01-11 02:40:45 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
|
|
|
|
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) {
|
|
|
|
doc.ins.forEach(function(input) {
|
|
|
|
this.addInput(new TransactionIn(input))
|
|
|
|
}, this)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc.outs && doc.outs.length) {
|
|
|
|
doc.outs.forEach(function(output) {
|
|
|
|
this.addOutput(new TransactionOut(output))
|
|
|
|
}, this)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.hash = this.hash || this.getHash()
|
|
|
|
}
|
|
|
|
}
|
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:
|
|
|
|
*
|
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
|
|
|
|
* - A single string argument of the form txhash:index
|
2013-02-17 06:39:15 +01:00
|
|
|
*
|
|
|
|
* 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])
|
2014-05-08 00:48:09 +02:00
|
|
|
return
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2014-05-08 00:48:09 +02:00
|
|
|
|
|
|
|
var hash
|
|
|
|
if (arguments[0].length > 65) {
|
2014-03-31 05:47:47 +02:00
|
|
|
var args = arguments[0].split(':')
|
2014-05-08 00:48:09 +02:00
|
|
|
hash = args[0]
|
|
|
|
outIndex = parseInt(args[1])
|
2014-03-31 05:47:47 +02:00
|
|
|
|
2014-05-08 00:48:09 +02:00
|
|
|
} else {
|
|
|
|
hash = typeof tx === "string" ? tx : tx.hash
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2014-05-08 00:48:09 +02:00
|
|
|
|
|
|
|
this.ins.push(new TransactionIn({
|
|
|
|
outpoint: {
|
|
|
|
hash: hash,
|
|
|
|
index: outIndex
|
|
|
|
},
|
|
|
|
script: new Script(),
|
|
|
|
sequence: this.defaultSequence
|
|
|
|
}))
|
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:
|
|
|
|
*
|
|
|
|
* i) An existing TransactionOut object
|
2014-05-05 13:17:34 +02:00
|
|
|
* ii) An address object or a string address, and a value
|
2013-11-02 11:51:27 +01:00
|
|
|
* iii) An address:value string
|
|
|
|
*
|
2014-04-19 20:54:50 +02:00
|
|
|
* FIXME: This is a bit convoluted
|
2013-02-17 06:39:15 +01:00
|
|
|
*/
|
2014-05-06 08:52:31 +02:00
|
|
|
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
|
|
|
|
2014-05-05 07:48:44 +02:00
|
|
|
if (typeof address === 'string') {
|
|
|
|
if (arguments[0].indexOf(':') >= 0) {
|
|
|
|
var args = arguments[0].split(':')
|
|
|
|
address = args[0]
|
|
|
|
value = parseInt(args[1])
|
|
|
|
}
|
2014-04-19 20:44:30 +02:00
|
|
|
|
|
|
|
address = Address.fromBase58Check(address)
|
|
|
|
}
|
2014-04-17 15:31:45 +02:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
this.outs.push(new TransactionOut({
|
|
|
|
value: value,
|
2014-05-06 08:52:31 +02:00
|
|
|
script: address.toScriptPubKey(),
|
|
|
|
address: address // TODO: Remove me
|
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) {
|
2014-05-04 00:28:31 +02:00
|
|
|
if (Array.isArray(slice)) slice = new Buffer(slice) // FIXME: Performance: transitionary only
|
2014-05-01 22:36:21 +02:00
|
|
|
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-05-01 22:36:21 +02:00
|
|
|
this.ins.forEach(function(txin, i) {
|
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
|
2014-05-01 22:36:21 +02:00
|
|
|
Array.prototype.reverse.call(hash)
|
2014-03-23 20:03:58 +01:00
|
|
|
|
2014-05-01 22:36:21 +02:00
|
|
|
writeSlice(hash)
|
|
|
|
writeUInt32(txin.outpoint.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
|
|
|
}
|
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
//var OP_CODESEPARATOR = 171
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var SIGHASH_ALL = 1
|
|
|
|
var SIGHASH_NONE = 2
|
|
|
|
var SIGHASH_SINGLE = 3
|
|
|
|
var SIGHASH_ANYONECANPAY = 80
|
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-05-04 07:03:24 +02:00
|
|
|
Transaction.prototype.hashForSignature =
|
2014-03-31 05:47:47 +02:00
|
|
|
function (connectedScript, inIndex, hashType)
|
2013-02-17 06:39:15 +01:00
|
|
|
{
|
2014-03-31 05:47:47 +02:00
|
|
|
var txTmp = this.clone()
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
|
|
// In case concatenating two scripts ends up with two codeseparators,
|
|
|
|
// or an extra one at the end, this prevents all those possible
|
|
|
|
// incompatibilities.
|
|
|
|
/*scriptCode = scriptCode.filter(function (val) {
|
2014-03-31 05:47:47 +02:00
|
|
|
return val !== 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-03-31 05:47:47 +02:00
|
|
|
txin.script = new Script()
|
|
|
|
})
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
txTmp.ins[inIndex].script = connectedScript
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-21 03:15:15 +01:00
|
|
|
// Blank out some of the outputs
|
|
|
|
if ((hashType & 0x1f) == SIGHASH_NONE) {
|
2014-03-31 05:47:47 +02:00
|
|
|
txTmp.outs = []
|
2014-03-21 03:15:15 +01:00
|
|
|
|
|
|
|
// Let the others update at will
|
2014-03-23 20:03:58 +01:00
|
|
|
txTmp.ins.forEach(function(txin, i) {
|
|
|
|
if (i != inIndex) {
|
2014-03-31 05:47:47 +02:00
|
|
|
txTmp.ins[i].sequence = 0
|
2014-03-23 20:03:58 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-03-23 20:03:58 +01:00
|
|
|
|
2014-03-21 03:15:15 +01:00
|
|
|
} else if ((hashType & 0x1f) == SIGHASH_SINGLE) {
|
|
|
|
// TODO: Implement
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blank out other inputs completely, not recommended for open transactions
|
|
|
|
if (hashType & SIGHASH_ANYONECANPAY) {
|
2014-03-31 05:47:47 +02:00
|
|
|
txTmp.ins = [txTmp.ins[inIndex]]
|
2014-03-21 03:15:15 +01:00
|
|
|
}
|
|
|
|
|
2014-05-01 22:25:57 +02:00
|
|
|
var htB = new Buffer(4)
|
|
|
|
htB.writeUInt32LE(hashType, 0)
|
2014-03-23 20:01:33 +01:00
|
|
|
|
2014-05-08 02:44:35 +02:00
|
|
|
var buffer = Buffer.concat([txTmp.toBuffer(), htB])
|
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-02 23:55:47 +02:00
|
|
|
Transaction.prototype.getHash = 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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
this.ins.forEach(function(txin) {
|
2014-03-31 05:47:47 +02:00
|
|
|
newTx.addInput(txin.clone())
|
|
|
|
})
|
2014-03-23 20:03:58 +01:00
|
|
|
|
|
|
|
this.outs.forEach(function(txout) {
|
2014-03-31 05:47:47 +02:00
|
|
|
newTx.addOutput(txout.clone())
|
|
|
|
})
|
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-05 00:14:29 +02:00
|
|
|
// Copy because we mutate (reverse TxOutHashs)
|
|
|
|
buffer = new Buffer(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-01 22:36:21 +02:00
|
|
|
var ins = []
|
|
|
|
var outs = []
|
|
|
|
|
|
|
|
var version = readUInt32()
|
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)
|
|
|
|
|
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)
|
|
|
|
|
2014-05-01 22:36:21 +02:00
|
|
|
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()
|
|
|
|
|
|
|
|
ins.push({
|
2014-03-31 05:47:47 +02:00
|
|
|
outpoint: {
|
2014-05-01 22:36:21 +02:00
|
|
|
hash: hash.toString('hex'),
|
|
|
|
index: vout,
|
2014-03-31 05:47:47 +02:00
|
|
|
},
|
2014-05-01 22:36:21 +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()
|
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)
|
|
|
|
|
|
|
|
outs.push({
|
|
|
|
value: value,
|
|
|
|
script: Script.fromBuffer(script)
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
|
|
|
}
|
2014-03-23 20:02:31 +01:00
|
|
|
|
2014-05-01 22:36:21 +02:00
|
|
|
var locktime = readUInt32()
|
|
|
|
assert.equal(offset, buffer.length, 'Invalid transaction')
|
2014-03-09 06:46:20 +01:00
|
|
|
|
2014-05-01 22:36:21 +02:00
|
|
|
return new Transaction({
|
|
|
|
version: version,
|
|
|
|
ins: ins,
|
|
|
|
outs: outs,
|
|
|
|
locktime: locktime
|
|
|
|
})
|
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
|
|
|
/**
|
|
|
|
* Signs a standard output at some index with the given key
|
|
|
|
*/
|
2014-05-06 08:52:31 +02:00
|
|
|
Transaction.prototype.sign = function(index, key, type) {
|
2014-04-17 11:08:16 +02:00
|
|
|
assert(key instanceof ECKey)
|
2014-04-25 19:50:41 +02:00
|
|
|
|
2014-05-06 08:52:31 +02:00
|
|
|
var script = key.pub.getAddress().toScriptPubKey()
|
2014-04-25 19:50:41 +02:00
|
|
|
var signature = this.signScriptSig(index, script, key, type)
|
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-04-25 19:58:25 +02:00
|
|
|
var scriptSig = Script.createPubKeyHashScriptSig(signature, key.pub)
|
2014-04-22 21:31:24 +02:00
|
|
|
this.setScriptSig(index, scriptSig)
|
2013-10-07 21:27:19 +02:00
|
|
|
}
|
|
|
|
|
2014-04-25 19:50:41 +02:00
|
|
|
Transaction.prototype.signScriptSig = function(index, script, key, type) {
|
2014-03-31 05:47:47 +02:00
|
|
|
type = type || SIGHASH_ALL
|
2014-04-25 19:51:01 +02:00
|
|
|
|
2014-05-03 04:19:38 +02:00
|
|
|
assert((index >= 0), 'Invalid vin index')
|
2014-04-25 19:51:01 +02:00
|
|
|
assert(script instanceof Script, 'Invalid Script object')
|
|
|
|
assert(key instanceof ECKey, 'Invalid private key')
|
|
|
|
// assert.equal(type & 0x7F, type, 'Invalid type') // TODO
|
|
|
|
|
2014-05-04 07:03:24 +02:00
|
|
|
var hash = this.hashForSignature(script, index, type)
|
2014-04-22 21:33:11 +02:00
|
|
|
return key.sign(hash).concat([type])
|
2013-10-07 21:27:19 +02:00
|
|
|
}
|
|
|
|
|
2014-04-22 21:31:24 +02:00
|
|
|
Transaction.prototype.setScriptSig = function(index, script) {
|
|
|
|
this.ins[index].script = script
|
2013-11-02 11:20:09 +01:00
|
|
|
}
|
|
|
|
|
2014-05-16 16:22:56 +02:00
|
|
|
Transaction.prototype.validateSig = function(index, script, pub, sig) {
|
|
|
|
var type = sig[sig.length - 1]
|
2014-05-04 07:03:24 +02:00
|
|
|
var hash = this.hashForSignature(script, index, type)
|
2014-04-22 21:33:11 +02:00
|
|
|
|
2014-05-16 16:22:56 +02:00
|
|
|
sig = sig.slice(0, -1)
|
2014-04-22 21:33:11 +02:00
|
|
|
return pub.verify(hash, sig)
|
2013-10-08 08:55:52 +02:00
|
|
|
}
|
2013-10-07 21:27:19 +02:00
|
|
|
|
2014-03-22 16:48:21 +01:00
|
|
|
Transaction.feePerKb = 20000
|
|
|
|
Transaction.prototype.estimateFee = function(feePerKb){
|
2014-03-25 04:21:23 +01:00
|
|
|
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;
|
2014-03-25 04:21:23 +01:00
|
|
|
var size = this.ins.length * uncompressedInSize + this.outs.length * outSize + fixedPadding
|
2014-03-22 16:48:21 +01:00
|
|
|
|
2014-03-25 05:28:26 +01:00
|
|
|
return feePerKb * Math.ceil(size / 1000)
|
2014-03-22 16:48:21 +01:00
|
|
|
}
|
|
|
|
|
2013-10-21 20:00:31 +02:00
|
|
|
var TransactionIn = function (data) {
|
2014-03-31 05:47:47 +02:00
|
|
|
if (typeof data == "string") {
|
|
|
|
this.outpoint = { hash: data.split(':')[0], index: data.split(':')[1] }
|
|
|
|
} else if (data.outpoint) {
|
|
|
|
this.outpoint = data.outpoint
|
|
|
|
} else {
|
|
|
|
this.outpoint = { hash: data.hash, index: data.index }
|
|
|
|
}
|
|
|
|
|
2014-05-05 13:19:09 +02:00
|
|
|
assert(data.script, 'Invalid TxIn parameters')
|
|
|
|
this.script = data.script
|
2014-03-31 05:47:47 +02:00
|
|
|
this.sequence = data.sequence || this.defaultSequence
|
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2013-10-21 20:00:31 +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
|
|
|
|
},
|
|
|
|
script: this.script.clone(),
|
|
|
|
sequence: this.sequence
|
|
|
|
})
|
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-05-05 13:19:09 +02:00
|
|
|
function TransactionOut(data) {
|
|
|
|
this.script = data.script
|
|
|
|
this.value = data.value
|
|
|
|
this.address = data.address
|
2014-03-31 05:47:47 +02:00
|
|
|
|
2014-05-06 08:52:31 +02:00
|
|
|
if (data.address) this.address = data.address
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-23 14:34:52 +01:00
|
|
|
TransactionOut.prototype.clone = function() {
|
2014-05-05 13:19:09 +02:00
|
|
|
return new TransactionOut({
|
2013-02-17 06:39:15 +01:00
|
|
|
script: this.script.clone(),
|
2014-05-05 13:19:09 +02:00
|
|
|
value: this.value,
|
|
|
|
address: this.address
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-03-23 14:34:52 +01:00
|
|
|
}
|
|
|
|
|
2014-03-23 20:01:33 +01:00
|
|
|
module.exports = {
|
|
|
|
Transaction: Transaction,
|
|
|
|
TransactionIn: TransactionIn,
|
|
|
|
TransactionOut: TransactionOut
|
|
|
|
}
|