@@ -74,8 +74,7 @@ class MiniWallet:
7474 def __init__ (self , test_node , * , mode = MiniWalletMode .ADDRESS_OP_TRUE ):
7575 self ._test_node = test_node
7676 self ._utxos = []
77- self ._priv_key = None
78- self ._address = None
77+ self ._mode = mode
7978
8079 assert isinstance (mode , MiniWalletMode )
8180 if mode == MiniWalletMode .RAW_OP_TRUE :
@@ -113,7 +112,7 @@ def scan_tx(self, tx):
113112
114113 def sign_tx (self , tx , fixed_length = True ):
115114 """Sign tx that has been created by MiniWallet in P2PK mode"""
116- assert self ._priv_key is not None
115+ assert_equal ( self ._mode , MiniWalletMode . RAW_P2PK )
117116 (sighash , err ) = SignatureHash (CScript (self ._scriptPubKey ), tx , 0 , SIGHASH_ALL )
118117 assert err is None
119118 # for exact fee calculation, create only signatures with fixed size by default (>49.89% probability):
@@ -142,6 +141,7 @@ def get_descriptor(self):
142141 return descsum_create (f'raw({ self ._scriptPubKey .hex ()} )' )
143142
144143 def get_address (self ):
144+ assert_equal (self ._mode , MiniWalletMode .ADDRESS_OP_TRUE )
145145 return self ._address
146146
147147 def get_utxo (self , * , txid : str = '' , vout : Optional [int ] = None , mark_as_spent = True ):
@@ -193,27 +193,27 @@ def create_self_transfer(self, *, fee_rate=Decimal("0.003"), from_node=None, utx
193193 """Create and return a tx with the specified fee_rate. Fee may be exact or at most one satoshi higher than needed."""
194194 from_node = from_node or self ._test_node
195195 utxo_to_spend = utxo_to_spend or self .get_utxo ()
196- if self ._priv_key is None :
196+ if self ._mode in ( MiniWalletMode . RAW_OP_TRUE , MiniWalletMode . ADDRESS_OP_TRUE ) :
197197 vsize = Decimal (85 ) # anyone-can-spend
198- else :
198+ elif self . _mode == MiniWalletMode . RAW_P2PK :
199199 vsize = Decimal (168 ) # P2PK (73 bytes scriptSig + 35 bytes scriptPubKey + 60 bytes other)
200+ else :
201+ assert False
200202 send_value = int (COIN * (utxo_to_spend ['value' ] - fee_rate * (vsize / 1000 )))
201203 assert send_value > 0
202204
203205 tx = CTransaction ()
204206 tx .vin = [CTxIn (COutPoint (int (utxo_to_spend ['txid' ], 16 ), utxo_to_spend ['vout' ]), nSequence = sequence )]
205207 tx .vout = [CTxOut (send_value , self ._scriptPubKey )]
206208 tx .nLockTime = locktime
207- if not self ._address :
208- # raw script
209- if self ._priv_key is not None :
210- # P2PK, need to sign
211- self .sign_tx (tx )
212- else :
213- # anyone-can-spend
214- tx .vin [0 ].scriptSig = CScript ([OP_NOP ] * 24 ) # pad to identical size
215- else :
209+ if self ._mode == MiniWalletMode .RAW_P2PK :
210+ self .sign_tx (tx )
211+ elif self ._mode == MiniWalletMode .RAW_OP_TRUE :
212+ tx .vin [0 ].scriptSig = CScript ([OP_NOP ] * 24 ) # pad to identical size
213+ elif self ._mode == MiniWalletMode .ADDRESS_OP_TRUE :
216214 tx .vin [0 ].scriptSig = CScript ([CScript ([OP_TRUE ])])
215+ else :
216+ assert False
217217 tx_hex = tx .serialize ().hex ()
218218
219219 tx_info = from_node .testmempoolaccept ([tx_hex ])[0 ]
0 commit comments