2023-02-13 10:43:49 +00:00
|
|
|
import
|
2023-04-19 14:39:52 +00:00
|
|
|
std/[options, times],
|
2023-04-05 14:01:51 +00:00
|
|
|
stew/[results, byteutils],
|
|
|
|
stew/shims/net,
|
|
|
|
chronos,
|
2023-02-13 10:43:49 +00:00
|
|
|
libp2p/switch,
|
2023-03-10 00:33:52 +00:00
|
|
|
libp2p/builders,
|
|
|
|
libp2p/crypto/crypto as libp2p_keys,
|
|
|
|
eth/keys as eth_keys
|
2024-03-15 23:08:47 +00:00
|
|
|
import ../../../waku/waku_core, ./common
|
2023-02-13 10:43:49 +00:00
|
|
|
|
|
|
|
export switch
|
|
|
|
|
2023-04-19 14:39:52 +00:00
|
|
|
# Time
|
|
|
|
|
|
|
|
proc now*(): Timestamp =
|
|
|
|
getNanosecondTime(getTime().toUnixFloat())
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc ts*(offset = 0, origin = now()): Timestamp =
|
2024-03-01 12:58:45 +00:00
|
|
|
origin + getNanosecondTime(int64(offset))
|
2023-04-19 14:39:52 +00:00
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
# Switch
|
|
|
|
|
2023-03-10 00:33:52 +00:00
|
|
|
proc generateEcdsaKey*(): libp2p_keys.PrivateKey =
|
|
|
|
libp2p_keys.PrivateKey.random(ECDSA, rng[]).get()
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2023-03-10 00:33:52 +00:00
|
|
|
proc generateEcdsaKeyPair*(): libp2p_keys.KeyPair =
|
|
|
|
libp2p_keys.KeyPair.random(ECDSA, rng[]).get()
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2023-03-10 00:33:52 +00:00
|
|
|
proc generateSecp256k1Key*(): libp2p_keys.PrivateKey =
|
|
|
|
libp2p_keys.PrivateKey.random(Secp256k1, rng[]).get()
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2023-03-10 00:33:52 +00:00
|
|
|
proc ethSecp256k1Key*(hex: string): eth_keys.PrivateKey =
|
|
|
|
eth_keys.PrivateKey.fromHex(hex).get()
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc newTestSwitch*(
|
|
|
|
key = none(libp2p_keys.PrivateKey), address = none(MultiAddress)
|
|
|
|
): Switch =
|
2023-02-13 10:43:49 +00:00
|
|
|
let peerKey = key.get(generateSecp256k1Key())
|
|
|
|
let peerAddr = address.get(MultiAddress.init("/ip4/127.0.0.1/tcp/0").get())
|
2024-03-15 23:08:47 +00:00
|
|
|
return newStandardSwitch(some(peerKey), addrs = peerAddr)
|
2023-02-13 10:43:49 +00:00
|
|
|
|
|
|
|
# Waku message
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
export waku_core.DefaultPubsubTopic, waku_core.DefaultContentTopic
|
2023-02-13 10:43:49 +00:00
|
|
|
|
|
|
|
proc fakeWakuMessage*(
|
2024-03-15 23:08:47 +00:00
|
|
|
payload: string | seq[byte] = "TEST-PAYLOAD",
|
|
|
|
contentTopic = DefaultContentTopic,
|
2024-05-06 08:20:21 +00:00
|
|
|
meta: string | seq[byte] = newSeq[byte](),
|
2024-03-15 23:08:47 +00:00
|
|
|
ts = now(),
|
|
|
|
ephemeral = false,
|
2023-02-13 10:43:49 +00:00
|
|
|
): WakuMessage =
|
|
|
|
var payloadBytes: seq[byte]
|
2024-05-06 08:20:21 +00:00
|
|
|
var metaBytes: seq[byte]
|
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
when payload is string:
|
|
|
|
payloadBytes = toBytes(payload)
|
|
|
|
else:
|
|
|
|
payloadBytes = payload
|
|
|
|
|
2024-05-06 08:20:21 +00:00
|
|
|
when meta is string:
|
|
|
|
metaBytes = toBytes(meta)
|
|
|
|
else:
|
|
|
|
metaBytes = meta
|
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
WakuMessage(
|
|
|
|
payload: payloadBytes,
|
|
|
|
contentTopic: contentTopic,
|
2024-05-06 08:20:21 +00:00
|
|
|
meta: metaBytes,
|
2023-02-13 10:43:49 +00:00
|
|
|
version: 2,
|
|
|
|
timestamp: ts,
|
2024-03-15 23:08:47 +00:00
|
|
|
ephemeral: ephemeral,
|
2023-02-13 10:43:49 +00:00
|
|
|
)
|