-
Notifications
You must be signed in to change notification settings - Fork 12
/
Ethereum_wallet_balance.py
109 lines (95 loc) · 4.01 KB
/
Ethereum_wallet_balance.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
import binascii, hashlib, hmac, struct
import mnemonic
import requests
import simplejson
import cryptowallethash
from ecdsa.curves import SECP256k1
from eth_utils import to_checksum_address, keccak as eth_utils_keccak
BIP39_PBKDF2_ROUNDS = 2048
BIP39_SALT_MODIFIER = "mnemonic"
BIP32_PRIVDEV = 0x80000000
BIP32_CURVE = SECP256k1
BIP32_SEED_MODIFIER = b'Bitcoin seed'
ETH_DERIVATION_PATH = "m/44'/60'/0'/0"
class PublicKey:
def __init__(self, private_key):
self.point = int.from_bytes(private_key, byteorder='big') * BIP32_CURVE.generator
def __bytes__(self):
xstr = self.point.x().to_bytes(32, byteorder='big')
parity = self.point.y() & 1
return (2 + parity).to_bytes(1, byteorder='big') + xstr
def address(self):
x = self.point.x()
y = self.point.y()
s = x.to_bytes(32, 'big') + y.to_bytes(32, 'big')
return to_checksum_address(eth_utils_keccak(s)[12:])
def mnemonic_to_bip39seed(mnemonic, passphrase):
mnemonic = bytes(mnemonic, 'utf8')
salt = bytes(BIP39_SALT_MODIFIER + passphrase, 'utf8')
return cryptowallethash.pbkdf2_hmac('sha512', mnemonic, salt, BIP39_PBKDF2_ROUNDS)
def bip39seed_to_bip32masternode(seed):
k = seed
h = hmac.new(BIP32_SEED_MODIFIER, seed, hashlib.sha512).digest()
key, chain_code = h[:32], h[32:]
return key, chain_code
def derive_bip32childkey(parent_key, parent_chain_code, i):
assert len(parent_key) == 32
assert len(parent_chain_code) == 32
k = parent_chain_code
if (i & BIP32_PRIVDEV) != 0:
key = b'\x00' + parent_key
else:
key = bytes(PublicKey(parent_key))
d = key + struct.pack('>L', i)
while True:
h = hmac.new(k, d, hashlib.sha512).digest()
key, chain_code = h[:32], h[32:]
a = int.from_bytes(key, byteorder='big')
b = int.from_bytes(parent_key, byteorder='big')
key = (a + b) % BIP32_CURVE.order
if a < BIP32_CURVE.order and key != 0:
key = key.to_bytes(32, byteorder='big')
break
d = b'\x01' + h[32:] + struct.pack('>L', i)
return key, chain_code
def parse_derivation_path(str_derivation_path):
path = []
if str_derivation_path[0:2] != 'm/':
raise ValueError("Can't recognize derivation path. It should look like \"m/44'/60/0'/0\".")
for i in str_derivation_path.lstrip('m/').split('/'):
if "'" in i:
path.append(BIP32_PRIVDEV + int(i[:-1]))
else:
path.append(int(i))
return path
def mnemonic_to_private_key(mnemonic, str_derivation_path, passphrase=""):
derivation_path = parse_derivation_path(str_derivation_path)
bip39seed = mnemonic_to_bip39seed(mnemonic, passphrase)
master_private_key, master_chain_code = bip39seed_to_bip32masternode(bip39seed)
private_key, chain_code = master_private_key, master_chain_code
for i in derivation_path:
private_key, chain_code = derive_bip32childkey(private_key, chain_code, i)
return private_key
if __name__ == '__main__':
f = open('result_eth.txt', 'a')
mobj = mnemonic.Mnemonic("english")
while True:
mnemonic_words = mobj.generate(strength=128)
private_key = mnemonic_to_private_key(mnemonic_words, str_derivation_path=f'{ETH_DERIVATION_PATH}/0')
public_key = PublicKey(private_key)
balance_url = "https://api-eu1.tatum.io/v3/ethereum/account/balance/" + public_key.address()
r = requests.get(balance_url, headers={"x-api-key":"55546c04-d5ee-4304-8053-50053377835f"})
try:
r = r.json()
# print(r)
if "balance" in r:
btc = float(r['balance'])
if btc > 0:
f.write("seed phrase: " + mnemonic_words + "\t" + "Bal: " + str(btc) + " ETH.\n")
text1 = "Bal: " + str(btc) + " ETH."
print("seed phrase: {:<90} {:<15}".format(mnemonic_words, text1))
else:
print("Fatal error")
except simplejson.errors.JSONDecodeError:
print("API error")