Bip39和Python问题(chatGPT写的代码)(更新)

问题描述 投票:0回答:0

此代码要求 Bip39 的前 11 个词,然后应该输出正确的第 12 个词(这是一个校验和词)。然而,根据 ian coleman bip39 工具,它产生的答案是无效的

谁能告诉我它有什么问题吗?

import hashlib
from mnemonic import Mnemonic

def is_word_valid(word, language="english"):
    mnemo = Mnemonic(language)
    return word in mnemo.wordlist

def generate_checksum_word(first_11_words, language="english"):
    mnemo = Mnemonic(language)
    wordlist = mnemo.wordlist
    word_indices = [wordlist.index(word) for word in first_11_words]
    entropy_bits = "".join(f"{idx:011b}" for idx in word_indices)

    entropy_bytes = int(entropy_bits, 2).to_bytes((len(entropy_bits) + 7) // 8, byteorder='big')
    checksum_byte = hashlib.sha256(entropy_bytes).digest()[0]

    missing_bits = entropy_bits + f"{checksum_byte:08b}"[:len(entropy_bits) // 32 + 1]
    last_word_index = int(missing_bits[-11:], 2)

    return wordlist[last_word_index]

def are_words_valid(words_list):
    return all(is_word_valid(word) for word in words_list)

print("Enter the first 11 words of your mnemonic seed separated by spaces:")
while True:
    input_words = input().strip().lower().split()
    if len(input_words) == 11 and are_words_valid(input_words):
        first_11_words = input_words
        break
    else:
        print("Invalid words. Please enter 11 valid BIP 39 words separated by spaces.")

# Generate the 12th checksum word
checksum_word = generate_checksum_word(first_11_words)
print(f"Checksum word: {checksum_word}")

# Print the complete 12-word mnemonic seed
mnemonic_seed = " ".join(first_11_words + [checksum_word])
print(f"Mnemonic seed: {mnemonic_seed}")

输出:

Enter the first 11 words of your mnemonic seed separated by spaces:
fever fall orbit melody grant fan order outside harvest birth mom
Checksum word: uncover
Mnemonic seed: fever fall orbit melody grant fan order outside harvest birth mom uncover

更新:嗯,“愚蠢的”ChatGPT 最终确实编写了生成有效 Bip39 种子的代码(根据 iancoleman.io Bip39 工具)。基本上,我想让它编写一个 python 代码,要求用户输入前 11 个单词并输出一个有效的 12 个单词种子。

import hashlib
from mnemonic import Mnemonic

def get_word_index(word, words):
    return words.index(word)

def main():
    mnemo = Mnemonic("english")
    words = mnemo.wordlist

    user_words = input("Enter 11 space-separated words: ").split()
    if len(user_words) != 11:
        print("Please enter exactly 11 words.")
        return

    try:
        indices = [get_word_index(word, words) for word in user_words]
    except ValueError:
        print("Invalid word entered. Please use words from the official BIP39 word list.")
        return

    for i in range(2048):
        candidate_indices = indices + [i]
        bits = ''.join(['{:011b}'.format(index) for index in candidate_indices])
        data = int(bits[:-4], 2).to_bytes(16, 'big')
        checksum = hashlib.sha256(data).digest()
        target_checksum_bits = int.from_bytes(checksum, 'big') >> (256 - len(bits) // 33)

        if int(bits[-4:], 2) == target_checksum_bits:
            valid_seed = [words[index] for index in candidate_indices]
            valid_seed_phrase = " ".join(valid_seed)
            print("Valid 12-word BIP39 seed: ", valid_seed_phrase)
            break

if __name__ == "__main__":
    main()

Enter 11 space-separated words: cargo drive arrest artist paddle hurt midnight van bacon sea ticket
Valid 12-word BIP39 seed:  cargo drive arrest artist paddle hurt midnight van bacon sea ticket accuse
python bitcoin cryptocurrency openai-api
© www.soinside.com 2019 - 2024. All rights reserved.