-
Notifications
You must be signed in to change notification settings - Fork 57
/
wakucore.nim
74 lines (58 loc) · 1.78 KB
/
wakucore.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import
std/[options, times],
stew/[results, byteutils],
stew/shims/net,
chronos,
libp2p/switch,
libp2p/builders,
libp2p/crypto/crypto as libp2p_keys,
eth/keys as eth_keys
import waku/waku_core, ./common
export switch
# Time
proc now*(): Timestamp =
getNanosecondTime(getTime().toUnixFloat())
proc ts*(offset = 0, origin = now()): Timestamp =
origin + getNanosecondTime(int64(offset))
# Switch
proc generateEcdsaKey*(): libp2p_keys.PrivateKey =
libp2p_keys.PrivateKey.random(ECDSA, rng[]).get()
proc generateEcdsaKeyPair*(): libp2p_keys.KeyPair =
libp2p_keys.KeyPair.random(ECDSA, rng[]).get()
proc generateSecp256k1Key*(): libp2p_keys.PrivateKey =
libp2p_keys.PrivateKey.random(Secp256k1, rng[]).get()
proc ethSecp256k1Key*(hex: string): eth_keys.PrivateKey =
eth_keys.PrivateKey.fromHex(hex).get()
proc newTestSwitch*(
key = none(libp2p_keys.PrivateKey), address = none(MultiAddress)
): Switch =
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 waku_core.DefaultPubsubTopic, waku_core.DefaultContentTopic
proc fakeWakuMessage*(
payload: string | seq[byte] = "TEST-PAYLOAD",
contentTopic = DefaultContentTopic,
meta: string | seq[byte] = newSeq[byte](),
ts = now(),
ephemeral = false,
): WakuMessage =
var payloadBytes: seq[byte]
var metaBytes: seq[byte]
when payload is string:
payloadBytes = toBytes(payload)
else:
payloadBytes = payload
when meta is string:
metaBytes = toBytes(meta)
else:
metaBytes = meta
WakuMessage(
payload: payloadBytes,
contentTopic: contentTopic,
meta: metaBytes,
version: 2,
timestamp: ts,
ephemeral: ephemeral,
)