如何从 TrustWallet 或 Metamask 导出的 12 字助记种子短语为 ThunderCore 生成私钥?

How do I generate private keys for ThunderCore from a 12-word mnemonic seed phrase exported from TrustWallet or Metamask?

我从 TrustWallet 导出了一个 12 字的助记词种子短语,但是,我需要它作为 thunderCore 私钥。我如何从中生成 thunderCore 私钥?如果种子短语是从 Metamask 导出的怎么办?

要从12个单词的助记词生成私钥,您需要BIP-0049中指定的推导路径(字符串)。

字段中使用的推导路径:

  • m/44'/1001'/0'/0:使用在 SLIP-0044 中注册的 ThunderCore (1001) 的正确硬币类型,由 TrustWallet
  • 使用
  • "m/44'/60'/0'/0:是以太坊主网使用的推导路径,被MetaMask
  • 使用

这是一个使用 ethereum-hdwallet 库从 12 字助记符生成私钥的独立示例:

hdwallet.js

const EthereumHDWallet = require('ethereum-hdwallet')

// https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki
// https://github.com/satoshilabs/slips/blob/master/slip-0044.md
const TrustWalletHdPath = "m/44'/1001'/0'/0" // coin_type 1001 is ThunderCore, this is the recommended path
const MetaMaskHdPath = "m/44'/60'/0'/0"      // coin_type 60 is really Ethereum, but MetaMask use it for all EVM compatible chains

class HdWallet {
  constructor(mnemonic) {
    this.mnemonic = this.mnemonic
    this.w = EthereumHDWallet.fromMnemonic(mnemonic)
  }
  deriveUsingTrustWalletPath() {
    return this.w.derive(TrustWalletHdPath)
  }
  deriveUsingMetaMaskPath() {
    return this.w.derive(MetaMaskHdPath)
  }
  metaMaskAddress(index /*: number */) /*: string */ {
    return '0x' + this.deriveUsingMetaMaskPath().derive(index).getAddress().toString('hex')
  }
  trustWalletAddress(index /*: number */) /*: string */ {
    return '0x' + this.deriveUsingTrustWalletPath().derive(index).getAddress().toString('hex')
  }
  metaMaskPrivateKey(index /*: number */) /*: string */ {
    return this.deriveUsingMetaMaskPath().derive(index).getPrivateKey().toString('hex')
  }
  trustWalletPrivateKey(index /*: number */) /*: string */ {
    return this.deriveUsingTrustWalletPath().derive(index).getPrivateKey().toString('hex')
  }
}

const fromMnemonic = (s /*: string or buffer */) => {
  return new HdWallet(s)
}

module.exports = {
  fromMnemonic: fromMnemonic,
}

testHdWallet.js

const assert = require('assert');
const HdWallet = require('../src/hdwallet');

const mnemonic = 'feel pulp crunch segment buzz turn organ broccoli elder ask phone limit';

describe('fromMnemonic', () => {
  it('trustWalletAddress', async() => {
    const w = HdWallet.fromMnemonic(mnemonic);
    console.log('TrustWallet:', w.trustWalletAddress(0));
    assert('0x2323Beb990514446bA4c073C2e1A4BDC0ECf06Af'.toLowerCase() ===
            w.trustWalletAddress(0).toLowerCase());
  });
  it('metaMaskAddress', async() => {
    const w = HdWallet.fromMnemonic(mnemonic);
    console.log('MetaMask:', w.metaMaskAddress(0));
    assert('0x9A7be7ae9a2779167bc5b64d1cC672cc5b2593e4'.toLowerCase() ===
            w.metaMaskAddress(0).toLowerCase());
  });
  it('trustWalletPrivateKey', async() => {
    const w = HdWallet.fromMnemonic(mnemonic);
    console.log('TrustWallet sk:', w.trustWalletPrivateKey(0));
    assert('6d7bf444545ce47d7fda9df58275f5f4dd5eb911494ab66d81f76f1aca2b763e'.toLowerCase() ===
            w.trustWalletPrivateKey(0).toLowerCase());
  });
  it('metaMaskPrivateKey', async() => {
    const w = HdWallet.fromMnemonic(mnemonic);
    console.log('MetaMask sk:', w.metaMaskPrivateKey(0));
    assert('6aad31c479c44230721b470570c12bd3f41e71b79d8f27ca08b913cbaeac25af'.toLowerCase() ===
            w.metaMaskPrivateKey(0).toLowerCase());
  });
})

field-support 存储库的 hdwallet 分支中查看完整项目。

  1. 确保您已安装 Node.js 和 NPM。

  2. 打开终端并执行以下命令,假设您有

npx mnemonic-to-private-key "paste your 12 word phrase here".

不要使用在线网站,因为您永远不知道它们是否会存储您的短语。事实上,即使是上面的“助记符到私钥”存储库也可能被破坏,就像“Great Suspender”一样。所以最好使用 npm 的版本固定。