diff --git a/key/signing.go b/key/signing.go index c9a8bbb..16e5823 100644 --- a/key/signing.go +++ b/key/signing.go @@ -35,7 +35,8 @@ func (k *SigningKey) Sign(data []byte) ([]byte, error) { return ed25519.Sign(k.PrivKey, data), nil } -// Generates a signing key for the given identifier, ie. IPNS name +// Generates a signing key for the given identifier, ie. IPNS name. +// The IPNS key itself could be used, but lets stick to a more common DID structure. func NewSigningKey(d did.DID) (SigningKey, error) { name, err := nanoid.New() @@ -121,7 +122,7 @@ func validateSigningPrivateKey(privKey ed25519.PrivateKey) error { } if len(privKey) != ed25519.PrivateKeySize { - return fmt.Errorf("Invalid key length %d, should be %d: %w", len(privKey), ed25519.PrivateKeySize, ErrInvalidKeySize) + return fmt.Errorf("invalid key length %d, should be %d: %w", len(privKey), ed25519.PrivateKeySize, ErrInvalidKeySize) } return nil } diff --git a/msg/chat.go b/msg/chat.go deleted file mode 100644 index 5656963..0000000 --- a/msg/chat.go +++ /dev/null @@ -1,48 +0,0 @@ -package msg - -import ( - "crypto/ed25519" - "fmt" - - "github.com/bahner/go-ma" - nanoid "github.com/matoous/go-nanoid/v2" -) - -const CHAT = PREFIX + "chat/" + ma.VERSION - -// New creates a new Message instance -func Chat( - from string, - to string, - content []byte, - priv_key ed25519.PrivateKey) (*Message, error) { - - id, err := nanoid.New() - if err != nil { - return nil, err - } - - m := &Message{ - // Message meta data - Id: id, - Type: CHAT, - // Recipient - From: from, - To: to, - // Body - ContentType: DEFAULT_CONTENT_TYPE, - Content: content, - } - - err = verifyContent(content) - if err != nil { - return nil, err - } - - err = m.Sign(priv_key) - if err != nil { - return nil, fmt.Errorf("msg_new: failed to sign message: %w", err) - } - - return m, nil -} diff --git a/msg/errors.go b/msg/errors.go index 6326476..929a9b3 100644 --- a/msg/errors.go +++ b/msg/errors.go @@ -9,12 +9,10 @@ import ( var ( ErrBroadcastHasRecipient = fmt.Errorf("broadcast message must not have a recipient") ErrBroadcastInvalidTopic = fmt.Errorf("broadcast topic must be %s", ma.BROADCAST_TOPIC) - ErrBroadcastInvalidType = fmt.Errorf("broadcast message must not %s", BROADCAST) - ErrEmptyID = fmt.Errorf("id must be non-empty") ErrInvalidID = fmt.Errorf("invalid message id") + ErrEmptyID = fmt.Errorf("empty message id") ErrFetchDoc = fmt.Errorf("failed to fetch entity document") ErrInvalidMessageType = fmt.Errorf("invalid Message type") - ErrInvalidSender = fmt.Errorf("invalid sender") ErrInvalidRecipient = fmt.Errorf("invalid recipient") ErrMissingContentType = fmt.Errorf("empty ContentType") ErrMissingContent = fmt.Errorf("empty ContentType") diff --git a/msg/message.go b/msg/message.go index f49d65f..7c2fed8 100644 --- a/msg/message.go +++ b/msg/message.go @@ -5,13 +5,15 @@ import ( "crypto/ed25519" "fmt" + "github.com/bahner/go-ma" cbor "github.com/fxamacker/cbor/v2" pubsub "github.com/libp2p/go-libp2p-pubsub" nanoid "github.com/matoous/go-nanoid/v2" ) const ( - PREFIX = "/ma/message/" + PREFIX = "/ma/" + MESSAGE = PREFIX + ma.VERSION DEFAULT_CONTENT_TYPE = "text/plain" ) @@ -39,7 +41,6 @@ type Message struct { func New( from string, to string, - msg_type string, content []byte, contentType string, priv_key ed25519.PrivateKey) (*Message, error) { @@ -52,7 +53,7 @@ func New( m := &Message{ // Message meta data Id: id, - Type: msg_type, + Type: MESSAGE, // Recipient From: from, To: to, diff --git a/msg/reply.go b/msg/reply.go deleted file mode 100644 index f26f520..0000000 --- a/msg/reply.go +++ /dev/null @@ -1,71 +0,0 @@ -package msg - -import ( - "context" - "crypto/ed25519" - - "github.com/bahner/go-ma" - "github.com/fxamacker/cbor/v2" - nanoid "github.com/matoous/go-nanoid/v2" - - pubsub "github.com/libp2p/go-libp2p-pubsub" -) - -const ( - REPLY = PREFIX + "reply/" + ma.VERSION - REPLY_CONTENT_TYPE = "application/cbor" -) - -type ReplyContent struct { - // Id of the messagew to reply to - RequestID string `cbor:"requestID"` - // Type of the message to reply to - RequestType string `cbor:"requestType"` - // Reply content - Reply []byte `cbor:"reply"` -} - -func NewReply(m *Message, reply []byte) ([]byte, error) { - return cbor.Marshal( - &ReplyContent{ - RequestID: m.Id, - RequestType: m.Type, - Reply: reply, - }) -} - -func (m *Message) Reply(ctx context.Context, replyBytes []byte, privKey ed25519.PrivateKey, topic *pubsub.Topic) error { - id, err := nanoid.New() - if err != nil { - return err - } - - replyContent, err := NewReply(m, replyBytes) - if err != nil { - return err - } - - reply := &Message{ - // Message meta data - Id: id, - Type: REPLY, - // Recipient - From: m.To, - To: m.From, - // Body - ContentType: REPLY_CONTENT_TYPE, - Content: replyContent, - } - - err = reply.Sign(privKey) - if err != nil { - return err - } - - envelope, err := reply.Enclose() - if err != nil { - return err - } - - return envelope.Send(ctx, topic) -} diff --git a/msg/request.go b/msg/request.go deleted file mode 100644 index 2119ad0..0000000 --- a/msg/request.go +++ /dev/null @@ -1,51 +0,0 @@ -package msg - -import ( - "crypto/ed25519" - "fmt" - - "github.com/bahner/go-ma" - nanoid "github.com/matoous/go-nanoid/v2" -) - -const ( - REQUEST = PREFIX + "request/" + ma.VERSION - - REQUEST_CONTENT_TYPE = "application/cbor" -) - -func Request( - from string, - to string, - content []byte, - priv_key ed25519.PrivateKey) (*Message, error) { - - id, err := nanoid.New() - if err != nil { - return nil, err - } - - m := &Message{ - // Message meta data - Id: id, - Type: REQUEST, - // Recipient - From: from, - To: to, - // Body - ContentType: REQUEST_CONTENT_TYPE, - Content: content, - } - - err = verifyContent(content) - if err != nil { - return nil, err - } - - err = m.Sign(priv_key) - if err != nil { - return nil, fmt.Errorf("msg_new: failed to sign message: %w", err) - } - - return m, nil -} diff --git a/msg/validate.go b/msg/validate.go index 8dcd51c..b4561b6 100644 --- a/msg/validate.go +++ b/msg/validate.go @@ -132,9 +132,7 @@ func verifyID(id string) error { func verifyType(t string) error { if t == BROADCAST || - t == CHAT || - t == REQUEST || - t == REPLY { + t == MESSAGE { return nil }