2014-04-17 15:31:45 +02:00
|
|
|
var assert = require('assert')
|
2014-06-13 01:58:52 +02:00
|
|
|
var scripts = require('../src/scripts')
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2014-05-13 09:55:53 +02:00
|
|
|
var Address = require('../src/address')
|
|
|
|
var ECKey = require('../src/eckey')
|
2014-05-16 09:12:39 +02:00
|
|
|
var Transaction = require('../src/transaction')
|
2014-10-16 06:25:43 +02:00
|
|
|
var Script = require('../src/script')
|
2014-03-23 14:34:52 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
var fixtures = require('./fixtures/transaction')
|
2014-03-09 06:46:20 +01:00
|
|
|
|
2014-12-12 23:20:19 +01:00
|
|
|
describe('Transaction', function() {
|
|
|
|
function fromRaw(raw) {
|
|
|
|
var tx = new Transaction()
|
|
|
|
tx.version = raw.version
|
|
|
|
tx.locktime = raw.locktime
|
2014-05-21 03:48:30 +02:00
|
|
|
|
2014-12-12 23:20:19 +01:00
|
|
|
raw.ins.forEach(function(txIn) {
|
|
|
|
var txHash = new Buffer(txIn.hash, 'hex')
|
|
|
|
var script = txIn.script ? Script.fromASM(txIn.script) : undefined
|
2014-05-21 03:48:30 +02:00
|
|
|
|
2014-12-12 23:20:19 +01:00
|
|
|
tx.addInput(txHash, txIn.index, txIn.sequence, script)
|
|
|
|
})
|
|
|
|
|
|
|
|
raw.outs.forEach(function(txOut) {
|
|
|
|
tx.addOutput(Script.fromASM(txOut.script), txOut.value)
|
|
|
|
})
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
2014-03-09 06:46:20 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
describe('fromBuffer/fromHex', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
2014-12-12 23:48:31 +01:00
|
|
|
it('imports ' + f.id + ' correctly', function() {
|
2014-06-16 16:17:08 +02:00
|
|
|
var actual = Transaction.fromHex(f.hex)
|
2014-05-05 00:14:29 +02:00
|
|
|
|
2014-12-12 23:20:19 +01:00
|
|
|
assert.deepEqual(actual.toHex(), f.hex)
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-05-05 00:14:29 +02:00
|
|
|
})
|
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
fixtures.invalid.fromBuffer.forEach(function(f) {
|
|
|
|
it('throws on ' + f.exception, function() {
|
|
|
|
assert.throws(function() {
|
|
|
|
Transaction.fromHex(f.hex)
|
2014-06-16 16:16:48 +02:00
|
|
|
}, new RegExp(f.exception))
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-03-17 09:31:35 +01:00
|
|
|
})
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-03-17 09:31:35 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
describe('toBuffer/toHex', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
2014-12-12 23:48:31 +01:00
|
|
|
it('exports ' + f.id + ' correctly', function() {
|
2014-12-12 23:20:19 +01:00
|
|
|
var actual = fromRaw(f.raw)
|
2014-06-16 16:17:08 +02:00
|
|
|
|
2014-12-12 23:20:19 +01:00
|
|
|
assert.deepEqual(actual.toHex(), f.hex)
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-03-17 09:31:35 +01:00
|
|
|
})
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-03-17 09:31:35 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
describe('addInput', function() {
|
|
|
|
// FIXME: not as pretty as could be
|
|
|
|
// Probably a bit representative of the API
|
|
|
|
var prevTxHash, prevTxId, prevTx
|
|
|
|
beforeEach(function() {
|
|
|
|
var f = fixtures.valid[0]
|
|
|
|
prevTx = Transaction.fromHex(f.hex)
|
2014-06-16 10:52:06 +02:00
|
|
|
prevTxHash = prevTx.getHash()
|
2014-06-16 16:17:08 +02:00
|
|
|
prevTxId = prevTx.getId()
|
|
|
|
})
|
2014-03-17 09:31:35 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
it('accepts a transaction id', function() {
|
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addInput(prevTxId, 0)
|
2014-03-17 09:31:35 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.deepEqual(tx.ins[0].hash, prevTxHash)
|
2014-03-17 09:31:35 +01:00
|
|
|
})
|
|
|
|
|
2014-06-16 16:16:19 +02:00
|
|
|
it('accepts a transaction hash', function() {
|
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addInput(prevTxHash, 0)
|
|
|
|
|
|
|
|
assert.deepEqual(tx.ins[0].hash, prevTxHash)
|
|
|
|
})
|
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
it('accepts a Transaction object', function() {
|
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addInput(prevTx, 0)
|
2014-03-17 09:31:35 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.deepEqual(tx.ins[0].hash, prevTxHash)
|
2014-03-17 09:31:35 +01:00
|
|
|
})
|
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
it('returns an index', function() {
|
2014-03-26 09:11:10 +01:00
|
|
|
var tx = new Transaction()
|
2014-06-16 16:16:19 +02:00
|
|
|
assert.equal(tx.addInput(prevTxHash, 0), 0)
|
|
|
|
assert.equal(tx.addInput(prevTxHash, 0), 1)
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-05-01 22:25:57 +02:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
it('defaults to DEFAULT_SEQUENCE', function() {
|
|
|
|
var tx = new Transaction()
|
2014-06-16 16:16:19 +02:00
|
|
|
tx.addInput(prevTxHash, 0)
|
2014-05-01 22:25:57 +02:00
|
|
|
|
2014-06-16 09:31:01 +02:00
|
|
|
assert.equal(tx.ins[0].sequence, Transaction.DEFAULT_SEQUENCE)
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-03-09 06:46:20 +01:00
|
|
|
|
2014-10-16 06:25:43 +02:00
|
|
|
it('defaults to empty script', function() {
|
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addInput(prevTxHash, 0)
|
|
|
|
|
|
|
|
assert.equal(tx.ins[0].script, Script.EMPTY)
|
|
|
|
})
|
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
2014-12-12 23:48:31 +01:00
|
|
|
it('should add the inputs for ' + f.id + ' correctly', function() {
|
2014-06-16 16:17:08 +02:00
|
|
|
var tx = new Transaction()
|
2014-03-17 10:49:37 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
f.raw.ins.forEach(function(txIn, i) {
|
2014-12-12 23:20:19 +01:00
|
|
|
var txHash = new Buffer(txIn.hash, 'hex')
|
|
|
|
var script = txIn.script ? Script.fromASM(txIn.script) : undefined
|
|
|
|
var j = tx.addInput(txHash, txIn.index, txIn.sequence, script)
|
|
|
|
var sequence = txIn.sequence
|
2014-12-16 04:21:34 +01:00
|
|
|
if (sequence === undefined || sequence === null ) {
|
2014-12-12 23:20:19 +01:00
|
|
|
sequence = Transaction.DEFAULT_SEQUENCE
|
|
|
|
}
|
2014-03-17 10:49:37 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.equal(i, j)
|
2014-12-12 23:20:19 +01:00
|
|
|
assert.equal(tx.ins[i].hash.toString('hex'), txIn.hash)
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.equal(tx.ins[i].index, txIn.index)
|
2014-06-16 10:41:01 +02:00
|
|
|
assert.equal(tx.ins[i].sequence, sequence)
|
2014-12-12 23:20:19 +01:00
|
|
|
assert.deepEqual(tx.ins[i].script, script || Script.EMPTY)
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-03-17 10:49:37 +01:00
|
|
|
})
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-06-17 12:18:39 +02:00
|
|
|
|
|
|
|
fixtures.invalid.addInput.forEach(function(f) {
|
|
|
|
it('throws on ' + f.exception, function() {
|
|
|
|
var tx = new Transaction()
|
|
|
|
var hash = new Buffer(f.hash, 'hex')
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
tx.addInput(hash, f.index)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-03-17 10:49:37 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
describe('addOutput', function() {
|
|
|
|
// FIXME: not as pretty as could be
|
|
|
|
// Probably a bit representative of the API
|
|
|
|
var destAddressB58, destAddress, destScript
|
|
|
|
beforeEach(function() {
|
|
|
|
destAddressB58 = '15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3'
|
|
|
|
destAddress = Address.fromBase58Check(destAddressB58)
|
|
|
|
destScript = destAddress.toOutputScript()
|
|
|
|
})
|
2014-06-13 08:44:02 +02:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
it('accepts an address string', function() {
|
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addOutput(destAddressB58, 40000)
|
2014-03-17 10:49:37 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.deepEqual(tx.outs[0].script, destScript)
|
|
|
|
assert.equal(tx.outs[0].value, 40000)
|
|
|
|
})
|
2014-03-17 10:49:37 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
it('accepts an Address', function() {
|
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addOutput(destAddress, 40000)
|
2014-03-17 10:49:37 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.deepEqual(tx.outs[0].script, destScript)
|
|
|
|
assert.equal(tx.outs[0].value, 40000)
|
2014-03-17 11:01:36 +01:00
|
|
|
})
|
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
it('accepts a scriptPubKey', function() {
|
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addOutput(destScript, 40000)
|
2014-05-05 07:48:44 +02:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.deepEqual(tx.outs[0].script, destScript)
|
|
|
|
assert.equal(tx.outs[0].value, 40000)
|
|
|
|
})
|
2014-06-13 08:29:57 +02:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
it('returns an index', function() {
|
|
|
|
var tx = new Transaction()
|
|
|
|
assert.equal(tx.addOutput(destScript, 40000), 0)
|
|
|
|
assert.equal(tx.addOutput(destScript, 40000), 1)
|
|
|
|
})
|
2014-05-19 06:55:54 +02:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
2014-12-12 23:48:31 +01:00
|
|
|
it('should add the outputs for ' + f.id + ' correctly', function() {
|
2014-06-16 16:17:08 +02:00
|
|
|
var tx = new Transaction()
|
2014-05-06 06:12:37 +02:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
f.raw.outs.forEach(function(txOut, i) {
|
2014-12-12 23:20:19 +01:00
|
|
|
var scriptPubKey = Script.fromASM(txOut.script)
|
|
|
|
var j = tx.addOutput(scriptPubKey, txOut.value)
|
2014-05-06 06:12:37 +02:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.equal(i, j)
|
2014-12-12 23:20:19 +01:00
|
|
|
assert.equal(tx.outs[i].script, scriptPubKey)
|
|
|
|
assert.equal(tx.outs[i].value, txOut.value)
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-06-13 08:44:02 +02:00
|
|
|
})
|
2014-03-17 10:49:37 +01:00
|
|
|
})
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-03-17 18:32:31 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
describe('clone', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
var expected = Transaction.fromHex(f.hex)
|
|
|
|
var actual = expected.clone()
|
2014-03-18 02:31:17 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
it('should have value equality', function() {
|
|
|
|
assert.deepEqual(actual, expected)
|
2014-03-18 02:31:17 +01:00
|
|
|
})
|
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
it('should not have reference equality', function() {
|
|
|
|
assert.notEqual(actual, expected)
|
2014-03-18 02:31:17 +01:00
|
|
|
})
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
|
|
|
})
|
2014-03-18 02:31:17 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
describe('getId', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
2014-12-12 23:48:31 +01:00
|
|
|
it('should return the id for ' + f.id, function() {
|
2014-06-16 16:17:08 +02:00
|
|
|
var tx = Transaction.fromHex(f.hex)
|
|
|
|
var actual = tx.getId()
|
2014-03-18 02:31:17 +01:00
|
|
|
|
2014-12-12 23:48:31 +01:00
|
|
|
assert.equal(actual, f.id)
|
2014-03-17 18:32:31 +01:00
|
|
|
})
|
|
|
|
})
|
2014-03-17 10:49:37 +01:00
|
|
|
})
|
|
|
|
|
2014-06-16 10:52:06 +02:00
|
|
|
describe('getHash', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
2014-12-12 23:48:31 +01:00
|
|
|
it('should return the hash for ' + f.id, function() {
|
2014-06-16 10:52:06 +02:00
|
|
|
var tx = Transaction.fromHex(f.hex)
|
|
|
|
var actual = tx.getHash().toString('hex')
|
|
|
|
|
|
|
|
assert.equal(actual, f.hash)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
// TODO:
|
|
|
|
// hashForSignature: [Function],
|
|
|
|
|
2014-07-25 08:11:45 +02:00
|
|
|
// FIXME: remove in 2.x.y
|
2014-06-16 16:17:08 +02:00
|
|
|
describe('signInput/validateInput', function() {
|
2014-04-26 07:38:43 +02:00
|
|
|
it('works for multi-sig redeem script', function() {
|
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addInput('d6f72aab8ff86ff6289842a0424319bf2ddba85dc7c52757912297f948286389', 0)
|
2014-05-06 08:52:31 +02:00
|
|
|
tx.addOutput('mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', 1)
|
2014-04-26 07:38:43 +02:00
|
|
|
|
|
|
|
var privKeys = [
|
|
|
|
'5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf',
|
|
|
|
'5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAvUcVfH'
|
|
|
|
].map(function(wif) {
|
|
|
|
return ECKey.fromWIF(wif)
|
|
|
|
})
|
|
|
|
var pubKeys = privKeys.map(function(eck) { return eck.pub })
|
2014-06-13 01:58:52 +02:00
|
|
|
var redeemScript = scripts.multisigOutput(2, pubKeys)
|
2014-04-22 21:35:44 +02:00
|
|
|
|
2014-04-26 07:38:43 +02:00
|
|
|
var signatures = privKeys.map(function(privKey) {
|
2014-06-13 03:30:07 +02:00
|
|
|
return tx.signInput(0, redeemScript, privKey)
|
2014-04-26 07:38:43 +02:00
|
|
|
})
|
2014-04-22 21:35:44 +02:00
|
|
|
|
2014-06-13 01:58:52 +02:00
|
|
|
var redeemScriptSig = scripts.multisigInput(signatures)
|
|
|
|
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
|
2014-06-13 03:30:07 +02:00
|
|
|
tx.setInputScript(0, scriptSig)
|
2014-04-22 21:35:44 +02:00
|
|
|
|
2014-04-26 07:38:43 +02:00
|
|
|
signatures.forEach(function(sig, i){
|
2014-06-13 03:30:07 +02:00
|
|
|
assert(tx.validateInput(0, redeemScript, privKeys[i].pub, sig))
|
2014-04-26 07:38:43 +02:00
|
|
|
})
|
2014-04-22 21:35:44 +02:00
|
|
|
|
2014-04-26 07:38:43 +02:00
|
|
|
var expected = '010000000189632848f99722915727c5c75da8db2dbf194342a0429828f66ff88fab2af7d600000000fd1b0100483045022100e5be20d440b2bbbc886161f9095fa6d0bca749a4e41d30064f30eb97adc7a1f5022061af132890d8e4e90fedff5e9365aeeb77021afd8ef1d5c114d575512e9a130a0147304402205054e38e9d7b5c10481b6b4991fde5704cd94d49e344406e3c2ce4d18a43bf8e022051d7ba8479865b53a48bee0cce86e89a25633af5b2918aa276859489e232f51c014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0101000000000000001976a914751e76e8199196d454941c45d1b3a323f1433bd688ac00000000'
|
2014-05-08 02:44:35 +02:00
|
|
|
assert.equal(tx.toHex(), expected)
|
2014-04-26 07:38:43 +02:00
|
|
|
})
|
2014-04-22 21:35:44 +02:00
|
|
|
})
|
2014-03-09 06:46:20 +01:00
|
|
|
})
|