Ejemplo n.º 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
Ejemplo n.º 2
0
    def DecodeAddr(addr: str, **kwargs: Any) -> bytes:
        """
        Decode an Ethereum address to bytes.

        Args:
            addr (str): Address string

        Other Parameters:
            skip_chksum_enc (bool, optional): True to skip checksum encoding verification, false otherwise (default)

        Returns:
            bytes: Public key hash bytes

        Raises:
            ValueError: If the address encoding is not valid
        """
        skip_chksum_enc = kwargs.get("skip_chksum_enc", False)

        # Validate and remove prefix
        addr_no_prefix = AddrDecUtils.ValidateAndRemovePrefix(
            addr, CoinsConf.Ethereum.Params("addr_prefix"))
        # Validate length
        AddrDecUtils.ValidateLength(addr_no_prefix, EthAddrConst.ADDR_LEN)
        # Check checksum encoding
        if not skip_chksum_enc and addr_no_prefix != _EthAddrUtils.ChecksumEncode(
                addr_no_prefix):
            raise ValueError("Invalid checksum encode")

        return BytesUtils.FromHexString(addr_no_prefix)
Ejemplo n.º 3
0
    def DecodeAddr(addr: str, **kwargs: Any) -> bytes:
        """
        Decode a Tron address to bytes.

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

        Returns:
            bytes: Public key hash bytes

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

        try:
            # Decode from base58
            addr_dec = Base58Decoder.CheckDecode(addr)
        except Base58ChecksumError as ex:
            raise ValueError("Invalid base58 checksum") from ex
        else:
            # Validate length
            AddrDecUtils.ValidateLength(
                addr_dec, (EthAddrConst.ADDR_LEN // 2) +
                len(CoinsConf.Tron.Params("addr_prefix")))
            # Validate and remove prefix
            addr_no_prefix = AddrDecUtils.ValidateAndRemovePrefix(
                addr_dec, CoinsConf.Tron.Params("addr_prefix"))

            return EthAddrDecoder.DecodeAddr(
                CoinsConf.Ethereum.Params("addr_prefix") +
                BytesUtils.ToHexString(addr_no_prefix),
                skip_chksum_enc=True)
Ejemplo n.º 4
0
    def DecodeAddr(addr: str, **kwargs: Any) -> bytes:
        """
        Decode a P2PKH address to bytes.

        Args:
            addr (str): Address string

        Other Parameters:
            net_ver (bytes)                        : Net address version
            base58_alph (Base58Alphabets, optional): Base58 alphabet, Bitcoin alphabet by default

        Returns:
            bytes: Public key hash bytes

        Raises:
            ValueError: If the address encoding is not valid
        """
        net_ver_bytes = kwargs["net_ver"]
        base58_alph = kwargs.get("base58_alph", Base58Alphabets.BITCOIN)

        try:
            addr_dec_bytes = Base58Decoder.CheckDecode(addr, base58_alph)
        except Base58ChecksumError as ex:
            raise ValueError("Invalid base58 checksum") from ex
        else:
            # Validate length
            AddrDecUtils.ValidateLength(
                addr_dec_bytes,
                CryptoUtils.Hash160DigestSize() + len(net_ver_bytes))
            # Validate and remove prefix
            return AddrDecUtils.ValidateAndRemovePrefix(
                addr_dec_bytes, net_ver_bytes)
Ejemplo n.º 5
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
Ejemplo n.º 6
0
    def DecodeAddr(addr: str, **kwargs: Any) -> bytes:
        """
        Decode an Algorand address to bytes.

        Args:
            addr (str): Address string

        Other Parameters:
            hrp (str): HRP

        Returns:
            bytes: Public key hash bytes

        Raises:
            ValueError: If the address encoding is not valid
        """
        hrp = kwargs["hrp"]

        try:
            addr_dec_bytes = Bech32Decoder.Decode(hrp, addr)
        except Bech32ChecksumError as ex:
            raise ValueError("Invalid bech32 checksum") from ex
        else:
            AddrDecUtils.ValidateLength(addr_dec_bytes,
                                        CryptoUtils.Hash160DigestSize())
            return addr_dec_bytes
Ejemplo n.º 7
0
    def DecodeAddr(addr: str,
                   **kwargs: Any) -> bytes:
        """
        Decode a P2TR address to bytes.

        Args:
            addr (str): Address string

        Other Parameters:
            hrp (str): HRP

        Returns:
            bytes: X coordinate of the tweaked public key

        Raises:
            ValueError: If the address encoding is not valid
        """
        hrp = kwargs["hrp"]

        try:
            wit_ver_got, addr_dec_bytes = SegwitBech32Decoder.Decode(hrp, addr)
        except Bech32ChecksumError as ex:
            raise ValueError("Invalid bech32 checksum") from ex
        else:
            # Validate length
            AddrDecUtils.ValidateLength(addr_dec_bytes, Secp256k1PublicKey.CompressedLength() - 1)
            # Check witness version
            if wit_ver_got != P2TRConst.WITNESS_VER:
                raise ValueError(f"Invalid witness version (expected {P2TRConst.WITNESS_VER}, got {wit_ver_got})")
            return addr_dec_bytes
Ejemplo n.º 8
0
    def DecodeAddr(addr: str, **kwargs: Any) -> bytes:
        """
        Decode a Bitcoin Cash P2PKH address to bytes.

        Args:
            addr (str): Address string

        Other Parameters:
            hrp (str)      : HRP
            net_ver (bytes): Net address version

        Returns:
            bytes: Public key hash bytes

        Raises:
            ValueError: If the address encoding is not valid
        """
        hrp = kwargs["hrp"]
        net_ver_bytes = kwargs["net_ver"]

        try:
            net_ver_bytes_got, addr_dec_bytes = BchBech32Decoder.Decode(
                hrp, addr)
        except Bech32ChecksumError as ex:
            raise ValueError("Invalid bech32 checksum") from ex
        else:
            # Check net version
            if net_ver_bytes != net_ver_bytes_got:
                raise ValueError(
                    f"Invalid net version (expected {BytesUtils.ToHexString(net_ver_bytes)}, "
                    f"got {BytesUtils.ToHexString(net_ver_bytes_got)})")
            # Validate length
            AddrDecUtils.ValidateLength(addr_dec_bytes,
                                        CryptoUtils.Hash160DigestSize())
            return addr_dec_bytes
Ejemplo n.º 9
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
Ejemplo n.º 10
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
Ejemplo n.º 11
0
    def DecodeAddr(addr: str, addr_type: FillAddrTypes) -> bytes:
        """
        Decode a Filecoin address to bytes.

        Args:
            addr (str)               : Address string
            addr_type (FillAddrTypes): Address type

        Returns:
            bytes: Public key hash bytes

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

        # Validate and remove prefix
        addr_no_prefix = AddrDecUtils.ValidateAndRemovePrefix(
            addr, CoinsConf.Filecoin.Params("addr_prefix"))
        # Check address type
        addr_type_got = ord(addr_no_prefix[0]) - ord("0")
        if addr_type != addr_type_got:
            raise ValueError(
                f"Invalid address type (expected {addr_type}, got {addr_type_got})"
            )
        # Decode from base32
        addr_dec_bytes = Base32Decoder.Decode(addr_no_prefix[1:],
                                              FilAddrConst.BASE32_ALPHABET)
        # Validate length
        AddrDecUtils.ValidateLength(
            addr_dec_bytes,
            FilAddrConst.BLAKE2B_BYTE_LEN + FilAddrConst.CHECKSUM_BYTE_LEN)

        # Get back checksum and public key bytes
        pub_key_hash_bytes, checksum_bytes = AddrDecUtils.SplitPartsByChecksum(
            addr_dec_bytes, FilAddrConst.CHECKSUM_BYTE_LEN)
        # Validate checksum
        AddrDecUtils.ValidateChecksum(
            pub_key_hash_bytes, checksum_bytes,
            lambda pub_key_bytes: _FilAddrUtils.ComputeChecksum(
                pub_key_bytes, addr_type))

        return pub_key_hash_bytes
