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
package market
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net/url"
"strings"
"github.com/bluesky-social/indigo/atproto/syntax"
)
func validateSignatures(sigs Signatures) error {
if len(sigs) == 0 {
return fmt.Errorf("%w: missing signatures", ErrInvalidRecord)
}
for _, sig := range sigs {
if err := sig.validate(); err != nil {
return err
}
}
return nil
}
func (s Signature) validate() error {
switch {
case s.AttestedSignature != nil:
return validateInlineSignature(s.AttestedSignature.Key, true, s.AttestedSignature.CID)
case s.StrongRef != nil:
_, err := ParseRecordRef(s.StrongRef.URI, s.StrongRef.CID)
return err
}
var entry struct {
Key *string `json:"key"`
Signature json.RawMessage `json:"signature"`
URI *string `json:"uri"`
CID *string `json:"cid"`
}
if err := json.Unmarshal(s.Unknown, &entry); err != nil {
return fmt.Errorf("%w: signature entry is not an object", ErrInvalidRecord)
}
if entry.Key != nil {
var raw any
_ = json.Unmarshal(entry.Signature, &raw)
_, ok := normalizeSignatureBytes(raw)
return validateInlineSignature(*entry.Key, ok, deref(entry.CID))
}
if entry.URI == nil || entry.CID == nil {
return fmt.Errorf("%w: signature entry is neither inline nor a strongRef", ErrInvalidRecord)
}
_, err := ParseRecordRef(*entry.URI, *entry.CID)
return err
}
func validateInlineSignature(key string, hasBytes bool, cidStr string) error {
if key == "" {
return fmt.Errorf("%w: signature missing key", ErrInvalidRecord)
}
if !hasBytes {
return fmt.Errorf("%w: signature missing signature bytes", ErrInvalidRecord)
}
if cidStr == "" || !IsAttestationCIDString(cidStr) {
return fmt.Errorf("%w: signature missing valid cid", ErrInvalidRecord)
}
return nil
}
func deref(s *string) string {
if s == nil {
return ""
}
return *s
}
func validateOptionalDatetime(value, field string) error {
if value == "" {
return nil
}
if _, err := syntax.ParseDatetime(value); err != nil {
return fmt.Errorf("%w: %s: %v", ErrInvalidRecord, field, err)
}
return nil
}
func validateRefCollection(ref StrongRef, field, collection string) error {
if err := ref.Validate(); err != nil {
return err
}
if NSIDFromURI(ref.URI) != collection {
return fmt.Errorf("%w: %s is not a %s ref", ErrInvalidRecord, field, collection)
}
return nil
}
func validateOptionalRef(ref *StrongRef) error {
if ref == nil {
return nil
}
return ref.Validate()
}
func validateOptionalServiceRef(value string) error {
if value == "" {
return nil
}
return ValidServiceRef(value)
}
func validateIngressRef(ref *StrongRef) error {
if ref == nil {
return nil
}
if err := ref.Validate(); err != nil {
return err
}
if NSIDFromURI(ref.URI) != IngressWebRTCNSID {
return fmt.Errorf("%w: ingress ref %q is not %s", ErrInvalidRecord, ref.URI, IngressWebRTCNSID)
}
return nil
}
func validateIngressList(refs []StrongRef) error {
for i := range refs {
if err := validateIngressRef(&refs[i]); err != nil {
return err
}
}
return nil
}
func (r *IngressWebRTC) Validate() error {
if r == nil {
return fmt.Errorf("%w: the ingress webRTC record is empty", ErrInvalidRecord)
}
if r.Kind != "webrtc" {
return fmt.Errorf("%w: ingress kind %q", ErrInvalidRecord, r.Kind)
}
switch r.Mode {
case IngressModeListen:
if r.Peer != "" || r.Session != "" || r.Sdp != "" || r.Descriptor != nil {
return fmt.Errorf("%w: a listen ingress names no peer, session, sdp or descriptor", ErrInvalidRecord)
}
case IngressModeOffer, IngressModeAnswer:
if r.Peer == "" || r.Session == "" || r.Sdp == "" {
return fmt.Errorf("%w: an ingress %s needs peer, session and sdp", ErrInvalidRecord, r.Mode)
}
default:
return fmt.Errorf("%w: ingress mode %q", ErrInvalidRecord, r.Mode)
}
if r.ID == "" {
return fmt.Errorf("%w: ingress id is empty", ErrInvalidRecord)
}
if err := validateOptionalDatetime(r.CreatedAt, "createdAt"); err != nil {
return err
}
for _, raw := range r.StunUrls {
if strings.TrimSpace(raw) == "" {
return fmt.Errorf("%w: ingress stunUrls has an empty entry", ErrInvalidRecord)
}
}
if err := validateIngressRef(r.Descriptor); err != nil {
return err
}
return nil
}
func validateAttestationCID(value, what string) error {
if value == "" || !IsAttestationCIDString(value) {
return fmt.Errorf("%w: %s cid is not an attestation cid", ErrInvalidRecord, what)
}
return nil
}
func validateRelayURLs(relays []string) error {
if len(relays) == 0 {
return fmt.Errorf("%w: relays is empty", ErrInvalidRecord)
}
for _, raw := range relays {
u, err := url.Parse(raw)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
return fmt.Errorf("%w: relay url %q", ErrInvalidRecord, raw)
}
}
return nil
}
func validateAppliesTo(appliesTo []string) error {
if len(appliesTo) == 0 {
return fmt.Errorf("%w: offering missing appliesTo", ErrInvalidRecord)
}
for _, nsid := range appliesTo {
if _, err := syntax.ParseNSID(nsid); err != nil {
return fmt.Errorf("%w: appliesTo: %v", ErrInvalidRecord, err)
}
}
return nil
}
func validateRecordURI(value, what string) error {
aturi, err := syntax.ParseATURI(value)
if err != nil || aturi.Collection() == "" || aturi.RecordKey() == "" {
return fmt.Errorf("%w: %s is not a record AT URI", ErrInvalidRecord, what)
}
return nil
}
func (r *RFP) Validate() error {
if err := r.Payload.Validate(); err != nil {
return err
}
for _, p := range r.Policies {
if err := p.Validate(); err != nil {
return err
}
}
if err := validateOptionalServiceRef(r.SubmitBid); err != nil {
return err
}
if err := validateIngressList(r.Ingress); err != nil {
return err
}
if err := validateOptionalDatetime(r.CreatedAt, "createdAt"); err != nil {
return err
}
return validateSignatures(r.Signatures)
}
func (r *Bid) Validate() error {
if err := validateRefCollection(r.RFP, "rfp", RFPNSID); err != nil {
return err
}
if err := r.Payload.Validate(); err != nil {
return err
}
if err := validateOptionalRef(r.Config); err != nil {
return err
}
if err := validateOptionalServiceRef(r.SubmitAccept); err != nil {
return err
}
if err := validateIngressList(r.Ingress); err != nil {
return err
}
if err := validateOptionalDatetime(r.CreatedAt, "createdAt"); err != nil {
return err
}
return validateSignatures(r.Signatures)
}
func (r *Accept) Validate() error {
if err := validateRefCollection(r.RFP, "rfp", RFPNSID); err != nil {
return err
}
if err := validateRefCollection(r.Bid, "bid", BidNSID); err != nil {
return err
}
if err := validateOptionalRef(r.Payload); err != nil {
return err
}
if err := validateOptionalServiceRef(r.SubmitEvent); err != nil {
return err
}
if err := validateIngressList(r.Ingress); err != nil {
return err
}
if err := validateOptionalDatetime(r.CreatedAt, "createdAt"); err != nil {
return err
}
return validateSignatures(r.Signatures)
}
func (r *Receipt) Validate() error {
if r.RFP.URI != "" || r.RFP.CID != "" {
if err := validateRefCollection(r.RFP, "rfp", RFPNSID); err != nil {
return err
}
}
if r.Bid.URI != "" || r.Bid.CID != "" {
if err := validateRefCollection(r.Bid, "bid", BidNSID); err != nil {
return err
}
}
if err := validateRefCollection(r.Accept, "accept", AcceptNSID); err != nil {
return err
}
if err := validateOptionalRef(r.Payload); err != nil {
return err
}
if err := validateOptionalServiceRef(r.SubmitEvent); err != nil {
return err
}
if err := validateIngressList(r.Ingress); err != nil {
return err
}
if err := validateAttestationCID(r.CID, "receipt"); err != nil {
return err
}
if err := validateOptionalDatetime(r.CreatedAt, "createdAt"); err != nil {
return err
}
return validateSignatures(r.Signatures)
}
func (r *Event) Validate() error {
if err := validateRefCollection(r.Receipt, "receipt", ReceiptNSID); err != nil {
return err
}
if err := r.Payload.Validate(); err != nil {
return err
}
if err := validateOptionalDatetime(r.CreatedAt, "createdAt"); err != nil {
return err
}
return validateSignatures(r.Signatures)
}
func (r *Offering) Validate() error {
r.EndpointURL = strings.TrimSpace(r.EndpointURL)
if err := validateOptionalServiceRef(r.EndpointURL); err != nil {
return err
}
if r.WithdrawnAt == "" || len(r.AppliesTo) > 0 {
if err := validateAppliesTo(r.AppliesTo); err != nil {
return err
}
}
if err := validateIngressList(r.Ingress); err != nil {
return err
}
if r.CreatedAt == "" {
return fmt.Errorf("%w: offering missing createdAt", ErrInvalidRecord)
}
if err := validateOptionalDatetime(r.CreatedAt, "createdAt"); err != nil {
return err
}
if err := validateOptionalDatetime(r.WithdrawnAt, "withdrawnAt"); err != nil {
return err
}
return validateOptionalDatetime(r.RefreshedAt, "refreshedAt")
}
func (r *Heartbeat) Validate() error {
if err := validateRecordURI(r.Offering, "heartbeat offering"); err != nil {
return err
}
if r.CreatedAt == "" {
return fmt.Errorf("%w: heartbeat missing createdAt", ErrInvalidRecord)
}
return validateOptionalDatetime(r.CreatedAt, "createdAt")
}
func (r *Relays) Validate() error {
if err := validateRelayURLs(r.Relays); err != nil {
return err
}
if r.CreatedAt == "" {
return fmt.Errorf("%w: relays missing createdAt", ErrInvalidRecord)
}
return validateOptionalDatetime(r.CreatedAt, "createdAt")
}
func (r *BadgeBlueKeys) Validate() error {
if r.KeyID == "" || r.Challenge == "" || r.CreatedAt == "" {
return fmt.Errorf("%w: badgeBlueKeys missing required fields", ErrInvalidRecord)
}
if _, err := syntax.ParseDID(r.Challenge); err != nil {
return fmt.Errorf("%w: challenge: %v", ErrInvalidRecord, err)
}
return validateOptionalDatetime(r.CreatedAt, "createdAt")
}
func (r *BidsFree) Validate() error {
return validateSignatures(r.Signatures)
}
func (r *BidsX402) Validate() error {
if _, missing := r.absent["cost"]; missing {
return fmt.Errorf("%w: bids.x402 missing cost", ErrInvalidRecord)
}
if r.Currency == "" || r.Frequency == "" || r.URL == "" {
return fmt.Errorf("%w: bids.x402 missing required fields", ErrInvalidRecord)
}
return validateSignatures(r.Signatures)
}
func validateAccepts(bid StrongRef, payload *StrongRef, sigs Signatures) error {
if err := validateRefCollection(bid, "bid", BidNSID); err != nil {
return err
}
if err := validateOptionalRef(payload); err != nil {
return err
}
return validateSignatures(sigs)
}
func (r *AcceptsFree) Validate() error {
return validateAccepts(r.Bid, r.Payload, r.Signatures)
}
func (r *AcceptsX402) Validate() error {
return validateAccepts(r.Bid, r.Payload, r.Signatures)
}
func validateReceipts(accept StrongRef, cidStr string, sigs Signatures) error {
if err := validateRefCollection(accept, "accept", AcceptNSID); err != nil {
return err
}
if err := validateAttestationCID(cidStr, "receipts"); err != nil {
return err
}
return validateSignatures(sigs)
}
func (r *ReceiptsFree) Validate() error {
return validateReceipts(r.Accept, r.CID, r.Signatures)
}
func (r *ReceiptsX402) Validate() error {
return validateReceipts(r.Accept, r.CID, r.Signatures)
}
func strongRef(ref RecordRef) (StrongRef, error) {
if _, err := ParseRecordRef(ref.URI, ref.CID); err != nil {
return StrongRef{}, err
}
return NewStrongRef(ref.URI, ref.CID), nil
}
func optionalStrongRef(ref *RecordRef) (*StrongRef, error) {
if ref == nil {
return nil, nil
}
out, err := strongRef(*ref)
if err != nil {
return nil, err
}
return &out, nil
}
func optionalStrongRefs(refs []RecordRef) ([]StrongRef, error) {
if len(refs) == 0 {
return nil, nil
}
out := make([]StrongRef, 0, len(refs))
for _, ref := range refs {
value, err := strongRef(ref)
if err != nil {
return nil, err
}
out = append(out, value)
}
return out, nil
}
func isoOrNow(value string) string {
if value == "" {
return NowISO()
}
return value
}
func createSigned(ctx context.Context, w RecordWriter, repo, rkey string, record Record, signer *Signer) (SignedRecord, error) {
value, err := EncodeRecord(record)
if err != nil {
return SignedRecord{}, err
}
if rkey == "" {
return CreateSignedRecord(ctx, w, repo, record.NSID(), value, signer)
}
return CreateSignedRecordAt(ctx, w, repo, record.NSID(), rkey, value, signer)
}
type RFPOptions struct {
Payload RecordRef
SubmitBid string
Ingress []RecordRef
Domain string
Policies []RecordRef
CreatedAt string
}
func BuildRFP(opts RFPOptions) (RFP, error) {
payload, err := strongRef(opts.Payload)
if err != nil {
return RFP{}, err
}
if err := validateOptionalServiceRef(opts.SubmitBid); err != nil {
return RFP{}, err
}
ingress, err := optionalStrongRefs(opts.Ingress)
if err != nil {
return RFP{}, err
}
out := RFP{Payload: payload, Domain: opts.Domain, SubmitBid: opts.SubmitBid, Ingress: ingress, CreatedAt: isoOrNow(opts.CreatedAt)}
for _, p := range opts.Policies {
ref, err := strongRef(p)
if err != nil {
return RFP{}, err
}
out.Policies = append(out.Policies, ref)
}
return out, nil
}
func CreateRFP(ctx context.Context, w RecordWriter, repo string, opts RFPOptions, signer *Signer) (SignedRecord, error) {
return CreateRFPAt(ctx, w, repo, "", opts, signer)
}
func CreateRFPAt(ctx context.Context, w RecordWriter, repo, rkey string, opts RFPOptions, signer *Signer) (SignedRecord, error) {
record, err := BuildRFP(opts)
if err != nil {
return SignedRecord{}, err
}
return createSigned(ctx, w, repo, rkey, record, signer)
}
type BidOptions struct {
RFP RecordRef
Config *RecordRef
Payload RecordRef
SubmitAccept string
Ingress []RecordRef
CreatedAt string
}
func BuildBid(opts BidOptions) (Bid, error) {
rfp, err := strongRef(opts.RFP)
if err != nil {
return Bid{}, err
}
payload, err := strongRef(opts.Payload)
if err != nil {
return Bid{}, err
}
config, err := optionalStrongRef(opts.Config)
if err != nil {
return Bid{}, err
}
ingress, err := optionalStrongRefs(opts.Ingress)
if err != nil {
return Bid{}, err
}
if err := validateOptionalServiceRef(opts.SubmitAccept); err != nil {
return Bid{}, err
}
return Bid{RFP: rfp, Config: config, Payload: payload, SubmitAccept: opts.SubmitAccept, Ingress: ingress, CreatedAt: isoOrNow(opts.CreatedAt)}, nil
}
func BidRKey(bidderDID, rfpURI, rfpCID string) string {
sum := sha256.Sum256([]byte(bidderDID + "\x00" + rfpURI + "#" + rfpCID))
return "bid-" + hex.EncodeToString(sum[:])[:20]
}
func TerminationKey(receiptURI string) string {
return "terminated:" + receiptURI
}
func EventRKeys(key string) (string, string) {
prefix := eventRKeyPrefix(key)
payload := sha256.Sum256([]byte(key))
event := sha256.Sum256([]byte("event:" + key))
return prefix + "-payload-" + hex.EncodeToString(payload[:16]), prefix + "-event-" + hex.EncodeToString(event[:16])
}
func eventRKeyPrefix(key string) string {
prefix := strings.TrimSpace(key)
if i := strings.IndexByte(prefix, ':'); i >= 0 {
prefix = prefix[:i]
}
var clean strings.Builder
for _, r := range strings.ToLower(prefix) {
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') {
clean.WriteRune(r)
}
}
switch prefix = clean.String(); prefix {
case "":
return "market"
case "terminated":
return "termination"
}
return prefix
}
const sessionRKeyAlphabet = "234567abcdefghijklmnopqrstuvwxyz"
func SessionRKey(label string, parts ...string) string {
h := sha256.New()
h.Write([]byte(label))
for _, part := range parts {
h.Write([]byte{0})
h.Write([]byte(part))
}
sum := h.Sum(nil)
out := make([]byte, 13)
for i := range out {
out[i] = sessionRKeyAlphabet[int(sum[i])%len(sessionRKeyAlphabet)]
}
return string(out)
}
func ReleasePayloadRKey(receiptURI string) string {
return SessionRKey("delete", receiptURI)
}
func RBACRKey(acceptRKey string) string {
if acceptRKey == "" {
return ""
}
return "node." + acceptRKey
}
func RBACRKeyOfAccept(acceptURI string) string {
if acceptURI == "" {
return ""
}
return RBACRKey(string(syntax.ATURI(acceptURI).RecordKey()))
}
func CreateBid(ctx context.Context, w RecordWriter, repo string, opts BidOptions, signer *Signer) (SignedRecord, error) {
record, err := BuildBid(opts)
if err != nil {
return SignedRecord{}, err
}
return createSigned(ctx, w, repo, "", record, signer)
}
type AcceptOptions struct {
RFP RecordRef
Bid RecordRef
Payload *RecordRef
SubmitEvent string
Ingress []RecordRef
CreatedAt string
}
func BuildAccept(opts AcceptOptions) (Accept, error) {
rfp, err := strongRef(opts.RFP)
if err != nil {
return Accept{}, err
}
bid, err := strongRef(opts.Bid)
if err != nil {
return Accept{}, err
}
payload, err := optionalStrongRef(opts.Payload)
if err != nil {
return Accept{}, err
}
ingress, err := optionalStrongRefs(opts.Ingress)
if err != nil {
return Accept{}, err
}
if err := validateOptionalServiceRef(opts.SubmitEvent); err != nil {
return Accept{}, err
}
return Accept{RFP: rfp, Bid: bid, Payload: payload, SubmitEvent: opts.SubmitEvent, Ingress: ingress, CreatedAt: isoOrNow(opts.CreatedAt)}, nil
}
func CreateAcceptAt(ctx context.Context, w RecordWriter, repo, rkey string, opts AcceptOptions, signer *Signer) (SignedRecord, error) {
record, err := BuildAccept(opts)
if err != nil {
return SignedRecord{}, err
}
return createSigned(ctx, w, repo, rkey, record, signer)
}
type ReceiptRefs struct {
RFP RecordRef
Bid RecordRef
Accept RecordRef
Payload *RecordRef
SubmitEvent string
Ingress []RecordRef
}
func BuildReceipt(refs ReceiptRefs, createdAt string) (Receipt, error) {
rfp, err := strongRef(refs.RFP)
if err != nil {
return Receipt{}, err
}
bid, err := strongRef(refs.Bid)
if err != nil {
return Receipt{}, err
}
accept, err := strongRef(refs.Accept)
if err != nil {
return Receipt{}, err
}
payload, err := optionalStrongRef(refs.Payload)
if err != nil {
return Receipt{}, err
}
ingress, err := optionalStrongRefs(refs.Ingress)
if err != nil {
return Receipt{}, err
}
if err := validateOptionalServiceRef(refs.SubmitEvent); err != nil {
return Receipt{}, err
}
out := Receipt{RFP: rfp, Bid: bid, Accept: accept, Payload: payload, SubmitEvent: refs.SubmitEvent, Ingress: ingress, CreatedAt: isoOrNow(createdAt)}
if payload == nil {
out.Extra = map[string]json.RawMessage{"payload": json.RawMessage("null")}
}
return out, nil
}
type EventOptions struct {
Receipt RecordRef
Payload RecordRef
CreatedAt string
}
func BuildEvent(opts EventOptions) (Event, error) {
receipt, err := strongRef(opts.Receipt)
if err != nil {
return Event{}, err
}
payload, err := strongRef(opts.Payload)
if err != nil {
return Event{}, err
}
return Event{Receipt: receipt, Payload: payload, CreatedAt: opts.CreatedAt}, nil
}
func PutEvent(ctx context.Context, w RecordWriter, repo, rkey string, opts EventOptions, signer *Signer) (SignedRecord, error) {
record, err := BuildEvent(opts)
if err != nil {
return SignedRecord{}, err
}
value, err := EncodeRecord(record)
if err != nil {
return SignedRecord{}, err
}
signed, err := SignRecord(value, repo, signer)
if err != nil {
return SignedRecord{}, err
}
ref, err := w.PutRecord(ctx, repo, EventNSID, rkey, signed)
if err != nil {
return SignedRecord{}, err
}
return SignedRecord{URI: ref.URI, CID: ref.CID, Record: signed}, nil
}
func BuildOffering(endpointURL string, appliesTo []string, ingress []RecordRef, createdAt, refreshedAt string) (Offering, error) {
endpointURL = strings.TrimSpace(endpointURL)
if err := validateOptionalServiceRef(endpointURL); err != nil {
return Offering{}, err
}
if err := validateAppliesTo(appliesTo); err != nil {
return Offering{}, err
}
records, err := optionalStrongRefs(ingress)
if err != nil {
return Offering{}, err
}
return Offering{EndpointURL: endpointURL, AppliesTo: appliesTo, Ingress: records, CreatedAt: isoOrNow(createdAt), RefreshedAt: refreshedAt}, nil
}
func BuildWithdrawnOffering(createdAt, withdrawnAt string) (Offering, error) {
withdrawnAt = isoOrNow(withdrawnAt)
if err := validateOptionalDatetime(withdrawnAt, "withdrawnAt"); err != nil {
return Offering{}, err
}
return Offering{
AppliesTo: []string{},
CreatedAt: isoOrNow(createdAt),
RefreshedAt: withdrawnAt,
WithdrawnAt: withdrawnAt,
Extra: map[string]json.RawMessage{"appliesTo": json.RawMessage("[]")},
}, nil
}
func OfferingWithdrawn(offering Offering) bool {
return offering.WithdrawnAt != "" && len(offering.AppliesTo) == 0
}
func BuildHeartbeat(offeringURI, createdAt string) (Heartbeat, error) {
if err := validateRecordURI(offeringURI, "heartbeat offering"); err != nil {
return Heartbeat{}, err
}
return Heartbeat{Offering: offeringURI, CreatedAt: isoOrNow(createdAt)}, nil
}