1

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

4

2 回答 2

0

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

现场使用的推导路径:

  • m/44'/1001'/0'/0: 使用正确的 ThunderCore ( 1001) 硬币类型,在SLIP-0044中注册,由 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());
  });
})

在 repo 的hdwallet分支中查看完整的项目field-support

于 2020-04-30T08:44:50.860 回答
0
  1. 确保已安装 Node.js 和 NPM。

  2. 打开一个终端并执行以下,假设你有

npx mnemonic-to-private-key “在此处粘贴您的 12 字词组”。

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

于 2021-04-26T23:54:28.140 回答