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

Make public folder available for CLI #262

Merged
merged 2 commits into from
Feb 8, 2024
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
1 change: 1 addition & 0 deletions packages/phase2cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@types/winston": "^2.4.4",
"rollup-plugin-auto-external": "^2.0.0",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-typescript2": "^0.34.1",
"solc": "^0.8.19",
"ts-node": "^10.9.1",
Expand Down
4 changes: 4 additions & 0 deletions packages/phase2cli/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from "fs"
import typescript from "rollup-plugin-typescript2"
import autoExternal from "rollup-plugin-auto-external"
import cleanup from "rollup-plugin-cleanup"
import copy from "rollup-plugin-copy"

const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8"))
const banner = `#!/usr/bin/env node
Expand All @@ -24,6 +25,9 @@ export default {
tsconfig: "./build.tsconfig.json",
useTsconfigDeclarationDir: true
}),
(copy as any)({
targets: [{ src: "public/*", dest: "dist/public" }]
}),
cleanup({ comments: "jsdoc" })
]
}
149 changes: 80 additions & 69 deletions packages/phase2cli/src/commands/authBandada.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { Identity } from "@semaphore-protocol/identity"
import { commonTerms } from "@p0tion/actions"
import { httpsCallable } from "firebase/functions"
import { groth16 } from "snarkjs"
import path from "path"
import { dirname } from "path"
import { getAuth, signInWithCustomToken } from "firebase/auth"
import prompts from "prompts"
import { fileURLToPath } from "url"
import theme from "../lib/theme.js"
import { customSpinner } from "../lib/utils.js"
import { VerifiedBandadaResponse } from "../types/index.js"
Expand All @@ -13,85 +15,94 @@ import { bootstrapCommandExecutionAndServices } from "../lib/services.js"
import { addMemberToGroup, isGroupMember } from "../lib/bandada.js"
import {
checkLocalBandadaIdentity,
deleteLocalAccessToken,
deleteLocalBandadaIdentity,
getLocalBandadaIdentity,
setLocalAccessToken,
setLocalBandadaIdentity
} from "../lib/localConfigs.js"
import prompts from "prompts"

const { BANDADA_DASHBOARD_URL, BANDADA_GROUP_ID } = process.env

const authBandada = async () => {
const { firebaseFunctions } = await bootstrapCommandExecutionAndServices()
const spinner = customSpinner(`Checking identity string for Semaphore...`, `clock`)
spinner.start()
// 1. check if _identity string exists in local storage
let identityString: string | unknown
const isIdentityStringStored = checkLocalBandadaIdentity()
if (isIdentityStringStored) {
identityString = getLocalBandadaIdentity()
spinner.succeed(`Identity seed found\n`)
} else {
spinner.warn(`Identity seed not found\n`)
// 2. generate a random _identity string and save it in local storage
const { seed } = await prompts({
type: "text",
name: "seed",
message: theme.text.bold(`Enter a secret string to use as your identity seed in Semaphore:`),
initial: false
})
identityString = seed as string
setLocalBandadaIdentity(identityString as string)
}
// 3. create a semaphore identity with _identity string as a seed
const identity = new Identity(identityString as string)
try {
const { firebaseFunctions } = await bootstrapCommandExecutionAndServices()
const spinner = customSpinner(`Checking identity string for Semaphore...`, `clock`)
spinner.start()
// 1. check if _identity string exists in local storage
let identityString: string | unknown
const isIdentityStringStored = checkLocalBandadaIdentity()
if (isIdentityStringStored) {
identityString = getLocalBandadaIdentity()
spinner.succeed(`Identity seed found\n`)
} else {
spinner.warn(`Identity seed not found\n`)
// 2. generate a random _identity string and save it in local storage
const { seed } = await prompts({
type: "text",
name: "seed",
message: theme.text.bold(`Enter a secret string to use as your identity seed in Semaphore:`),
initial: false
})
identityString = seed as string
setLocalBandadaIdentity(identityString as string)
}
// 3. create a semaphore identity with _identity string as a seed
const identity = new Identity(identityString as string)

// 4. check if the user is a member of the group
console.log(`Checking Bandada membership...`)
const isMember = await isGroupMember(BANDADA_GROUP_ID, identity)
if (!isMember) {
await addMemberToGroup(BANDADA_GROUP_ID, BANDADA_DASHBOARD_URL, identity)
}
// 4. check if the user is a member of the group
console.log(`Checking Bandada membership...`)
const isMember = await isGroupMember(BANDADA_GROUP_ID, identity)
if (!isMember) {
await addMemberToGroup(BANDADA_GROUP_ID, BANDADA_DASHBOARD_URL, identity)
}

// 5. generate a proof that the user owns the commitment.
spinner.text = `Generating proof of identity...`
spinner.start()
// publicSignals = [hash(externalNullifier, identityNullifier), commitment]
const { proof, publicSignals } = await groth16.fullProve(
{
identityTrapdoor: identity.trapdoor,
identityNullifier: identity.nullifier,
externalNullifier: BANDADA_GROUP_ID
},
path.join(path.resolve(), "/public/mini-semaphore.wasm"),
path.join(path.resolve(), "/public/mini-semaphore.zkey")
)
spinner.succeed(`Proof generated.\n`)
spinner.text = `Sending proof to verification...`
spinner.start()
// 6. send proof to a cloud function that verifies it and checks membership
const cf = httpsCallable(firebaseFunctions, commonTerms.cloudFunctionsNames.bandadaValidateProof)
const result = await cf({
proof,
publicSignals
})
const { valid, token, message } = result.data as VerifiedBandadaResponse
if (!valid) {
showError(message, true)
}
spinner.succeed(`Proof verified.\n`)
spinner.text = `Authenticating...`
spinner.start()
// 7. Auth to p0tion firebase
const userCredentials = await signInWithCustomToken(getAuth(), token)
setLocalAccessToken(token)
spinner.succeed(`Authenticated as ${theme.text.bold(userCredentials.user.uid)}.`)
// 5. generate a proof that the user owns the commitment.
spinner.text = `Generating proof of identity...`
spinner.start()
// publicSignals = [hash(externalNullifier, identityNullifier), commitment]
const { proof, publicSignals } = await groth16.fullProve(
{
identityTrapdoor: identity.trapdoor,
identityNullifier: identity.nullifier,
externalNullifier: BANDADA_GROUP_ID
},
`${dirname(fileURLToPath(import.meta.url))}/public/mini-semaphore.wasm`,
`${dirname(fileURLToPath(import.meta.url))}/public/mini-semaphore.zkey`
)
spinner.succeed(`Proof generated.\n`)
spinner.text = `Sending proof to verification...`
spinner.start()
// 6. send proof to a cloud function that verifies it and checks membership
const cf = httpsCallable(firebaseFunctions, commonTerms.cloudFunctionsNames.bandadaValidateProof)
const result = await cf({
proof,
publicSignals
})
const { valid, token, message } = result.data as VerifiedBandadaResponse
if (!valid) {
showError(message, true)
}
spinner.succeed(`Proof verified.\n`)
spinner.text = `Authenticating...`
spinner.start()
// 7. Auth to p0tion firebase
const userCredentials = await signInWithCustomToken(getAuth(), token)
setLocalAccessToken(token)
spinner.succeed(`Authenticated as ${theme.text.bold(userCredentials.user.uid)}.`)

console.log(
`\n${theme.symbols.warning} You can always log out by running the ${theme.text.bold(
`phase2cli logout`
)} command`
)
console.log(
`\n${theme.symbols.warning} You can always log out by running the ${theme.text.bold(
`phase2cli logout`
)} command`
)
} catch (error) {
// Delete local token.
console.log("An error crashed the process. Deleting local token and identity.")
console.error(error)
deleteLocalAccessToken()
deleteLocalBandadaIdentity()
}

process.exit(0)
}
Expand Down
Loading
Loading