-
Notifications
You must be signed in to change notification settings - Fork 0
/
whoami.js
63 lines (50 loc) · 1.28 KB
/
whoami.js
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
53
54
55
56
57
58
59
60
61
62
63
const isDomainName = require('is-domain-name')
const { resolveDNS } = require('./util')
const { normalize } = require('./did')
const { resolve } = require('./resolve')
const rc = require('./rc')()
async function whoami(opts) {
const defaults = {
cache: true,
fast: false,
network: rc.network.identity.resolver.network,
keyring: (
rc.network.identity.resolver.keyring
|| rc.network.identity.keyring
|| rc.network.keyring
),
secret: (
rc.network.identity.resolver.secret
|| rc.network.identity.secret
|| rc.network.secret
)
}
const ropts = Object.assign(defaults, opts)
const identifier = (
ropts.identifier
|| rc.network.identity.whoami
|| rc.network.whoami
)
if (!identifier) {
throw new Error('Could not determine identifier in `whoami`.')
}
if (true === ropts.fast) {
if (isDomainName(identifier)) {
return normalize(await resolveDNS(identifier))
}
return normalize(identifier)
}
let ddo = null
try {
ddo = await resolve(identifier, ropts)
} catch (err) {
throw new Error(`Unable to resolve identity: ${identifier}.`)
}
if (!ddo) {
throw new Error('Missing or malformed identity document (ddo.json).')
}
return ddo.id
}
module.exports = {
whoami
}