Skip to content

Commit e7af140

Browse files
author
Janaka-Steph
committed
BitcoinJS v5 rewrite
1 parent 3bc1c3b commit e7af140

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2454
-1821
lines changed

code/algebra_puzzle_np2wsh.js

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const bitcoin = require('bitcoinjs-lib')
2-
const { alice } = require('./wallets.json')
2+
const {alice} = require('./wallets.json')
3+
const witnessStackToScriptWitness = require('./tools/witnessStackToScriptWitness')
34
const network = bitcoin.networks.regtest
45

56
const witnessScript = bitcoin.script.compile([
@@ -17,22 +18,52 @@ console.log('P2SH address:')
1718
console.log(p2sh.address)
1819
console.log()
1920

20-
const keyPairAlice1 = bitcoin.ECPair.fromWIF(alice[1].wif, network)
21-
const p2wpkhAlice1 = bitcoin.payments.p2wpkh({pubkey: keyPairAlice1.publicKey, network})
21+
// Create PSBT
22+
const psbt = new bitcoin.Psbt({network})
23+
.addInput({
24+
hash: 'TX_ID',
25+
index: TX_VOUT,
26+
witnessUtxo: {
27+
script: Buffer.from('a914' +
28+
bitcoin.crypto.hash160(p2wsh.output).toString('hex') +
29+
'87', 'hex'),
30+
value: 1e8,
31+
},
32+
witnessScript: Buffer.from(witnessScript, 'hex')
33+
})
34+
.addOutput({
35+
address: alice[1].p2wpkh,
36+
value: 999e5,
37+
})
2238

23-
const txb = new bitcoin.TransactionBuilder(network)
39+
// Finalizing
40+
const getFinalScripts = (inputIndex, input, script) => {
41+
// Step 1: Check to make sure the meaningful locking script matches what you expect.
42+
const decompiled = bitcoin.script.decompile(script)
43+
if (!decompiled || decompiled[0] !== bitcoin.opcodes.OP_ADD) {
44+
throw new Error(`Can not finalize input #${inputIndex}`)
45+
}
2446

25-
txb.addInput('TX_ID', TX_VOUT)
47+
// Step 2: Create final scripts
48+
const payment = bitcoin.payments.p2sh({
49+
redeem: bitcoin.payments.p2wsh({
50+
redeem: {
51+
output: script,
52+
input: bitcoin.script.compile([bitcoin.opcodes.OP_2, bitcoin.opcodes.OP_3]),
53+
}
54+
})
55+
})
2656

27-
txb.addOutput(p2wpkhAlice1.address, 999e5)
57+
return {
58+
finalScriptSig: payment.input,
59+
finalScriptWitness:
60+
payment.witness && payment.witness.length > 0
61+
? witnessStackToScriptWitness(payment.witness)
62+
: undefined
63+
}
64+
}
2865

29-
const tx = txb.buildIncomplete()
30-
31-
const scriptSig = bitcoin.script.compile([p2wsh.output])
32-
tx.setInputScript(0, scriptSig)
33-
34-
const witness = [Buffer.from('02','hex'), Buffer.from('03','hex'), p2wsh.redeem.output]
35-
tx.setWitness(0, witness)
66+
psbt.finalizeInput(0, getFinalScripts)
3667

3768
console.log('Transaction hexadecimal:')
38-
console.log(tx.toHex())
69+
console.log(psbt.extractTransaction().toHex())

code/algebra_puzzle_p2sh.js

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const bitcoin = require('bitcoinjs-lib')
2-
const { alice } = require('./wallets.json')
2+
const {alice} = require('./wallets.json')
33
const network = bitcoin.networks.regtest
44

55
const redeemScript = bitcoin.script.compile([
@@ -16,19 +16,41 @@ console.log('P2SH address:')
1616
console.log(p2sh.address)
1717
console.log()
1818

19-
const keyPairAlice1 = bitcoin.ECPair.fromWIF(alice[1].wif, network)
20-
const p2wpkhAlice1 = bitcoin.payments.p2wpkh({pubkey: keyPairAlice1.publicKey, network})
21-
22-
const txb = new bitcoin.TransactionBuilder(network)
23-
24-
txb.addInput('TX_ID', TX_VOUT)
25-
26-
txb.addOutput(p2wpkhAlice1.address, 999e5)
27-
28-
const tx = txb.buildIncomplete()
29-
30-
const InputScriptP2SH = bitcoin.script.compile([bitcoin.opcodes.OP_2, bitcoin.opcodes.OP_3, p2sh.redeem.output])
31-
tx.setInputScript(0, InputScriptP2SH)
19+
// Create PSBT
20+
const psbt = new bitcoin.Psbt({network})
21+
.addInput({
22+
hash: 'TX_ID',
23+
index: TX_VOUT,
24+
nonWitnessUtxo: Buffer.from('TX_HEX', 'hex'),
25+
redeemScript: Buffer.from(redeemScript, 'hex')
26+
})
27+
.addOutput({
28+
address: alice[1].p2wpkh,
29+
value: 999e5,
30+
})
31+
32+
// Finalizing
33+
const getFinalScripts = (inputIndex, input, script) => {
34+
// Step 1: Check to make sure the meaningful locking script matches what you expect.
35+
const decompiled = bitcoin.script.decompile(script)
36+
if (!decompiled || decompiled[0] !== bitcoin.opcodes.OP_ADD) {
37+
throw new Error(`Can not finalize input #${inputIndex}`)
38+
}
39+
40+
// Step 2: Create final scripts
41+
const payment = bitcoin.payments.p2sh({
42+
redeem: {
43+
output: script,
44+
input: bitcoin.script.compile([bitcoin.opcodes.OP_2, bitcoin.opcodes.OP_3]),
45+
},
46+
})
47+
48+
return {
49+
finalScriptSig: payment.input
50+
}
51+
}
52+
53+
psbt.finalizeInput(0, getFinalScripts)
3254

3355
console.log('Transaction hexadecimal:')
34-
console.log(tx.toHex())
56+
console.log(psbt.extractTransaction().toHex())

code/algebra_puzzle_p2wsh.js

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const bitcoin = require('bitcoinjs-lib')
2-
const { alice } = require('./wallets.json')
2+
const {alice} = require('./wallets.json')
3+
const witnessStackToScriptWitness = require('./tools/witnessStackToScriptWitness')
34
const network = bitcoin.networks.regtest
45

56
const witnessScript = bitcoin.script.compile([
@@ -9,24 +10,56 @@ const witnessScript = bitcoin.script.compile([
910

1011
console.log('Witness script:')
1112
console.log(witnessScript.toString('hex'))
13+
console.log()
1214

1315
const p2wsh = bitcoin.payments.p2wsh({redeem: {output: witnessScript, network}, network})
1416
console.log('P2WSH address:')
1517
console.log(p2wsh.address)
18+
console.log()
1619

17-
const keyPairAlice1 = bitcoin.ECPair.fromWIF(alice[1].wif, network)
18-
const p2wpkhAlice1 = bitcoin.payments.p2wpkh({pubkey: keyPairAlice1.publicKey, network})
20+
// Create PSBT
21+
const psbt = new bitcoin.Psbt({network})
22+
.addInput({
23+
hash: 'TX_ID',
24+
index: TX_VOUT,
25+
witnessUtxo: {
26+
script: Buffer.from('0020' +
27+
bitcoin.crypto.sha256(witnessScript).toString('hex'),
28+
'hex'),
29+
value: 1e8,
30+
},
31+
witnessScript: Buffer.from(witnessScript, 'hex')
32+
})
33+
.addOutput({
34+
address: alice[1].p2wpkh,
35+
value: 999e5,
36+
})
1937

20-
const txb = new bitcoin.TransactionBuilder(network)
38+
// Finalizing
39+
const getFinalScripts = (inputIndex, input, script) => {
40+
// Step 1: Check to make sure the meaningful locking script matches what you expect.
41+
const decompiled = bitcoin.script.decompile(script)
42+
if (!decompiled || decompiled[0] !== bitcoin.opcodes.OP_ADD) {
43+
throw new Error(`Can not finalize input #${inputIndex}`)
44+
}
2145

22-
txb.addInput('TX_ID', TX_VOUT)
46+
// Step 2: Create final scripts
47+
const payment = bitcoin.payments.p2wsh({
48+
redeem: {
49+
output: script,
50+
input: bitcoin.script.compile([bitcoin.opcodes.OP_2, bitcoin.opcodes.OP_3]),
51+
},
52+
})
2353

24-
txb.addOutput(p2wpkhAlice1.address, 999e5)
54+
return {
55+
finalScriptWitness:
56+
payment.witness && payment.witness.length > 0
57+
? witnessStackToScriptWitness(payment.witness)
58+
: undefined
59+
}
60+
}
2561

26-
const tx = txb.buildIncomplete()
27-
28-
const witness = [Buffer.from('02','hex'), Buffer.from('03','hex'), p2wsh.redeem.output]
29-
tx.setWitness(0, witness)
62+
psbt.finalizeInput(0, getFinalScripts)
3063

3164
console.log('Transaction hexadecimal:')
32-
console.log(tx.toHex())
65+
console.log(psbt.extractTransaction().toHex())

0 commit comments

Comments
 (0)