'Best way to save a secretKey

I'm learning Solana API on ReactJS. After generating a keypair you can recreate the same keypair with the secretKey. How can I save this keypair? I'm using react and I want to recreate always the same keypair unless I generate another.



Solution 1:[1]

The question of "best" is a matter of opinion, but in your case, since you're just learning and using this keypair for development, your best bet is to save it to your local disk.

If you're developing in a browser environment, you can just hardcode the bytes of your secret key in the code.

Later on, once you're deployed to mainnet and want to work safely, a hardware wallet is likely your best option.

You can find more information on wallets in the docs: https://docs.solana.com/wallet-guide

Solution 2:[2]

I write it to a file. Create generateKeypair.js in src directory and add this

const fs=require('fs')
const anchor=require("@project-serum/anchor")
const account=anchor.web3.Keypair.generate()
fs.writeFileSync('./keypair.json',JSON.stringify(account))

then execute this script once:

node src/generateKeypair.js

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jon C
Solution 2