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
|
2023-02-13 10:43:49 +00:00
|
|
|
import
|
2023-08-09 17:11:50 +00:00
|
|
|
../../../waku/waku_core,
|
2023-02-13 10:43:49 +00:00
|
|
|
./common
|
|
|
|
|
|
|
|
export switch
|
|
|
|
|
|
|
|
|
2023-04-19 14:39:52 +00:00
|
|
|
# Time
|
|
|
|
|
|
|
|
proc now*(): Timestamp =
|
|
|
|
getNanosecondTime(getTime().toUnixFloat())
|
|
|
|
|
|
|
|
proc ts*(offset=0, origin=now()): Timestamp =
|
|
|
|
origin + getNanosecondTime(offset)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-03-10 00:33:52 +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())
|
|
|
|
return newStandardSwitch(some(peerKey), addrs=peerAddr)
|
|
|
|
|
|
|
|
|
|
|
|
# Waku message
|
|
|
|
|
|
|
|
export
|
2023-04-19 11:29:23 +00:00
|
|
|
waku_core.DefaultPubsubTopic,
|
|
|
|
waku_core.DefaultContentTopic
|
2023-02-13 10:43:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc fakeWakuMessage*(
|
|
|
|
payload: string|seq[byte] = "TEST-PAYLOAD",
|
|
|
|
contentTopic = DefaultContentTopic,
|
2023-03-02 10:41:41 +00:00
|
|
|
meta = newSeq[byte](),
|
2023-02-13 10:43:49 +00:00
|
|
|
ts = now(),
|
|
|
|
ephemeral = false
|
|
|
|
): WakuMessage =
|
|
|
|
var payloadBytes: seq[byte]
|
|
|
|
when payload is string:
|
|
|
|
payloadBytes = toBytes(payload)
|
|
|
|
else:
|
|
|
|
payloadBytes = payload
|
|
|
|
|
|
|
|
WakuMessage(
|
|
|
|
payload: payloadBytes,
|
|
|
|
contentTopic: contentTopic,
|
2023-03-02 10:41:41 +00:00
|
|
|
meta: meta,
|
2023-02-13 10:43:49 +00:00
|
|
|
version: 2,
|
|
|
|
timestamp: ts,
|
|
|
|
ephemeral: ephemeral
|
|
|
|
)
|