Exemple #1
0
    def FromAddressPrivKey(cls, key_bytes, coin_type):
        """ Create a Bip object (e.g. BIP44, BIP49, BIP84) from the specified private key related to an address.

        Args:
            key_bytes (bytes)     : Key bytes
            coin_type (Bip44Coins): Coin type, must be a Bip44Coins enum

        Returns:
            Bip object: Bip object

        Raises:
            TypeError: If coin index is not a Bip44Coins enum
            Bip44CoinNotAllowedError: If the coin is not allowed to derive from the BIP specification
            Bip32KeyError: If the key is not valid
        """
        if not cls.IsCoinAllowed(coin_type):
            raise Bip44CoinNotAllowedError(
                "Coin %s cannot derive from %s specification" %
                (coin_type, cls.SpecName()))
        return cls(
            Bip32(key_bytes,
                  b"",
                  Bip44Levels.ADDRESS_INDEX,
                  key_net_ver=cls._GetCoinClass(coin_type).KeyNetVersions()),
            coin_type)
Exemple #2
0
    def FromSeed(cls, seed_bytes, coin_type):
        """ Create a Bip object (e.g. BIP44, BIP49, BIP84) from the specified seed (e.g. BIP39 seed).
        The test net flag is automatically set when the coin is derived. However, if you want to get the correct master
        or purpose keys, you have to specify here if it's a test net.

        Args:
            seed_bytes (bytes)   : Seed bytes
            coin_type (Bip44Coins): Coin type, must be a Bip44Coins enum

        Returns:
            Bip object: Bip object

        Raises:
            TypeError: If coin index is not a Bip44Coins enum
            ValueError: If the seed is too short
            Bip44CoinNotAllowedError: If the coin is not allowed to derive from the BIP specification
            Bip32KeyError: If the seed is not suitable for master key generation
        """
        if not cls.IsCoinAllowed(coin_type):
            raise Bip44CoinNotAllowedError(
                "Coin %s cannot derive from %s specification" %
                (coin_type, cls.SpecName()))
        return cls(
            Bip32.FromSeed(seed_bytes,
                           cls._GetCoinClass(coin_type).KeyNetVersions()),
            coin_type)
Exemple #3
0
    def __init__(self, bip32_obj: Bip32, coin_type: Bip44Coins) -> None:
        """ Construct class from a Bip32 object and coin type.

        Args:
            bip32_obj (Bip32 object): Bip32 object
            coin_type (Bip44Coins)  : Coin type, must be a Bip44Coins enum

        Returns:
            Bip44DepthError: If the Bip32 object depth is not valid
        """

        # If the Bip32 is public-only, the depth shall start from the account level because hardened derivation is
        # used below it, which is not possible with public keys
        if bip32_obj.IsPublicOnly():
            if bip32_obj.Depth() < Bip44Levels.ACCOUNT or \
                    bip32_obj.Depth() > Bip44Levels.ADDRESS_INDEX:
                raise Bip44DepthError(
                    "Depth of the public-only Bip32 object (%d) is below account level or beyond address index level"
                    % bip32_obj.Depth())
        # If the Bip32 object is not public-only, any depth is fine as long as it is not greater
        # than address index level
        else:
            if bip32_obj.Depth() > Bip44Levels.ADDRESS_INDEX:
                raise Bip44DepthError(
                    "Depth of the Bip32 object (%d) is beyond address index level"
                    % bip32_obj.Depth())

        # Finally, initialize class
        self.m_bip32 = bip32_obj
        self.m_coin_type = coin_type
        self.m_coin_class = self._GetCoinClass(coin_type)
Exemple #4
0
    def FromExtendedKey(cls, key_str, coin_type):
        """ Create a Bip object (e.g. BIP44, BIP49, BIP84) from the specified extended key.

        Args:
            key_str (str)        : Extended key string
            coin_type (Bip44Coins): Coin type, must be a Bip44Coins enum

        Returns:
            Bip object: Bip object

        Raises:
            TypeError: If coin index is not a Bip44Coins enum
            Bip44CoinNotAllowedError: If the coin is not allowed to derive from the BIP specification
            Bip32KeyError: If the extended key is not valid
        """
        if not cls.IsCoinAllowed(coin_type):
            raise Bip44CoinNotAllowedError("Coin %s cannot derive from %s specification" % (coin_type, cls.SpecName()))
        return cls(Bip32.FromExtendedKey(key_str, cls._GetCoinClass(coin_type).KeyNetVersions()), coin_type)