// POST /api/buy {"owner", "count"}: transactions that each mint one blank in the wave on sale, // signed by the new blank's key and the co-signer. The wallet signs them next and the app hands // them to /api/send. import { buildBuy } from '../../server/card.ts' import { ConfigError, configFailure, contextFor, cosignerFor, gate, type CardEnv } from '../../server/env.ts' import { failure, json, readBody } from '../../server/http.ts' export const onRequestPost: PagesFunction = async ({ request, env }) => { const body = await readBody(request, ['owner', 'count']) if (!body) return json({ error: 'Expected {"owner", "count"}.', code: 'bad-request' }, 400) const refused = await gate(request, env, 'buy', 20) if (refused) return refused try { const ctx = await contextFor(env) const built = await buildBuy(ctx, await cosignerFor(env, ctx), { owner: body.owner, count: body.count }) console.log('buy built', built.wave, built.count, String(body.owner)) return json(built) } catch (err) { return err instanceof ConfigError ? configFailure(err) : failure(err, 'buy') } }