Пример #1
0
    def DecodeAddr(addr: str, **kwargs: Any) -> bytes:
        """
        Decode an Algorand address to bytes.

        Args:
            addr (str): Address string
            **kwargs  : Not used

        Returns:
            bytes: Public key bytes

        Raises:
            ValueError: If the address encoding is not valid
        """

        # Decode from base32
        addr_dec_bytes = Base32Decoder.Decode(addr)
        # Validate length
        AddrDecUtils.ValidateLength(
            addr_dec_bytes,
            Ed25519PublicKey.CompressedLength() +
            AlgoAddrConst.CHECKSUM_BYTE_LEN - 1)
        # Get back checksum and public key bytes
        pub_key_bytes, checksum_bytes = AddrDecUtils.SplitPartsByChecksum(
            addr_dec_bytes, AlgoAddrConst.CHECKSUM_BYTE_LEN)

        # Validate checksum
        AddrDecUtils.ValidateChecksum(pub_key_bytes, checksum_bytes,
                                      _AlgoAddrUtils.ComputeChecksum)
        # Validate public key
        AddrDecUtils.ValidatePubKey(pub_key_bytes, Ed25519PublicKey)

        return pub_key_bytes
Пример #2
0
    def DecodeAddr(addr: str, **kwargs: Any) -> bytes:
        """
        Decode an Elrond address to bytes.

        Args:
            addr (str): Address string
            **kwargs  : Not used

        Returns:
            bytes: Public key bytes

        Raises:
            ValueError: If the address encoding is not valid
        """
        try:
            addr_dec_bytes = Bech32Decoder.Decode(
                CoinsConf.Elrond.Params("addr_hrp"), addr)
        except Bech32ChecksumError as ex:
            raise ValueError("Invalid bech32 checksum") from ex
        else:
            AddrDecUtils.ValidateLength(
                addr_dec_bytes,
                Ed25519PublicKey.CompressedLength() - 1)
            AddrDecUtils.ValidatePubKey(addr_dec_bytes, Ed25519PublicKey)

            return addr_dec_bytes
Пример #3
0
    def DecodeAddr(addr: str, ss58_format: int,
                   pub_key_cls: Type[IPublicKey]) -> bytes:
        """
        Decode a Substrate address to bytes.

        Args:
            addr (str)              : Address string
            ss58_format (int)       : SS58 format
            pub_key_cls (IPublicKey): Public key class type

        Returns:
            bytes: Public key bytes

        Raises:
            ValueError: If the address encoding is not valid
        """

        try:
            # Decode from SS58 (SS58Decoder.Decode also validates the length)
            ss58_format_got, addr_dec_bytes = SS58Decoder.Decode(addr)
        except SS58ChecksumError as ex:
            raise ValueError("Invalid SS58 encoding") from ex
        # Check SS58 format
        if ss58_format != ss58_format_got:
            raise ValueError(
                f"Invalid SS58 format (expected {ss58_format}, got {ss58_format_got})"
            )
        # Validate public key
        AddrDecUtils.ValidatePubKey(addr_dec_bytes, pub_key_cls)

        return addr_dec_bytes
Пример #4
0
    def DecodeAddr(addr: str,
                   **kwargs: Any) -> bytes:
        """
        Decode a Stellar address to bytes.

        Args:
            addr (str): Address string

        Other Parameters:
            addr_type (XlmAddrTypes): Address type

        Returns:
            bytes: Public key bytes

        Raises:
            ValueError: If the address encoding is not valid
            TypeError: If the address type is not a XlmAddrTypes enum
        """

        # Get and check address type
        addr_type = kwargs["addr_type"]
        if not isinstance(addr_type, XlmAddrTypes):
            raise TypeError("Address type is not an enumerative of XlmAddrTypes")

        # Decode from base32
        addr_dec_bytes = Base32Decoder.Decode(addr)
        # Validate length
        AddrDecUtils.ValidateLength(addr_dec_bytes,
                                    Ed25519PublicKey.CompressedLength() + XlmAddrConst.CHECKSUM_BYTE_LEN)
        # Get back checksum and payload bytes
        payload_bytes, checksum_bytes = AddrDecUtils.SplitPartsByChecksum(addr_dec_bytes,
                                                                          XlmAddrConst.CHECKSUM_BYTE_LEN)
        # Check address type
        addr_type_got = payload_bytes[0]
        if addr_type != addr_type_got:
            raise ValueError(f"Invalid address type (expected {addr_type.value}, "
                             f"got {addr_type_got})")

        # Validate checksum
        AddrDecUtils.ValidateChecksum(payload_bytes, checksum_bytes, _XlmAddrUtils.ComputeChecksum)
        # Validate public key
        pub_key_bytes = payload_bytes[1:]
        AddrDecUtils.ValidatePubKey(pub_key_bytes, Ed25519PublicKey)

        return pub_key_bytes
