<p>Please try the following demo, and the result is same with <a href="https://iancoleman.io/bip39/" rel="noreferrer">https://iancoleman.io/bip39/</a>.</p>

<pre><code>$ python3.6 test.py
{'addr': '1A9vZ4oPLb29szfRWVFe1VoEe7a2qEMjvJ',
 'coin': 'BTC',
 'mnemonic_words': 'aware report movie exile buyer drum poverty supreme gym oppose float elegant',
 'privatekey': 'L3g3hhYabnBFbGqd7qReebwCrRkGhAzaX4cBpYSv5S667sWJAn5A',
 'publickey': '029dc2912196f2ad7a830747c2490287e4ff3ea52c417598681a955dcdf473b6c0'}
</code></pre>

<hr>

<pre><code>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import pprint
import binascii
import mnemonic
import bip32utils

def bip39(mnemonic_words):
    mobj = mnemonic.Mnemonic("english")
    seed = mobj.to_seed(mnemonic_words)

    bip32_root_key_obj = bip32utils.BIP32Key.fromEntropy(seed)
    bip32_child_key_obj = bip32_root_key_obj.ChildKey(
        44 + bip32utils.BIP32_HARDEN
    ).ChildKey(
        0 + bip32utils.BIP32_HARDEN
    ).ChildKey(
        0 + bip32utils.BIP32_HARDEN
    ).ChildKey(0).ChildKey(0)

    # return {
    #     'mnemonic_words': mnemonic_words,
    #     'bip32_root_key': bip32_root_key_obj.ExtendedKey(),
    #     'bip32_extended_private_key': bip32_child_key_obj.ExtendedKey(),
    #     'bip32_derivation_path': "m/44'/0'/0'/0",
    #     'bip32_derivation_addr': bip32_child_key_obj.Address(),
    #     'coin': 'BTC'
    # }

    return {
        'mnemonic_words': mnemonic_words,
        # 'bip32_root_key': bip32_root_key_obj.ExtendedKey(),
        # 'bip32_extended_private_key': bip32_child_key_obj.ExtendedKey(),
        # 'path': "m/44'/0'/0'/0",
        'addr': bip32_child_key_obj.Address(),
        'publickey': binascii.hexlify(bip32_child_key_obj.PublicKey()).decode(),
        'privatekey': bip32_child_key_obj.WalletImportFormat(),
        'coin': 'BTC'
    }


if __name__ == '__main__':
    mnemonic_words = "aware report movie exile buyer drum poverty supreme gym oppose float elegant"
    pprint.pprint(bip39(mnemonic_words))
</code></pre>

CC BY-SA 4.0
2020-02-29T09:46:33.870Z
How do you write the result in a txt file? python 3
STB1982
CC BY-SA 4.0
2021-08-19T11:26:31.383Z
@debug what if I want to do this for ether rather than Bitcoin? what aspects of the code I should change to do this?
Anonymous
CC BY-SA 4.0
2019-02-14T10:42:02.547Z
2019-02-14T10:42:02.547Z
debug
<p>The public key should be in hex as @MohamedLEGH pointed out above. Regarding the private key you have to append the network code to it first. See step 2 <a href="https://en.bitcoin.it/wiki/Wallet_import_format" rel="nofollow noreferrer">here</a>.</p>

CC BY-SA 4.0
2018-12-14T10:08:14.143Z
2018-12-14T10:08:14.143Z
Abdussamad
<p>I found the problem I think.</p>

<p>You should not do the Base58.check_encode </p>

<p>To print the PublicKey, use the .hex() method.</p>

<p>Also, <a href="https://iancoleman.io/bip39/#english" rel="nofollow noreferrer">https://iancoleman.io/bip39/#english</a> print Private key in the WIF form so you should do .WalletImportFormat() instead of .PrivateKey() if you want do have the same output as iancoleman.io . This time, no need to use .hex()
(again no need to encode in base58)</p>

CC BY-SA 4.0
2018-08-07T19:34:34.870Z
2018-08-07T19:34:34.870Z
MohamedLEGH
<p>I basically followed the instructions here <a href="https://bitcoin.stackexchange.com/questions/62491/how-to-generate-mycelium-addresses-from-the-12-words-in-python?newreg=2a489942b68e4c0795498a6d322213bf">How to generate mycelium addresses from the 12 words in python</a></p>

<p>So my code is similar:</p>

<pre><code>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()
</code></pre>

<p>And I verified it using <a href="https://iancoleman.io/bip39/#english" rel="noreferrer">https://iancoleman.io/bip39/#english</a> 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:</p>

<pre><code>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())
</code></pre>

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

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

<p>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:
<a href="https://i.sstatic.net/lvmdJ.png" rel="noreferrer"><img src="https://i.sstatic.net/lvmdJ.png" alt="enter image description here"></a></p>

<p>While the output of the above python code is:</p>

<pre><code>Address: 1K6WQtD7bLQ5nQ14GyBV33mBWSbkiRKhQs
Public Key: 62Yi9HBYYagf8CY1Ve2fquHKjBqAA7GFjGUUtkUHbkP5PHzv3W
Private Key: EGHMsAp7nY7Jo9F589zCU227KBLTDhiwRq5vYVvRVZxJNPJn4
</code></pre>

