|
4 | 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
5 | 5 | """Test the SegWit changeover logic."""
|
6 | 6 |
|
| 7 | +from test_framework.address import ( |
| 8 | + key_to_p2sh_p2wpkh, |
| 9 | + key_to_p2wpkh, |
| 10 | + program_to_witness, |
| 11 | + script_to_p2sh_p2wsh, |
| 12 | + script_to_p2wsh, |
| 13 | +) |
| 14 | +from test_framework.blocktools import witness_script, send_to_witness |
7 | 15 | from test_framework.test_framework import BitcoinTestFramework
|
8 | 16 | from test_framework.util import *
|
9 | 17 | from test_framework.mininode import sha256, CTransaction, CTxIn, COutPoint, CTxOut, COIN, ToHex, FromHex
|
10 |
| -from test_framework.address import script_to_p2sh, key_to_p2pkh, key_to_p2sh_p2wpkh, key_to_p2wpkh, script_to_p2sh_p2wsh, script_to_p2wsh, program_to_witness |
| 18 | +from test_framework.address import script_to_p2sh, key_to_p2pkh |
11 | 19 | from test_framework.script import CScript, OP_HASH160, OP_CHECKSIG, OP_0, hash160, OP_EQUAL, OP_DUP, OP_EQUALVERIFY, OP_1, OP_2, OP_CHECKMULTISIG, OP_TRUE
|
12 | 20 | from io import BytesIO
|
13 | 21 |
|
|
16 | 24 | WIT_V0 = 0
|
17 | 25 | WIT_V1 = 1
|
18 | 26 |
|
19 |
| -# Create a scriptPubKey corresponding to either a P2WPKH output for the |
20 |
| -# given pubkey, or a P2WSH output of a 1-of-1 multisig for the given |
21 |
| -# pubkey. Returns the hex encoding of the scriptPubKey. |
22 |
| -def witness_script(use_p2wsh, pubkey): |
23 |
| - if (use_p2wsh == False): |
24 |
| - # P2WPKH instead |
25 |
| - pubkeyhash = hash160(hex_str_to_bytes(pubkey)) |
26 |
| - pkscript = CScript([OP_0, pubkeyhash]) |
27 |
| - else: |
28 |
| - # 1-of-1 multisig |
29 |
| - witness_program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG]) |
30 |
| - scripthash = sha256(witness_program) |
31 |
| - pkscript = CScript([OP_0, scripthash]) |
32 |
| - return bytes_to_hex_str(pkscript) |
33 |
| - |
34 |
| -# Return a transaction (in hex) that spends the given utxo to a segwit output, |
35 |
| -# optionally wrapping the segwit output using P2SH. |
36 |
| -def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount): |
37 |
| - if use_p2wsh: |
38 |
| - program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG]) |
39 |
| - addr = script_to_p2sh_p2wsh(program) if encode_p2sh else script_to_p2wsh(program) |
40 |
| - else: |
41 |
| - addr = key_to_p2sh_p2wpkh(pubkey) if encode_p2sh else key_to_p2wpkh(pubkey) |
42 |
| - if not encode_p2sh: |
43 |
| - assert_equal(node.validateaddress(addr)['scriptPubKey'], witness_script(use_p2wsh, pubkey)) |
44 |
| - return node.createrawtransaction([utxo], {addr: amount}) |
45 |
| - |
46 |
| -# Create a transaction spending a given utxo to a segwit output corresponding |
47 |
| -# to the given pubkey: use_p2wsh determines whether to use P2WPKH or P2WSH; |
48 |
| -# encode_p2sh determines whether to wrap in P2SH. |
49 |
| -# sign=True will have the given node sign the transaction. |
50 |
| -# insert_redeem_script will be added to the scriptSig, if given. |
51 |
| -def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""): |
52 |
| - tx_to_witness = create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount) |
53 |
| - if (sign): |
54 |
| - signed = node.signrawtransaction(tx_to_witness) |
55 |
| - assert("errors" not in signed or len(["errors"]) == 0) |
56 |
| - return node.sendrawtransaction(signed["hex"]) |
57 |
| - else: |
58 |
| - if (insert_redeem_script): |
59 |
| - tx = FromHex(CTransaction(), tx_to_witness) |
60 |
| - tx.vin[0].scriptSig += CScript([hex_str_to_bytes(insert_redeem_script)]) |
61 |
| - tx_to_witness = ToHex(tx) |
62 |
| - |
63 |
| - return node.sendrawtransaction(tx_to_witness) |
64 |
| - |
65 | 27 | def getutxo(txid):
|
66 | 28 | utxo = {}
|
67 | 29 | utxo["vout"] = 0
|
|
0 commit comments