diff --git a/encoding/cid.go b/encoding/cid.go index ba2d56e..a927dc5 100644 --- a/encoding/cid.go +++ b/encoding/cid.go @@ -9,6 +9,7 @@ import ( "git.lumeweb.com/LumeWeb/libs5-go/internal/bases" "git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/utils" + "github.com/vmihailenco/msgpack/v5" ) var ( @@ -24,6 +25,8 @@ type CID struct { } var _ json.Marshaler = (*CID)(nil) +var _ msgpack.CustomEncoder = (*CID)(nil) +var _ msgpack.CustomDecoder = (*CID)(nil) func NewCID(Type types.CIDType, Hash Multihash, Size uint32) *CID { c := &CID{ @@ -209,10 +212,46 @@ func (cid *CID) UnmarshalJSON(data []byte) error { } return nil } +func (cid CID) EncodeMsgpack(enc *msgpack.Encoder) error { + return enc.EncodeBytes(cid.ToBytes()) +} + +func (cid *CID) DecodeMsgpack(dec *msgpack.Decoder) error { + return decodeMsgpackCID(cid, dec) +} + func CIDFromRegistryPublicKey(pubkey interface{}) (*CID, error) { return CIDFromHash(pubkey, 0, types.CIDTypeResolver) } +func decodeMsgpackCID(cid interface{}, dec *msgpack.Decoder) error { + byt, err := dec.DecodeBytes() + if err != nil { + return err + } + + switch v := cid.(type) { + case *CID: + cidInstance, err := CIDFromBytes(byt) + if err != nil { + return err + } + + *v = *cidInstance + case *EncryptedCID: + cidInstance, err := EncryptedCIDFromBytes(byt) + if err != nil { + return err + } + + *v = *cidInstance + default: + return errors.New("Unsupported type") + } + + return nil +} + func initCID(bytes []byte) (*CID, error) { if len(bytes) == 0 { return nil, errEmptyBytes diff --git a/encoding/encrypted_cid.go b/encoding/encrypted_cid.go index fd3614b..27725b5 100644 --- a/encoding/encrypted_cid.go +++ b/encoding/encrypted_cid.go @@ -4,6 +4,7 @@ import ( "errors" "git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/utils" + "github.com/vmihailenco/msgpack/v5" ) type EncryptedCID struct { @@ -16,6 +17,9 @@ type EncryptedCID struct { encryptionKey []byte } +var _ msgpack.CustomEncoder = (*EncryptedCID)(nil) +var _ msgpack.CustomDecoder = (*EncryptedCID)(nil) + func NewEncryptedCID(encryptedBlobHash Multihash, originalCID CID, encryptionKey []byte, padding uint32, chunkSizeAsPowerOf2 int, encryptionAlgorithm byte) *EncryptedCID { e := &EncryptedCID{ encryptedBlobHash: encryptedBlobHash, @@ -75,3 +79,10 @@ func (c *EncryptedCID) ToBytes() []byte { data = append(data, c.OriginalCID.ToBytes()...) return data } +func (cid EncryptedCID) EncodeMsgpack(enc *msgpack.Encoder) error { + return enc.EncodeBytes(cid.ToBytes()) +} + +func (cid *EncryptedCID) DecodeMsgpack(dec *msgpack.Decoder) error { + return decodeMsgpackCID(cid, dec) +} diff --git a/encoding/multibase.go b/encoding/multibase.go index 6ba4300..e868447 100644 --- a/encoding/multibase.go +++ b/encoding/multibase.go @@ -5,6 +5,7 @@ import ( "errors" "git.lumeweb.com/LumeWeb/libs5-go/internal/bases" "github.com/multiformats/go-multibase" + "github.com/vmihailenco/msgpack/v5" ) var ( @@ -32,6 +33,7 @@ type Multibase interface { } var _ Multibase = (*multibaseImpl)(nil) +var _ msgpack.CustomEncoder = (*multibaseImpl)(nil) func NewMultibase(encoder Encoder) Multibase { return &multibaseImpl{encoder: encoder} @@ -82,3 +84,7 @@ func (b multibaseImpl) MarshalJSON() ([]byte, error) { return []byte(url), nil } + +func (b multibaseImpl) EncodeMsgpack(enc *msgpack.Encoder) error { + return enc.EncodeBytes(b.encoder.ToBytes()) +}