Пример #5
0
    def DecodeAddr(addr: str, **kwargs: Any) -> bytes:
        """
        Decode a Nano address to bytes.

        Args:
            addr (str): Address string
            **kwargs  : Not used

        Returns:
            bytes: Public key bytes

        Raises:
            ValueError: If the address encoding is not valid
        """

        # Validate and remove prefix
        addr_no_prefix = AddrDecUtils.ValidateAndRemovePrefix(
            addr, CoinsConf.Nano.Params("addr_prefix"))
        # Decode from base32
        addr_dec_bytes = Base32Decoder.Decode(
            NanoAddrConst.PAYLOAD_PAD_ENC + addr_no_prefix,
            NanoAddrConst.BASE32_ALPHABET)
        # Validate length
        AddrDecUtils.ValidateLength(
            addr_dec_bytes,
            Ed25519Blake2bPublicKey.CompressedLength() +
            NanoAddrConst.CHECKSUM_BYTE_LEN +
            len(NanoAddrConst.PAYLOAD_PAD_DEC) - 1)

        # Get back checksum and public key bytes
        pub_key_bytes, checksum_bytes = AddrDecUtils.SplitPartsByChecksum(
            addr_dec_bytes[len(NanoAddrConst.PAYLOAD_PAD_DEC):],
            NanoAddrConst.CHECKSUM_BYTE_LEN)
        # Validate checksum
        AddrDecUtils.ValidateChecksum(pub_key_bytes, checksum_bytes,
                                      _NanoAddrUtils.ComputeChecksum)
        # Validate public key
        AddrDecUtils.ValidatePubKey(pub_key_bytes, Ed25519Blake2bPublicKey)

        return pub_key_bytes
Пример #6
0
    def DecodeAddr(addr: str,
                   **kwargs: Any) -> bytes:
        """
        Decode a Near Protocol address to bytes.

        Args:
            addr (str): Address string
            **kwargs  : Not used

        Returns:
            bytes: Public key bytes

        Raises:
            ValueError: If the address encoding is not valid
        """
        pub_key_bytes = BytesUtils.FromHexString(addr)
        # Validate length
        AddrDecUtils.ValidateLength(pub_key_bytes,
                                    Ed25519PublicKey.CompressedLength() - 1)
        # Validate public key
        AddrDecUtils.ValidatePubKey(pub_key_bytes, Ed25519PublicKey)

        return pub_key_bytes
Пример #7
0
    def DecodeAddr(addr: str, **kwargs: Any) -> bytes:
        """
        Decode a Solana address to bytes.

        Args:
            addr (str): Address string
            **kwargs  : Not used

        Returns:
            bytes: Public key bytes

        Raises:
            ValueError: If the address encoding is not valid
        """

        # Decode from base58
        addr_dec_bytes = Base58Decoder.Decode(addr)
        # Validate length
        AddrDecUtils.ValidateLength(addr_dec_bytes,
                                    Ed25519PublicKey.CompressedLength() - 1)
        # Validate public key
        AddrDecUtils.ValidatePubKey(addr_dec_bytes, Ed25519PublicKey)

        return addr_dec_bytes
