summaryrefslogtreecommitdiffstats
path: root/katzenpost/dispatcher/codec.go
diff options
context:
space:
mode:
Diffstat (limited to 'katzenpost/dispatcher/codec.go')
-rw-r--r--katzenpost/dispatcher/codec.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/katzenpost/dispatcher/codec.go b/katzenpost/dispatcher/codec.go
new file mode 100644
index 0000000..3053dde
--- /dev/null
+++ b/katzenpost/dispatcher/codec.go
@@ -0,0 +1,29 @@
+package dispatcher
+
+import (
+ "fmt"
+
+ "github.com/fxamacker/cbor/v2"
+)
+
+func EncodeEnvelope(envelope Envelope) ([]byte, error) {
+ encoded, err := cbor.Marshal(envelope)
+ if err != nil {
+ return nil, fmt.Errorf("encode envelope: %w", err)
+ }
+ if len(encoded) > MaxEnvelopeBytes {
+ return nil, fmt.Errorf("encoded envelope is too large")
+ }
+ return encoded, nil
+}
+
+func DecodeEnvelope(encoded []byte) (Envelope, error) {
+ if len(encoded) == 0 || len(encoded) > MaxEnvelopeBytes {
+ return Envelope{}, fmt.Errorf("invalid encoded envelope size")
+ }
+ var envelope Envelope
+ if err := cbor.Unmarshal(encoded, &envelope); err != nil {
+ return Envelope{}, fmt.Errorf("decode envelope: %w", err)
+ }
+ return envelope, nil
+}