def Generate(self, passphrase = ""): """ Generate the seed using the specified passphrase. Args: passphrase (str, optional): Passphrase, empty if not specified Returns: bytes: Generated seed """ # Get salt salt = Bip39Const.SEED_SALT_MOD + passphrase # Compute key key = CryptoUtils.Pbkdf2HmacSha512(AlgoUtils.StringEncode(self.m_mnemonic), AlgoUtils.StringEncode(salt), Bip39Const.SEED_PBKDF2_ROUNDS) return key[:Bip39Const.SEED_LEN]
def _DecodeBech32(cls, bech_str, sep, checksum_len): """ Decode and validate a Bech32 string, determining its HRP and data. Args: bech_str (str) : Bech32 string sep (str) : Bech32 separator checksum_len (int): Checksum length Returns: tuple: HRP (index 0) and data part (index 1) Raises: Bech32FormatError: If the string is not valid Bech32ChecksumError: If the checksum is not valid """ # Check string length and case if len(bech_str ) > Bech32BaseConst.MAX_STR_LEN or AlgoUtils.IsStringMixed( bech_str): raise Bech32FormatError("Invalid bech32 format (length not valid)") # Lower string bech_str = bech_str.lower() # Find separator and check its position sep_pos = bech_str.rfind(sep) if sep_pos == -1: raise Bech32FormatError( "Invalid bech32 format (no separator found)") # Get HRP and check it hrp = bech_str[:sep_pos] if len(hrp) == 0 or any(ord(x) < 33 or ord(x) > 126 for x in hrp): raise Bech32FormatError("Invalid bech32 format (HRP not valid)") # Get data and check it data_part = bech_str[sep_pos + 1:] if len(data_part) < Bech32BaseConst.MIN_DATA_PART_LEN or not all( x in Bech32BaseConst.CHARSET for x in data_part): raise Bech32FormatError( "Invalid bech32 format (data part not valid)") # Convert back from alphabet and verify checksum int_data = [Bech32BaseConst.CHARSET.find(x) for x in data_part] if not cls._VerifyChecksum(hrp, int_data): raise Bech32ChecksumError("Invalid bech32 checksum") return hrp, int_data[:-checksum_len]
def ChecksumEncode(addr: str) -> str: """ Checksum encode the specified address. Args: addr (str): Address string Returns: str: Checksum encoded address """ # Compute address digest addr_digest = sha3.keccak_256(AlgoUtils.Encode(addr)).hexdigest() # Encode it enc_addr = [c.upper() if (int(addr_digest[i], 16) >= 8) else c.lower() for i, c in enumerate(addr)] return "".join(enc_addr)
def GetWordIdx(self, word: str) -> int: """ Get the index of the specified word, by searching it in the list. Args: word (str): Word to be searched Returns: int: Word index Raises: ValueError: If the word is not found """ idx = AlgoUtils.BinarySearch(self.m_words_list, word) if idx == -1: raise ValueError("Word %s is not existent in word list" % word) return idx
def GetWordIdx(self, word: str) -> int: """ Get the index of the specified word, by searching it in the list. Args: word (str): Word to be searched Returns: int: Word index Raises: ValueError: If the word is not found """ # Use binary search when possible if self.m_lang in self.BIN_SEARCH_LANG: idx = AlgoUtils.BinarySearch(self.m_words_list, word) else: idx = self.m_words_list.index(word) # Check index if idx == -1: raise ValueError("Word '%s' is not existent in word list" % word) return idx