Address Generation

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

2bc477088e399c94c9a3a2736663d1986b5261bf6ab33e54588b1f96d655ec4e

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

00001:11010:00110:11011:10011:11111:01001:11000:
10100:00111:10111:00010:00001:10000:01100:10110:
11110:11000:10010:11001:00100:01011:01101:01100:
11101:00101:00010:01101:10000:01100:11110:11010

The base 32 digits written in decimal are:

1:26:6:27:19:31:9:24:20:7:23:2:1:16:12:22:30:24:18:25:4:11:13:12:29:5:2:13:16:12:30:26

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

p6xmnlfc58hzpsvk7cjeytdva9zdsv76

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):

qp6xmnlfc58hzpsvk7cjeytdva9zdsv76

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

uq03wu

The generated Bech32 address is

ric1qp6xmnlfc58hzpsvk7cjeytdva9zdsv76uq03wu
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

tric1qp6xmnlfc58hzpsvk7cjeytdva9zdsv76tjvcak
rric1qp6xmnlfc58hzpsvk7cjeytdva9zdsv76w7dzdg

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?