Skip to content

Commit

Permalink
Merge pull request #79 from BigWhaleLabs/send-ready-event-every-second
Browse files Browse the repository at this point in the history
send Ready event every second and remove old listeners
  • Loading branch information
MixailE authored Oct 31, 2023
2 parents 412d8a9 + 301e250 commit a22304d
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 114 deletions.
28 changes: 8 additions & 20 deletions src/components/MainBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import classnames, {
} from 'classnames/tailwind'
import onCreateAttestationProofMessage from 'helpers/onCreateAttestationProofMessage'
import onCreatePasswordProofMessage from 'helpers/onCreatePasswordProofMessage'
import onFindAttestationTypeMessage from 'helpers/onFindAttestationTypeMessage'
import onValidateParamsMessage from 'helpers/onValidateParamsMessage'
import postWebViewMessage from 'helpers/postWebViewMessage'
import useMessageHandler from 'hooks/useMessageHandler'

Expand All @@ -37,30 +35,20 @@ export default function MainBlock() {
case MessageType.CreatePasswordProof:
await onCreatePasswordProofMessage(message)
break
// TODO: Validate Params can be removed because it's no longer called by ketl-app
case MessageType.ValidateParams:
await onValidateParamsMessage(message)
break
case MessageType.FindAttestationType:
await onFindAttestationTypeMessage(message)
break
case MessageType.Status:
postWebViewMessage({
data: {},
id: message.id,
type: Messages.Ready,
})
break
}
}, [])

useMessageHandler(onMessage)

useEffect(() => {
postWebViewMessage({
data: {},
type: Messages.Ready,
})
const interval = setInterval(() => {
postWebViewMessage({
data: {},
type: Messages.Ready,
})
}, 1000)

return () => clearInterval(interval)
}, [])

return (
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/createPasswordProof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import createPasswordInput from 'helpers/createPasswordInput'
import generatePasswordProof from 'helpers/generatePasswordProof'

export default async function createPasswordProof(
params: CreatePasswordProofParams
params: CreatePasswordProofParams,
logger: (message: string) => void
) {
const input = await createPasswordInput(params)
const proof = await generatePasswordProof(input)
const proof = await generatePasswordProof(input, logger)

return proof
}
8 changes: 6 additions & 2 deletions src/helpers/generateAttestationProof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import GeneratorError from 'helpers/GeneratorError'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare const snarkjs: any

export default function generateAttestationProof(input: unknown) {
export default function generateAttestationProof(
input: unknown,
debug: (message: string) => void
) {
try {
return snarkjs.groth16.fullProve(
input,
'zk/AttestationChecker.wasm',
'zk/AttestationChecker_final.zkey'
'zk/AttestationChecker_final.zkey',
{ debug }
)
} catch (e) {
throw new GeneratorError('The verifier failed to validate the invite', {
Expand Down
8 changes: 6 additions & 2 deletions src/helpers/generatePasswordProof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import GeneratorError from 'helpers/GeneratorError'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare const snarkjs: any

export default function generatePasswordProof(input: unknown) {
export default function generatePasswordProof(
input: unknown,
debug: (message: string) => void
) {
try {
return snarkjs.groth16.fullProve(
input,
'zk/PasswordChecker.wasm',
'zk/PasswordChecker_final.zkey'
'zk/PasswordChecker_final.zkey',
{ debug }
)
} catch (e) {
throw new GeneratorError(
Expand Down
36 changes: 36 additions & 0 deletions src/helpers/getProofProgress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const messages = [
'Reading Wtns',
'Reading Coeffs',
'Building ABC',
'QAP AB',
'QAP C',
'IFFT_A: fft 14 mix start',
'IFFT_A: fft 14 mix end',
'IFFT_A: fft 14 join',
'FFT_A: fft 14 mix start',
'FFT_A: fft 14 mix end',
'FFT_A: fft 14 join',
'IFFT_B: fft 14 mix start',
'IFFT_B: fft 14 mix end',
'IFFT_B: fft 14 join',
'FFT_B: fft 14 mix start',
'FFT_B: fft 14 mix end',
'FFT_B: fft 14 join',
'IFFT_C: fft 14 mix start',
'IFFT_C: fft 14 mix end',
'IFFT_C: fft 14 join',
'FFT_C: fft 14 mix start',
'FFT_C: fft 14 mix end',
'FFT_C: fft 14 join',
'Reading A Points',
'Reading B1 Points',
'Reading B2 Points',
'Reading C Points',
'Reading H Points',
]

export default function getProofProgress(log: string) {
const position = messages.findIndex((message) => log.startsWith(message))
if (position === -1) return null
return (position * 100) / messages.length
}
6 changes: 5 additions & 1 deletion src/helpers/onCreateAttestationProofMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ProofResultStatus from 'models/ProofResultStatus'
import createAttestationInput from 'helpers/createAttestationInput'
import generateAttestationProof from 'helpers/generateAttestationProof'
import isValidAttestationProofMessage from 'helpers/isValidAttestationProofMessage'
import onProofProgress from 'helpers/onProofProgress'
import postWebViewMessage from 'helpers/postWebViewMessage'

export default async function onCreateAttestationProofMessage(
Expand Down Expand Up @@ -37,7 +38,10 @@ export default async function onCreateAttestationProofMessage(
type: Messages.GetProofStatus,
})

const attestationProof = await generateAttestationProof(input)
const attestationProof = await generateAttestationProof(
input,
onProofProgress(message.id)
)

postWebViewMessage({
data: {
Expand Down
6 changes: 5 additions & 1 deletion src/helpers/onCreatePasswordProofMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import Message from 'models/Message'
import Messages from 'models/Messages'
import createPasswordProof from 'helpers/createPasswordProof'
import isValidPasswordProofMessage from 'helpers/isValidPasswordProofMessage'
import onProofProgress from 'helpers/onProofProgress'
import postWebViewMessage from 'helpers/postWebViewMessage'

export default async function onPasswordProofMessage(message: Message) {
try {
if (!isValidPasswordProofMessage(message.params))
throw new GeneratorError('Invalid data for password proof!')

const data = await createPasswordProof(message.params)
const data = await createPasswordProof(
message.params,
onProofProgress(message.id)
)

postWebViewMessage({
data,
Expand Down
51 changes: 0 additions & 51 deletions src/helpers/onFindAttestationTypeMessage.ts

This file was deleted.

18 changes: 18 additions & 0 deletions src/helpers/onProofProgress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Messages from 'models/Messages'
import ProofResultStatus from 'models/ProofResultStatus'
import getProofProgress from 'helpers/getProofProgress'
import postWebViewMessage from 'helpers/postWebViewMessage'

export default function onProofProgress(id: string) {
return (info: string) => {
postWebViewMessage({
data: {
info,
percent: getProofProgress(info),
status: ProofResultStatus.ProofGeneration,
},
id,
type: Messages.GetProofStatus,
})
}
}
35 changes: 0 additions & 35 deletions src/helpers/onValidateParamsMessage.ts

This file was deleted.

0 comments on commit a22304d

Please sign in to comment.