<p>So the address matches, but not the keypair. </p>

CC BY-SA 4.0
2018-06-27T11:07:12.910Z
take a look at this: https://bitcointalk.org/index.php?topic=1060451.0
Zombie
CC BY-SA 4.0
2018-06-28T05:14:42.977Z
Are you sure it's not matching? I see you are using `PublicKey()` vs`Address()`, did you want the public key or the address?
m1xolyd1an
CC BY-SA 4.0
2018-06-28T06:19:01.327Z
I want the public key and the private key. The address is already correct. I've edited the question to add the sample output of both the reference webpage and the python script for reference
Stormy Dan
<python><bip32-hd-wallets><mnemonic-seed><mycelium-wallet><bip44>
How to generate public and private key pairs from the 12 seed words in python
Stormy Dan
# How to generate public and private key pairs from the 12 seed words in python

(Asked by: username_0 on 2018-06-27)

I basically followed the instructions here [How to generate mycelium addresses from the 12 words in python](https://bitcoin.stackexchange.com/questions/62491/how-to-generate-mycelium-addresses-from-the-12-words-in-python?newreg=2a489942b68e4c0795498a6d322213bf)

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 <https://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:
[![enter image description here](https://i.sstatic.net/lvmdJ.png)](https://i.sstatic.net/lvmdJ.png)

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.

**Tags:** python, bip32-hd-wallets, mnemonic-seed, mycelium-wallet, bip44

> **Comments:**
> **username_1**: take a look at this: https://bitcointalk.org/index.php?topic=1060451.0  
> **username_2**: Are you sure it's not matching? I see you are using `PublicKey()` vs`Address()`, did you want the public key or the address?  
> **username_0**: I want the public key and the private key. The address is already correct. I've edited the question to add the sample output of both the reference webpage and the python script for reference  

---

## 3 answers

### Answer by username_3 on 2019-02-14. Score: 6

Please try the following demo, and the result is same with <https://iancoleman.io/bip39/>.

```
$ python3.6 test.py
{'addr': '1A9vZ4oPLb29szfRWVFe1VoEe7a2qEMjvJ',
 'coin': 'BTC',
 'mnemonic_words': 'aware report movie exile buyer drum poverty supreme gym oppose float elegant',
 'privatekey': 'L3g3hhYabnBFbGqd7qReebwCrRkGhAzaX4cBpYSv5S667sWJAn5A',
 'publickey': '029dc2912196f2ad7a830747c2490287e4ff3ea52c417598681a955dcdf473b6c0'}

```

---

```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import pprint
import binascii
import mnemonic
import bip32utils

def bip39(mnemonic_words):
    mobj = mnemonic.Mnemonic("english")
    seed = mobj.to_seed(mnemonic_words)

    bip32_root_key_obj = bip32utils.BIP32Key.fromEntropy(seed)
    bip32_child_key_obj = bip32_root_key_obj.ChildKey(
        44 + bip32utils.BIP32_HARDEN
    ).ChildKey(
        0 + bip32utils.BIP32_HARDEN
    ).ChildKey(
        0 + bip32utils.BIP32_HARDEN
    ).ChildKey(0).ChildKey(0)

    # return {
    #     'mnemonic_words': mnemonic_words,
    #     'bip32_root_key': bip32_root_key_obj.ExtendedKey(),
    #     'bip32_extended_private_key': bip32_child_key_obj.ExtendedKey(),
    #     'bip32_derivation_path': "m/44'/0'/0'/0",
    #     'bip32_derivation_addr': bip32_child_key_obj.Address(),
    #     'coin': 'BTC'
    # }

    return {
        'mnemonic_words': mnemonic_words,
        # 'bip32_root_key': bip32_root_key_obj.ExtendedKey(),
        # 'bip32_extended_private_key': bip32_child_key_obj.ExtendedKey(),
        # 'path': "m/44'/0'/0'/0",
        'addr': bip32_child_key_obj.Address(),
        'publickey': binascii.hexlify(bip32_child_key_obj.PublicKey()).decode(),
        'privatekey': bip32_child_key_obj.WalletImportFormat(),
        'coin': 'BTC'
    }

if __name__ == '__main__':
    mnemonic_words = "aware report movie exile buyer drum poverty supreme gym oppose float elegant"
    pprint.pprint(bip39(mnemonic_words))

```

> **Comments:**
> **username_4**: How do you write the result in a txt file? python 3  
> **username_5**: @username_3 what if I want to do this for ether rather than Bitcoin? what aspects of the code I should change to do this?  

---

### Answer by username_6 on 2018-12-14. Score: 0

The public key should be in hex as @username_7 pointed out above. Regarding the private key you have to append the network code to it first. See step 2 [here](https://en.bitcoin.it/wiki/Wallet_import_format).


---

### Answer by username_7 on 2018-08-07. Score: 0

I found the problem I think.

You should not do the Base58.check\_encode

To print the PublicKey, use the .hex() method.

Also, <https://iancoleman.io/bip39/#english> print Private key in the WIF form so you should do .WalletImportFormat() instead of .PrivateKey() if you want do have the same output as iancoleman.io . This time, no need to use .hex()
(again no need to encode in base58)


---

