2014-04-17 15:31:45 +02:00
|
|
|
var assert = require('assert')
|
2014-05-13 09:55:53 +02:00
|
|
|
var networks = require('../src/networks')
|
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-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-06-16 16:17:08 +02:00
|
|
|
// FIXME: what is a better way to do this, seems a bit odd
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
var Script = require('../src/script')
|
2014-05-21 03:48:30 +02:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
f.raw.ins.forEach(function(fin) {
|
|
|
|
fin.hash = new Buffer(fin.hash, 'hex')
|
|
|
|
fin.script = Script.fromHex(fin.script)
|
2014-05-21 03:48:30 +02:00
|
|
|
})
|
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
f.raw.outs.forEach(function(fout) {
|
|
|
|
fout.script = Script.fromHex(fout.script)
|
|
|
|
})
|
|
|
|
})
|
2014-03-09 06:46:20 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
describe('Transaction', function() {
|
|
|
|
describe('fromBuffer/fromHex', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('imports ' + f.txid + ' correctly', function() {
|
|
|
|
var actual = Transaction.fromHex(f.hex)
|
2014-05-05 00:14:29 +02:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.deepEqual(actual, f.raw)
|
|
|
|
})
|
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)
|
|
|
|
}, /Invalid transaction/)
|
|
|
|
})
|
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) {
|
|
|
|
it('exports ' + f.txid + ' correctly', function() {
|
|
|
|
var actual = Transaction.prototype.toBuffer.call(f.raw)
|
|
|
|
|
|
|
|
assert.equal(actual.toString('hex'), f.hex)
|
|
|
|
})
|
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 crypto = require('../src/crypto')
|
2014-03-17 09:31:35 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
var f = fixtures.valid[0]
|
|
|
|
prevTx = Transaction.fromHex(f.hex)
|
|
|
|
prevTxId = prevTx.getId()
|
|
|
|
prevTxHash = crypto.hash256(prevTx.toBuffer())
|
|
|
|
})
|
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: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:17:08 +02:00
|
|
|
assert.equal(tx.addInput(prevTxId, 0), 0)
|
|
|
|
assert.equal(tx.addInput(prevTxId, 0), 1)
|
|
|
|
})
|
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()
|
|
|
|
tx.addInput(prevTxId, 0)
|
2014-05-01 22:25:57 +02:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.equal(tx.ins[0].sequence, 0xffffffff)
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-03-09 06:46:20 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('should add the inputs for ' + f.txid + ' correctly', function() {
|
|
|
|
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) {
|
|
|
|
var txInId = new Buffer(txIn.hash)
|
|
|
|
Array.prototype.reverse.call(txInId)
|
|
|
|
txInId = txInId.toString('hex')
|
2014-06-13 08:29:57 +02:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
var j = tx.addInput(txInId, txIn.index)
|
2014-03-17 10:49:37 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.equal(i, j)
|
|
|
|
assert.deepEqual(tx.ins[i].hash, txIn.hash)
|
|
|
|
assert.equal(tx.ins[i].index, txIn.index)
|
|
|
|
})
|
2014-03-17 10:49:37 +01:00
|
|
|
})
|
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) {
|
|
|
|
it('should add the outputs for ' + f.txid + ' correctly', function() {
|
|
|
|
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) {
|
|
|
|
var j = tx.addOutput(txOut.script, txOut.value)
|
2014-05-06 06:12:37 +02:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.equal(i, j)
|
|
|
|
})
|
2014-06-13 08:44:02 +02:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.deepEqual(tx.outs, f.raw.outs)
|
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) {
|
|
|
|
it('should return the txid for ' + f.txid, function() {
|
|
|
|
var tx = Transaction.fromHex(f.hex)
|
|
|
|
var actual = tx.getId()
|
2014-03-18 02:31:17 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.equal(actual, f.txid)
|
2014-03-17 18:32:31 +01:00
|
|
|
})
|
|
|
|
})
|
2014-03-17 10:49:37 +01:00
|
|
|
})
|
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
// TODO:
|
|
|
|
// hashForSignature: [Function],
|
|
|
|
|
|
|
|
// FIXME: could be better
|
|
|
|
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
|
|
|
})
|