mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-08 00:43:06 +00:00
refactor(utils): moved noise/compat payload encryption code to utils
This commit is contained in:
parent
c9bc774895
commit
7f2ea1caeb
@ -27,17 +27,21 @@ import
|
||||
../../waku/v2/protocol/waku_lightpush/rpc,
|
||||
../../waku/v2/protocol/waku_filter,
|
||||
../../waku/v2/protocol/waku_store,
|
||||
../../waku/v2/node/[waku_node, waku_payload, waku_metrics],
|
||||
../../waku/v2/node/waku_node,
|
||||
../../waku/v2/node/waku_metrics,
|
||||
../../waku/v2/node/dnsdisc/waku_dnsdisc,
|
||||
../../waku/v2/node/peer_manager,
|
||||
../../waku/v2/utils/[peers, time],
|
||||
../../waku/v2/utils/compat,
|
||||
../../waku/v2/utils/peers,
|
||||
../../waku/v2/utils/time,
|
||||
../../waku/common/utils/nat,
|
||||
./config_chat2
|
||||
|
||||
when defined(rln):
|
||||
import
|
||||
libp2p/protocols/pubsub/rpc/messages,
|
||||
libp2p/protocols/pubsub/pubsub,
|
||||
libp2p/protocols/pubsub/pubsub
|
||||
import
|
||||
../../waku/v2/protocol/waku_rln_relay
|
||||
|
||||
const Help = """
|
||||
|
||||
@ -4,7 +4,9 @@ import
|
||||
./v2/test_confutils_envvar,
|
||||
./v2/test_sqlite_migrations
|
||||
|
||||
## Waku archive test suite
|
||||
## Waku v2
|
||||
|
||||
# Waku archive test suite
|
||||
import
|
||||
./v2/waku_archive/test_driver_queue_index,
|
||||
./v2/waku_archive/test_driver_queue_pagination,
|
||||
@ -15,7 +17,7 @@ import
|
||||
./v2/waku_archive/test_retention_policy,
|
||||
./v2/waku_archive/test_waku_archive
|
||||
|
||||
## Waku store test suite
|
||||
# Waku store test suite
|
||||
import
|
||||
./v2/waku_store/test_rpc_codec,
|
||||
./v2/waku_store/test_waku_store,
|
||||
@ -38,7 +40,6 @@ import
|
||||
./v2/test_wakunode_filter,
|
||||
./v2/test_waku_peer_exchange,
|
||||
./v2/test_peer_store_extended,
|
||||
./v2/test_waku_payload,
|
||||
./v2/test_waku_swap,
|
||||
./v2/test_utils_peers,
|
||||
./v2/test_message_cache,
|
||||
@ -62,6 +63,7 @@ import
|
||||
./v2/test_waku_noise_sessions,
|
||||
./v2/test_waku_switch,
|
||||
# Utils
|
||||
./v2/test_utils_compat,
|
||||
./v2/test_utils_keyfile
|
||||
|
||||
|
||||
@ -74,7 +76,6 @@ when defined(rln):
|
||||
./v2/test_waku_rln_relay_onchain
|
||||
|
||||
|
||||
|
||||
# TODO: Only enable this once swap module is integrated more nicely as a dependency, i.e. as submodule with CI etc
|
||||
# For PoC execute it manually and run separate module here: https://github.com/vacp2p/swap-contracts-module
|
||||
# ./v2/test_waku_swap_contracts
|
||||
|
||||
@ -32,6 +32,7 @@ import
|
||||
../../waku/v2/protocol/waku_filter,
|
||||
../../waku/v2/protocol/waku_filter/rpc,
|
||||
../../waku/v2/protocol/waku_filter/client,
|
||||
../../waku/v2/utils/compat,
|
||||
../../waku/v2/utils/peers,
|
||||
../../waku/v2/utils/time,
|
||||
./testlib/common,
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
{.used.}
|
||||
|
||||
import
|
||||
testutils/unittests,
|
||||
testutils/unittests
|
||||
import
|
||||
../../waku/v2/protocol/waku_message,
|
||||
../../waku/v2/node/waku_payload,
|
||||
../../waku/v2/utils/compat,
|
||||
../../waku/v2/utils/time
|
||||
|
||||
procSuite "Waku Payload":
|
||||
@ -18,7 +18,8 @@ import
|
||||
import
|
||||
../../waku/v1/protocol/waku_protocol,
|
||||
../../waku/v2/protocol/waku_message,
|
||||
../../waku/v2/node/[waku_node, waku_payload],
|
||||
../../waku/v2/node/waku_node,
|
||||
../../waku/v2/utils/compat,
|
||||
../../waku/v2/utils/peers,
|
||||
../../apps/wakubridge/wakubridge,
|
||||
../test_helpers
|
||||
|
||||
@ -5,7 +5,7 @@ import
|
||||
std/random,
|
||||
std/tables,
|
||||
stew/byteutils,
|
||||
../../waku/v2/node/waku_payload,
|
||||
../../waku/v2/utils/noise as waku_message_utils,
|
||||
../../waku/v2/protocol/waku_noise/noise_types,
|
||||
../../waku/v2/protocol/waku_noise/noise_utils,
|
||||
../../waku/v2/protocol/waku_noise/noise,
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
{.used.}
|
||||
|
||||
import
|
||||
std/[random,tables],
|
||||
stew/byteutils,
|
||||
std/[random, tables],
|
||||
stew/[results, byteutils],
|
||||
testutils/unittests,
|
||||
libp2p/protobuf/minprotobuf
|
||||
import
|
||||
../../waku/v2/node/waku_payload,
|
||||
../../waku/v2/utils/noise as waku_message_utils,
|
||||
../../waku/v2/protocol/waku_noise/noise_types,
|
||||
../../waku/v2/protocol/waku_noise/noise_utils,
|
||||
../../waku/v2/protocol/waku_noise/noise_handshake_processing,
|
||||
@ -87,7 +87,7 @@ procSuite "Waku Noise Sessions":
|
||||
sentTransportMessage: seq[byte]
|
||||
aliceStep, bobStep: HandshakeStepResult
|
||||
msgFromPb: ProtoResult[WakuMessage]
|
||||
wakuMsg: WakuResult[WakuMessage]
|
||||
wakuMsg: Result[WakuMessage, cstring]
|
||||
pb: ProtoBuffer
|
||||
readPayloadV2: PayloadV2
|
||||
aliceMessageNametag, bobMessageNametag: MessageNametag
|
||||
|
||||
@ -7,13 +7,10 @@ import
|
||||
import
|
||||
libp2p/protocols/ping,
|
||||
libp2p/crypto/[crypto, secp],
|
||||
libp2p/nameresolving/nameresolver,
|
||||
libp2p/nameresolving/dnsresolver
|
||||
import
|
||||
../../waku/v2/node/peer_manager,
|
||||
../../waku/v2/utils/peers,
|
||||
../../waku/v2/node/waku_node,
|
||||
../../waku/v2/node/waku_payload,
|
||||
../../waku/v2/utils/peers
|
||||
|
||||
# protocols and their tag
|
||||
@ -60,7 +57,7 @@ type
|
||||
proc parseCmdArg*(T: type chronos.Duration, p: string): T =
|
||||
try:
|
||||
result = chronos.seconds(parseInt(p))
|
||||
except CatchableError as e:
|
||||
except CatchableError:
|
||||
raise newException(ConfigurationError, "Invalid timeout value")
|
||||
|
||||
proc completeCmdArg*(T: type chronos.Duration, val: string): seq[string] =
|
||||
|
||||
@ -9,8 +9,8 @@ import
|
||||
../../protocol/waku_message,
|
||||
../../protocol/waku_store,
|
||||
../../protocol/waku_store/rpc,
|
||||
../../utils/compat,
|
||||
../../utils/time,
|
||||
../waku_payload,
|
||||
./hexstrings,
|
||||
./jsonrpc_types
|
||||
|
||||
|
||||
@ -4,17 +4,18 @@ else:
|
||||
{.push raises: [].}
|
||||
|
||||
import
|
||||
std/[tables,sequtils],
|
||||
std/[tables, sequtils],
|
||||
chronicles,
|
||||
eth/keys,
|
||||
json_rpc/rpcserver,
|
||||
nimcrypto/sysrand,
|
||||
nimcrypto/sysrand
|
||||
import
|
||||
../../utils/compat,
|
||||
../waku_node,
|
||||
../waku_payload,
|
||||
./jsonrpc_types,
|
||||
./jsonrpc_utils
|
||||
|
||||
export waku_payload, jsonrpc_types
|
||||
export compat, jsonrpc_types
|
||||
|
||||
logScope:
|
||||
topics = "waku node jsonrpc private_api"
|
||||
@ -42,7 +43,7 @@ proc installPrivateApiHandlers*(node: WakuNode, rpcsrv: RpcServer, topicCache: T
|
||||
|
||||
let msg = message.toWakuMessage(version = 1,
|
||||
rng = node.rng,
|
||||
pubKey = none(waku_payload.PublicKey),
|
||||
pubKey = none(compat.PublicKey),
|
||||
symkey = some(symkey.toSymKey()))
|
||||
|
||||
if (await node.publish(topic, msg).withTimeout(futTimeout)):
|
||||
@ -65,7 +66,7 @@ proc installPrivateApiHandlers*(node: WakuNode, rpcsrv: RpcServer, topicCache: T
|
||||
# Clear cache before next call
|
||||
topicCache[topic] = @[]
|
||||
return msgs.mapIt(it.toWakuRelayMessage(symkey = some(symkey.toSymKey()),
|
||||
privateKey = none(waku_payload.PrivateKey)))
|
||||
privateKey = none(compat.PrivateKey)))
|
||||
else:
|
||||
# Not subscribed to this topic
|
||||
raise newException(ValueError, "Not subscribed to topic: " & topic)
|
||||
@ -76,7 +77,7 @@ proc installPrivateApiHandlers*(node: WakuNode, rpcsrv: RpcServer, topicCache: T
|
||||
## Generates and returns a public/private key pair for asymmetric message encryption and decryption.
|
||||
debug "get_waku_v2_private_v1_asymmetric_keypair"
|
||||
|
||||
let privKey = waku_payload.PrivateKey.random(node.rng[])
|
||||
let privKey = compat.PrivateKey.random(node.rng[])
|
||||
|
||||
return WakuKeyPair(seckey: privKey, pubkey: privKey.toPublicKey())
|
||||
|
||||
|
||||
@ -2,8 +2,6 @@
|
||||
##
|
||||
## See https://github.com/vacp2p/specs/blob/master/specs/waku/v2/waku-message.md
|
||||
## for spec.
|
||||
##
|
||||
## For payload content and encryption, see waku/v2/node/waku_payload.nim
|
||||
|
||||
when (NimMajor, NimMinor) < (1, 4):
|
||||
{.push raises: [Defect].}
|
||||
|
||||
@ -3,13 +3,14 @@ when (NimMajor, NimMinor) < (1, 4):
|
||||
else:
|
||||
{.push raises: [].}
|
||||
|
||||
|
||||
import
|
||||
std/options,
|
||||
eth/keys,
|
||||
stew/results,
|
||||
eth/keys
|
||||
import
|
||||
../../whisper/whisper_types,
|
||||
../protocol/waku_message,
|
||||
../protocol/waku_noise/noise_types,
|
||||
../protocol/waku_noise/noise_utils
|
||||
../protocol/waku_message
|
||||
|
||||
export whisper_types, keys, options
|
||||
|
||||
@ -28,9 +29,6 @@ type
|
||||
of None:
|
||||
discard
|
||||
|
||||
# NOTE: Currently only used here, if we start using it elsewhere pull it out.
|
||||
WakuResult*[T] = Result[T, cstring]
|
||||
|
||||
|
||||
# TODO:
|
||||
# - This is using `DecodedPayload` from Waku v1 / Whisper and could be altered
|
||||
@ -41,7 +39,7 @@ type
|
||||
# - For now this `KeyInfo` is a bit silly also, but perhaps with v2 or
|
||||
# adjustments to Waku v1 encoding, it can be better.
|
||||
proc decodePayload*(message: WakuMessage, keyInfo: KeyInfo):
|
||||
WakuResult[DecodedPayload] =
|
||||
Result[DecodedPayload, cstring] =
|
||||
case message.version
|
||||
of 0:
|
||||
return ok(DecodedPayload(payload:message.payload))
|
||||
@ -69,7 +67,7 @@ proc decodePayload*(message: WakuMessage, keyInfo: KeyInfo):
|
||||
# TODO: same story as for `decodedPayload`, but then regarding the `Payload`
|
||||
# object.
|
||||
proc encode*(payload: Payload, version: uint32, rng: var HmacDrbgContext):
|
||||
WakuResult[seq[byte]] =
|
||||
Result[seq[byte], cstring] =
|
||||
case version
|
||||
of 0:
|
||||
# This is rather silly
|
||||
@ -82,36 +80,3 @@ proc encode*(payload: Payload, version: uint32, rng: var HmacDrbgContext):
|
||||
return err("Couldn't encode the payload")
|
||||
else:
|
||||
return err("Unsupported WakuMessage version")
|
||||
|
||||
|
||||
# Decodes a WakuMessage to a PayloadV2
|
||||
# Currently, this is just a wrapper over deserializePayloadV2 and encryption/decryption is done on top (no KeyInfo)
|
||||
proc decodePayloadV2*(message: WakuMessage): WakuResult[PayloadV2]
|
||||
{.raises: [Defect, NoiseMalformedHandshake, NoisePublicKeyError].} =
|
||||
# We check message version (only 2 is supported in this proc)
|
||||
case message.version
|
||||
of 2:
|
||||
# We attempt to decode the WakuMessage payload
|
||||
let deserializedPayload2 = deserializePayloadV2(message.payload)
|
||||
if deserializedPayload2.isOk():
|
||||
return ok(deserializedPayload2.get())
|
||||
else:
|
||||
return err("Failed to decode WakuMessage")
|
||||
else:
|
||||
return err("Wrong message version while decoding payload")
|
||||
|
||||
|
||||
# Encodes a PayloadV2 to a WakuMessage
|
||||
# Currently, this is just a wrapper over serializePayloadV2 and encryption/decryption is done on top (no KeyInfo)
|
||||
proc encodePayloadV2*(payload2: PayloadV2, contentTopic: ContentTopic = default(ContentTopic)): WakuResult[WakuMessage]
|
||||
{.raises: [Defect, NoiseMalformedHandshake, NoisePublicKeyError].} =
|
||||
|
||||
# We attempt to encode the PayloadV2
|
||||
let serializedPayload2 = serializePayloadV2(payload2)
|
||||
if not serializedPayload2.isOk():
|
||||
return err("Failed to encode PayloadV2")
|
||||
|
||||
# If successful, we create and return a WakuMessage
|
||||
let msg = WakuMessage(payload: serializedPayload2.get(), version: 2, contentTopic: contentTopic)
|
||||
|
||||
return ok(msg)
|
||||
44
waku/v2/utils/noise.nim
Normal file
44
waku/v2/utils/noise.nim
Normal file
@ -0,0 +1,44 @@
|
||||
when (NimMajor, NimMinor) < (1, 4):
|
||||
{.push raises: [Defect].}
|
||||
else:
|
||||
{.push raises: [].}
|
||||
|
||||
import
|
||||
stew/results
|
||||
import
|
||||
../protocol/waku_message,
|
||||
../protocol/waku_noise/noise_types,
|
||||
../protocol/waku_noise/noise_utils
|
||||
|
||||
|
||||
# Decodes a WakuMessage to a PayloadV2
|
||||
# Currently, this is just a wrapper over deserializePayloadV2 and encryption/decryption is done on top (no KeyInfo)
|
||||
proc decodePayloadV2*(message: WakuMessage): Result[PayloadV2, cstring]
|
||||
{.raises: [NoiseMalformedHandshake, NoisePublicKeyError].} =
|
||||
# We check message version (only 2 is supported in this proc)
|
||||
case message.version
|
||||
of 2:
|
||||
# We attempt to decode the WakuMessage payload
|
||||
let deserializedPayload2 = deserializePayloadV2(message.payload)
|
||||
if deserializedPayload2.isOk():
|
||||
return ok(deserializedPayload2.get())
|
||||
else:
|
||||
return err("Failed to decode WakuMessage")
|
||||
else:
|
||||
return err("Wrong message version while decoding payload")
|
||||
|
||||
|
||||
# Encodes a PayloadV2 to a WakuMessage
|
||||
# Currently, this is just a wrapper over serializePayloadV2 and encryption/decryption is done on top (no KeyInfo)
|
||||
proc encodePayloadV2*(payload2: PayloadV2, contentTopic: ContentTopic = default(ContentTopic)): Result[WakuMessage, cstring]
|
||||
{.raises: [NoiseMalformedHandshake, NoisePublicKeyError].} =
|
||||
|
||||
# We attempt to encode the PayloadV2
|
||||
let serializedPayload2 = serializePayloadV2(payload2)
|
||||
if not serializedPayload2.isOk():
|
||||
return err("Failed to encode PayloadV2")
|
||||
|
||||
# If successful, we create and return a WakuMessage
|
||||
let msg = WakuMessage(payload: serializedPayload2.get(), version: 2, contentTopic: contentTopic)
|
||||
|
||||
return ok(msg)
|
||||
Loading…
x
Reference in New Issue
Block a user