generated from fission-codes/project-template
/
getters.ts
48 lines (40 loc) · 1.18 KB
/
getters.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
import * as common from "../common/index.js"
import * as dns from "../dns/index.js"
import { isString } from "../common/index.js"
import { getDictionary } from "../ucan/store.js"
import { rootIssuer } from "../ucan/token.js"
import { setup } from "../setup/internal.js"
export * from "./local.js"
/**
* Get the root write-key DID for a user.
* Stored at `_did.${username}.${endpoints.user}`
*/
export async function root(
username: string
): Promise<string> {
const domain = setup.endpoints.user
try {
const maybeDid = await dns.lookupTxtRecord(`_did.${username}.${domain}`)
if (maybeDid !== null) return maybeDid
} catch (_err) {
// lookup failed
}
throw new Error("Could not locate user DID in DNS.")
}
/**
* Get a user's own root write-key DID.
*/
export async function ownRoot(): Promise<string> {
// first try looking in UCAN dictionary
const dict = getDictionary()
const first = Object.values(dict)[0]
if (first !== undefined) {
return rootIssuer(first)
}
// if that fails look up user DNS root
const username = await common.authenticatedUsername()
if (!isString(username)) {
throw new Error("No logged in user")
}
return root(username)
}