示例#1
0
    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))
示例#2
0
    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 Decode(self, mnemonic: Union[str, Mnemonic]) -> bytes:
        """
        Decode a mnemonic phrase to bytes (no checksum).

        Args:
            mnemonic (str or Mnemonic object): Mnemonic

        Returns:
            bytes: Decoded bytes

        Raises:
            MnemonicChecksumError: If checksum is not valid
            ValueError: If mnemonic is not valid
        """
        mnemonic_obj = AlgorandMnemonic.FromString(mnemonic) if isinstance(
            mnemonic, str) else mnemonic

        # Check mnemonic length
        if mnemonic_obj.WordsCount(
        ) not in AlgorandMnemonicConst.MNEMONIC_WORD_NUM:
            raise ValueError(
                f"Mnemonic words count is not valid ({mnemonic_obj.WordsCount()})"
            )

        # Get words
        words = mnemonic_obj.ToList()
        # Detect language if it was not specified at construction
        words_list, _ = self._FindLanguage(mnemonic_obj)

        # Get words indexes
        word_indexes = [words_list.GetWordIdx(w) for w in words]
        # Get back entropy as list
        entropy_list = AlgorandMnemonicUtils.ConvertBits(
            word_indexes[:-1], 11, 8)
        # Cannot be None if the number of words is valid (checked at the beginning)
        assert entropy_list is not None
        # Get back entropy bytes
        entropy_bytes = BytesUtils.FromList(entropy_list)[:-1]

        # Validate checksum
        self.__ValidateChecksum(entropy_bytes, word_indexes[-1], words_list)

        return entropy_bytes