generate mixKey if not provided

This commit is contained in:
Prem Chaitanya Prathi 2025-03-25 22:16:57 +05:30
parent 47ad7c87b0
commit 61c234db05
5 changed files with 37 additions and 28 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
./external_config,
@ -16,7 +17,7 @@ import
../waku_core
proc enrConfiguration*(
conf: WakuNodeConf, netConfig: NetConfig, key: crypto.PrivateKey
conf: WakuNodeConf, netConfig: NetConfig, key: crypto.PrivateKey, mixPubKey: Option[Curve25519Key]
): Result[enr.Record, string] =
var enrBuilder = EnrBuilder.init(key)
@ -34,8 +35,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,
@ -37,7 +38,8 @@ import
../waku_lightpush_legacy/common,
../common/utils/parse_size_units,
../common/rate_limit/setting,
../common/databases/dburl
../common/databases/dburl,
../../vendor/mix/src/curve25519
## Peer persistence
@ -155,7 +157,8 @@ proc getAutoshards*(
return ok(autoshards)
proc setupProtocols(
node: WakuNode, conf: WakuNodeConf, nodeKey: crypto.PrivateKey
node: WakuNode, conf: WakuNodeConf, nodeKey: crypto.PrivateKey,
mixPrivKey: Curve25519Key
): Future[Result[void, string]] {.async.} =
## Setup configured protocols on an existing Waku v2 node.
## Optionally include persistent message storage.
@ -428,12 +431,6 @@ 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:
@ -506,11 +503,23 @@ proc setupNode*(
error "Failed to generate key", error = error
return err("Failed to generate key: " & $error)
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(conf, clientId).valueOr:
error "failed to create internal config", error = error
return err("failed to create internal config: " & error)
let record = enrConfiguration(conf, netConfig, key).valueOr:
let record = enrConfiguration(conf, netConfig, key, some(mixPubKey)).valueOr:
error "failed to create record", error = error
return err("failed to create record: " & error)
@ -536,7 +545,7 @@ proc setupNode*(
debug "Mounting protocols"
try:
(waitFor node.setupProtocols(conf, key)).isOkOr:
(waitFor node.setupProtocols(conf, key, 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,
@ -335,7 +336,7 @@ 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, waku[].key).valueOr:
let record = enrConfiguration(waku[].conf, netConf, waku[].key, none(Curve25519Key)).valueOr:
return err("ENR setup failed: " & error)
if isClusterMismatched(record, waku[].conf.clusterId):

View File

@ -219,7 +219,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 = ["/ip4/127.0.0.1/tcp/60001/p2p/16Uiu2HAmPiEs2ozjjJF2iN2Pe2FYeMC9w4caRHKYdLdAfjgbWM6o",
@ -246,7 +246,7 @@ proc getBootStrapMixNodes*(node: WakuNode): Table[PeerId, MixPubInfo] =
mixNodes[peerId] = mixNodePubInfo
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 =
@ -330,23 +330,22 @@ 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): Future[Result[void, string]] {.async.} =
proc mountMix*(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.")
info "local addr", localaddr = localaddrStr
let localMixNodeInfo = initMixNodeInfo(
localaddrStr & "/p2p/" & $node.peerId, mixPubKey, mixKey, node.switch.peerInfo.publicKey.skkey,
localaddrStr & "/p2p/" & $node.peerId, mixPubKey, mixPrivKey, node.switch.peerInfo.publicKey.skkey,
node.switch.peerInfo.privateKey.skkey,
)
# TODO: Pass bootnodes from config,

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]] =