2019-09-07 06:42:03 +02:00
|
|
|
import * as assert from 'assert';
|
2021-10-20 12:18:01 +02:00
|
|
|
import { ECPair } from 'ecpair';
|
2019-09-07 06:42:03 +02:00
|
|
|
import { before, describe, it } from 'mocha';
|
|
|
|
import * as bitcoin from '../..';
|
|
|
|
import { regtestUtils } from './_regtest';
|
|
|
|
const regtest = regtestUtils.network;
|
|
|
|
const bip65 = require('bip65');
|
|
|
|
|
2019-09-12 06:15:52 +02:00
|
|
|
function toOutputScript(address: string): Buffer {
|
|
|
|
return bitcoin.address.toOutputScript(address, regtest);
|
|
|
|
}
|
|
|
|
|
|
|
|
function idToHash(txid: string): Buffer {
|
2019-09-12 08:20:33 +02:00
|
|
|
return Buffer.from(txid, 'hex').reverse();
|
2019-09-12 06:15:52 +02:00
|
|
|
}
|
|
|
|
|
2021-10-20 12:18:01 +02:00
|
|
|
const alice = ECPair.fromWIF(
|
2019-09-07 06:42:03 +02:00
|
|
|
'cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe',
|
|
|
|
regtest,
|
|
|
|
);
|
2021-10-20 12:18:01 +02:00
|
|
|
const bob = ECPair.fromWIF(
|
2019-09-07 06:42:03 +02:00
|
|
|
'cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x',
|
|
|
|
regtest,
|
|
|
|
);
|
2016-08-13 03:42:16 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
|
2018-05-30 02:23:55 +02:00
|
|
|
// force update MTP
|
2019-04-09 08:09:50 +02:00
|
|
|
before(async () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
await regtestUtils.mine(11);
|
|
|
|
});
|
|
|
|
|
|
|
|
const hashType = bitcoin.Transaction.SIGHASH_ALL;
|
|
|
|
|
2019-09-12 10:35:08 +02:00
|
|
|
interface KeyPair {
|
|
|
|
publicKey: Buffer;
|
|
|
|
}
|
|
|
|
function cltvCheckSigOutput(
|
|
|
|
aQ: KeyPair,
|
|
|
|
bQ: KeyPair,
|
|
|
|
lockTime: number,
|
|
|
|
): Buffer {
|
2019-09-07 06:42:03 +02:00
|
|
|
return bitcoin.script.fromASM(
|
|
|
|
`
|
|
|
|
OP_IF
|
|
|
|
${bitcoin.script.number.encode(lockTime).toString('hex')}
|
|
|
|
OP_CHECKLOCKTIMEVERIFY
|
|
|
|
OP_DROP
|
|
|
|
OP_ELSE
|
|
|
|
${bQ.publicKey.toString('hex')}
|
|
|
|
OP_CHECKSIGVERIFY
|
|
|
|
OP_ENDIF
|
|
|
|
${aQ.publicKey.toString('hex')}
|
|
|
|
OP_CHECKSIG
|
|
|
|
`
|
|
|
|
.trim()
|
|
|
|
.replace(/\s+/g, ' '),
|
|
|
|
);
|
2016-08-13 03:42:16 +02:00
|
|
|
}
|
|
|
|
|
2019-09-12 10:35:08 +02:00
|
|
|
function utcNow(): number {
|
2019-09-07 06:42:03 +02:00
|
|
|
return Math.floor(Date.now() / 1000);
|
2016-08-13 03:42:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// expiry past, {Alice's signature} OP_TRUE
|
2019-09-12 10:35:08 +02:00
|
|
|
it(
|
|
|
|
'can create (and broadcast via 3PBP) a Transaction where Alice can redeem ' +
|
|
|
|
'the output after the expiry (in the past)',
|
|
|
|
async () => {
|
|
|
|
// 3 hours ago
|
|
|
|
const lockTime = bip65.encode({ utc: utcNow() - 3600 * 3 });
|
|
|
|
const redeemScript = cltvCheckSigOutput(alice, bob, lockTime);
|
|
|
|
const { address } = bitcoin.payments.p2sh({
|
|
|
|
redeem: { output: redeemScript, network: regtest },
|
|
|
|
network: regtest,
|
|
|
|
});
|
|
|
|
|
|
|
|
// fund the P2SH(CLTV) address
|
|
|
|
const unspent = await regtestUtils.faucet(address!, 1e5);
|
|
|
|
const tx = new bitcoin.Transaction();
|
|
|
|
tx.locktime = lockTime;
|
|
|
|
// Note: nSequence MUST be <= 0xfffffffe otherwise LockTime is ignored, and is immediately spendable.
|
|
|
|
tx.addInput(idToHash(unspent.txId), unspent.vout, 0xfffffffe);
|
|
|
|
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 7e4);
|
|
|
|
|
|
|
|
// {Alice's signature} OP_TRUE
|
|
|
|
const signatureHash = tx.hashForSignature(0, redeemScript, hashType);
|
|
|
|
const redeemScriptSig = bitcoin.payments.p2sh({
|
|
|
|
redeem: {
|
|
|
|
input: bitcoin.script.compile([
|
|
|
|
bitcoin.script.signature.encode(
|
|
|
|
alice.sign(signatureHash),
|
|
|
|
hashType,
|
|
|
|
),
|
|
|
|
bitcoin.opcodes.OP_TRUE,
|
|
|
|
]),
|
|
|
|
output: redeemScript,
|
|
|
|
},
|
|
|
|
}).input;
|
|
|
|
tx.setInputScript(0, redeemScriptSig!);
|
|
|
|
|
|
|
|
await regtestUtils.broadcast(tx.toHex());
|
|
|
|
|
|
|
|
await regtestUtils.verify({
|
|
|
|
txId: tx.getId(),
|
|
|
|
address: regtestUtils.RANDOM_ADDRESS,
|
|
|
|
vout: 0,
|
|
|
|
value: 7e4,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2018-01-31 02:46:35 +01:00
|
|
|
|
|
|
|
// expiry will pass, {Alice's signature} OP_TRUE
|
2019-09-12 10:35:08 +02:00
|
|
|
it(
|
|
|
|
'can create (and broadcast via 3PBP) a Transaction where Alice can redeem ' +
|
|
|
|
'the output after the expiry (in the future)',
|
|
|
|
async () => {
|
|
|
|
const height = await regtestUtils.height();
|
|
|
|
// 5 blocks from now
|
|
|
|
const lockTime = bip65.encode({ blocks: height + 5 });
|
|
|
|
const redeemScript = cltvCheckSigOutput(alice, bob, lockTime);
|
|
|
|
const { address } = bitcoin.payments.p2sh({
|
|
|
|
redeem: { output: redeemScript, network: regtest },
|
|
|
|
network: regtest,
|
|
|
|
});
|
|
|
|
|
|
|
|
// fund the P2SH(CLTV) address
|
|
|
|
const unspent = await regtestUtils.faucet(address!, 1e5);
|
|
|
|
const tx = new bitcoin.Transaction();
|
|
|
|
tx.locktime = lockTime;
|
|
|
|
// Note: nSequence MUST be <= 0xfffffffe otherwise LockTime is ignored, and is immediately spendable.
|
|
|
|
tx.addInput(idToHash(unspent.txId), unspent.vout, 0xfffffffe);
|
|
|
|
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 7e4);
|
|
|
|
|
|
|
|
// {Alice's signature} OP_TRUE
|
|
|
|
const signatureHash = tx.hashForSignature(0, redeemScript, hashType);
|
|
|
|
const redeemScriptSig = bitcoin.payments.p2sh({
|
|
|
|
redeem: {
|
|
|
|
input: bitcoin.script.compile([
|
|
|
|
bitcoin.script.signature.encode(
|
|
|
|
alice.sign(signatureHash),
|
|
|
|
hashType,
|
|
|
|
),
|
|
|
|
bitcoin.opcodes.OP_TRUE,
|
|
|
|
]),
|
|
|
|
output: redeemScript,
|
|
|
|
},
|
|
|
|
}).input;
|
|
|
|
tx.setInputScript(0, redeemScriptSig!);
|
|
|
|
|
|
|
|
// TODO: test that it failures _prior_ to expiry, unfortunately, race conditions when run concurrently
|
|
|
|
// ...
|
|
|
|
// into the future!
|
|
|
|
await regtestUtils.mine(5);
|
|
|
|
await regtestUtils.broadcast(tx.toHex());
|
|
|
|
await regtestUtils.verify({
|
|
|
|
txId: tx.getId(),
|
|
|
|
address: regtestUtils.RANDOM_ADDRESS,
|
|
|
|
vout: 0,
|
|
|
|
value: 7e4,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2016-08-13 03:42:16 +02:00
|
|
|
|
|
|
|
// expiry ignored, {Bob's signature} {Alice's signature} OP_FALSE
|
2019-09-12 10:35:08 +02:00
|
|
|
it(
|
|
|
|
'can create (and broadcast via 3PBP) a Transaction where Alice and Bob can ' +
|
|
|
|
'redeem the output at any time',
|
|
|
|
async () => {
|
|
|
|
// two hours ago
|
|
|
|
const lockTime = bip65.encode({ utc: utcNow() - 3600 * 2 });
|
|
|
|
const redeemScript = cltvCheckSigOutput(alice, bob, lockTime);
|
|
|
|
const { address } = bitcoin.payments.p2sh({
|
|
|
|
redeem: { output: redeemScript, network: regtest },
|
|
|
|
network: regtest,
|
|
|
|
});
|
|
|
|
|
|
|
|
// fund the P2SH(CLTV) address
|
|
|
|
const unspent = await regtestUtils.faucet(address!, 2e5);
|
|
|
|
const tx = new bitcoin.Transaction();
|
|
|
|
tx.locktime = lockTime;
|
|
|
|
// Note: nSequence MUST be <= 0xfffffffe otherwise LockTime is ignored, and is immediately spendable.
|
|
|
|
tx.addInput(idToHash(unspent.txId), unspent.vout, 0xfffffffe);
|
|
|
|
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 8e4);
|
|
|
|
|
|
|
|
// {Alice's signature} {Bob's signature} OP_FALSE
|
|
|
|
const signatureHash = tx.hashForSignature(0, redeemScript, hashType);
|
|
|
|
const redeemScriptSig = bitcoin.payments.p2sh({
|
|
|
|
redeem: {
|
|
|
|
input: bitcoin.script.compile([
|
|
|
|
bitcoin.script.signature.encode(
|
|
|
|
alice.sign(signatureHash),
|
|
|
|
hashType,
|
|
|
|
),
|
|
|
|
bitcoin.script.signature.encode(bob.sign(signatureHash), hashType),
|
|
|
|
bitcoin.opcodes.OP_FALSE,
|
|
|
|
]),
|
|
|
|
output: redeemScript,
|
|
|
|
},
|
|
|
|
}).input;
|
|
|
|
tx.setInputScript(0, redeemScriptSig!);
|
|
|
|
|
|
|
|
await regtestUtils.broadcast(tx.toHex());
|
|
|
|
await regtestUtils.verify({
|
|
|
|
txId: tx.getId(),
|
|
|
|
address: regtestUtils.RANDOM_ADDRESS,
|
|
|
|
vout: 0,
|
|
|
|
value: 8e4,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2016-08-13 03:42:16 +02:00
|
|
|
|
|
|
|
// expiry in the future, {Alice's signature} OP_TRUE
|
2019-09-12 10:35:08 +02:00
|
|
|
it(
|
|
|
|
'can create (but fail to broadcast via 3PBP) a Transaction where Alice ' +
|
|
|
|
'attempts to redeem before the expiry',
|
|
|
|
async () => {
|
|
|
|
// two hours from now
|
|
|
|
const lockTime = bip65.encode({ utc: utcNow() + 3600 * 2 });
|
|
|
|
const redeemScript = cltvCheckSigOutput(alice, bob, lockTime);
|
|
|
|
const { address } = bitcoin.payments.p2sh({
|
|
|
|
redeem: { output: redeemScript, network: regtest },
|
|
|
|
network: regtest,
|
|
|
|
});
|
|
|
|
|
|
|
|
// fund the P2SH(CLTV) address
|
|
|
|
const unspent = await regtestUtils.faucet(address!, 2e4);
|
|
|
|
const tx = new bitcoin.Transaction();
|
|
|
|
tx.locktime = lockTime;
|
|
|
|
// Note: nSequence MUST be <= 0xfffffffe otherwise LockTime is ignored, and is immediately spendable.
|
|
|
|
tx.addInput(idToHash(unspent.txId), unspent.vout, 0xfffffffe);
|
|
|
|
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 1e4);
|
|
|
|
|
|
|
|
// {Alice's signature} OP_TRUE
|
|
|
|
const signatureHash = tx.hashForSignature(0, redeemScript, hashType);
|
|
|
|
const redeemScriptSig = bitcoin.payments.p2sh({
|
|
|
|
redeem: {
|
|
|
|
input: bitcoin.script.compile([
|
|
|
|
bitcoin.script.signature.encode(
|
|
|
|
alice.sign(signatureHash),
|
|
|
|
hashType,
|
|
|
|
),
|
|
|
|
bitcoin.script.signature.encode(bob.sign(signatureHash), hashType),
|
|
|
|
bitcoin.opcodes.OP_TRUE,
|
|
|
|
]),
|
|
|
|
output: redeemScript,
|
|
|
|
},
|
|
|
|
}).input;
|
|
|
|
tx.setInputScript(0, redeemScriptSig!);
|
|
|
|
|
|
|
|
await regtestUtils.broadcast(tx.toHex()).catch(err => {
|
|
|
|
assert.throws(() => {
|
|
|
|
if (err) throw err;
|
2021-11-11 13:18:41 +01:00
|
|
|
}, /Error: non-final/);
|
2019-09-12 10:35:08 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|