bunch of debugging crap 🦡

This commit is contained in:
dysbulic
2024-01-11 10:50:34 -05:00
committed by Sero
parent 05bb53bc76
commit 18beb540ec

View File

@@ -2,7 +2,7 @@ import { CarReader } from '@ipld/car';
import * as DID from '@ipld/dag-ucan/did';
import * as Delegation from '@ucanto/core/delegation';
import type { Block } from '@ucanto/interface';
import * as Signer from '@ucanto/principal/ed25519';
import { Signer } from '@ucanto/principal/ed25519';
import * as Client from '@web3-storage/w3up-client';
import { CONFIG } from 'config';
import type { NextApiRequest, NextApiResponse } from 'next';
@@ -12,15 +12,19 @@ async function w3sDelegation(did: string) {
throw new Error('$WEB3_STORAGE_KEY is not set.');
}
const principal = Signer.parse(CONFIG.web3StorageKey);
console.debug({ principal });
const client = await Client.create({ principal });
if (!CONFIG.web3StorageProof) {
throw new Error('$WEB3_STORAGE_PROOF is not set.');
}
const proof = await parseProof(CONFIG.web3StorageProof);
console.debug({ proof });
const space = await client.addSpace(proof);
await client.setCurrentSpace(space.did());
console.debug({ space: space.did().toString(), audience: did })
const audience = DID.parse(did);
const abilities = ['store/add', 'upload/add'];
const expiration = Math.floor(Date.now() / 1000) + 60 * 60 * 24; // 24 hours from now
@@ -29,17 +33,20 @@ async function w3sDelegation(did: string) {
});
const archive = await delegation.archive();
console.debug({ archive, ok: archive.ok });
return archive.ok;
}
/** @param {string} data Base64 encoded CAR file */
async function parseProof(proof: string) {
console.debug({ proof });
const blocks = [];
const reader = await CarReader.fromBytes(Buffer.from(proof, 'base64'));
// eslint-disable-next-line no-restricted-syntax
for await (const block of reader.blocks()) {
blocks.push(block as Block<unknown, number, number, 1>);
}
console.debug({ blocks });
return Delegation.importDAG(blocks);
}
@@ -55,10 +62,12 @@ export const handler: (
if (!did) {
throw new Error('Missing `did` in request body.');
}
console.debug({ did });
const ucan = await w3sDelegation(did);
if (!ucan) {
throw new Error(`No UCAN generated for "${did}".`);
}
console.debug({ ucan });
res.status(201).json({ ucan: ucan.toString() });
} catch (error) {
console.error({ 'error generating W3S UCAN': error });