-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPMessage.ts
More file actions
866 lines (811 loc) · 25.7 KB
/
SPMessage.ts
File metadata and controls
866 lines (811 loc) · 25.7 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
import type { Key } from '@chelonia/crypto'
import {
CURVE25519XSALSA20POLY1305,
EDWARDS25519SHA512BATCH,
XSALSA20POLY1305,
keyId
} from '@chelonia/crypto'
import { serdesDeserializeSymbol, serdesSerializeSymbol, serdesTagSymbol } from '@chelonia/serdes'
import { has } from 'turtledash'
import type { EncryptedData } from './encryptedData.js'
import {
encryptedIncomingData,
encryptedIncomingForeignData,
maybeEncryptedIncomingData,
unwrapMaybeEncryptedData
} from './encryptedData.js'
import { createCID, multicodes } from './functions.js'
import type { SignedData } from './signedData.js'
import {
isRawSignedData,
isSignedData,
rawSignedIncomingData,
signedIncomingData
} from './signedData.js'
import type { ChelContractKey, ChelContractState, JSONObject, JSONType } from './types.js'
export type SPKeyType =
| typeof EDWARDS25519SHA512BATCH
| typeof CURVE25519XSALSA20POLY1305
| typeof XSALSA20POLY1305;
export type SPKeyPurpose = 'enc' | 'sig' | 'sak';
export type SPKey = {
id: string;
name: string;
purpose: SPKeyPurpose[];
ringLevel: number;
permissions: '*' | string[];
allowedActions?: '*' | string[];
permissionsContext?: '*' | string[];
meta?: {
quantity?: number;
expires?: number;
private?: {
transient?: boolean;
content?: EncryptedData<string>;
shareable?: boolean;
oldKeys?: string;
};
keyRequest?: {
contractID?: string;
reference?: string | EncryptedData<string>;
};
};
data: string;
foreignKey?: string;
_notBeforeHeight: number;
_notAfterHeight?: number;
_private?: string;
};
// Allows server to check if the user is allowed to register this type of contract
// TODO: rename 'type' to 'contractName':
export type SPOpContract = {
type: string;
keys: (SPKey | EncryptedData<SPKey>)[];
parentContract?: string;
};
export type ProtoSPOpActionUnencrypted = { action: string; data: JSONType; meta: JSONObject };
export type SPOpActionUnencrypted =
| ProtoSPOpActionUnencrypted
| SignedData<ProtoSPOpActionUnencrypted>;
// encrypted version of SPOpActionUnencrypted
export type SPOpActionEncrypted = EncryptedData<SPOpActionUnencrypted>;
export type SPOpKeyAdd = (SPKey | EncryptedData<SPKey>)[];
export type SPOpKeyDel = (string | EncryptedData<string>)[];
export type SPOpPropSet = { key: string; value: JSONType };
export type ProtoSPOpKeyShare = {
contractID: string;
keys: SPKey[];
foreignContractID?: string;
keyRequestHash?: string;
keyRequestHeight?: number;
};
export type SPOpKeyShare = ProtoSPOpKeyShare | EncryptedData<ProtoSPOpKeyShare>;
// TODO encrypted SPOpKeyRequest
export type ProtoSPOpKeyRequest = {
contractID: string;
height: number;
replyWith: SignedData<{
encryptionKeyId: string;
responseKey: EncryptedData<string>;
}>;
request: string;
};
export type SPOpKeyRequest = ProtoSPOpKeyRequest | EncryptedData<ProtoSPOpKeyRequest>;
export type ProtoSPOpKeyRequestSeen = {
keyRequestHash: string;
keyShareHash?: string;
success: boolean;
};
export type SPOpKeyRequestSeen = ProtoSPOpKeyRequestSeen | EncryptedData<ProtoSPOpKeyRequestSeen>;
export type SPKeyUpdate = {
name: string;
id?: string;
oldKeyId: string;
data?: string;
purpose?: string[];
permissions?: string[];
allowedActions?: '*' | string[];
permissionsContext?: '*' | string[];
meta?: {
quantity?: number;
expires?: number;
private?: {
transient?: boolean;
content?: string;
shareable?: boolean;
oldKeys?: string;
};
};
};
export type SPOpKeyUpdate = (SPKeyUpdate | EncryptedData<SPKeyUpdate>)[];
export type SPOpType =
| 'c'
| 'a'
| 'ae'
| 'au'
| 'ka'
| 'kd'
| 'ku'
| 'pu'
| 'ps'
| 'pd'
| 'ks'
| 'kr'
| 'krs';
type ProtoSPOpValue =
| SPOpContract
| SPOpActionEncrypted
| SPOpActionUnencrypted
| SPOpKeyAdd
| SPOpKeyDel
| SPOpPropSet
| SPOpKeyShare
| SPOpKeyRequest
| SPOpKeyRequestSeen
| SPOpKeyUpdate;
export type ProtoSPOpMap = {
c: SPOpContract;
ae: SPOpActionEncrypted;
au: SPOpActionUnencrypted;
ka: SPOpKeyAdd;
kd: SPOpKeyDel;
ku: SPOpKeyUpdate;
pu: never;
ps: SPOpPropSet;
pd: never;
ks: SPOpKeyShare;
kr: SPOpKeyRequest;
krs: SPOpKeyRequestSeen;
};
export type SPOpAtomic = {
[K in keyof ProtoSPOpMap]: [K, ProtoSPOpMap[K]];
}[keyof ProtoSPOpMap][];
export type SPOpValue = ProtoSPOpValue | SPOpAtomic;
export type SPOpRaw = [SPOpType, SignedData<SPOpValue>];
export type SPOpMap = ProtoSPOpMap & { a: SPOpAtomic };
export type SPOp = {
[K in keyof SPOpMap]: [K, SPOpMap[K]];
}[keyof SPOpMap];
export type SPMsgDirection = 'incoming' | 'outgoing';
export type SPHead = {
version: '1.0.0';
op: SPOpType;
height: number;
contractID: string | null;
previousKeyOp: string | null;
previousHEAD: string | null;
manifest: string;
};
type SPMsgParams = {
direction: SPMsgDirection;
mapping: { key: string; value: string };
head: SPHead;
signedMessageData: SignedData<SPOpValue>;
};
// Takes a raw message and processes it so that EncryptedData and SignedData
// attributes are defined
const decryptedAndVerifiedDeserializedMessage = (
head: SPHead,
headJSON: string,
contractID: string,
parsedMessage: SPOpValue,
additionalKeys: Record<string, Key | string> | undefined,
state: ChelContractState
): SPOpValue => {
const op = head.op
const height = head.height
const message: SPOpValue =
op === SPMessage.OP_ACTION_ENCRYPTED
? encryptedIncomingData<SPOpActionUnencrypted>(
contractID,
state,
parsedMessage as [string, string],
height,
additionalKeys,
headJSON,
undefined
)
: parsedMessage
// If the operation is SPMessage.OP_KEY_ADD or SPMessage.OP_KEY_UPDATE,
// extract encrypted data from key.meta?.private?.content
if (([SPMessage.OP_KEY_ADD, SPMessage.OP_KEY_UPDATE] as SPOpType[]).includes(op as SPOpType)) {
return (message as SPOpKeyAdd | SPOpKeyUpdate).map((key) => {
return maybeEncryptedIncomingData<SPKey>(
contractID,
state,
key as SPKey,
height,
additionalKeys,
headJSON,
(key) => {
if (key.meta?.private?.content) {
key.meta.private.content = encryptedIncomingData<string>(
contractID,
state,
key.meta.private.content as unknown as [string, string],
height,
additionalKeys,
headJSON,
(value) => {
// Validator function to verify the key matches its expected ID
const computedKeyId = keyId(value)
if (computedKeyId !== key.id) {
throw new Error(
`Key ID mismatch. Expected to decrypt key ID ${key.id} but got ${computedKeyId}`
)
}
}
)
}
// key.meta?.keyRequest?.contractID could be optionally encrypted
if (key.meta?.keyRequest?.reference) {
try {
key.meta.keyRequest.reference = maybeEncryptedIncomingData<string>(
contractID,
state,
key.meta.keyRequest.reference as string,
height,
additionalKeys,
headJSON
)?.valueOf()
} catch {
// If we couldn't decrypt it, this value is of no use to us (we
// can't keep track of key requests and key shares), so we delete it
delete key.meta.keyRequest.reference
}
}
// key.meta?.keyRequest?.contractID could be optionally encrypted
if (key.meta?.keyRequest?.contractID) {
try {
key.meta.keyRequest.contractID = maybeEncryptedIncomingData<string>(
contractID,
state,
key.meta.keyRequest.contractID,
height,
additionalKeys,
headJSON
)?.valueOf()
} catch {
// If we couldn't decrypt it, this value is of no use to us (we
// can't keep track of key requests and key shares), so we delete it
delete key.meta.keyRequest.contractID
}
}
}
)
})
}
// If the operation is SPMessage.OP_CONTRACT,
// extract encrypted data from keys?.[].meta?.private?.content
if (op === SPMessage.OP_CONTRACT) {
(message as SPOpContract).keys = (message as SPOpContract).keys?.map((key) => {
return maybeEncryptedIncomingData<SPKey>(
contractID,
state,
key as SPKey,
height,
additionalKeys,
headJSON,
(key) => {
if (!key.meta?.private?.content) return
// The following two lines are commented out because this feature
// (using a foreign decryption contract) doesn't seem to be in use and
// the use case seems unclear.
// const decryptionFn = key.meta.private.foreignContractID ? encryptedIncomingForeignData : encryptedIncomingData
// const decryptionContract = key.meta.private.foreignContractID ? key.meta.private.foreignContractID : contractID
const decryptionFn = encryptedIncomingData
const decryptionContract = contractID
key.meta.private.content = decryptionFn<string>(
decryptionContract,
state as never,
key.meta.private.content as unknown as [string, string],
height as never,
additionalKeys,
headJSON,
(value) => {
const computedKeyId = keyId(value)
if (computedKeyId !== key.id) {
throw new Error(
`Key ID mismatch. Expected to decrypt key ID ${key.id} but got ${computedKeyId}`
)
}
}
)
}
)
})
}
// If the operation is SPMessage.OP_KEY_SHARE,
// extract encrypted data from keys?.[].meta?.private?.content
if (op === SPMessage.OP_KEY_SHARE) {
return maybeEncryptedIncomingData<ProtoSPOpKeyShare>(
contractID,
state,
message as ProtoSPOpKeyShare,
height,
additionalKeys,
headJSON,
(message) => {
message.keys?.forEach((key) => {
if (!key.meta?.private?.content) return
const decryptionFn = message.foreignContractID
? encryptedIncomingForeignData
: encryptedIncomingData
const decryptionContract = message.foreignContractID || contractID
key.meta.private.content = decryptionFn<string>(
decryptionContract,
state as never,
key.meta.private.content as unknown as [string, string],
height as never,
additionalKeys,
headJSON,
(value) => {
const computedKeyId = keyId(value)
if (computedKeyId !== key.id) {
throw new Error(
`Key ID mismatch. Expected to decrypt key ID ${key.id} but got ${computedKeyId}`
)
}
}
)
})
}
)
}
// If the operation is OP_KEY_REQUEST, the payload might be EncryptedData
// The ReplyWith attribute is SignedData
if (op === SPMessage.OP_KEY_REQUEST) {
return maybeEncryptedIncomingData<ProtoSPOpKeyRequest>(
contractID,
state,
message as ProtoSPOpKeyRequest,
height,
additionalKeys,
headJSON,
(msg) => {
msg.replyWith = signedIncomingData(
msg.contractID,
undefined,
msg.replyWith as unknown as { _signedData: [string, string, string] },
msg.height,
headJSON
)
}
)
}
// If the operation is OP_ACTION_UNENCRYPTED, it may contain an inner
// signature
// Actions must be signed using a key for the current contract
if (op === SPMessage.OP_ACTION_UNENCRYPTED && isRawSignedData(message)) {
return signedIncomingData<ProtoSPOpActionUnencrypted>(
contractID,
state,
message,
height,
headJSON
)
}
// Inner signatures are handled by EncryptedData
if (op === SPMessage.OP_ACTION_ENCRYPTED) {
return message
}
if (op === SPMessage.OP_KEY_DEL) {
return (message as SPOpKeyDel).map((key) => {
return maybeEncryptedIncomingData<string>(
contractID,
state,
key as unknown as string,
height,
additionalKeys,
headJSON,
undefined
)
})
}
if (op === SPMessage.OP_KEY_REQUEST_SEEN) {
return maybeEncryptedIncomingData<ProtoSPOpKeyRequestSeen>(
contractID,
state,
parsedMessage as unknown as ProtoSPOpKeyRequestSeen,
height,
additionalKeys,
headJSON,
undefined
)
}
// If the operation is OP_ATOMIC, call this function recursively
if (op === SPMessage.OP_ATOMIC) {
return (message as SPOpAtomic).map(([opT, opV]) => [
opT,
decryptedAndVerifiedDeserializedMessage(
{ ...head, op: opT },
headJSON,
contractID,
opV,
additionalKeys,
state
)
]) as SPOpAtomic
}
return message
}
export class SPMessage {
// flow type annotations to make flow happy
_mapping: { key: string; value: string }
_head: SPHead
_message!: SPOpValue
_signedMessageData: SignedData<SPOpValue>
_direction: SPMsgDirection
_decryptedValue?: unknown
_innerSigningKeyId?: string
static OP_CONTRACT = 'c' as const
static OP_ACTION_ENCRYPTED = 'ae' as const // e2e-encrypted action
static OP_ACTION_UNENCRYPTED = 'au' as const // publicly readable action
static OP_KEY_ADD = 'ka' as const // add this key to the list of keys allowed to write to this contract, or update an existing key
static OP_KEY_DEL = 'kd' as const // remove this key from authorized keys
static OP_KEY_UPDATE = 'ku' as const // update key in authorized keys
static OP_PROTOCOL_UPGRADE = 'pu' as const
static OP_PROP_SET = 'ps' as const // set a public key/value pair
static OP_PROP_DEL = 'pd' as const // delete a public key/value pair
static OP_CONTRACT_AUTH = 'ca' as const // authorize a contract
static OP_CONTRACT_DEAUTH = 'cd' as const // deauthorize a contract
static OP_ATOMIC = 'a' as const // atomic op
static OP_KEY_SHARE = 'ks' as const // key share
static OP_KEY_REQUEST = 'kr' as const // key request
static OP_KEY_REQUEST_SEEN = 'krs' as const // key request response
// eslint-disable-next-line camelcase
static createV1_0 ({
contractID,
previousHEAD = null,
previousKeyOp = null,
// Height will be automatically set to the correct value when sending
// The reason to set it to Number.MAX_SAFE_INTEGER is so that we can
// temporarily process outgoing messages with signature validation
// still working
height = Number.MAX_SAFE_INTEGER,
op,
manifest
}: {
contractID: string | null;
previousHEAD?: string | null;
previousKeyOp?: string | null;
height?: number;
op: SPOpRaw;
manifest: string;
}): SPMessage {
const head: SPHead = {
version: '1.0.0',
previousHEAD,
previousKeyOp,
height,
contractID,
op: op[0],
manifest
}
return new this(messageToParams(head, op[1]))
}
// SPMessage.cloneWith could be used when make a SPMessage object having the same id()
// https://github.com/okTurtles/group-income/issues/1503
static cloneWith (targetHead: SPHead, targetOp: SPOpRaw, sources: Partial<SPHead>): SPMessage {
const head = Object.assign({}, targetHead, sources)
return new this(messageToParams(head, targetOp[1]))
}
static deserialize (
value: string,
additionalKeys?: Record<string, Key | string>,
state?: ChelContractState,
unwrapMaybeEncryptedDataFn: (
data: SPKey | EncryptedData<SPKey>,
) => { encryptionKeyId: string | null; data: SPKey } | undefined = unwrapMaybeEncryptedData
): SPMessage {
if (!value) throw new Error(`deserialize bad value: ${value}`)
const { head: headJSON, ...parsedValue } = JSON.parse(value)
const head = JSON.parse(headJSON)
const contractID =
head.op === SPMessage.OP_CONTRACT
? createCID(value, multicodes.SHELTER_CONTRACT_DATA)
: head.contractID
// Special case for OP_CONTRACT, since the keys are not yet present in the
// state
if (!state?._vm?.authorizedKeys && head.op === SPMessage.OP_CONTRACT) {
const value = rawSignedIncomingData<SPOpContract>(parsedValue)
const authorizedKeys = Object.fromEntries(
value
.valueOf()
?.keys.map((wk) => {
const k = unwrapMaybeEncryptedDataFn(wk)
if (!k) return null
return [k.data.id, k.data] as [string, ChelContractKey]
})
// eslint-disable-next-line no-use-before-define
.filter(Boolean as unknown as ((x: unknown) => x is [string, ChelContractKey]))
)
state = {
_vm: {
type: head.type,
authorizedKeys
}
}
}
const signedMessageData = signedIncomingData<SPOpValue>(
contractID,
state,
parsedValue,
head.height,
headJSON,
(message) =>
decryptedAndVerifiedDeserializedMessage(
head,
headJSON,
contractID,
message,
additionalKeys,
state!
)
)
return new this({
direction: 'incoming',
mapping: { key: createCID(value, multicodes.SHELTER_CONTRACT_DATA), value },
head,
signedMessageData
})
}
static deserializeHEAD (value: string): {
head: SPHead;
hash: string;
contractID: string;
isFirstMessage: boolean;
description: () => string;
} {
if (!value) throw new Error(`deserialize bad value: ${value}`)
let head: SPHead, hash: string
const result = {
get head () {
if (head === undefined) {
head = JSON.parse(JSON.parse(value).head)
}
return head
},
get hash () {
if (!hash) {
hash = createCID(value, multicodes.SHELTER_CONTRACT_DATA)
}
return hash
},
get contractID () {
return result.head?.contractID ?? result.hash
},
// `description` is not a getter to prevent the value from being copied
// if the object is cloned or serialized
description (): string {
const type = this.head.op
return `<op_${type}|${this.hash} of ${this.contractID}>`
},
get isFirstMessage (): boolean {
return !result.head?.contractID
}
}
return result
}
constructor (params: SPMsgParams) {
this._direction = params.direction
this._mapping = params.mapping
this._head = params.head
this._signedMessageData = params.signedMessageData
// perform basic sanity check
const type = this.opType()
let atomicTopLevel = true
const validate = (type: string, message: SPOpValue) => {
switch (type) {
case SPMessage.OP_CONTRACT:
if (!this.isFirstMessage() || !atomicTopLevel) { throw new Error('OP_CONTRACT: must be first message') }
break
case SPMessage.OP_ATOMIC:
if (!atomicTopLevel) {
throw new Error('OP_ATOMIC not allowed inside of OP_ATOMIC')
}
if (!Array.isArray(message)) {
throw new TypeError('OP_ATOMIC must be of an array type')
}
atomicTopLevel = false;
(message as SPOpAtomic).forEach(([t, m]) => validate(t, m))
break
case SPMessage.OP_KEY_ADD:
case SPMessage.OP_KEY_DEL:
case SPMessage.OP_KEY_UPDATE:
if (!Array.isArray(message)) { throw new TypeError('OP_KEY_{ADD|DEL|UPDATE} must be of an array type') }
break
case SPMessage.OP_KEY_SHARE:
case SPMessage.OP_KEY_REQUEST:
case SPMessage.OP_KEY_REQUEST_SEEN:
case SPMessage.OP_ACTION_ENCRYPTED:
case SPMessage.OP_ACTION_UNENCRYPTED:
// nothing for now
break
default:
throw new Error(`unsupported op: ${type}`)
}
}
// this._message is set as a getter to verify the signature only once the
// message contents are read
Object.defineProperty(this, '_message', {
get: ((validated?: boolean) => () => {
const message = this._signedMessageData.valueOf()
// If we haven't validated the message, validate it now
if (!validated) {
validate(type, message)
validated = true
}
return message
})()
})
}
decryptedValue (): unknown | undefined {
if (this._decryptedValue) return this._decryptedValue
try {
const value = this.message()
// TODO: This uses `unwrapMaybeEncryptedData` instead of a configurable
// version based on `skipDecryptionAttempts`. This is fine based on current
// use, and also something else might be confusing based on the explicit
// name of this function, `decryptedValue`.
const data = unwrapMaybeEncryptedData(value)
// Did decryption succeed? (unwrapMaybeEncryptedData will return undefined
// on failure)
if (data?.data) {
// The data inside could be signed. In this case, we unwrap that to get
// to the inner contents
if (isSignedData(data.data)) {
this._innerSigningKeyId = data.data.signingKeyId
this._decryptedValue = data.data.valueOf()
} else {
this._decryptedValue = data.data
}
}
return this._decryptedValue
} catch {
// Signature or encryption error
// We don't log this error because it's already logged when the value is
// retrieved
return undefined
}
}
innerSigningKeyId (): string | undefined {
if (!this._decryptedValue) {
this.decryptedValue()
}
return this._innerSigningKeyId
}
head (): SPHead {
return this._head
}
message (): SPOpValue {
return this._message
}
op (): SPOp {
return [this.head().op, this.message()] as SPOp
}
rawOp (): SPOpRaw {
return [this.head().op, this._signedMessageData]
}
opType (): SPOpType {
return this.head().op
}
opValue (): SPOpValue {
return this.message()
}
signingKeyId (): string {
return this._signedMessageData.signingKeyId
}
manifest (): string {
return this.head().manifest
}
description (): string {
const type = this.opType()
let desc = `<op_${type}`
if (type === SPMessage.OP_ACTION_UNENCRYPTED) {
try {
const value = this.opValue().valueOf() as ProtoSPOpActionUnencrypted
if (typeof value.action === 'string') {
desc += `|${value.action}`
}
} catch (e) {
console.warn('Error on .description()', this.hash(), e)
}
}
return `${desc}|${this.hash()} of ${this.contractID()}>`
}
isFirstMessage (): boolean {
return !this.head().contractID
}
contractID (): string {
return this.head().contractID || this.hash()
}
serialize (): string {
return this._mapping.value
}
hash (): string {
return this._mapping.key
}
previousKeyOp (): string | null {
return this._head.previousKeyOp
}
height (): number {
return this._head.height
}
id (): string {
// TODO: Schedule for later removal
throw new Error('SPMessage.id() was called but it has been removed')
}
direction (): 'incoming' | 'outgoing' {
return this._direction
}
// `isKeyOp` is used to filter out non-key operations for providing an
// abbreviated chain fo snapshot validation
isKeyOp (): boolean {
let value: SPOpValue
return !!(
(keyOps as SPOpType[]).includes(this.opType()) ||
(this.opType() === SPMessage.OP_ATOMIC &&
Array.isArray((value = this.opValue())) &&
(value as SPOpAtomic).some(([opT]) => {
return (keyOps as SPOpType[]).includes(opT)
}))
)
}
static get [serdesTagSymbol] () {
return 'SPMessage'
}
static [serdesSerializeSymbol] (m: SPMessage) {
return [m.serialize(), m.direction(), m.decryptedValue(), m.innerSigningKeyId()]
}
static [serdesDeserializeSymbol] ([serialized, direction, decryptedValue, innerSigningKeyId]: [
string,
SPMsgDirection,
object,
string,
]) {
const m = SPMessage.deserialize(serialized)
m._direction = direction
m._decryptedValue = decryptedValue
m._innerSigningKeyId = innerSigningKeyId
return m
}
}
function messageToParams (head: SPHead, message: SignedData<SPOpValue>): SPMsgParams {
// NOTE: the JSON strings generated here must be preserved forever.
// do not ever regenerate this message using the contructor.
// instead store it using serialize() and restore it using deserialize().
// The issue is that different implementations of JavaScript engines might generate different strings
// when serializing JS objects using JSON.stringify
// and that would lead to different hashes resulting from createCID.
// So to get around this we save the serialized string upon creation
// and keep a copy of it (instead of regenerating it as needed).
// https://github.com/okTurtles/group-income/pull/1513#discussion_r1142809095
let mapping: { key: string; value: string }
return {
direction: has(message, 'recreate') ? 'outgoing' : 'incoming',
// Lazy computation of mapping to prevent us from serializing outgoing
// atomic operations
get mapping () {
if (!mapping) {
const headJSON = JSON.stringify(head)
const messageJSON = { ...message.serialize(headJSON), head: headJSON }
const value = JSON.stringify(messageJSON)
mapping = {
key: createCID(value, multicodes.SHELTER_CONTRACT_DATA),
value
}
}
return mapping
},
head,
signedMessageData: message
}
}
// Operations that affect valid keys
const keyOps = [
SPMessage.OP_CONTRACT,
SPMessage.OP_KEY_ADD,
SPMessage.OP_KEY_DEL,
SPMessage.OP_KEY_UPDATE
]