Python

如何從 Python 中的 BTC 助記詞中獲取正確的地址

  • October 18, 2021

遇到一個問題,我有一個用 Python 編寫的加密錢包,有一個程式碼可以從助記詞(在本例中為 BTC 地址)創建錢包地址。但如果我將這個助記詞輸入到 Trust Wallet 移動應用程序中,它會給我一個完全不同的比特幣地址。為什麼?如何解決?這是程式碼:

from bipwallet import wallet
from bipwallet.utils import *

Seed = wallet.generate_mnemonic()

WalletBTC = wallet.create_wallet(network="BTC", seed=Seed, children=0)

AddressBTC = WalletBTC.get("address")

print("BTC Address: ", AddressBTC)

我假設 Python 庫和 Trustwallet 都實現了 BIP32 來創建分層確定性 (HD) 錢包。

你得到的地址取決於派生路徑。如果您的 Python 庫和 Trustwallet 使用不同的派生路徑,您將獲得不同的地址。

一個 HD 錢包會產生多個地址,每次你請求一個地址時都會產生一個不同的地址。因此,您必須注意不要將一個錢包的第一個地址與另一個錢包的第二個地址進行比較。

謝謝你。我獨立發現了與 Trust Wallet 以及 Cryptoneed(移動應用程序)生成相同地址的庫。誰需要我的程式碼,自己複製和定制:

from hdwallet import BIP44HDWallet
from hdwallet.utils import generate_mnemonic
from hdwallet.derivations import BIP44Derivation
from hdwallet.cryptocurrencies import BitcoinMainnet, EthereumMainnet
from bip_utils import Bip84, Bip84Coins, Bip44Changes, Bip39SeedGenerator


LanguageInMnemonic = "english"

Mnemonic = generate_mnemonic(language = LanguageInMnemonic, strength = 128)
   Seed = Bip39SeedGenerator(Mnemonic).Generate()

   print(Mnemonic)

   if IsCheckBTC == "+":
       if IsCheckBTCBip44 == "+":
           CheckedWalletBTCBip44 = BIP44HDWallet(cryptocurrency=BitcoinMainnet)
           CheckedWalletBTCBip44.from_mnemonic(mnemonic = Mnemonic, language = LanguageInMnemonic, passphrase = None)
           CheckedWalletBTCBip44.clean_derivation()
           CheckedWalletBTCBip44Derivation = BIP44Derivation(cryptocurrency = BitcoinMainnet, account = 0, change = False, address = 0)
           CheckedWalletBTCBip44.from_path(path = CheckedWalletBTCBip44Derivation)

           CheckedAddressBTCBip44 = CheckedWalletBTCBip44.address()
           print(CheckedAddressBTCBip44)

       if IsCheckBTCBip84 == "+":
           CheckedWalletBTCBip84 = Bip84.FromSeed(Seed, Bip84Coins.BITCOIN)
           CheckedWalletBTCBip84Step1 = (CheckedWalletBTCBip84.Purpose().Coin().Account(0)).Change(Bip44Changes.CHAIN_EXT)
           CheckedWalletBTCBip84Step2 = CheckedWalletBTCBip84Step1.AddressIndex(0)

           CheckedAddressBTCBip84 = CheckedWalletBTCBip84Step2.PublicKey().ToAddress()
           print(CheckedAddressBTCBip84)

   if IsCheckETH == "+":
       CheckedWalletETH = BIP44HDWallet(cryptocurrency = EthereumMainnet)
       CheckedWalletETH.from_mnemonic(mnemonic = Mnemonic, language = LanguageInMnemonic, passphrase = None)
       CheckedWalletETH.clean_derivation()
       CheckedWalletETHDerivation: BIP44Derivation = BIP44Derivation(cryptocurrency = EthereumMainnet, account = 0, change = False, address = 0)
       CheckedWalletETH.from_path(path=CheckedWalletETHDerivation)

       CheckedAddressETH = CheckedWalletETH.address()
       print(CheckedAddressETH)

(PS我用Google翻譯)

引用自:https://bitcoin.stackexchange.com/questions/110248