Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

txbuilder: add setLockTime #507

Merged
merged 3 commits into from
Jan 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/transaction_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var bscript = require('./script')
var bufferEquals = require('buffer-equals')
var networks = require('./networks')
var ops = require('./opcodes')
var typeforce = require('typeforce')
var types = require('./types')

var ECPair = require('./ecpair')
var ECSignature = require('./ecsignature')
Expand Down Expand Up @@ -135,6 +137,21 @@ function TransactionBuilder (network) {
this.tx = new Transaction()
}

TransactionBuilder.prototype.setLockTime = function (locktime) {
typeforce(types.UInt32, locktime)

// if any signatures exist, throw
if (this.inputs.some(function (input) {
if (!input.signatures) return false

return input.signatures.some(function (s) { return s })
})) {
throw new Error('No, this would invalidate signatures')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still find this error message funny because it answers "no" without any question asked :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, I know. But, it works.
If you can think of something for 3.0.0, go for it :)

}

this.tx.locktime = locktime
}

TransactionBuilder.fromTransaction = function (transaction, network) {
var txb = new TransactionBuilder(network)

Expand Down
22 changes: 22 additions & 0 deletions test/fixtures/transaction_builder.json
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,28 @@
"value": 10000
}
]
},
{
"description": "Transaction w/ nLockTime, pubKeyHash -> pubKeyHash",
"txHex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000006b483045022100a3b254e1c10b5d039f36c05f323995d6e5a367d98dd78a13d5bbc3991b35720e022022fccea3897d594de0689601fbd486588d5bfa6915be2386db0397ee9a6e80b601210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588acffff0000",
"locktime": 65535,
"inputs": [
{
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"vout": 0,
"signs": [
{
"keyPair": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
}
]
}
],
"outputs": [
{
"script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
"value": 10000
}
]
}
],
"fromTransaction": [
Expand Down
8 changes: 2 additions & 6 deletions test/integration/advanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,11 @@ describe('bitcoinjs-lib (advanced)', function () {
this.timeout(30000)

var tx2 = new bitcoin.TransactionBuilder(network)
// tx2.setLockTime(threeHoursAgo) // TODO
tx2.setLockTime(threeHoursAgo)
tx2.addInput(txId, 0, 0xfffffffe)
tx2.addOutput(alice.getAddress(), 1000)

var tx2Raw = tx2.buildIncomplete()
tx2Raw.locktime = threeHoursAgo // TODO

var hashType = bitcoin.Transaction.SIGHASH_ALL
var signatureHash = tx2Raw.hashForSignature(0, redeemScript, hashType)
var signature = alice.sign(signatureHash)
Expand All @@ -120,13 +118,11 @@ describe('bitcoinjs-lib (advanced)', function () {
this.timeout(30000)

var tx2 = new bitcoin.TransactionBuilder(network)
// tx2.setLockTime(threeHoursAgo) // TODO
tx2.setLockTime(threeHoursAgo)
tx2.addInput(txId, 0, 0xfffffffe)
tx2.addOutput(alice.getAddress(), 1000)

var tx2Raw = tx2.buildIncomplete()
tx2Raw.locktime = threeHoursAgo // TODO

var hashType = bitcoin.Transaction.SIGHASH_ALL
var signatureHash = tx2Raw.hashForSignature(0, redeemScript, hashType)
var signatureA = alice.sign(signatureHash)
Expand Down
15 changes: 13 additions & 2 deletions test/transaction_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ function construct (f, sign) {
var network = NETWORKS[f.network]
var txb = new TransactionBuilder(network)

// FIXME: add support for locktime/version in TransactionBuilder API
// FIXME: add support for version in TransactionBuilder API
if (f.version !== undefined) {
txb.tx.version = f.version
}

if (f.locktime !== undefined) {
txb.tx.locktime = f.locktime
txb.setLockTime(f.locktime)
}

f.inputs.forEach(function (input) {
Expand Down Expand Up @@ -241,6 +241,17 @@ describe('TransactionBuilder', function () {
})
})

describe('setLockTime', function () {
it('throws if if there exist any scriptSigs', function () {
txb.addInput(txHash, 0)
txb.sign(0, keyPair)

assert.throws(function () {
txb.setLockTime(65535)
}, /No, this would invalidate signatures/)
})
})

describe('sign', function () {
fixtures.invalid.sign.forEach(function (f) {
it('throws on ' + f.exception + (f.description ? ' (' + f.description + ')' : ''), function () {
Expand Down