-1

I'm looking to developing on Solana but...I love to understand what I'm Working on. I've take a look at the documentation, and I can't unuderstand how solana-keygen work. I've try so hard to reproduce the same public address from the same mnemonic but nothing seems to work. Anyone that know exactly how address is generated? If you have your private key how you derive the public without using @solana/web3 library.

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

const seed: Buffer Bip39.mnemonicToSeedSync("title spell imitate observe kidney ready interest border inject quiz misery motor")

const derivedSeed = ed25519.derivePath("m/44'/501'/0'/0'", seed.toString('hex')).key;
const keyPair = Keypair.fromSeed(derivedSeed)
console.log(keyPair.publicKey.toString())   

This code work good, so if I go to https://solflare.com/access and try to insert mnemonic I can see the address.

But, in fact, solana-keygen return me this address with that mnemonic: nsaayLiawKPiui9fWYCpRdYkdKeqj2fNn9u8LjauEkn

This is a sample wallet. Feel free to experiment with this parameters.

Please, do not fund this wallet.

How it's possible to get the same address that solana-keygen give me?

I've try to pass all possible parameters on ed25519, pbkd2, but it seems that I'm missing something on the process.

3 Answers3

1

var bip39 = require('bip39');

var ed25519 = require('ed25519-hd-key');

var solanaWeb3 = require("@solana/web3.js");

Get the master seed from the mnemonics:

masterSeed = bip39.mnemonicToSeedSync(seedPhrase, passPhrase);

Get the derived Solana address seed:

var index = 0; var derivedPath = "m/44'/501'/" + index +"'/0'";

const derivedSeed = ed25519HdKey.derivePath(derivedPath, masterSeed.toString('hex')).key;

Get the keypair:

keypair = solanaWeb3.Keypair.fromSeed(derivedSeed);

Get the wallet address and secret key:

walletAddr = keypair.publicKey.toBase58();

secretKey = keypair.secretKey;

NOTE: To run this in a browser, use browserify as follows:

For example:

Create a file, bip39-input.js:

var bip39 = require('bip39');

Create the browserify bundle:

browserify bip39-input.js -r bip39 -s bip39 \ --exclude=./wordlists/english.json \ --exclude=./wordlists/japanese.json \ --exclude=./wordlists/spanish.json \ --exclude=./wordlists/italian.json \ --exclude=./wordlists/french.json \ --exclude=./wordlists/korean.json \ --exclude=./wordlists/czech.json \ --exclude=./wordlists/portuguese.json \ --exclude=./wordlists/chinese_traditional.json \ -o bip39-bundle.js

Place the bip39-bundle.js file in a script tag.

0

Actually I ran into the same problem before. I still dont get it upto now. But I use another method to workaround. Basically I used solana-keygen recover 'prompt://?key=0/0' -o file.json to recover the keypair into a json. Then open the file and copy the private key back to the code and use let secretKey = Uint8Array.from(private key) to extract. You may find details from my blog https://medium.com/@lianxiongdi/solana-web3-tutorial-2-connect-your-web3-program-to-wallet-39b335f4b4b .

ouflak
  • 2,458
  • 10
  • 44
  • 49
CK Chan
  • 39
  • 1
0

Your trouble is caused by the fact that solana-keygen new doesn't use ed25519HdKey derivation path but it just slices first 32 bytes from seed and then generate keypair from it. Don't worry, I had the same troubles as you before figuring this out .

import bip39 from 'bip39'
import solanaWeb3, { Keypair } from '@solana/web3.js'

async function getKeyCreatedBySolanaKeygenFromMnemonic(mnemonic, password) {
    const pass = ''
    const seed = await bip39.mnemonicToSeed(mnemonic, pass)
    let derivedSeed = seed.subarray(0, 32);
    const kp = Keypair.fromSeed(derivedSeed);
    return kp;
}

async function main() {
    const mnemonic = "title spell imitate observe kidney ready interest border inject quiz misery motor"
    const kp = await getKeyCreatedBySolanaKeygenFromMnemonic(mnemonic);
    console.log('Public key derived from solana-keygen new mnemonic:', kp.publicKey.toBase58())
}

main();

Output: Public key derived from solana-keygen new mnemonic: nsaayLiawKPiui9fWYCpRdYkdKeqj2fNn9u8LjauEkn

riengi
  • 66
  • 2