I am trying to get proper public address for Solana wallet using solana-web3.js at my react-native test project
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;
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.
An account keypair used for signing transactions.
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.
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With