Skip to content

Commit

Permalink
upd fallback provider usage, fix build error
Browse files Browse the repository at this point in the history
  • Loading branch information
lukachi committed Apr 10, 2024
1 parent 3bb6020 commit f08a0a0
Show file tree
Hide file tree
Showing 22 changed files with 209 additions and 552 deletions.
19 changes: 10 additions & 9 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<title>Voting App</title>
<title>Freedomtool App</title>

<noscript>
<style>
Expand All @@ -16,15 +16,17 @@
<meta name="theme-color" content="#ffffff">

<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0">
<meta name="description" content="Manage your identity credentials and Soulbound Tokens (SBTs) easily from the Voting App">
<meta property="og:title" content="Voting App">
<meta property="og:description" content="Manage your identity credentials and Soulbound Tokens (SBTs) easily from the Voting App.">
<!-- TODO: fix link to the actual one -->
<meta property="og:url" content="https://app.example.com">
<meta property="og:image" content="https://app.example.com/branding/logo.png">
<meta name="description" content="Freedomtool App">
<meta property="og:title" content="Freedomtool App">
<meta property="og:description" content="Freedomtool App">
<meta property="og:url" content="<%- host %>">
<meta property="og:image" content="<%- host %>/branding/og-img.png">
<meta property="og:locale" content="en_GB">
<meta property="og:type" content="website">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="<%- host %>/branding/og-img.png">
<meta name="twitter:title" content="Freedomtool App">
<meta name="twitter:description" content="Freedomtool App">

<link rel="apple-touch-icon" sizes="180x180" href="/branding/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/branding/favicon-32x32.png">
Expand All @@ -40,13 +42,12 @@

<link href="/noscript/style.css" type="text/css" rel="stylesheet">
<link href="/init-loader/style.css" type="text/css" rel="stylesheet">
<script type="module" src="/src/main.tsx"></script>
</head>

<body>
<div id="root">
<div id="noscript" class="app__init">
<img src="/branding/logo.svg" alt="logo" width="192">
<img src="/branding/logo.svg" alt="Freedomtool App logo" width="192">
</div>
</div>
<div id="modal"></div>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@mui/x-date-pickers": "^6.18.7",
"copy-to-clipboard": "^3.3.3",
"ejc": "^1.0.4",
"ethers": "^5.7.2",
"ethers": "5.7.2",
"i18next": "^22.4.3",
"jdenticon": "^3.2.0",
"lodash": "^4.17.21",
Expand Down
13 changes: 2 additions & 11 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { CircularProgress, CssBaseline, Stack, ThemeProvider } from '@mui/material'
import { FC, HTMLAttributes, memo, useCallback, useEffect, useMemo, useState } from 'react'

import { ToastsManager, useWeb3Context } from '@/contexts'
import { ErrorHandler } from '@/helpers'
import { ToastsManager } from '@/contexts'
import { useViewportSizes } from '@/hooks'
import { AppRoutes } from '@/routes'
import { useUiState } from '@/store'
Expand All @@ -11,21 +10,13 @@ import { createTheme } from '@/theme'
const App: FC<HTMLAttributes<HTMLDivElement>> = () => {
const [isAppInitialized, setIsAppInitialized] = useState(false)

const { init: initWeb3Provider } = useWeb3Context()
const { paletteMode } = useUiState()

useViewportSizes()

const init = useCallback(async () => {
try {
/* empty */
await initWeb3Provider()
} catch (error) {
ErrorHandler.processWithoutFeedback(error)
}

setIsAppInitialized(true)
}, [initWeb3Provider])
}, [])

const theme = useMemo(() => createTheme(paletteMode), [paletteMode])

Expand Down
59 changes: 59 additions & 0 deletions src/api/modules/verify/helpers/contracts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { config } from '@config'
import type { RawProvider } from '@distributedlab/w3p'
import type { Provider } from '@ethersproject/abstract-provider'
import type { Signer } from '@ethersproject/abstract-signer'
import { type ExternalProvider, JsonRpcProvider, Web3Provider } from '@ethersproject/providers'

import {
RegisterVerifier__factory,
Voting__factory,
VotingRegistration__factory,
VotingRegistry__factory,
} from '@/types'

type AbstractFactoryClass = {
connect: (address: string, signerOrProvider: Signer | Provider) => unknown
createInterface: () => unknown
}

type AbstractFactoryClassReturnType<F extends AbstractFactoryClass> = {
contractInstance: ReturnType<F['connect']>
contractInterface: ReturnType<F['createInterface']>
}

