@@ -181,22 +181,33 @@ class BoilerplateGenerator {
181181 } ,
182182 } ;
183183
184- membershipWitness = {
185- postStatements ( { stateName,
184+ getInputCommitments = {
185+ postStatements ( {
186+ stateName,
186187 contractName,
187- stateType, mappingName, structProperties, isSharedSecret, stateVarIds } ) : string [ ] {
188+ stateType,
189+ mappingName,
190+ structProperties,
191+ isSharedSecret,
192+ stateVarIds,
193+ } ) : string [ ] {
188194 const stateVarId : string [ ] = [ ] ;
189- if ( stateVarIds . length > 1 ) {
190- stateVarId . push ( ( stateVarIds [ 0 ] . split ( " = " ) [ 1 ] ) . split ( ";" ) [ 0 ] ) ;
195+ if ( stateVarIds . length > 1 ) {
196+ stateVarId . push ( stateVarIds [ 0 ] . split ( ' = ' ) [ 1 ] . split ( ';' ) [ 0 ] ) ;
191197 stateVarId . push ( `${ stateName } _stateVarId_key` ) ;
192- } else
193- stateVarId . push ( `${ stateName } _stateVarId` ) ;
198+ } else stateVarId . push ( `${ stateName } _stateVarId` ) ;
194199 switch ( stateType ) {
195200 case 'partitioned' :
196201 if ( structProperties )
197- return [ `
202+ return [
203+ `
198204 \n\n// First check if required commitments exist or not
199- \nconst ${ stateName } _newCommitmentValue = generalise([${ Object . values ( structProperties ) . map ( ( sp ) => `generalise(parseInt(${ stateName } _${ sp } _newCommitmentValue.integer, 10) - parseInt(${ stateName } _${ sp } _newCommitmentValue_inc.integer, 10))` ) } ]).all;
205+ \nconst ${ stateName } _newCommitmentValue = generalise([${ Object . values (
206+ structProperties ,
207+ ) . map (
208+ sp =>
209+ `generalise(parseInt(${ stateName } _${ sp } _newCommitmentValue.integer, 10) - parseInt(${ stateName } _${ sp } _newCommitmentValue_inc.integer, 10))` ,
210+ ) } ]).all;
200211 \nlet [${ stateName } _commitmentFlag, ${ stateName } _0_oldCommitment, ${ stateName } _1_oldCommitment] = getInputCommitments(
201212 publicKey.hex(32),
202213 ${ stateName } _newCommitmentValue.integer,
@@ -207,19 +218,10 @@ class BoilerplateGenerator {
207218 \nlet ${ stateName } _witness_0;
208219 \nlet ${ stateName } _witness_1;
209220
210- const ${ stateName } _0_prevSalt = generalise(${ stateName } _0_oldCommitment.preimage.salt);
211- const ${ stateName } _1_prevSalt = generalise(${ stateName } _1_oldCommitment.preimage.salt);
212- const ${ stateName } _0_prev = generalise(${ stateName } _0_oldCommitment.preimage.value);
213- const ${ stateName } _1_prev = generalise(${ stateName } _1_oldCommitment.preimage.value);
214- \n\n// generate witness for partitioned state
215- ${ stateName } _witness_0 = await getMembershipWitness('${ contractName } ', generalise(${ stateName } _0_oldCommitment._id).integer);
216- ${ stateName } _witness_1 = await getMembershipWitness('${ contractName } ', generalise(${ stateName } _1_oldCommitment._id).integer);
217- const ${ stateName } _0_index = generalise(${ stateName } _witness_0.index);
218- const ${ stateName } _1_index = generalise(${ stateName } _witness_1.index);
219- const ${ stateName } _root = generalise(${ stateName } _witness_0.root);
220- const ${ stateName } _0_path = generalise(${ stateName } _witness_0.path).all;
221- const ${ stateName } _1_path = generalise(${ stateName } _witness_1.path).all;\n` ] ;
222- return [ `
221+ ` ,
222+ ] ;
223+ return [
224+ `
223225 \n\n// First check if required commitments exist or not
224226 \n${ stateName } _newCommitmentValue = generalise(parseInt(${ stateName } _newCommitmentValue.integer, 10) - parseInt(${ stateName } _newCommitmentValue_inc.integer, 10));
225227 \nlet [${ stateName } _commitmentFlag, ${ stateName } _0_oldCommitment, ${ stateName } _1_oldCommitment] = getInputCommitments(
@@ -251,11 +253,52 @@ class BoilerplateGenerator {
251253 ${ stateName } _preimage = await getCommitmentsById(${ stateName } _stateVarId);
252254
253255 [${ stateName } _commitmentFlag, ${ stateName } _0_oldCommitment, ${ stateName } _1_oldCommitment] = getInputCommitments(
254- ${ isSharedSecret ? `sharedPublicKey.hex(32)` : `publicKey.hex(32)` } ,
256+ ${
257+ isSharedSecret
258+ ? `sharedPublicKey.hex(32)`
259+ : `publicKey.hex(32)`
260+ } ,
255261 ${ stateName } _newCommitmentValue.integer,
256262 ${ stateName } _preimage,
257263 );
258264 }
265+ ` ,
266+ ] ;
267+ default :
268+ throw new TypeError ( stateType ) ;
269+ }
270+ } ,
271+ } ;
272+
273+ membershipWitness = {
274+ postStatements ( {
275+ stateName,
276+ contractName,
277+ stateType,
278+ mappingName,
279+ structProperties,
280+ isSharedSecret,
281+ stateVarIds,
282+ } ) : string [ ] {
283+ switch ( stateType ) {
284+ case 'partitioned' :
285+ if ( structProperties )
286+ return [
287+ `
288+ const ${ stateName } _0_prevSalt = generalise(${ stateName } _0_oldCommitment.preimage.salt);
289+ const ${ stateName } _1_prevSalt = generalise(${ stateName } _1_oldCommitment.preimage.salt);
290+ const ${ stateName } _0_prev = generalise(${ stateName } _0_oldCommitment.preimage.value);
291+ const ${ stateName } _1_prev = generalise(${ stateName } _1_oldCommitment.preimage.value);
292+ \n\n// generate witness for partitioned state
293+ ${ stateName } _witness_0 = await getMembershipWitness('${ contractName } ', generalise(${ stateName } _0_oldCommitment._id).integer);
294+ ${ stateName } _witness_1 = await getMembershipWitness('${ contractName } ', generalise(${ stateName } _1_oldCommitment._id).integer);
295+ const ${ stateName } _0_index = generalise(${ stateName } _witness_0.index);
296+ const ${ stateName } _1_index = generalise(${ stateName } _witness_1.index);
297+ const ${ stateName } _root = generalise(${ stateName } _witness_0.root);
298+ const ${ stateName } _0_path = generalise(${ stateName } _witness_0.path).all;
299+ const ${ stateName } _1_path = generalise(${ stateName } _witness_1.path).all;\n` ] ;
300+ return [
301+ `
259302 const ${ stateName } _0_prevSalt = generalise(${ stateName } _0_oldCommitment.preimage.salt);
260303 const ${ stateName } _1_prevSalt = generalise(${ stateName } _1_oldCommitment.preimage.salt);
261304 const ${ stateName } _0_prev = generalise(${ stateName } _0_oldCommitment.preimage.value);
@@ -267,29 +310,34 @@ class BoilerplateGenerator {
267310 const ${ stateName } _1_index = generalise(${ stateName } _witness_1.index);
268311 const ${ stateName } _root = generalise(${ stateName } _witness_0.root);
269312 const ${ stateName } _0_path = generalise(${ stateName } _witness_0.path).all;
270- const ${ stateName } _1_path = generalise(${ stateName } _witness_1.path).all;\n` ] ;
313+ const ${ stateName } _1_path = generalise(${ stateName } _witness_1.path).all;\n` ,
314+ ] ;
271315 case 'whole' :
272- return [ `
316+ return [
317+ `
273318 \n\n// generate witness for whole state
274319 const ${ stateName } _emptyPath = new Array(32).fill(0);
275320 const ${ stateName } _witness = ${ stateName } _witnessRequired
276321 \t? await getMembershipWitness('${ contractName } ', ${ stateName } _currentCommitment.integer)
277322 \t: { index: 0, path: ${ stateName } _emptyPath, root: await getRoot('${ contractName } ') || 0 };
278323 const ${ stateName } _index = generalise(${ stateName } _witness.index);
279324 const ${ stateName } _root = generalise(${ stateName } _witness.root);
280- const ${ stateName } _path = generalise(${ stateName } _witness.path).all;\n` ] ;
325+ const ${ stateName } _path = generalise(${ stateName } _witness.path).all;\n` ,
326+ ] ;
281327 case 'accessedOnly' :
282- return [ `
328+ return [
329+ `
283330 \n\n// generate witness for whole accessed state
284331 const ${ stateName } _witness = await getMembershipWitness('${ contractName } ', ${ stateName } _currentCommitment.integer);
285332 const ${ stateName } _index = generalise(${ stateName } _witness.index);
286333 const ${ stateName } _root = generalise(${ stateName } _witness.root);
287- const ${ stateName } _path = generalise(${ stateName } _witness.path).all;\n` ] ;
334+ const ${ stateName } _path = generalise(${ stateName } _witness.path).all;\n` ,
335+ ] ;
288336 default :
289337 throw new TypeError ( stateType ) ;
290338 }
291- }
292- } ;
339+ } ,
340+ } ;
293341
294342 calculateNullifier = {
295343
0 commit comments