status-go/protocol/v1/message.go

50 lines
1.5 KiB
Go
Raw Normal View History

package protocol
2019-07-17 22:25:42 +00:00
import (
"crypto/ecdsa"
"time"
2019-07-17 22:25:42 +00:00
"github.com/golang/protobuf/proto"
2019-08-06 21:50:13 +00:00
"github.com/pkg/errors"
2020-01-02 09:10:19 +00:00
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/protobuf"
2019-07-17 22:25:42 +00:00
)
var (
// ErrInvalidDecodedValue means that the decoded message is of wrong type.
// This might mean that the status message serialization tag changed.
ErrInvalidDecodedValue = errors.New("invalid decoded value type")
)
// TimestampInMsFromTime returns a TimestampInMs from a time.Time instance.
func TimestampInMsFromTime(t time.Time) uint64 {
return uint64(t.UnixNano() / int64(time.Millisecond))
2019-07-17 22:25:42 +00:00
}
2019-08-06 21:50:13 +00:00
// MessageID calculates the messageID from author's compressed public key
// and not encrypted but encoded payload.
func MessageID(author *ecdsa.PublicKey, data []byte) types.HexBytes {
keyBytes := crypto.FromECDSAPub(author)
return types.HexBytes(crypto.Keccak256(append(keyBytes, data...)))
2019-07-17 22:25:42 +00:00
}
// WrapMessageV1 wraps a payload into a protobuf message and signs it if an identity is provided
func WrapMessageV1(payload []byte, messageType protobuf.ApplicationMetadataMessage_Type, identity *ecdsa.PrivateKey) ([]byte, error) {
2019-07-17 22:25:42 +00:00
var signature []byte
if identity != nil {
var err error
signature, err = crypto.Sign(crypto.Keccak256(payload), identity)
if err != nil {
return nil, err
}
}
message := &protobuf.ApplicationMetadataMessage{
2019-07-17 22:25:42 +00:00
Signature: signature,
Type: messageType,
2022-05-27 09:14:40 +00:00
Payload: payload}
2019-07-17 22:25:42 +00:00
return proto.Marshal(message)
}