Simplify nonWitnessUtxo integration tests to not confuse learners

This commit is contained in:
junderw 2019-08-23 15:49:24 +09:00
parent 4264bd631c
commit a395af47fd
No known key found for this signature in database
GPG key ID: B256185D3A971908

View file

@ -283,41 +283,27 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
}); });
it('can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2WPKH) input with nonWitnessUtxo', async () => { it('can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2WPKH) input with nonWitnessUtxo', async () => {
// For learning purposes, ignore this test.
// REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
const p2sh = createPayment('p2sh-p2wpkh'); const p2sh = createPayment('p2sh-p2wpkh');
const inputData = await getInputData(5e4, p2sh.payment, false, 'p2sh'); const inputData = await getInputData(5e4, p2sh.payment, false, 'p2sh');
const inputData2 = await getInputData(5e4, p2sh.payment, false, 'p2sh'); const inputData2 = await getInputData(5e4, p2sh.payment, false, 'p2sh');
{
const {
hash,
index,
nonWitnessUtxo,
redeemScript,
} = inputData;
assert.deepStrictEqual(
{ hash, index, nonWitnessUtxo, redeemScript },
inputData,
);
}
const keyPair = p2sh.keys[0]; const keyPair = p2sh.keys[0];
const outputData = { const outputData = {
script: p2sh.payment.output, // sending to myself for fun script: p2sh.payment.output,
value: 2e4, value: 2e4,
}; };
const outputData2 = { const outputData2 = {
script: p2sh.payment.output, // sending to myself for fun script: p2sh.payment.output,
value: 7e4, value: 7e4,
}; };
const tx = new bitcoin.Psbt() const tx = new bitcoin.Psbt()
.addInputs([inputData, inputData2]) .addInputs([inputData, inputData2])
.addOutputs([outputData, outputData2]) .addOutputs([outputData, outputData2])
.signAllInputs(keyPair) .signAllInputs(keyPair)
.finalizeAllInputs() .finalizeAllInputs()
.extractTransaction(); .extractTransaction();
// build and broadcast to the Bitcoin RegTest network
await regtestUtils.broadcast(tx.toHex()); await regtestUtils.broadcast(tx.toHex());
await regtestUtils.verify({ await regtestUtils.verify({
txId: tx.getId(), txId: tx.getId(),
address: p2sh.payment.address, address: p2sh.payment.address,
@ -361,15 +347,10 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
}); });
it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input with nonWitnessUtxo', async () => { it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input with nonWitnessUtxo', async () => {
// the only thing that changes is you don't give a redeemscript for input data // For learning purposes, ignore this test.
// REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
const p2wpkh = createPayment('p2wpkh'); const p2wpkh = createPayment('p2wpkh');
const inputData = await getInputData(5e4, p2wpkh.payment, false, 'noredeem'); const inputData = await getInputData(5e4, p2wpkh.payment, false, 'noredeem');
{
const { hash, index, nonWitnessUtxo } = inputData;
assert.deepStrictEqual({ hash, index, nonWitnessUtxo }, inputData);
}
const psbt = new bitcoin.Psbt({ network: regtest }) const psbt = new bitcoin.Psbt({ network: regtest })
.addInput(inputData) .addInput(inputData)
.addOutput({ .addOutput({
@ -377,15 +358,9 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
value: 2e4, value: 2e4,
}) })
.signInput(0, p2wpkh.keys[0]); .signInput(0, p2wpkh.keys[0]);
assert.strictEqual(psbt.validateSignaturesOfInput(0), true);
psbt.finalizeAllInputs(); psbt.finalizeAllInputs();
const tx = psbt.extractTransaction(); const tx = psbt.extractTransaction();
// build and broadcast to the Bitcoin RegTest network
await regtestUtils.broadcast(tx.toHex()); await regtestUtils.broadcast(tx.toHex());
await regtestUtils.verify({ await regtestUtils.verify({
txId: tx.getId(), txId: tx.getId(),
address: regtestUtils.RANDOM_ADDRESS, address: regtestUtils.RANDOM_ADDRESS,
@ -435,21 +410,10 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
}); });
it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WSH(P2PK) input with nonWitnessUtxo', async () => { it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WSH(P2PK) input with nonWitnessUtxo', async () => {
// For learning purposes, ignore this test.
// REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
const p2wsh = createPayment('p2wsh-p2pk'); const p2wsh = createPayment('p2wsh-p2pk');
const inputData = await getInputData(5e4, p2wsh.payment, false, 'p2wsh'); const inputData = await getInputData(5e4, p2wsh.payment, false, 'p2wsh');
{
const {
hash,
index,
nonWitnessUtxo,
witnessScript, // NEW: A Buffer of the witnessScript
} = inputData;
assert.deepStrictEqual(
{ hash, index, nonWitnessUtxo, witnessScript },
inputData,
);
}
const psbt = new bitcoin.Psbt({ network: regtest }) const psbt = new bitcoin.Psbt({ network: regtest })
.addInput(inputData) .addInput(inputData)
.addOutput({ .addOutput({
@ -457,15 +421,9 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
value: 2e4, value: 2e4,
}) })
.signInput(0, p2wsh.keys[0]); .signInput(0, p2wsh.keys[0]);
assert.strictEqual(psbt.validateSignaturesOfInput(0), true);
psbt.finalizeAllInputs(); psbt.finalizeAllInputs();
const tx = psbt.extractTransaction(); const tx = psbt.extractTransaction();
// build and broadcast to the Bitcoin RegTest network
await regtestUtils.broadcast(tx.toHex()); await regtestUtils.broadcast(tx.toHex());
await regtestUtils.verify({ await regtestUtils.verify({
txId: tx.getId(), txId: tx.getId(),
address: regtestUtils.RANDOM_ADDRESS, address: regtestUtils.RANDOM_ADDRESS,
@ -525,22 +483,10 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
}); });
it('can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2WSH(P2MS(3 of 4))) (SegWit multisig) input with nonWitnessUtxo', async () => { it('can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2WSH(P2MS(3 of 4))) (SegWit multisig) input with nonWitnessUtxo', async () => {
// For learning purposes, ignore this test.
// REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
const p2sh = createPayment('p2sh-p2wsh-p2ms(3 of 4)'); const p2sh = createPayment('p2sh-p2wsh-p2ms(3 of 4)');
const inputData = await getInputData(5e4, p2sh.payment, false, 'p2sh-p2wsh'); const inputData = await getInputData(5e4, p2sh.payment, false, 'p2sh-p2wsh');
{
const {
hash,
index,
nonWitnessUtxo,
redeemScript,
witnessScript,
} = inputData;
assert.deepStrictEqual(
{ hash, index, nonWitnessUtxo, redeemScript, witnessScript },
inputData,
);
}
const psbt = new bitcoin.Psbt({ network: regtest }) const psbt = new bitcoin.Psbt({ network: regtest })
.addInput(inputData) .addInput(inputData)
.addOutput({ .addOutput({
@ -550,22 +496,9 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
.signInput(0, p2sh.keys[0]) .signInput(0, p2sh.keys[0])
.signInput(0, p2sh.keys[2]) .signInput(0, p2sh.keys[2])
.signInput(0, p2sh.keys[3]); .signInput(0, p2sh.keys[3]);
assert.strictEqual(psbt.validateSignaturesOfInput(0), true);
assert.strictEqual(
psbt.validateSignaturesOfInput(0, p2sh.keys[3].publicKey),
true,
);
assert.throws(() => {
psbt.validateSignaturesOfInput(0, p2sh.keys[1].publicKey);
}, new RegExp('No signatures for this pubkey'));
psbt.finalizeAllInputs(); psbt.finalizeAllInputs();
const tx = psbt.extractTransaction(); const tx = psbt.extractTransaction();
// build and broadcast to the Bitcoin RegTest network
await regtestUtils.broadcast(tx.toHex()); await regtestUtils.broadcast(tx.toHex());
await regtestUtils.verify({ await regtestUtils.verify({
txId: tx.getId(), txId: tx.getId(),
address: regtestUtils.RANDOM_ADDRESS, address: regtestUtils.RANDOM_ADDRESS,