@7h3/protocol 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +1169 -175
  3. package/bin/7h3.ts +22 -1
  4. package/docs/assets/banner-github.png +0 -0
  5. package/docs/assets/banner.svg +123 -0
  6. package/package.json +55 -13
  7. package/sdk/browser/package.json +1 -1
  8. package/sdk/go/cbor.go +551 -0
  9. package/sdk/go/cbor_test.go +232 -0
  10. package/sdk/go/encryption.go +280 -0
  11. package/sdk/go/encryption_test.go +318 -0
  12. package/sdk/go/go.mod +5 -1
  13. package/sdk/go/go.sum +4 -0
  14. package/sdk/go/replay.go +121 -0
  15. package/sdk/go/replay_test.go +149 -0
  16. package/sdk/pq/package-lock.json +1358 -0
  17. package/sdk/pq/package.json +42 -0
  18. package/sdk/pq/src/index.test.ts +143 -0
  19. package/sdk/pq/src/index.ts +166 -0
  20. package/sdk/pq/tsconfig.json +14 -0
  21. package/sdk/pq/vitest.config.ts +7 -0
  22. package/sdk/python/protocol_7h3/encryption.py +252 -0
  23. package/sdk/python/protocol_7h3/pq.py +244 -0
  24. package/sdk/python/protocol_7h3/replay.py +98 -0
  25. package/sdk/python/pyproject.toml +1 -1
  26. package/sdk/python/tests/test_encryption.py +206 -0
  27. package/sdk/rust/Cargo.lock +1 -1
  28. package/sdk/rust/Cargo.toml +1 -1
  29. package/sdk/threshold/index.d.ts +68 -0
  30. package/sdk/threshold/index.d.ts.map +1 -0
  31. package/sdk/threshold/index.js +254 -0
  32. package/sdk/threshold/package-lock.json +1361 -0
  33. package/sdk/threshold/package.json +39 -0
  34. package/sdk/threshold/src/index.d.ts +68 -0
  35. package/sdk/threshold/src/index.d.ts.map +1 -0
  36. package/sdk/threshold/src/index.js +254 -0
  37. package/sdk/threshold/src/index.test.ts +238 -0
  38. package/sdk/threshold/src/index.ts +355 -0
  39. package/sdk/threshold/tsconfig.json +19 -0
  40. package/sdk/threshold/vitest.config.ts +12 -0
  41. package/src/capability.test.ts +504 -0
  42. package/src/capability.ts +380 -0
  43. package/src/cborCodec.test.ts +263 -0
  44. package/src/cborCodec.ts +339 -0
  45. package/src/encryption.test.ts +206 -0
  46. package/src/encryption.ts +245 -0
  47. package/src/envelopeCbor.ts +140 -0
  48. package/src/gateway.ts +75 -0
  49. package/src/httpBinding.ts +37 -11
  50. package/src/index.ts +7 -0
  51. package/src/otel.ts +136 -0
  52. package/src/protocol.d.ts +67 -0
  53. package/src/protocol.d.ts.map +1 -0
  54. package/src/protocol.js +294 -0
  55. package/src/protocol.ts +1 -0
  56. package/src/replayStores.test.ts +133 -1
  57. package/src/replayStores.ts +136 -3
  58. package/src/stream.test.ts +254 -0
  59. package/src/stream.ts +417 -0
  60. package/src/telemetry.test.ts +251 -0
  61. package/src/telemetry.ts +299 -0
  62. package/src/wsBinding.ts +100 -0
  63. package/vitest.config.ts +11 -0
