我从TrustWallet导出了一个12字的助记符种子短语,但是,我需要它作为一个thunderCore私钥。如何从它生成thunderCore私钥?如果种子短语是从Metamask导出的呢?
发布于 2020-04-30 08:44:50
要从12个单词的助记符短语中生成私钥,需要一个在比普-0049中指定的派生路径(字符串)。
字段中使用的派生路径:
m/44'/1001'/0'/0:使用ThunderCore (1001)正确的硬币类型,在滑动-0044中注册,由TrustWallet使用"m/44'/60'/0'/0:是MetaMask使用的Ethereum的派生路径下面是一个使用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分支中的完整项目。
https://stackoverflow.com/questions/61356443
复制相似问题