TxBuilder: Initial commit and tests
This commit is contained in:
parent
884fd542fe
commit
bcbcd58964
3 changed files with 500 additions and 0 deletions
test
113
test/fixtures/transaction_builder.json
vendored
Normal file
113
test/fixtures/transaction_builder.json
vendored
Normal file
|
@ -0,0 +1,113 @@
|
|||
{
|
||||
"valid": {
|
||||
"build": [
|
||||
{
|
||||
"description": "pubKeyHash 1:1 transaction",
|
||||
"txid": "bd641f4b0aa8bd70189ab45e935c4762f0e1c49f294b4779d79887937b7cf42e",
|
||||
"inputs": [
|
||||
{
|
||||
"index": 0,
|
||||
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||
"privKeys": ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"]
|
||||
}
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||
"value": 10000
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "2-of-2 P2SH multisig Transaction",
|
||||
"txid": "8c500ce6eef6c78a10de923b68394cf31120151bdc4600e4b12de865defa9d24",
|
||||
"inputs": [
|
||||
{
|
||||
"index": 0,
|
||||
"prevTx": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf",
|
||||
"privKeys": ["91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT"],
|
||||
"redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG"
|
||||
}
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"script": "OP_DUP OP_HASH160 faf1d99bf040ea9c7f8cc9f14ac6733ad75ce246 OP_EQUALVERIFY OP_CHECKSIG",
|
||||
"value": 10000
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"invalid": {
|
||||
"build": [
|
||||
{
|
||||
"exception": "Transaction has no inputs",
|
||||
"hex": "",
|
||||
"outputs": [
|
||||
{
|
||||
"script": "",
|
||||
"value": 1000
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"exception": "Transaction has no outputs",
|
||||
"hex": "",
|
||||
"inputs": [
|
||||
{
|
||||
"hash": "",
|
||||
"index": 0
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"exception": "Transaction has no signatures",
|
||||
"hex": "",
|
||||
"inputs": [
|
||||
{
|
||||
"hash": "",
|
||||
"index": 0
|
||||
}
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"script": "",
|
||||
"value": 1000
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"exception": "Transaction is missing signatures",
|
||||
"hex": "",
|
||||
"inputs": [
|
||||
{
|
||||
"hash": "",
|
||||
"index": 0
|
||||
},
|
||||
{
|
||||
"hash": "",
|
||||
"index": 1
|
||||
}
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"script": "",
|
||||
"value": 1000
|
||||
}
|
||||
],
|
||||
"signatures": [
|
||||
{
|
||||
"wif": "",
|
||||
"index": 0
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"fromTransaction": [
|
||||
{
|
||||
"exception": "Transaction contains unsupported script types",
|
||||
"hex": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
228
test/transaction_builder.js
Normal file
228
test/transaction_builder.js
Normal file
|
@ -0,0 +1,228 @@
|
|||
var assert = require('assert')
|
||||
var ecdsa = require('../src/ecdsa')
|
||||
var scripts = require('../src/scripts')
|
||||
|
||||
var Address = require('../src/address')
|
||||
var BigInteger = require('bigi')
|
||||
var ECKey = require('../src/eckey')
|
||||
var Script = require('../src/script')
|
||||
var Transaction = require('../src/transaction')
|
||||
var TransactionBuilder = require('../src/transaction_builder')
|
||||
|
||||
var fixtures = require('./fixtures/transaction_builder')
|
||||
|
||||
describe('TransactionBuilder', function() {
|
||||
var privAddress, privScript
|
||||
var prevTx, prevTxHash
|
||||
var privKey
|
||||
var txb
|
||||
|
||||
beforeEach(function() {
|
||||
txb = new TransactionBuilder()
|
||||
|
||||
prevTx = new Transaction()
|
||||
prevTx.addOutput('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH', 0)
|
||||
prevTx.addOutput('1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP', 1)
|
||||
prevTxHash = prevTx.getHash()
|
||||
prevTxId = prevTx.getId()
|
||||
|
||||
privKey = new ECKey(BigInteger.ONE, false)
|
||||
privAddress = privKey.pub.getAddress()
|
||||
privScript = privAddress.toOutputScript()
|
||||
value = 10000
|
||||
})
|
||||
|
||||
describe('addInput', function() {
|
||||
it('accepts a txHash and index', function() {
|
||||
var vin = txb.addInput(prevTxHash, 1)
|
||||
assert.equal(vin, 0)
|
||||
|
||||
var txin = txb.tx.ins[0]
|
||||
assert.equal(txin.hash, prevTxHash)
|
||||
assert.equal(txin.index, 1)
|
||||
assert.equal(txb.prevOutScripts[0], undefined)
|
||||
})
|
||||
|
||||
it('accepts a txHash, index and scriptPubKey', function() {
|
||||
var vin = txb.addInput(prevTxHash, 1, prevTx.outs[1].script)
|
||||
assert.equal(vin, 0)
|
||||
|
||||
var txin = txb.tx.ins[0]
|
||||
assert.equal(txin.hash, prevTxHash)
|
||||
assert.equal(txin.index, 1)
|
||||
assert.equal(txb.prevOutScripts[0], prevTx.outs[1].script)
|
||||
})
|
||||
|
||||
it('accepts a prevTx and index', function() {
|
||||
var vin = txb.addInput(prevTx, 1)
|
||||
assert.equal(vin, 0)
|
||||
|
||||
var txin = txb.tx.ins[0]
|
||||
assert.deepEqual(txin.hash, prevTxHash)
|
||||
assert.equal(txin.index, 1)
|
||||
assert.equal(txb.prevOutScripts[0], prevTx.outs[1].script)
|
||||
})
|
||||
|
||||
it('returns the input index', function() {
|
||||
assert.equal(txb.addInput(prevTxHash, 0), 0)
|
||||
assert.equal(txb.addInput(prevTxHash, 1), 1)
|
||||
})
|
||||
|
||||
it('throws if a Tx and prevOutScript is given', function() {
|
||||
assert.throws(function() {
|
||||
txb.addInput(prevTx, 0, privScript)
|
||||
}, /Unnecessary Script provided/)
|
||||
})
|
||||
|
||||
it('throws if prevOutScript is not supported', function() {
|
||||
assert.throws(function() {
|
||||
txb.addInput(prevTxHash, 0, Script.EMPTY)
|
||||
}, /PrevOutScript not supported \(nonstandard\)/)
|
||||
})
|
||||
|
||||
it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function() {
|
||||
txb.addInput(prevTxHash, 0)
|
||||
txb.sign(0, privKey)
|
||||
|
||||
assert.throws(function() {
|
||||
txb.addInput(prevTxHash, 0)
|
||||
}, /No, this would invalidate signatures/)
|
||||
})
|
||||
})
|
||||
|
||||
describe('addOutput', function() {
|
||||
it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function() {
|
||||
txb.addInput(prevTxHash, 0)
|
||||
txb.addOutput(privScript, value)
|
||||
txb.sign(0, privKey)
|
||||
|
||||
assert.throws(function() {
|
||||
txb.addOutput(privScript, 9000)
|
||||
}, /No, this would invalidate signatures/)
|
||||
})
|
||||
})
|
||||
|
||||
describe('sign', function() {
|
||||
describe('when prevOutScript is undefined', function() {
|
||||
it('assumes pubKeyHash', function() {
|
||||
txb.addInput(prevTxHash, 0)
|
||||
txb.sign(0, privKey)
|
||||
|
||||
assert.strictEqual(txb.signatures[0].redeemScript, undefined)
|
||||
assert.equal(txb.signatures[0].scriptType, 'pubkeyhash')
|
||||
})
|
||||
})
|
||||
|
||||
describe('when redeemScript is defined', function() {
|
||||
it('assumes scriptHash', function() {
|
||||
txb.addInput(prevTxHash, 0)
|
||||
txb.sign(0, privKey, privScript)
|
||||
|
||||
assert.equal(txb.signatures[0].redeemScript, privScript)
|
||||
})
|
||||
|
||||
it('throws if prevOutScript is not P2SH', function() {
|
||||
txb.addInput(prevTx, 0)
|
||||
|
||||
assert.throws(function() {
|
||||
txb.sign(0, privKey, privScript)
|
||||
}, /PrevOutScript must be P2SH/)
|
||||
})
|
||||
|
||||
it('throws if redeemScript is P2SH', function() {
|
||||
txb.addInput(prevTxHash, 0)
|
||||
|
||||
var privScriptP2SH = scripts.scriptHashOutput(privScript.getHash())
|
||||
|
||||
assert.throws(function() {
|
||||
txb.sign(0, privKey, privScriptP2SH)
|
||||
}, /RedeemScript can\'t be P2SH/)
|
||||
})
|
||||
|
||||
it('throws if redeemScript not supported', function() {
|
||||
txb.addInput(prevTxHash, 0)
|
||||
|
||||
assert.throws(function() {
|
||||
txb.sign(0, privKey, Script.EMPTY)
|
||||
}, /RedeemScript not supported \(nonstandard\)/)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('build', function() {
|
||||
fixtures.valid.build.forEach(function(f) {
|
||||
it('builds the correct transaction', function() {
|
||||
f.inputs.forEach(function(input) {
|
||||
var prevTx
|
||||
if (input.prevTx.length === 64) {
|
||||
prevTx = input.prevTx
|
||||
} else {
|
||||
prevTx = Transaction.fromHex(input.prevTx)
|
||||
}
|
||||
|
||||
txb.addInput(prevTx, input.index)
|
||||
})
|
||||
|
||||
f.outputs.forEach(function(output) {
|
||||
var script = Script.fromASM(output.script)
|
||||
|
||||
txb.addOutput(script, output.value)
|
||||
})
|
||||
|
||||
f.inputs.forEach(function(input, index) {
|
||||
var redeemScript
|
||||
|
||||
if (input.redeemScript) {
|
||||
redeemScript = Script.fromASM(input.redeemScript)
|
||||
}
|
||||
|
||||
input.privKeys.forEach(function(wif) {
|
||||
var privKey = ECKey.fromWIF(wif)
|
||||
|
||||
txb.sign(index, privKey, redeemScript)
|
||||
})
|
||||
})
|
||||
|
||||
var tx = txb.build()
|
||||
|
||||
assert.equal(tx.getId(), f.txid)
|
||||
})
|
||||
})
|
||||
|
||||
it('throws if Transaction has no inputs', function() {
|
||||
txb.addOutput(privScript, value)
|
||||
|
||||
assert.throws(function() {
|
||||
txb.build()
|
||||
}, /Transaction has no inputs/)
|
||||
})
|
||||
|
||||
it('throws if Transaction has no outputs', function() {
|
||||
txb.addInput(prevTxHash, 0)
|
||||
|
||||
assert.throws(function() {
|
||||
txb.build()
|
||||
}, /Transaction has no outputs/)
|
||||
})
|
||||
|
||||
it('throws if Transaction has no signatures', function() {
|
||||
txb.addInput(prevTxHash, 0)
|
||||
txb.addOutput(privScript, value)
|
||||
|
||||
assert.throws(function() {
|
||||
txb.build()
|
||||
}, /Transaction is missing signatures/)
|
||||
})
|
||||
|
||||
it('throws if Transaction has not enough signatures', function() {
|
||||
txb.addInput(prevTxHash, 0)
|
||||
txb.addInput(prevTxHash, 1)
|
||||
txb.addOutput(privScript, value)
|
||||
txb.sign(0, privKey)
|
||||
|
||||
assert.throws(function() {
|
||||
txb.build()
|
||||
}, /Transaction is missing signatures/)
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue