-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassert.js
132 lines (124 loc) · 3.9 KB
/
assert.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
import { capability, URI, Schema, ok } from '@ucanto/server'
import { and, equal, equalLinkOrDigestContent, equalWith } from './utils.js'
const linkOrDigest = () => Schema.link().or(Schema.struct({ digest: Schema.bytes() }))
export const assert = capability({
can: 'assert/*',
with: URI.match({ protocol: 'did:' })
})
/**
* Claims that a CID is available at a URL.
*/
export const location = capability({
can: 'assert/location',
with: URI.match({ protocol: 'did:' }),
nb: Schema.struct({
/** CAR CID */
content: linkOrDigest(),
location: Schema.array(URI),
range: Schema.struct({
offset: Schema.integer(),
length: Schema.integer().optional()
}).optional(),
space: Schema.didBytes().optional()
}),
derives: (claimed, delegated) => (
and(equalWith(claimed, delegated)) ||
and(equalLinkOrDigestContent(claimed, delegated)) ||
and(equal(claimed.nb.location, delegated.nb.location, 'location')) ||
and(equal(claimed.nb.range?.offset, delegated.nb.range?.offset, 'offset')) ||
and(equal(claimed.nb.range?.length, delegated.nb.range?.length, 'length')) ||
and(equal(claimed.nb.space, delegated.nb.space, 'space')) ||
ok({})
)
})
/**
* Claims that a CID includes the contents claimed in another CID.
*/
export const inclusion = capability({
can: 'assert/inclusion',
with: URI.match({ protocol: 'did:' }),
nb: Schema.struct({
/** CAR CID */
content: linkOrDigest(),
/** CARv2 index CID */
includes: Schema.link({ version: 1 }),
proof: Schema.link({ version: 1 }).optional()
})
})
/**
* Claims that a content graph can be found in blob(s) that are identified and
* indexed in the given index CID.
*/
export const index = capability({
can: 'assert/index',
with: URI.match({ protocol: 'did:' }),
nb: Schema.struct({
/** DAG root CID */
content: linkOrDigest(),
/**
* Link to a Content Archive that contains the index.
* e.g. `index/sharded/[email protected]`
* @see https://github.com/storacha/specs/blob/main/w3-index.md
*/
index: Schema.link({ version: 1 })
}),
derives: (claimed, delegated) => (
and(equalWith(claimed, delegated)) ||
and(equal(claimed.nb.content, delegated.nb.content, 'content')) ||
and(equal(claimed.nb.index, delegated.nb.index, 'index')) ||
ok({})
)
})
/**
* Claims that a CID's graph can be read from the blocks found in parts.
*/
export const partition = capability({
can: 'assert/partition',
with: URI.match({ protocol: 'did:' }),
nb: Schema.struct({
/** Content root CID */
content: linkOrDigest(),
/** CIDs CID */
blocks: Schema.link({ version: 1 }).optional(),
parts: Schema.array(Schema.link({ version: 1 }))
})
})
/**
* Claims that a CID links to other CIDs.
*/
export const relation = capability({
can: 'assert/relation',
with: URI.match({ protocol: 'did:' }),
nb: Schema.struct({
content: linkOrDigest(),
/** CIDs this content links to directly. */
children: Schema.array(Schema.link()),
/** Parts this content and it's children can be read from. */
parts: Schema.array(Schema.struct({
content: Schema.link({ version: 1 }),
/** CID of contents (CARv2 index) included in this part. */
includes: Schema.struct({
content: Schema.link({ version: 1 }),
/** CIDs of parts this index may be found in. */
parts: Schema.array(Schema.link({ version: 1 })).optional()
}).optional()
}))
})
})
/**
* Claim data is referred to by another CID and/or multihash. e.g CAR CID & CommP CID
*/
export const equals = capability({
can: 'assert/equals',
with: URI.match({ protocol: 'did:' }),
nb: Schema.struct({
content: linkOrDigest(),
equals: Schema.link()
}),
derives: (claimed, delegated) => (
and(equalWith(claimed, delegated)) ||
and(equalLinkOrDigestContent(claimed, delegated)) ||
and(equal(claimed.nb.equals, delegated.nb.equals, 'equals')) ||
ok({})
)
})