DEV Community

Discussion on: The Complete Guide to Full Stack Solana Development with React, Anchor, Rust, and Phantom

Collapse
 
waglik profile image
waglik

Is it possible for base account not to be generated each time? I wanted to use key/pair that I used for deploying the contract but it failed. What is the right way to do it if I want to persist data for the users?

Collapse
 
johnamcconnell profile image
CHIΞFMCCONNΞLL.ΞTH

I think there is a link in the article to make it happen but here is a possible solution.

In the src directory just make a file called createKeyPair.js and add this code:

// Shoutout to Nader Dabit for helping w/ this!
// twitter.com/dabit3

const fs = require('fs')
const anchor = require("@project-serum/anchor")

const account = anchor.web3.Keypair.generate()

fs.writeFileSync('./keypair.json', JSON.stringify(account))

Then inside your app run:
node createKeyPair.js
which should create a keypair.json (which is what holds the keypair for persistence)

Then in the App.js import the json file
import kp from './keypair.json'

and then whereever in your App.js you are calling:
let baseAccount = Keypair.generate();

Just replace with the following:

const arr = Object.values(kp._keypair.secretKey)
const secret = new Uint8Array(arr)
const baseAccount = web3.Keypair.fromSecretKey(secret)

That should be it. now it should be persistent on loading for anyone connected to your app.