Blockchain
如何使用種子生成比特幣地址?
嗨,我是比特幣的新手,我想知道的是如何使用助記符種子生成地址?
let testnet =Bitcoin.networks.testnet; let keypair =Bitcoin.ECPair.makeRandom({network:testnet}); let address=keypair.getAddress(); let privateKey=keypair.toWIF(); res.json("Public Address "+ address+ " "+privateKey)
使用bitcoinjs-lib和bip39。你可以安裝那些
npm install bitcoinjs-lib bip39 --save
const Bitcoin = require('bitcoinjs-lib'); const Bip39 = require('bip39'); const Bip32 = require('bip32'); function getAddress (node, network) { return Bitcoin.payments.p2pkh({ pubkey: node.publicKey, network }).address } const mnemonic = `entire taste skull already invest view turtle surge razor key next \ buffalo venue canoe sheriff winner wash ten subject hamster scrap unit shield garden`; const seed = Bip39.mnemonicToSeed(mnemonic); const root = Bip32.fromSeed(seed, Bitcoin.networks.bitcoin); const child1 = root.derivePath("m/44'/0'/0'/0/0"); const child2 = root.deriveHardened(44).deriveHardened(0).deriveHardened(0).derive(0).derive(0); console.log(getAddress(child1)); //1ENQm8nEP7sd6dqXbAMYZ4AuqcP8Y7AtR console.log(getAddress(child2)); //1Hb6Z1uZ1RuZ6GXTvedQ2ETYKYsMc5qynN
您可以使用此網站檢查程式碼是否正常工作。您可能還想閱讀一些關於 BIP32、BIP39 和 BIP44 的資訊。執行摘要如下:
- BIP32 描述瞭如何以程式方式從單個密鑰和一些其他屬性生成新的子密鑰。
- BIP39 定義了一組用於模仿鍵的標準詞,以及如何將它們轉換回原始“種子”(來自 BIP32 的鍵)。
- BIP44 指定您應該如何計算來自父密鑰的子密鑰。您可以從我的程式碼中看到我使用
derivePath("m/44'/0'/0'/0/0");
的符合其規範的程式碼。
這是從瀏覽器執行所有內容的完整程式碼:
#!/bin/bash # # call this {anything}.sh # when finished, run chmod +x *.sh # then ./*.sh should execute the script # # Create a ECDSA keypair for use with crypto currencies # The key will be derived from whatever seed phrase is entered by the user # # Copyright (c) 2019 B Tasker read -p "Enter a seed sentence: " seedphrase # Derive a private key privkey=$(echo "$seedphrase" | openssl sha256 | cut -d\ -f2) # Get a proper copy of the private key privkeyfull=$(openssl ec -inform DER -in <(cat <(echo -n "302e0201010420") <(echo -n "$privkey") <(echo -n "a00706052b8104000a") | xxd -r -p) 2>/dev/null) # Now start creating the pub key longpub=$(openssl ec -inform DER -text -noout -in <(cat <(echo -n "302e0201010420") <(echo -n "$privkey") <(echo -n "a00706052b8104000a") | xxd -r -p) 2>/dev/null | tail -6 | head -5 | sed 's/[ :]//g' | tr -d '\n' && echo) # Create the compressed version compub=$(echo -n "$longpub" | cut -c1-66 | sed 's/^04/02/') # Now RipeMD it: hash256=$(echo "$compub" | xxd -r -p | openssl sha256 | cut -d\ -f2) ripemd=$(echo "$hash256" | xxd -r -p | openssl ripemd160 | cut -d\ -f2) # Now RipeMD the uncompressed : hash256=$(echo "$longpub" | xxd -r -p | openssl sha256 | cut -d\ -f2) ripemdunc=$(echo "$hash256" | xxd -r -p | openssl ripemd160 | cut -d\ -f2) # And a version we can pass into OpenSSL if we need to pubkeyfull=$(openssl ec -inform DER -in <(cat <(echo -n "302e0201010420") <(echo -n "e359ae12b3c49fa0d59d0947a97acc9d8595017205909a883501ae09d4ea1888") <(echo -n "a00706052b8104000a") | xxd -r -p) -pubout 2>/dev/null) cat << EOM Seed Phrase: $seedphrase Keys: Private: $privkey Long public: $longpub Compressed Public: $compub RipeMD (Compressed) Public: $ripemd RipeMD (Uncompressed) Public: $ripemdunc PEMs: $privkeyfull $pubkeyfull Keep these safe EOM