diff --git a/interfaces/p2p.go b/interfaces/p2p.go index e389d09..9568e77 100644 --- a/interfaces/p2p.go +++ b/interfaces/p2p.go @@ -25,5 +25,6 @@ type P2PService interface { DownVote(nodeId *encoding.NodeId) error NodeId() *encoding.NodeId SelfConnectionUris() []*url.URL + PrepareProvideMessage(hash *encoding.Multihash, location StorageLocation) []byte Service } diff --git a/service/p2p.go b/service/p2p.go index 7bb5965..f0d47f2 100644 --- a/service/p2p.go +++ b/service/p2p.go @@ -1,6 +1,7 @@ package service import ( + ed25519p "crypto/ed25519" "errors" "fmt" "git.lumeweb.com/LumeWeb/libs5-go/ed25519" @@ -661,3 +662,45 @@ func (p *P2PImpl) vote(nodeId *encoding.NodeId, upvote bool) error { func (p *P2PImpl) NodeId() *encoding.NodeId { return p.localNodeID } + +func (p *P2PImpl) PrepareProvideMessage(hash *encoding.Multihash, location interfaces.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)), 4)...) + + // 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.PublicKeyRaw(), 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 +}