-
Notifications
You must be signed in to change notification settings - Fork 199
/
message.go
46 lines (38 loc) · 1.18 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package libp2p
import (
"fmt"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/p2p"
"github.com/ElrondNetwork/elrond-go/p2p/data"
"github.com/ElrondNetwork/elrond-go/p2p/message"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-pubsub"
)
// NewMessage returns a new instance of a Message object
func NewMessage(msg *pubsub.Message, marshalizer p2p.Marshalizer) (*message.Message, error) {
if check.IfNil(marshalizer) {
return nil, p2p.ErrNilMarshalizer
}
newMsg := &message.Message{
FromField: msg.From,
PayloadField: msg.Data,
SeqNoField: msg.Seqno,
TopicsField: msg.TopicIDs,
SignatureField: msg.Signature,
KeyField: msg.Key,
}
topicMessage := &data.TopicMessage{}
err := marshalizer.Unmarshal(topicMessage, msg.Data)
if err != nil {
return nil, fmt.Errorf("%w error: %s", p2p.ErrMessageUnmarshalError, err.Error())
}
newMsg.DataField = topicMessage.Payload
newMsg.TimestampField = topicMessage.Timestamp
id, err := peer.IDFromBytes(newMsg.From())
if err != nil {
return nil, err
}
newMsg.PeerField = core.PeerID(id)
return newMsg, nil
}