2024-01-06 14:45:00 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ed25519"
|
|
|
|
"fmt"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
2024-01-07 08:58:22 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
2024-01-06 14:45:00 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
2024-01-07 10:12:43 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
2024-01-07 09:30:03 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/storage"
|
2024-01-06 14:45:00 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/utils"
|
|
|
|
"github.com/emirpasic/gods/sets/hashset"
|
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2024-01-07 10:12:43 +00:00
|
|
|
var _ base.IncomingMessageTyped = (*StorageLocation)(nil)
|
2024-01-06 14:45:00 +00:00
|
|
|
|
|
|
|
type StorageLocation struct {
|
|
|
|
hash *encoding.Multihash
|
|
|
|
kind int
|
|
|
|
expiry int64
|
|
|
|
parts []string
|
|
|
|
publicKey []byte
|
|
|
|
signature []byte
|
|
|
|
|
2024-01-07 10:12:43 +00:00
|
|
|
base.IncomingMessageTypedImpl
|
|
|
|
base.IncomingMessageHandler
|
2024-01-06 14:45:00 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 18:58:11 +00:00
|
|
|
func NewStorageLocation() *StorageLocation {
|
|
|
|
return &StorageLocation{}
|
|
|
|
}
|
|
|
|
|
2024-01-06 14:45:00 +00:00
|
|
|
func (s *StorageLocation) DecodeMessage(dec *msgpack.Decoder) error {
|
2024-01-09 19:54:59 +00:00
|
|
|
// nop, we use the incoming message -> original already stored
|
2024-01-06 14:45:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-01-07 11:35:41 +00:00
|
|
|
func (s *StorageLocation) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
|
2024-01-09 19:54:59 +00:00
|
|
|
msg := s.IncomingMessage().Original()
|
|
|
|
|
|
|
|
hash := encoding.NewMultihash(msg[1:34]) // Replace NewMultihash with appropriate function
|
2024-01-06 14:45:00 +00:00
|
|
|
fmt.Println("Hash:", hash)
|
|
|
|
|
2024-01-09 19:54:59 +00:00
|
|
|
typeOfData := msg[34]
|
2024-01-06 14:45:00 +00:00
|
|
|
|
2024-01-09 19:54:59 +00:00
|
|
|
expiry := utils.DecodeEndian(msg[35:39])
|
2024-01-06 14:45:00 +00:00
|
|
|
|
2024-01-09 19:54:59 +00:00
|
|
|
partCount := msg[39]
|
2024-01-06 14:45:00 +00:00
|
|
|
|
|
|
|
parts := []string{}
|
|
|
|
cursor := 40
|
|
|
|
for i := 0; i < int(partCount); i++ {
|
2024-01-09 19:54:59 +00:00
|
|
|
length := utils.DecodeEndian(msg[cursor : cursor+2])
|
2024-01-06 14:45:00 +00:00
|
|
|
cursor += 2
|
2024-01-09 19:54:59 +00:00
|
|
|
part := string(msg[cursor : cursor+int(length)])
|
2024-01-06 14:45:00 +00:00
|
|
|
parts = append(parts, part)
|
|
|
|
cursor += int(length)
|
|
|
|
}
|
|
|
|
|
2024-01-09 19:54:59 +00:00
|
|
|
cursor++
|
|
|
|
|
|
|
|
publicKey := msg[cursor : cursor+33]
|
|
|
|
signature := msg[cursor+33:]
|
2024-01-06 14:45:00 +00:00
|
|
|
|
|
|
|
if types.HashType(publicKey[0]) != types.HashTypeEd25519 { // Replace CID_HASH_TYPES_ED25519 with actual constant
|
|
|
|
return fmt.Errorf("Unsupported public key type %d", publicKey[0])
|
|
|
|
}
|
|
|
|
|
2024-01-09 19:54:59 +00:00
|
|
|
if !ed25519.Verify(publicKey[1:], msg[:cursor], signature) {
|
2024-01-06 14:45:00 +00:00
|
|
|
return fmt.Errorf("Signature verification failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeId := encoding.NewNodeId(publicKey)
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
// Assuming `node` is an instance of your NodeImpl structure
|
2024-01-09 19:54:59 +00:00
|
|
|
err := node.AddStorageLocation(hash, nodeId, storage.NewStorageLocation(int(typeOfData), parts, int64(expiry)), msg, node.Config()) // Implement AddStorageLocation
|
2024-01-06 14:45:00 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to add storage location: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-01-08 17:40:40 +00:00
|
|
|
hashStr, err := hash.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-06 14:45:00 +00:00
|
|
|
var list *hashset.Set
|
2024-01-08 17:40:40 +00:00
|
|
|
listVal, ok := node.HashQueryRoutingTable().Get(hashStr) // Implement HashQueryRoutingTable method
|
2024-01-06 14:45:00 +00:00
|
|
|
if !ok {
|
|
|
|
list = hashset.New()
|
2024-01-08 17:40:40 +00:00
|
|
|
} else {
|
|
|
|
list = listVal.(*hashset.Set)
|
2024-01-06 14:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, peerIdVal := range list.Values() {
|
|
|
|
peerId := peerIdVal.(*encoding.NodeId)
|
|
|
|
|
|
|
|
if peerId.Equals(nodeId) || peerId.Equals(peer) {
|
|
|
|
continue
|
|
|
|
}
|
2024-01-08 17:40:40 +00:00
|
|
|
peerIdStr, err := peerId.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if peerVal, ok := node.Services().P2P().Peers().Get(peerIdStr); ok {
|
2024-01-06 14:45:00 +00:00
|
|
|
foundPeer := peerVal.(net.Peer)
|
2024-01-09 19:54:59 +00:00
|
|
|
err := foundPeer.SendMessage(msg)
|
2024-01-06 14:45:00 +00:00
|
|
|
if err != nil {
|
|
|
|
node.Logger().Error("Failed to send message", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node.HashQueryRoutingTable().Remove(hash.HashCode())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|