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
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 35 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ZqField } from 'ffjavascript'

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

/*
This is the "Baby Jubjub" curve described here:
Expand Down Expand Up @@ -61,3 +62,37 @@ 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 checkFileExistsOnWeb(path)
} else {

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

const fs = await import('fs')
return fs.existsSync(path)
}
}
28 changes: 27 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'
Expand Down Expand Up @@ -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\`: wasmFilePath=${args.wasmFilePath}`,
)
}

if (typeof args.finalZkeyPath === 'string' && !await checkFileExists(args.finalZkeyPath)) {
throw new Error(
`the file does not exist at the path for \`finalZkeyPath\`: finalZkeyPath=${args.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)
Expand Down Expand Up @@ -346,6 +359,19 @@ export class RLN implements IRLN {
// If all params are not given, use the default
let withdrawWasmFilePath: string | Uint8Array | undefined
let withdrawFinalZkeyPath: string | Uint8Array | undefined

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

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

// If `args.withdrawWasmFilePath`, `args.finalZkeyPath`, see if we have defaults that can be used
if (args.withdrawWasmFilePath === undefined && args.withdrawFinalZkeyPath === undefined) {
const defaultParams = await getDefaultWithdrawParams()
Expand Down
68 changes: 68 additions & 0 deletions tests/rln.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,74 @@ describe("RLN", function () {
);
});

test("should fail when wasmFilePath doesn't exist on the web", async function () {
const wasmFilePath = "https://rln-trusted-setup-ceremony-pse-p0tion-production.s3.eu-central-1.amazonaws.com/404"
const finalZkeyPath = "https://rln-trusted-setup-ceremony-pse-p0tion-production.s3.eu-central-1.amazonaws.com/404"
await expect(async () => {
await RLN.createWithContractRegistry({
rlnIdentifier: rlnIdentifierA,
treeDepth: treeDepthWithoutDefaultParams,
provider: fakeProvider,
contractAddress: fakeContractAddress,
wasmFilePath: wasmFilePath,
finalZkeyPath: finalZkeyPath
});
}).rejects.toThrow(
`the file does not exist at the path for \`wasmFilePath\`: wasmFilePath=${wasmFilePath}`
);
});

test("should fail when finalZkeyPath doesn't exist on the web", async function () {
const wasmFilePath = "https://rln-trusted-setup-ceremony-pse-p0tion-production.s3.eu-central-1.amazonaws.com/circuits/rln-20/RLN-20.wasm"
const finalZkeyPath = "https://rln-trusted-setup-ceremony-pse-p0tion-production.s3.eu-central-1.amazonaws.com/404"
await expect(async () => {
await RLN.createWithContractRegistry({
rlnIdentifier: rlnIdentifierA,
treeDepth: treeDepthWithoutDefaultParams,
provider: fakeProvider,
contractAddress: fakeContractAddress,
wasmFilePath: wasmFilePath,
finalZkeyPath: finalZkeyPath
});
}).rejects.toThrow(
`the file does not exist at the path for \`finalZkeyPath\`: finalZkeyPath=${finalZkeyPath}`
);
});

test("should fail when wasmFilePath doesn't exist on local", async function () {
const wasmFilePath = "./404"
const finalZkeyPath = "./404"
await expect(async () => {
await RLN.createWithContractRegistry({
rlnIdentifier: rlnIdentifierA,
treeDepth: treeDepthWithoutDefaultParams,
provider: fakeProvider,
contractAddress: fakeContractAddress,
wasmFilePath: wasmFilePath,
finalZkeyPath: finalZkeyPath
});
}).rejects.toThrow(
`the file does not exist at the path for \`wasmFilePath\`: wasmFilePath=${wasmFilePath}`
);
});

test("should fail when finalZkeyPath doesn't exist on local", async function () {
const wasmFilePath = "./package.json"
const finalZkeyPath = "./404"
await expect(async () => {
await RLN.createWithContractRegistry({
rlnIdentifier: rlnIdentifierA,
treeDepth: treeDepthWithoutDefaultParams,
provider: fakeProvider,
contractAddress: fakeContractAddress,
wasmFilePath: wasmFilePath,
finalZkeyPath: finalZkeyPath
});
}).rejects.toThrow(
`the file does not exist at the path for \`finalZkeyPath\`: finalZkeyPath=${finalZkeyPath}`
);
});

test("should fail to prove if no proving params is given as constructor arguments", async function () {
const rln = await RLN.createWithContractRegistry({
rlnIdentifier: rlnIdentifierA,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "ES5",
"module": "ES6",
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"allowSyntheticDefaultImports": true,
Expand Down
Loading