ebellocchia / bip_utils

Generation of mnemonics, seeds, private/public keys and addresses for different types of cryptocurrencies
MIT License
324 stars 86 forks source link

Please show how to generate a private key valid for metamask import #69

Closed tdiesler closed 2 years ago

tdiesler commented 2 years ago

For a given mnemonic, I can generate a number of ETH addresses that correspond to what Metamask and MEW show. From Metamask I can then export the private key to use in uniswap_python

None of the keys that I get through bip44_ctx.PrivateKey().Raw().ToHex() correspond to the metamask key, nor are they usable with uniswap

Could you pls show how to get a usable private key for these addresses from bip_utils?

roof blush uncover blouse basic retire bundle clinic champion guilt make globe

Metamask: 0xe6d13d33cd3ca24071b640fa0d33cf178006c7c8945f04f5db796bb3c8694482

Master-Key: 0xca557027464a183ebabf3db4497db106c44c341c1599aa191007c38888bc8c64
Account-Key: 0xda875f44f8a5aca81a72d9818d7fb91517409cb5c873ed668227185aa1e94ca1
Change-Key: 0xcadd8f17e40b541384273a5e5602b71eaf11ed34c1a88cf209119c5c7fabe645

m/44'/60'/0'/0/0 - 0xeF869F5f88268FbF9B3698F25A87Ea5B77690D1f
m/44'/60'/0'/0/1 - 0xA5E5D00F828F930d56c0DC7cEb059376dB4A257F
m/44'/60'/0'/0/2 - 0xf38FFAA9578C6935534403109A113aee59799132
m/44'/60'/0'/0/3 - 0xC07a971baCC0157651f46322D594Ef66915aDff8
m/44'/60'/0'/0/4 - 0xa16E4d321053cbA37A8D25cAeaF6b7667E417f14
tdiesler commented 2 years ago

Here is the code ...

# Mnemonic can be generated with Bip39MnemonicGenerator
mnemonic = Bip39MnemonicGenerator().FromWordsNumber(Bip39WordsNum.WORDS_NUM_12)
mnemonic = "roof blush uncover blouse basic retire bundle clinic champion guilt make globe"
priv_key = "0xe6d13d33cd3ca24071b640fa0d33cf178006c7c8945f04f5db796bb3c8694482"
log.info(mnemonic)
log.info("Metamask: %s", priv_key)

# Generate with automatic language detection and passphrase (empty)
seed_bytes = Bip39SeedGenerator(mnemonic).Generate()

# Create from seed
bip44_mst_ctx = Bip44.FromSeed(seed_bytes, Bip44Coins.ETHEREUM)
log.info("MPrivKey: 0x%s", bip44_mst_ctx.PrivateKey().Raw().ToHex())

# Derive account 0 for Ethereum: m/44'/60'/0'
bip44_acc_ctx = bip44_mst_ctx.Purpose().Coin().Account(0)
log.info("APrivKey: 0x%s", bip44_acc_ctx.PrivateKey().Raw().ToHex())

# Derive the external chain: m/44'/60'/0'/0
bip44_chg_ctx = bip44_acc_ctx.Change(Bip44Changes.CHAIN_EXT)
log.info("CPrivKey: 0x%s", bip44_chg_ctx.PrivateKey().Raw().ToHex())

# Derive the first 20 addresses of the external chain: m/44'/60'/0'/0/i
for i in range(5):
    bip44_addr_ctx = bip44_chg_ctx.AddressIndex(i)
    log.info("m/44'/60'/0'/0/%d - %s", i, bip44_addr_ctx.PublicKey().ToAddress())
tdiesler commented 2 years ago

The Metamask private key is that of the derived address

m/44'/60'/0'/0/0 - 0xeF869F5f88268FbF9B3698F25A87Ea5B77690D1f e6d13d33cd3ca24071b640fa0d33cf178006c7c8945f04f5db796bb3c8694482
m/44'/60'/0'/0/1 - 0xA5E5D00F828F930d56c0DC7cEb059376dB4A257F e32cb779da181e342397b8c7eebd99d0c17fc621592892369b3245632f676c54
m/44'/60'/0'/0/2 - 0xf38FFAA9578C6935534403109A113aee59799132 e845005cb693e2d6c83df045e886c454e73807513b521e545f623f205ec7c498
m/44'/60'/0'/0/3 - 0xC07a971baCC0157651f46322D594Ef66915aDff8 e6790d009b4453d15295c545f6465412cc6741b14c26189db01ad764841fcdd3
m/44'/60'/0'/0/4 - 0xa16E4d321053cbA37A8D25cAeaF6b7667E417f14 a6b25fa0c3aae7dced3f33a31a01dcd8930a44a198adf58633e0705bf8ed8113