Address Generation

From Riecoin

For educational or development purposes, this page will explain how to generate a Riecoin Bech32 (P2WPKH) address from scratch.

Firstly, generate 256 random bits (32 bytes), which will be your private key. This number[1] can be represented in hexadecimal with 64 hex digits. For example, consider the private key


The hexadecimal string may directly be imported in a Riecoin wallet: since Riecoin Core 22.02, it is no longer needed to make an additional step to calculate a "WIF" private key, which did not really provide a lot of advantages in practice[2]. Of course, never share a private key that you use, in any form.

With this private key, generate the compressed public key[3] of 33 bytes using the ECDSA algorithm with the Secp256k1 elliptic curve, which should be done using an existing library. The public key associated to the private key is in hexadecimal

PubC = 02dc7438cb82428573712e4f1703bccb621f3b1d4b8ba88892f365253940cfb9f7

Now, compute the Witness Program, which takes 20 bytes, by hashing the public key with two algorithms,

RIPEMD160(SHA256(PubC)) = 0e8db9fd38a1ee20c196f625922dace944d833da

The Witness Program will now be converted to the Bech32 format. We will not show all the technical details, only general steps. To learn more, the Bitcoin's Bech32 specification and a reference code may be studied.

The first step of the conversion to Bech32 is to convert the witness program to Base 32. By hand, this can be done by using its binary representation and grouping the bits by 5


The base 32 digits written in decimal are:


Use this table to change these digits to letters from the Bech32 alphabet. 1 becomes p, 26 becomes 6, 6 becomes x,...

_0 _1 _2 _3 _4 _5 _6 _7 _8 _9
0_ q p z r y 9 x 8 g f
1_ 2 t v d w 0 s 3 j n
2_ 5 4 k h c e 6 m u a
3_ 7 l

We get


Such addresses are using the Witness Version 0, which is included in the Bech32 address by prepending the version (0 is represented by q according to the table):


Finally, use the human readable part (HRP) ric to generate the checksum, prepend with the HRP and delimiter 1, and append the checksum.


The generated Bech32 address is

Broken down: ric . 1 . q . p6xmnlfc58hzpsvk7cjeytdva9zdsv76 . uq03wu
             HRP . Delimiter . Witness Version . Witness Program . Checksum

For Testnet and Regtest, the respective tric and rric HRP must be used instead, and the addresses would have been


Notes and references

  1. It must be between 1 and fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140.
  2. The advantages are that they are a bit shorter, and the type of address and a checksum could be included. However, Private Keys should be copy pasted anyway, Bech32 adoption is basically 100% in Riecoin, and one can double check by reimporting the key and look at the address. Also Private Keys are normally not handled by most users. Not using WIF also save developers from the trouble of having to write or find a Base58 implementation.
  3. What is a compressed Bitcoin key?