2019-09-07 06:42:03 +02:00
|
|
|
import * as assert from 'assert';
|
2019-10-28 04:27:35 +01:00
|
|
|
import { PsbtInput } from 'bip174/src/lib/interfaces';
|
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 bip68 = require('bip68');
|
2019-10-28 04:27:35 +01:00
|
|
|
const varuint = require('varuint-bitcoin');
|
2019-09-07 06:42:03 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
const alice = bitcoin.ECPair.fromWIF(
|
|
|
|
'cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe',
|
|
|
|
regtest,
|
|
|
|
);
|
|
|
|
const bob = bitcoin.ECPair.fromWIF(
|
|
|
|
'cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x',
|
|
|
|
regtest,
|
|
|
|
);
|
|
|
|
const charles = bitcoin.ECPair.fromWIF(
|
|
|
|
'cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsMSb4Ubnf',
|
|
|
|
regtest,
|
|
|
|
);
|
|
|
|
const dave = bitcoin.ECPair.fromWIF(
|
|
|
|
'cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsMwS4pqnx',
|
|
|
|
regtest,
|
|
|
|
);
|
2017-12-01 03:13:55 +01:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
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);
|
|
|
|
});
|
2018-05-30 02:23:55 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
const hashType = bitcoin.Transaction.SIGHASH_ALL;
|
2017-12-01 03:13:55 +01:00
|
|
|
|
2019-09-12 10:35:08 +02:00
|
|
|
interface KeyPair {
|
|
|
|
publicKey: Buffer;
|
|
|
|
}
|
2019-05-21 09:27:42 +02:00
|
|
|
// IF MTP (from when confirmed) > seconds, _alice can redeem
|
2019-09-12 10:35:08 +02:00
|
|
|
function csvCheckSigOutput(
|
|
|
|
_alice: KeyPair,
|
|
|
|
_bob: KeyPair,
|
|
|
|
sequence: number,
|
|
|
|
): Buffer {
|
2019-09-07 06:42:03 +02:00
|
|
|
return bitcoin.script.fromASM(
|
|
|
|
`
|
2018-09-03 09:54:56 +02:00
|
|
|
OP_IF
|
|
|
|
${bitcoin.script.number.encode(sequence).toString('hex')}
|
|
|
|
OP_CHECKSEQUENCEVERIFY
|
|
|
|
OP_DROP
|
|
|
|
OP_ELSE
|
2019-05-21 09:26:00 +02:00
|
|
|
${_bob.publicKey.toString('hex')}
|
2018-09-03 09:54:56 +02:00
|
|
|
OP_CHECKSIGVERIFY
|
|
|
|
OP_ENDIF
|
2019-05-21 09:26:00 +02:00
|
|
|
${_alice.publicKey.toString('hex')}
|
2018-09-03 09:54:56 +02:00
|
|
|
OP_CHECKSIG
|
2019-09-07 06:42:03 +02:00
|
|
|
`
|
|
|
|
.trim()
|
|
|
|
.replace(/\s+/g, ' '),
|
|
|
|
);
|
2017-12-01 03:13:55 +01:00
|
|
|
}
|
|
|
|
|
2019-05-21 09:27:42 +02:00
|
|
|
// 2 of 3 multisig of _bob, _charles, _dave,
|
|
|
|
// but after sequence1 time, _alice can allow the multisig to become 1 of 3.
|
|
|
|
// but after sequence2 time, _alice can sign for the output all by themself.
|
2019-09-12 10:35:08 +02:00
|
|
|
|
|
|
|
/* tslint:disable-next-line */
|
2018-09-03 08:53:09 +02:00
|
|
|
// Ref: https://github.com/bitcoinbook/bitcoinbook/blob/f8b883dcd4e3d1b9adf40fed59b7e898fbd9241f/ch07.asciidoc#complex-script-example
|
2019-09-12 10:35:08 +02:00
|
|
|
|
2018-09-03 08:53:09 +02:00
|
|
|
// Note: bitcoinjs-lib will not offer specific support for problems with
|
|
|
|
// advanced script usages such as below. Use at your own risk.
|
2019-09-07 06:42:03 +02:00
|
|
|
function complexCsvOutput(
|
2019-09-12 10:35:08 +02:00
|
|
|
_alice: KeyPair,
|
|
|
|
_bob: KeyPair,
|
|
|
|
_charles: KeyPair,
|
|
|
|
_dave: KeyPair,
|
2019-09-07 06:42:03 +02:00
|
|
|
sequence1: number,
|
|
|
|
sequence2: number,
|
2019-09-12 10:35:08 +02:00
|
|
|
): Buffer {
|
2019-09-07 06:42:03 +02:00
|
|
|
return bitcoin.script.fromASM(
|
|
|
|
`
|
2018-09-03 09:54:56 +02:00
|
|
|
OP_IF
|
|
|
|
OP_IF
|
|
|
|
OP_2
|
|
|
|
OP_ELSE
|
|
|
|
${bitcoin.script.number.encode(sequence1).toString('hex')}
|
|
|
|
OP_CHECKSEQUENCEVERIFY
|
|
|
|
OP_DROP
|
2019-05-21 09:26:00 +02:00
|
|
|
${_alice.publicKey.toString('hex')}
|
2018-09-03 09:54:56 +02:00
|
|
|
OP_CHECKSIGVERIFY
|
|
|
|
OP_1
|
|
|
|
OP_ENDIF
|
2019-05-21 09:26:00 +02:00
|
|
|
${_bob.publicKey.toString('hex')}
|
|
|
|
${_charles.publicKey.toString('hex')}
|
|
|
|
${_dave.publicKey.toString('hex')}
|
2018-09-03 09:54:56 +02:00
|
|
|
OP_3
|
|
|
|
OP_CHECKMULTISIG
|
|
|
|
OP_ELSE
|
|
|
|
${bitcoin.script.number.encode(sequence2).toString('hex')}
|
|
|
|
OP_CHECKSEQUENCEVERIFY
|
|
|
|
OP_DROP
|
2019-05-21 09:26:00 +02:00
|
|
|
${_alice.publicKey.toString('hex')}
|
2018-09-03 09:54:56 +02:00
|
|
|
OP_CHECKSIG
|
|
|
|
OP_ENDIF
|
2019-09-07 06:42:03 +02:00
|
|
|
`
|
|
|
|
.trim()
|
|
|
|
.replace(/\s+/g, ' '),
|
|
|
|
);
|
2018-08-22 05:23:56 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 03:13:55 +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) (simple CHECKSEQUENCEVERIFY)',
|
|
|
|
async () => {
|
|
|
|
// 5 blocks from now
|
|
|
|
const sequence = bip68.encode({ blocks: 5 });
|
|
|
|
const p2sh = bitcoin.payments.p2sh({
|
|
|
|
redeem: {
|
|
|
|
output: csvCheckSigOutput(alice, bob, sequence),
|
|
|
|
},
|
2019-04-07 15:27:16 +02:00
|
|
|
network: regtest,
|
2019-09-12 10:35:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// fund the P2SH(CSV) address
|
|
|
|
const unspent = await regtestUtils.faucet(p2sh.address!, 1e5);
|
2019-10-28 04:27:35 +01:00
|
|
|
const utx = await regtestUtils.fetch(unspent.txId);
|
|
|
|
// for non segwit inputs, you must pass the full transaction buffer
|
|
|
|
const nonWitnessUtxo = Buffer.from(utx.txHex, 'hex');
|
|
|
|
|
|
|
|
// This is an example of using the finalizeInput second parameter to
|
|
|
|
// define how you finalize the inputs, allowing for any type of script.
|
|
|
|
const tx = new bitcoin.Psbt({ network: regtest })
|
|
|
|
.setVersion(2)
|
|
|
|
.addInput({
|
|
|
|
hash: unspent.txId,
|
|
|
|
index: unspent.vout,
|
|
|
|
sequence,
|
|
|
|
redeemScript: p2sh.redeem!.output!,
|
|
|
|
nonWitnessUtxo,
|
|
|
|
})
|
|
|
|
.addOutput({
|
|
|
|
address: regtestUtils.RANDOM_ADDRESS,
|
|
|
|
value: 7e4,
|
|
|
|
})
|
|
|
|
.signInput(0, alice)
|
|
|
|
.finalizeInput(0, csvGetFinalScripts) // See csvGetFinalScripts below
|
|
|
|
.extractTransaction();
|
2019-09-12 10:35:08 +02:00
|
|
|
|
|
|
|
// TODO: test that it failures _prior_ to expiry, unfortunately, race conditions when run concurrently
|
|
|
|
// ...
|
|
|
|
// into the future!
|
|
|
|
await regtestUtils.mine(10);
|
|
|
|
|
|
|
|
await regtestUtils.broadcast(tx.toHex());
|
|
|
|
|
|
|
|
await regtestUtils.verify({
|
|
|
|
txId: tx.getId(),
|
|
|
|
address: regtestUtils.RANDOM_ADDRESS,
|
|
|
|
vout: 0,
|
|
|
|
value: 7e4,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2017-12-01 03:13:55 +01: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 (simple CHECKSEQUENCEVERIFY)',
|
|
|
|
async () => {
|
|
|
|
// two hours after confirmation
|
|
|
|
const sequence = bip68.encode({ seconds: 7168 });
|
|
|
|
const p2sh = bitcoin.payments.p2sh({
|
2018-07-03 14:54:24 +02:00
|
|
|
network: regtest,
|
2019-09-12 10:35:08 +02:00
|
|
|
redeem: {
|
|
|
|
output: csvCheckSigOutput(alice, bob, sequence),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// fund the P2SH(CSV) address
|
|
|
|
const unspent = await regtestUtils.faucet(p2sh.address!, 2e4);
|
|
|
|
|
|
|
|
const tx = new bitcoin.Transaction();
|
|
|
|
tx.version = 2;
|
|
|
|
tx.addInput(idToHash(unspent.txId), unspent.vout, sequence);
|
|
|
|
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 1e4);
|
|
|
|
|
|
|
|
// {Alice's signature} OP_TRUE
|
|
|
|
const signatureHash = tx.hashForSignature(
|
|
|
|
0,
|
|
|
|
p2sh.redeem!.output!,
|
|
|
|
hashType,
|
|
|
|
);
|
|
|
|
const redeemScriptSig = bitcoin.payments.p2sh({
|
|
|
|
network: regtest,
|
|
|
|
redeem: {
|
|
|
|
network: regtest,
|
|
|
|
output: p2sh.redeem!.output,
|
|
|
|
input: bitcoin.script.compile([
|
|
|
|
bitcoin.script.signature.encode(
|
|
|
|
alice.sign(signatureHash),
|
|
|
|
hashType,
|
|
|
|
),
|
|
|
|
bitcoin.script.signature.encode(bob.sign(signatureHash), hashType),
|
|
|
|
bitcoin.opcodes.OP_TRUE,
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
}).input;
|
|
|
|
tx.setInputScript(0, redeemScriptSig!);
|
|
|
|
|
|
|
|
await regtestUtils.broadcast(tx.toHex()).catch(err => {
|
|
|
|
assert.throws(() => {
|
|
|
|
if (err) throw err;
|
|
|
|
}, /Error: non-BIP68-final \(code 64\)/);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2018-08-22 05:23:56 +02:00
|
|
|
|
|
|
|
// Check first combination of complex CSV, 2 of 3
|
2019-09-12 10:35:08 +02:00
|
|
|
it(
|
|
|
|
'can create (and broadcast via 3PBP) a Transaction where Bob and Charles ' +
|
|
|
|
'can send (complex CHECKSEQUENCEVERIFY)',
|
|
|
|
async () => {
|
|
|
|
// 2 blocks from now
|
|
|
|
const sequence1 = bip68.encode({ blocks: 2 });
|
|
|
|
// 5 blocks from now
|
|
|
|
const sequence2 = bip68.encode({ blocks: 5 });
|
|
|
|
const p2sh = bitcoin.payments.p2sh({
|
|
|
|
redeem: {
|
|
|
|
output: complexCsvOutput(
|
|
|
|
alice,
|
|
|
|
bob,
|
|
|
|
charles,
|
|
|
|
dave,
|
|
|
|
sequence1,
|
|
|
|
sequence2,
|
2019-09-07 06:42:03 +02:00
|
|
|
),
|
2019-09-12 10:35:08 +02:00
|
|
|
},
|
|
|
|
network: regtest,
|
|
|
|
});
|
|
|
|
|
|
|
|
// fund the P2SH(CCSV) address
|
|
|
|
const unspent = await regtestUtils.faucet(p2sh.address!, 1e5);
|
|
|
|
|
|
|
|
const tx = new bitcoin.Transaction();
|
|
|
|
tx.version = 2;
|
|
|
|
tx.addInput(idToHash(unspent.txId), unspent.vout);
|
|
|
|
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 7e4);
|
|
|
|
|
|
|
|
// OP_0 {Bob sig} {Charles sig} OP_TRUE OP_TRUE
|
|
|
|
const signatureHash = tx.hashForSignature(
|
|
|
|
0,
|
|
|
|
p2sh.redeem!.output!,
|
|
|
|
hashType,
|
|
|
|
);
|
|
|
|
const redeemScriptSig = bitcoin.payments.p2sh({
|
|
|
|
network: regtest,
|
|
|
|
redeem: {
|
|
|
|
network: regtest,
|
|
|
|
output: p2sh.redeem!.output,
|
|
|
|
input: bitcoin.script.compile([
|
|
|
|
bitcoin.opcodes.OP_0,
|
|
|
|
bitcoin.script.signature.encode(bob.sign(signatureHash), hashType),
|
|
|
|
bitcoin.script.signature.encode(
|
|
|
|
charles.sign(signatureHash),
|
|
|
|
hashType,
|
|
|
|
),
|
|
|
|
bitcoin.opcodes.OP_TRUE,
|
|
|
|
bitcoin.opcodes.OP_TRUE,
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
}).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-08-22 05:23:56 +02:00
|
|
|
|
2018-09-03 08:53:09 +02:00
|
|
|
// Check first combination of complex CSV, mediator + 1 of 3 after 2 blocks
|
2019-09-12 10:35:08 +02:00
|
|
|
it(
|
|
|
|
'can create (and broadcast via 3PBP) a Transaction where Alice (mediator) ' +
|
|
|
|
'and Bob can send after 2 blocks (complex CHECKSEQUENCEVERIFY)',
|
|
|
|
async () => {
|
|
|
|
// 2 blocks from now
|
|
|
|
const sequence1 = bip68.encode({ blocks: 2 });
|
|
|
|
// 5 blocks from now
|
|
|
|
const sequence2 = bip68.encode({ blocks: 5 });
|
|
|
|
const p2sh = bitcoin.payments.p2sh({
|
|
|
|
redeem: {
|
|
|
|
output: complexCsvOutput(
|
|
|
|
alice,
|
|
|
|
bob,
|
|
|
|
charles,
|
|
|
|
dave,
|
|
|
|
sequence1,
|
|
|
|
sequence2,
|
|
|
|
),
|
|
|
|
},
|
2019-05-21 09:11:45 +02:00
|
|
|
network: regtest,
|
2019-09-12 10:35:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// fund the P2SH(CCSV) address
|
|
|
|
const unspent = await regtestUtils.faucet(p2sh.address!, 1e5);
|
|
|
|
|
|
|
|
const tx = new bitcoin.Transaction();
|
|
|
|
tx.version = 2;
|
|
|
|
tx.addInput(idToHash(unspent.txId), unspent.vout, sequence1); // Set sequence1 for input
|
|
|
|
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 7e4);
|
|
|
|
|
|
|
|
// OP_0 {Bob sig} {Alice mediator sig} OP_FALSE OP_TRUE
|
|
|
|
const signatureHash = tx.hashForSignature(
|
|
|
|
0,
|
|
|
|
p2sh.redeem!.output!,
|
|
|
|
hashType,
|
|
|
|
);
|
|
|
|
const redeemScriptSig = bitcoin.payments.p2sh({
|
|
|
|
network: regtest,
|
|
|
|
redeem: {
|
|
|
|
network: regtest,
|
|
|
|
output: p2sh.redeem!.output,
|
|
|
|
input: bitcoin.script.compile([
|
|
|
|
bitcoin.opcodes.OP_0,
|
|
|
|
bitcoin.script.signature.encode(bob.sign(signatureHash), hashType),
|
|
|
|
bitcoin.script.signature.encode(
|
|
|
|
alice.sign(signatureHash),
|
|
|
|
hashType,
|
|
|
|
),
|
|
|
|
bitcoin.opcodes.OP_0,
|
|
|
|
bitcoin.opcodes.OP_TRUE,
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
}).input;
|
|
|
|
tx.setInputScript(0, redeemScriptSig!);
|
|
|
|
|
|
|
|
// Wait 2 blocks
|
|
|
|
await regtestUtils.mine(2);
|
|
|
|
|
|
|
|
await regtestUtils.broadcast(tx.toHex());
|
|
|
|
|
|
|
|
await regtestUtils.verify({
|
|
|
|
txId: tx.getId(),
|
|
|
|
address: regtestUtils.RANDOM_ADDRESS,
|
|
|
|
vout: 0,
|
|
|
|
value: 7e4,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2018-08-22 05:23:56 +02:00
|
|
|
|
2018-09-03 08:53:09 +02:00
|
|
|
// Check first combination of complex CSV, mediator after 5 blocks
|
2019-09-12 10:35:08 +02:00
|
|
|
it(
|
|
|
|
'can create (and broadcast via 3PBP) a Transaction where Alice (mediator) ' +
|
|
|
|
'can send after 5 blocks (complex CHECKSEQUENCEVERIFY)',
|
|
|
|
async () => {
|
|
|
|
// 2 blocks from now
|
|
|
|
const sequence1 = bip68.encode({ blocks: 2 });
|
|
|
|
// 5 blocks from now
|
|
|
|
const sequence2 = bip68.encode({ blocks: 5 });
|
|
|
|
const p2sh = bitcoin.payments.p2sh({
|
|
|
|
redeem: {
|
|
|
|
output: complexCsvOutput(
|
|
|
|
alice,
|
|
|
|
bob,
|
|
|
|
charles,
|
|
|
|
dave,
|
|
|
|
sequence1,
|
|
|
|
sequence2,
|
|
|
|
),
|
|
|
|
},
|
2019-05-21 09:11:45 +02:00
|
|
|
network: regtest,
|
2019-09-12 10:35:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// fund the P2SH(CCSV) address
|
|
|
|
const unspent = await regtestUtils.faucet(p2sh.address!, 1e5);
|
|
|
|
|
|
|
|
const tx = new bitcoin.Transaction();
|
|
|
|
tx.version = 2;
|
|
|
|
tx.addInput(idToHash(unspent.txId), unspent.vout, sequence2); // Set sequence2 for input
|
|
|
|
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 7e4);
|
|
|
|
|
|
|
|
// {Alice mediator sig} OP_FALSE
|
|
|
|
const signatureHash = tx.hashForSignature(
|
|
|
|
0,
|
|
|
|
p2sh.redeem!.output!,
|
|
|
|
hashType,
|
|
|
|
);
|
|
|
|
const redeemScriptSig = bitcoin.payments.p2sh({
|
|
|
|
network: regtest,
|
|
|
|
redeem: {
|
|
|
|
network: regtest,
|
|
|
|
output: p2sh.redeem!.output,
|
|
|
|
input: bitcoin.script.compile([
|
|
|
|
bitcoin.script.signature.encode(
|
|
|
|
alice.sign(signatureHash),
|
|
|
|
hashType,
|
|
|
|
),
|
|
|
|
bitcoin.opcodes.OP_0,
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
}).input;
|
|
|
|
tx.setInputScript(0, redeemScriptSig!);
|
|
|
|
|
|
|
|
// Wait 5 blocks
|
|
|
|
await regtestUtils.mine(5);
|
|
|
|
|
|
|
|
await regtestUtils.broadcast(tx.toHex());
|
|
|
|
|
|
|
|
await regtestUtils.verify({
|
|
|
|
txId: tx.getId(),
|
|
|
|
address: regtestUtils.RANDOM_ADDRESS,
|
|
|
|
vout: 0,
|
|
|
|
value: 7e4,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
2019-10-28 04:27:35 +01:00
|
|
|
|
|
|
|
// This function is used to finalize a CSV transaction using PSBT.
|
|
|
|
// See first test above.
|
|
|
|
function csvGetFinalScripts(
|
|
|
|
inputIndex: number,
|
|
|
|
input: PsbtInput,
|
|
|
|
script: Buffer,
|
|
|
|
isSegwit: boolean,
|
|
|
|
isP2SH: boolean,
|
|
|
|
isP2WSH: boolean,
|
|
|
|
): {
|
|
|
|
finalScriptSig: Buffer | undefined;
|
|
|
|
finalScriptWitness: Buffer | undefined;
|
|
|
|
} {
|
|
|
|
// Step 1: Check to make sure the meaningful script matches what you expect.
|
|
|
|
const decompiled = bitcoin.script.decompile(script);
|
|
|
|
// Checking if first OP is OP_IF... should do better check in production!
|
|
|
|
// You may even want to check the public keys in the script against a
|
|
|
|
// whitelist depending on the circumstances!!!
|
|
|
|
// You also want to check the contents of the input to see if you have enough
|
|
|
|
// info to actually construct the scriptSig and Witnesses.
|
|
|
|
if (!decompiled || decompiled[0] !== bitcoin.opcodes.OP_IF) {
|
|
|
|
throw new Error(`Can not finalize input #${inputIndex}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: Create final scripts
|
|
|
|
let payment: bitcoin.Payment = {
|
|
|
|
network: regtest,
|
|
|
|
output: script,
|
|
|
|
// This logic should be more strict and make sure the pubkeys in the
|
|
|
|
// meaningful script are the ones signing in the PSBT etc.
|
|
|
|
input: bitcoin.script.compile([
|
|
|
|
input.partialSig![0].signature,
|
|
|
|
bitcoin.opcodes.OP_TRUE,
|
|
|
|
]),
|
|
|
|
};
|
|
|
|
if (isP2WSH && isSegwit)
|
|
|
|
payment = bitcoin.payments.p2wsh({
|
|
|
|
network: regtest,
|
|
|
|
redeem: payment,
|
|
|
|
});
|
|
|
|
if (isP2SH)
|
|
|
|
payment = bitcoin.payments.p2sh({
|
|
|
|
network: regtest,
|
|
|
|
redeem: payment,
|
|
|
|
});
|
|
|
|
|
|
|
|
function witnessStackToScriptWitness(witness: Buffer[]): Buffer {
|
|
|
|
let buffer = Buffer.allocUnsafe(0);
|
|
|
|
|
|
|
|
function writeSlice(slice: Buffer): void {
|
|
|
|
buffer = Buffer.concat([buffer, Buffer.from(slice)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeVarInt(i: number): void {
|
|
|
|
const currentLen = buffer.length;
|
|
|
|
const varintLen = varuint.encodingLength(i);
|
|
|
|
|
|
|
|
buffer = Buffer.concat([buffer, Buffer.allocUnsafe(varintLen)]);
|
|
|
|
varuint.encode(i, buffer, currentLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeVarSlice(slice: Buffer): void {
|
|
|
|
writeVarInt(slice.length);
|
|
|
|
writeSlice(slice);
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeVector(vector: Buffer[]): void {
|
|
|
|
writeVarInt(vector.length);
|
|
|
|
vector.forEach(writeVarSlice);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeVector(witness);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
finalScriptSig: payment.input,
|
|
|
|
finalScriptWitness:
|
|
|
|
payment.witness && payment.witness.length > 0
|
|
|
|
? witnessStackToScriptWitness(payment.witness)
|
|
|
|
: undefined,
|
|
|
|
};
|
|
|
|
}
|