def Decode(hrp: str, addr: str) -> bytes: """ Decode from Atom Bech32. Args: hrp (str) : Human readable part addr (str): Address Returns: bytes: Data Raises: Bech32FormatError: If the bech32 string is not valid Bech32ChecksumError: If the checksum is not valid """ # Decode string hrpgot, data = AtomBech32Decoder._DecodeBech32( addr, AtomBech32Const.SEPARATOR, AtomBech32Const.CHECKSUM_LEN) # Check HRP if hrpgot != hrp: raise Bech32FormatError( "Invalid format (HRP not valid, expected %s, got %s)" % (hrp, hrpgot)) # Convert back from base32 conv_data = Bech32BaseUtils.ConvertFromBase32(data) # Check converted data if len(conv_data) < AtomBech32Const.DATA_MIN_LEN or len( conv_data) > AtomBech32Const.DATA_MAX_LEN: raise Bech32FormatError("Invalid format (length not valid)") return ConvUtils.ListToBytes(conv_data)
def Decode(cls, hrp: str, addr: str) -> bytes: """ Decode from Bech32. Args: hrp (str) : Human readable part addr (str): Address Returns: bytes: Decoded address Raises: ValueError: If the bech32 string is not valid Bech32ChecksumError: If the checksum is not valid """ # Decode string hrp_got, data = cls._DecodeBech32(addr, Bech32Const.SEPARATOR, Bech32Const.CHECKSUM_STR_LEN) # Check HRP if hrp != hrp_got: raise ValueError( f"Invalid format (HRP not valid, expected {hrp}, got {hrp_got})" ) # Convert back from base32 return BytesUtils.FromList(Bech32BaseUtils.ConvertFromBase32(data))
def Decode(hrp, addr): """ Decode from Bitcoin Cash Bech32. Args: hrp (str) : Human readable part addr (str): Address Returns: tuple: Net version (index 0) and data (index 1) Raises: BchBech32FormatError: If the address is not valid Bech32FormatError: If the bech32 string is not valid Bech32ChecksumError: If the checksum is not valid """ # Decode string hrpgot, data = BchBech32Decoder._DecodeBech32( addr, BchBech32Const.SEPARATOR, BchBech32Const.CHECKSUM_LEN) # Check HRP if hrpgot != hrp: raise BchBech32FormatError( "Invalid BCH format (HRP not valid, expected %s, got %s)" % (hrp, hrpgot)) # Convert back from base32 conv_data = Bech32BaseUtils.ConvertFromBase32(data) # Check converted data if len(conv_data) < BchBech32Const.DATA_MIN_LEN or len( conv_data) > BchBech32Const.DATA_MAX_LEN: raise BchBech32FormatError("Invalid BCH format (length not valid)") return (conv_data[0], ConvUtils.ListToBytes(conv_data[1:]))
def Decode(hrp, addr): """ Decode from Segwit Bech32. Args: hrp (str) : Human readable part addr (str): Address Returns: tuple: Witness version (index 0) and witness program (index 1) Raises: SegwitBech32FormatError: If the address is not valid Bech32FormatError: If the bech32 string is not valid Bech32ChecksumError: If the checksum is not valid """ # Decode string hrpgot, data = SegwitBech32Decoder._DecodeBech32(addr, SegwitBech32Const.SEPARATOR, SegwitBech32Const.CHECKSUM_LEN) # Check HRP if hrpgot != hrp: raise SegwitBech32FormatError("Invalid segwit format (HRP not valid, expected %s, got %s)" % (hrp, hrpgot)) # Convert back from base32 conv_data = Bech32BaseUtils.ConvertFromBase32(data[1:]) # Check converted data if len(conv_data) < SegwitBech32Const.DATA_MIN_LEN or len(conv_data) > SegwitBech32Const.DATA_MAX_LEN: raise SegwitBech32FormatError("Invalid segwit format (length not valid)") elif data[0] > SegwitBech32Const.WITNESS_VER_MAX_VAL: raise SegwitBech32FormatError("Invalid segwit format (witness version not valid)") elif data[0] == 0 and not len(conv_data) in SegwitBech32Const.WITNESS_VER_ZERO_DATA_LEN: raise SegwitBech32FormatError("Invalid segwit format (length not valid)") return (data[0], ConvUtils.ListToBytes(conv_data))
def Encode(cls, hrp: str, data: bytes) -> str: """ Encode to Bech32. Args: hrp (str) : HRP data (bytes): Data Returns: str: Encoded address Raises: ValueError: If the data is not valid """ return cls._EncodeBech32(hrp, Bech32BaseUtils.ConvertToBase32(data), Bech32Const.SEPARATOR)
def Encode(hrp, wit_ver, wit_prog): """ Encode to Segwit Bech32. Args: hrp (str) : HRP wit_ver (int) : Witness version wit_prog (bytes): Witness program Returns: str: Encoded address Raises: Bech32FormatError: If the data is not valid """ return SegwitBech32Encoder._EncodeBech32(hrp, [wit_ver] + Bech32BaseUtils.ConvertToBase32(wit_prog), SegwitBech32Const.SEPARATOR)
def Encode(hrp: str, data: bytes) -> str: """ Encode to Atom Bech32. Args: hrp (str) : HRP data (bytes): Data Returns: str: Encoded address Raises: Bech32FormatError: If the data is not valid """ return AtomBech32Encoder._EncodeBech32( hrp, Bech32BaseUtils.ConvertToBase32(data), AtomBech32Const.SEPARATOR)
def Decode(cls, hrp: str, addr: str) -> Tuple[int, bytes]: """ Decode from Segwit Bech32. Args: hrp (str) : Human readable part addr (str): Address Returns: tuple[int, bytes]: Witness version (index 0) and witness program (index 1) Raises: Bech32ChecksumError: If the checksum is not valid ValueError: If the bech32 string is not valid """ # Decode string hrp_got, data = cls._DecodeBech32(addr, SegwitBech32Const.SEPARATOR, SegwitBech32Const.CHECKSUM_BYTE_LEN) # Check HRP if hrp != hrp_got: raise ValueError( f"Invalid format (HRP not valid, expected {hrp}, got {hrp_got})" ) # Convert back from base32 (remove witness version) conv_data = Bech32BaseUtils.ConvertFromBase32(data[1:]) # Check data length if (len(conv_data) < SegwitBech32Const.WITNESS_PROG_MIN_BYTE_LEN or len(conv_data) > SegwitBech32Const.WITNESS_PROG_MAX_BYTE_LEN): raise ValueError( f"Invalid format (witness program length not valid: {len(conv_data)})" ) # Check witness version wit_ver = data[0] if wit_ver > SegwitBech32Const.WITNESS_VER_MAX_VAL: raise ValueError( f"Invalid format (witness version not valid: {wit_ver})") if wit_ver == 0 and not len( conv_data) in SegwitBech32Const.WITNESS_VER_ZERO_DATA_BYTE_LEN: raise ValueError( f"Invalid format (length not valid: {len(conv_data)})") return wit_ver, BytesUtils.FromList(conv_data)
def Encode(hrp, net_ver, data): """ Encode to Bitcoin Cash Bech32. Args: hrp (str) : HRP net_ver (bytes): Net version data (bytes) : Data Returns: str: Encoded address Raises: Bech32FormatError: If the data is not valid """ return BchBech32Encoder._EncodeBech32( hrp, Bech32BaseUtils.ConvertToBase32(net_ver + data), BchBech32Const.SEPARATOR)
def Encode(cls, hrp: str, wit_ver: int, wit_prog: bytes) -> str: """ Encode to Segwit Bech32. Args: hrp (str) : HRP wit_ver (int) : Witness version wit_prog (bytes): Witness program Returns: str: Encoded address Raises: ValueError: If the data is not valid """ return cls._EncodeBech32(hrp, [wit_ver] + Bech32BaseUtils.ConvertToBase32(wit_prog), SegwitBech32Const.SEPARATOR)