Пример #8
0
    def DecodeAddr(addr: str, **kwargs: Any) -> bytes:
        """
        Decode an EOS address to bytes.

        Args:
            addr (str): Address string
            **kwargs  : Not used

        Returns:
            bytes: Public key bytes

        Raises:
            ValueError: If the address encoding is not valid
        """

        # Validate and remove prefix
        addr_no_prefix = AddrDecUtils.ValidateAndRemovePrefix(
            addr, CoinsConf.Eos.Params("addr_prefix"))
        # Decode from base58
        addr_dec_bytes = Base58Decoder.Decode(addr_no_prefix)
        # Validate length
        AddrDecUtils.ValidateLength(
            addr_dec_bytes,
            Secp256k1PublicKey.CompressedLength() +
            EosAddrConst.CHECKSUM_BYTE_LEN)

        # Get back checksum and public key bytes
        pub_key_bytes, checksum_bytes = AddrDecUtils.SplitPartsByChecksum(
            addr_dec_bytes, EosAddrConst.CHECKSUM_BYTE_LEN)
        # Validate checksum
        AddrDecUtils.ValidateChecksum(pub_key_bytes, checksum_bytes,
                                      _EosAddrUtils.ComputeChecksum)
        # Validate public key
        AddrDecUtils.ValidatePubKey(pub_key_bytes, Secp256k1PublicKey)

        return pub_key_bytes
Пример #9
0
    def DecodeAddr(addr: str,
                   net_ver_bytes: bytes,
                   payment_id_bytes: Optional[bytes] = None) -> bytes:
        """
        Decode a Monero address to bytes.

        Args:
            addr (str)                       : Address string
            net_ver_bytes (bytes)            : Net version
           payment_id_bytes (bytes, optional): Payment ID (only for integrated addresses)

        Returns:
            bytes: Public spend (first) and view (second) keys joined together

        Raises:
            ValueError: If the address encoding is not valid
        """

        # Decode from base58 XMR
        addr_dec_bytes = Base58XmrDecoder.Decode(addr)
        # Validate, remove prefix and split
        payload_bytes, checksum_bytes = AddrDecUtils.SplitPartsByChecksum(
            addr_dec_bytes, XmrAddrConst.CHECKSUM_BYTE_LEN)
        # Validate checksum
        AddrDecUtils.ValidateChecksum(payload_bytes, checksum_bytes,
                                      _XmrAddrUtils.ComputeChecksum)
        # Validate and remove prefix
        payload_bytes = AddrDecUtils.ValidateAndRemovePrefix(
            payload_bytes, net_ver_bytes)

        try:
            # Validate length without payment ID
            AddrDecUtils.ValidateLength(
                payload_bytes,
                Ed25519MoneroPublicKey.CompressedLength() * 2)
        except ValueError as ex:
            # Validate length with payment ID
            AddrDecUtils.ValidateLength(
                payload_bytes,
                (Ed25519MoneroPublicKey.CompressedLength() * 2) +
                XmrAddrConst.PAYMENT_ID_BYTE_LEN)
            # Check payment ID
            if payment_id_bytes is None or len(
                    payment_id_bytes) != XmrAddrConst.PAYMENT_ID_BYTE_LEN:
                raise ValueError("Invalid payment ID") from ex

            payment_id_got_bytes = payload_bytes[-XmrAddrConst.
                                                 PAYMENT_ID_BYTE_LEN:]
            if payment_id_bytes != payment_id_got_bytes:
                raise ValueError(
                    f"Invalid payment ID (expected {BytesUtils.ToHexString(payment_id_bytes)}, "
                    f"got {BytesUtils.ToHexString(payment_id_got_bytes)})"
                ) from ex

        # Validate public spend key
        pub_spend_key_bytes = payload_bytes[:Ed25519MoneroPublicKey.
                                            CompressedLength()]
        AddrDecUtils.ValidatePubKey(pub_spend_key_bytes,
                                    Ed25519MoneroPublicKey)
        # Validate public view key
        pub_view_key_bytes = payload_bytes[
            Ed25519MoneroPublicKey.CompressedLength(
            ):Ed25519MoneroPublicKey.CompressedLength() * 2]
        AddrDecUtils.ValidatePubKey(pub_view_key_bytes, Ed25519MoneroPublicKey)

        return pub_spend_key_bytes + pub_view_key_bytes