BlockChain區塊鏈
  • 本書簡介
  • 區塊鏈運作原理
  • Bitcoin介紹
    • 簡介
    • Bitcoin其他知識
  • Bitcoin原理與實作
  • BitcoinJS
    • BTC 地址格式種類
    • 從 Mnemonic 轉為地址
  • Serverless 架構實作
  • Docker常用指令
  • ethereum初探
    • ethereum歷史
    • EVM
  • ethereum PoS 節點架設
  • ethereum(Docker)
  • ethereum(Geth)
    • Parity
  • ethereum(智能合約)
    • 合約測試 Unit Test
    • DAO
    • 可升級合約
    • 使用合約進行 multiswap
    • 合約安全
    • 開發工具
    • Hardhat 教學
      • Hardhat 寫測試
    • ERC-721 範例
      • 白名單機制
    • OpenZeppelin 合約 library
    • Truffle
    • 合約部屬
    • solidity 教學
  • ethereum(Dapp)
    • 相關 SDK
    • Multicall
    • Ethers.js 使用
    • Remix IDE
    • web3.js 使用
    • 在網頁上使用 web3 並操作區塊鏈
      • solidity筆記
  • Hyperledger Fabric
  • blockchainDB
  • 挖礦程式使用教學
    • 門羅幣/Monero (XMR)
  • Bitfinex API 使用
  • FTX API
  • CCXT 通用交易所 API
  • Solana 教學
  • Ethereum BigQuery
  • The Graph
    • yaml 定義
    • mapping 語法
    • Schema 定義
    • Query 範例
    • Unit test
  • DeFi 筆記
    • MEV 相關
    • Dex 聚合
    • Yearn
    • Curve
    • Uniswap
      • Swap 互動
    • AAVE、Compound
      • Compound 原理
      • AAVE 合約開發
Powered by GitBook
On this page
  • 原理
  • 範例:
  • privateKeyPrefix

Was this helpful?

  1. BitcoinJS

從 Mnemonic 轉為地址

PreviousBTC 地址格式種類NextServerless 架構實作

Last updated 11 months ago

Was this helpful?

從註記詞轉為地址可以參考 BIP44:

原理

1.Mnemonic 為 12 個英文單字,會先按照 BIP39 轉為 Seed(使用 PBKDF2 function)

2.之後從 Seed derive 出地址的private key buffer(不同 path 可以產生不同地址)

btcRoot.derivePath("m/44'/3'/0'/0/0")

3.之後在 private key buffer 前方加上在 version byte

4.將結果經過兩次 sha256,取前四個 byte 為 checksum

5.之後將原本的 private key buffer 最後串上第四部的 checksum

6.使用 base58 encode 後即為 WIF 的 private key。

範例:

以下產生 Doge chain 的地址

const bitcoin = require('bitcoinjs-lib');
const ecc = require('tiny-secp256k1')
const { BIP32Factory } = require('bip32')
const bip39 = require('bip39');
const bip32 = BIP32Factory(ecc)
const { Signer, SignerAsync, ECPairInterface, ECPairFactory, ECPairAPI, TinySecp256k1Interface } = require('ecpair');
const ECPair = ECPairFactory(ecc);
const bs58 = require('bs58')

const btcMnemonic = '...';
const dogeVersionByte = 0x9E;

const btcSeed = bip39.mnemonicToSeedSync(btcMnemonic);
const btcRoot = bip32.fromSeed(btcSeed);
const btcPrivateKey = btcRoot.derivePath("m/44'/3'/0'/0/0").toWIF(); // Replace the path as needed

const btcPrivateKeyBuffer = ECPair.fromWIF(btcPrivateKey).privateKey;
const dogePrivateKeyBuffer = Buffer.concat([Buffer.from([dogeVersionByte]), btcPrivateKeyBuffer]);

const firstSha256 = bitcoin.crypto.sha256(dogePrivateKeyBuffer);
const secondSha256 = bitcoin.crypto.sha256(firstSha256);
const checksum = secondSha256.slice(0, 4);

const dogePrivateKeyWithChecksum = Buffer.concat([dogePrivateKeyBuffer, checksum]);


const dogePrivateKeyWIF = bs58.encode(dogePrivateKeyWithChecksum);

console.log('DOGE Private Key:', dogePrivateKeyWIF);
  • m: This indicates that it's a BIP32 mnemonic code (i.e., a seed phrase).

  • 44': The first number (44) represents the BIP44 purpose. Purpose 44 is used for cryptocurrencies. The apostrophe ('), which is a hardened derivation, ensures that child keys are derived using hardened keys.

  • 3': The second number (3) represents the coin type for Dogecoin. Coin type 3 is commonly used for Dogecoin according to the BIP44 specification.

  • 0': The third number (0) is the account level. This is where you can specify different accounts within your wallet, but in this case, it's set to 0.

  • 0: The next two numbers (0/0) represent the index of the external (receiving) addresses. The first 0 indicates the account's external addresses, and the second 0 indicates the first receiving address.

privateKeyPrefix

轉為 Seed 的過程為 one-way hashing,不可逆,意思為不可從 private key 轉回 Mnemonic。

https://learnmeabitcoin.com/technical/derivation-paths
https://github.com/walletgeneratornet/WalletGenerator.net/blob/36cefb15c625f86c1427e9a17c2bb8d5140918a4/src/janin.currency.js#L196
Mnemonic Seed - A simple explanation of BIP39.Learn Me A Bitcoinhttps://learnmeabitcoin.com › technical › mnemonic
https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
slips/slip-0044.md at master · satoshilabs/slipsGitHub
Logo