I'm interested in generating an Ethereum public key from a private key using Python. I've tried googling around and found some resources but these are all JS nothing using Python itself.
Asked
Active
Viewed 8,062 times
6
-
try out this link https://stackoverflow.com/questions/39074253/extract-publickey-from-privatekey-input-using-python – Nikhil Aug 21 '18 at 09:37
-
2I'm not very into these things but maybe that's your answer. https://github.com/ethereum/eth-keys#publickeyfrom_privateprivate_key---publickey – Tom Wojcik Aug 21 '18 at 09:38
2 Answers
10
Public Key vs Address
An account's address is the last 20 bytes of the keccak256 of the public key. Most tasks in Ethereum require the address instead of the public key.
Getting the Public Key
Install eth_keys with pip install eth-keys
from eth_keys import keys
from eth_utils import decode_hex
priv_key_bytes = decode_hex('0x44b9abf2708d9adeb1722dcc1e61bef14e5611dee710d66f106e356a111bef90')
priv_key = keys.PrivateKey(priv_key_bytes)
pub_key = priv_key.public_key
assert pub_key.to_hex() == '0xcabb8a3a73ea4a03d025a6ac2ebbbb19a545e4fb10e791ec9b5c942d77aa20760f64e4604cdfbec665435a382a8c9bfd560c6f0fca8a2708cda302f658368b36'
Getting the Address
Just in case the question was intending to ask about the address...
There are simpler ways to generate the address from scratch, but since we've already done the eth-keys setup, this is a one-liner:
assert pub_key.to_checksum_address() == '0xa0784ba3fcea41fD65a7A47b4cc1FA4C3DaA326f'

carver
- 2,229
- 12
- 28
-
1Nice information. I used to think that eth address is also the public key – Yohanes Gultom Aug 21 '18 at 23:21
-
0
Too long for a comment. I'm not exactly sure how ethereum works and what exact problem you are facing.
But a quick google gave me these links
A reddit answer to something similar you are asking
The link which is being referred seems broken, so I did some backtracking, and here is the probable link to the referred code

Arghya Saha
- 5,599
- 4
- 26
- 48