-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
78 lines (59 loc) · 2.13 KB
/
Copy pathutil.py
File metadata and controls
78 lines (59 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import secp256k1
from eth_keys import KeyAPI
import config
def convert_json_key_to_public_key_bytes(json_key):
pubkey = bytearray()
pubkey.append(0x04)
pubkey.extend(json_key.x)
pubkey.extend(json_key.y)
pubkey = bytes(pubkey)
return pubkey
def public_key_to_address(public_key):
return KeyAPI.PublicKey(public_key_bytes=public_key).to_checksum_address()
def int_to_bytes(x: int) -> bytes:
return x.to_bytes((x.bit_length() + 7) // 8, 'big')
def int_from_bytes(xbytes: bytes) -> int:
return int.from_bytes(xbytes, 'big')
def is_high(s):
return s > config.HALF_CURV_ORDER
def make_canonical(sig_bytes):
r = sig_bytes[0:32]
s = sig_bytes[32:64]
r_int = int_from_bytes(r)
s_int = int_from_bytes(s)
if is_high(s_int):
s_int = config.CURVE_ORDER - s_int
canonical = bytearray()
r = int_to_bytes(r_int)
s = int_to_bytes(s_int)
canonical.extend(r)
canonical.extend(s)
return canonical
def convert_azure_secp256k1_signature_to_vrs(pub_key_bytes, msg_hash_bytes, sig_bytes, chain_id=0):
sig_bytes = bytes(make_canonical(sig_bytes))
# Check the signature is still valid
ecdsa_pubkey = secp256k1.PublicKey(pubkey=pub_key_bytes, raw=True)
if len(sig_bytes)<64:
return 0,0,0, False
sig_ser = ecdsa_pubkey.ecdsa_deserialize_compact(sig_bytes)
verified_ecdsa = ecdsa_pubkey.ecdsa_verify(msg_hash_bytes, sig_ser, raw=True)
v = -1
unrelated = MyECDSA()
for i in range(0, 2):
recsig = unrelated.ecdsa_recoverable_deserialize(sig_bytes, i)
pubkey_recovered = unrelated.ecdsa_recover(msg_hash_bytes, recsig, raw=True)
pubser = secp256k1.PublicKey(pubkey_recovered).serialize(compressed=False)
if pubser == pub_key_bytes:
v = i
break
assert v == 0 or v == 1
v += 27
if chain_id > 0:
v += chain_id * 2 + 8
r = sig_bytes[0:32]
s = sig_bytes[32:64]
v = v
return v, int_from_bytes(r), int_from_bytes(s), True
class MyECDSA(secp256k1.Base, secp256k1.ECDSA):
def __init__(self):
secp256k1.Base.__init__(self, ctx=None, flags=secp256k1.ALL_FLAGS)