Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if wasm and zkey exist #109

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add: check if wasm and zkey exist
  • Loading branch information
s-ekai committed Sep 26, 2023
commit 25998b66bc9297440d8a86ea0a5d4f84ce6fdbd0
35 changes: 35 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -6,6 +6,8 @@ import { ZqField } from 'ffjavascript'

import poseidon from 'poseidon-lite'
import { Identity } from '@semaphore-protocol/identity'
import axios from 'axios'
import fs from 'fs'

/*
This is the "Baby Jubjub" curve described here:
@@ -61,3 +63,36 @@ export function shamirRecovery(x1: bigint, x2: bigint, y1: bigint, y2: bigint):
export function calculateIdentityCommitment(identitySecret: bigint) {
return poseidon([identitySecret])
}

export function isValidUrl(str: string): boolean {
try {
new URL(str)
return true
} catch (_) {
return false
}
}

export async function checkFileExistsOnWeb(url: string): Promise<boolean> {
try {
await axios.head(url);
return true
} catch (error) {
return false
}
}

export async function checkFileExists(path: string): Promise<boolean> {
if (isValidUrl(path)) {
return await checkFileExistsOnWeb(path)
} else {

if (typeof window !== 'undefined' && typeof window.document !== 'undefined') {
throw new Error(
'not allowed to read local files from browser'
)
}

return fs.existsSync(path)
}
}
15 changes: 14 additions & 1 deletion src/rln.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Identity } from '@semaphore-protocol/identity'
import { VerificationKey } from './types'
import { DEFAULT_MERKLE_TREE_DEPTH, calculateIdentitySecret, calculateSignalHash } from './common'
import { DEFAULT_MERKLE_TREE_DEPTH, calculateIdentitySecret, calculateSignalHash, checkFileExists } from './common'
import { IRLNRegistry, ContractRLNRegistry } from './registry'
import { MemoryCache, EvaluatedProof, ICache, Status } from './cache'
import { IMessageIDCounter, MemoryMessageIDCounter } from './message-id-counter'
@@ -224,6 +224,19 @@ export class RLN implements IRLN {
let wasmFilePath: string | Uint8Array | undefined
let finalZkeyPath: string | Uint8Array | undefined
let verificationKey: VerificationKey | undefined

if (typeof args.wasmFilePath === 'string' && !await checkFileExists(args.wasmFilePath)) {
throw new Error(
'the file does not exist at the path for `wasmFilePath`'
)
}

if (typeof args.finalZkeyPath === 'string' && !await checkFileExists(args.finalZkeyPath)) {
throw new Error(
'the file does not exist at the path for `finalZkeyPath`'
)
}

// If `args.wasmFilePath`, `args.finalZkeyPath`, and `args.verificationKey` are not given, see if we have defaults that can be used
if (args.wasmFilePath === undefined && args.finalZkeyPath === undefined && args.verificationKey === undefined) {
const defaultParams = await getDefaultRLNParams(treeDepth)