Created
January 14, 2022 01:53
-
-
Save topherPedersen/53dcd732e58411ede9382494c9aa0e0c to your computer and use it in GitHub Desktop.
Solana 101: Creating Solana Accounts AKA Keypair (From Figment.io's Solana 101 Course)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type {NextApiRequest, NextApiResponse} from 'next'; | |
import {Keypair} from '@solana/web3.js'; | |
/* | |
* Like with most Web 3 protocols, transactions on Solana happen between accounts. | |
* To create an account, a client generates a keypair which has a public key (or | |
* address, used to identify and lookup an account) and a secret key used to sign | |
* transactions. | |
*/ | |
type ResponseT = { | |
secret: string; | |
address: string; | |
}; | |
export default function keypair( | |
_req: NextApiRequest, | |
res: NextApiResponse<string | ResponseT>, | |
) { | |
try { | |
const keypair = Keypair.generate(); | |
const address = keypair?.publicKey.toString(); | |
const secret = JSON.stringify(Array.from(keypair.secretKey)); | |
res.status(200).json({ | |
secret, | |
address, | |
}); | |
} catch (error) { | |
let errorMessage = error instanceof Error ? error.message : 'Unknown Error'; | |
res.status(500).json(errorMessage); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
C5YbjLRa9S4HBk2DsH1HSuSyB52KTA5pSA7SW48KxMi3
Sol... Donasi