DILSHAD737 commited on
Commit
9cd6dad
·
1 Parent(s): 8b63f7f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # debug_which.py
2
+ from mnemonic import Mnemonic
3
+ from eth_account import Account
4
+ from eth_utils import keccak, to_checksum_address
5
+ import coincurve, hmac, hashlib
6
+
7
+ Account.enable_unaudited_hdwallet_features()
8
+ phrase = "lesson cushion crane affair federal decline limb spend reunion chief cart zebra"
9
+ TARGET = "0x3f0482fc3e9132c3d3acb5f588e980d7b3a5af07"
10
+
11
+ # --- Manual BIP32 ---
12
+ _N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
13
+ _H = 0x80000000
14
+ def _child(priv, chain, i):
15
+ if i >= _H:
16
+ data = b'\x00' + priv + i.to_bytes(4, 'big')
17
+ else:
18
+ pub = coincurve.PrivateKey(priv).public_key.format(compressed=True)
19
+ data = pub + i.to_bytes(4, 'big')
20
+ I = hmac.new(chain, data, hashlib.sha512).digest()
21
+ ki = (int.from_bytes(I[:32], 'big') + int.from_bytes(priv, 'big')) % _N
22
+ return ki.to_bytes(32, 'big'), I[32:]
23
+
24
+ def seed_to_privkey_hd(seed, idx=0):
25
+ I = hmac.new(b"Bitcoin seed", seed, hashlib.sha512).digest()
26
+ key, chain = I[:32], I[32:]
27
+ for i in [44+_H, 60+_H, _H, 0, idx]:
28
+ key, chain = _child(key, chain, i)
29
+ return key
30
+
31
+ # Test 1: Python mnemonic + manual BIP32
32
+ seed_py = Mnemonic("english").to_seed(phrase, "")
33
+ priv_py = seed_to_privkey_hd(seed_py, 0)
34
+ pub = coincurve.PrivateKey(priv_py).public_key.format(compressed=False)[1:]
35
+ addr_manual = to_checksum_address(keccak(pub)[-20:])
36
+
37
+ # Test 2: eth_account
38
+ acct = Account.from_mnemonic(phrase, account_path="m/44'/60'/0'/0/0")
39
+
40
+ print(f"Manual BIP32: {addr_manual}")
41
+ print(f"eth_account: {acct.address}")
42
+ print(f"Target: {TARGET}")
43
+ print(f"Manual == Target: {addr_manual.lower() == TARGET}")
44
+ print(f"eth_account == Target: {acct.address.lower() == TARGET}")
45
+ print(f"Manual == eth_account: {addr_manual.lower() == acct.address.lower()}")