Skip to content

Commit cc94858

Browse files
Try fixing CData
1 parent 9cceaf7 commit cc94858

File tree

1 file changed

+88
-8
lines changed

1 file changed

+88
-8
lines changed

internal_filesystem/lib/secp256k1_compat.py

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,47 @@
88
SECP256K1_EC_COMPRESSED = 1 << 1 # 2
99
SECP256K1_EC_UNCOMPRESSED = 0
1010

11+
# Dummy CData class to mimic cffi's CData
12+
class CData:
13+
def __init__(self, data, type_str):
14+
self._data = data
15+
self._type = type_str
16+
1117
# Dummy ffi class to mimic cffi
1218
class FFI:
1319
NULL = None # Mimic cffi's NULL pointer
20+
CData = CData # Expose CData class
1421

1522
def new(self, type_str):
1623
if 'char' in type_str:
1724
size = int(type_str.split('[')[1].rstrip(']'))
18-
return bytearray(size)
25+
return CData(bytearray(size), type_str)
1926
elif 'size_t *' in type_str:
20-
return [0]
27+
return CData([0], type_str)
2128
elif type_str == 'secp256k1_pubkey *':
22-
return bytearray(64)
29+
return CData(bytearray(64), type_str)
2330
elif type_str == 'secp256k1_ecdsa_signature *':
24-
return bytearray(64)
31+
return CData(bytearray(64), type_str)
2532
elif type_str == 'secp256k1_ecdsa_recoverable_signature *':
26-
return bytearray(65)
33+
return CData(bytearray(65), type_str)
2734
elif type_str == 'secp256k1_xonly_pubkey *':
28-
return bytearray(32)
35+
return CData(bytearray(32), type_str)
2936
elif type_str == 'secp256k1_keypair *':
30-
return bytearray(96)
37+
return CData(bytearray(96), type_str)
3138
raise ValueError(f"Unsupported ffi type: {type_str}")
3239

3340
def buffer(self, obj, size=None):
41+
if isinstance(obj, CData):
42+
obj = obj._data
3443
if isinstance(obj, list):
3544
return bytes(obj)
3645
return bytes(obj[:size] if size is not None else obj)
3746

3847
def memmove(self, dst, src, n):
48+
if isinstance(dst, CData):
49+
dst = dst._data
50+
if isinstance(src, CData):
51+
src = src._data
3952
if isinstance(src, bytes):
4053
src = bytearray(src)
4154
dst[:n] = src[:n]
@@ -45,6 +58,11 @@ def decorator(func):
4558
return func
4659
return decorator
4760

61+
def typeof(self, obj):
62+
if isinstance(obj, CData):
63+
return obj._type
64+
raise TypeError("Object is not a CData instance")
65+
4866
# Dummy lib class to map to usecp256k1 functions
4967
class Lib:
5068
SECP256K1_EC_COMPRESSED = SECP256K1_EC_COMPRESSED
@@ -63,6 +81,8 @@ def secp256k1_ec_seckey_verify(self, ctx, seckey):
6381

