简体   繁体   English

如何从 Solana 的助记词中获取正确的公共地址?

[英]How to get proper public address from mnemonic phrase for Solana?

I am trying to get proper public address for Solana wallet using solana-web3.js at my react-native test project我正在尝试在我的 react-native 测试项目中使用solana-web3.js为 Solana 钱包获取正确的公共地址Solflare 钱包截图

import { Keypair} from '@solana/web3.js';
import * as bip39 from 'bip39';
import * as bip32 from 'bip32';

const derivePath = "m/44'/501'/0'/0'";
const mnemonic = "...12 word phrase"

const seed: Buffer = yield bip39.mnemonicToSeed(mnemonic);
// also tried to slice seed.slice(0, 32);
const derivedSeed = bip32.fromSeed(seed).derivePath(derivePath).privateKey;
const keypair = Keypair.fromSeed(derivedSeed);
const publicKey = keypair.publicKey.toString();

I took derive path that is supposed to be for Phantom wallet (and could be chosen at Solflare wallet) But the problem is - I do not get the same public key as I get at these browser wallets.我采用了应该用于 Phantom 钱包的派生路径(并且可以在 Solflare 钱包中选择)但问题是 - 我没有获得与这些浏览器钱包相同的公钥。

So where am I possibly making mistake at code above?那么我在上面的代码中可能在哪里出错?

UPDATE: When I use 'ed25519-hd-key' lib instead of 'bip32' to get derived seed problem disappears.更新:当我使用 'ed25519-hd-key' lib 而不是 'bip32' 来获取衍生种子问题时消失了。

import * as ed25519 from 'ed25519-hd-key';

const derivedSeed = ed25519.derivePath(derivePath, seed.toString('hex')).key;

work variant for me:对我来说工作变体:

import nacl from 'tweetnacl';
import * as bip39  from 'bip39';
import { derivePath } from 'ed25519-hd-key';

const web3 = require('@solana/web3.js')

const seed = await bip39.mnemonicToSeed(mnemonic);
const seedBuffer = Buffer.from(seed).toString('hex');
const path44Change = `m/44'/501'/0'/0'`;
const derivedSeed = derivePath(path44Change, seedBuffer).key;
keypair = new web3.Account(nacl.sign.keyPair.fromSeed(derivedSeed).secretKey);

您应该使用“m/501'/0'/0'”路径

This endpoint on the Blockchain API should help you.区块链 API 上的这个端点应该可以帮助你。

It is called "Derive Public Key" and will derive and return a public key given a secret phrase, a derivation path, and a passphrase.它被称为“派生公钥”,将派生并返回给定密码短语、派生路径和密码短语的公钥。

You can derive the appropriate public key with an API call.您可以通过 API 调用派生适当的公钥。

I've try your same thing, and everything looks good but...I can't understand how solana-keygen generate that public address 'cuz if I backtest with this script on the mnemonic that solana-keygen give to me I can not get the same address... Some suggestion?我已经尝试过您的相同操作,一切看起来都不错,但是...我无法理解solana-keygen是如何生成该公共地址的,因为如果我在solana-keygen给我的助记符上使用此脚本进行回测,我不能获得相同的地址...一些建议? If someone want to try this is what solana-keygen give me:如果有人想尝试这是solana-keygen给我的:


mnemonic = "title spell imitate observe kidney ready interest border inject quiz misery motor"

address = 'nsaayLiawKPiui9fWYCpRdYkdKeqj2fNn9u8LjauEkn'

I'm unable to understand which algorithm they use.我无法理解他们使用哪种算法。

Please, do not fund this wallet.请不要为这个钱包提供资金。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM