-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
52 lines (45 loc) · 1.33 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
export interface DSNPResolver {
resolve(dsnpUserId: bigint): Promise<object | null>;
}
export function driver(dsnpResolvers: DSNPResolver[]) {
async function get(options: { did: string }): Promise<object> {
const did = options.did;
if (!did || !did.startsWith("did:dsnp:")) {
throw new Error("Not a valid DSNP DID");
}
const id = did.substring("did:dsnp:".length);
if (id === "") {
throw new Error("Missing DSNP User Id");
}
let dsnpUserId: bigint = 0n;
try {
dsnpUserId = BigInt(id);
} catch (e) {
throw new Error("Could not parse DSNP User Id");
}
if (dsnpUserId < 0n || dsnpUserId >= 2n ** 64n) {
throw new Error("DSNP User Id not in valid range");
}
for (const dsnpResolver of dsnpResolvers) {
try {
const output = await dsnpResolver.resolve(dsnpUserId);
if (output) return output;
} catch (cause) {
throw new Error(`Error resolving DSNP User Id ${dsnpUserId}`, {
cause,
});
}
}
throw new Error("DSNP User Id not found");
}
if (!dsnpResolvers || dsnpResolvers.length === 0)
throw new Error("Must provide at least one DSNPResolver");
return {
method: "dsnp",
get,
generate: () => {
throw new Error("generate() is not implemented");
},
};
}
export default { driver };