-
Notifications
You must be signed in to change notification settings - Fork 4
/
schema.js
334 lines (306 loc) · 8.15 KB
/
schema.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
333
334
import * as UCAN from "./ucan.js"
import * as UTF8 from "./utf8.js"
import * as Link from "multiformats/link"
import { identity } from "multiformats/hashes/identity"
import * as DID from "./did.js"
import * as raw from "multiformats/codecs/raw"
import * as Signature from "./signature.js"
/**
* @template {UCAN.Capabilities} C
* @param {Record<string, unknown>|UCAN.Payload<C>} data
* @returns {UCAN.Payload<C>}
*/
export const readPayload = data =>
readPayloadWith(data, {
readPrincipal,
readProof,
})
/**
* @template {UCAN.Capabilities} C
* @param {Record<string, unknown>|UCAN.Payload<C>} data
* @returns {UCAN.Payload<C>}
*/
export const readJWTPayload = data =>
readPayloadWith(data, {
readPrincipal: readStringPrincipal,
readProof: readStringProof,
})
/**
*
* @template {UCAN.Capabilities} C
* @param {Record<string, unknown>|UCAN.Payload<C>} data
* @param {object} readers
* @param {(source:unknown, context:string) => UCAN.Principal} readers.readPrincipal
* @param {(source:unknown, context:string) => UCAN.Link} readers.readProof
* @returns {UCAN.Payload<C>}
*/
const readPayloadWith = (data, { readPrincipal, readProof }) => ({
iss: readPrincipal(data.iss, "iss"),
aud: readPrincipal(data.aud, "aud"),
att: readCapabilities(data.att, "att"),
prf: readOptionalArray(data.prf, readProof, "prf") || [],
exp: readNullable(data.exp === Infinity ? null : data.exp, readInt, "exp"),
nbf: readOptional(data.nbf, readInt, "nbf"),
fct: readOptionalArray(data.fct, readFact, "fct") || [],
nnc: readOptional(data.nnc, readString, "nnc"),
})
/**
* @template {unknown} T
* @template {number} A
* @param {UCAN.ByteView<UCAN.Signature<T, A>>|unknown} source
*/
export const readSignature = source => {
if (source instanceof Uint8Array) {
return Signature.decode(source)
} else {
throw new TypeError(
`Can only decode Uint8Array into a Signature, instead got ${JSON.stringify(
source
)}`
)
}
}
/**
* @param {unknown} input
* @param {string} name
* @returns {number}
*/
export const readInt = (input, name) =>
Number.isInteger(input)
? /** @type {number} */ (input)
: ParseError.throw(
`Expected ${name} to be integer, instead got ${JSON.stringify(input)}`
)
/**
* @param {unknown} input
* @param {string} context
*/
export const readCapability = (input, context) =>
readStruct(input, asCapability, context)
/**
* @template {UCAN.Capabilities} C
* @param {unknown|C} input
* @param {string} context
* @returns {C}
*/
export const readCapabilities = (input, context) =>
/** @type {C} */ (readArray(input, readCapability, context))
/**
* @template {UCAN.Capability} C
* @param {object & {can?:unknown, with?:unknown}|C} input
* @returns {C}
*/
export const asCapability = input =>
/** @type {C} */ ({
...input,
can: readAbility(input.can),
with: readResource(input.with),
})
/**
* @param {unknown} input
*/
const readAbility = input =>
typeof input !== "string"
? ParseError.throw(
`Capability has invalid 'can: ${JSON.stringify(
input
)}', value must be a string`
)
: input.slice(1, -1).includes("/")
? /** @type {UCAN.Ability} */ (input.toLocaleLowerCase())
: input === "*"
? input
: ParseError.throw(
`Capability has invalid 'can: "${input}"', value must have at least one path segment`
)
/**
* @param {unknown} input
*/
const readResource = input =>
typeof input !== "string"
? ParseError.throw(
`Capability has invalid 'with: ${JSON.stringify(
input
)}', value must be a string`
)
: parseURL(input) ||
ParseError.throw(
`Capability has invalid 'with: "${input}"', value must be a valid URI string`
)
/**
* @param {string} input
*/
const parseURL = input => {
try {
new URL(input)
return input
} catch (_) {
return null
}
}
/**
* @template T
* @param {unknown} input
* @param {(input:unknown, context:string) => T} read
* @param {string} context
* @returns {T[]}
*/
export const readArray = (input, read, context) =>
Array.isArray(input)
? input.map((element, n) => read(element, `${context}[${n}]`))
: ParseError.throw(`${context} must be an array`)
/**
* @template T
* @param {unknown} input
* @param {(input:unknown, context: string) => T} reader
* @param {string} context
* @returns {T[]|undefined}
*/
export const readOptionalArray = (input, reader, context) =>
input === undefined ? input : readArray(input, reader, context)
/**
* @template T
* @param {unknown} input
* @param {(input:object) => T} reader
* @param {string} context
* @returns {T}
*/
export const readStruct = (input, reader, context) =>
input != null && typeof input === "object"
? reader(input)
: ParseError.throw(
`${context} must be of type object, instead got ${input}`
)
/**
* @param {unknown} input
* @param {string} context
* @returns {UCAN.Fact}
*/
export const readFact = (input, context) => readStruct(input, Object, context)
/**
* @param {unknown} source
* @param {string} context
* @returns {UCAN.Link}
*/
export const readProof = (source, context) =>
Link.isLink(source)
? /** @type {UCAN.Link} */ (source)
: fail(
`Expected ${context} to be IPLD link, instead got ${JSON.stringify(
source
)}`
)
/**
* @param {unknown} source
* @param {string} context
* @returns {UCAN.Link}
*/
export const readStringProof = (source, context) =>
parseProof(readString(source, context))
/**
* @param {string} source
* @returns {UCAN.Link}
*/
const parseProof = source => {
// First we attempt to read proof as CID, if we fail fallback to reading it as
// an inline proof.
try {
return Link.parse(source)
} catch (error) {
return Link.create(raw.code, identity.digest(UTF8.encode(source)))
}
}
/**
* @param {unknown} input
* @param {string} context
*/
export const readPrincipal = (input, context) =>
DID.decode(readBytes(input, context))
/**
* @param {unknown} source
* @param {string} context
*/
export const readStringPrincipal = (source, context) =>
DID.parse(readString(source, context))
/**
* @template T
* @param {unknown} source
* @param {(source:unknown, context:string) => T} read
* @param {string} [context]
* @returns {T|undefined}
*/
export const readOptional = (source, read, context = "Field") =>
source !== undefined ? read(source, context) : undefined
/**
* @template T
* @param {unknown} source
* @param {(source:unknown, context:string) => T} read
* @param {string} context
* @returns {T|null}
*/
export const readNullable = (source, read, context) =>
source === null ? null : read(source, context)
/**
* @param {unknown} source
* @param {string} [context]
* @returns {string}
*/
export const readString = (source, context = "Field") =>
typeof source === "string"
? source
: fail(`${context} has invalid value ${source}`)
/**
*
* @param {unknown} source
* @param {string} context
* @returns {Uint8Array}
*/
export const readBytes = (source, context) =>
source instanceof Uint8Array
? source
: fail(
`Expected ${context} to be Uint8Array, instead got ${JSON.stringify(
source
)}`
)
/**
* @param {unknown} input
* @param {string} context
* @returns {UCAN.Version}
*/
export const readVersion = (input, context) =>
/\d+\.\d+\.\d+/.test(/** @type {string} */ (input))
? /** @type {UCAN.Version} */ (input)
: ParseError.throw(`Invalid version '${context}: ${JSON.stringify(input)}'`)
/**
* @template {string|number|boolean|null} T
* @param {unknown} input
* @param {T} literal
* @param {string} context
* @returns {T}
*/
export const readLiteral = (input, literal, context) =>
input === literal
? literal
: ParseError.throw(
`Expected ${context} to be a ${JSON.stringify(
literal
)} instead got ${JSON.stringify(input)}`
)
export class ParseError extends TypeError {
get name() {
return "ParseError"
}
/**
* @param {string} message
* @returns {never}
*/
static throw(message) {
throw new this(message)
}
}
/**
* @param {string} reason
*/
export const fail = reason => ParseError.throw(reason)
export { fail as throw }