Generate private key from seed phrase python

I basically followed the instructions here How to generate mycelium addresses from the 12 words in python

So my code is similar:

from bip32utils import BIP32Key
from bip32utils import BIP32_HARDEN
from bip32utils import Base58
import os, bip39

strength_bits = 128
entropy = os.urandom[strength_bits // 8]
wallet_generator = bip39.Mnemonic['english']
mnemonic = wallet_generator.to_mnemonic[entropy]
assert wallet_generator.to_entropy[mnemonic] == entropy  # see, bijective!

# Or specify the mnemonic directly if you prefer:
mnemonic = 'aware report movie exile buyer drum poverty supreme gym oppose float elegant'
passphrase = 'test'

seed = bip39.Mnemonic.to_seed[mnemonic, passphrase=passphrase]
key = BIP32Key.fromEntropy[seed]
account_number = 0
i = 0
print "Address: " + key.ChildKey[44 + BIP32_HARDEN] \
         .ChildKey[0 + BIP32_HARDEN] \
         .ChildKey[account_number + BIP32_HARDEN] \
         .ChildKey[0] \
         .ChildKey[i] \
         .Address[]

And I verified it using //iancoleman.io/bip39/#english that the generated address is indeed the first address that this webpage generated as well. However, I also want to get the public and private key pairs using this same library. I originally tried:

print "Public Key: " + Base58.check_encode[key.ChildKey[44 + BIP32_HARDEN] \
         .ChildKey[0 + BIP32_HARDEN] \
         .ChildKey[account_number + BIP32_HARDEN] \
         .ChildKey[0] \
         .ChildKey[i] \
         .PublicKey[]]

print "Private Key: " + Base58.check_encode[key.ChildKey[44 + BIP32_HARDEN] \
         .ChildKey[0 + BIP32_HARDEN] \
         .ChildKey[account_number + BIP32_HARDEN] \
         .ChildKey[0] \
         .ChildKey[i] \
         .PrivateKey[]]

However, the output of these two calls are not the same as the ones provided by the website above for the same address.

So my question is: what's the proper way for me to generate the public and private key pairs?

Edit: To clarify, for the exact mnemonic and passphrase above, the website I'm using for reference tells me the first address and keypair should be:

While the output of the above python code is:

Address: 1K6WQtD7bLQ5nQ14GyBV33mBWSbkiRKhQs
Public Key: 62Yi9HBYYagf8CY1Ve2fquHKjBqAA7GFjGUUtkUHbkP5PHzv3W
Private Key: EGHMsAp7nY7Jo9F589zCU227KBLTDhiwRq5vYVvRVZxJNPJn4

So the address matches, but not the keypair.

NotATether [OP]

Legendary


Offline

Activity: 1008
Merit: 3855

aka. The Underboss

Need to quickly get some Python for deriving private keys at a path given a seed, as I'm trying to create payment addresses for users on my website. [The addresses will be used in my Flask server and stored in a database.]

The seed phrase itself is going to be stored in some libsodium-kind of container, so no need to worry about that.

Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.

ETFbitcoin

Legendary


Offline

Activity: 2282
Merit: 4973

PNNV.COM Live bitcoin price monitor

bitcoinlib library should do the job. I've made sure there's option to enter your own BIP39 seed phrase and path. Here's example code [i checked the result with //iancoleman.io/bip39/],

Code:

>>> from bitcoinlib.wallets import Wallet
>>> mnemonic = "jaguar laptop random problem corn nut fun happy maximum federal wrap address"
>>> w = Wallet.create['Wallet1', keys=mnemonic, network='bitcoin']
>>> k = w.key_for_path[[0, 0]]
>>> k.address
'17AmSBNzGunnuPzCiJoTV2mdqu4v9Jt7QY'
>>> k = w.key_for_path[[0, 1]]
>>> k.address
'1Q9UgCADh26mfVaA5xmKxiCLZuiCG1g3zr'

Can you get private key from seed phrase?

The seed phrase enables a wallet to derive your private key. A private key to a cryptocurrency wallet is the equivalent of an ATM PIN to a bank account. Bank accounts have a unique PIN, which proves to the ATM that a user owns the account. Using the PIN, anyone can spend funds from the account.

Is private key the same as seed?

Private key is usually an access key to just one address [account], while seed phrase is an access key to the whole wallet, which can hold multiple addresses.

What is BIP39 seed?

BIP39 is a design implementation that lays out how cryptocurrency wallets create the set of words [or "mnemonic codes"] that make up a mnemonic sentence, and how the wallet turns them into a binary "seed" that is used to create encryption keys, which are then are used to execute cryptocurrency transactions.

Chủ Đề