2024-01-10 11:21:03 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2024-03-11 15:50:27 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/internal/bases"
|
2024-01-10 11:21:03 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
2024-03-11 15:50:27 +00:00
|
|
|
"go.uber.org/zap"
|
2024-01-10 11:21:03 +00:00
|
|
|
)
|
|
|
|
|
2024-01-30 03:25:21 +00:00
|
|
|
var _ IncomingMessage = (*RegistryEntryRequest)(nil)
|
|
|
|
var _ EncodeableMessage = (*RegistryEntryRequest)(nil)
|
2024-01-10 11:21:03 +00:00
|
|
|
|
|
|
|
type RegistryEntryRequest struct {
|
2024-01-29 04:59:43 +00:00
|
|
|
sre SignedRegistryEntry
|
2024-01-30 03:25:21 +00:00
|
|
|
HandshakeRequirement
|
2024-01-10 11:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewEmptyRegistryEntryRequest() *RegistryEntryRequest {
|
2024-01-13 16:22:01 +00:00
|
|
|
rer := &RegistryEntryRequest{}
|
|
|
|
|
|
|
|
rer.SetRequiresHandshake(true)
|
|
|
|
|
|
|
|
return rer
|
2024-01-10 11:21:03 +00:00
|
|
|
}
|
2024-01-29 04:59:43 +00:00
|
|
|
func NewRegistryEntryRequest(sre SignedRegistryEntry) *RegistryEntryRequest {
|
2024-01-10 11:21:03 +00:00
|
|
|
return &RegistryEntryRequest{sre: sre}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RegistryEntryRequest) EncodeMsgpack(enc *msgpack.Encoder) error {
|
|
|
|
err := enc.EncodeInt(int64(types.RecordTypeRegistryEntry))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = enc.EncodeBytes(MarshalSignedRegistryEntry(s.sre))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-30 03:25:21 +00:00
|
|
|
func (s *RegistryEntryRequest) DecodeMessage(dec *msgpack.Decoder, message IncomingMessageData) error {
|
2024-03-03 17:25:27 +00:00
|
|
|
sre, err := UnmarshalSignedRegistryEntry(message.Original)
|
2024-01-10 11:21:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.sre = sre
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-30 03:25:21 +00:00
|
|
|
func (s *RegistryEntryRequest) HandleMessage(message IncomingMessageData) error {
|
2024-03-11 15:50:27 +00:00
|
|
|
entry, err := encoding.CIDFromRegistryPublicKey(s.sre.PK())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pid, err := message.Peer.Id().ToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b64, err := bases.ToBase64Url(s.sre.PK())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
message.Logger.Debug("Handling registry entry set request", zap.Any("entryCID", entry), zap.Any("entryBase64", b64), zap.Any("peer", pid))
|
2024-01-30 05:31:31 +00:00
|
|
|
return message.Mediator.RegistrySet(s.sre, false, message.Peer)
|
2024-01-10 11:21:03 +00:00
|
|
|
}
|