Ejemplo n.º 12
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
Ejemplo n.º 13
0
    def DecodeAddr(addr: str, **kwargs: Any) -> bytes:
        """
        Decode a Tezos address to bytes.

        Args:
            addr (str): Address string

        Other Parameters:
            prefix (XtzAddrPrefixes): Address prefix

        Returns:
            bytes: Public key hash bytes

        Raises:
            ValueError: If the address encoding is not valid
            TypeError: If the prefix is not a XtzAddrPrefixes enum
        """

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

        # Decode from base58
        try:
            addr_dec_bytes = Base58Decoder.CheckDecode(addr)
        except Base58ChecksumError as ex:
            raise ValueError("Invalid base58 checksum") from ex
        else:
            # Validate length
            AddrDecUtils.ValidateLength(
                addr_dec_bytes,
                len(prefix.value) + XtzAddrConst.BLAKE2B_BYTE_LEN)
            # Validate and remove prefix
            blake_bytes = AddrDecUtils.ValidateAndRemovePrefix(
                addr_dec_bytes, prefix.value)

            return blake_bytes
Ejemplo n.º 14
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
Ejemplo n.º 15
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
Ejemplo n.º 16
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
Ejemplo n.º 17
0
    def DecodeAddr(addr: str, prefix: str, hrp: str) -> bytes:
        """
        Decode an Avax address to bytes.

        Args:
            addr (str)  : Address string
            prefix (str): Address prefix
            hrp (str)   : Address HRP

        Returns:
            bytes: Public key hash bytes

        Raises:
            ValueError: If the address encoding is not valid
        """
        addr_no_prefix = AddrDecUtils.ValidateAndRemovePrefix(addr, prefix)
        return AtomAddrDecoder.DecodeAddr(addr_no_prefix, hrp=hrp)
