forked from storacha/w3cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.js
61 lines (54 loc) · 1.55 KB
/
account.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
import * as Account from '@web3-storage/w3up-client/account'
import * as Result from '@web3-storage/w3up-client/result'
import * as DidMailto from '@web3-storage/did-mailto'
import { getClient } from './lib.js'
import ora from 'ora'
/**
* @typedef {Awaited<ReturnType<Account.login>>['ok']&{}} View
*/
/**
* @param {DidMailto.EmailAddress} email
*/
export const login = async (email) => loginWithClient(email, await getClient())
/**
* @param {DidMailto.EmailAddress} email
* @param {import('@web3-storage/w3up-client').Client} client
* @returns {Promise<View>}
*/
export const loginWithClient = async (email, client) => {
/** @type {import('ora').Ora|undefined} */
let spinner
const timeout = setTimeout(() => {
spinner = ora(
`🔗 please click the link sent to ${email} to authorize this agent`
).start()
}, 1000)
try {
const account = Result.try(await Account.login(client, email))
Result.try(await account.save())
if (spinner) spinner.stop()
console.log(`⁂ Agent was authorized by ${account.did()}`)
return account
} catch (err) {
if (spinner) spinner.stop()
console.error(err)
process.exit(1)
} finally {
clearTimeout(timeout)
}
}
/**
*
*/
export const list = async () => {
const client = await getClient()
const accounts = Object.values(Account.list(client))
for (const account of accounts) {
console.log(account.did())
}
if (accounts.length === 0) {
console.log(
'⁂ Agent has not been authorized yet. Try `w3 login` to authorize this agent with your account.'
)
}
}