@@ -86,8 +86,7 @@ class MiniWallet:
86
86
def __init__ (self , test_node , * , mode = MiniWalletMode .ADDRESS_OP_TRUE ):
87
87
self ._test_node = test_node
88
88
self ._utxos = []
89
- self ._priv_key = None
90
- self ._address = None
89
+ self ._mode = mode
91
90
92
91
assert isinstance (mode , MiniWalletMode )
93
92
if mode == MiniWalletMode .RAW_OP_TRUE :
@@ -121,7 +120,7 @@ def scan_tx(self, tx):
121
120
122
121
def sign_tx (self , tx , fixed_length = True ):
123
122
"""Sign tx that has been created by MiniWallet in P2PK mode"""
124
- assert self ._priv_key is not None
123
+ assert_equal ( self ._mode , MiniWalletMode . RAW_P2PK )
125
124
(sighash , err ) = LegacySignatureHash (CScript (self ._scriptPubKey ), tx , 0 , SIGHASH_ALL )
126
125
assert err is None
127
126
# for exact fee calculation, create only signatures with fixed size by default (>49.89% probability):
@@ -151,6 +150,7 @@ def get_descriptor(self):
151
150
return descsum_create (f'raw({ self ._scriptPubKey .hex ()} )' )
152
151
153
152
def get_address (self ):
153
+ assert_equal (self ._mode , MiniWalletMode .ADDRESS_OP_TRUE )
154
154
return self ._address
155
155
156
156
def get_utxo (self , * , txid : str = '' , vout : Optional [int ] = None , mark_as_spent = True ) -> dict :
@@ -257,28 +257,28 @@ def create_self_transfer(self, *, fee_rate=Decimal("0.003"), from_node=None, utx
257
257
"""Create and return a tx with the specified fee_rate. Fee may be exact or at most one satoshi higher than needed."""
258
258
from_node = from_node or self ._test_node
259
259
utxo_to_spend = utxo_to_spend or self .get_utxo ()
260
- if self ._priv_key is None :
260
+ if self ._mode in ( MiniWalletMode . RAW_OP_TRUE , MiniWalletMode . ADDRESS_OP_TRUE ) :
261
261
vsize = Decimal (104 ) # anyone-can-spend
262
- else :
262
+ elif self . _mode == MiniWalletMode . RAW_P2PK :
263
263
vsize = Decimal (168 ) # P2PK (73 bytes scriptSig + 35 bytes scriptPubKey + 60 bytes other)
264
+ else :
265
+ assert False
264
266
send_value = int (COIN * (utxo_to_spend ['value' ] - fee_rate * (vsize / 1000 )))
265
267
assert send_value > 0
266
268
267
269
tx = CTransaction ()
268
270
tx .vin = [CTxIn (COutPoint (int (utxo_to_spend ['txid' ], 16 ), utxo_to_spend ['vout' ]), nSequence = sequence )]
269
271
tx .vout = [CTxOut (send_value , self ._scriptPubKey )]
270
272
tx .nLockTime = locktime
271
- if not self ._address :
272
- # raw script
273
- if self ._priv_key is not None :
274
- # P2PK, need to sign
275
- self .sign_tx (tx )
276
- else :
277
- # anyone-can-spend
278
- tx .vin [0 ].scriptSig = CScript ([OP_NOP ] * 43 ) # pad to identical size
279
- else :
273
+ if self ._mode == MiniWalletMode .RAW_P2PK :
274
+ self .sign_tx (tx )
275
+ elif self ._mode == MiniWalletMode .RAW_OP_TRUE :
276
+ tx .vin [0 ].scriptSig = CScript ([OP_NOP ] * 43 ) # pad to identical size
277
+ elif self ._mode == MiniWalletMode .ADDRESS_OP_TRUE :
280
278
tx .wit .vtxinwit = [CTxInWitness ()]
281
279
tx .wit .vtxinwit [0 ].scriptWitness .stack = [CScript ([OP_TRUE ]), bytes ([LEAF_VERSION_TAPSCRIPT ]) + self ._internal_key ]
280
+ else :
281
+ assert False
282
282
tx_hex = tx .serialize ().hex ()
283
283
284
284
assert_equal (tx .get_vsize (), vsize )
0 commit comments