Adding this snippet as reference for one way to generate and reuse the privatekey from a burner keypair. If someone finds a simpler way, please also share.
import {
getBase58Decoder,
createKeyPairSignerFromPrivateKeyBytes,
} from "@solana/web3.js";
// Generate a keypair
const keypair = await crypto.subtle.generateKey("Ed25519", true, [
"sign",
"verify",
]);
console.log("keypair", keypair);
// keypair [Object: null prototype] {
// privateKey: CryptoKey {
// type: 'private',
// extractable: true,
// algorithm: { name: 'Ed25519' },
// usages: [ 'sign' ]
// },
// publicKey: CryptoKey {
// type: 'public',
// extractable: true,
// algorithm: { name: 'Ed25519' },
// usages: [ 'verify' ]
// }
// }
// Export private key as JWK
const privateKeyJwk = await crypto.subtle.exportKey("jwk", keypair.privateKey);
console.log("privateKeyJwk", privateKeyJwk);
// privateKeyJwk {
// key_ops: [ 'sign' ],
// ext: true,
// crv: 'Ed25519',
// d: '6nwXBth_bVK0po7ub3rq4DW95Gp5V2k7KEHCwipfqjs',
// x: '1tk7CFLGL2kOkwfVxqwsTMFcfk0k79UUmNtY1cJ0PyM',
// kty: 'OKP'
// }
// The 'd' parameter in JWK contains the base64 private key
const privateKeyBase64 = privateKeyJwk.d;
if (!privateKeyBase64) throw new Error("Failed to get private key bytes");
console.log("privateKeyBase64", privateKeyBase64);
// privateKeyBase64 6nwXBth_bVK0po7ub3rq4DW95Gp5V2k7KEHCwipfqjs
// Decode base64url to get the raw 32 bytes private key
const privateKeyBytes = new Uint8Array(Buffer.from(privateKeyBase64, "base64"));
console.log("privateKeyBytes", privateKeyBytes);
// privateKeyBytes Uint8Array(32) [
// 234, 124, 23, 6, 216, 127, 109, 82,
// 180, 166, 142, 238, 111, 122, 234, 224,
// 53, 189, 228, 106, 121, 87, 105, 59,
// 40, 65, 194, 194, 42, 95, 170, 59
// ]
// Get public key and convert to base58
const publicKey = await crypto.subtle.exportKey("raw", keypair.publicKey);
const publicKeyBytes = new Uint8Array(publicKey);
const publicKeyBase58 = getBase58Decoder().decode(publicKeyBytes);
console.log("publicKey:", publicKeyBase58);
// publicKey: FTgNJ8XB4ajD1BYir1MjNMS1PyPvNo8eJkika8nWGh9k
// Create web3js KeyPairSigner
const keypairSigner = await createKeyPairSignerFromPrivateKeyBytes(
privateKeyBytes
);
console.log("keypairSigner", keypairSigner);
// keypairSigner {
// address: 'FTgNJ8XB4ajD1BYir1MjNMS1PyPvNo8eJkika8nWGh9k',
// keyPair: {
// privateKey: CryptoKey {
// type: 'private',
// extractable: false,
// algorithm: { name: 'Ed25519' },
// usages: [ 'sign' ]
// },
// publicKey: CryptoKey {
// type: 'public',
// extractable: true,
// algorithm: { name: 'Ed25519' },
// usages: [ 'verify' ]
// }
// },
// signMessages: [Function: signMessages],
// signTransactions: [Function: signTransactions]
// }