const createContract = <F extends AbstractFactoryClass>(
address: string,
provider: RawProvider | undefined,
factoryClass: F,
): AbstractFactoryClassReturnType<F> => {
const fallbackProvider = new JsonRpcProvider(config.RPC_URL)

const targetProvider = provider
? new Web3Provider(provider as ExternalProvider, 'any')
: fallbackProvider

const contractInstance = factoryClass.connect(address, targetProvider) as ReturnType<F['connect']>

const contractInterface = factoryClass.createInterface() as ReturnType<F['createInterface']>

return {
contractInstance,
contractInterface,
}
}

export const createRegisterVerifierContract = (address: string, provider?: RawProvider) => {
return createContract(address, provider, RegisterVerifier__factory)
}

export const createVotingContract = (address: string, provider?: RawProvider) => {
return createContract(address, provider, Voting__factory)
}

export const createVotingRegistrationContract = (address: string, provider?: RawProvider) => {
return createContract(address, provider, VotingRegistration__factory)
}

export const createVotingRegistryContract = (address: string, provider?: RawProvider) => {
return createContract(address, provider, VotingRegistry__factory)
}
105 changes: 0 additions & 105 deletions src/api/modules/verify/helpers/zkp.ts
Original file line number Diff line number Diff line change
@@ -1,114 +1,9 @@
import { config } from '@config'
import { Poseidon } from '@iden3/js-crypto'
import { Token } from '@iden3/js-jwz'
import { BytesLike, utils } from 'ethers'
import get from 'lodash/get'
import { groth16 } from 'snarkjs'

import { api } from '@/api/clients'
import { ClaimTypes, ClaimTypesMapOnChain, ProofRequestResponse } from '@/api/modules/verify'
import { VerifierHelper } from '@/types/contracts/Voting'

export type SecretPair = {
secret: string
nullifier: string
}

function splitHexIntoChunks(hexString: string, chunkSize = 64) {
const regex = new RegExp(`.{1,${chunkSize}}`, 'g')
const chunks = hexString.match(regex)

if (!chunks) {
throw new Error('Invalid hex string')
}

return chunks.map(chunk => '0x' + chunk)
}

export function poseidonHash(data: string): string {
data = utils.hexlify(data)
const chunks = splitHexIntoChunks(data.replace('0x', ''), 64)
const inputs = chunks.map(v => BigInt(v))

// FIXME
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return utils.hexZeroPad(Poseidon.hash(inputs), 32)
}

export function generateSecrets(): SecretPair {
const secret = utils.randomBytes(31)
const nullifier = utils.randomBytes(31)

return {
secret: padElement(utils.hexlify(secret)),
nullifier: padElement(utils.hexlify(nullifier)),
}
}

export function getCommitment(pair: SecretPair): string {
return poseidonHash(pair.secret + pair.nullifier.replace('0x', ''))
}

function padElement(element: BytesLike) {
return utils.hexZeroPad(element, 32)
}

export function getNullifierHash(pair: SecretPair): string {
return poseidonHash(pair.nullifier)
}

export async function getVoteZKP(
pair: SecretPair,
root: string,
candidateHash: string,
votingAddress: string,
siblings: string[],
) {
const nullifierHash = getNullifierHash(pair)

const { proof, publicSignals } = await groth16.fullProve(
{
root: BigInt(root),
vote: candidateHash,
votingAddress,
secret: pair.secret,
nullifier: pair.nullifier,
siblings,
},
`./test/circuits/voting.wasm`,
`./test/circuits/voting.zkey`,
)

swap(proof.pi_b[0], 0, 1)
swap(proof.pi_b[1], 0, 1)

const a = proof.pi_a.slice(0, 2).map(x => padElement(x)) as [string, string]
const b = proof.pi_b.slice(0, 2).map(x => x.map(y => padElement(y))) as [
[string, string],
[string, string],
]
const c = proof.pi_c.slice(0, 2).map(x => padElement(x)) as [string, string]

const formattedProof: VerifierHelper.ProofPointsStruct = {
a,
b,
c,
}

return {
publicSignals,
formattedProof,
nullifierHash,
}
}

// Function to swap two elements in an array
function swap(arr: unknown[], i: number, j: number) {
const temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}

export const buildAppRequest = async <T extends ClaimTypes>(opts: ClaimTypesMapOnChain[T]) => {
const { data } = await api.get<{
Expand Down
1 change: 0 additions & 1 deletion src/contexts/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { default as ToastsManager } from './toasts-manager'
export * from './web3'
Loading

0 comments on commit f08a0a0

Please sign in to comment.