1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
# Copyright (C) The python-bitcoinlib developers
#
# This file is part of python-bitcoinlib.
#
# It is subject to the license terms in the LICENSE file found in the top-level
# directory of this distribution.
#
# No part of python-bitcoinlib, including this file, may be copied, modified,
# propagated, or distributed except according to the terms contained in the
# LICENSE file.
"""Bech32 encoding and decoding"""
from bitcoin.segwit_addr import encode, decode
import bitcoin
class Bech32Error(Exception):
pass
class Bech32ChecksumError(Bech32Error):
pass
class CBech32Data(bytes):
"""Bech32-encoded data
Includes a witver and checksum.
"""
def __new__(cls, s):
"""from bech32 addr to """
witver, data = decode(bitcoin.params.BECH32_HRP, s)
if witver is None and data is None:
raise Bech32Error('Bech32 decoding error')
return cls.from_bytes(witver, data)
def __init__(self, s):
"""Initialize from bech32-encoded string
Note: subclasses put your initialization routines here, but ignore the
argument - that's handled by __new__(), and .from_bytes() will call
__init__() with None in place of the string.
"""
@classmethod
def from_bytes(cls, witver, witprog):
"""Instantiate from witver and data"""
if not (0 <= witver <= 16):
raise ValueError('witver must be in range 0 to 16 inclusive; got %d' % witver)
self = bytes.__new__(cls, witprog)
self.witver = witver
return self
def to_bytes(self):
"""Convert to bytes instance
Note that it's the data represented that is converted; the checkum and
witver is not included.
"""
return b'' + self
def __str__(self):
"""Convert to string"""
return encode(bitcoin.params.BECH32_HRP, self.witver, self)
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, str(self))
__all__ = (
'Bech32Error',
'Bech32ChecksumError',
'CBech32Data',
)
|