-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
1199 lines (1124 loc) · 43.4 KB
/
utils.ts
File metadata and controls
1199 lines (1124 loc) · 43.4 KB
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type { Key } from '@chelonia/crypto'
import { deserializeKey, serializeKey, sign, verifySignature } from '@chelonia/crypto'
import sbp from '@sbp/sbp'
import { Buffer } from 'buffer'
import { has, omit } from 'turtledash'
import type {
ProtoSPOpActionUnencrypted,
SPKey,
SPKeyPurpose,
SPKeyUpdate,
SPOpActionUnencrypted,
SPOpAtomic,
SPOpKeyAdd,
SPOpKeyDel,
SPOpKeyUpdate,
SPOpRaw,
SPOpValue
} from './SPMessage.js'
import { SPMessage } from './SPMessage.js'
import { Secret } from './Secret.js'
import { INVITE_STATUS } from './constants.js'
import type { EncryptedData } from './encryptedData.js'
import {
ChelErrorForkedChain,
ChelErrorResourceGone,
ChelErrorUnexpectedHttpResponseCode,
ChelErrorWarning
} from './errors.js'
import { CONTRACT_IS_PENDING_KEY_REQUESTS } from './events.js'
import { b64ToStr } from './functions.js'
import type { SignedData } from './signedData.js'
import { isSignedData } from './signedData.js'
import {
ChelContractKey,
ChelContractState,
ChelRootState,
CheloniaConfig,
CheloniaContext,
JSONType
} from './types.js'
const MAX_EVENTS_AFTER = Number.parseInt(process.env.MAX_EVENTS_AFTER || '', 10) || Infinity
const copiedExistingData = Symbol('copiedExistingData')
export const findKeyIdByName = (
state: ChelContractState,
name: string
): string | null | undefined =>
state._vm?.authorizedKeys &&
Object.values(state._vm.authorizedKeys).find((k) => k.name === name && k._notAfterHeight == null)
?.id
export const findForeignKeysByContractID = (
state: ChelContractState,
contractID: string
): string[] | undefined =>
state._vm?.authorizedKeys &&
Object.values(state._vm.authorizedKeys)
.filter((k) => k._notAfterHeight == null && k.foreignKey?.includes(contractID))
.map((k) => k.id)
export const findRevokedKeyIdsByName = (state: ChelContractState, name: string): string[] =>
state._vm?.authorizedKeys &&
Object.values(state._vm.authorizedKeys || {})
.filter((k) => k.name === name && k._notAfterHeight != null)
.map((k) => k.id)
export const findSuitableSecretKeyId = (
state: ChelContractState,
permissions: '*' | string[],
purposes: SPKeyPurpose[],
ringLevel?: number,
allowedActions?: '*' | string[]
): string | null | undefined => {
return (
state._vm?.authorizedKeys &&
Object.values(state._vm.authorizedKeys)
.filter((k) => {
return (
k._notAfterHeight == null &&
k.ringLevel <= (ringLevel ?? Number.POSITIVE_INFINITY) &&
sbp('chelonia/haveSecretKey', k.id) &&
(Array.isArray(permissions)
? permissions.reduce(
(acc, permission) =>
acc && (k.permissions === '*' || k.permissions.includes(permission)),
true
)
: permissions === k.permissions) &&
purposes.reduce((acc, purpose) => acc && k.purpose.includes(purpose), true) &&
(Array.isArray(allowedActions)
? allowedActions.reduce(
(acc, action) =>
acc && (k.allowedActions === '*' || !!k.allowedActions?.includes(action)),
true
)
: allowedActions
? allowedActions === k.allowedActions
: true)
)
})
.sort((a, b) => b.ringLevel - a.ringLevel)[0]?.id
)
}
export const findContractIDByForeignKeyId = (
state: ChelContractState,
keyId: string
): string | null | undefined => {
let fk: string | undefined
if (!keyId || !(fk = state?._vm?.authorizedKeys?.[keyId]?.foreignKey)) return
try {
const fkUrl = new URL(fk)
return fkUrl.pathname
} catch {}
}
// TODO: Resolve inviteKey being added (doesn't have krs permission)
export const findSuitablePublicKeyIds = (
state: ChelContractState,
permissions: '*' | string[],
purposes: SPKeyPurpose[],
ringLevel?: number
): string[] | null | undefined => {
return (
state._vm?.authorizedKeys &&
Object.values(state._vm.authorizedKeys)
.filter(
(k) =>
k._notAfterHeight == null &&
k.ringLevel <= (ringLevel ?? Number.POSITIVE_INFINITY) &&
(Array.isArray(permissions)
? permissions.reduce(
(acc, permission) =>
acc && (k.permissions === '*' || k.permissions.includes(permission)),
true
)
: permissions === k.permissions) &&
purposes.reduce((acc, purpose) => acc && k.purpose.includes(purpose), true)
)
.sort((a, b) => b.ringLevel - a.ringLevel)
.map((k) => k.id)
)
}
const validateActionPermissions = (
msg: SPMessage,
signingKey: SPKey | ChelContractKey,
state: { _vm: { authorizedKeys: ChelContractState['_vm']['authorizedKeys'] } },
opT: string,
opV: SPOpActionUnencrypted
) => {
const data = isSignedData(opV)
? (opV.valueOf() as ProtoSPOpActionUnencrypted)
: (opV as ProtoSPOpActionUnencrypted)
if (
signingKey.allowedActions !== '*' &&
(!Array.isArray(signingKey.allowedActions) || !signingKey.allowedActions.includes(data.action))
) {
logEvtError(msg, `Signing key ${signingKey.id} is not allowed for action ${data.action}`)
return false
}
if (isSignedData(opV)) {
const s = opV as SignedData<void>
const innerSigningKey = state._vm?.authorizedKeys?.[s.signingKeyId]
// For outgoing messages, we may be using an inner signing key that isn't
// available for us to see. In this case, we ignore the missing key.
// For incoming messages, we must check permissions and a missing
// key means no permissions.
if (!innerSigningKey && msg._direction === 'outgoing') return true
if (
!innerSigningKey ||
!Array.isArray(innerSigningKey.purpose) ||
!innerSigningKey.purpose.includes('sig') ||
(innerSigningKey.permissions !== '*' &&
(!Array.isArray(innerSigningKey.permissions) ||
!innerSigningKey.permissions.includes(opT + '#inner')))
) {
logEvtError(msg, `Signing key ${s.signingKeyId} is missing permissions for operation ${opT}`)
return false
}
if (
innerSigningKey.allowedActions !== '*' &&
(!Array.isArray(innerSigningKey.allowedActions) ||
!innerSigningKey.allowedActions.includes(data.action + '#inner'))
) {
logEvtError(
msg,
`Signing key ${innerSigningKey.id} is not allowed for action ${data.action}`
)
return false
}
}
return true
}
export const validateKeyPermissions = (
msg: SPMessage,
config: CheloniaConfig,
state: { _vm: { authorizedKeys: ChelContractState['_vm']['authorizedKeys'] } },
signingKeyId: string,
opT: string,
opV: SPOpValue
): boolean => {
const signingKey = state._vm?.authorizedKeys?.[signingKeyId]
if (
!signingKey ||
!Array.isArray(signingKey.purpose) ||
!signingKey.purpose.includes('sig') ||
(signingKey.permissions !== '*' &&
(!Array.isArray(signingKey.permissions) || (
!signingKey.permissions.includes(opT) &&
!(
opT === SPMessage.OP_KEY_DEL &&
signingKey.permissions.includes(`${opT}#self`)
)
)))
) {
logEvtError(msg, `Signing key ${signingKeyId} is missing permissions for operation ${opT}`)
return false
}
if (
opT === SPMessage.OP_ACTION_UNENCRYPTED &&
!validateActionPermissions(msg, signingKey, state, opT, opV as SPOpActionUnencrypted)
) {
return false
}
if (
!config.skipActionProcessing &&
opT === SPMessage.OP_ACTION_ENCRYPTED &&
!validateActionPermissions(msg, signingKey, state, opT, opV.valueOf() as SPOpActionUnencrypted)
) {
return false
}
return true
}
export const validateKeyAddPermissions = function (
this: CheloniaContext,
contractID: string,
signingKey: ChelContractKey,
state: ChelContractState,
v: (ChelContractKey | SPKey | EncryptedData<SPKey>)[],
skipPrivateCheck?: boolean
) {
const signingKeyPermissions = Array.isArray(signingKey.permissions)
? new Set(signingKey.permissions)
: signingKey.permissions
const signingKeyAllowedActions = Array.isArray(signingKey.allowedActions)
? new Set(signingKey.allowedActions)
: signingKey.allowedActions
if (!state._vm?.authorizedKeys?.[signingKey.id]) {
throw new Error(
'Singing key for OP_KEY_ADD or OP_KEY_UPDATE must exist in _vm.authorizedKeys. contractID=' +
contractID +
' signingKeyId=' +
signingKey.id
)
}
const localSigningKey = state._vm.authorizedKeys[signingKey.id]
v.forEach((wk) => {
const data = this.config.unwrapMaybeEncryptedData(wk)
if (!data) return
const k = data.data as SPKey
if (!skipPrivateCheck && signingKey._private && !data.encryptionKeyId) {
throw new Error('Signing key is private but it tried adding a public key')
}
if (!Number.isSafeInteger(k.ringLevel) || k.ringLevel < localSigningKey.ringLevel) {
throw new Error(
'Signing key has ringLevel ' +
localSigningKey.ringLevel +
' but attempted to add or update a key with ringLevel ' +
k.ringLevel
)
}
if (signingKeyPermissions !== '*') {
if (
!Array.isArray(k.permissions) ||
k.permissions.some((cv) => {
if (cv === `${SPMessage.OP_KEY_DEL}#self`) {
// Granting `kd#self` requires `kd`
cv = SPMessage.OP_KEY_DEL
}
return !signingKeyPermissions.has(cv)
})
) {
throw new Error(
'Unable to add or update a key with more permissions than the signing key. signingKey permissions: ' +
String(signingKey?.permissions) +
'; key add permissions: ' +
String(k.permissions)
)
}
}
if (signingKeyAllowedActions !== '*' && k.allowedActions) {
if (
!signingKeyAllowedActions ||
!Array.isArray(k.allowedActions) ||
!k.allowedActions.reduce((acc, cv) => acc && signingKeyAllowedActions.has(cv), true)
) {
throw new Error(
'Unable to add or update a key with more allowed actions than the signing key. signingKey allowed actions: ' +
String(signingKey?.allowedActions) +
'; key add allowed actions: ' +
String(k.allowedActions)
)
}
}
})
}
export const validateKeyDelPermissions = function (
this: CheloniaContext,
contractID: string,
signingKey: ChelContractKey,
state: ChelContractState,
v: (string | EncryptedData<string>)[]
) {
if (!state._vm?.authorizedKeys?.[signingKey.id]) {
throw new Error(
'Singing key for OP_KEY_DEL must exist in _vm.authorizedKeys. contractID=' +
contractID +
' signingKeyId=' +
signingKey.id
)
}
const localSigningKey = state._vm.authorizedKeys[signingKey.id]
const selfDeleteOnly = localSigningKey.permissions !== '*' && !localSigningKey.permissions.includes(SPMessage.OP_KEY_DEL)
v.forEach((wid) => {
const data = this.config.unwrapMaybeEncryptedData<string>(wid)
if (!data) return
const id = data.data
const k = state._vm.authorizedKeys[id]
if (!k) {
throw new Error('Nonexisting key ID ' + id)
}
if (signingKey._private) {
throw new Error('Signing key is private')
}
if (!k._private !== !data.encryptionKeyId) {
throw new Error('_private attribute must be preserved')
}
if (!Number.isSafeInteger(k.ringLevel) || k.ringLevel < localSigningKey.ringLevel) {
throw new Error(
'Signing key has ringLevel ' +
localSigningKey.ringLevel +
' but attempted to remove a key with ringLevel ' +
k.ringLevel
)
}
if (selfDeleteOnly) {
if (!k._addedByKeyId || !state._vm.authorizedKeys[k._addedByKeyId]) {
throw new Error('Missing or invalid _addedByKeyId')
}
if (state._vm.authorizedKeys[k._addedByKeyId].name !== localSigningKey.name) {
throw new Error('Key was added by a different key')
}
}
})
}
export const validateKeyUpdatePermissions = function (
this: CheloniaContext,
contractID: string,
signingKey: ChelContractKey,
state: ChelContractState,
v: (SPKeyUpdate | EncryptedData<SPKeyUpdate>)[]
): [ChelContractKey[], Record<string, string>] {
const updatedMap = Object.create(null) as Record<string, string>
const keys = v
.map((wuk): ChelContractKey | undefined => {
const data = this.config.unwrapMaybeEncryptedData(wuk)
if (!data) return undefined
const uk = data.data
const existingKey = state._vm.authorizedKeys[uk.oldKeyId]
if (!existingKey) {
throw new ChelErrorWarning('Missing old key ID ' + uk.oldKeyId)
}
if (!existingKey._private !== !data.encryptionKeyId) {
throw new Error('_private attribute must be preserved')
}
if (uk.name !== existingKey.name) {
throw new Error('Name cannot be updated')
}
if (!uk.id !== !uk.data) {
throw new Error(
'Both or none of the id and data attributes must be provided. Old key ID: ' + uk.oldKeyId
)
}
if (uk.data && existingKey.meta?.private && !uk.meta?.private) {
throw new Error('Missing private key. Old key ID: ' + uk.oldKeyId)
}
if (uk.id && uk.id !== uk.oldKeyId) {
updatedMap[uk.id] = uk.oldKeyId
}
// Discard `_notAfterHeight` and `_notBeforeHeight`, since retaining them
// can cause issues reprocessing messages.
// An example is reprocessing old messages in a chatroom using
// `chelonia/in/processMessage`: cloning `_notAfterHeight` will break key
// rotations, since the new key will have the same expiration value as the
// old key (the new key is supposed to have no expiration height).
const updatedKey = omit(existingKey, [
'_notAfterHeight',
'_notBeforeHeight'
]) as ChelContractKey
// Set the corresponding updated attributes
if (uk.permissions) {
updatedKey.permissions = uk.permissions
}
if (uk.allowedActions) {
updatedKey.allowedActions = uk.allowedActions
}
if (uk.purpose) {
updatedKey.purpose = uk.purpose as SPKeyPurpose[]
}
if (uk.meta) {
updatedKey.meta = uk.meta
} else if (updatedKey.meta) {
Object.defineProperty(updatedKey.meta, copiedExistingData, { value: true })
}
if (uk.id) {
updatedKey.id = uk.id
}
if (uk.data) {
updatedKey.data = uk.data
}
return updatedKey
})
// eslint-disable-next-line no-use-before-define
.filter(Boolean as unknown as (key: unknown) => key is ChelContractKey)
validateKeyAddPermissions.call(this, contractID, signingKey, state, keys, true)
return [keys, updatedMap]
}
export const keyAdditionProcessor = function (
this: CheloniaContext,
_msg: SPMessage,
hash: string,
keys: (ChelContractKey | SPKey | EncryptedData<SPKey>)[],
state: ChelContractState,
contractID: string,
_signingKey: ChelContractKey,
internalSideEffectStack?: (({
state,
message
}: {
state: ChelContractState;
message: SPMessage;
}) => void)[]
) {
const decryptedKeys = []
const keysToPersist: { key: Key; transient: boolean }[] = []
const storeSecretKey = (key: SPKey | ChelContractKey, decryptedKey: string) => {
const decryptedDeserializedKey = deserializeKey(decryptedKey)
const transient = !!key.meta?.private?.transient
sbp(
'chelonia/storeSecretKeys',
new Secret([
{
key: decryptedDeserializedKey,
// We always set this to true because this could be done from
// an outgoing message
transient: true
}
])
)
if (!transient) {
keysToPersist.push({ key: decryptedDeserializedKey, transient })
}
}
for (const wkey of keys) {
const data = this.config.unwrapMaybeEncryptedData(wkey)
if (!data) continue
const key = data.data
let decryptedKey: string | null | undefined
// Does the key have key.meta?.private? If so, attempt to decrypt it
// copiedExistingData refers to key data that have been copied from an
// existing key on OP_KEY_UPDATE. These shouldn't be processed.
if (key.meta?.private?.content && !has(key.meta, copiedExistingData)) {
if (key.id && !sbp('chelonia/haveSecretKey', key.id, !key.meta.private.transient)) {
const decryptedKeyResult = this.config.unwrapMaybeEncryptedData(key.meta.private.content)
// Ignore data that couldn't be decrypted
if (decryptedKeyResult) {
// Data aren't encrypted
if (decryptedKeyResult.encryptionKeyId == null) {
throw new Error(
'Expected encrypted data but got unencrypted data for key with ID: ' + key.id
)
}
decryptedKey = decryptedKeyResult.data
decryptedKeys.push([key.id, decryptedKey])
storeSecretKey(key, decryptedKey)
}
}
}
// Is this a #sak
if (key.name === '#sak') {
if (data.encryptionKeyId) {
throw new Error('#sak may not be encrypted')
}
if (key.permissions && (!Array.isArray(key.permissions) || key.permissions.length !== 0)) {
throw new Error('#sak may not have permissions')
}
if (!Array.isArray(key.purpose) || key.purpose.length !== 1 || key.purpose[0] !== 'sak') {
throw new Error("#sak must have exactly one purpose: 'sak'")
}
if (key.ringLevel !== 0) {
throw new Error('#sak must have ringLevel 0')
}
}
// Is this a an invite key? If so, run logic for invite keys and invitation
// accounting
if (key.name.startsWith('#inviteKey-')) {
if (!state._vm.invites) state._vm.invites = Object.create(null)
const inviteSecret =
decryptedKey ||
(has(this.transientSecretKeys, key.id)
? serializeKey(this.transientSecretKeys[key.id], true)
: undefined)
state._vm.invites![key.id] = {
status: INVITE_STATUS.VALID,
initialQuantity: key.meta!.quantity!,
quantity: key.meta!.quantity!,
expires: key.meta!.expires!,
inviteSecret: inviteSecret!,
responses: []
}
}
// Is this KEY operation the result of requesting keys for another contract?
if (
key.meta?.keyRequest?.contractID &&
findSuitableSecretKeyId(state, [SPMessage.OP_KEY_ADD], ['sig'])
) {
const data = this.config.unwrapMaybeEncryptedData(key.meta.keyRequest.contractID)
// Are we subscribed to this contract?
// If we are not subscribed to the contract, we don't set pendingKeyRequests because we don't need that contract's state
// Setting pendingKeyRequests in these cases could result in issues
// when a corresponding OP_KEY_SHARE is received, which could trigger subscribing to this previously unsubscribed to contract
if (data && internalSideEffectStack) {
const keyRequestContractID = data.data
const reference = this.config.unwrapMaybeEncryptedData(key.meta.keyRequest.reference)
// Since now we'll make changes to keyRequestContractID, we need to
// do this while no other operations are running for that
// contract
internalSideEffectStack.push(() => {
sbp('chelonia/private/queueEvent', keyRequestContractID, () => {
const rootState = sbp(this.config.stateSelector) as ChelRootState
const originatingContractState = rootState[contractID] as ChelContractState
if (
sbp(
'chelonia/contract/hasKeyShareBeenRespondedBy',
originatingContractState,
keyRequestContractID,
reference
)
) {
// In the meantime, our key request has been responded, so we
// don't need to set pendingKeyRequests.
return
}
if (!has(rootState, keyRequestContractID)) {
this.config.reactiveSet(rootState, keyRequestContractID, Object.create(null))
}
const targetState = rootState[keyRequestContractID] as ChelContractState
if (!targetState._volatile) {
this.config.reactiveSet(targetState, '_volatile', Object.create(null))
}
if (!targetState._volatile!.pendingKeyRequests) {
this.config.reactiveSet(
rootState[keyRequestContractID]._volatile!,
'pendingKeyRequests',
[]
)
}
if (
targetState._volatile!.pendingKeyRequests!.some((pkr) => {
return pkr && pkr.contractID === contractID && pkr.hash === hash
})
) {
// This pending key request has already been registered.
// Nothing left to do.
return
}
// Mark the contract for which keys were requested as pending keys
// The hash (of the current message) is added to this dictionary
// for cross-referencing puposes.
targetState._volatile!.pendingKeyRequests!.push({
contractID,
name: key.name,
hash,
reference: reference?.data
})
this.setPostSyncOp(contractID, 'pending-keys-for-' + keyRequestContractID, [
'okTurtles.events/emit',
CONTRACT_IS_PENDING_KEY_REQUESTS,
{ contractID: keyRequestContractID }
])
}).catch((e: unknown) => {
// Using console.error instead of logEvtError because this
// is a side-effect and not relevant for outgoing messages
console.error(
'Error while setting or updating pendingKeyRequests',
{ contractID, keyRequestContractID, reference },
e
)
})
})
}
}
}
// Any persistent keys are stored as a side-effect
if (keysToPersist.length) {
internalSideEffectStack?.push(() => {
sbp('chelonia/storeSecretKeys', new Secret(keysToPersist))
})
}
internalSideEffectStack?.push(() => subscribeToForeignKeyContracts.call(this, contractID, state))
}
export const subscribeToForeignKeyContracts = function (
this: CheloniaContext,
contractID: string,
state: ChelContractState
) {
try {
Object.values(state._vm.authorizedKeys)
.filter((key) => !!key.foreignKey && findKeyIdByName(state, key.name) != null)
.forEach((key) => {
const foreignKey = String(key.foreignKey)
const fkUrl = new URL(foreignKey)
const foreignContract = fkUrl.pathname
const foreignKeyName = fkUrl.searchParams.get('keyName')
if (!foreignContract || !foreignKeyName) {
console.warn('Invalid foreign key: missing contract or key name', {
contractID,
keyId: key.id
})
return
}
const rootState = sbp(this.config.stateSelector)
const signingKey = findSuitableSecretKeyId(
state,
[SPMessage.OP_KEY_DEL],
['sig'],
key.ringLevel
)
const canMirrorOperations = !!signingKey
// If we cannot mirror operations, then there is nothing left to do
if (!canMirrorOperations) return
// If the key is already being watched, do nothing
if (Array.isArray(rootState?.[foreignContract]?._volatile?.watch)) {
if (
(rootState[foreignContract] as ChelContractState)._volatile!.watch!.find(
(v) => v[0] === key.name && v[1] === contractID
)
) { return }
}
if (!has(state._vm, 'pendingWatch')) {
this.config.reactiveSet(state._vm, 'pendingWatch', Object.create(null))
}
if (!has(state._vm.pendingWatch, foreignContract)) {
this.config.reactiveSet(state._vm.pendingWatch!, foreignContract, [])
}
if (!state._vm.pendingWatch![foreignContract].find(([n]) => n === foreignKeyName)) {
state._vm.pendingWatch![foreignContract].push([foreignKeyName, key.id])
}
this.setPostSyncOp(contractID, `watchForeignKeys-${contractID}`, [
'chelonia/private/watchForeignKeys',
contractID
])
})
} catch (e: unknown) {
console.warn('Error at subscribeToForeignKeyContracts: ' + ((e as Error).message || e))
}
}
// Messages might be sent before receiving already posted messages, which will
// result in a conflict
// When resending a message, race conditions might also occur (for example, if
// key rotation is required and there are many clients simultaneously online, it
// may be performed by all connected clients at once).
// The following function handles re-signing of messages when a conflict
// occurs (required because the message's previousHEAD will change) as well as
// duplicate operations. For operations involving keys, the payload will be
// rewritten to eliminate no-longer-relevant keys. In most cases, this would
// result in an empty payload, in which case the message is omitted entirely.
// The `raw` parameter will not modify the message payload in any way,
// which is typically done to avoid sending out invalid or redundant operations
// (e.g., a duplicate OP_KEY_ADD).
export const recreateEvent = (
entry: SPMessage,
state: ChelContractState,
contractsState: ChelRootState['contracts'][string],
disableAutoDedup?: boolean
): undefined | SPMessage => {
const { HEAD: previousHEAD, height: previousHeight, previousKeyOp } = contractsState || {}
if (!previousHEAD) {
throw new Error('recreateEvent: Giving up because the contract has been removed')
}
const head = entry.head()
const [opT, rawOpV] = entry.rawOp()
const recreateOperation = (opT: string, rawOpV: SignedData<SPOpValue>) => {
const opV = rawOpV.valueOf()
const recreateOperationInternal = (
opT: string,
opV: SPOpValue
): SPOpValue | typeof undefined => {
let newOpV: SPOpValue
if (opT === SPMessage.OP_KEY_ADD) {
if (!Array.isArray(opV)) throw new Error('Invalid message format')
newOpV = (opV as SPOpKeyAdd).filter((k) => {
const kId = (k.valueOf() as SPKey).id
return (
!has(state._vm.authorizedKeys, kId) ||
state._vm.authorizedKeys[kId]._notAfterHeight != null
)
})
// Has this key already been added? (i.e., present in authorizedKeys)
if (newOpV.length === 0) {
console.info('Omitting empty OP_KEY_ADD', { head })
} else if (newOpV.length === opV.length) {
return opV
}
} else if (opT === SPMessage.OP_KEY_DEL) {
if (!Array.isArray(opV)) throw new Error('Invalid message format')
// Has this key already been removed? (i.e., no longer in authorizedKeys)
newOpV = (opV as SPOpKeyDel).filter((keyId) => {
const kId = Object(keyId).valueOf()
return (
has(state._vm.authorizedKeys, kId) &&
state._vm.authorizedKeys[kId]._notAfterHeight == null
)
})
if (newOpV.length === 0) {
console.info('Omitting empty OP_KEY_DEL', { head })
} else if (newOpV.length === opV.length) {
return opV
}
} else if (opT === SPMessage.OP_KEY_UPDATE) {
if (!Array.isArray(opV)) throw new Error('Invalid message format')
// Has this key already been replaced? (i.e., no longer in authorizedKeys)
newOpV = (opV as SPOpKeyUpdate).filter((k) => {
const oKId = (k.valueOf() as SPKeyUpdate).oldKeyId
const nKId = (k.valueOf() as SPKeyUpdate).id
return (
nKId == null ||
(has(state._vm.authorizedKeys, oKId) &&
state._vm.authorizedKeys[oKId]._notAfterHeight == null)
)
})
if (newOpV.length === 0) {
console.info('Omitting empty OP_KEY_UPDATE', { head })
} else if (newOpV.length === opV.length) {
return opV
}
} else if (opT === SPMessage.OP_ATOMIC) {
if (!Array.isArray(opV)) throw new Error('Invalid message format')
newOpV = (opV as SPOpAtomic)
.map(([t, v]) => [t, recreateOperationInternal(t, v)])
.filter(([, v]) => !!v) as SPOpAtomic
if ((newOpV as SPOpAtomic).length === 0) {
console.info('Omitting empty OP_ATOMIC', { head })
} else if (
(newOpV as SPOpAtomic).length === opV.length &&
(newOpV as SPOpAtomic).reduce((acc, cv, i) => acc && cv === opV[i], true)
) {
return opV
} else {
return newOpV
}
} else {
return opV
}
}
const newOpV = recreateOperationInternal(opT, opV)
if (newOpV === opV) {
return rawOpV
} else if (newOpV === undefined) {
return
}
if (typeof rawOpV.recreate !== 'function') {
throw new Error('Unable to recreate operation')
}
return rawOpV.recreate(newOpV)
}
const newRawOpV = disableAutoDedup ? rawOpV : recreateOperation(opT, rawOpV)
if (!newRawOpV) return
const newOp = [opT, newRawOpV]
entry = SPMessage.cloneWith(head, newOp as SPOpRaw, {
previousKeyOp,
previousHEAD,
height: previousHeight + 1
})
return entry
}
export const getContractIDfromKeyId = (
contractID: string,
signingKeyId: string | null | undefined,
state: ChelContractState
): string | null | undefined => {
if (!signingKeyId) return
return signingKeyId && state._vm?.authorizedKeys?.[signingKeyId]?.foreignKey
? new URL(state._vm.authorizedKeys[signingKeyId].foreignKey!).pathname
: contractID
}
export function eventsAfter (
this: CheloniaContext,
contractID: string,
{
sinceHeight,
limit,
sinceHash,
stream = true
}: { sinceHeight: number; limit?: number; sinceHash?: string; stream: boolean }
): ReadableStream<string> | Promise<string[]> {
if (!contractID) {
// Avoid making a network roundtrip to tell us what we already know
throw new Error('Missing contract ID')
}
let lastUrl: string
const fetchEventsStreamReader = async () => {
requestLimit = Math.min(limit ?? MAX_EVENTS_AFTER, remainingEvents)
lastUrl = `${this.config.connectionURL}/eventsAfter/${contractID}/${sinceHeight}${Number.isInteger(requestLimit) ? `/${requestLimit}` : ''}`
const eventsResponse = await this.config.fetch(lastUrl, { signal })
if (!eventsResponse.ok) {
const msg = `${eventsResponse.status}: ${eventsResponse.statusText}`
if (eventsResponse.status === 404 || eventsResponse.status === 410) {
throw new ChelErrorResourceGone(msg, { cause: eventsResponse.status })
}
throw new ChelErrorUnexpectedHttpResponseCode(msg, { cause: eventsResponse.status })
}
if (!eventsResponse.body) throw new Error('Missing body')
latestHeight = parseInt(eventsResponse.headers.get('shelter-headinfo-height')!, 10)
if (!Number.isSafeInteger(latestHeight)) throw new Error('Invalid latest height')
requestCount++
return eventsResponse.body.getReader()
}
if (!Number.isSafeInteger(sinceHeight) || sinceHeight < 0) {
throw new TypeError('Invalid since height value. Expected positive integer.')
}
const signal = this.abortController.signal
let requestCount = 0
let remainingEvents = limit ?? Number.POSITIVE_INFINITY
let eventsStreamReader: ReadableStreamDefaultReader<Uint8Array>
let latestHeight: number
let state: 'fetch' | 'read-eos' | 'read-new-response' | 'read' | 'events' | 'eod' = 'fetch'
let requestLimit: number
let count: number
let buffer: string = ''
let currentEvent: string
// return ReadableStream with a custom pull function to handle streamed data
const s = new ReadableStream<string>({
// The pull function is called whenever the internal buffer of the stream
// becomes empty and needs more data.
async pull (controller) {
try {
for (;;) {
// Handle different states of the stream reading process.
switch (state) {
// When in 'fetch' state, initiate a new fetch request to obtain a
// stream reader for events.
case 'fetch': {
eventsStreamReader = await fetchEventsStreamReader()
// Transition to reading the new response and reset the processed
// events counter
state = 'read-new-response'
count = 0
break
}
case 'read-eos': // End of stream case
case 'read-new-response': // Just started reading a new response
case 'read': {
// Reading from the response stream
const { done, value } = await eventsStreamReader.read()
// If done, determine if the stream should close or fetch more
// data by making a new request
if (done) {
// No more events to process or reached the latest event
// Using `>=` instead of `===` to avoid an infinite loop in the
// event of data loss on the server.
if (remainingEvents === 0 || sinceHeight >= latestHeight) {
controller.close()
return
} else if (state === 'read-new-response' || buffer) {
// If done prematurely, throw an error
throw new Error('Invalid response: done too early')
} else {
// If there are still events to fetch, switch state to fetch
state = 'fetch'
break
}
}
if (!value) {
// If there's no value (e.g., empty response), throw an error
throw new Error('Invalid response: missing body')
}
// Concatenate new data to the buffer, trimming any
// leading/trailing whitespace (the response is a JSON array of
// base64-encoded data, meaning that whitespace is not significant)
buffer = buffer + Buffer.from(value).toString().trim()
// If there was only whitespace, try reading again
if (!buffer) break
if (state === 'read-new-response') {
// Response is in JSON format, so we look for the start of an
// array (`[`)
if (buffer[0] !== '[') {
throw new Error('Invalid response: no array start delimiter')
}
// Trim the array start delimiter from the buffer
buffer = buffer.slice(1)
} else if (state === 'read-eos') {
// If in 'read-eos' state and still reading data, it's an error
// because the response isn't valid JSON (there should be
// nothing other than whitespace after `]`)
throw new Error('Invalid data at the end of response')
}
// If not handling new response or end-of-stream, switch to
// processing events
state = 'events'
break
}
case 'events': {
// Process events by looking for a comma or closing bracket that
// indicates the end of an event
const nextIdx = buffer.search(/(?<=\s*)[,\]]/)
// If the end of the event isn't found, go back to reading more
// data
if (nextIdx < 0) {
state = 'read'
break
}
let enqueued = false
try {
// Extract the current event's value and trim whitespace
const eventValue = buffer.slice(0, nextIdx).trim()
if (eventValue) {
// Check if the event limit is reached; if so, throw an error
if (count === requestLimit) {
throw new Error('Received too many events')
}
currentEvent = JSON.parse(b64ToStr(JSON.parse(eventValue))).message
if (count === 0) {
const hash = SPMessage.deserializeHEAD(currentEvent).hash
const height = SPMessage.deserializeHEAD(currentEvent).head.height
if (height !== sinceHeight || (sinceHash && sinceHash !== hash)) {
if (height === sinceHeight && sinceHash && sinceHash !== hash) {
throw new ChelErrorForkedChain(
`Forked chain ${contractID}: hash(${hash}) !== since(${sinceHash})`
)
} else {
throw new Error(
`Unexpected data in ${contractID}: hash(${hash}) !== since(${sinceHash || ''}) or height(${height}) !== since(${sinceHeight})`
)