44
44
from ..wallet import CCoinExtPubKey
45
45
46
46
from ..util import (
47
- ensure_isinstance , no_bool_use_as_property ,
47
+ ensure_isinstance , no_bool_use_as_property , assert_never ,
48
48
ClassMappingDispatcher , activate_class_dispatcher
49
49
)
50
50
@@ -135,13 +135,8 @@ class PSBT_OutKeyType(Enum):
135
135
('key_type' , int ), ('key_data' , bytes ), ('value' , bytes )
136
136
])
137
137
138
- T_KeyTypeEnum_Type = Union [
139
- Type [PSBT_GlobalKeyType ],
140
- Type [PSBT_OutKeyType ],
141
- Type [PSBT_InKeyType ],
142
- ]
143
-
144
- T_KeyTypeEnum = Union [PSBT_GlobalKeyType , PSBT_OutKeyType , PSBT_InKeyType ]
138
+ T_KeyTypeEnum = TypeVar (
139
+ 'T_KeyTypeEnum' , PSBT_GlobalKeyType , PSBT_OutKeyType , PSBT_InKeyType )
145
140
146
141
147
142
def proprietary_field_repr (
@@ -296,7 +291,7 @@ def merge_unknown_fields(
296
291
def read_psbt_keymap (
297
292
f : ByteStream_Type ,
298
293
keys_seen : Set [bytes ],
299
- keys_enum_class : T_KeyTypeEnum_Type ,
294
+ keys_enum_class : Type [ T_KeyTypeEnum ] ,
300
295
proprietary_fields : Dict [bytes , List [PSBT_ProprietaryTypeData ]],
301
296
unknown_fields : List [PSBT_UnknownTypeData ]
302
297
) -> Generator [Tuple [T_KeyTypeEnum , bytes , bytes ], None , None ]:
@@ -1295,10 +1290,7 @@ def check_witness_and_nonwitness_utxo_in_sync(
1295
1290
ensure_empty_key_data (key_type , key_data , descr ('' ))
1296
1291
proof_of_reserves_commitment = value
1297
1292
else :
1298
- raise AssertionError (
1299
- f'If key type { key_type } is recognized, '
1300
- f'it must be handled, and this statement '
1301
- f'should not be reached.' )
1293
+ assert_never (key_type )
1302
1294
1303
1295
# non_witness_utxo is preferred over witness_utxo for `utxo` kwarg
1304
1296
# because non_witness_utxo is a full transaction,
@@ -1646,13 +1638,13 @@ def descr(msg: str) -> str:
1646
1638
read_psbt_keymap (f , keys_seen , PSBT_OutKeyType ,
1647
1639
proprietary_fields , unknown_fields ):
1648
1640
1649
- if key_type == PSBT_OutKeyType .REDEEM_SCRIPT :
1641
+ if key_type is PSBT_OutKeyType .REDEEM_SCRIPT :
1650
1642
ensure_empty_key_data (key_type , key_data , descr ('' ))
1651
1643
redeem_script = CScript (value )
1652
- elif key_type == PSBT_OutKeyType .WITNESS_SCRIPT :
1644
+ elif key_type is PSBT_OutKeyType .WITNESS_SCRIPT :
1653
1645
ensure_empty_key_data (key_type , key_data , descr ('' ))
1654
1646
witness_script = CScript (value )
1655
- elif key_type == PSBT_OutKeyType .BIP32_DERIVATION :
1647
+ elif key_type is PSBT_OutKeyType .BIP32_DERIVATION :
1656
1648
pub = CPubKey (key_data )
1657
1649
if not pub .is_fullyvalid ():
1658
1650
raise SerializationError (
@@ -1662,6 +1654,8 @@ def descr(msg: str) -> str:
1662
1654
("duplicate keys should have been catched "
1663
1655
"inside read_psbt_keymap()" )
1664
1656
derivation_map [pub ] = PSBT_KeyDerivationInfo .deserialize (value )
1657
+ else :
1658
+ assert_never (key_type )
1665
1659
1666
1660
return cls (redeem_script = redeem_script , witness_script = witness_script ,
1667
1661
derivation_map = derivation_map ,
@@ -2107,10 +2101,10 @@ def stream_deserialize(cls: Type[T_PartiallySignedTransaction],
2107
2101
read_psbt_keymap (f , keys_seen , PSBT_GlobalKeyType ,
2108
2102
proprietary_fields , unknown_fields ):
2109
2103
2110
- if key_type == PSBT_GlobalKeyType .UNSIGNED_TX :
2104
+ if key_type is PSBT_GlobalKeyType .UNSIGNED_TX :
2111
2105
ensure_empty_key_data (key_type , key_data )
2112
2106
unsigned_tx = CTransaction .deserialize (value )
2113
- elif key_type == PSBT_GlobalKeyType .XPUB :
2107
+ elif key_type is PSBT_GlobalKeyType .XPUB :
2114
2108
if key_data [:4 ] != CCoinExtPubKey .base58_prefix :
2115
2109
raise ValueError (
2116
2110
f'One of global xpubs has unknown prefix: expected '
@@ -2121,17 +2115,14 @@ def stream_deserialize(cls: Type[T_PartiallySignedTransaction],
2121
2115
("duplicate keys should have been catched "
2122
2116
"inside read_psbt_keymap()" )
2123
2117
xpubs [xpub ] = PSBT_KeyDerivationInfo .deserialize (value )
2124
- elif key_type == PSBT_GlobalKeyType .VERSION :
2118
+ elif key_type is PSBT_GlobalKeyType .VERSION :
2125
2119
ensure_empty_key_data (key_type , key_data )
2126
2120
if len (value ) != 4 :
2127
2121
raise SerializationError (
2128
2122
f'Incorrect data length for { key_type .name } ' )
2129
2123
version = struct .unpack (b'<I' , value )[0 ]
2130
2124
else :
2131
- raise AssertionError (
2132
- f'If key type { key_type } is present in PSBT_GLOBAL_KEYS, '
2133
- f'it must be handled, and this statement '
2134
- f'should not be reached.' )
2125
+ assert_never (key_type )
2135
2126
2136
2127
if unsigned_tx is None :
2137
2128
raise ValueError (
0 commit comments