-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathextract_pubkey_from_tx.py
83 lines (69 loc) · 2.02 KB
/
extract_pubkey_from_tx.py
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
79
80
81
82
83
stx = """
{
"expiration": "2020-06-24T23:04:03",
"ref_block_num": 50971,
"ref_block_prefix": 2411618883,
"max_net_usage_words": 0,
"max_cpu_usage_ms": 0,
"delay_sec": 0,
"context_free_actions": [],
"actions": [{
"account": "evolutiondex",
"name": "nop",
"authorization": [{
"actor": "evolutiondex",
"permission": "active"
}
],
"data": ""
}
],
"transaction_extensions": [],
"signatures": [
"SIG_K1_JveJGJxRvsCGamQpugco2JrZwC2HVM7rkdCkhrZeytPgb8qHyk9nEQEsWuwJej23hUUUttTWTpKsKDZ29exgSWyyHZxVzK"
],
"context_free_data": []
}
"""
import json
tx = json.loads(stx)
from ueosio.ds import DataStream
from cryptos import ecdsa_raw_recover, decode_sig
from cryptos import from_byte_to_int, decode, encode_pubkey
import hashlib
import binascii
dsp = DataStream()
dsp.pack_transaction(tx)
packed_trx = dsp.getvalue()
# m = hashlib.sha256()
# m.update(packed_trx)
# tx_id = binascii.hexlify(m.digest()).decode('utf-8')
#print(tx_id)
def get_sig_hash(chain_id):
zeros = '0000000000000000000000000000000000000000000000000000000000000000'
ds = DataStream()
ds.pack_checksum256(chain_id)
ds.write(packed_trx)
ds.pack_checksum256(zeros)
m = hashlib.sha256()
m.update(ds.getvalue())
return binascii.hexlify(m.digest()).decode('utf-8')
sig_hash = get_sig_hash("aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906")
#print(sig_hash)
def print_pub(sig_hash, sig):
ds = DataStream()
print("Signature => %s" % sig)
ds.pack_signature(sig)
sig0 = ds.getvalue()[1:]
#print(len(sig0))
#print(sig0)
v,r,s = from_byte_to_int(sig0[0]), decode(sig0[1:33], 256), decode(sig0[33:], 256)
# v,r,s = decode_sig(sig0[1:])
#print(v,r,s)
Q = ecdsa_raw_recover(sig_hash, (v,r,s))
pub = encode_pubkey(Q, 'hex_compressed') if v >= 31 else encode_pubkey(Q, 'hex')
#print(pub)
ds = DataStream(bytes.fromhex("00"+pub))
pub0 = ds.unpack_public_key()
print(" |----> ", pub0)
print_pub(sig_hash, tx['signatures'][0])