Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 projectScreenshot from Solflare wallet

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 words 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.

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.

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

const derivedSeed = ed25519.derivePath(derivePath, seed.toString('hex')).key;
like image 601
Mikenso Avatar asked Sep 20 '21 02:09

Mikenso


People also ask

What is my Solana public key?

The public key (commonly shortened to pubkey) is known as the wallet's receiving address or simply its address. The wallet address may be shared and displayed freely. When another party is going to send some amount of cryptocurrency to a wallet, they need to know the wallet's receiving address.

What is Solana Keypair?

An account keypair used for signing transactions.

What's a mnemonic phrase?

I. What is a Mnemonic? A mnemonic, also known as a memory aid, is a tool that helps you remember an idea or phrase with a pattern of letters, numbers, or relatable associations. Mnemonic devices include special rhymes and poems, acronyms, images, songs, outlines, and other tools.

How do you generate an address from a mnemonic phrase?

First, we generate random bits to create a new mnemonic phrase. Then, to go from a mnemonic phrase to an address, we have to generate a master key, derive multiple child keys, and finally derive an address from that.

How do you get the private key from a mnemonic phrase?

The process of getting private keys from a mnemonic phrase involves a few steps. These steps are described in a couple different BIPs, Bitcoin Improvement Proposals. In order to get the actual private key, the mnemonic phrase is first turned into a seed.

How is the seed derived from the mnemonic phrase?

The seed is derived from the mnemonic phrase using Password-Based Key Derivation Function 2, also known as PBKDF2. A key derivation function like PBKDF2 takes some input parameters, and gives a cryptographic key back.

How do you generate a SHA256 mnemonic phrase?

We start off by generating 128 bits of entropy. We then take the first entropy length / 32 bits of the SHA-256 hash of the entropy. So, in our case, we take 128 / 32 = 4 bits, which results in 0100 . This is the checksum for our mnemonic phrase.


Video Answer


1 Answers

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);
like image 131
Артур Ельченков Avatar answered Sep 17 '22 19:09

Артур Ельченков