Ejemplo n.º 1
0
    def Encode(key_bytes, compr_pub_key = True, net_addr_ver = BitcoinConf.WIF_NET_VER.Main()):
        """ Encode key bytes into a WIF string.

        Args:
            key_bytes (bytes)             : Key bytes
            compr_pub_key (bool)          : True if private key corresponds to a compressed public key, false otherwise
            net_addr_ver (bytes, optional): Net address version, default is Bitcoin main network

        Returns:
            str: WIF encoded string

        Raises:
            ValueError: If the key is not valid
        """

        # Check key
        if not KeyUtils.IsPrivate(key_bytes):
            raise ValueError("Invalid key (%s)" % ConvUtils.BytesToHexString(key_bytes))

        # Add suffix if correspond to a compressed public key
        if compr_pub_key:
            key_bytes += WifConst.COMPR_PUB_KEY_SUFFIX

        # Add net address version
        key_bytes = net_addr_ver + key_bytes

        # Encode key
        return Base58Encoder.CheckEncode(key_bytes)
Ejemplo n.º 2
0
    def Decode(
        wif_str: str, net_addr_ver: bytes = BitcoinConf.WIF_NET_VER.Main()
    ) -> bytes:
        """ Decode key bytes from a WIF string.

        Args:
            wif_str (str)                 : WIF string
            net_addr_ver (bytes, optional): Net address version, default is Bitcoin main network

        Returns:
            bytes: Key bytes

        Raises:
            Base58ChecksumError: If the base58 checksum is not valid
            ValueError: If the resulting key is not valid
        """

        # Decode string
        key_bytes = Base58Decoder.CheckDecode(wif_str)

        # Check net version
        if key_bytes[0] != ord(net_addr_ver):
            raise ValueError("Invalid net version (expected %x, got %x)" %
                             (ord(net_addr_ver), key_bytes[0]))

        # Remove net version
        key_bytes = key_bytes[1:]

        # Remove suffix if correspond to a compressed public key
        if KeyUtils.IsPrivate(key_bytes[:-1]):
            # Check the compressed public key suffix
            if key_bytes[-1] != ord(WifConst.COMPR_PUB_KEY_SUFFIX):
                raise ValueError(
                    "Invalid compressed public key suffix (expected %x, got %x)"
                    % (ord(WifConst.COMPR_PUB_KEY_SUFFIX), key_bytes[-1]))
            # Remove it
            key_bytes = key_bytes[:-1]

        # Check if valid
        if not KeyUtils.IsValid(key_bytes):
            raise ValueError("Invalid decoded key (%s)" %
                             ConvUtils.BytesToHexString(key_bytes))

        return key_bytes
Ejemplo n.º 3
0
    def ToAddress(pub_key_bytes: bytes) -> str:
        """ Get address in Ethereum format.

        Args:
            pub_key_bytes (bytes): Public key bytes

        Returns:
            str: Address string

        Raised:
            ValueError: If the key is not a public uncompressed key
        """
        if not KeyUtils.IsPublicUncompressed(pub_key_bytes):
            raise ValueError("Public uncompressed key is required for Ethereum address")

        addr = sha3.keccak_256(pub_key_bytes).hexdigest()[EthAddrConst.START_BYTE:]
        return EthAddrConst.PREFIX + EthAddrUtils.ChecksumEncode(addr)
Ejemplo n.º 4
0
    def ToAddress(pub_key_bytes: bytes) -> str:
        """ Get address in Atom format.

        Args:
            pub_key_bytes (bytes) : Public key bytes

        Returns:
            str: Address string

        Raises:
            ValueError: If key is not a public compressed key
        """
        if not KeyUtils.IsPublicCompressed(pub_key_bytes):
            raise ValueError(
                "Public compressed key is required for Avax address")

        return AvaxBech32Encoder.Encode(CryptoUtils.Hash160(pub_key_bytes),
                                        AvaxChainTypes.AVAX_P_CHAIN)
Ejemplo n.º 5
0
    def ToAddress(pub_key_bytes: bytes,
                  net_addr_ver: str = BitcoinConf.P2WPKH_NET_VER.Main()) -> str:
        """ Get address in P2WPKH format.

        Args:
            pub_key_bytes (bytes)       : Public key bytes
            net_addr_ver (str, optional): Net address version, default is Bitcoin main network

        Returns:
            str: Address string

        Raises:
            ValueError: If key is not a public compressed key
        """
        if not KeyUtils.IsPublicCompressed(pub_key_bytes):
            raise ValueError("Public compressed key is required for P2WPKH")

        return SegwitBech32Encoder.Encode(net_addr_ver, P2WPKHConst.WITNESS_VER, CryptoUtils.Hash160(pub_key_bytes))
Ejemplo n.º 6
0
    def ToAddress(pub_key_bytes: bytes, hrp: str) -> str:
        """ Get address in Atom format.

        Args:
            pub_key_bytes (bytes): Public key bytes
            hrp (str)            : HRP

        Returns:
            str: Address string

        Raises:
            ValueError: If key is not a public compressed key
        """
        if not KeyUtils.IsPublicCompressed(pub_key_bytes):
            raise ValueError(
                "Public compressed key is required for Atom address")

        return AtomBech32Encoder.Encode(hrp,
                                        CryptoUtils.Hash160(pub_key_bytes))
Ejemplo n.º 7
0
    def ToAddress(pub_key_bytes, net_addr_ver=BitcoinConf.P2SH_NET_VER.Main()):
        """ Get address in P2SH format.

        Args:
            pub_key_bytes (bytes)         : Public key bytes
            net_addr_ver (bytes, optional): Net address version, default is Bitcoin main network

        Returns:
            str: Address string

        Raises:
            ValueError: If the key is not a public compressed key
        """
        if not KeyUtils.IsPublicCompressed(pub_key_bytes):
            raise ValueError("Public compressed key is required for P2SH")

        # Final address: Base58Check(addr_prefix | address_bytes)
        return Base58Encoder.CheckEncode(net_addr_ver +
                                         P2SHUtils.AddScriptSig(pub_key_bytes))
Ejemplo n.º 8
0
    def ToAddress(pub_key_bytes, hrp, net_addr_ver):
        """ Get address in Bitcoin Cash P2SH format.

        Args:
            pub_key_bytes (bytes): Public key bytes
            hrp (str)            : HRP
            net_addr_ver (int)   : Net address version

        Returns:
            str: Address string

        Raises:
            ValueError: If key is not a public compressed key
        """
        if not KeyUtils.IsPublicCompressed(pub_key_bytes):
            raise ValueError(
                "Public compressed key is required for Bitcoin Cash")

        return BchBech32Encoder.Encode(hrp, net_addr_ver,
                                       P2SHUtils.AddScriptSig(pub_key_bytes))
Ejemplo n.º 9
0
    def ToAddress(pub_key_bytes,
                  net_addr_ver=BitcoinConf.P2PKH_NET_VER.Main(),
                  base58_alph=Base58Alphabets.BITCOIN):
        """ Get address in P2PKH format.

        Args:
            pub_key_bytes (bytes)                  : Public key bytes
            net_addr_ver (bytes, optional)         : Net address version, default is Bitcoin main network
            base58_alph (Base58Alphabets, optional): Base58 alphabet, Bitcoin by default

        Returns:
            str: Address string

        Raises:
            ValueError: If the key is not a public compressed key
        """
        if not KeyUtils.IsPublicCompressed(pub_key_bytes):
            raise ValueError("Public compressed key is required for P2PKH")

        return Base58Encoder.CheckEncode(
            net_addr_ver + CryptoUtils.Hash160(pub_key_bytes), base58_alph)
Ejemplo n.º 10
0
def is_compressed_wif(wif: str):
    key_bytes = Base58Decoder.CheckDecode(wif)
    key_bytes = key_bytes[1:]
    return not KeyUtils.IsPrivate(key_bytes)