generate mixKey if not provided

This commit is contained in:
Prem Chaitanya Prathi 2025-03-25 22:16:57 +05:30
parent fa57dcddef
commit 907f8527b1
5 changed files with 39 additions and 30 deletions

View File

@ -2,9 +2,10 @@ import
chronicles,
chronos,
libp2p/crypto/crypto,
libp2p/crypto/curve25519,
libp2p/multiaddress,
libp2p/nameresolving/dnsresolver,
std/[options, sequtils, net],
std/[options, sequtils, net, strutils],
results
import
../common/utils/nat,
@ -15,7 +16,7 @@ import
./networks_config
proc enrConfiguration*(
conf: WakuConf, netConfig: NetConfig
conf: WakuConf, netConfig: NetConfig, mixPubKey: Option[Curve25519Key]
): Result[enr.Record, string] =
var enrBuilder = EnrBuilder.init(conf.nodeKey)
@ -33,8 +34,8 @@ proc enrConfiguration*(
).isOkOr:
return err("could not initialize ENR with shards")
if conf.mix and conf.mixKey.isSome():
enrBuilder.withMixKey(conf.mixKey.get())
if conf.mix and mixPubKey.isSome():
enrBuilder.withMixKey(mixPubKey.get())
let recordRes = enrBuilder.build()
let record =

View File

@ -6,7 +6,8 @@ import
libp2p/protocols/pubsub/gossipsub,
libp2p/protocols/connectivity/relay/relay,
libp2p/nameresolving/dnsresolver,
libp2p/crypto/crypto
libp2p/crypto/crypto,
libp2p/crypto/curve25519
import
./internal_config,
@ -36,7 +37,8 @@ import
../node/peer_manager/peer_store/migrations as peer_store_sqlite_migrations,
../waku_lightpush_legacy/common,
../common/rate_limit/setting,
../common/databases/dburl
../common/databases/dburl,
../../vendor/mix/src/curve25519
## Peer persistence
@ -145,7 +147,7 @@ proc getAutoshards*(
return ok(autoshards)
proc setupProtocols(
node: WakuNode, conf: WakuConf
node: WakuNode, conf: WakuConf, mixPrivKey: Curve25519Key
): Future[Result[void, string]] {.async.} =
## Setup configured protocols on an existing Waku v2 node.
## Optionally include persistent message storage.
@ -417,15 +419,7 @@ proc setupProtocols(
#mount mix
if conf.mix:
let mixPrivKey:string =
if conf.mixkey.isSome():
conf.mixkey.get()
else:
error "missing mix key"
return err("missing mix key")
(
await node.mountMix(mixPrivKey)
).isOkOr:
(await node.mountMix(mixPrivKey)).isOkOr:
return err("failed to mount waku mix protocol: " & $error)
return ok()
@ -487,6 +481,18 @@ proc startNode*(
proc setupNode*(
wakuConf: WakuConf, rng: ref HmacDrbgContext = crypto.newRng(), relay: Relay
): Result[WakuNode, string] =
var mixPubKey, mixPrivKey: Curve25519Key
if conf.mix:
if conf.mixKey.isSome():
mixPrivKey = intoCurve25519Key(ncrutils.fromHex(conf.mixKey.get()))
mixPubKey = public(mixPrivKey)
else:
warn "missing mix key, generating new"
let keyPairResult = generateKeyPair()
if keyPairResult.isErr:
return err("Generate key pair error: " & $keyPairResult.error)
(mixPrivKey, mixPubKey) = keyPairResult.get()
let netConfig = networkConfiguration(
wakuConf.clusterId, wakuConf.networkConf, wakuConf.discv5Conf,
wakuConf.webSocketConf, wakuConf.wakuFlags, wakuConf.dnsAddrsNameServers,
@ -495,7 +501,7 @@ proc setupNode*(
error "failed to create internal config", error = error
return err("failed to create internal config: " & error)
let record = enrConfiguration(wakuConf, netConfig).valueOr:
let record = enrConfiguration(wakuConf, netConfig, some(mixPubKey)).valueOr:
error "failed to create record", error = error
return err("failed to create record: " & error)
@ -521,7 +527,7 @@ proc setupNode*(
debug "Mounting protocols"
try:
(waitFor node.setupProtocols(wakuConf)).isOkOr:
(waitFor node.setupProtocols(wakuConf, mixPrivKey)).isOkOr:
error "Mounting protocols failed", error = error
return err("Mounting protocols failed: " & error)
except CatchableError:

View File

@ -9,6 +9,7 @@ import
libp2p/protocols/connectivity/relay/client,
libp2p/wire,
libp2p/crypto/crypto,
libp2p/crypto/curve25519,
libp2p/protocols/pubsub/gossipsub,
libp2p/services/autorelayservice,
libp2p/services/hpservice,
@ -266,7 +267,7 @@ proc getRunningNetConfig(waku: ptr Waku): Result[NetConfig, string] =
proc updateEnr(waku: ptr Waku): Result[void, string] =
let netConf: NetConfig = getRunningNetConfig(waku).valueOr:
return err("error calling updateNetConfig: " & $error)
let record = enrConfiguration(waku[].conf, netConf).valueOr:
let record = enrConfiguration(waku[].conf, netConf, none(Curve25519Key)).valueOr:
return err("ENR setup failed: " & error)
if isClusterMismatched(record, waku[].conf.clusterId):

View File

@ -215,7 +215,7 @@ proc mountSharding*(
node.wakuSharding = Sharding(clusterId: clusterId, shardCountGenZero: shardCount)
return ok()
proc getBootStrapMixNodes*(node: WakuNode): Table[PeerId, MixPubInfo] =
#[ proc getBootStrapMixNodes*(node: WakuNode): Table[PeerId, MixPubInfo] =
var mixNodes = initTable[PeerId, MixPubInfo]()
# MixNode Multiaddrs and PublicKeys:
let bootNodesMultiaddrs = [
@ -247,6 +247,8 @@ proc getBootStrapMixNodes*(node: WakuNode): Table[PeerId, MixPubInfo] =
info "using mix bootstrap nodes ", bootNodes = mixNodes
return mixNodes
]#
#TODO: Ideally these procs should be moved out into mix specific file, but keeping it here for now.
proc mixPoolFilter*(cluster: Option[uint16], peer: RemotePeerInfo): bool =
# Note that origin based(discv5) filtering is not done intentionally
@ -331,17 +333,17 @@ proc startMixNodePoolMgr*(node: WakuNode) {.async.} =
proc getMixNodePoolSize*(node: WakuNode): int =
return node.mix.getNodePoolSize()
proc setMixBootStrapNodes*(node: WakuNode) {.async.} =
#[ proc setMixBootStrapNodes*(node: WakuNode,){.async}=
node.mix.setNodePool(node.getBootStrapMixNodes())
]#
# Mix Protocol
proc mountMix*(
node: WakuNode, mixPrivKey: string
node: WakuNode, mixPrivKey: Curve25519Key
): Future[Result[void, string]] {.async.} =
info "mounting mix protocol", nodeId = node.info #TODO log the config used
info "mixPrivKey", mixPrivKey = mixPrivKey
let mixPubKey = public(mixPrivKey)
let mixKey = intoCurve25519Key(ncrutils.fromHex(mixPrivKey))
let mixPubKey = public(mixKey)
info "mixPrivKey", mixPrivKey = mixPrivKey, mixPubKey = mixPubKey
let localaddrStr = node.announcedAddresses[0].toString().valueOr:
return err("Failed to convert multiaddress to string.")
@ -350,7 +352,7 @@ proc mountMix*(
let localMixNodeInfo = initMixNodeInfo(
localaddrStr & "/p2p/" & $node.peerId,
mixPubKey,
mixKey,
mixPrivKey,
node.switch.peerInfo.publicKey.skkey,
node.switch.peerInfo.privateKey.skkey,
)

View File

@ -10,9 +10,8 @@ import ../common/enr
const MixKeyEnrField* = "mix-key"
func withMixKey*(builder: var EnrBuilder, mixPrivKey:string) =
let mixKey = intoCurve25519Key(ncrutils.fromHex(mixPrivKey))
let mixPubKey = public(mixKey)
func withMixKey*(builder: var EnrBuilder, mixPubKey: Curve25519Key) =
builder.addFieldPair(MixKeyEnrField, getBytes(mixPubKey))
func mixKey*(record: TypedRecord): Option[seq[byte]] =