feat: add secure message signing and encoding
This commit is contained in:
parent
3ce371986b
commit
00c8a081f6
|
@ -14,6 +14,7 @@ import (
|
|||
var (
|
||||
_ base.IncomingMessageTyped = (*SignedMessage)(nil)
|
||||
_ msgpack.CustomDecoder = (*signedMessagePayoad)(nil)
|
||||
_ msgpack.CustomEncoder = (*SignedMessage)(nil)
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -27,6 +28,22 @@ type SignedMessage struct {
|
|||
base.IncomingMessageTypedImpl
|
||||
}
|
||||
|
||||
func (s *SignedMessage) SetNodeId(nodeId *encoding.NodeId) {
|
||||
s.nodeId = nodeId
|
||||
}
|
||||
|
||||
func (s *SignedMessage) SetSignature(signature []byte) {
|
||||
s.signature = signature
|
||||
}
|
||||
|
||||
func (s *SignedMessage) SetMessage(message []byte) {
|
||||
s.message = message
|
||||
}
|
||||
|
||||
func NewSignedMessageRequest(message []byte) *SignedMessage {
|
||||
return &SignedMessage{message: message}
|
||||
}
|
||||
|
||||
type signedMessagePayoad struct {
|
||||
kind int
|
||||
message msgpack.RawMessage
|
||||
|
@ -105,5 +122,44 @@ func (s *SignedMessage) DecodeMessage(dec *msgpack.Decoder) error {
|
|||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
func (s *SignedMessage) EncodeMsgpack(enc *msgpack.Encoder) error {
|
||||
err := enc.EncodeInt(int64(types.ProtocolMethodSignedMessage))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = enc.EncodeBytes(s.nodeId.Raw())
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = enc.EncodeBytes(s.signature)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = enc.EncodeBytes(s.message)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
func (s *SignedMessage) Sign(node interfaces.Node) error {
|
||||
if s.nodeId == nil {
|
||||
panic("nodeId is nil")
|
||||
}
|
||||
|
||||
if s.message == nil {
|
||||
panic("message is nil")
|
||||
}
|
||||
|
||||
s.signature = ed25519.Sign(node.Config().KeyPair.ExtractBytes(), s.message)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
||||
"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/structs"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/utils"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
|
@ -316,3 +317,21 @@ func (p *P2PImpl) SortNodesByScore(nodes []*encoding.NodeId) ([]*encoding.NodeId
|
|||
|
||||
return nodes, errOccurred
|
||||
}
|
||||
func (p *P2PImpl) SignMessageSimple(message []byte) ([]byte, error) {
|
||||
signedMessage := signed.NewSignedMessageRequest(message)
|
||||
signedMessage.SetNodeId(p.localNodeID)
|
||||
|
||||
err := signedMessage.Sign(p.Node())
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result, err := msgpack.Marshal(signedMessage)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue