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