package/sdk/go/cbor.go ADDED
@@ -0,0 +1,551 @@
1
+ // Package protocol7h3 — minimal deterministic CBOR encoder/decoder for ProtocolEnvelope.
2
+ // No external dependencies. Follows RFC 8949 §4.2 deterministic encoding.
3
+ //
4
+ // Envelope numeric field key scheme (matches TypeScript envelopeCbor.ts):
5
+ // Top-level: 1=header, 2=body, 3=signature(optional)
6
+ // Header: 1=version, 2=messageId, 3=timestampMs, 4=ttlMs, 5=sender, 6=recipient(opt), 7=nonce
7
+ // Body: 1=intent, 2=content, 3=capability(opt), 4=correlationId(opt)
8
+ // Signature: 1=alg, 2=keyId, 3=value
9
+ package protocol7h3
10
+
11
+ import (
12
+ "encoding/binary"
13
+ "fmt"
14
+ "math"
15
+ "sort"
16
+ )
17
+
18
+ // CBOR major types
19
+ const (
20
+ cborMTUint = 0
21
+ cborMTNint = 1
22
+ cborMTBstr = 2
23
+ cborMTTstr = 3
24
+ cborMTArray = 4
25
+ cborMTMap = 5
26
+ cborMTSimple = 7
27
+ )
28
+
29
+ // Additional info values
30
+ const (
31
+ cborAI1Byte = 24
32
+ cborAI2Byte = 25
33
+ cborAI4Byte = 26
34
+ cborAI8Byte = 27
35
+ )
36
+
37
+ // Simple values
38
+ const (
39
+ cborFalse = 0xf4
40
+ cborTrue = 0xf5
41
+ cborNull = 0xf6
42
+ cborFloat64 = 0xfb
43
+ )
44
+
45
+ // ─── encoder ────────────────────────────────────────────────────────────────
46
+
47
+ type cborEncoder struct {
48
+ buf []byte
49
+ }
50
+
51
+ func (e *cborEncoder) encodeHead(mt int, val uint64) {
52
+ base := byte(mt << 5)
53
+ switch {
54
+ case val <= 23:
55
+ e.buf = append(e.buf, base|byte(val))
56
+ case val <= 0xff:
57
+ e.buf = append(e.buf, base|cborAI1Byte, byte(val))
58
+ case val <= 0xffff:
59
+ e.buf = append(e.buf, base|cborAI2Byte, byte(val>>8), byte(val))
60
+ case val <= 0xffffffff:
61
+ b := [4]byte{}
62
+ binary.BigEndian.PutUint32(b[:], uint32(val))
63
+ e.buf = append(e.buf, base|cborAI4Byte)
64
+ e.buf = append(e.buf, b[:]...)
65
+ default:
66
+ b := [8]byte{}
67
+ binary.BigEndian.PutUint64(b[:], val)
68
+ e.buf = append(e.buf, base|cborAI8Byte)
69
+ e.buf = append(e.buf, b[:]...)
70
+ }
71
+ }
72
+
73
+ func (e *cborEncoder) encodeUint(v uint64) {
74
+ e.encodeHead(cborMTUint, v)
75
+ }
76
+
77
+ func (e *cborEncoder) encodeText(s string) {
78
+ b := []byte(s)
79
+ e.encodeHead(cborMTTstr, uint64(len(b)))
80
+ e.buf = append(e.buf, b...)
81
+ }
82
+
83
+ func (e *cborEncoder) encodeFloat64(f float64) {
84
+ e.buf = append(e.buf, cborFloat64)
85
+ b := [8]byte{}
86
+ binary.BigEndian.PutUint64(b[:], math.Float64bits(f))
87
+ e.buf = append(e.buf, b[:]...)
88
+ }
89
+
90
+ // encodedKeyForUint returns the CBOR encoding of an unsigned integer key (for sorting).
91
+ func encodedKeyForUint(v uint64) []byte {
92
+ tmp := &cborEncoder{}
93
+ tmp.encodeHead(cborMTUint, v)
94
+ return tmp.buf
95
+ }
96
+
97
+ // encodedKeyForText returns the CBOR encoding of a text string key (for sorting).
98
+ func encodedKeyForText(s string) []byte {
99
+ tmp := &cborEncoder{}
100
+ tmp.encodeText(s)
101
+ return tmp.buf
102
+ }
103
+
104
+ type intKV struct {
105
+ key uint64
106
+ keyEnc []byte
107
+ value interface{}
108
+ }
109
+
110
+ // encodeIntMap encodes a map with integer keys deterministically (RFC 8949 §4.2).
111
+ func (e *cborEncoder) encodeIntMap(pairs []intKV) {
112
+ // Sort by encoded key bytes (lexicographic)
113
+ sort.Slice(pairs, func(i, j int) bool {
114
+ a, b := pairs[i].keyEnc, pairs[j].keyEnc
115
+ for k := 0; k < len(a) && k < len(b); k++ {
116
+ if a[k] != b[k] {
117
+ return a[k] < b[k]
118
+ }
119
+ }
120
+ return len(a) < len(b)
121
+ })
122
+
123
+ e.encodeHead(cborMTMap, uint64(len(pairs)))
124
+ for _, kv := range pairs {
125
+ e.encodeHead(cborMTUint, kv.key)
126
+ e.encodeAny(kv.value)
127
+ }
128
+ }
129
+
130
+ func (e *cborEncoder) encodeAny(v interface{}) {
131
+ switch val := v.(type) {
132
+ case nil:
133
+ e.buf = append(e.buf, cborNull)
134
+ case bool:
135
+ if val {
136
+ e.buf = append(e.buf, cborTrue)
137
+ } else {
138
+ e.buf = append(e.buf, cborFalse)
139
+ }
140
+ case int:
141
+ if val >= 0 {
142
+ e.encodeHead(cborMTUint, uint64(val))
143
+ } else {
144
+ e.encodeHead(cborMTNint, uint64(-1-val))
145
+ }
146
+ case int64:
147
+ if val >= 0 {
148
+ e.encodeHead(cborMTUint, uint64(val))
149
+ } else {
150
+ e.encodeHead(cborMTNint, uint64(-1-val))
151
+ }
152
+ case uint64:
153
+ e.encodeHead(cborMTUint, val)
154
+ case float64:
155
+ e.encodeFloat64(val)
156
+ case string:
157
+ e.encodeText(val)
158
+ case []byte:
159
+ e.encodeHead(cborMTBstr, uint64(len(val)))
160
+ e.buf = append(e.buf, val...)
161
+ default:
162
+ panic(fmt.Sprintf("cborEncoder: unsupported type %T", v))
163
+ }
164
+ }
165
+
166
+ // ─── decoder ────────────────────────────────────────────────────────────────
167
+
168
+ type cborDecoder struct {
169
+ data []byte
170
+ offset int
171
+ }
172
+
173
+ func (d *cborDecoder) remaining() int {
174
+ return len(d.data) - d.offset
175
+ }
176
+
177
+ func (d *cborDecoder) readByte() (byte, error) {
178
+ if d.remaining() < 1 {
179
+ return 0, fmt.Errorf("cbor: unexpected end of data")
180
+ }
181
+ b := d.data[d.offset]
182
+ d.offset++
183
+ return b, nil
184
+ }
185
+
186
+ func (d *cborDecoder) readN(n int) ([]byte, error) {
187
+ if d.remaining() < n {
188
+ return nil, fmt.Errorf("cbor: unexpected end of data")
189
+ }
190
+ out := d.data[d.offset : d.offset+n]
191
+ d.offset += n
192
+ return out, nil
193
+ }
194
+
195
+ func (d *cborDecoder) decodeHead() (mt int, val uint64, err error) {
196
+ b, err := d.readByte()
197
+ if err != nil {
198
+ return 0, 0, err
199
+ }
200
+ mt = int(b >> 5)
201
+ ai := b & 0x1f
202
+
203
+ switch {
204
+ case ai <= 23:
205
+ val = uint64(ai)
206
+ case ai == cborAI1Byte:
207
+ nb, err := d.readByte()
208
+ if err != nil {
209
+ return 0, 0, err
210
+ }
211
+ val = uint64(nb)
212
+ case ai == cborAI2Byte:
213
+ nb, err := d.readN(2)
214
+ if err != nil {
215
+ return 0, 0, err
216
+ }
217
+ val = uint64(binary.BigEndian.Uint16(nb))
218
+ case ai == cborAI4Byte:
219
+ nb, err := d.readN(4)
220
+ if err != nil {
221
+ return 0, 0, err
222
+ }
223
+ val = uint64(binary.BigEndian.Uint32(nb))
224
+ case ai == cborAI8Byte:
225
+ nb, err := d.readN(8)
226
+ if err != nil {
227
+ return 0, 0, err
228
+ }
229
+ val = binary.BigEndian.Uint64(nb)
230
+ default:
231
+ return 0, 0, fmt.Errorf("cbor: unsupported additional info %d", ai)
232
+ }
233
+ return mt, val, nil
234
+ }
235
+
236
+ // decodeStringValue decodes a text string from the already-consumed initial byte context.
237
+ // The caller has already called decodeHead and got mt=3, val=length.
238
+ func (d *cborDecoder) readString(length uint64) (string, error) {
239
+ nb, err := d.readN(int(length))
240
+ if err != nil {
241
+ return "", err
242
+ }
243
+ return string(nb), nil
244
+ }
245
+
246
+ // decodeIntMapFlat decodes a CBOR map with integer keys and string values.
247
+ // Returns a map[uint64]string. Handles int and string typed values.
248
+ func (d *cborDecoder) decodeIntStringMap() (map[uint64]string, error) {
249
+ mt, count, err := d.decodeHead()
250
+ if err != nil {
251
+ return nil, err
252
+ }
253
+ if mt != cborMTMap {
254
+ return nil, fmt.Errorf("cbor: expected map (mt=5) got mt=%d", mt)
255
+ }
256
+
257
+ result := make(map[uint64]string, count)
258
+ for i := uint64(0); i < count; i++ {
259
+ // Decode key (must be uint)
260
+ kmt, kval, err := d.decodeHead()
261
+ if err != nil {
262
+ return nil, err
263
+ }
264
+ if kmt != cborMTUint {
265
+ return nil, fmt.Errorf("cbor: expected uint key got mt=%d", kmt)
266
+ }
267
+ // Decode value — strings or ints
268
+ vmt, vval, err := d.decodeHead()
269
+ if err != nil {
270
+ return nil, err
271
+ }
272
+ switch vmt {
273
+ case cborMTTstr:
274
+ s, err := d.readString(vval)
275
+ if err != nil {
276
+ return nil, err
277
+ }
278
+ result[kval] = s
279
+ case cborMTUint:
280
+ result[kval] = fmt.Sprintf("%d", vval)
281
+ case cborMTNint:
282
+ result[kval] = fmt.Sprintf("%d", -int64(1)-int64(vval))
283
+ default:
284
+ return nil, fmt.Errorf("cbor: unsupported value type mt=%d at key %d", vmt, kval)
285
+ }
286
+ }
287
+ return result, nil
288
+ }
289
+
290
+ // decodeIntMixedMap decodes a CBOR map with uint keys and mixed value types (string or int64).
291
+ func (d *cborDecoder) decodeIntMixedMap() (map[uint64]interface{}, error) {
292
+ mt, count, err := d.decodeHead()
293
+ if err != nil {
294
+ return nil, err
295
+ }
296
+ if mt != cborMTMap {
297
+ return nil, fmt.Errorf("cbor: expected map (mt=5) got mt=%d", mt)
298
+ }
299
+
300
+ result := make(map[uint64]interface{}, count)
301
+ for i := uint64(0); i < count; i++ {
302
+ kmt, kval, err := d.decodeHead()
303
+ if err != nil {
304
+ return nil, err
305
+ }
306
+ if kmt != cborMTUint {
307
+ return nil, fmt.Errorf("cbor: expected uint key got mt=%d", kmt)
308
+ }
309
+ vmt, vval, err := d.decodeHead()
310
+ if err != nil {
311
+ return nil, err
312
+ }
313
+ switch vmt {
314
+ case cborMTTstr:
315
+ s, err := d.readString(vval)
316
+ if err != nil {
317
+ return nil, err
318
+ }
319
+ result[kval] = s
320
+ case cborMTUint:
321
+ result[kval] = int64(vval)
322
+ case cborMTNint:
323
+ result[kval] = -int64(1) - int64(vval)
324
+ default:
325
+ return nil, fmt.Errorf("cbor: unsupported value type mt=%d at key %d", vmt, kval)
326
+ }
327
+ }
328
+ return result, nil
329
+ }
330
+
331
+ // decodeTopMap decodes the top-level envelope map (uint keys → sub-maps).
332
+ // Returns the raw offsets for each sub-map so we can decode them individually.
333
+ func (d *cborDecoder) decodeTopLevel() (header map[uint64]interface{}, body map[uint64]interface{}, sig map[uint64]string, err error) {
334
+ mt, count, err := d.decodeHead()
335
+ if err != nil {
336
+ return nil, nil, nil, err
337
+ }
338
+ if mt != cborMTMap {
339
+ return nil, nil, nil, fmt.Errorf("cbor: expected map at top level")
340
+ }
341
+
342
+ for i := uint64(0); i < count; i++ {
343
+ kmt, kval, err := d.decodeHead()
344
+ if err != nil {
345
+ return nil, nil, nil, err
346
+ }
347
+ if kmt != cborMTUint {
348
+ return nil, nil, nil, fmt.Errorf("cbor: expected uint key at top level")
349
+ }
350
+ switch kval {
351
+ case 1: // header
352
+ header, err = d.decodeIntMixedMap()
353
+ if err != nil {
354
+ return nil, nil, nil, fmt.Errorf("cbor: decode header: %w", err)
355
+ }
356
+ case 2: // body
357
+ body, err = d.decodeIntMixedMap()
358
+ if err != nil {
359
+ return nil, nil, nil, fmt.Errorf("cbor: decode body: %w", err)
360
+ }
361
+ case 3: // signature (optional)
362
+ sig, err = d.decodeIntStringMap()
363
+ if err != nil {
364
+ return nil, nil, nil, fmt.Errorf("cbor: decode signature: %w", err)
365
+ }
366
+ default:
367
+ return nil, nil, nil, fmt.Errorf("cbor: unknown top-level key %d", kval)
368
+ }
369
+ }
370
+ return header, body, sig, nil
371
+ }
372
+
373
+ // ─── public API ─────────────────────────────────────────────────────────────
374
+
375
+ // EncodeEnvelopeCBOR encodes a ProtocolEnvelope to CBOR bytes using numeric field keys.
376
+ func EncodeEnvelopeCBOR(env ProtocolEnvelope) ([]byte, error) {
377
+ e := &cborEncoder{}
378
+
379
+ // Count top-level entries: always header(1) + body(2), optionally sig(3)
380
+ topCount := 2
381
+ if env.Signature != nil {
382
+ topCount = 3
383
+ }
384
+ e.encodeHead(cborMTMap, uint64(topCount))
385
+
386
+ // Key 1: header
387
+ e.encodeHead(cborMTUint, 1)
388
+ {
389
+ headerCount := 6 // version, messageId, timestampMs, ttlMs, sender, nonce (always)
390
+ if env.Header.Recipient != "" {
391
+ headerCount = 7
392
+ }
393
+ headerPairs := []intKV{
394
+ {key: 1, keyEnc: encodedKeyForUint(1), value: env.Header.Version},
395
+ {key: 2, keyEnc: encodedKeyForUint(2), value: env.Header.MessageID},
396
+ {key: 3, keyEnc: encodedKeyForUint(3), value: env.Header.TimestampMs},
397
+ {key: 4, keyEnc: encodedKeyForUint(4), value: env.Header.TTLMs},
398
+ {key: 5, keyEnc: encodedKeyForUint(5), value: env.Header.Sender},
399
+ {key: 7, keyEnc: encodedKeyForUint(7), value: env.Header.Nonce},
400
+ }
401
+ if env.Header.Recipient != "" {
402
+ headerPairs = append(headerPairs, intKV{key: 6, keyEnc: encodedKeyForUint(6), value: env.Header.Recipient})
403
+ }
404
+ _ = headerCount
405
+ e.encodeIntMap(headerPairs)
406
+ }
407
+
408
+ // Key 2: body
409
+ e.encodeHead(cborMTUint, 2)
410
+ {
411
+ bodyPairs := []intKV{
412
+ {key: 1, keyEnc: encodedKeyForUint(1), value: env.Body.Intent},
413
+ {key: 2, keyEnc: encodedKeyForUint(2), value: env.Body.Content},
414
+ }
415
+ if env.Body.Capability != "" {
416
+ bodyPairs = append(bodyPairs, intKV{key: 3, keyEnc: encodedKeyForUint(3), value: env.Body.Capability})
417
+ }
418
+ if env.Body.CorrelationID != "" {
419
+ bodyPairs = append(bodyPairs, intKV{key: 4, keyEnc: encodedKeyForUint(4), value: env.Body.CorrelationID})
420
+ }
421
+ e.encodeIntMap(bodyPairs)
422
+ }
423
+
424
+ // Key 3: signature (optional)
425
+ if env.Signature != nil {
426
+ e.encodeHead(cborMTUint, 3)
427
+ sigPairs := []intKV{
428
+ {key: 1, keyEnc: encodedKeyForUint(1), value: env.Signature.Alg},
429
+ {key: 2, keyEnc: encodedKeyForUint(2), value: env.Signature.KeyID},
430
+ {key: 3, keyEnc: encodedKeyForUint(3), value: env.Signature.Value},
431
+ }
432
+ e.encodeIntMap(sigPairs)
433
+ }
434
+
435
+ return e.buf, nil
436
+ }
437
+
438
+ // DecodeEnvelopeCBOR decodes CBOR bytes into a ProtocolEnvelope.
439
+ func DecodeEnvelopeCBOR(data []byte) (ProtocolEnvelope, error) {
440
+ d := &cborDecoder{data: data}
441
+
442
+ header, body, sig, err := d.decodeTopLevel()
443
+ if err != nil {
444
+ return ProtocolEnvelope{}, fmt.Errorf("DecodeEnvelopeCBOR: %w", err)
445
+ }
446
+
447
+ if header == nil {
448
+ return ProtocolEnvelope{}, fmt.Errorf("DecodeEnvelopeCBOR: missing header (key 1)")
449
+ }
450
+ if body == nil {
451
+ return ProtocolEnvelope{}, fmt.Errorf("DecodeEnvelopeCBOR: missing body (key 2)")
452
+ }
453
+
454
+ getString := func(m map[uint64]interface{}, key uint64, field string) (string, error) {
455
+ v, ok := m[key]
456
+ if !ok {
457
+ return "", fmt.Errorf("DecodeEnvelopeCBOR: missing field %s (key %d)", field, key)
458
+ }
459
+ s, ok := v.(string)
460
+ if !ok {
461
+ return "", fmt.Errorf("DecodeEnvelopeCBOR: field %s (key %d) is not a string", field, key)
462
+ }
463
+ return s, nil
464
+ }
465
+
466
+ getInt64 := func(m map[uint64]interface{}, key uint64, field string) (int64, error) {
467
+ v, ok := m[key]
468
+ if !ok {
469
+ return 0, fmt.Errorf("DecodeEnvelopeCBOR: missing field %s (key %d)", field, key)
470
+ }
471
+ n, ok := v.(int64)
472
+ if !ok {
473
+ return 0, fmt.Errorf("DecodeEnvelopeCBOR: field %s (key %d) is not an int", field, key)
474
+ }
475
+ return n, nil
476
+ }
477
+
478
+ getOptString := func(m map[uint64]interface{}, key uint64) string {
479
+ v, ok := m[key]
480
+ if !ok {
481
+ return ""
482
+ }
483
+ s, _ := v.(string)
484
+ return s
485
+ }
486
+
487
+ version, err := getString(header, 1, "version")
488
+ if err != nil {
489
+ return ProtocolEnvelope{}, err
490
+ }
491
+ messageID, err := getString(header, 2, "messageId")
492
+ if err != nil {
493
+ return ProtocolEnvelope{}, err
494
+ }
495
+ timestampMs, err := getInt64(header, 3, "timestampMs")
496
+ if err != nil {
497
+ return ProtocolEnvelope{}, err
498
+ }
499
+ ttlMs, err := getInt64(header, 4, "ttlMs")
500
+ if err != nil {
501
+ return ProtocolEnvelope{}, err
502
+ }
503
+ sender, err := getString(header, 5, "sender")
504
+ if err != nil {
505
+ return ProtocolEnvelope{}, err
506
+ }
507
+ nonce, err := getString(header, 7, "nonce")
508
+ if err != nil {
509
+ return ProtocolEnvelope{}, err
510
+ }
511
+ recipient := getOptString(header, 6)
512
+
513
+ intent, err := getString(body, 1, "intent")
514
+ if err != nil {
515
+ return ProtocolEnvelope{}, err
516
+ }
517
+ content, err := getString(body, 2, "content")
518
+ if err != nil {
519
+ return ProtocolEnvelope{}, err
520
+ }
521
+ capability := getOptString(body, 3)
522
+ correlationID := getOptString(body, 4)
523
+
524
+ env := ProtocolEnvelope{
525
+ Header: ProtocolHeader{
526
+ Version: version,
527
+ MessageID: messageID,
528
+ TimestampMs: timestampMs,
529
+ TTLMs: ttlMs,
530
+ Sender: sender,
531
+ Recipient: recipient,
532
+ Nonce: nonce,
533
+ },
534
+ Body: ProtocolBody{
535
+ Intent: intent,
536
+ Content: content,
537
+ Capability: capability,
538
+ CorrelationID: correlationID,
539
+ },
540
+ }
541
+
542
+ if sig != nil {
543
+ env.Signature = &ProtocolSignature{
544
+ Alg: sig[1],
545
+ KeyID: sig[2],
546
+ Value: sig[3],
547
+ }
548
+ }
549
+
550
+ return env, nil
551
+ }