.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/wallets/addresses/plot_02_bech32_addresses.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_wallets_addresses_plot_02_bech32_addresses.py: Bech32 addresses (BIP173, 2017) =============================== Pieter Wuille and Gregory Maxwell designed Bech32 for segregated-witness addresses. It uses one case and 32 characters chosen to avoid look-alikes, and a 6-character checksum that is a BCH code over GF(32): the string, read as a polynomial, must satisfy .. math:: \mathrm{polymod}(\mathrm{expand}(hrp) \,\|\, data \,\|\, checksum) = 1 . Out of 159,605 candidate codes, the one chosen detects every error in up to 4 characters for strings of up to 89 characters. Base58Check's hash detects most errors, but promises nothing about any of them. Here every pattern of one and two substitutions in an address is tried, and a sample of three and four. .. GENERATED FROM PYTHON SOURCE LINES 22-29 .. code-block:: Python import random from itertools import combinations import matplotlib.pyplot as plt import blockchainkit as bk .. GENERATED FROM PYTHON SOURCE LINES 30-32 The address of a key -------------------- .. GENERATED FROM PYTHON SOURCE LINES 32-41 .. code-block:: Python key = bk.crypto.public_key(1) address = bk.wallets.p2wpkh_address(key) print("private key 1 ->", address) program = bk.wallets.segwit_decode("bc", address) print("witness version", program.version, "program", program.program.hex()) assert address == "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4" assert program.program == bk.wallets.base58check_decode(bk.wallets.p2pkh_address(key))[1] .. rst-class:: sphx-glr-script-out .. code-block:: none private key 1 -> bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4 witness version 0 program 751e76e8199196d454941c45d1b3a323f1433bd6 .. GENERATED FROM PYTHON SOURCE LINES 42-44 Errors in up to four characters ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 44-76 .. code-block:: Python def detected(positions, rng): chars = list(address) for i in positions: chars[i] = rng.choice([c for c in bk.wallets.CHARSET if c != chars[i]]) try: bk.wallets.bech32_decode("".join(chars)) except ValueError: return True return False rng = random.Random(173) data_positions = range(3, len(address)) rates = [] for k in (1, 2, 3, 4): patterns = list(combinations(data_positions, k)) if len(patterns) > 3_000: patterns = rng.sample(patterns, 3_000) hits = sum(detected(p, rng) for p in patterns) rates.append(hits / len(patterns)) print(f"{k} characters wrong: {hits} of {len(patterns)} detected") assert rates == [1.0, 1.0, 1.0, 1.0] fig, ax = plt.subplots(figsize=(6, 4)) ax.bar([1, 2, 3, 4], rates, color="#2563eb") ax.set(xlabel="characters substituted", ylabel="share detected", ylim=(0, 1.05)) ax.set_title("Bech32 detects every error in up to 4 characters") fig.tight_layout() plt.show() .. image-sg:: /api/gallery/wallets/addresses/images/sphx_glr_plot_02_bech32_addresses_001.png :alt: Bech32 detects every error in up to 4 characters :srcset: /api/gallery/wallets/addresses/images/sphx_glr_plot_02_bech32_addresses_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 1 characters wrong: 39 of 39 detected 2 characters wrong: 741 of 741 detected 3 characters wrong: 3000 of 3000 detected 4 characters wrong: 3000 of 3000 detected .. GENERATED FROM PYTHON SOURCE LINES 77-81 Exercise -------- Upper-case the address. Is it still valid? Mix the case. Is it still valid? A worked solution is in :doc:`/exercises/wallets`. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.214 seconds) .. _sphx_glr_download_api_gallery_wallets_addresses_plot_02_bech32_addresses.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/wallets/addresses/plot_02_bech32_addresses.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_bech32_addresses.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_bech32_addresses.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_bech32_addresses.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_