Graphical PDS migrator for AT Protocol
1import { Secp256k1Keypair } from "@atproto/crypto";
2import { getSessionAgent } from "../../../lib/sessions.ts";
3import { define } from "../../../utils.ts";
4import * as ui8 from "npm:uint8arrays";
5
6/**
7 * Generate and return PLC keys for the authenticated user
8 */
9export const handler = define.handlers({
10 async GET(ctx) {
11 const agent = await getSessionAgent(ctx.req);
12 if (!agent) {
13 return new Response("Unauthorized", { status: 401 });
14 }
15
16 // Create a new keypair
17 const keypair = await Secp256k1Keypair.create({ exportable: true });
18
19 // Export private key bytes
20 const privateKeyBytes = await keypair.export();
21 const privateKeyHex = ui8.toString(privateKeyBytes, "hex");
22
23 // Get public key as DID
24 const publicKeyDid = keypair.did();
25
26 // Convert private key to multikey format (base58btc)
27 const privateKeyMultikey = ui8.toString(privateKeyBytes, "base58btc");
28
29 // Return the key information
30 return new Response(
31 JSON.stringify({
32 keyType: "secp256k1",
33 publicKeyDid: publicKeyDid,
34 privateKeyHex: privateKeyHex,
35 privateKeyMultikey: privateKeyMultikey,
36 }),
37 {
38 headers: { "Content-Type": "application/json" },
39 },
40 );
41 },
42});