diff --git a/client/client.go b/client/client.go index 63a01ce..79831ef 100644 --- a/client/client.go +++ b/client/client.go @@ -3,8 +3,6 @@ package client import ( "crypto/ecdsa" - "crypto/sha256" - "encoding/binary" "log" "github.com/ethereum/go-ethereum/crypto" @@ -99,6 +97,15 @@ func (c *Client) onReceive(message mvdsproto.Message) { return } + pubkey, err := crypto.SigToPub(msg.ID(), msg.Signature) + if err != nil { + log.Printf("error while recovering pubkey: %s", err.Error()) + // @todo + return + } + + // @todo probably store the sender somewhere? + // @todo pump messages to subscriber channels if len(msg.PreviousMessage) == 0 { @@ -124,12 +131,7 @@ func (c *Client) handlePreviousMessage(group state.GroupID, previousMessage stat // sign signs generates a signature of the message and adds it to the message. func (c *Client) sign(m *protobuf.Message) error { - b := make([]byte, 4) - binary.LittleEndian.PutUint32(b, uint32(m.MessageType)) - b = append(b, m.Body...) - b = append(b, m.PreviousMessage...) - - hash := sha256.Sum256(b) + hash := m.ID() sig, err := crypto.Sign(hash[:], c.identity) if err != nil { diff --git a/protobuf/messageid.go b/protobuf/messageid.go new file mode 100644 index 0000000..33f747c --- /dev/null +++ b/protobuf/messageid.go @@ -0,0 +1,16 @@ +package protobuf + +import ( + "crypto/sha256" + "encoding/binary" +) + +func (m *Message) ID() []byte { + b := make([]byte, 4) + binary.LittleEndian.PutUint32(b, uint32(m.MessageType)) + b = append(b, m.Body...) + b = append(b, m.PreviousMessage...) + + hash := sha256.Sum256(b) + return hash[:] +}