7
7
from test_framework .test_framework import BitcoinTestFramework
8
8
from test_framework .util import *
9
9
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
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
11
11
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
12
from io import BytesIO
13
13
@@ -33,23 +33,23 @@ def witness_script(use_p2wsh, pubkey):
33
33
34
34
# Return a transaction (in hex) that spends the given utxo to a segwit output,
35
35
# optionally wrapping the segwit output using P2SH.
36
- def create_witnessprogram ( use_p2wsh , utxo , pubkey , encode_p2sh , amount ):
37
- pkscript = hex_str_to_bytes ( witness_script ( use_p2wsh , pubkey ))
38
- if ( encode_p2sh ):
39
- p2sh_hash = hash160 ( pkscript )
40
- pkscript = CScript ([ OP_HASH160 , p2sh_hash , OP_EQUAL ])
41
- tx = CTransaction ( )
42
- tx . vin . append ( CTxIn ( COutPoint ( int ( utxo [ "txid" ], 16 ), utxo [ "vout" ]), b"" ))
43
- tx . vout . append ( CTxOut ( int ( amount * COIN ), pkscript ))
44
- return ToHex ( tx )
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
45
46
46
# Create a transaction spending a given utxo to a segwit output corresponding
47
47
# to the given pubkey: use_p2wsh determines whether to use P2WPKH or P2WSH;
48
48
# encode_p2sh determines whether to wrap in P2SH.
49
49
# sign=True will have the given node sign the transaction.
50
50
# insert_redeem_script will be added to the scriptSig, if given.
51
51
def send_to_witness (use_p2wsh , node , utxo , pubkey , encode_p2sh , amount , sign = True , insert_redeem_script = "" ):
52
- tx_to_witness = create_witnessprogram ( use_p2wsh , utxo , pubkey , encode_p2sh , amount )
52
+ tx_to_witness = create_witness_tx ( node , use_p2wsh , utxo , pubkey , encode_p2sh , amount )
53
53
if (sign ):
54
54
signed = node .signrawtransaction (tx_to_witness )
55
55
assert ("errors" not in signed or len (["errors" ]) == 0 )
@@ -133,8 +133,15 @@ def run_test(self):
133
133
newaddress = self .nodes [i ].getnewaddress ()
134
134
self .pubkey .append (self .nodes [i ].validateaddress (newaddress )["pubkey" ])
135
135
multiaddress = self .nodes [i ].addmultisigaddress (1 , [self .pubkey [- 1 ]])
136
- self .nodes [i ].addwitnessaddress (newaddress )
137
- self .nodes [i ].addwitnessaddress (multiaddress )
136
+ multiscript = CScript ([OP_1 , hex_str_to_bytes (self .pubkey [- 1 ]), OP_1 , OP_CHECKMULTISIG ])
137
+ p2sh_addr = self .nodes [i ].addwitnessaddress (newaddress , True )
138
+ bip173_addr = self .nodes [i ].addwitnessaddress (newaddress , False )
139
+ p2sh_ms_addr = self .nodes [i ].addwitnessaddress (multiaddress , True )
140
+ bip173_ms_addr = self .nodes [i ].addwitnessaddress (multiaddress , False )
141
+ assert_equal (p2sh_addr , key_to_p2sh_p2wpkh (self .pubkey [- 1 ]))
142
+ assert_equal (bip173_addr , key_to_p2wpkh (self .pubkey [- 1 ]))
143
+ assert_equal (p2sh_ms_addr , script_to_p2sh_p2wsh (multiscript ))
144
+ assert_equal (bip173_ms_addr , script_to_p2wsh (multiscript ))
138
145
p2sh_ids .append ([])
139
146
wit_ids .append ([])
140
147
for v in range (2 ):
@@ -558,6 +565,13 @@ def run_test(self):
558
565
solvable_txid .append (self .mine_and_test_listunspent (solvable_after_addwitnessaddress , 1 ))
559
566
self .mine_and_test_listunspent (unseen_anytime , 0 )
560
567
568
+ # Check that createrawtransaction/decoderawtransaction with non-v0 Bech32 works
569
+ v1_addr = program_to_witness (1 , [3 ,5 ])
570
+ v1_tx = self .nodes [0 ].createrawtransaction ([getutxo (spendable_txid [0 ])],{v1_addr : 1 })
571
+ v1_decoded = self .nodes [1 ].decoderawtransaction (v1_tx )
572
+ assert_equal (v1_decoded ['vout' ][0 ]['scriptPubKey' ]['addresses' ][0 ], v1_addr )
573
+ assert_equal (v1_decoded ['vout' ][0 ]['scriptPubKey' ]['hex' ], "51020305" )
574
+
561
575
# Check that spendable outputs are really spendable
562
576
self .create_and_mine_tx_from_txids (spendable_txid )
563
577
0 commit comments