@@ -19,12 +19,23 @@ class FFI:
1919 NULL = None # Mimic cffi's NULL pointer
2020 CData = CData # Expose CData class
2121
22- def new (self , type_str ):
22+ def __init__ (self ):
23+ # Cache type strings for identity comparison
24+ self ._types = {
25+ 'secp256k1_pubkey *' : 'secp256k1_pubkey *' ,
26+ 'secp256k1_ecdsa_signature *' : 'secp256k1_ecdsa_signature *' ,
27+ 'secp256k1_ecdsa_recoverable_signature *' : 'secp256k1_ecdsa_recoverable_signature *' ,
28+ 'secp256k1_xonly_pubkey *' : 'secp256k1_xonly_pubkey *' ,
29+ 'secp256k1_keypair *' : 'secp256k1_keypair *' ,
30+ }
31+
32+ def new (self , type_str , init = None ):
2333 if 'char' in type_str :
2434 size = int (type_str .split ('[' )[1 ].rstrip (']' ))
2535 return CData (bytearray (size ), type_str )
2636 elif 'size_t *' in type_str :
27- return CData ([0 ], type_str )
37+ data = [init if init is not None else 0 ]
38+ return CData (data , type_str )
2839 elif type_str == 'secp256k1_pubkey *' :
2940 return CData (bytearray (64 ), type_str )
3041 elif type_str == 'secp256k1_ecdsa_signature *' :
@@ -60,8 +71,10 @@ def decorator(func):
6071
6172 def typeof (self , obj ):
6273 if isinstance (obj , CData ):
63- return obj ._type
64- raise TypeError ("Object is not a CData instance" )
74+ return self ._types .get (obj ._type , obj ._type )
75+ if isinstance (obj , str ):
76+ return self ._types .get (obj , obj )
77+ raise TypeError ("Object is not a CData instance or type string" )
6578
6679# Dummy lib class to map to usecp256k1 functions
6780class Lib :
@@ -263,6 +276,8 @@ def secp256k1_ec_pubkey_serialize(self, ctx, output, outlen, pubkey, flags):
263276 try :
264277 if isinstance (pubkey , FFI .CData ):
265278 pubkey = pubkey ._data
279+ if isinstance (outlen , FFI .CData ):
280+ outlen = outlen ._data
266281 result = usecp256k1 .ec_pubkey_serialize (pubkey , flags )
267282 if result is None :
268283 return 0
@@ -338,6 +353,8 @@ def secp256k1_xonly_pubkey_from_pubkey(self, ctx, xonly_pubkey, pk_parity, pubke
338353 xonly_pubkey = xonly_pubkey ._data
339354 if isinstance (pubkey , FFI .CData ):
340355 pubkey = pubkey ._data
356+ if isinstance (pk_parity , FFI .CData ):
357+ pk_parity = pk_parity ._data
341358 result , parity = usecp256k1 .xonly_pubkey_from_pubkey (pubkey )
342359 if result is None :
343360 return 0
0 commit comments