Skip to content

Commit 8424485

Browse files
committed
Rewrite 2.2.11 and 2.2.12 with CTransaction
1 parent 008ba57 commit 8424485

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

2.2-taptweak.ipynb

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"import util\n",
1212
"from test_framework.address import program_to_witness\n",
1313
"from test_framework.key import ECKey, ECPubKey, SECP256K1_ORDER, generate_key_pair, generate_schnorr_nonce\n",
14-
"from test_framework.messages import CTxInWitness, sha256\n",
14+
"from test_framework.messages import COutPoint, CTxIn, CTxInWitness, CTxOut, sha256\n",
1515
"from test_framework.musig import generate_musig_key, aggregate_schnorr_nonces, sign_musig, aggregate_musig_signatures\n",
16-
"from test_framework.script import CTransaction, OP_RETURN, SIGHASH_ALL_TAPROOT, TaprootSignatureHash"
16+
"from test_framework.script import CScript, CTransaction, OP_RETURN, SIGHASH_ALL_TAPROOT, TaprootSignatureHash"
1717
]
1818
},
1919
{
@@ -509,7 +509,7 @@
509509
"\n",
510510
"# Fetch the oldest unspent outpoint in the Bitcoin Core wallet\n",
511511
"unspent_txid = node.listunspent(1)[-1][\"txid\"]\n",
512-
"unspent_outpoint = [{\"txid\": unspent_txid, \"vout\": 0}]"
512+
"unspent_outpoint = COutPoint(int(unspent_txid,16), 0)"
513513
]
514514
},
515515
{
@@ -527,15 +527,26 @@
527527
"metadata": {},
528528
"outputs": [],
529529
"source": [
530-
"# Construct the two outputs:\n",
530+
"# Construct transaction spending previously generated outpoint\n",
531+
"op_return_tx = CTransaction()\n",
532+
"op_return_tx.nVersion = 1\n",
533+
"op_return_tx.nLockTime = 0\n",
534+
"op_return_tx_in = CTxIn(outpoint=unspent_outpoint, nSequence=0)\n",
535+
"op_return_tx.vin = [op_return_tx_in]\n",
536+
"\n",
531537
"# Output 0) Alice's destination address\n",
532-
"# Output 1) OP_RETURN with Alice's commitment\n",
533538
"address_alice = node.getnewaddress(address_type=\"bech32\")\n",
534-
"outputs = [{address_alice: 1}, {\"data\": commitment_bytes.hex()}] \n",
539+
"p2wpkh_output_script = bytes.fromhex(node.getaddressinfo(address_alice)['scriptPubKey'])\n",
540+
"p2wpkh_output_amount_sat = 100_000_000\n",
541+
"p2wpkh_output = CTxOut(nValue=p2wpkh_output_amount_sat, scriptPubKey=p2wpkh_output_script)\n",
542+
"\n",
543+
"# Output 1) OP_RETURN with Alice's commitment\n",
544+
"op_return_output_script = CScript([OP_RETURN, commitment_bytes])\n",
545+
"op_return_output = CTxOut(nValue=0, scriptPubKey=op_return_output_script)\n",
535546
"\n",
536-
"# Construct and sign a transaction with the p2pkh and OP_RETURN outputs\n",
537-
"op_return_tx_hex = node.createrawtransaction(inputs=unspent_outpoint, outputs=outputs)\n",
538-
"op_return_tx_hex_signed = node.signrawtransactionwithwallet(hexstring=op_return_tx_hex)['hex']\n",
547+
"# Populate transaction with p2pkh and OP_RETURN outputs and add valid witness\n",
548+
"op_return_tx.vout = [p2wpkh_output, op_return_output]\n",
549+
"op_return_tx_hex_signed = node.signrawtransactionwithwallet(hexstring=op_return_tx.serialize().hex())['hex']\n",
539550
"\n",
540551
"# Confirm details of the OP_RETURN output\n",
541552
"op_return_tx_decoded = node.decoderawtransaction(op_return_tx_hex_signed)\n",

0 commit comments

Comments
 (0)