Im building a solwallet using Solana/Web3/js, and i need to create a wallet account, so my approach is generate mnemonic so it can become seeds for a keypair, so it’ll be the recovery phrase if needed.
heres my approach code
export default async function generateKeypair() {
const mnemonic = bip39.generateMnemonic();
// Convert mnemonic to binary seed
const seedBytes = await mnemonicToSeed(mnemonic);
const keypair = Keypair.fromSeed(seedBytes);
const privateKey = keypair.secretKey;
const publicKey = keypair.publicKey;
// Convert the private key to a base58 string
const base58PrivateKey = bs58.encode(privateKey);
// Convert into Hex string
const privateKeyHex = Array.from(privateKey)
.map((byte) => byte.toString(16).padStart(2, '0'))
.join('');
return { base58PrivateKey, privateKeyHex, publicKey, mnemonic };
}
But i got an error which from node_modules/react-native
it says
Error creating wallet: [Error: crypto.getRandomValues must be defined]
when I debug it one by one, the cause is from
const mnemonic = bip39.generateMnemonic();
anyway i call it like this
export default function createWallet() {
const handleCreateWallet = async () => {
try {
const { mnemonic } = await generateKeypair();
// console.log(' Base 58 Private Key:', base58PrivateKey);
// console.log(' Hex Private Key:', privateKeyHex);
// console.log('Public Key:', publicKey);
console.log('Mnemonics:', mnemonic);
} catch (error) {
console.error('Error creating wallet:', error);
}
};
return (
<View>
<Button title="Generate Wallet" onPress={handleCreateWallet} />
</View>
);
}
so it got me stuck in here, I cant generate a key pair from mnemonics
ps. im new in this web3 thing, if anyone have different approach or logic so please enlighten me