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.
|
|
|
|
|
2020-06-28 15:56:38 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-07-16 10:06:57 +00:00
|
|
|
import chronicles, metrics, stew/[byteutils, endians2]
|
|
|
|
import ./messages, ./protobuf,
|
2020-07-01 06:25:09 +00:00
|
|
|
../../../peerid,
|
2019-12-07 16:36:39 +00:00
|
|
|
../../../peerinfo,
|
2019-12-06 02:16:18 +00:00
|
|
|
../../../crypto/crypto,
|
|
|
|
../../../protobuf/minprotobuf
|
2020-07-16 10:06:57 +00:00
|
|
|
|
|
|
|
export messages
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
logScope:
|
2020-06-10 08:48:01 +00:00
|
|
|
topics = "pubsubmessage"
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-06-28 15:56:38 +00:00
|
|
|
const PubSubPrefix = toBytes("libp2p-pubsub:")
|
2019-12-07 16:36:39 +00:00
|
|
|
|
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-06-28 15:56:38 +00:00
|
|
|
func defaultMsgIdProvider*(m: Message): string =
|
|
|
|
byteutils.toHex(m.seqno) & m.fromPeer.pretty
|
2020-05-27 18:33:49 +00:00
|
|
|
|
2020-07-16 10:06:57 +00:00
|
|
|
proc sign*(msg: Message, p: PeerInfo): CryptoResult[seq[byte]] =
|
|
|
|
ok((? p.privateKey.sign(PubSubPrefix & encodeMessage(msg))).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 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-07-13 12:43:07 +00:00
|
|
|
result = remote.verify(PubSubPrefix & encodeMessage(msg), key)
|
2020-06-28 15:56:38 +00:00
|
|
|
|
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
|
|
|
|
2020-06-28 15:56:38 +00:00
|
|
|
proc init*(
|
|
|
|
T: type Message,
|
|
|
|
p: PeerInfo,
|
|
|
|
data: seq[byte],
|
|
|
|
topic: string,
|
2020-07-15 03:51:33 +00:00
|
|
|
seqno: uint64,
|
2020-06-28 15:56:38 +00:00
|
|
|
sign: bool = true): Message {.gcsafe, raises: [CatchableError, Defect].} =
|
|
|
|
result = Message(
|
|
|
|
fromPeer: p.peerId,
|
|
|
|
data: data,
|
2020-07-15 03:51:33 +00:00
|
|
|
seqno: @(seqno.toBytesBE), # unefficient, fine for now
|
2020-06-28 15:56:38 +00:00
|
|
|
topicIDs: @[topic])
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-06-28 15:56:38 +00:00
|
|
|
if sign and p.publicKey.isSome:
|
2020-07-16 10:06:57 +00:00
|
|
|
result.signature = sign(result, p).tryGet()
|
2020-06-28 15:56:38 +00:00
|
|
|
result.key = p.publicKey.get().getBytes().tryGet()
|