feat: implement PrepareProvideMessage

This commit is contained in:
Derrick Hammer 2024-01-24 02:53:56 -05:00
parent d734e1a89b
commit 34bb591bfe
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 44 additions and 0 deletions

View File

@ -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
}

View File

@ -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
}