mirror of
https://github.com/logos-messaging/logos-delivery.git
synced 2026-06-04 05:00:02 +00:00
* bump libp2p pin to release/v2.0.0 (c43199378) * pin nimble.lock: lsquic/websock/boringssl/protobuf_serialization/npeg/jwt * add libp2p_mix dep and point libp2p/protocols/mix -> libp2p_mix * migrate rng to libp2p Rng type (prod, channels, noise, tests) * noise: take Rng, extract bearSslDrbg internally * waku_switch: TransportConfig factory; withMaxInOut; local MaxConnections * waku_relay/rendezvous/discv5/kademlia: v2.0.0 API (rng, config, ServiceDiscovery) * tests: newStandardSwitch shim; PeerId.random(rng); common.rng()/crypto.newRng() * drop libp2p/utils/semaphore (use chronos AsyncSemaphore) * add waku/compat/option_valueor shim where needed * add std/options where transitive re-export dropped
92 lines
2.2 KiB
Nim
92 lines
2.2 KiB
Nim
import
|
|
std/[options, times],
|
|
results,
|
|
stew/byteutils,
|
|
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, common.rng()).get()
|
|
|
|
proc generateEcdsaKeyPair*(): libp2p_keys.KeyPair =
|
|
libp2p_keys.KeyPair.random(ECDSA, common.rng()).get()
|
|
|
|
proc generateSecp256k1Key*(): libp2p_keys.PrivateKey =
|
|
libp2p_keys.PrivateKey.random(Secp256k1, common.rng()).get()
|
|
|
|
proc ethSecp256k1Key*(hex: string): eth_keys.PrivateKey =
|
|
eth_keys.PrivateKey.fromHex(hex).get()
|
|
|
|
proc newStandardSwitch*(
|
|
privKey = Opt.none(libp2p_keys.PrivateKey),
|
|
addrs: MultiAddress = MultiAddress.init("/ip4/127.0.0.1/tcp/0").get(),
|
|
): Switch =
|
|
var b = SwitchBuilder
|
|
.new()
|
|
.withRng(common.rng())
|
|
.withAddress(addrs)
|
|
.withTcpTransport()
|
|
.withMplex()
|
|
.withNoise()
|
|
if privKey.isSome():
|
|
b = b.withPrivateKey(privKey.get())
|
|
b.build()
|
|
|
|
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(privKey = Opt.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,
|
|
proof = newSeq[byte](),
|
|
): 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,
|
|
proof: proof,
|
|
)
|