diff --git a/encoding/cid.go b/encoding/cid.go index a927dc5..20358b4 100644 --- a/encoding/cid.go +++ b/encoding/cid.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "git.lumeweb.com/LumeWeb/libs5-go/internal/bases" + "git.lumeweb.com/LumeWeb/libs5-go/serialize" "git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/utils" "github.com/vmihailenco/msgpack/v5" @@ -25,6 +26,7 @@ type CID struct { } var _ json.Marshaler = (*CID)(nil) +var _ json.Unmarshaler = (*CID)(nil) var _ msgpack.CustomEncoder = (*CID)(nil) var _ msgpack.CustomDecoder = (*CID)(nil) @@ -201,17 +203,32 @@ func (cid *CID) HashCode() int { int(fullBytes[3])<<24 } -func (cid CID) MarshalJSON() ([]byte, error) { - // Delegate to the MarshalJSON method of the encoder - return json.Marshal(cid.Multibase) +func (b CID) MarshalJSON() ([]byte, error) { + url, err := b.ToBase64Url() + if err != nil { + return nil, err + } + + return json.Marshal(url) } func (cid *CID) UnmarshalJSON(data []byte) error { - if err := json.Unmarshal(data, &cid.Multibase); err != nil { + decData, err := serialize.UnmarshalBase64UrlJSON(data) + + if err != nil { return err } + + decodedCid, err := CIDFromBytes(decData) + + if err != nil { + return err + } + + *cid = *decodedCid return nil } + func (cid CID) EncodeMsgpack(enc *msgpack.Encoder) error { return enc.EncodeBytes(cid.ToBytes()) } diff --git a/encoding/encrypted_cid.go b/encoding/encrypted_cid.go index 27725b5..0d990f3 100644 --- a/encoding/encrypted_cid.go +++ b/encoding/encrypted_cid.go @@ -1,7 +1,9 @@ package encoding import ( + "encoding/json" "errors" + "git.lumeweb.com/LumeWeb/libs5-go/serialize" "git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/utils" "github.com/vmihailenco/msgpack/v5" @@ -19,6 +21,8 @@ type EncryptedCID struct { var _ msgpack.CustomEncoder = (*EncryptedCID)(nil) var _ msgpack.CustomDecoder = (*EncryptedCID)(nil) +var _ json.Marshaler = (*EncryptedCID)(nil) +var _ json.Unmarshaler = (*EncryptedCID)(nil) func NewEncryptedCID(encryptedBlobHash Multihash, originalCID CID, encryptionKey []byte, padding uint32, chunkSizeAsPowerOf2 int, encryptionAlgorithm byte) *EncryptedCID { e := &EncryptedCID{ @@ -79,10 +83,36 @@ 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 (c EncryptedCID) EncodeMsgpack(enc *msgpack.Encoder) error { + return enc.EncodeBytes(c.ToBytes()) } -func (cid *EncryptedCID) DecodeMsgpack(dec *msgpack.Decoder) error { - return decodeMsgpackCID(cid, dec) +func (c *EncryptedCID) DecodeMsgpack(dec *msgpack.Decoder) error { + return decodeMsgpackCID(c, dec) +} +func (c EncryptedCID) MarshalJSON() ([]byte, error) { + str, err := c.ToString() + + if err != nil { + return nil, err + } + // Delegate to the MarshalJSON method of the encoder + return json.Marshal(str) +} + +func (c *EncryptedCID) UnmarshalJSON(data []byte) error { + decData, err := serialize.UnmarshalBase64UrlJSON(data) + + if err != nil { + return err + } + + decodedCid, err := EncryptedCIDFromBytes(decData) + + if err != nil { + return err + } + + *c = *decodedCid + return nil } diff --git a/encoding/multibase.go b/encoding/multibase.go index e868447..f0a11cd 100644 --- a/encoding/multibase.go +++ b/encoding/multibase.go @@ -1,11 +1,9 @@ package encoding import ( - "encoding/json" "errors" "git.lumeweb.com/LumeWeb/libs5-go/internal/bases" "github.com/multiformats/go-multibase" - "github.com/vmihailenco/msgpack/v5" ) var ( @@ -22,8 +20,6 @@ type multibaseImpl struct { encoder Encoder } -var _ json.Marshaler = (*multibaseImpl)(nil) - type Multibase interface { ToHex() (string, error) ToBase32() (string, error) @@ -33,7 +29,6 @@ type Multibase interface { } var _ Multibase = (*multibaseImpl)(nil) -var _ msgpack.CustomEncoder = (*multibaseImpl)(nil) func NewMultibase(encoder Encoder) Multibase { return &multibaseImpl{encoder: encoder} @@ -75,16 +70,3 @@ func (m *multibaseImpl) ToBase58() (string, error) { func (m *multibaseImpl) ToString() (string, error) { return m.ToBase58() } -func (b multibaseImpl) MarshalJSON() ([]byte, error) { - url, err := b.ToBase64Url() - if err != nil { - return nil, err - } - - return []byte(url), nil - -} - -func (b multibaseImpl) EncodeMsgpack(enc *msgpack.Encoder) error { - return enc.EncodeBytes(b.encoder.ToBytes()) -}