Skip to content

Instantly share code, notes, and snippets.

@topherPedersen
Created January 14, 2022 01:53
Show Gist options
  • Save topherPedersen/53dcd732e58411ede9382494c9aa0e0c to your computer and use it in GitHub Desktop.
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)
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);
}
}
@Pemburu88
Copy link

C5YbjLRa9S4HBk2DsH1HSuSyB52KTA5pSA7SW48KxMi3
Sol... Donasi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment