diff --git a/protocol/base/base.go b/protocol/base/base.go index c2b7781..efd7988 100644 --- a/protocol/base/base.go +++ b/protocol/base/base.go @@ -2,6 +2,7 @@ package base import ( "context" + "git.lumeweb.com/LumeWeb/libs5-go/config" "git.lumeweb.com/LumeWeb/libs5-go/net" "git.lumeweb.com/LumeWeb/libs5-go/service" "github.com/vmihailenco/msgpack/v5" @@ -26,6 +27,7 @@ type IncomingMessageData struct { Services service.Services Logger *zap.Logger Peer net.Peer + Config *config.NodeConfig VerifyId bool } diff --git a/protocol/hash_query.go b/protocol/hash_query.go index ff7015e..a8ba12e 100644 --- a/protocol/hash_query.go +++ b/protocol/hash_query.go @@ -4,6 +4,7 @@ import ( "git.lumeweb.com/LumeWeb/libs5-go/encoding" "git.lumeweb.com/LumeWeb/libs5-go/net" "git.lumeweb.com/LumeWeb/libs5-go/protocol/base" + "git.lumeweb.com/LumeWeb/libs5-go/storage" "git.lumeweb.com/LumeWeb/libs5-go/types" "github.com/emirpasic/gods/sets/hashset" "github.com/vmihailenco/msgpack/v5" @@ -91,6 +92,7 @@ func (h *HashQuery) HandleMessage(message base.IncomingMessageData) error { peer := message.Peer services := message.Services logger := message.Logger + config := message.Config mapLocations, err := services.Storage().GetCachedStorageLocations(h.hash, h.kinds) if err != nil { @@ -136,7 +138,7 @@ func (h *HashQuery) HandleMessage(message base.IncomingMessageData) error { return err } - message := services.P2P().PrepareProvideMessage(h.hash, location) + message := storage.PrepareProvideMessage(config.KeyPair, h.hash, location) err = services.Storage().AddStorageLocation(h.hash, services.P2P().NodeId(), location, message) if err != nil { diff --git a/service/p2p.go b/service/p2p.go index 803851f..0ebd879 100644 --- a/service/p2p.go +++ b/service/p2p.go @@ -3,7 +3,6 @@ package service import ( "bytes" "context" - ed25519p "crypto/ed25519" "errors" "fmt" "git.lumeweb.com/LumeWeb/libs5-go/ed25519" @@ -12,7 +11,6 @@ import ( "git.lumeweb.com/LumeWeb/libs5-go/protocol" "git.lumeweb.com/LumeWeb/libs5-go/protocol/base" "git.lumeweb.com/LumeWeb/libs5-go/protocol/signed" - "git.lumeweb.com/LumeWeb/libs5-go/storage" "git.lumeweb.com/LumeWeb/libs5-go/structs" "git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/utils" @@ -445,6 +443,7 @@ func (p *P2PService) OnNewPeerListen(peer net.Peer, verifyId bool) { Ctx: context.Background(), Peer: peer, VerifyId: verifyId, + Config: p.config, } dec := msgpack.NewDecoder(bytes.NewReader(reader.Data)) @@ -664,48 +663,6 @@ func (p *P2PService) NodeId() *encoding.NodeId { return p.localNodeID } -func (p *P2PService) PrepareProvideMessage(hash *encoding.Multihash, location storage.StorageLocation) []byte { - // Initialize the list with the record type. - list := []byte{byte(types.RecordTypeStorageLocation)} - - // Append the full bytes of the hash. - list = append(list, hash.FullBytes()...) - - // Append the location type. - list = append(list, byte(location.Type())) - - // Append the expiry time of the location, encoded as 4 bytes. - list = append(list, utils.EncodeEndian(uint64(location.Expiry()), 4)...) - - // Append the number of parts in the location. - list = append(list, byte(len(location.Parts()))) - - // Iterate over each part in the location. - for _, part := range location.Parts() { - // Convert part to bytes. - bytes := []byte(part) - - // Encode the length of the part as 4 bytes and append. - list = append(list, utils.EncodeEndian(uint64(len(bytes)), 2)...) - - // Append the actual part bytes. - list = append(list, bytes...) - } - - // Append a null byte at the end of the list. - list = append(list, 0) - - // Sign the list using the node's private key. - signature := ed25519p.Sign(p.nodeKeyPair.ExtractBytes(), list) - - // Append the public key and signature to the list. - finalList := append(list, p.nodeKeyPair.PublicKey()...) - finalList = append(finalList, signature...) - - // Return the final byte slice. - return finalList -} - func (p *P2PService) WaitOnConnectedPeers() { p.connections.Wait() } diff --git a/storage/location.go b/storage/location.go new file mode 100644 index 0000000..2be87b1 --- /dev/null +++ b/storage/location.go @@ -0,0 +1,73 @@ +package storage + +type StorageLocation interface { + BytesURL() string + OutboardBytesURL() string + String() string + ProviderMessage() []byte + Type() int + Parts() []string + BinaryParts() [][]byte + Expiry() int64 + SetProviderMessage(msg []byte) + SetType(t int) + SetParts(p []string) + SetBinaryParts(bp [][]byte) + SetExpiry(e int64) +} + +func (s *StorageLocationImpl) Type() int { + return s.kind +} + +func (s *StorageLocationImpl) Parts() []string { + return s.parts +} + +func (s *StorageLocationImpl) BinaryParts() [][]byte { + return s.binaryParts +} + +func (s *StorageLocationImpl) Expiry() int64 { + return s.expiry +} + +func (s *StorageLocationImpl) SetType(t int) { + s.kind = t +} + +func (s *StorageLocationImpl) SetParts(p []string) { + s.parts = p +} + +func (s *StorageLocationImpl) SetBinaryParts(bp [][]byte) { + s.binaryParts = bp +} + +func (s *StorageLocationImpl) SetExpiry(e int64) { + s.expiry = e +} + +func (s *StorageLocationImpl) SetProviderMessage(msg []byte) { + s.providerMessage = msg +} + +func (s *StorageLocationImpl) ProviderMessage() []byte { + return s.providerMessage +} + +func NewStorageLocation(Type int, Parts []string, Expiry int64) StorageLocation { + return &StorageLocationImpl{ + kind: Type, + parts: Parts, + expiry: Expiry, + } +} + +type StorageLocationImpl struct { + kind int + parts []string + binaryParts [][]byte + expiry int64 + providerMessage []byte +} diff --git a/storage/p2p.go b/storage/p2p.go new file mode 100644 index 0000000..ebffbab --- /dev/null +++ b/storage/p2p.go @@ -0,0 +1,51 @@ +package storage + +import ( + ed25519p "crypto/ed25519" + "git.lumeweb.com/LumeWeb/libs5-go/ed25519" + "git.lumeweb.com/LumeWeb/libs5-go/encoding" + "git.lumeweb.com/LumeWeb/libs5-go/types" + "git.lumeweb.com/LumeWeb/libs5-go/utils" +) + +func PrepareProvideMessage(identity *ed25519.KeyPairEd25519, hash *encoding.Multihash, location StorageLocation) []byte { + // Initialize the list with the record type. + list := []byte{byte(types.RecordTypeStorageLocation)} + + // Append the full bytes of the hash. + list = append(list, hash.FullBytes()...) + + // Append the location type. + list = append(list, byte(location.Type())) + + // Append the expiry time of the location, encoded as 4 bytes. + list = append(list, utils.EncodeEndian(uint64(location.Expiry()), 4)...) + + // Append the number of parts in the location. + list = append(list, byte(len(location.Parts()))) + + // Iterate over each part in the location. + for _, part := range location.Parts() { + // Convert part to bytes. + bytes := []byte(part) + + // Encode the length of the part as 4 bytes and append. + list = append(list, utils.EncodeEndian(uint64(len(bytes)), 2)...) + + // Append the actual part bytes. + list = append(list, bytes...) + } + + // Append a null byte at the end of the list. + list = append(list, 0) + + // Sign the list using the node's private key. + signature := ed25519p.Sign(identity.ExtractBytes(), list) + + // Append the public key and signature to the list. + finalList := append(list, identity.PublicKey()...) + finalList = append(finalList, signature...) + + // Return the final byte slice. + return finalList +} diff --git a/storage/provider_store.go b/storage/provider_store.go new file mode 100644 index 0000000..8f3469a --- /dev/null +++ b/storage/provider_store.go @@ -0,0 +1,11 @@ +package storage + +import ( + "git.lumeweb.com/LumeWeb/libs5-go/encoding" + "git.lumeweb.com/LumeWeb/libs5-go/types" +) + +type ProviderStore interface { + CanProvide(hash *encoding.Multihash, kind []types.StorageLocationType) bool + Provide(hash *encoding.Multihash, kind []types.StorageLocationType) (StorageLocation, error) +} diff --git a/storage/signed_location.go b/storage/signed_location.go new file mode 100644 index 0000000..90b8683 --- /dev/null +++ b/storage/signed_location.go @@ -0,0 +1,9 @@ +package storage + +import "git.lumeweb.com/LumeWeb/libs5-go/encoding" + +type SignedStorageLocation interface { + String() string + NodeId() *encoding.NodeId + Location() StorageLocation +} diff --git a/storage/storage.go b/storage/storage.go index 32378e7..83b51ee 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -25,14 +25,6 @@ type StorageLocationMap map[int]NodeStorage type NodeStorage map[string]NodeDetailsStorage type NodeDetailsStorage map[int]interface{} -type StorageLocationImpl struct { - kind int - parts []string - binaryParts [][]byte - expiry int64 - providerMessage []byte -} - type StorageLocationProviderParams struct { Services service.Services Hash *encoding.Multihash @@ -40,54 +32,6 @@ type StorageLocationProviderParams struct { service.ServiceParams } -func (s *StorageLocationImpl) Type() int { - return s.kind -} - -func (s *StorageLocationImpl) Parts() []string { - return s.parts -} - -func (s *StorageLocationImpl) BinaryParts() [][]byte { - return s.binaryParts -} - -func (s *StorageLocationImpl) Expiry() int64 { - return s.expiry -} - -func (s *StorageLocationImpl) SetType(t int) { - s.kind = t -} - -func (s *StorageLocationImpl) SetParts(p []string) { - s.parts = p -} - -func (s *StorageLocationImpl) SetBinaryParts(bp [][]byte) { - s.binaryParts = bp -} - -func (s *StorageLocationImpl) SetExpiry(e int64) { - s.expiry = e -} - -func (s *StorageLocationImpl) SetProviderMessage(msg []byte) { - s.providerMessage = msg -} - -func (s *StorageLocationImpl) ProviderMessage() []byte { - return s.providerMessage -} - -func NewStorageLocation(Type int, Parts []string, Expiry int64) StorageLocation { - return &StorageLocationImpl{ - kind: Type, - parts: Parts, - expiry: Expiry, - } -} - func (s *StorageLocationImpl) BytesURL() string { return s.parts[0] } @@ -373,29 +317,3 @@ type StorageLocationProvider interface { Upvote(uri SignedStorageLocation) error Downvote(uri SignedStorageLocation) error } - -type StorageLocation interface { - BytesURL() string - OutboardBytesURL() string - String() string - ProviderMessage() []byte - Type() int - Parts() []string - BinaryParts() [][]byte - Expiry() int64 - SetProviderMessage(msg []byte) - SetType(t int) - SetParts(p []string) - SetBinaryParts(bp [][]byte) - SetExpiry(e int64) -} -type SignedStorageLocation interface { - String() string - NodeId() *encoding.NodeId - Location() StorageLocation -} - -type ProviderStore interface { - CanProvide(hash *encoding.Multihash, kind []types.StorageLocationType) bool - Provide(hash *encoding.Multihash, kind []types.StorageLocationType) (StorageLocation, error) -}