44# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
66import argparse
7- import base64
87import json
98import logging
109import math
@@ -20,7 +19,8 @@ PATH_BASE_TEST_FUNCTIONAL = os.path.abspath(os.path.join(PATH_BASE_CONTRIB_SIGNE
2019sys .path .insert (0 , PATH_BASE_TEST_FUNCTIONAL )
2120
2221from test_framework .blocktools import get_witness_script , script_BIP34_coinbase_height # noqa: E402
23- from test_framework .messages import CBlock , CBlockHeader , COutPoint , CTransaction , CTxIn , CTxInWitness , CTxOut , from_binary , from_hex , deser_string , ser_compact_size , ser_string , ser_uint256 , tx_from_hex # noqa: E402
22+ from test_framework .messages import CBlock , CBlockHeader , COutPoint , CTransaction , CTxIn , CTxInWitness , CTxOut , from_binary , from_hex , ser_string , ser_uint256 , tx_from_hex # noqa: E402
23+ from test_framework .psbt import PSBT , PSBTMap # noqa: E402
2424from test_framework .script import CScriptOp # noqa: E402
2525
2626logging .basicConfig (
@@ -32,76 +32,6 @@ SIGNET_HEADER = b"\xec\xc7\xda\xa2"
3232PSBT_SIGNET_BLOCK = b"\xfc \x06 signetb" # proprietary PSBT global field holding the block being signed
3333RE_MULTIMINER = re .compile ("^(\d+)(-(\d+))?/(\d+)$" )
3434
35- # #### some helpers that could go into test_framework
36-
37- class PSBTMap :
38- """Class for serializing and deserializing PSBT maps"""
39-
40- def __init__ (self , map = None ):
41- self .map = map if map is not None else {}
42-
43- def deserialize (self , f ):
44- m = {}
45- while True :
46- k = deser_string (f )
47- if len (k ) == 0 :
48- break
49- v = deser_string (f )
50- if len (k ) == 1 :
51- k = k [0 ]
52- assert k not in m
53- m [k ] = v
54- self .map = m
55-
56- def serialize (self ):
57- m = b""
58- for k ,v in self .map .items ():
59- if isinstance (k , int ) and 0 <= k and k <= 255 :
60- k = bytes ([k ])
61- m += ser_compact_size (len (k )) + k
62- m += ser_compact_size (len (v )) + v
63- m += b"\x00 "
64- return m
65-
66- class PSBT :
67- """Class for serializing and deserializing PSBTs"""
68-
69- def __init__ (self ):
70- self .g = PSBTMap ()
71- self .i = []
72- self .o = []
73- self .tx = None
74-
75- def deserialize (self , f ):
76- assert f .read (5 ) == b"psbt\xff "
77- self .g = from_binary (PSBTMap , f )
78- assert 0 in self .g .map
79- self .tx = from_binary (CTransaction , self .g .map [0 ])
80- self .i = [from_binary (PSBTMap , f ) for _ in self .tx .vin ]
81- self .o = [from_binary (PSBTMap , f ) for _ in self .tx .vout ]
82- return self
83-
84- def serialize (self ):
85- assert isinstance (self .g , PSBTMap )
86- assert isinstance (self .i , list ) and all (isinstance (x , PSBTMap ) for x in self .i )
87- assert isinstance (self .o , list ) and all (isinstance (x , PSBTMap ) for x in self .o )
88- assert 0 in self .g .map
89- tx = from_binary (CTransaction , self .g .map [0 ])
90- assert len (tx .vin ) == len (self .i )
91- assert len (tx .vout ) == len (self .o )
92-
93- psbt = [x .serialize () for x in [self .g ] + self .i + self .o ]
94- return b"psbt\xff " + b"" .join (psbt )
95-
96- def to_base64 (self ):
97- return base64 .b64encode (self .serialize ()).decode ("utf8" )
98-
99- @classmethod
100- def from_base64 (cls , b64psbt ):
101- return from_binary (cls , base64 .b64decode (b64psbt ))
102-
103- # #####
104-
10535def create_coinbase (height , value , spk ):
10636 cb = CTransaction ()
10737 cb .vin = [CTxIn (COutPoint (0 , 0xffffffff ), script_BIP34_coinbase_height (height ), 0xffffffff )]
0 commit comments