Bech32: Difference between revisions

From Bitcoin Wiki
Jump to navigation Jump to search
Fresheneesz (talk | contribs)
stub
 
934 (talk | contribs)
mNo edit summary
 
(12 intermediate revisions by 7 users not shown)
Line 1: Line 1:
Bech32 is a segwit [[address]] format specified by [[BIP 0173]].
'''Bech32''' is a [[segwit]] [[address]] format specified by [[BIP 0173]] and [[BIP 0350]]. This address format is also known as "bc1 addresses". Bech32 is more efficient with block space. As of October 2020, the Bech32 address format is supported in many popular wallets and is the preferred address scheme.
 
See the page [[Bech32 adoption]] to track adoption.
 
=== Examples ===
 
Examples of the address format being used on mainnet are the TXIDs <code>4ef47f6eb681d5d9fa2f7e16336cd629303c635e8da51e425b76088be9c8744c</code> and <code>514a33f1d46179b89e1fea7bbb07b682ab14083a276979f91038369d1a8d689b</code>. And addresses <code>bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq</code> and <code>bc1qc7slrfxkknqcq2jevvvkdgvrt8080852dfjewde450xdlk4ugp7szw5tk9</code>.
 
== Creating a Bech32 address ==
 
A Bech32 address is created from a public key as follows:
 
1. Having a compressed public key (0x02 or 0x03 followed by 32 byte X coordinate):
<code>0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798</code>
 
2. Perform SHA-256 hashing on the public key:
<code>0f715baf5d4c2ed329785cef29e562f73488c8a2bb9dbc5700b361d54b9b0554</code>
 
3. Perform RIPEMD-160 hashing on the result of SHA-256:
<code>751e76e8199196d454941c45d1b3a323f1433bd6</code>
 
4. The result of step 3 is an array of 8-bit unsigned integers (base 2^8=256) and Bech32 encoding converts this to an array of 5-bit unsigned integers (base 2^5=32) so we “squash” the bytes to get:
 
in hex:
<code>0e140f070d1a001912060b0d081504140311021d030c1d03040f1814060e1e16</code>
 
in numbers:
<code>14 20 15 07 13 26 00 25 18 06 11 13 08 21 04 20 03 17 02 29 03 12 29 03 04 15 24 20 06 14 30 22</code>
 
5 bits binary:
<code>01110 10100 01111 00111 01101 11010 00000 11001 10010 00110 01011 01101 01000 10101 00100 10100 00011 10001 00010 11101 00011 01100 11101 00011 00100 01111 11000 10100 00110 01110 11110 10110</code>
 
5. Add the witness version byte in front of the step 4 result (current version is 0):
<code>000e140f070d1a001912060b0d081504140311021d030c1d03040f1814060e1e16</code>
 
6. Compute the checksum by using the data from step 5 and the H.R.P (bc for MainNet and tb for TestNet)
<code>0c0709110b15</code>
 
7. Append the checksum to result of step 5 (we now have an array of 5-bit integers):
<code>000e140f070d1a001912060b0d081504140311021d030c1d03040f1814060e1e160c0709110b15</code>
 
8. Map each value to its corresponding character in Bech32Chars (qpzry9x8gf2tvdw0s3jn54khce6mua7l) 00 -> q, 0e -> w,…
<code>qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4</code>
 
9. A Bech32_encoded address consists of 3 parts: HRP + Separator + Data:
<code>bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4</code>
 
The final result from step 9 is the same as example in BIP173.
 
== See Also ==
 
* Talk by Pieter Wuille the new bech32 address format (March 2017) https://www.reddit.com/r/Bitcoin/comments/62fydd/pieter_wuille_lecture_on_new_bech32_address_format/
* Comment by maaku7 explaining how bech32 addresses improve the security of (for example) [[multisignature]] https://www.reddit.com/r/Bitcoin/comments/74tonn/bech32_native_segwit_address_already_used_on_the/dqlogru/
* Step by step guide to go from public key to a Bech32 encoded address on Bitcointalk: https://bitcointalk.org/index.php?topic=4992632

Latest revision as of 10:18, 23 May 2023

Bech32 is a segwit address format specified by BIP 0173 and BIP 0350. This address format is also known as "bc1 addresses". Bech32 is more efficient with block space. As of October 2020, the Bech32 address format is supported in many popular wallets and is the preferred address scheme.

See the page Bech32 adoption to track adoption.

Examples

Examples of the address format being used on mainnet are the TXIDs 4ef47f6eb681d5d9fa2f7e16336cd629303c635e8da51e425b76088be9c8744c and 514a33f1d46179b89e1fea7bbb07b682ab14083a276979f91038369d1a8d689b. And addresses bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq and bc1qc7slrfxkknqcq2jevvvkdgvrt8080852dfjewde450xdlk4ugp7szw5tk9.

Creating a Bech32 address

A Bech32 address is created from a public key as follows:

1. Having a compressed public key (0x02 or 0x03 followed by 32 byte X coordinate): 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798

2. Perform SHA-256 hashing on the public key: 0f715baf5d4c2ed329785cef29e562f73488c8a2bb9dbc5700b361d54b9b0554

3. Perform RIPEMD-160 hashing on the result of SHA-256: 751e76e8199196d454941c45d1b3a323f1433bd6

4. The result of step 3 is an array of 8-bit unsigned integers (base 2^8=256) and Bech32 encoding converts this to an array of 5-bit unsigned integers (base 2^5=32) so we “squash” the bytes to get:

in hex: 0e140f070d1a001912060b0d081504140311021d030c1d03040f1814060e1e16

in numbers: 14 20 15 07 13 26 00 25 18 06 11 13 08 21 04 20 03 17 02 29 03 12 29 03 04 15 24 20 06 14 30 22

5 bits binary: 01110 10100 01111 00111 01101 11010 00000 11001 10010 00110 01011 01101 01000 10101 00100 10100 00011 10001 00010 11101 00011 01100 11101 00011 00100 01111 11000 10100 00110 01110 11110 10110

5. Add the witness version byte in front of the step 4 result (current version is 0): 000e140f070d1a001912060b0d081504140311021d030c1d03040f1814060e1e16

6. Compute the checksum by using the data from step 5 and the H.R.P (bc for MainNet and tb for TestNet) 0c0709110b15

7. Append the checksum to result of step 5 (we now have an array of 5-bit integers): 000e140f070d1a001912060b0d081504140311021d030c1d03040f1814060e1e160c0709110b15

8. Map each value to its corresponding character in Bech32Chars (qpzry9x8gf2tvdw0s3jn54khce6mua7l) 00 -> q, 0e -> w,… qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4

9. A Bech32_encoded address consists of 3 parts: HRP + Separator + Data: bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4

The final result from step 9 is the same as example in BIP173.

See Also