Skip to content

Commit

Permalink
Low-effort did:web support
Browse files Browse the repository at this point in the history
  • Loading branch information
BrokenR3C0RD committed Nov 29, 2024
1 parent 0e8040b commit 3d858fb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 31 deletions.
8 changes: 4 additions & 4 deletions src/app/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export async function resolveHandle(_: string | undefined, formData: FormData) {
}

if (handle.startsWith("did:")) {
if (handle.startsWith("did:plc:")) {
if (handle.startsWith("did:plc:") || handle.startsWith("did:web:")) {
redirect(`/did/${handle}`);
} else {
return "Non-PLC DIDs are not supported by this tool.";
return "Non-PLC/web DIDs are not supported by this tool.";
}
} else {
if (handle.startsWith("@")) handle = handle.slice(1);
Expand All @@ -31,10 +31,10 @@ export async function resolveHandle(_: string | undefined, formData: FormData) {
});

if (res.success) {
if (res.data.did.startsWith("did:plc:")) {
if (res.data.did.startsWith("did:plc:") || res.data.did.startsWith("did:web:")) {
redirect(`/did/${res.data.did}`);
} else {
return "Non-PLC DIDs are not supported by this tool.";
return "Non-PLC/web DIDs are not supported by this tool.";
}
} else {
return "Handle not found. Are you sure it's correct?";
Expand Down
91 changes: 64 additions & 27 deletions src/app/did/[did]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ interface Props {
params: { did: string };
}

interface DidDocument {
"@context": string[];
id: string;
alsoKnownAs?: string[];
verificationMethod?: {
id: string;
type: string;
controller: string;
publicKeyMultibase?: string;
}[];
service?: { id: string; type: string; serviceEndpoint: string }[];
}

export const generateMetadata = async ({
params: { did },
}: Props): Promise<Metadata> => {
Expand Down Expand Up @@ -51,24 +64,28 @@ export default async function InfoScreen({ params: { did } }: Props) {

const agent = getAgent();

const doc = (await fetch(`https://plc.directory/${did}`, {
cache: "no-store",
}).then((res) => res.json())) as {
"@context": string[];
id: string;
alsoKnownAs?: string[];
verificationMethod?: {
id: string;
type: string;
controller: string;
publicKeyMultibase?: string;
}[];
service?: { id: string; type: string; serviceEndpoint: string }[];
};

const audit = (await fetch(`https://plc.directory/${did}/log/audit`, {
cache: "no-store",
}).then((res) => res.json())) as AuditRecord[];
let doc: DidDocument, audit: AuditRecord[], didDomain: string | null;
if (did.startsWith('did:plc:')) {
didDomain = null;

doc = (await fetch(`https://plc.directory/${did}`, {
cache: "no-store",
}).then((res) => res.json())) as DidDocument;

audit = (await fetch(`https://plc.directory/${did}/log/audit`, {
cache: "no-store",
}).then((res) => res.json())) as AuditRecord[];
} else if (did.startsWith('did:web:')) {
didDomain = did.split(':', 3)[2];

doc = (await fetch(`https://${didDomain}/.well-known/did.json`, {
cache: 'no-store',
}).then((res) => res.json())) as DidDocument;

audit = [];
} else {
throw Error('unsupported DID method');
}

const profile = await agent.getProfile({ actor: did }).catch((err) => {
if (err instanceof Error && err.message.includes("deactivated")) {
Expand Down Expand Up @@ -156,8 +173,11 @@ export default async function InfoScreen({ params: { did } }: Props) {
/>
Names and aliases:
</span>{" "}
{doc.alsoKnownAs?.join(", ") ?? "None"} -{" "}
<HistoryDialog log={audit} />
{doc.alsoKnownAs?.join(", ") ?? "None"}
{did.startsWith("did:plc:") && <>
{" - "}
<HistoryDialog log={audit} />
</>}
</p>

<p className="text-sm">
Expand All @@ -168,7 +188,11 @@ export default async function InfoScreen({ params: { did } }: Props) {
/>
First appearance:
</span>{" "}
<DateTime date={new Date(audit[0].createdAt)} />
{
did.startsWith('did:plc:')
? <DateTime date={new Date(audit[0].createdAt)} />
: "Unknown"
}
</p>

<p className="text-sm">
Expand All @@ -186,12 +210,25 @@ export default async function InfoScreen({ params: { did } }: Props) {
<Link href="/">
<Button variant="link">Back</Button>
</Link>
<Link href={`https://web.plc.directory/did/${did}`}>
<Button variant="outline">
View on plc.directory
<ExternalLinkIcon className="ml-2 inline-block" size={14} />
</Button>
</Link>
{
did.startsWith('did:plc:') &&
<Link href={`https://web.plc.directory/did/${did}`}>
<Button variant="outline">
View on plc.directory
<ExternalLinkIcon className="ml-2 inline-block" size={14} />
</Button>
</Link>
}
{
did.startsWith('did:web:') &&
<Link href={`https://${didDomain}/.well-known/did.json`}>
<Button variant="outline">
View DID document
<ExternalLinkIcon className="ml-2 inline-block" size={14} />
</Button>
</Link>
}

</div>
</main>
);
Expand Down

0 comments on commit 3d858fb

Please sign in to comment.