11import CryptoJS from 'crypto-js' ;
22import { solidityPackedSha256 } from 'ethers' ;
3+ import snarkjs from 'snarkjs' ;
34
45import { MaciAccount } from './account' ;
56import {
@@ -12,6 +13,7 @@ import {
1213 Tree ,
1314 stringizing ,
1415 SNARK_FIELD_SIZE ,
16+ adaptToUncompressed ,
1517} from './crypto' ;
1618import { poseidon } from './crypto/hashing' ;
1719import { poseidonEncrypt } from '@zk-kit/poseidon-cipher' ;
@@ -102,7 +104,7 @@ export class VoterClient {
102104 derivePathParams
103105 ) ;
104106
105- return payload ;
107+ return stringizing ( payload ) ;
106108 }
107109
108110 batchGenMessage (
@@ -201,28 +203,59 @@ export class VoterClient {
201203 stateTreeDepth,
202204 operatorPubkey,
203205 deactivates,
206+ wasmFile,
207+ zkeyFile,
204208 derivePathParams,
205209 } : {
206210 stateTreeDepth : number ;
207211 operatorPubkey : bigint ;
208212 deactivates : DeactivateMessage [ ] ;
213+ wasmFile : string ;
214+ zkeyFile : string ;
209215 derivePathParams ?: DerivePathParams ;
210- } ) {
216+ } ) : Promise < {
217+ proof : {
218+ a : string ;
219+ b : string ;
220+ c : string ;
221+ } ;
222+ d : string [ ] ;
223+ nullifier : string ;
224+ } > {
211225 const [ coordPubkeyX , coordPubkeyY ] =
212226 this . unpackMaciPubkey ( operatorPubkey ) ;
213227 // const stateTreeDepth = Number(circuitPower.split('-')[0]);
214- const inputObj = this . genAddKeyInput ( stateTreeDepth + 2 , {
228+ const addKeyInput = await this . genAddKeyInput ( stateTreeDepth + 2 , {
215229 coordPubKey : [ coordPubkeyX , coordPubkeyY ] ,
216230 deactivates : deactivates . map ( ( d : any ) => d . map ( BigInt ) ) ,
217231 derivePathParams,
218232 } ) ;
219- return inputObj ;
233+
234+ if ( addKeyInput === null ) {
235+ throw Error ( 'genAddKeyInput failed' ) ;
236+ }
220237
221238 // 1. generate proof
239+ const { proof } = await snarkjs . groth16 . fullProve (
240+ addKeyInput ,
241+ wasmFile ,
242+ zkeyFile
243+ ) ;
222244
223245 // 2. compress proof to vote proof
246+ const proofHex = await adaptToUncompressed ( proof ) ;
224247
225248 // 3. send addNewKey tx
249+ return {
250+ proof : proofHex ,
251+ d : [
252+ addKeyInput . d1 [ 0 ] . toString ( ) ,
253+ addKeyInput . d1 [ 1 ] . toString ( ) ,
254+ addKeyInput . d2 [ 0 ] . toString ( ) ,
255+ addKeyInput . d2 [ 1 ] . toString ( ) ,
256+ ] ,
257+ nullifier : addKeyInput . nullifier . toString ( ) ,
258+ } ;
226259 }
227260
228261 async genAddKeyInput (
@@ -318,6 +351,6 @@ export class VoterClient {
318351 [ [ 0 , 0 ] ] ,
319352 derivePathParams
320353 ) ;
321- return payload ;
354+ return stringizing ( payload [ 0 ] ) ;
322355 }
323356}
0 commit comments