[英]how to generate keypair from secret phrase (mnemonic) SOLANA
I am working with solana and phantom wallet I have a wallet with a public key and I have it's secret phrase composed of 12 wordw.我正在使用 solana 和幻像钱包我有一个带公钥的钱包,我有一个由 12 个 wordw 组成的秘密短语。 when I generate keypairs from the secret phrase I use :
当我从我使用的秘密短语生成密钥对时:
const getKeyPair = (mnemomic) => {
const seed = bip39.mnemonicToSeedSync(mnemomic).slice(0, 32);
const Keypair = web3.Keypair.fromSeed(seed);
return Keypair;
};
the generated keypair has publicKey and privateKey , but when am checking my balance using the generated public key I find the balance is always 0 even when I try to airdrop sols using my code it's not getting in the account.生成的密钥对具有 publicKey 和 privateKey ,但是当我使用生成的公钥检查我的余额时,我发现余额始终为 0,即使我尝试使用我的代码空投 sols 也没有进入帐户。
But if I check using my public key from phantom wallet I get the sols I have and if I want to airdrop sols they also proceed normally.但是,如果我使用幻像钱包中的公钥进行检查,我会得到我拥有的 sols,如果我想空投 sols,它们也会正常进行。
Why is my generated public key is not the same as the one in phantom wallet?为什么我生成的公钥与虚拟钱包中的不一样?
First, we need to generate the publicKey and privateKey from the seed phrase using this command :首先,我们需要使用以下命令从种子短语生成publicKey和privateKey :
solana-keygen recover 'prompt:?key=0/0' -o phantom_wallet.json
solana-keygen 恢复 'prompt:?key=0/0' -o phantom_wallet.json
remark : our account may have many wallets the command above access the first wallet , if we need to access wallet number 10 for example we need to change the 'prompt:?key=0/0' to 'prompt:?key=10/0' "only the first 0 is changing"
备注:我们的账户可能有很多钱包上面的命令访问第一个钱包,如果我们需要访问10号钱包例如我们需要将'prompt:?key=0/0'更改为'prompt:?key=10/ 0' "只有第一个 0 在改变"
next this will generate public key "will be written in the console" and a json file with secret key contains an array of 64 element which is the secret key .接下来,这将生成公钥“将写入控制台”,并且带有密钥的 json 文件包含一个64 元素的数组,即密钥。 now to generate the KEYPAIRS in solana web3 javascript SDK we do the following:
现在要在 solana web3 javascript SDK 中生成密钥对,我们执行以下操作:
let seed = Uint8Array.from(
process.env.REACT_APP_OUR_SECRET_KEY.split(",")
).slice(0, 32);
// create keypairs
let KEYPAIRS = web3.Keypair.fromSeed(seed);
IMPORTANT : in .env files REACT_APP_OUR_SECRET_KEY is stored without brackets [ ] example : 15,320,52,... as we see a table without brackets [ ]
重要提示:在 .env 文件中,REACT_APP_OUR_SECRET_KEY 存储时不带括号 [] 示例:15,320,52,... 因为我们看到的表格不带括号 []
solana-keygen recover 'prompt:?key=0/0' -o phantom_wallet.json
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.