diff --git a/bulletin_board/js-client/src/key-ceremony/key-ceremony.component.js b/bulletin_board/js-client/src/key-ceremony/key-ceremony.component.js index d58e0ac5..6ba56cf6 100644 --- a/bulletin_board/js-client/src/key-ceremony/key-ceremony.component.js +++ b/bulletin_board/js-client/src/key-ceremony/key-ceremony.component.js @@ -18,7 +18,7 @@ export class KeyCeremonyComponent extends TrusteeComponent { setupElection({ bulletinBoardClientParams, electionUniqueId, - authorizationExpirationTimestamp, + authorizationExpirationTimestamp }) { return this.setupElectionWithTypesFilter({ electionUniqueId, @@ -28,7 +28,7 @@ export class KeyCeremonyComponent extends TrusteeComponent { "create_election", "start_key_ceremony", "key_ceremony", - "end_key_ceremony", + "end_key_ceremony" ], }); } @@ -64,7 +64,7 @@ export class KeyCeremonyComponent extends TrusteeComponent { onTrusteeNeedsToBeRestored, onBackupNeeded, onBindBackupButton, - onBackupStarted, + onBackupStarted }) { const onSetupDone = await this.trustee.setup(); diff --git a/bulletin_board/js-client/src/tally/tally.component.js b/bulletin_board/js-client/src/tally/tally.component.js index 0062532b..e8902104 100644 --- a/bulletin_board/js-client/src/tally/tally.component.js +++ b/bulletin_board/js-client/src/tally/tally.component.js @@ -31,7 +31,7 @@ export class TallyComponent extends TrusteeComponent { "end_key_ceremony", "start_tally", "tally", - "end_tally", + "end_tally" ], }); } @@ -60,7 +60,7 @@ export class TallyComponent extends TrusteeComponent { onRestore, onComplete, onStart, - onTrusteeNeedsToBeRestored, + onTrusteeNeedsToBeRestored }) { const onSetupDone = this.trustee.setup(); diff --git a/bulletin_board/js-client/src/trustee/trustee.component.js b/bulletin_board/js-client/src/trustee/trustee.component.js index 203d5fd8..15e16b9f 100644 --- a/bulletin_board/js-client/src/trustee/trustee.component.js +++ b/bulletin_board/js-client/src/trustee/trustee.component.js @@ -27,7 +27,7 @@ export class TrusteeComponent { uniqueId: trusteeUniqueId, authorityPublicKeyJSON, identificationKeys: trusteeIdentificationKeys, - wrapperAdapter: trusteeWrapperAdapter, + wrapperAdapter: trusteeWrapperAdapter }); } @@ -69,14 +69,14 @@ export class TrusteeComponent { ...bulletinBoardClientParams, headers: { Authorization: authorizationHeader, - TrusteeUniqueId: trusteeUniqueIdHeader, + TrusteeUniqueId: trusteeUniqueIdHeader }, }); const election = new Election({ uniqueId: electionUniqueId, bulletinBoardClient, - typesFilter, + typesFilter }); this.trustee.election = election; diff --git a/voting_schemes/electionguard/js-adapter/src/trustee_wrapper_adapter.js b/voting_schemes/electionguard/js-adapter/src/trustee_wrapper_adapter.js index ca29e5f8..04d74060 100644 --- a/voting_schemes/electionguard/js-adapter/src/trustee_wrapper_adapter.js +++ b/voting_schemes/electionguard/js-adapter/src/trustee_wrapper_adapter.js @@ -26,14 +26,14 @@ export class TrusteeWrapperAdapter extends WrapperAdapter { * @returns {Promise} */ async setup() { - return await this.processPythonCode( + return await this.processPythonCodeOnWorker( ` from js import trustee_id from bulletin_board.electionguard.trustee import Trustee trustee = Trustee(trustee_id) `, { - trustee_id: this.trusteeId, + trustee_id: this.trusteeId } ); } @@ -47,7 +47,7 @@ export class TrusteeWrapperAdapter extends WrapperAdapter { * @returns {Promise} */ async processMessage(messageType, decodedData) { - const result = await this.processPythonCode( + const result = await this.processPythonCodeOnWorker( ` from js import message_type, decoded_data import json @@ -58,16 +58,17 @@ export class TrusteeWrapperAdapter extends WrapperAdapter { `, { message_type: messageType, - decoded_data: JSON.stringify(decodedData), + decoded_data: JSON.stringify(decodedData) } ); if (result && result.length > 0) { // eslint-disable-next-line camelcase - const [{ message_type, content }] = result; + // Pyodide 0.17 return a Map instead of a object when python is a dict + const { message_type, content } = result[0] instanceof Map ? Object.fromEntries(result[0]) : result[0]; return { messageType: message_type, - content, + content }; } } @@ -78,7 +79,7 @@ export class TrusteeWrapperAdapter extends WrapperAdapter { * @returns {Promise} */ isFresh() { - return this.processPythonCode( + return this.processPythonCodeOnWorker( ` trustee.is_fresh() ` @@ -91,7 +92,7 @@ export class TrusteeWrapperAdapter extends WrapperAdapter { * @returns {Promise} */ backup() { - return this.processPythonCode( + return this.processPythonCodeOnWorker( ` trustee.backup().hex() ` @@ -105,14 +106,14 @@ export class TrusteeWrapperAdapter extends WrapperAdapter { * @returns {Promise} */ restore(state) { - return this.processPythonCode( + return this.processPythonCodeOnWorker( ` from js import state trustee = Trustee.restore(bytes.fromhex(state)) True `, { - state, + state } ); } @@ -123,7 +124,7 @@ export class TrusteeWrapperAdapter extends WrapperAdapter { * @returns {Promise} */ isKeyCeremonyDone() { - return this.processPythonCode( + return this.processPythonCodeOnWorker( ` trustee.is_key_ceremony_done() ` @@ -136,7 +137,7 @@ export class TrusteeWrapperAdapter extends WrapperAdapter { * @returns {Promise} */ isTallyDone() { - return this.processPythonCode( + return this.processPythonCodeOnWorker( ` trustee.is_tally_done() ` diff --git a/voting_schemes/electionguard/js-adapter/src/voter_wrapper_adapter.js b/voting_schemes/electionguard/js-adapter/src/voter_wrapper_adapter.js index e8d618b5..0777195a 100644 --- a/voting_schemes/electionguard/js-adapter/src/voter_wrapper_adapter.js +++ b/voting_schemes/electionguard/js-adapter/src/voter_wrapper_adapter.js @@ -26,14 +26,14 @@ export class VoterWrapperAdapter extends WrapperAdapter { * @returns {Promise} */ async setup() { - return await this.processPythonCode( + return await this.processPythonCodeOnWorker( ` from js import voter_id from bulletin_board.electionguard.voter import Voter voter = Voter(voter_id) `, { - voter_id: this.voterId, + voter_id: this.voterId } ); } @@ -47,7 +47,7 @@ export class VoterWrapperAdapter extends WrapperAdapter { * @returns {Promise} */ async processMessage(messageType, decodedData) { - const result = await this.processPythonCode( + const result = await this.processPythonCodeOnWorker( ` from js import message_type, decoded_data import json @@ -55,16 +55,17 @@ export class VoterWrapperAdapter extends WrapperAdapter { `, { message_type: messageType, - decoded_data: JSON.stringify(decodedData), + decoded_data: JSON.stringify(decodedData) } ); - if (result && result[0]) { + if (result && result.length > 0) { // eslint-disable-next-line camelcase - const { message_type, content } = result[0]; + // Pyodide 0.17 return a Map instead of a object when python is a dict + const { message_type, content } = result[0] instanceof Map ? Object.fromEntries(result[0]) : result[0]; return { messageType: message_type, - content: content.toJs(), + content: content }; } } @@ -80,17 +81,19 @@ export class VoterWrapperAdapter extends WrapperAdapter { * @returns {Promise} */ async encrypt(plainVote, ballotStyle) { - const [auditableData, encryptedData] = await this.processPythonCode( + console.log("Encrypting vote", plainVote, ballotStyle); + const [auditableData, encryptedData] = await this.processPythonCodeOnWorker( ` from js import plain_vote, ballot_style voter.encrypt(plain_vote.to_py(), ballot_style) `, { plain_vote: plainVote, - ballot_style: ballotStyle, + ballot_style: ballotStyle } ); - + console.log("Encrypted vote: auditableData:", auditableData, "encryptedData:", encryptedData); +// TODO, FIXME check if we need Object.fromEntries return { auditableData, encryptedData }; } } diff --git a/voting_schemes/electionguard/js-adapter/src/wrapper_adapter.js b/voting_schemes/electionguard/js-adapter/src/wrapper_adapter.js index f73d690f..fe9da9d4 100644 --- a/voting_schemes/electionguard/js-adapter/src/wrapper_adapter.js +++ b/voting_schemes/electionguard/js-adapter/src/wrapper_adapter.js @@ -3,7 +3,7 @@ */ export class WrapperAdapter { /** - * Runs arbitrary python code + * Runs arbitrary python code in the web worker * * @param {String} pythonCode - A string representing valid python code. * @param {Object} pythonData - An Object which values can be referenced from @@ -11,20 +11,21 @@ export class WrapperAdapter { * @private * @returns {Promise} */ - async processPythonCode(pythonCode, pythonData) { + async processPythonCodeOnWorker(pythonCode, pythonData) { return new Promise((resolve, reject) => { this.worker.onmessage = (event) => { resolve(event.data.results); }; this.worker.onerror = (error) => { + console.error("Error in worker, pythonCode:", pythonCode, "pythonData:", pythonData); console.error(error); reject(error); }; this.worker.postMessage({ python: pythonCode, - ...pythonData, + ...pythonData }); }); }