mirror of
https://github.com/logos-messaging/logos-delivery.git
synced 2026-07-26 14:33:27 +00:00
Squash of 13 commits from feat/mix-dos-protection-libp2p-v2.0.0 onto the logos_delivery/ folder-restructure base from #3935 (build-messaging-folder). Original commit history (squashed): - d8e6dcef feat(mix): integrate mix protocol with extended kademlia + RLN spam protection - fb72f18d refactor(mix): split DoS-protection self-registration into background retry - d8bbef0c feat(mix): bump libp2p stack to v2.0.0 + adopt stateless RLN spam protection - 2f24448a fix(tests): use HmacDrbgContext.new() instead of crypto.newRng() - 5a21455c fix(ci): regen nimble.lock for v2.0.0 + disambiguate rng in wakucore - 03ef02a2 fix(tests): wrap HmacDrbgContext via newBearSslRng for libp2p v2.0.0 - 167ab1df fix(nix): regenerate deps.nix from updated nimble.lock - 97a27222 fix(tests): wrap or pass Rng correctly for 3-arg PrivateKey.random - 5561fcb5 fix(tests): replace removed newStandardSwitch with SwitchBuilder - ba39ee4a fix(tests): libp2p v2.0.0 API migrations across test suite - 328e11df fix: gitignore test binaries + remove accidentally-committed binary - cc712444 fix(tests): more v2.0.0 API migrations (rng template, PeerId.random, etc.) - 412d97a9 fix(tests): unblock CI — nph, excise orphan waku_noise, complete v2.0.0 Rng migration Conflict resolutions (#3935 → ours): - 11 import-path migrations: waku/X → logos_delivery/waku/X - waku_node/waku_node/relay.nim: dropped our `registerRelayHandler` proc (relocated to subscription_manager.nim by #3935; see cascade fix below) - factory/builder.nim: combined both sides' new imports (net_config + waku_switch) - factory/conf_builder/mix_conf_builder.nim: libp2p_mix package (not libp2p/protocols/mix) - waku_mix/protocol.nim: combined paths + our mix_rln_spam_protection/relay/nimchronos imports - 3 test files: dropped noise_utils import (replicates noise excision from original PR) - 2 UA file moves: option_shims.nim and waku_mix_coordination.nim added at new paths Cascade fixes (#3935 lost our work, restored): - subscription_manager.nim: added `mixHandler` to #3935's `registerRelayHandler`, and added `waku_mix` to its imports. Without this, mix messages were silently dropped from the relay handler chain. - config.nims: option_shims auto-import path migrated to logos_delivery/... Validation: - nph check on all 82 staged .nim files: clean (0 reformats needed) - wakunode2 build: exit 0, 38 MB binary - (sim PASS confirmed in earlier identical-state run: 5/5 mix init, 5 RLN proofs gen/verify, 0 errors) Backup tag at original tip: backup/3931-pre-3935-rebase (412d97a9).
304 lines
9.8 KiB
Nim
304 lines
9.8 KiB
Nim
{.used.}
|
|
|
|
import std/[os, json], chronos, testutils/unittests
|
|
import logos_delivery/waku/waku_keystore, ./testlib/common
|
|
|
|
procSuite "Credentials test suite":
|
|
let testAppInfo = AppInfo(application: "test", appIdentifier: "1234", version: "0.1")
|
|
|
|
test "Create keystore":
|
|
let filepath = "./testAppKeystore.txt"
|
|
defer:
|
|
removeFile(filepath)
|
|
|
|
let keystoreRes = createAppKeystore(path = filepath, appInfo = testAppInfo)
|
|
|
|
check:
|
|
keystoreRes.isOk()
|
|
|
|
test "Load keystore":
|
|
let filepath = "./testAppKeystore.txt"
|
|
defer:
|
|
removeFile(filepath)
|
|
|
|
# If no keystore exists at filepath, a new one is created for appInfo and empty credentials
|
|
let keystoreRes = loadAppKeystore(path = filepath, appInfo = testAppInfo)
|
|
|
|
check:
|
|
keystoreRes.isOk()
|
|
|
|
let keystore = keystoreRes.get()
|
|
|
|
check:
|
|
keystore.hasKeys(["application", "appIdentifier", "version", "credentials"])
|
|
keystore["application"].getStr() == testAppInfo.application
|
|
keystore["appIdentifier"].getStr() == testAppInfo.appIdentifier
|
|
keystore["version"].getStr() == testAppInfo.version
|
|
# We assume the loaded keystore to not have credentials set (previous tests delete the keystore at filepath)
|
|
keystore["credentials"].len() == 0
|
|
|
|
test "Add credentials to keystore":
|
|
let filepath = "./testAppKeystore.txt"
|
|
defer:
|
|
removeFile(filepath)
|
|
|
|
# We generate a random identity credential (inter-value constrains are not enforced, otherwise we need to load e.g. zerokit RLN keygen)
|
|
var
|
|
idTrapdoor = randomSeqByte(rng[], 32)
|
|
idNullifier = randomSeqByte(rng[], 32)
|
|
idSecretHash = randomSeqByte(rng[], 32)
|
|
idCommitment = randomSeqByte(rng[], 32)
|
|
|
|
var idCredential = IdentityCredential(
|
|
idTrapdoor: idTrapdoor,
|
|
idNullifier: idNullifier,
|
|
idSecretHash: idSecretHash,
|
|
idCommitment: idCommitment,
|
|
)
|
|
|
|
var contract = MembershipContract(
|
|
chainId: "5", address: "0x0123456789012345678901234567890123456789"
|
|
)
|
|
var index = MembershipIndex(1)
|
|
|
|
let membershipCredential = KeystoreMembership(
|
|
membershipContract: contract, treeIndex: index, identityCredential: idCredential
|
|
)
|
|
let password = "%m0um0ucoW%"
|
|
|
|
let keystoreRes = addMembershipCredentials(
|
|
path = filepath,
|
|
membership = membershipCredential,
|
|
password = password,
|
|
appInfo = testAppInfo,
|
|
)
|
|
|
|
check:
|
|
keystoreRes.isOk()
|
|
|
|
test "Add/retrieve credentials in keystore":
|
|
let filepath = "./testAppKeystore.txt"
|
|
defer:
|
|
removeFile(filepath)
|
|
|
|
# We generate two random identity credentials (inter-value constrains are not enforced, otherwise we need to load e.g. zerokit RLN keygen)
|
|
var
|
|
idTrapdoor = randomSeqByte(rng[], 32)
|
|
idNullifier = randomSeqByte(rng[], 32)
|
|
idSecretHash = randomSeqByte(rng[], 32)
|
|
idCommitment = randomSeqByte(rng[], 32)
|
|
idCredential = IdentityCredential(
|
|
idTrapdoor: idTrapdoor,
|
|
idNullifier: idNullifier,
|
|
idSecretHash: idSecretHash,
|
|
idCommitment: idCommitment,
|
|
)
|
|
|
|
# We generate two distinct membership groups
|
|
var contract = MembershipContract(
|
|
chainId: "5", address: "0x0123456789012345678901234567890123456789"
|
|
)
|
|
var index = MembershipIndex(1)
|
|
var membershipCredential = KeystoreMembership(
|
|
membershipContract: contract, treeIndex: index, identityCredential: idCredential
|
|
)
|
|
|
|
let password = "%m0um0ucoW%"
|
|
|
|
# We add credentials to the keystore. Note that only 3 credentials should be effectively added, since rlnMembershipCredentials3 is equal to membershipCredentials2
|
|
let keystoreRes = addMembershipCredentials(
|
|
path = filepath,
|
|
membership = membershipCredential,
|
|
password = password,
|
|
appInfo = testAppInfo,
|
|
)
|
|
|
|
check:
|
|
keystoreRes.isOk()
|
|
|
|
# We test retrieval of credentials.
|
|
var expectedMembership = membershipCredential
|
|
let membershipQuery =
|
|
KeystoreMembership(membershipContract: contract, treeIndex: index)
|
|
|
|
var recoveredCredentialsRes = getMembershipCredentials(
|
|
path = filepath,
|
|
password = password,
|
|
query = membershipQuery,
|
|
appInfo = testAppInfo,
|
|
)
|
|
|
|
check:
|
|
recoveredCredentialsRes.isOk()
|
|
recoveredCredentialsRes.get() == expectedMembership
|
|
|
|
test "if the keystore contains only one credential, fetch that irrespective of treeIndex":
|
|
let filepath = "./testAppKeystore.txt"
|
|
defer:
|
|
removeFile(filepath)
|
|
|
|
# We generate random identity credentials (inter-value constrains are not enforced, otherwise we need to load e.g. zerokit RLN keygen)
|
|
let
|
|
idTrapdoor = randomSeqByte(rng[], 32)
|
|
idNullifier = randomSeqByte(rng[], 32)
|
|
idSecretHash = randomSeqByte(rng[], 32)
|
|
idCommitment = randomSeqByte(rng[], 32)
|
|
idCredential = IdentityCredential(
|
|
idTrapdoor: idTrapdoor,
|
|
idNullifier: idNullifier,
|
|
idSecretHash: idSecretHash,
|
|
idCommitment: idCommitment,
|
|
)
|
|
|
|
let contract = MembershipContract(
|
|
chainId: "5", address: "0x0123456789012345678901234567890123456789"
|
|
)
|
|
let index = MembershipIndex(1)
|
|
let membershipCredential = KeystoreMembership(
|
|
membershipContract: contract, treeIndex: index, identityCredential: idCredential
|
|
)
|
|
|
|
let password = "%m0um0ucoW%"
|
|
|
|
let keystoreRes = addMembershipCredentials(
|
|
path = filepath,
|
|
membership = membershipCredential,
|
|
password = password,
|
|
appInfo = testAppInfo,
|
|
)
|
|
|
|
assert(keystoreRes.isOk(), $keystoreRes.error)
|
|
|
|
# We test retrieval of credentials.
|
|
let expectedMembership = membershipCredential
|
|
let membershipQuery = KeystoreMembership(membershipContract: contract)
|
|
|
|
let recoveredCredentialsRes = getMembershipCredentials(
|
|
path = filepath,
|
|
password = password,
|
|
query = membershipQuery,
|
|
appInfo = testAppInfo,
|
|
)
|
|
|
|
assert(recoveredCredentialsRes.isOk(), $recoveredCredentialsRes.error)
|
|
check:
|
|
recoveredCredentialsRes.get() == expectedMembership
|
|
|
|
test "if the keystore contains multiple credentials, then error out if treeIndex has not been passed in":
|
|
let filepath = "./testAppKeystore.txt"
|
|
defer:
|
|
removeFile(filepath)
|
|
|
|
# We generate random identity credentials (inter-value constrains are not enforced, otherwise we need to load e.g. zerokit RLN keygen)
|
|
let
|
|
idTrapdoor = randomSeqByte(rng[], 32)
|
|
idNullifier = randomSeqByte(rng[], 32)
|
|
idSecretHash = randomSeqByte(rng[], 32)
|
|
idCommitment = randomSeqByte(rng[], 32)
|
|
idCredential = IdentityCredential(
|
|
idTrapdoor: idTrapdoor,
|
|
idNullifier: idNullifier,
|
|
idSecretHash: idSecretHash,
|
|
idCommitment: idCommitment,
|
|
)
|
|
|
|
# We generate two distinct membership groups
|
|
let contract = MembershipContract(
|
|
chainId: "5", address: "0x0123456789012345678901234567890123456789"
|
|
)
|
|
let index = MembershipIndex(1)
|
|
var membershipCredential = KeystoreMembership(
|
|
membershipContract: contract, treeIndex: index, identityCredential: idCredential
|
|
)
|
|
|
|
let password = "%m0um0ucoW%"
|
|
|
|
let keystoreRes = addMembershipCredentials(
|
|
path = filepath,
|
|
membership = membershipCredential,
|
|
password = password,
|
|
appInfo = testAppInfo,
|
|
)
|
|
|
|
assert(keystoreRes.isOk(), $keystoreRes.error)
|
|
|
|
membershipCredential.treeIndex = MembershipIndex(2)
|
|
let keystoreRes2 = addMembershipCredentials(
|
|
path = filepath,
|
|
membership = membershipCredential,
|
|
password = password,
|
|
appInfo = testAppInfo,
|
|
)
|
|
assert(keystoreRes2.isOk(), $keystoreRes2.error)
|
|
|
|
# We test retrieval of credentials.
|
|
let membershipQuery = KeystoreMembership(membershipContract: contract)
|
|
|
|
let recoveredCredentialsRes = getMembershipCredentials(
|
|
path = filepath,
|
|
password = password,
|
|
query = membershipQuery,
|
|
appInfo = testAppInfo,
|
|
)
|
|
|
|
check:
|
|
recoveredCredentialsRes.isErr()
|
|
recoveredCredentialsRes.error.kind == KeystoreCredentialNotFoundError
|
|
|
|
test "if a keystore exists, but the keystoreQuery doesn't match it":
|
|
let filepath = "./testAppKeystore.txt"
|
|
defer:
|
|
removeFile(filepath)
|
|
|
|
# We generate random identity credentials (inter-value constrains are not enforced, otherwise we need to load e.g. zerokit RLN keygen)
|
|
let
|
|
idTrapdoor = randomSeqByte(rng[], 32)
|
|
idNullifier = randomSeqByte(rng[], 32)
|
|
idSecretHash = randomSeqByte(rng[], 32)
|
|
idCommitment = randomSeqByte(rng[], 32)
|
|
idCredential = IdentityCredential(
|
|
idTrapdoor: idTrapdoor,
|
|
idNullifier: idNullifier,
|
|
idSecretHash: idSecretHash,
|
|
idCommitment: idCommitment,
|
|
)
|
|
|
|
# We generate two distinct membership groups
|
|
let contract = MembershipContract(
|
|
chainId: "5", address: "0x0123456789012345678901234567890123456789"
|
|
)
|
|
let index = MembershipIndex(1)
|
|
var membershipCredential = KeystoreMembership(
|
|
membershipContract: contract, treeIndex: index, identityCredential: idCredential
|
|
)
|
|
|
|
let password = "%m0um0ucoW%"
|
|
|
|
let keystoreRes = addMembershipCredentials(
|
|
path = filepath,
|
|
membership = membershipCredential,
|
|
password = password,
|
|
appInfo = testAppInfo,
|
|
)
|
|
|
|
assert(keystoreRes.isOk(), $keystoreRes.error)
|
|
|
|
let badTestAppInfo =
|
|
AppInfo(application: "_bad_test_", appIdentifier: "1234", version: "0.1")
|
|
|
|
# We test retrieval of credentials.
|
|
let membershipQuery = KeystoreMembership(membershipContract: contract)
|
|
|
|
let recoveredCredentialsRes = getMembershipCredentials(
|
|
path = filepath,
|
|
password = password,
|
|
query = membershipQuery,
|
|
appInfo = badTestAppInfo,
|
|
)
|
|
|
|
check:
|
|
recoveredCredentialsRes.isErr()
|
|
recoveredCredentialsRes.error.kind == KeystoreJsonValueMismatchError
|
|
recoveredCredentialsRes.error.msg ==
|
|
"Application does not match. Expected '_bad_test_' but got 'test'"
|