2019-12-06 02:16:18 +00:00
|
|
|
## Nim-LibP2P
|
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
|
|
|
import options
|
2020-06-03 02:21:11 +00:00
|
|
|
import chronicles, stew/byteutils
|
2020-06-07 07:15:21 +00:00
|
|
|
import metrics
|
2019-12-06 02:16:18 +00:00
|
|
|
import nimcrypto/sysrand
|
|
|
|
import messages, protobuf,
|
|
|
|
../../../peer,
|
2019-12-07 16:36:39 +00:00
|
|
|
../../../peerinfo,
|
2019-12-06 02:16:18 +00:00
|
|
|
../../../crypto/crypto,
|
|
|
|
../../../protobuf/minprotobuf
|
|
|
|
|
|
|
|
logScope:
|
2020-06-10 08:48:01 +00:00
|
|
|
topics = "pubsubmessage"
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2019-12-07 16:36:39 +00:00
|
|
|
const PubSubPrefix = "libp2p-pubsub:"
|
|
|
|
|
2020-06-07 07:41:23 +00:00
|
|
|
declareCounter(libp2p_pubsub_sig_verify_success, "pubsub successfully validated messages")
|
|
|
|
declareCounter(libp2p_pubsub_sig_verify_failure, "pubsub failed validated messages")
|
2020-06-07 07:15:21 +00:00
|
|
|
|
2020-05-27 18:33:49 +00:00
|
|
|
proc msgIdProvider(m: Message): string =
|
|
|
|
## default msg id provider
|
2020-06-03 02:21:11 +00:00
|
|
|
crypto.toHex(m.seqno) & PeerID.init(m.fromPeer).pretty
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-27 18:33:49 +00:00
|
|
|
template msgId*(m: Message): string =
|
|
|
|
## calls the ``msgIdProvider`` from
|
|
|
|
## the instantiation scope
|
|
|
|
##
|
|
|
|
mixin msgIdProvider
|
|
|
|
m.msgIdProvider()
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
proc fromPeerId*(m: Message): PeerId =
|
|
|
|
PeerID.init(m.fromPeer)
|
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
proc sign*(msg: Message, p: PeerInfo): Message {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
var buff = initProtoBuffer()
|
|
|
|
encodeMessage(msg, buff)
|
|
|
|
if buff.buffer.len > 0:
|
|
|
|
result = msg
|
2019-12-07 16:36:39 +00:00
|
|
|
result.signature = p.privateKey.
|
2020-06-03 02:21:11 +00:00
|
|
|
sign(PubSubPrefix.toBytes() & buff.buffer).tryGet().
|
2019-12-07 16:36:39 +00:00
|
|
|
getBytes()
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
proc verify*(m: Message, p: PeerInfo): bool =
|
2019-12-06 02:16:18 +00:00
|
|
|
if m.signature.len > 0 and m.key.len > 0:
|
|
|
|
var msg = m
|
|
|
|
msg.signature = @[]
|
|
|
|
msg.key = @[]
|
|
|
|
|
|
|
|
var buff = initProtoBuffer()
|
|
|
|
encodeMessage(msg, buff)
|
|
|
|
|
|
|
|
var remote: Signature
|
|
|
|
var key: PublicKey
|
|
|
|
if remote.init(m.signature) and key.init(m.key):
|
2019-12-17 05:24:03 +00:00
|
|
|
trace "verifying signature", remoteSignature = remote
|
2020-06-03 02:21:11 +00:00
|
|
|
result = remote.verify(PubSubPrefix.toBytes() & buff.buffer, key)
|
2020-06-07 07:15:21 +00:00
|
|
|
|
|
|
|
if result:
|
|
|
|
libp2p_pubsub_sig_verify_success.inc()
|
|
|
|
else:
|
|
|
|
libp2p_pubsub_sig_verify_failure.inc()
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2019-12-07 16:36:39 +00:00
|
|
|
proc newMessage*(p: PeerInfo,
|
2019-12-06 02:16:18 +00:00
|
|
|
data: seq[byte],
|
2020-05-06 09:26:08 +00:00
|
|
|
topic: string,
|
2019-12-08 21:06:58 +00:00
|
|
|
sign: bool = true): Message {.gcsafe.} =
|
2020-05-27 18:33:49 +00:00
|
|
|
var seqno: seq[byte] = newSeq[byte](8)
|
|
|
|
if randomBytes(addr seqno[0], 8) > 0:
|
|
|
|
if p.publicKey.isSome:
|
|
|
|
var key: seq[byte] = p.publicKey.get().getBytes().tryGet()
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-27 18:33:49 +00:00
|
|
|
result = Message(fromPeer: p.peerId.getBytes(),
|
|
|
|
data: data,
|
|
|
|
seqno: seqno,
|
|
|
|
topicIDs: @[topic])
|
|
|
|
if sign:
|
|
|
|
result = result.sign(p)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-27 18:33:49 +00:00
|
|
|
result.key = key
|