1

I'm stuck between a rock and a hard place right now.

I'm developing an app with react native, and users need to be able to import their wallets via their private keys (and hopefully mnemonic phrases)

However, when I use the eas toolchain to build my app, I run into an issue with the Solana/web3.js package I was using to do this.

My current code is this:

import * as b58 from "bs58";
import * as bip39 from "bip39";
import * as solanaWeb3 from "@solana/web3.js";

export function deriveSolAddressFromKey(key: string): string {
    try {   
        return solanaWeb3.Keypair.fromSecretKey(
            b58.decode(key.toString().trim())
        ).publicKey.toBase58();
    }
    catch (e) {
        console.error(e);
    }
}

export function deriveSolAddressFromMemonic(phrase: string): string {
    const seed = bip39.mnemonicToSeedSync(phrase, "");
    const keypair = solanaWeb3.Keypair.fromSeed(seed.slice(0, 32));
    return keypair.publicKey.toBase58();
}

How can I do this without the use of solana/web3.js?

1
  • Can you update the question with the error message?
    – BFault
    Commented Jul 28, 2023 at 4:41

1 Answer 1

3

You can use tweetnacl to generate pubkey from the secret key.

...
import * as nacl from 'tweetnacl';
import * as bs58 from 'bs58';
...

export function deriveSolAddressFromKey(key: string): string {
    try {
        let key_pair = nacl.sign.keyPair.fromSecretKey(bs58.decode(key));
        return bs58.encode(key_pair.publicKey);
    }
    catch (e) {
        console.error(e);
    }
}
2
  • Thanks, is there any way to add mnemonic support? I don't fully understand how mnemonics work, so some advice on that would be greatly appreciated.
    – Morgandril
    Commented Jul 29, 2023 at 17:28
  • I did not try mnemonic support. However, solana is using bip39 to generate mnemonic and I think you can use bip39 module. https://www.npmjs.com/package/bip39
    – cowboy
    Commented Jul 31, 2023 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.