Skip to content

Commit da6aea4

Browse files
authored
Merge pull request bitcoinjs#615 from bitcoinjs/hltc
Add another CLTV test, isolate tests and cleanup
2 parents acbc8fc + 49564c2 commit da6aea4

File tree

5 files changed

+199
-139
lines changed

5 files changed

+199
-139
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Definitions for [Flow typechecker](https://flowtype.org/) are available in flow-
9191

9292
# npm install -g flow-typed
9393
$ flow-typed install -f 0.27 [email protected] # 0.27 for flow version, 2.2.0 for bitcoinjs-lib version
94-
94+
9595
The definitions are complete and up to date with version 2.2.0. The definitions are maintained by [@runn1ng](https://github.com/runn1ng).
9696

9797
## Examples
@@ -108,10 +108,13 @@ The below examples are implemented as integration tests, they should be very eas
108108
- [Create an OP RETURN transaction](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/advanced.js#L24)
109109
- [Create a 2-of-3 multisig P2SH address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/multisig.js#L9)
110110
- [Spend from a 2-of-4 multisig P2SH address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/multisig.js#L25)
111-
- [Generate a single-key stealth address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/crypto.js#L14)
112-
- [Generate a dual-key stealth address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/crypto.js#L53)
113-
- [Recover a BIP32 parent private key from the parent public key and a derived non-hardened child private key](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/crypto.js#L55)
114-
- [Recover a Private key from duplicate R values in a signature](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/crypto.js#L102)
111+
- [Generate a single-key stealth address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/stealth.js#L11)
112+
- [Generate a dual-key stealth address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/stealth.js#L48)
113+
- [Recover a BIP32 parent private key from the parent public key and a derived non-hardened child private key](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/crypto.js#L14)
114+
- [Recover a Private key from duplicate R values in a signature](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/crypto.js#L60)
115+
- [Create a CLTV locked transaction where the expiry is past](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.js#L36)
116+
- [Create a CLTV locked transaction where the parties bypass the expiry](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.js#L70)
117+
- [Create a CLTV locked transaction which fails due to expiry in the future](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.js#L102)
115118

116119
If you have a use case that you feel could be listed here, please [ask for it](https://github.com/bitcoinjs/bitcoinjs-lib/issues/new)!
117120

test/integration/advanced.js

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global describe, it, beforeEach */
1+
/* global describe, it */
22

33
var assert = require('assert')
44
var bitcoin = require('../../')
@@ -43,97 +43,4 @@ describe('bitcoinjs-lib (advanced)', function () {
4343
blockchain.t.transactions.propagate(txRaw.toHex(), done)
4444
})
4545
})
46-
47-
describe('can create transactions using OP_CHECKLOCKTIMEVERIFY', function (done) {
48-
var network = bitcoin.networks.testnet
49-
var alice = bitcoin.ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe', network)
50-
var bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x', network)
51-
52-
// now - 3 hours
53-
var threeHoursAgo = Math.floor(Date.now() / 1000) - (3600 * 3)
54-
var redeemScript = bitcoin.script.compile([
55-
bitcoin.opcodes.OP_IF,
56-
57-
bitcoin.script.number.encode(threeHoursAgo),
58-
bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
59-
bitcoin.opcodes.OP_DROP,
60-
61-
bitcoin.opcodes.OP_ELSE,
62-
63-
bob.getPublicKeyBuffer(),
64-
bitcoin.opcodes.OP_CHECKSIGVERIFY,
65-
66-
bitcoin.opcodes.OP_ENDIF,
67-
68-
alice.getPublicKeyBuffer(),
69-
bitcoin.opcodes.OP_CHECKSIG
70-
])
71-
var scriptPubKey = bitcoin.script.scriptHashOutput(bitcoin.crypto.hash160(redeemScript))
72-
73-
var txId
74-
beforeEach(function (done) {
75-
this.timeout(10000)
76-
77-
blockchain.t.faucet(alice.getAddress(), 2e4, function (err, unspent) {
78-
if (err) return done(err)
79-
80-
// build the transaction
81-
var tx = new bitcoin.TransactionBuilder(network)
82-
tx.addInput(unspent.txId, unspent.vout)
83-
tx.addOutput(scriptPubKey, 1e4)
84-
tx.sign(0, alice)
85-
var txRaw = tx.build()
86-
87-
txId = txRaw.getId()
88-
89-
blockchain.t.transactions.propagate(txRaw.toHex(), done)
90-
})
91-
})
92-
93-
// expiry past, {Alice's signature} OP_TRUE
94-
it('where Alice can redeem after the expiry is past', function (done) {
95-
this.timeout(30000)
96-
97-
var tx2 = new bitcoin.TransactionBuilder(network)
98-
tx2.setLockTime(threeHoursAgo)
99-
tx2.addInput(txId, 0, 0xfffffffe)
100-
tx2.addOutput(alice.getAddress(), 1000)
101-
102-
var tx2Raw = tx2.buildIncomplete()
103-
var hashType = bitcoin.Transaction.SIGHASH_ALL
104-
var signatureHash = tx2Raw.hashForSignature(0, redeemScript, hashType)
105-
var signature = alice.sign(signatureHash)
106-
107-
var redeemScriptSig = bitcoin.script.scriptHashInput([
108-
signature.toScriptSignature(hashType), bitcoin.opcodes.OP_TRUE
109-
], redeemScript)
110-
111-
tx2Raw.setInputScript(0, redeemScriptSig)
112-
113-
blockchain.t.transactions.propagate(tx2Raw.toHex(), done)
114-
})
115-
116-
// {Bob's signature} {Alice's signature} OP_FALSE
117-
it('where Alice and Bob can redeem at any time', function (done) {
118-
this.timeout(30000)
119-
120-
var tx2 = new bitcoin.TransactionBuilder(network)
121-
tx2.setLockTime(threeHoursAgo)
122-
tx2.addInput(txId, 0, 0xfffffffe)
123-
tx2.addOutput(alice.getAddress(), 1000)
124-
125-
var tx2Raw = tx2.buildIncomplete()
126-
var hashType = bitcoin.Transaction.SIGHASH_ALL
127-
var signatureHash = tx2Raw.hashForSignature(0, redeemScript, hashType)
128-
var signatureA = alice.sign(signatureHash)
129-
var signatureB = bob.sign(signatureHash)
130-
var redeemScriptSig = bitcoin.script.scriptHashInput([
131-
signatureA.toScriptSignature(hashType), signatureB.toScriptSignature(hashType), bitcoin.opcodes.OP_FALSE
132-
], redeemScript)
133-
134-
tx2Raw.setInputScript(0, redeemScriptSig)
135-
136-
blockchain.t.transactions.propagate(tx2Raw.toHex(), done)
137-
})
138-
})
13946
})

test/integration/cltv.js

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/* global describe, it */
2+
3+
var assert = require('assert')
4+
var bitcoin = require('../../')
5+
var blockchain = require('./_blockchain')
6+
7+
var network = bitcoin.networks.testnet
8+
var alice = bitcoin.ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe', network)
9+
var bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x', network)
10+
11+
describe('bitcoinjs-lib (CLTV)', function () {
12+
var hashType = bitcoin.Transaction.SIGHASH_ALL
13+
14+
function cltvCheckSigInput (aQ, bQ, utcSeconds) {
15+
return bitcoin.script.compile([
16+
bitcoin.opcodes.OP_IF,
17+
bitcoin.script.number.encode(utcSeconds),
18+
bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
19+
bitcoin.opcodes.OP_DROP,
20+
21+
bitcoin.opcodes.OP_ELSE,
22+
bQ.getPublicKeyBuffer(),
23+
bitcoin.opcodes.OP_CHECKSIGVERIFY,
24+
bitcoin.opcodes.OP_ENDIF,
25+
26+
aQ.getPublicKeyBuffer(),
27+
bitcoin.opcodes.OP_CHECKSIG
28+
])
29+
}
30+
31+
function utcNow () {
32+
return Math.floor(Date.now() / 1000)
33+
}
34+
35+
// expiry past, {Alice's signature} OP_TRUE
36+
it('where Alice can redeem after the expiry is past', function (done) {
37+
this.timeout(30000)
38+
39+
// three hours ago
40+
var timeUtc = utcNow() - (3600 * 3)
41+
var redeemScript = cltvCheckSigInput(alice, bob, timeUtc)
42+
var scriptPubKey = bitcoin.script.scriptHashOutput(bitcoin.crypto.hash160(redeemScript))
43+
var address = bitcoin.address.fromOutputScript(scriptPubKey, network)
44+
45+
// fund the P2SH(CLTV) address
46+
blockchain.t.faucet(address, 2e4, function (err, unspent) {
47+
if (err) return done(err)
48+
49+
var tx = new bitcoin.TransactionBuilder(network)
50+
tx.setLockTime(timeUtc)
51+
tx.addInput(unspent.txId, 0, 0xfffffffe)
52+
tx.addOutput(alice.getAddress(), 1000)
53+
54+
var txRaw = tx.buildIncomplete()
55+
var signatureHash = txRaw.hashForSignature(0, redeemScript, hashType)
56+
57+
// {Alice's signature} OP_TRUE
58+
var redeemScriptSig = bitcoin.script.scriptHashInput([
59+
alice.sign(signatureHash).toScriptSignature(hashType),
60+
bitcoin.opcodes.OP_TRUE
61+
], redeemScript)
62+
63+
txRaw.setInputScript(0, redeemScriptSig)
64+
65+
blockchain.t.transactions.propagate(txRaw.toHex(), done)
66+
})
67+
})
68+
69+
// expiry ignored, {Bob's signature} {Alice's signature} OP_FALSE
70+
it('where Alice and Bob can redeem at any time', function (done) {
71+
this.timeout(30000)
72+
73+
// two hours ago
74+
var timeUtc = utcNow() - (3600 * 2)
75+
var redeemScript = cltvCheckSigInput(alice, bob, timeUtc)
76+
var scriptPubKey = bitcoin.script.scriptHashOutput(bitcoin.crypto.hash160(redeemScript))
77+
var address = bitcoin.address.fromOutputScript(scriptPubKey, network)
78+
79+
// fund the P2SH(CLTV) address
80+
blockchain.t.faucet(address, 2e4, function (err, unspent) {
81+
if (err) return done(err)
82+
83+
var tx = new bitcoin.TransactionBuilder(network)
84+
tx.addInput(unspent.txId, 0, 0xfffffffe)
85+
tx.addOutput(alice.getAddress(), 1000)
86+
87+
var txRaw = tx.buildIncomplete()
88+
var signatureHash = txRaw.hashForSignature(0, redeemScript, hashType)
89+
var redeemScriptSig = bitcoin.script.scriptHashInput([
90+
alice.sign(signatureHash).toScriptSignature(hashType),
91+
bob.sign(signatureHash).toScriptSignature(hashType),
92+
bitcoin.opcodes.OP_FALSE
93+
], redeemScript)
94+
95+
txRaw.setInputScript(0, redeemScriptSig)
96+
97+
blockchain.t.transactions.propagate(txRaw.toHex(), done)
98+
})
99+
})
100+
101+
// expiry in the future, {Alice's signature} OP_TRUE
102+
it('fails when still time-locked', function (done) {
103+
this.timeout(30000)
104+
105+
// two hours from now
106+
var timeUtc = utcNow() + (3600 * 2)
107+
var redeemScript = cltvCheckSigInput(alice, bob, timeUtc)
108+
var scriptPubKey = bitcoin.script.scriptHashOutput(bitcoin.crypto.hash160(redeemScript))
109+
var address = bitcoin.address.fromOutputScript(scriptPubKey, network)
110+
111+
// fund the P2SH(CLTV) address
112+
blockchain.t.faucet(address, 2e4, function (err, unspent) {
113+
if (err) return done(err)
114+
115+
var tx = new bitcoin.TransactionBuilder(network)
116+
tx.setLockTime(timeUtc)
117+
tx.addInput(unspent.txId, 0, 0xfffffffe)
118+
tx.addOutput(alice.getAddress(), 1000)
119+
120+
var txRaw = tx.buildIncomplete()
121+
var signatureHash = txRaw.hashForSignature(0, redeemScript, hashType)
122+
123+
// {Alice's signature} OP_TRUE
124+
var redeemScriptSig = bitcoin.script.scriptHashInput([
125+
alice.sign(signatureHash).toScriptSignature(hashType),
126+
bitcoin.opcodes.OP_TRUE
127+
], redeemScript)
128+
129+
txRaw.setInputScript(0, redeemScriptSig)
130+
131+
blockchain.t.transactions.propagate(txRaw.toHex(), function (err) {
132+
assert.throws(function () {
133+
if (err) throw err
134+
}, /Error: 64: non-final/)
135+
136+
done()
137+
})
138+
})
139+
})
140+
})

test/integration/crypto.js

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,7 @@ var ecurve = require('ecurve')
1111
var secp256k1 = ecurve.getCurveByName('secp256k1')
1212

1313
describe('bitcoinjs-lib (crypto)', function () {
14-
it('can generate a single-key stealth address', function () {
15-
var G = secp256k1.G
16-
var n = secp256k1.n
17-
18-
function stealthSend (Q) {
19-
var noncePair = bitcoin.ECPair.makeRandom()
20-
var e = noncePair.d
21-
var eQ = Q.multiply(e) // shared secret
22-
var c = bigi.fromBuffer(bitcoin.crypto.sha256(eQ.getEncoded()))
23-
var cG = G.multiply(c)
24-
var Qprime = Q.add(cG)
25-
26-
return {
27-
shared: new bitcoin.ECPair(null, Qprime),
28-
nonce: noncePair.Q
29-
}
30-
}
31-
32-
function stealthReceive (d, P) {
33-
var dP = P.multiply(d) // shared secret
34-
var c = bigi.fromBuffer(bitcoin.crypto.sha256(dP.getEncoded()))
35-
return new bitcoin.ECPair(d.add(c).mod(n))
36-
}
37-
38-
// receiver private key
39-
var receiver = bitcoin.ECPair.fromWIF('5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss')
40-
41-
var stealthS = stealthSend(receiver.Q) // public, done by sender
42-
// ... sender now reveals nonce to receiver
43-
44-
var stealthR = stealthReceive(receiver.d, stealthS.nonce) // private, done by receiver
45-
46-
// and check that we derived both sides correctly
47-
assert.equal(stealthS.shared.getAddress(), stealthR.getAddress())
48-
})
49-
50-
// TODO
51-
it.skip('can generate a dual-key stealth address', function () {})
52-
53-
it("can recover a parent private key from the parent's public key and a derived non-hardened child private key", function () {
14+
it('can recover a BIP32 parent private key from the parent public key, and a derived, non-hardened child private key', function () {
5415
function recoverParent (master, child) {
5516
assert(!master.keyPair.d, 'You already have the parent private key')
5617
assert(child.keyPair.d, 'Missing child private key')

test/integration/stealth.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* global describe, it */
2+
3+
var assert = require('assert')
4+
var bigi = require('bigi')
5+
var bitcoin = require('../../')
6+
7+
var ecurve = require('ecurve')
8+
var secp256k1 = ecurve.getCurveByName('secp256k1')
9+
10+
describe('bitcoinjs-lib (crypto)', function () {
11+
it('can generate a single-key stealth address', function () {
12+
var G = secp256k1.G
13+
var n = secp256k1.n
14+
15+
function stealthSend (Q) {
16+
var noncePair = bitcoin.ECPair.makeRandom()
17+
var e = noncePair.d
18+
var eQ = Q.multiply(e) // shared secret
19+
var c = bigi.fromBuffer(bitcoin.crypto.sha256(eQ.getEncoded()))
20+
var cG = G.multiply(c)
21+
var Qprime = Q.add(cG)
22+
23+
return {
24+
shared: new bitcoin.ECPair(null, Qprime),
25+
nonce: noncePair.Q
26+
}
27+
}
28+
29+
function stealthReceive (d, P) {
30+
var dP = P.multiply(d) // shared secret
31+
var c = bigi.fromBuffer(bitcoin.crypto.sha256(dP.getEncoded()))
32+
return new bitcoin.ECPair(d.add(c).mod(n))
33+
}
34+
35+
// receiver private key
36+
var receiver = bitcoin.ECPair.fromWIF('5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss')
37+
38+
var stealthS = stealthSend(receiver.Q) // public, done by sender
39+
// ... sender now reveals nonce to receiver
40+
41+
var stealthR = stealthReceive(receiver.d, stealthS.nonce) // private, done by receiver
42+
43+
// and check that we derived both sides correctly
44+
assert.equal(stealthS.shared.getAddress(), stealthR.getAddress())
45+
})
46+
47+
// TODO
48+
it.skip('can generate a dual-key stealth address', function () {})
49+
})

0 commit comments

Comments
 (0)