|
| 1 | +import os |
| 2 | +import base64 |
| 3 | +import binascii |
| 4 | +from bip_utils import * |
| 5 | +from cryptography.fernet import Fernet |
| 6 | +from cryptography.hazmat.primitives import hashes |
| 7 | +from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC |
| 8 | + |
| 9 | + |
| 10 | +def pipeline(password, mnemonic = None, xprv = None): |
| 11 | + assert not (mnemonic == None and xprv == None) |
| 12 | + |
| 13 | + salt = b"\xff"*32 |
| 14 | + kdf = PBKDF2HMAC( |
| 15 | + algorithm=hashes.SHA256(), |
| 16 | + length=32, |
| 17 | + salt=salt, |
| 18 | + iterations=10_000_000, |
| 19 | + ) |
| 20 | + key = kdf.derive(password.encode()) |
| 21 | + |
| 22 | + if mnemonic: |
| 23 | + # Generate seed |
| 24 | + seed_bytes = Bip39SeedGenerator(mnemonic).Generate("") |
| 25 | + print("Seed:", seed_bytes.hex()) |
| 26 | + # BIP32 master key for Bitcoin (NOT SLIP-0010) |
| 27 | + bip32_mst = Bip32Secp256k1.FromSeed(seed_bytes) |
| 28 | + xprv = bip32_mst.PrivateKey().ToExtended() |
| 29 | + |
| 30 | + |
| 31 | + bip32_mst = Bip32Slip10Secp256k1.FromExtendedKey(xprv) |
| 32 | + |
| 33 | + raw_priv_hex = bip32_mst.PrivateKey().Raw().ToHex() |
| 34 | + chain_code_hex = bip32_mst.PrivateKey().ChainCode().ToHex() |
| 35 | + |
| 36 | + |
| 37 | + print("Master xprv:", xprv) |
| 38 | + print("Raw priv:", raw_priv_hex) |
| 39 | + print("Chain code:", chain_code_hex) |
| 40 | + |
| 41 | + |
| 42 | + a=int(key.hex(),16) |
| 43 | + b=int(chain_code_hex,16) |
| 44 | + c=int(raw_priv_hex,16) |
| 45 | + |
| 46 | + A,B = a ^ b, a ^ c |
| 47 | + |
| 48 | + chain_code_hex = '{:064x}'.format(A) |
| 49 | + raw_priv_hex = '{:064x}'.format(B) |
| 50 | + |
| 51 | + # Reconstruct manually |
| 52 | + priv_key_bytes = binascii.unhexlify(raw_priv_hex) |
| 53 | + chain_code_bytes = binascii.unhexlify(chain_code_hex) |
| 54 | + |
| 55 | + # --- VERSION BYTES (4-byte sequences) for Bitcoin mainnet --- |
| 56 | + xpub_ver = b"\x04\x88\xAD\xE4" |
| 57 | + xprv_ver = b"\x04\x88\xB2\x1E" |
| 58 | + net_versions = Bip32KeyNetVersions(xprv_ver, xpub_ver) |
| 59 | + |
| 60 | + # --- Build Bip32KeyData (master key example: depth=0, index=0, parent fingerprint = 0) --- |
| 61 | + key_data = Bip32KeyData( |
| 62 | + depth=0, |
| 63 | + index=0, |
| 64 | + chain_code=Bip32ChainCode(chain_code_bytes), |
| 65 | + parent_fprint=b"\x00\x00\x00\x00" |
| 66 | + ) |
| 67 | + |
| 68 | + # --- Construct Bip32 object from private key + key_data --- |
| 69 | + bip32 = Bip32Slip10Secp256k1.FromPrivateKey(priv_key_bytes, key_data, net_versions) |
| 70 | + |
| 71 | + # --- Export extended private/public keys --- |
| 72 | + xprv_rec = bip32.PrivateKey().ToExtended() |
| 73 | + #assert xprv == xprv_rec |
| 74 | + |
| 75 | + print("Reconstructed xprv:", xprv_rec) |
| 76 | + |
| 77 | + print("To print: ") |
| 78 | + print("="*30) |
| 79 | + for i in range(6): print(xprv_rec[i*22:(i+1)*22]) |
| 80 | + print("="*30) |
| 81 | + return xprv_rec |
| 82 | + |
| 83 | +password = "password" |
| 84 | + |
| 85 | +#mnemonic = "panel whisper dutch urge chicken despair ladder together target consider vague robot" |
| 86 | +#xprv = "xprv9s21ZrQH143K2vNDXNVDkJFaeR2P8jU4f2H3KDaY9raPytJ9Ht6ru4KZe8JV6DYZcWAU2uWi1psNkx6ZmvZPT6kEKTNBToUi3n8Ry9sL18m" |
| 87 | + |
| 88 | +mnemonic = "panel whisper dutch urge chicken despair ladder together target consider vague robot" |
| 89 | +xprv = None |
| 90 | +xprv_rec = pipeline(password, mnemonic, xprv) |
| 91 | + |
| 92 | +mnemonic = None |
| 93 | +pipeline(password, mnemonic, xprv_rec) |
| 94 | + |
0 commit comments