feat: add msfpack marshal support to encoding

This commit is contained in:
Derrick Hammer 2024-01-04 07:44:09 -05:00
parent 4457dff415
commit 05fb104990
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 56 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
"git.lumeweb.com/LumeWeb/libs5-go/internal/bases" "git.lumeweb.com/LumeWeb/libs5-go/internal/bases"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"git.lumeweb.com/LumeWeb/libs5-go/utils" "git.lumeweb.com/LumeWeb/libs5-go/utils"
"github.com/vmihailenco/msgpack/v5"
) )
var ( var (
@ -24,6 +25,8 @@ type CID struct {
} }
var _ json.Marshaler = (*CID)(nil) 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 { func NewCID(Type types.CIDType, Hash Multihash, Size uint32) *CID {
c := &CID{ c := &CID{
@ -209,10 +212,46 @@ func (cid *CID) UnmarshalJSON(data []byte) error {
} }
return nil 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) { func CIDFromRegistryPublicKey(pubkey interface{}) (*CID, error) {
return CIDFromHash(pubkey, 0, types.CIDTypeResolver) 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) { func initCID(bytes []byte) (*CID, error) {
if len(bytes) == 0 { if len(bytes) == 0 {
return nil, errEmptyBytes return nil, errEmptyBytes

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"git.lumeweb.com/LumeWeb/libs5-go/utils" "git.lumeweb.com/LumeWeb/libs5-go/utils"
"github.com/vmihailenco/msgpack/v5"
) )
type EncryptedCID struct { type EncryptedCID struct {
@ -16,6 +17,9 @@ type EncryptedCID struct {
encryptionKey []byte 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 { func NewEncryptedCID(encryptedBlobHash Multihash, originalCID CID, encryptionKey []byte, padding uint32, chunkSizeAsPowerOf2 int, encryptionAlgorithm byte) *EncryptedCID {
e := &EncryptedCID{ e := &EncryptedCID{
encryptedBlobHash: encryptedBlobHash, encryptedBlobHash: encryptedBlobHash,
@ -75,3 +79,10 @@ func (c *EncryptedCID) ToBytes() []byte {
data = append(data, c.OriginalCID.ToBytes()...) data = append(data, c.OriginalCID.ToBytes()...)
return data 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)
}

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"git.lumeweb.com/LumeWeb/libs5-go/internal/bases" "git.lumeweb.com/LumeWeb/libs5-go/internal/bases"
"github.com/multiformats/go-multibase" "github.com/multiformats/go-multibase"
"github.com/vmihailenco/msgpack/v5"
) )
var ( var (
@ -32,6 +33,7 @@ type Multibase interface {
} }
var _ Multibase = (*multibaseImpl)(nil) var _ Multibase = (*multibaseImpl)(nil)
var _ msgpack.CustomEncoder = (*multibaseImpl)(nil)
func NewMultibase(encoder Encoder) Multibase { func NewMultibase(encoder Encoder) Multibase {
return &multibaseImpl{encoder: encoder} return &multibaseImpl{encoder: encoder}
@ -82,3 +84,7 @@ func (b multibaseImpl) MarshalJSON() ([]byte, error) {
return []byte(url), nil return []byte(url), nil
} }
func (b multibaseImpl) EncodeMsgpack(enc *msgpack.Encoder) error {
return enc.EncodeBytes(b.encoder.ToBytes())
}