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
package relay
import (
"cmp"
"errors"
"fmt"
"strings"
"time"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/fxamacker/cbor/v2"
"github.com/ipfs/go-cid"
"github.com/johnandersen777/socialweb-computer-kcp/internal/contract/events"
)
const cidTag = 42
type cborHeader struct {
Op int64 `cbor:"op"`
T string `cbor:"t"`
}
type cborError struct {
Error string `cbor:"error"`
Message string `cbor:"message"`
}
type cborOp struct {
Action string `cbor:"action"`
Path string `cbor:"path"`
CID *cbor.Tag `cbor:"cid"`
Prev *cbor.Tag `cbor:"prev"`
}
type cborCommit struct {
Seq int64 `cbor:"seq"`
Repo string `cbor:"repo"`
Commit *cbor.Tag `cbor:"commit"`
Rev string `cbor:"rev"`
Ops []cborOp `cbor:"ops"`
Time string `cbor:"time"`
}
func decodeCBOR(data []byte) (events.Event, bool, error) {
var header cborHeader
rest, err := cbor.UnmarshalFirst(data, &header)
if err != nil {
return events.Event{}, false, fmt.Errorf("relay: decoding a CBOR frame header: %w", err)
}
if header.Op < 0 {
var body cborError
_ = cbor.Unmarshal(rest, &body)
if strings.TrimSpace(body.Error) == futureCursor {
return events.Event{}, false, fmt.Errorf("relay: %w: %s", events.ErrFutureCursor, strings.TrimSpace(body.Message))
}
return events.Event{}, false, fmt.Errorf("relay: %s: %s", cmp.Or(strings.TrimSpace(body.Error), "StreamError"),
cmp.Or(strings.TrimSpace(body.Message), "stream error"))
}
if header.T != "" && header.T != "#commit" {
return events.Event{}, false, nil
}
var body cborCommit
if err := cbor.Unmarshal(rest, &body); err != nil {
return events.Event{}, false, fmt.Errorf("relay: decoding a CBOR commit: %w", err)
}
repo, err := syntax.ParseDID(strings.TrimSpace(body.Repo))
if err != nil {
return events.Event{}, false, fmt.Errorf("relay: a CBOR commit has an invalid repo: %w", err)
}
if body.Seq < 0 {
return events.Event{}, false, errors.New("relay: a CBOR commit has a negative seq")
}
out := events.Event{Seq: body.Seq, Repo: repo.String(), Rev: strings.TrimSpace(body.Rev)}
if out.Commit, err = cidString(body.Commit); err != nil {
return events.Event{}, false, err
}
for _, op := range body.Ops {
converted := events.Op{Action: op.Action, Path: op.Path}
if converted.CID, err = cidString(op.CID); err != nil {
return events.Event{}, false, err
}
if converted.Prev, err = cidString(op.Prev); err != nil {
return events.Event{}, false, err
}
out.Ops = append(out.Ops, converted)
}
if raw := strings.TrimSpace(body.Time); raw != "" {
if out.Time, err = time.Parse(time.RFC3339Nano, raw); err != nil {
return events.Event{}, false, fmt.Errorf("relay: a CBOR commit has an invalid time: %w", err)
}
}
return out, true, nil
}
func cidString(tag *cbor.Tag) (string, error) {
if tag == nil {
return "", nil
}
raw, ok := tag.Content.([]byte)
if tag.Number != cidTag || !ok || len(raw) < 2 || raw[0] != 0 {
return "", errors.New("relay: a CBOR frame carries an invalid cid")
}
parsed, err := cid.Cast(raw[1:])
if err != nil || !parsed.Defined() {
return "", errors.New("relay: a CBOR frame carries an invalid cid")
}
return parsed.String(), nil
}