-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathethereumkeytool.py
33 lines (24 loc) · 950 Bytes
/
ethereumkeytool.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
from Cryptodome.Hash import keccak # for Ethereum hashing
from library import priv_to_pub_raw,Account
############## Ethereum tools ##############
def keccak256(x): return keccak.new(digest_bits=256,data=x).digest()
def checksum_encode(addr): # Takes a 20-byte binary address as input
address = addr.lower().replace('0x', '')
hashaddress = keccak256(address.encode('utf-8')).hex()
ret = ''
for i in range(len(address)):
if (int(hashaddress[i], 16) >= 8):
ret += address[i].upper()
else:
ret += address[i]
return '0x' + ret
class EthereumAccount(Account):
def __init__(self,private=None):
super().__init__(private)
def to_pub(self):
return priv_to_pub_raw(self.pk)
def to_address(self,checksum=True):
address = '0x'+ keccak256(self.to_pub())[-20:].hex()
if checksum:
address = checksum_encode(address)
return address