-
Notifications
You must be signed in to change notification settings - Fork 127
/
model-instance-document-handler.ts
440 lines (397 loc) · 14.2 KB
/
model-instance-document-handler.ts
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import jsonpatch from 'fast-json-patch'
import {
ModelInstanceDocument,
ModelInstanceDocumentMetadata,
ModelInstanceDocumentStateMetadata,
validateContentLength,
} from '@ceramicnetwork/stream-model-instance'
import {
AnchorStatus,
CommitData,
EventType,
SignatureStatus,
StreamConstructor,
StreamHandler,
StreamReader,
StreamReaderWriter,
StreamState,
StreamUtils,
UnreachableCaseError,
} from '@ceramicnetwork/common'
import { StreamID } from '@ceramicnetwork/streamid'
import { SchemaValidation } from './schema-utils.js'
import { Model, ModelDefinitionV2 } from '@ceramicnetwork/stream-model'
import { applyAnchorCommit, SignatureUtils } from '@ceramicnetwork/stream-handler-common'
import { toString } from 'uint8arrays'
// Hardcoding the model streamtype id to avoid introducing a dependency on the stream-model package
const MODEL_STREAM_TYPE_ID = 2
type Payload = {
data: JsonPatchOperation[]
}
type JsonPatchOperation = {
op: string
path: string
value?: any
from?: string
}
interface ModelInstanceDocumentHeader extends ModelInstanceDocumentMetadata {
unique?: Uint8Array
}
/**
* ModelInstanceDocument stream handler implementation
*/
export class ModelInstanceDocumentHandler implements StreamHandler<ModelInstanceDocument> {
private readonly _schemaValidator: SchemaValidation
constructor() {
this._schemaValidator = new SchemaValidation()
}
get type(): number {
return ModelInstanceDocument.STREAM_TYPE_ID
}
get name(): string {
return ModelInstanceDocument.STREAM_TYPE_NAME
}
get stream_constructor(): StreamConstructor<ModelInstanceDocument> {
return ModelInstanceDocument
}
/**
* Applies commit (genesis|signed|anchor)
* @param commitData - Commit (with JWS envelope or anchor proof, if available and extracted before application)
* @param context - Interface to read and write data to ceramic network
* @param state - Document state
*/
async applyCommit(
commitData: CommitData,
context: StreamReaderWriter,
state?: StreamState<ModelInstanceDocumentStateMetadata>
): Promise<StreamState> {
if (state == null) {
// apply genesis
return this._applyGenesis(commitData, context)
}
if (StreamUtils.isAnchorCommitData(commitData)) {
return this._applyAnchor(commitData, state)
}
return this._applySigned(commitData, state, context)
}
/**
* Applies genesis commit
* @param commitData - Genesis commit
* @param context - Ceramic context
* @private
*/
async _applyGenesis(commitData: CommitData, context: StreamReaderWriter): Promise<StreamState> {
const payload = commitData.commit
const { controllers, model, context: ctx, unique } = payload.header
const controller = controllers[0]
const modelStreamID = StreamID.fromBytes(model)
const streamId = new StreamID(ModelInstanceDocument.STREAM_TYPE_ID, commitData.cid)
const metadata: ModelInstanceDocumentStateMetadata = {
controllers: [controller],
model: modelStreamID,
unique,
}
if (ctx) {
metadata.context = StreamID.fromBytes(ctx)
}
if (!(payload.header.controllers && payload.header.controllers.length === 1)) {
throw new Error('Exactly one controller must be specified')
}
if (!StreamUtils.validDIDString(payload.header.controllers[0])) {
throw new Error(
`Attempting to create a ModelInstanceDocument with an invalid DID string: ${payload.header.controllers[0]}`
)
}
if (modelStreamID.type != MODEL_STREAM_TYPE_ID) {
throw new Error(`Model for ModelInstanceDocument must refer to a StreamID of a Model stream`)
}
const isSigned = StreamUtils.isSignedCommitData(commitData)
if (isSigned) {
await SignatureUtils.verifyCommitSignature(
commitData,
context.signer,
controller,
modelStreamID,
streamId
)
} else if (payload.data) {
throw Error('ModelInstanceDocument genesis commit with content must be signed')
}
const modelStream = await context.loadStream<Model>(metadata.model)
this._validateModel(modelStream)
await this._validateContent(context, modelStream, payload.data, true)
await this._validateHeader(modelStream, payload.header)
return {
type: ModelInstanceDocument.STREAM_TYPE_ID,
content: payload.data || null,
metadata,
signature: SignatureStatus.SIGNED,
anchorStatus: AnchorStatus.NOT_REQUESTED,
log: [StreamUtils.commitDataToLogEntry(commitData, EventType.INIT)],
}
}
/**
* Applies signed commit
* @param commitData - Signed commit
* @param state - Document state
* @param context - Interface to read and write to ceramic network
* @private
*/
async _applySigned(
commitData: CommitData,
state: StreamState<ModelInstanceDocumentStateMetadata>,
context: StreamReaderWriter
): Promise<StreamState> {
const deterministicTypes = ['set', 'single']
// Retrieve the payload
const payload = commitData.commit
StreamUtils.assertCommitLinksToState(state, payload)
// Verify the signature
const metadata = state.metadata
const controller = metadata.controllers[0]
const model = metadata.model
const streamId = StreamUtils.streamIdFromState(state)
await SignatureUtils.verifyCommitSignature(
commitData,
context.signer,
controller,
model,
streamId
)
if (payload.header) {
const { shouldIndex, ...others } = payload.header
const otherKeys = Object.keys(others)
if (otherKeys.length) {
throw new Error(
`Updating metadata for ModelInstanceDocument Streams is not allowed. Tried to change metadata for Stream ${streamId} from ${JSON.stringify(
state.metadata
)} to ${JSON.stringify(payload.header)}\``
)
}
if (shouldIndex != null) {
state.metadata.shouldIndex = shouldIndex
}
}
const oldContent = state.content ?? {}
const newContent = jsonpatch.applyPatch(oldContent, payload.data).newDocument
const modelStream = await context.loadStream<Model>(metadata.model)
const isDetType = deterministicTypes.includes(modelStream.content.accountRelation.type)
const isFirstDataCommit = !state.log.some((c) => c.type === EventType.DATA)
await this._validateContent(
context,
modelStream,
newContent,
false,
payload,
isDetType && isFirstDataCommit
)
await this._validateUnique(
modelStream,
metadata as unknown as ModelInstanceDocumentMetadata,
newContent
)
state.signature = SignatureStatus.SIGNED
state.anchorStatus = AnchorStatus.NOT_REQUESTED
state.content = newContent
state.log.push(StreamUtils.commitDataToLogEntry(commitData, EventType.DATA))
return state
}
/**
* Applies anchor commit
* @param commitData - Anchor commit
* @param state - Document state
* @private
*/
async _applyAnchor(commitData: CommitData, state: StreamState): Promise<StreamState> {
return applyAnchorCommit(commitData, state)
}
/**
* Validates the ModelInstanceDocument can be created for the given model
* @param model - The model that this ModelInstanceDocument belongs to
* @private
*/
_validateModel(model: Model): void {
if (model.content.version !== '1.0' && model.content.interface) {
throw new Error(
`ModelInstanceDocument Streams cannot be created on interface Models. Use a different model than ${model.id.toString()} to create the ModelInstanceDocument.`
)
}
}
/**
* Validates content against the schema of the model stream with given stream id
* @param ceramic - Interface for reading streams from ceramic network
* @param model - The model that this ModelInstanceDocument belongs to
* @param content - Content to validate
* @param genesis - Whether the commit being applied is a genesis commit
* @param skipImmutableFieldsCheck - Whether the incoming commit is the first data commit for a model with deterministic creation (Optional)
* @private
*/
async _validateContent(
ceramic: StreamReader,
model: Model,
content: any,
genesis: boolean,
payload?: Payload,
skipImmutableFieldsCheck?: boolean
): Promise<void> {
if (
genesis &&
(model.content.accountRelation.type === 'single' ||
model.content.accountRelation.type === 'set')
) {
if (content) {
throw new Error(
`Deterministic genesis commits for ModelInstanceDocuments must not have content`
)
}
return
}
validateContentLength(content)
this._schemaValidator.validateSchema(content, model.content.schema, model.commitId.toString())
// Now validate the relations
await this._validateRelationsContent(ceramic, model, content)
if (!genesis && payload && !skipImmutableFieldsCheck) {
await this._validateLockedFieldsUpdate(model, payload)
}
}
async _validateRelationsContent(ceramic: StreamReader, model: Model, content: any) {
if (!model.content.relations) {
return
}
for (const [fieldName, relationDefinition] of Object.entries(model.content.relations)) {
const relationType = relationDefinition.type
switch (relationType) {
case 'account':
continue
case 'document': {
// Ignore validation if the target field is empty
if (content[fieldName] == null) {
continue
}
// Validate StreamID value
let midStreamId
try {
midStreamId = StreamID.fromString(content[fieldName])
} catch (err: any) {
throw new Error(
`Error while parsing relation from field ${fieldName}: Invalid StreamID: ${err.toString()}`
)
}
// Ensure linked stream can be loaded and is a MID
const linkedMid = await ModelInstanceDocument.load(ceramic, midStreamId)
// Check for expected model the MID must use
const expectedModelStreamId = relationDefinition.model
if (expectedModelStreamId == null) {
continue
}
const foundModelStreamId = linkedMid.metadata.model.toString()
if (foundModelStreamId === expectedModelStreamId) {
// Exact model used
continue
}
// Other model used, check if it implements the expected interface
const linkedModel = await Model.load(ceramic, foundModelStreamId)
if (
linkedModel.content.version !== '1.0' &&
linkedModel.content.implements.includes(expectedModelStreamId)
) {
continue
}
throw new Error(
`Relation on field ${fieldName} points to Stream ${midStreamId.toString()}, which belongs to Model ${foundModelStreamId}, but this Stream's Model (${model.id.toString()}) specifies that this relation must be to a Stream in the Model ${expectedModelStreamId}`
)
}
default:
throw new UnreachableCaseError(relationType, 'Unknown relation type')
}
}
}
/**
* Validates the ModelInstanceDocument header against the Model definition.
* @param model - The model that this ModelInstanceDocument belongs to
* @param header - the header to validate
*/
async _validateHeader(model: Model, header: ModelInstanceDocumentHeader): Promise<void> {
const relationType = model.content.accountRelation.type
switch (relationType) {
case 'single':
if (header.unique) {
throw new Error(
`ModelInstanceDocuments for models with SINGLE accountRelations must be created deterministically`
)
}
break
case 'set':
if (!header.unique) {
throw new Error(
`ModelInstanceDocuments for models with SET accountRelations must be created with a unique field containing data from the fields providing the set semantics`
)
}
break
case 'list':
if (!header.unique) {
throw new Error(
`ModelInstanceDocuments for models with LIST accountRelations must be created with a unique field`
)
}
break
case 'none':
break
default:
throw new UnreachableCaseError(
relationType,
`Unsupported account relation ${relationType} found in Model ${model.content.name}`
)
}
}
/**
* Helper function to validate if immutable fields are being mutated
*/
async _validateLockedFieldsUpdate(model: Model, payload: Payload): Promise<void> {
if (!ModelDefinitionV2.is(model.content)) return
const immutableFields = model.content.immutableFields
const hasImmutableFields = immutableFields && immutableFields.length > 0
if (!hasImmutableFields) return
for (const lockedField of immutableFields) {
const mutated = payload.data.some(
(entry) => entry.path.slice(1).split('/').shift() === lockedField
)
if (mutated) {
throw new Error(`Immutable field "${lockedField}" cannot be updated`)
}
}
}
/*
* Validates the ModelInstanceDocument unique constraints against the Model definition.
* @param model - model that this ModelInstanceDocument belongs to
* @param metadata - ModelInstanceDocument metadata to validate
* @param content - ModelInstanceDocument content to validate
*/
async _validateUnique(
model: Model,
metadata: ModelInstanceDocumentMetadata,
content: Record<string, unknown> | null
): Promise<void> {
// Unique field validation only applies to the SET account relation
if (model.content.accountRelation.type !== 'set') {
return
}
if (metadata.unique == null) {
throw new Error('Missing unique metadata value')
}
if (content == null) {
throw new Error('Missing content')
}
const unique = model.content.accountRelation.fields
.map((field) => {
const value = content[field]
return value ? String(value) : ''
})
.join('|')
if (unique !== toString(metadata.unique)) {
throw new Error(
'Unique content fields value does not match metadata. If you are trying to change the value of these fields, this is causing this error: these fields values are not mutable.'
)
}
}
}