bitcoinjs-lib/test/transaction.js

257 lines
7.9 KiB
JavaScript
Raw Normal View History

var assert = require('assert')
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
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-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-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-06-16 16:17:08 +02:00
f.raw.outs.forEach(function(fout) {
fout.script = Script.fromHex(fout.script)
})
})
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-06-16 16:17:08 +02:00
assert.deepEqual(actual, f.raw)
})
})
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)
}, new RegExp(f.exception))
2014-06-16 16:17:08 +02:00
})
})
2014-06-16 16:17:08 +02: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-06-16 16:17:08 +02: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)
prevTxHash = prevTx.getHash()
2014-06-16 16:17:08 +02:00
prevTxId = prevTx.getId()
})
2014-06-16 16:17:08 +02:00
it('accepts a transaction id', function() {
var tx = new Transaction()
tx.addInput(prevTxId, 0)
2014-06-16 16:17:08 +02:00
assert.deepEqual(tx.ins[0].hash, prevTxHash)
})
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-06-16 16:17:08 +02:00
assert.deepEqual(tx.ins[0].hash, prevTxHash)
})
2014-06-16 16:17:08 +02:00
it('returns an index', function() {
var tx = new Transaction()
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()
tx.addInput(prevTxHash, 0)
2014-05-01 22:25:57 +02:00
assert.equal(tx.ins[0].sequence, Transaction.DEFAULT_SEQUENCE)
2014-03-31 05:47:47 +02: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 j = tx.addInput(txIn.hash, txIn.index, txIn.sequence)
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)
var sequence = txIn.sequence
if (sequence == undefined) sequence = Transaction.DEFAULT_SEQUENCE
assert.equal(tx.ins[i].sequence, sequence)
2014-06-16 16:17:08 +02:00
})
2014-03-17 10:49:37 +01:00
})
2014-06-16 16:17:08 +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-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-06-16 16:17:08 +02:00
assert.deepEqual(tx.outs[0].script, destScript)
assert.equal(tx.outs[0].value, 40000)
})
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-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-16 16:17:08 +02:00
assert.deepEqual(tx.outs, f.raw.outs)
})
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-06-16 16:17:08 +02:00
it('should have value equality', function() {
assert.deepEqual(actual, expected)
})
2014-06-16 16:17:08 +02:00
it('should not have reference equality', function() {
assert.notEqual(actual, expected)
})
2014-06-16 16:17:08 +02: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-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
})
describe('getHash', function() {
fixtures.valid.forEach(function(f) {
it('should return the hash for ' + f.txid, function() {
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],
// 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)
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) {
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)
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){
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'
assert.equal(tx.toHex(), expected)
2014-04-26 07:38:43 +02:00
})
2014-04-22 21:35:44 +02:00
})
})