Esempio n. 1
0
    def DecodeAddr(addr: str,
                   **kwargs: Any) -> bytes:
        """
        Decode a OKEx Chain 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.OkexChain.Params("addr_hrp"),
                                                  addr)
        except Bech32ChecksumError as ex:
            raise ValueError("Invalid bech32 checksum") from ex
        else:
            return EthAddrDecoder.DecodeAddr(
                CoinsConf.Ethereum.Params("addr_prefix") + BytesUtils.ToHexString(addr_dec_bytes),
                skip_chksum_enc=True
            )
Esempio n. 2
0
    def EncodeKey(pub_key: Union[bytes, IPublicKey], **kwargs: Any) -> str:
        """
        Encode a public key to Ethereum address.

        Args:
            pub_key (bytes or IPublicKey): Public key bytes or object

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

        Returns:
            str: Address string

        Raised:
            ValueError: If the public key is not valid
            TypeError: If the public key is not secp256k1
        """
        skip_chksum_enc = kwargs.get("skip_chksum_enc", False)

        pub_key_obj = AddrKeyValidator.ValidateAndGetSecp256k1Key(pub_key)

        # First byte of the uncompressed key (i.e. 0x04) is not needed
        kekkak_hex = BytesUtils.ToHexString(
            CryptoUtils.Kekkak256(pub_key_obj.RawUncompressed().ToBytes()[1:]))
        addr = kekkak_hex[EthAddrConst.START_BYTE:]
        return CoinsConf.Ethereum.Params("addr_prefix") + (
            _EthAddrUtils.ChecksumEncode(addr)
            if not skip_chksum_enc else addr)
Esempio 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)
Esempio n. 4
0
    def __IsType(mnemonic: Mnemonic,
                 mnemonic_type: ElectrumV2MnemonicTypes) -> bool:
        """
        Get if the specified mnemonic is of the specified type.

        Args:
            mnemonic (Mnemonic)                    : Mnemonic
            mnemonic_type (ElectrumV2MnemonicTypes): Mnemonic type

        Returns:
            bool: True if valid, false otherwise
        """
        h = CryptoUtils.HmacSha512(ElectrumV2MnemonicUtilsConst.HMAC_KEY,
                                   mnemonic.ToStr())
        return BytesUtils.ToHexString(h).startswith(
            ElectrumV2MnemonicConst.TYPE_TO_PREFIX[mnemonic_type])
Esempio n. 5
0
    def __IsAnyType(mnemonic: Mnemonic) -> bool:
        """
        Get if the specified mnemonic is of any valid type.

        Args:
            mnemonic (Mnemonic): Mnemonic

        Returns:
            bool: True if valid, false otherwise
        """
        h = CryptoUtils.HmacSha512(ElectrumV2MnemonicUtilsConst.HMAC_KEY,
                                   mnemonic.ToStr())
        for mnemonic_type in ElectrumV2MnemonicTypes:
            if BytesUtils.ToHexString(h).startswith(
                    ElectrumV2MnemonicConst.TYPE_TO_PREFIX[mnemonic_type]):
                return True
        return False
Esempio n. 6
0
    def ChecksumEncode(addr: str) -> str:
        """
        Checksum encode the specified address.

        Args:
            addr (str): Address string

        Returns:
            str: Checksum encoded address
        """

        # Compute address digest
        addr_hex_digest = BytesUtils.ToHexString(
            CryptoUtils.Kekkak256(addr.lower()))
        # Encode it
        enc_addr = [
            c.upper() if (int(addr_hex_digest[i], 16) >= 8) else c.lower()
            for i, c in enumerate(addr)
        ]

        return "".join(enc_addr)