From 1e7baabcb33cc54470664e4d0a33ac946df5e9b4 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 9 Jan 2024 14:54:59 -0500 Subject: [PATCH] fix: switch to using the originally stored message since we need everything to do message verification --- protocol/storage_location.go | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/protocol/storage_location.go b/protocol/storage_location.go index ab9a2d7..cd924aa 100644 --- a/protocol/storage_location.go +++ b/protocol/storage_location.go @@ -18,7 +18,6 @@ import ( var _ base.IncomingMessageTyped = (*StorageLocation)(nil) type StorageLocation struct { - raw []byte hash *encoding.Multihash kind int expiry int64 @@ -35,51 +34,48 @@ func NewStorageLocation() *StorageLocation { } func (s *StorageLocation) DecodeMessage(dec *msgpack.Decoder) error { - data, err := dec.DecodeRaw() - - if err != nil { - return err - } - - s.raw = data - + // nop, we use the incoming message -> original already stored return nil } func (s *StorageLocation) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error { - hash := encoding.NewMultihash(s.raw[1:34]) // Replace NewMultihash with appropriate function + msg := s.IncomingMessage().Original() + + hash := encoding.NewMultihash(msg[1:34]) // Replace NewMultihash with appropriate function fmt.Println("Hash:", hash) - typeOfData := s.raw[34] + typeOfData := msg[34] - expiry := utils.DecodeEndian(s.raw[35:39]) + expiry := utils.DecodeEndian(msg[35:39]) - partCount := s.raw[39] + partCount := msg[39] parts := []string{} cursor := 40 for i := 0; i < int(partCount); i++ { - length := utils.DecodeEndian(s.raw[cursor : cursor+2]) + length := utils.DecodeEndian(msg[cursor : cursor+2]) cursor += 2 - part := string(s.raw[cursor : cursor+int(length)]) + part := string(msg[cursor : cursor+int(length)]) parts = append(parts, part) cursor += int(length) } - publicKey := s.raw[cursor : cursor+33] - signature := s.raw[cursor+33:] + cursor++ + + publicKey := msg[cursor : cursor+33] + signature := msg[cursor+33:] 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]) } - if !ed25519.Verify(publicKey[1:], s.raw[:cursor], signature) { + if !ed25519.Verify(publicKey[1:], msg[:cursor], signature) { return fmt.Errorf("Signature verification failed") } nodeId := encoding.NewNodeId(publicKey) // Assuming `node` is an instance of your NodeImpl structure - err := node.AddStorageLocation(hash, nodeId, storage.NewStorageLocation(int(typeOfData), parts, int64(expiry)), s.raw, node.Config()) // Implement AddStorageLocation + err := node.AddStorageLocation(hash, nodeId, storage.NewStorageLocation(int(typeOfData), parts, int64(expiry)), msg, node.Config()) // Implement AddStorageLocation if err != nil { return fmt.Errorf("Failed to add storage location: %s", err) @@ -110,7 +106,7 @@ func (s *StorageLocation) HandleMessage(node interfaces.Node, peer net.Peer, ver } if peerVal, ok := node.Services().P2P().Peers().Get(peerIdStr); ok { foundPeer := peerVal.(net.Peer) - err := foundPeer.SendMessage(s.raw) + err := foundPeer.SendMessage(msg) if err != nil { node.Logger().Error("Failed to send message", zap.Error(err)) continue