6482
def secp256k1_ecdsa_signature_serialize_der(self, ctx, output, outputlen, raw_sig):
6583
try:
84+
if isinstance(raw_sig, FFI.CData):
85+
raw_sig = raw_sig._data
6686
result = usecp256k1.ecdsa_signature_serialize_der(raw_sig)
6787
if result is None:
6888
return 0
@@ -74,6 +94,8 @@ def secp256k1_ecdsa_signature_serialize_der(self, ctx, output, outputlen, raw_si
7494

7595
def secp256k1_ecdsa_signature_parse_der(self, ctx, raw_sig, ser_sig, ser_len):
7696
try:
97+
if isinstance(raw_sig, FFI.CData):
98+
raw_sig = raw_sig._data
7799
result = usecp256k1.ecdsa_signature_parse_der(ser_sig)
78100
if result is None:
79101
return 0
@@ -84,6 +106,8 @@ def secp256k1_ecdsa_signature_parse_der(self, ctx, raw_sig, ser_sig, ser_len):
84106

85107
def secp256k1_ecdsa_signature_serialize_compact(self, ctx, output, raw_sig):
86108
try:
109+
if isinstance(raw_sig, FFI.CData):
110+
raw_sig = raw_sig._data
87111
result = usecp256k1.ecdsa_signature_serialize_compact(raw_sig)
88112
if result is None:
89113
return 0
@@ -94,6 +118,8 @@ def secp256k1_ecdsa_signature_serialize_compact(self, ctx, output, raw_sig):
94118

95119
def secp256k1_ecdsa_signature_parse_compact(self, ctx, raw_sig, ser_sig):
96120
try:
121+
if isinstance(raw_sig, FFI.CData):
122+
raw_sig = raw_sig._data
97123
result = usecp256k1.ecdsa_signature_parse_compact(ser_sig)
98124
if result is None:
99125
return 0
@@ -104,6 +130,11 @@ def secp256k1_ecdsa_signature_parse_compact(self, ctx, raw_sig, ser_sig):
104130

105131
def secp256k1_ecdsa_signature_normalize(self, ctx, sigout, raw_sig):
106132
try:
133+
if isinstance(raw_sig, FFI.CData):
134+
raw_sig = raw_sig._data
135+
if sigout != FFI.NULL:
136+
if isinstance(sigout, FFI.CData):
137+
sigout = sigout._data
107138
is_normalized = usecp256k1.ecdsa_signature_normalize(raw_sig)
108139
if sigout != FFI.NULL:
109140
sigout[:] = is_normalized[1] if is_normalized[1] else raw_sig
@@ -113,6 +144,8 @@ def secp256k1_ecdsa_signature_normalize(self, ctx, sigout, raw_sig):
113144

114145
def secp256k1_ecdsa_sign(self, ctx, raw_sig, msg32, privkey, nonce_fn, nonce_data):
115146
try:
147+
if isinstance(raw_sig, FFI.CData):
148+
raw_sig = raw_sig._data
116149
result = usecp256k1.ecdsa_sign(msg32, privkey)
117150
if result is None:
118151
return 0
@@ -123,12 +156,18 @@ def secp256k1_ecdsa_sign(self, ctx, raw_sig, msg32, privkey, nonce_fn, nonce_dat
123156

124157
def secp256k1_ecdsa_verify(self, ctx, raw_sig, msg32, pubkey):
125158
try:
159+
if isinstance(raw_sig, FFI.CData):
160+
raw_sig = raw_sig._data
161+
if isinstance(pubkey, FFI.CData):
162+
pubkey = pubkey._data
126163
return usecp256k1.ecdsa_verify(raw_sig, msg32, pubkey)
127164
except (ValueError, AttributeError):
128165
return 0
129166

130167
def secp256k1_ecdsa_recoverable_signature_serialize_compact(self, ctx, output, recid, recover_sig):
131168
try:
169+
if isinstance(recover_sig, FFI.CData):
170+
recover_sig = recover_sig._data
132171
result, rec_id = usecp256k1.ecdsa_sign_recoverable(recover_sig)
133172
if result is None:
134173
return 0
@@ -140,6 +179,8 @@ def secp256k1_ecdsa_recoverable_signature_serialize_compact(self, ctx, output, r
140179

141180
def secp256k1_ecdsa_recoverable_signature_parse_compact(self, ctx, recover_sig, ser_sig, rec_id):
142181
try:
182+
if isinstance(recover_sig, FFI.CData):
183+
recover_sig = recover_sig._data
143184
result = usecp256k1.ecdsa_sign_recoverable(ser_sig, rec_id)
144185
if result is None:
145186
return 0
@@ -150,6 +191,10 @@ def secp256k1_ecdsa_recoverable_signature_parse_compact(self, ctx, recover_sig,
150191

151192
def secp256k1_ecdsa_recoverable_signature_convert(self, ctx, normal_sig, recover_sig):
152193
try:
194+
if isinstance(normal_sig, FFI.CData):
195+
normal_sig = normal_sig._data
196+
if isinstance(recover_sig, FFI.CData):
197+
recover_sig = recover_sig._data
153198
result = usecp256k1.ecdsa_sign_recoverable(recover_sig)
154199
if result is None:
155200
return 0
@@ -160,6 +205,8 @@ def secp256k1_ecdsa_recoverable_signature_convert(self, ctx, normal_sig, recover
160205

161206
def secp256k1_ecdsa_sign_recoverable(self, ctx, raw_sig, msg32, privkey, nonce_fn, nonce_data):
162207
try:
208+
if isinstance(raw_sig, FFI.CData):
209+
raw_sig = raw_sig._data
163210
result = usecp256k1.ecdsa_sign_recoverable(msg32, privkey)
164211
if result is None:
165212
return 0
@@ -170,6 +217,10 @@ def secp256k1_ecdsa_sign_recoverable(self, ctx, raw_sig, msg32, privkey, nonce_f
170217

171218
def secp256k1_ecdsa_recover(self, ctx, pubkey, recover_sig, msg32):
172219
try:
220+
if isinstance(pubkey, FFI.CData):
221+
pubkey = pubkey._data
222+
if isinstance(recover_sig, FFI.CData):
223+
recover_sig = recover_sig._data
173224
result = usecp256k1.ecdsa_sign_recoverable(recover_sig, msg32)
174225
if result is None:
175226
return 0
@@ -180,6 +231,8 @@ def secp256k1_ecdsa_recover(self, ctx, pubkey, recover_sig, msg32):
180231

181232
def secp256k1_schnorrsig_sign_custom(self, ctx, sig64, msg, msg_len, keypair, aux_rand32):
182233
try:
234+
if isinstance(keypair, FFI.CData):
235+
keypair = keypair._data
183236
result = usecp256k1.schnorrsig_sign(msg, keypair)
184237
if result is None:
185238
return 0
@@ -190,6 +243,8 @@ def secp256k1_schnorrsig_sign_custom(self, ctx, sig64, msg, msg_len, keypair, au
190243

191244
def secp256k1_schnorrsig_verify(self, ctx, schnorr_sig, msg, msg_len, xonly_pubkey):
192245
try:
246+
if isinstance(xonly_pubkey, FFI.CData):
247+
xonly_pubkey = xonly_pubkey._data
193248
return usecp256k1.schnorrsig_verify(schnorr_sig, msg, xonly_pubkey)
194249
except (ValueError, AttributeError):
195250
return 0
@@ -206,6 +261,8 @@ def secp256k1_tagged_sha256(self, ctx, hash32, tag, tag_len, msg, msg_len):
206261

207262
def secp256k1_ec_pubkey_serialize(self, ctx, output, outlen, pubkey, flags):
208263
try:
264+
if isinstance(pubkey, FFI.CData):
265+
pubkey = pubkey._data
209266
result = usecp256k1.ec_pubkey_serialize(pubkey, flags)
210267
if result is None:
211268
return 0
@@ -216,6 +273,8 @@ def secp256k1_ec_pubkey_serialize(self, ctx, output, outlen, pubkey, flags):
216273

217274
def secp256k1_ec_pubkey_parse(self, ctx, pubkey, pubkey_ser, ser_len):
218275
try:
276+
if isinstance(pubkey, FFI.CData):
277+
pubkey = pubkey._data
219278
result = usecp256k1.ec_pubkey_parse(pubkey_ser)
220279
if result is None:
221280
return 0
@@ -226,7 +285,10 @@ def secp256k1_ec_pubkey_parse(self, ctx, pubkey, pubkey_ser, ser_len):
226285

227286
def secp256k1_ec_pubkey_combine(self, ctx, outpub, pubkeys, n_pubkeys):
228287
try:
229-
result = usecp256k1.ec_pubkey_combine(pubkeys)
288+
if isinstance(outpub, FFI.CData):
289+
outpub = outpub._data
290+
pubkeys_data = [pk._data if isinstance(pk, FFI.CData) else pk for pk in pubkeys]
291+
result = usecp256k1.ec_pubkey_combine(pubkeys_data)
230292
if result is None:
231293
return 0
232294
outpub[:] = result
@@ -236,6 +298,8 @@ def secp256k1_ec_pubkey_combine(self, ctx, outpub, pubkeys, n_pubkeys):
236298

237299
def secp256k1_ec_pubkey_tweak_add(self, ctx, pubkey, scalar):
238300
try:
301+
if isinstance(pubkey, FFI.CData):
302+
pubkey = pubkey._data
239303
result = usecp256k1.ec_pubkey_tweak_add(pubkey, scalar)
240304
if result is None:
241305
return 0
@@ -246,6 +310,8 @@ def secp256k1_ec_pubkey_tweak_add(self, ctx, pubkey, scalar):
246310

247311
def secp256k1_ec_pubkey_tweak_mul(self, ctx, pubkey, scalar):
248312
try:
313+
if isinstance(pubkey, FFI.CData):
314+
pubkey = pubkey._data
249315
result = usecp256k1.ec_pubkey_tweak_mul(pubkey, scalar)
250316
if result is None:
251317
return 0
@@ -256,6 +322,8 @@ def secp256k1_ec_pubkey_tweak_mul(self, ctx, pubkey, scalar):
256322

257323
def secp256k1_ec_pubkey_create(self, ctx, pubkey, privkey):
258324
try:
325+
if isinstance(pubkey, FFI.CData):
326+
pubkey = pubkey._data
259327
result = usecp256k1.ec_pubkey_create(privkey)
260328
if result is None:
261329
return 0
@@ -266,6 +334,10 @@ def secp256k1_ec_pubkey_create(self, ctx, pubkey, privkey):
266334

267335
def secp256k1_xonly_pubkey_from_pubkey(self, ctx, xonly_pubkey, pk_parity, pubkey):
268336
try:
337+
if isinstance(xonly_pubkey, FFI.CData):
338+
xonly_pubkey = xonly_pubkey._data
339+
if isinstance(pubkey, FFI.CData):
340+
pubkey = pubkey._data
269341
result, parity = usecp256k1.xonly_pubkey_from_pubkey(pubkey)
270342
if result is None:
271343
return 0
@@ -278,6 +350,8 @@ def secp256k1_xonly_pubkey_from_pubkey(self, ctx, xonly_pubkey, pk_parity, pubke
278350

279351
def secp256k1_ec_privkey_tweak_add(self, ctx, privkey, scalar):
280352
try:
353+
if isinstance(privkey, FFI.CData):
354+
privkey = privkey._data
281355
result = usecp256k1.ec_privkey_tweak_add(privkey, scalar)
282356
if result is None:
283357
return 0
@@ -288,6 +362,8 @@ def secp256k1_ec_privkey_tweak_add(self, ctx, privkey, scalar):
288362

289363
def secp256k1_ec_privkey_tweak_mul(self, ctx, privkey, scalar):
290364
try:
365+
if isinstance(privkey, FFI.CData):
366+
privkey = privkey._data
291367
result = usecp256k1.ec_privkey_tweak_mul(privkey, scalar)
292368
if result is None:
293369
return 0
@@ -298,6 +374,8 @@ def secp256k1_ec_privkey_tweak_mul(self, ctx, privkey, scalar):
298374

299375
def secp256k1_keypair_create(self, ctx, keypair, privkey):
300376
try:
377+
if isinstance(keypair, FFI.CData):
378+
keypair = keypair._data
301379
result = usecp256k1.keypair_create(privkey)
302380
if result is None:
303381
return 0
@@ -308,6 +386,8 @@ def secp256k1_keypair_create(self, ctx, keypair, privkey):
308386

309387
def secp256k1_ecdh(self, ctx, output, pubkey, seckey, hashfn=FFI.NULL, hasharg=FFI.NULL):
310388
try:
389+
if isinstance(pubkey, FFI.CData):
390+
pubkey = pubkey._data
311391
result = usecp256k1.ecdh(pubkey, seckey)
312392
if result is None:
313393
return 0

0 commit comments

Comments
 (0)