def __init__(self, bip32_obj, coin_type): """ 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)
def _ChangeGeneric(cls, bip_obj, change_idx): """ Derive a child key from the specified chain type and return a new Bip object (e.g. BIP44, BIP49, BIP84). It shall be called from a child class. Args: bip_obj (Bip44Base child object): Bip44Base child object (e.g. BIP44, BIP49, BIP84) change_idx (Bip44Changes) : change index, must a Bip44Changes enum Returns: Bip44Base child object: Bip44Base child object Raises: TypeError: If chain index is not a Bip44Changes enum Bip44DepthError: If the current depth is not suitable for deriving keys Bip32KeyError: If the derivation results in an invalid key """ if not isinstance(change_idx, Bip44Changes): raise TypeError( "Change index is not an enumerative of Bip44Changes") if not cls.IsLevel(bip_obj, Bip44Levels.ACCOUNT): raise Bip44DepthError( "Current depth (%d) is not suitable for deriving change" % bip_obj.m_bip32.Depth()) return cls(bip_obj.m_bip32.ChildKey(change_idx), bip_obj.m_coin_type)
def _PurposeGeneric(cls, bip_obj): """ Derive a child key from the purpose and return a new Bip object (e.g. BIP44, BIP49, BIP84). It shall be called from a child class. Args: bip_obj (Bip44Base child object): Bip44Base child object (e.g. BIP44, BIP49, BIP84) Returns: Bip44Base child object: Bip44Base child object Raises: Bip44DepthError: If the current depth is not suitable for deriving keys Bip32KeyError: If the derivation results in an invalid key """ if not cls.IsLevel(bip_obj, Bip44Levels.MASTER): raise Bip44DepthError("Current depth (%d) is not suitable for deriving purpose" % bip_obj.m_bip32.Depth()) return cls(bip_obj.m_bip32.ChildKey(cls._GetPurpose()), bip_obj.m_coin_type)
def _AddressIndexGeneric(cls, bip_obj, addr_idx): """ Derive a child key from the specified address index and return a new Bip object (e.g. BIP44, BIP49, BIP84). It shall be called from a child class. Args: bip_obj (Bip44Base child object): Bip44Base child object (e.g. BIP44, BIP49, BIP84) addr_idx (int) : Address index Returns: Bip44Base child object: Bip44Base child object Raises: Bip44DepthError: If the current depth is not suitable for deriving keys Bip32KeyError: If the derivation results in an invalid key """ if not cls.IsLevel(bip_obj, Bip44Levels.CHANGE): raise Bip44DepthError("Current depth (%d) is not suitable for deriving address" % bip_obj.m_bip32.Depth()) return cls(bip_obj.m_bip32.ChildKey(addr_idx), bip_obj.m_coin_type)
def _CoinGeneric(cls, bip_obj): """ Derive a child key from the coin type specified at construction and return a new Bip object (e.g. BIP44, BIP49, BIP84). It shall be called from a child class. Args: bip_obj (Bip44Base child object): Bip44Base child object (e.g. BIP44, BIP49, BIP84) Returns: Bip44Base child object: Bip44Base child object Raises: Bip44DepthError: If the current depth is not suitable for deriving keys Bip32KeyError: If the derivation results in an invalid key """ if not cls.IsLevel(bip_obj, Bip44Levels.PURPOSE): raise Bip44DepthError("Current depth (%d) is not suitable for deriving coin" % bip_obj.m_bip32.Depth()) coin_idx = Bip44BaseConst.COIN_TO_IDX[bip_obj.m_coin_type] return cls(bip_obj.m_bip32.ChildKey(Bip32Utils.HardenIndex(coin_idx)), bip_obj.m_coin_type)
def _AccountGeneric(cls, bip_obj: 'Bip44Base', acc_idx: int) -> 'Bip44Base': """ Derive a child key from the specified account index and return a new Bip object (e.g. BIP44, BIP49, BIP84). It shall be called from a child class. Args: bip_obj (Bip44Base child object): Bip44Base child object (e.g. BIP44, BIP49, BIP84) acc_idx (int) : Account index Returns: Bip44Base child object: Bip44Base child object Raises: Bip44DepthError: If the current depth is not suitable for deriving keys Bip32KeyError: If the derivation results in an invalid key """ if not cls.IsLevel(bip_obj, Bip44Levels.COIN): raise Bip44DepthError( "Current depth (%d) is not suitable for deriving account" % bip_obj.m_bip32.Depth()) return cls(bip_obj.m_bip32.ChildKey(Bip32Utils.HardenIndex(acc_idx)), bip_obj.m_coin_type)