-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathspace.js
332 lines (293 loc) · 7.96 KB
/
space.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import * as ED25519 from '@ucanto/principal/ed25519'
import { delegate, Schema, UCAN, error, fail } from '@ucanto/core'
import * as BIP39 from '@scure/bip39'
import { wordlist } from '@scure/bip39/wordlists/english'
import * as API from './types.js'
import * as Access from './access.js'
import * as Provider from './provider.js'
/**
* Data model for the (owned) space.
*
* @typedef {object} Model
* @property {ED25519.EdSigner} signer
* @property {string} name
* @property {API.Agent} [agent]
*/
/**
* Generates a new space.
*
* @param {object} options
* @param {string} options.name
* @param {API.Agent} [options.agent]
*/
export const generate = async ({ name, agent }) => {
const { signer } = await ED25519.generate()
return new OwnedSpace({ signer, name, agent })
}
/**
* Recovers space from the saved mnemonic.
*
* @param {string} mnemonic
* @param {object} options
* @param {string} options.name - Name to give to the recovered space.
* @param {API.Agent} [options.agent]
*/
export const fromMnemonic = async (mnemonic, { name, agent }) => {
const secret = BIP39.mnemonicToEntropy(mnemonic, wordlist)
const signer = await ED25519.derive(secret)
return new OwnedSpace({ signer, name, agent })
}
/**
* Turns (owned) space into a BIP39 mnemonic that later can be used to recover
* the space using `fromMnemonic` function.
*
* @param {object} space
* @param {ED25519.EdSigner} space.signer
*/
export const toMnemonic = ({ signer }) => {
/** @type {Uint8Array} */
// @ts-expect-error - Field is defined but not in the interface
const secret = signer.secret
return BIP39.entropyToMnemonic(secret, wordlist)
}
/**
* Creates a (UCAN) delegation that gives full access to the space to the
* specified `account`. At the moment we only allow `did:mailto` principal
* to be used as an `account`.
*
* @param {Model} space
* @param {API.AccountDID} account
*/
export const createRecovery = (space, account) =>
createAuthorization(space, {
agent: space.signer.withDID(account),
access: Access.accountAccess,
expiration: Infinity,
})
// Default authorization session is valid for 1 year
export const SESSION_LIFETIME = 60 * 60 * 24 * 365
/**
* Creates (UCAN) delegation that gives specified `agent` an access to
* specified ability (passed as `access.can` field) on this space.
* Optionally, you can specify `access.expiration` field to set the
* expiration time for the authorization. By default the authorization
* is valid for 1 year and gives access to all capabilities on the space
* that are needed to use the space.
*
* @param {Model} space
* @param {object} options
* @param {API.Principal} options.agent
* @param {API.Access} [options.access]
* @param {API.UTCUnixTimestamp} [options.expiration]
*/
export const createAuthorization = async (
{ signer, name },
{
agent,
access = Access.spaceAccess,
expiration = UCAN.now() + SESSION_LIFETIME,
}
) => {
return await delegate({
issuer: signer,
audience: agent,
capabilities: toCapabilities({
[signer.did()]: access,
}),
...(expiration ? { expiration } : {}),
facts: [{ space: { name } }],
})
}
/**
* @param {Record<API.Resource, API.Access>} allow
* @returns {API.Capabilities}
*/
const toCapabilities = (allow) => {
const capabilities = []
for (const [subject, access] of Object.entries(allow)) {
const entries = /** @type {[API.Ability, API.Unit][]} */ (
Object.entries(access)
)
for (const [can, details] of entries) {
if (details) {
capabilities.push({ can, with: subject })
}
}
}
return /** @type {API.Capabilities} */ (capabilities)
}
/**
* Represents an owned space, meaning a space for which we have a private key
* and consequently have full authority over.
*/
export class OwnedSpace {
/**
* @param {Model} model
*/
constructor(model) {
this.model = model
}
get signer() {
return this.model.signer
}
get name() {
return this.model.name
}
did() {
return this.signer.did()
}
/**
* Creates a renamed version of this space.
*
* @param {string} name
*/
withName(name) {
return new OwnedSpace({ signer: this.signer, name })
}
/**
* Saves account in the agent store so it can be accessed across sessions.
*
* @param {object} input
* @param {API.Agent} [input.agent]
* @returns {Promise<API.Result<API.Unit, Error>>}
*/
async save({ agent = this.model.agent } = {}) {
if (!agent) {
return fail('Please provide an agent to save the space into')
}
const proof = await createAuthorization(this, { agent })
await agent.importSpaceFromDelegation(proof)
await agent.setCurrentSpace(this.did())
return { ok: {} }
}
/**
* @param {Authorization} authorization
* @param {object} options
* @param {API.Agent} [options.agent]
*/
provision({ proofs }, { agent = this.model.agent } = {}) {
if (!agent) {
return fail('Please provide an agent to save the space into')
}
return provision(this, { proofs, agent })
}
/**
* Creates a (UCAN) delegation that gives full access to the space to the
* specified `account`. At the moment we only allow `did:mailto` principal
* to be used as an `account`.
*
* @param {API.AccountDID} account
*/
async createRecovery(account) {
return createRecovery(this, account)
}
/**
* Creates (UCAN) delegation that gives specified `agent` an access to
* specified ability (passed as `access.can` field) on the this space.
* Optionally, you can specify `access.expiration` field to set the
*
* @param {API.Principal} agent
* @param {object} [input]
* @param {API.Access} [input.access]
* @param {API.UCAN.UTCUnixTimestamp} [input.expiration]
*/
createAuthorization(agent, input) {
return createAuthorization(this, { ...input, agent })
}
/**
* Derives BIP39 mnemonic that can be used to recover the space.
*
* @returns {string}
*/
toMnemonic() {
return toMnemonic(this)
}
}
const SpaceDID = Schema.did({ method: 'key' })
/**
* Creates a (shared) space from given delegation.
*
* @param {API.Delegation} delegation
*/
export const fromDelegation = (delegation) => {
const result = SpaceDID.read(delegation.capabilities[0].with)
if (result.error) {
throw Object.assign(
new Error(
`Invalid delegation, expected capabilities[0].with to be DID, ${result.error}`
),
{
cause: result.error,
}
)
}
/** @type {{name?:string}} */
const meta = delegation.facts[0]?.space ?? {}
return new SharedSpace({ id: result.ok, delegation, meta })
}
/**
* @typedef {object} Authorization
* @property {API.Delegation[]} proofs
*
* @typedef {object} Space
* @property {() => API.SpaceDID} did
*/
/**
* @param {Space} space
* @param {object} options
* @param {API.Delegation[]} options.proofs
* @param {API.Agent} options.agent
*/
export const provision = async (space, { proofs, agent }) => {
const [capability] = proofs[0].capabilities
const { ok: account, error: reason } = Provider.AccountDID.read(
capability.with
)
if (reason) {
return error(reason)
}
return await Provider.add(agent, {
consumer: space.did(),
account,
proofs,
})
}
/**
* Represents a shared space, meaning a space for which we have a delegation
* and consequently have limited authority over.
*/
export class SharedSpace {
/**
* @typedef {object} SharedSpaceModel
* @property {API.SpaceDID} id
* @property {API.Delegation} delegation
* @property {{name?:string}} meta
* @property {API.Agent} [agent]
*
* @param {SharedSpaceModel} model
*/
constructor(model) {
this.model = model
}
get delegation() {
return this.model.delegation
}
get meta() {
return this.model.meta
}
get name() {
return this.meta.name ?? ''
}
did() {
return this.model.id
}
/**
* @param {string} name
*/
withName(name) {
return new SharedSpace({
...this.model,
meta: { ...this.meta, name },
})
}
}