10

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

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;
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Mikenso
  • 121
  • 2
  • 6

2 Answers2

1

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);
Dapp deep
  • 11
  • 1
0

You should use "m/501'/0'/0'" path