Ejemplo n.º 18
0
    def DecodeAddr(addr: str, **kwargs: Any) -> bytes:
        """
        Decode a Zilliqa address to bytes.

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

        Returns:
            bytes: Public key hash bytes

        Raises:
            ValueError: If the address encoding is not valid
        """
        try:
            addr_dec_bytes = Bech32Decoder.Decode(
                CoinsConf.Zilliqa.Params("addr_hrp"), addr)
        except Bech32ChecksumError as ex:
            raise ValueError("Invalid bech32 checksum") from ex
        else:
            AddrDecUtils.ValidateLength(addr_dec_bytes,
                                        ZilAddrConst.SHA256_BYTE_LEN)
            return addr_dec_bytes
Ejemplo n.º 19
0
    def DecodeAddr(addr: str, **kwargs: Any) -> bytes:
        """
        Decode a Neo address to bytes.

        Args:
            addr (str): Address string

        Other Parameters:
            ver (bytes): Version

        Returns:
            bytes: Public key hash bytes

        Raises:
            ValueError: If the address encoding is not valid
        """
        ver_bytes = kwargs["ver"]

        try:
            # Decode from base58
            addr_dec_bytes = Base58Decoder.CheckDecode(addr)
        except Base58ChecksumError as ex:
            raise ValueError("Invalid base58 checksum") from ex
        else:
            # Validate length
            AddrDecUtils.ValidateLength(
                addr_dec_bytes,
                CryptoUtils.Hash160DigestSize() + len(ver_bytes))
            # Check version
            ver_got = IntegerUtils.ToBytes(addr_dec_bytes[0])
            if ver_bytes != ver_got:
                raise ValueError(
                    f"Invalid version (expected {BytesUtils.ToHexString(ver_bytes)}, "
                    f"got {BytesUtils.ToHexString(ver_got)})")

            return addr_dec_bytes[1:]
Ejemplo n.º 20
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