Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2019 Pieter Wuille3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.5"""Utility functions related to output descriptors"""7import re9INPUT_CHARSET = "0123456789()[],'/*abcdefgh@:$%{}IJKLMNOPQRSTUVWXYZ&+-.;<=>?!^_|~ijklmnopqrstuvwxyzABCDEFGH`#\"\\ "10CHECKSUM_CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"11GENERATOR = [0xf5dee51989, 0xa9fdca3312, 0x1bab10e32d, 0x3706b1677a, 0x644d626ffd]13def descsum_polymod(symbols):14"""Internal function that computes the descriptor checksum."""15chk = 116for value in symbols:17top = chk >> 3518chk = (chk & 0x7ffffffff) << 5 ^ value19for i in range(5):20chk ^= GENERATOR[i] if ((top >> i) & 1) else 021return chk23def descsum_expand(s):24"""Internal function that does the character to symbol expansion"""25groups = []26symbols = []27for c in s:28if c not in INPUT_CHARSET:29return None30v = INPUT_CHARSET.find(c)31symbols.append(v & 31)32groups.append(v >> 5)33if len(groups) == 3:34symbols.append(groups[0] * 9 + groups[1] * 3 + groups[2])35groups = []36if len(groups) == 1:37symbols.append(groups[0])38elif len(groups) == 2:39symbols.append(groups[0] * 3 + groups[1])40return symbols42def descsum_create(s):43"""Add a checksum to a descriptor without"""44symbols = descsum_expand(s) + [0, 0, 0, 0, 0, 0, 0, 0]45checksum = descsum_polymod(symbols) ^ 146return s + '#' + ''.join(CHECKSUM_CHARSET[(checksum >> (5 * (7 - i))) & 31] for i in range(8))48def descsum_check(s, require=True):49"""Verify that the checksum is correct in a descriptor"""50if '#' not in s:51return not require52if s[-9] != '#':53return False54if not all(x in CHECKSUM_CHARSET for x in s[-8:]):55return False56symbols = descsum_expand(s[:-9]) + [CHECKSUM_CHARSET.find(x) for x in s[-8:]]57return descsum_polymod(symbols) == 159def drop_origins(s):60'''Drop the key origins from a descriptor'''61desc = re.sub(r'\[.+?\]', '', s)62if '#' in s:63desc = desc[:desc.index('#')]64return descsum_create(desc)
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.