How to get an address from a private key on Bitcoin

How to get an address from a private key on Bitcoin

A description of how to get an address from a private key on Bitcoin, the simple way (Public Key Hash).

schema that describes the process to pass from a private key to an address

There is an other way

Just so you know, this is not the only way to get a Bitcoin address. Actually, I’m pretty sure your favourite wallet doesn’t use this method in order to produce Segwit-compatible address : an address not created from a public key but from a script (a set of operations which describes how your bitcoins can be spent : a smart contract 😉 ).

How to get a private key ?

You don’t actually get a private key, you generate one. You ask for a bank account, you take a Bitcoin account. The private key is nothing but a random number : to generate a private key you “just” have to generate a random number. I put “just” in quotes because it is impossible to generate a random number in informatic : you need a source of entropy (a source of randomness), for example bitaddress uses your mouse moves as an entropy source.

How to get the public key ?

A public key is derived from a private key. To derive the public key you need an Elliptic Curve, Bitcoin chose to use secp256k1. Your public key is your private key multiplied by the generator point (which is a constant set in the secp256k1 standard), so it’s a point on the curve. The security in this operation is based on the fact that on an elliptic curve you can kind of “multiply” but you can not divide : you cannot retrieve the private key by dividing you public key by the generator point. You can find more information about this process here.

How to get the address ?

The address is an encoded part of a hash of your public key. Because it is the last part of the post, let’s take a concrete example to do this part :

Generate a random private key :

EDCC6224FEE390A57C76C13A9BECC9502A6F3B1BF6F72B6ED11B83A0F0E3E9FC

Derive the public key from it :

04F3DF70315E569BBF9FB427DA65E60CE2E3660EA83EC8A8523DA4DE6901F7988E9E460CD594F27C9F6007A277820F3C1D8BB8485E1FCA38F37BCF9DC1A2DFA2A0

Pass it through the sha256 function, then the ripemd160 function :

C0CBEC6E4B3F537A68F64F65B68998158E211B92

Add 00to the begining. It is called “network byte” and means we are on Bitcoin main network.

00C0CBEC6E4B3F537A68F64F65B68998158E211B92

Then take the four first bytes of the sha256 hash of the sha256 hash of this word and append it to the end.

# The hash from which you take the first four bytes
B4AE3A0DCF1AAD584327FDB0974BBCBE3E19C2A6A2F9A29D7303C3A0D526910F
# The result
00C0CBEC6E4B3F537A68F64F65B68998158E211B92B4AE3A0D

Then base58check encode it :

1JaR2gwbg2vFvgHvshaL61HmCitaCGaBgQ

How to code it ?

If you are a developer interested in how these functions are coded, you can check the Python implementation I made for my Bitcoin library on github,here are links to specific functions :

Articles Similaires

Laisser une réponse