TxBuilder: adds fromTransaction impl. and basic tests

This commit is contained in:
Daniel Cousens 2014-07-28 14:28:44 +10:00
parent 7f62069d82
commit f9fed3c815
3 changed files with 143 additions and 31 deletions

View file

@ -1,9 +1,10 @@
var assert = require('assert') var assert = require('assert')
var scripts = require('./scripts') var scripts = require('./scripts')
var ECKey = require('./eckey') var ECPubKey = require('./ecpubkey')
var Transaction = require('./transaction') var ECSignature = require('./ecsignature')
var Script = require('./script') var Script = require('./script')
var Transaction = require('./transaction')
function TransactionBuilder() { function TransactionBuilder() {
this.prevOutMap = {} this.prevOutMap = {}
@ -14,7 +15,7 @@ function TransactionBuilder() {
this.tx = new Transaction() this.tx = new Transaction()
} }
TransactionBuilder.prototype.addInput = function(prevTx, index, prevOutScript, sequence) { TransactionBuilder.prototype.addInput = function(prevTx, index, sequence, prevOutScript) {
var prevOutHash var prevOutHash
if (typeof prevTx === 'string') { if (typeof prevTx === 'string') {
@ -103,7 +104,6 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT
} }
var input = this.signatures[index] var input = this.signatures[index]
assert.equal(input.hashType, hashType, 'Inconsistent hashType') assert.equal(input.hashType, hashType, 'Inconsistent hashType')
assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript')
@ -130,29 +130,34 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) {
var tx = this.tx.clone() var tx = this.tx.clone()
// Create script signatures from signature meta-data
this.signatures.forEach(function(input, index) { this.signatures.forEach(function(input, index) {
var scriptSig var scriptSig
var scriptType = input.scriptType
var signatures = input.signatures.map(function(signature) { var signatures = input.signatures.map(function(signature) {
return signature.toScriptSignature(input.hashType) return signature.toScriptSignature(input.hashType)
}) })
if (input.scriptType === 'pubkeyhash') { switch (scriptType) {
var signature = signatures[0] case 'pubkeyhash':
var publicKey = input.pubKeys[0] var signature = signatures[0]
scriptSig = scripts.pubKeyHashInput(signature, publicKey) var pubKey = input.pubKeys[0]
scriptSig = scripts.pubKeyHashInput(signature, pubKey)
} else if (input.scriptType === 'multisig') { break
var redeemScript = allowIncomplete ? undefined : input.redeemScript case 'multisig':
scriptSig = scripts.multisigInput(signatures, redeemScript) var redeemScript = allowIncomplete ? undefined : input.redeemScript
scriptSig = scripts.multisigInput(signatures, redeemScript)
} else if (input.scriptType === 'pubkey') { break
var signature = signatures[0] case 'pubkey':
scriptSig = scripts.pubKeyInput(signature) var signature = signatures[0]
scriptSig = scripts.pubKeyInput(signature)
} else {
assert(false, input.scriptType + ' not supported')
break
default:
assert(false, scriptType + ' not supported')
} }
if (input.redeemScript) { if (input.redeemScript) {
@ -165,4 +170,83 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) {
return tx return tx
} }
TransactionBuilder.fromTransaction = function(transaction) {
var txb = new TransactionBuilder()
// Extract/add inputs
transaction.ins.forEach(function(txin) {
txb.addInput(txin.hash, txin.index, txin.sequence)
})
// Extract/add outputs
transaction.outs.forEach(function(txout) {
txb.addOutput(txout.script, txout.value)
})
// Extract/add signatures
transaction.ins.forEach(function(txin) {
// Ignore empty scripts
if (txin.script.buffer.length === 0) return
var redeemScript
var scriptSig = txin.script
var scriptType = scripts.classifyInput(scriptSig)
// Re-classify if P2SH
if (scriptType === 'scripthash') {
redeemScript = Script.fromBuffer(scriptSig.chunks.slice(-1)[0])
scriptSig = Script.fromChunks(scriptSig.chunks.slice(0, -1))
scriptType = scripts.classifyInput(scriptSig)
assert.equal(scripts.classifyOutput(redeemScript), scriptType, 'Non-matching scriptSig and scriptPubKey in input')
}
// Extract hashType, pubKeys and signatures
var hashType, pubKeys, signatures
switch (scriptType) {
case 'pubkeyhash':
var parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0])
var pubKey = ECPubKey.fromBuffer(scriptSig.chunks[1])
hashType = parsed.hashType
pubKeys = [pubKey]
signatures = [parsed.signature]
break
case 'multisig':
var scriptSigs = scriptSig.chunks.slice(1) // ignore OP_0
var parsed = scriptSigs.map(function(scriptSig) {
return ECSignature.parseScriptSignature(scriptSig)
})
hashType = parsed[0].hashType
pubKeys = []
signatures = parsed.map(function(p) { return p.signature })
break
case 'pubkey':
var parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0])
hashType = parsed.hashType
pubKeys = []
signatures = [parsed.signature]
break
default:
assert(false, scriptType + ' not supported')
}
txb.signatures[txin.index] = {
hashType: hashType,
pubKeys: pubKeys,
redeemScript: redeemScript,
scriptType: scriptType,
signatures: signatures
}
})
return txb
}
module.exports = TransactionBuilder module.exports = TransactionBuilder

View file

@ -4,11 +4,14 @@
{ {
"description": "pubKeyHash->pubKeyHash 1:1 transaction", "description": "pubKeyHash->pubKeyHash 1:1 transaction",
"txid": "bd641f4b0aa8bd70189ab45e935c4762f0e1c49f294b4779d79887937b7cf42e", "txid": "bd641f4b0aa8bd70189ab45e935c4762f0e1c49f294b4779d79887937b7cf42e",
"txhex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000006b483045022100a3b254e1c10b5d039f36c05f323995d6e5a367d98dd78a13d5bbc3991b35720e022022fccea3897d594de0689601fbd486588d5bfa6915be2386db0397ee9a6e80b601210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000",
"inputs": [ "inputs": [
{ {
"index": 0, "index": 0,
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"privKeys": ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"] "privKeys": [
"KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
]
} }
], ],
"outputs": [ "outputs": [
@ -21,12 +24,15 @@
{ {
"description": "pubKey->pubKeyHash 1:1 transaction", "description": "pubKey->pubKeyHash 1:1 transaction",
"txid": "a900dea133a3c51e9fe55d82bf4a4f50a4c3ac6e380c841f93651a076573320c", "txid": "a900dea133a3c51e9fe55d82bf4a4f50a4c3ac6e380c841f93651a076573320c",
"txhex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000494830450221009833abb3ab49d7004c06bcc79eafd6905ada3eee91f3376ad388548034acd9a702202e84dda6ef2678c82256afcfc459aaa68e179b2bb0e6b2dc3f1410e132c5e6c301ffffffff0100f90295000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000",
"inputs": [ "inputs": [
{ {
"index": 0, "index": 0,
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"prevTxScript": "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 OP_CHECKSIG", "prevTxScript": "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 OP_CHECKSIG",
"privKeys": ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"] "privKeys": [
"KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
]
} }
], ],
"outputs": [ "outputs": [
@ -39,11 +45,15 @@
{ {
"description": "2-of-2 P2SH multisig -> pubKeyHash 1:1 Transaction", "description": "2-of-2 P2SH multisig -> pubKeyHash 1:1 Transaction",
"txid": "8c500ce6eef6c78a10de923b68394cf31120151bdc4600e4b12de865defa9d24", "txid": "8c500ce6eef6c78a10de923b68394cf31120151bdc4600e4b12de865defa9d24",
"txhex": "0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f0714900000000fd1a0100473044022040039a3d0a806d6c2c0ac8a62f2467c979c897c945f3f11905b9c5ea76b4a88002200976f187f852f7d186e8e8aa39332092aa8a504b63a7ae3d0eca09ebea1497fd0147304402205522d1949d13347054bd5ea86cdcad2344f49628a935faaee8f5e744bd3ef87e022063a28ab077817222ccd7d5a70e77ed7274840b9ba8db5dd93a33bdd41813d548014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0110270000000000001976a914faf1d99bf040ea9c7f8cc9f14ac6733ad75ce24688ac00000000",
"inputs": [ "inputs": [
{ {
"index": 0, "index": 0,
"prevTx": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", "prevTx": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf",
"privKeys": ["91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT"], "privKeys": [
"91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx",
"91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT"
],
"redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG" "redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG"
} }
], ],
@ -101,7 +111,9 @@
{ {
"index": 0, "index": 0,
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"privKeys": ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"] "privKeys": [
"KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
]
}, },
{ {
"index": 1, "index": 1,
@ -123,7 +135,9 @@
"index": 0, "index": 0,
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"prevTxScript": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474", "prevTxScript": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474",
"privKeys": ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"] "privKeys": [
"KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
]
} }
], ],
"outputs": [ "outputs": [

View file

@ -33,33 +33,36 @@ describe('TransactionBuilder', function() {
}) })
describe('addInput', function() { describe('addInput', function() {
it('accepts a txHash and index', function() { it('accepts a txHash, index [and sequence number]', function() {
var vin = txb.addInput(prevTxHash, 1) var vin = txb.addInput(prevTxHash, 1, 54)
assert.equal(vin, 0) assert.equal(vin, 0)
var txin = txb.tx.ins[0] var txin = txb.tx.ins[0]
assert.equal(txin.hash, prevTxHash) assert.equal(txin.hash, prevTxHash)
assert.equal(txin.index, 1) assert.equal(txin.index, 1)
assert.equal(txin.sequence, 54)
assert.equal(txb.prevOutScripts[0], undefined) assert.equal(txb.prevOutScripts[0], undefined)
}) })
it('accepts a txHash, index and scriptPubKey', function() { it('accepts a txHash, index [, sequence number and scriptPubKey]', function() {
var vin = txb.addInput(prevTxHash, 1, prevTx.outs[1].script) var vin = txb.addInput(prevTxHash, 1, 54, prevTx.outs[1].script)
assert.equal(vin, 0) assert.equal(vin, 0)
var txin = txb.tx.ins[0] var txin = txb.tx.ins[0]
assert.equal(txin.hash, prevTxHash) assert.equal(txin.hash, prevTxHash)
assert.equal(txin.index, 1) assert.equal(txin.index, 1)
assert.equal(txin.sequence, 54)
assert.equal(txb.prevOutScripts[0], prevTx.outs[1].script) assert.equal(txb.prevOutScripts[0], prevTx.outs[1].script)
}) })
it('accepts a prevTx and index', function() { it('accepts a prevTx, index [and sequence number]', function() {
var vin = txb.addInput(prevTx, 1) var vin = txb.addInput(prevTx, 1, 54)
assert.equal(vin, 0) assert.equal(vin, 0)
var txin = txb.tx.ins[0] var txin = txb.tx.ins[0]
assert.deepEqual(txin.hash, prevTxHash) assert.deepEqual(txin.hash, prevTxHash)
assert.equal(txin.index, 1) assert.equal(txin.index, 1)
assert.equal(txin.sequence, 54)
assert.equal(txb.prevOutScripts[0], prevTx.outs[1].script) assert.equal(txb.prevOutScripts[0], prevTx.outs[1].script)
}) })
@ -70,7 +73,7 @@ describe('TransactionBuilder', function() {
it('throws if prevOutScript is not supported', function() { it('throws if prevOutScript is not supported', function() {
assert.throws(function() { assert.throws(function() {
txb.addInput(prevTxHash, 0, Script.EMPTY) txb.addInput(prevTxHash, 0, undefined, Script.EMPTY)
}, /PrevOutScript not supported \(nonstandard\)/) }, /PrevOutScript not supported \(nonstandard\)/)
}) })
@ -153,7 +156,7 @@ describe('TransactionBuilder', function() {
prevTxScript = Script.fromASM(input.prevTxScript) prevTxScript = Script.fromASM(input.prevTxScript)
} }
txb.addInput(input.prevTx, input.index, prevTxScript) txb.addInput(input.prevTx, input.index, input.sequence, prevTxScript)
}) })
f.outputs.forEach(function(output) { f.outputs.forEach(function(output) {
@ -191,7 +194,7 @@ describe('TransactionBuilder', function() {
prevTxScript = Script.fromASM(input.prevTxScript) prevTxScript = Script.fromASM(input.prevTxScript)
} }
txb.addInput(input.prevTx, input.index, prevTxScript) txb.addInput(input.prevTx, input.index, input.sequence, prevTxScript)
}) })
f.outputs.forEach(function(output) { f.outputs.forEach(function(output) {
@ -220,4 +223,15 @@ describe('TransactionBuilder', function() {
}) })
}) })
}) })
describe('fromTransaction', function() {
fixtures.valid.build.forEach(function(f) {
it('builds the correct TransactionBuilder for ' + f.description, function() {
var tx = Transaction.fromHex(f.txhex)
var txb = TransactionBuilder.fromTransaction(tx)
assert.equal(txb.build().toHex(), f.txhex)
})
})
})
}) })