decidim-bulletin_board 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59d8c1978f37d8983b38e5f1ae53170b64128b4e5c01b9ef48187d68345b500b
4
- data.tar.gz: 0c32c4ea493dd9341b754b8cddab959dcf51eca117f9cd7c936ed14abb38eebe
3
+ metadata.gz: d8ddd128a3799dd648768fff34cf0bfbca4b71f89fb3ab284c6b7b5637409480
4
+ data.tar.gz: 93a6b2fc92ddba31a44f54ca0cdf6daa85edbe5dd76c0ed3005b5b837dbec8a2
5
5
  SHA512:
6
- metadata.gz: c616c66acd989f134d21b6dbf93d9a85002b9ae884638ca8766cdeb8699223c24d11ed09f3bdc4a699bef4f302818445270273c3338619214784c623251540d9
7
- data.tar.gz: fc7a471d80edc8bc06c8b8764c770182ccc8a2eae195e519c5190e1cc4c1fdbb1d2553eb3c5303f5a52981959b7f8b0da097447cc00c9f2c6002c6e231f5ce5d
6
+ metadata.gz: caeefd3655ee1ec87305e3ac26258d4d056632819a4d0c6641953bb2a55e2837cb67151cc191e6563c3f984d4aa2fafc12cdf6689098e5dff67dfc81b43a2e2f
7
+ data.tar.gz: 72cd4eedab91da1b1d9a864fef3f58f7390e19ef74d342e9b1c39b95e9523792a10d1b75a370bac8d1b7e85f6f969106ee81dd443b786d4263687c44e48e4d0d
data/CHANGELOG.md CHANGED
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## Unreleased
9
9
 
10
+ ## [0.9.2] - 2021-02-02
11
+
10
12
  ## [0.9.1] - 2021-02-02
11
13
 
12
14
  ## Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- decidim-bulletin_board (0.9.1)
4
+ decidim-bulletin_board (0.9.2)
5
5
  byebug (~> 11.0)
6
6
  graphlient (~> 0.4.0)
7
7
  jwt (~> 2.2.2)
@@ -13,4 +13,4 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13
13
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14
14
  PERFORMANCE OF THIS SOFTWARE.
15
15
  ***************************************************************************** */
16
- var r=function(e,t){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])})(e,t)};function i(e,t){function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var o=function(){return(o=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)};function s(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(e);i<r.length;i++)t.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(e,r[i])&&(n[r[i]]=e[r[i]])}return n}function a(e,t,n,r){return new(n||(n=Promise))((function(i,o){function s(e){try{c(r.next(e))}catch(e){o(e)}}function a(e){try{c(r.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,a)}c((r=r.apply(e,t||[])).next())}))}function c(e,t){var n,r,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;s;)try{if(n=1,r&&(i=2&o[0]?r.return:o[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,o[1])).done)return i;switch(r=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,r=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=s.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){s.label=o[1];break}if(6===o[0]&&s.label<i[1]){s.label=i[1],i=o;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(o);break}i[2]&&s.ops.pop(),s.trys.pop();continue}o=t.call(e,s)}catch(e){o=[6,e],r=0}finally{n=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,a])}}}function u(){for(var e=0,t=0,n=arguments.length;t<n;t++)e+=arguments[t].length;var r=Array(e),i=0;for(t=0;t<n;t++)for(var o=arguments[t],s=0,a=o.length;s<a;s++,i++)r[i]=o[s];return r}},9329:(e,t,n)=>{e.exports=n(516).Observable},516:(e,t)=>{"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function r(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function i(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e}t.Observable=void 0;var o=function(){return"function"==typeof Symbol},s=function(e){return o()&&Boolean(Symbol[e])},a=function(e){return s(e)?Symbol[e]:"@@"+e};o()&&!s("observable")&&(Symbol.observable=Symbol("observable"));var c=a("iterator"),u=a("observable"),l=a("species");function f(e,t){var n=e[t];if(null!=n){if("function"!=typeof n)throw new TypeError(n+" is not a function");return n}}function p(e){var t=e.constructor;return void 0!==t&&null===(t=t[l])&&(t=void 0),void 0!==t?t:S}function h(e){return e instanceof S}function d(e){d.log?d.log(e):setTimeout((function(){throw e}))}function y(e){Promise.resolve().then((function(){try{e()}catch(e){d(e)}}))}function v(e){var t=e._cleanup;if(void 0!==t&&(e._cleanup=void 0,t))try{if("function"==typeof t)t();else{var n=f(t,"unsubscribe");n&&n.call(t)}}catch(e){d(e)}}function m(e){e._observer=void 0,e._queue=void 0,e._state="closed"}function b(e,t,n){e._state="running";var r=e._observer;try{var i=f(r,t);switch(t){case"next":i&&i.call(r,n);break;case"error":if(m(e),!i)throw n;i.call(r,n);break;case"complete":m(e),i&&i.call(r)}}catch(e){d(e)}"closed"===e._state?v(e):"running"===e._state&&(e._state="ready")}function g(e,t,n){if("closed"!==e._state){if("buffering"!==e._state)return"ready"!==e._state?(e._state="buffering",e._queue=[{type:t,value:n}],void y((function(){return function(e){var t=e._queue;if(t){e._queue=void 0,e._state="ready";for(var n=0;n<t.length&&(b(e,t[n].type,t[n].value),"closed"!==e._state);++n);}}(e)}))):void b(e,t,n);e._queue.push({type:t,value:n})}}var k=function(){function e(t,r){n(this,e),this._cleanup=void 0,this._observer=t,this._queue=void 0,this._state="initializing";var i=new E(this);try{this._cleanup=r.call(void 0,i)}catch(e){i.error(e)}"initializing"===this._state&&(this._state="ready")}return i(e,[{key:"unsubscribe",value:function(){"closed"!==this._state&&(m(this),v(this))}},{key:"closed",get:function(){return"closed"===this._state}}]),e}(),E=function(){function e(t){n(this,e),this._subscription=t}return i(e,[{key:"next",value:function(e){g(this._subscription,"next",e)}},{key:"error",value:function(e){g(this._subscription,"error",e)}},{key:"complete",value:function(){g(this._subscription,"complete")}},{key:"closed",get:function(){return"closed"===this._subscription._state}}]),e}(),S=function(){function e(t){if(n(this,e),!(this instanceof e))throw new TypeError("Observable cannot be called as a function");if("function"!=typeof t)throw new TypeError("Observable initializer must be a function");this._subscriber=t}return i(e,[{key:"subscribe",value:function(e){return"object"==typeof e&&null!==e||(e={next:e,error:arguments[1],complete:arguments[2]}),new k(e,this._subscriber)}},{key:"forEach",value:function(e){var t=this;return new Promise((function(n,r){if("function"==typeof e)var i=t.subscribe({next:function(t){try{e(t,o)}catch(e){r(e),i.unsubscribe()}},error:r,complete:n});else r(new TypeError(e+" is not a function"));function o(){i.unsubscribe(),n()}}))}},{key:"map",value:function(e){var t=this;if("function"!=typeof e)throw new TypeError(e+" is not a function");return new(p(this))((function(n){return t.subscribe({next:function(t){try{t=e(t)}catch(e){return n.error(e)}n.next(t)},error:function(e){n.error(e)},complete:function(){n.complete()}})}))}},{key:"filter",value:function(e){var t=this;if("function"!=typeof e)throw new TypeError(e+" is not a function");return new(p(this))((function(n){return t.subscribe({next:function(t){try{if(!e(t))return}catch(e){return n.error(e)}n.next(t)},error:function(e){n.error(e)},complete:function(){n.complete()}})}))}},{key:"reduce",value:function(e){var t=this;if("function"!=typeof e)throw new TypeError(e+" is not a function");var n=p(this),r=arguments.length>1,i=!1,o=arguments[1],s=o;return new n((function(n){return t.subscribe({next:function(t){var o=!i;if(i=!0,!o||r)try{s=e(s,t)}catch(e){return n.error(e)}else s=t},error:function(e){n.error(e)},complete:function(){if(!i&&!r)return n.error(new TypeError("Cannot reduce an empty sequence"));n.next(s),n.complete()}})}))}},{key:"concat",value:function(){for(var e=this,t=arguments.length,n=new Array(t),r=0;r<t;r++)n[r]=arguments[r];var i=p(this);return new i((function(t){var r,o=0;return function e(s){r=s.subscribe({next:function(e){t.next(e)},error:function(e){t.error(e)},complete:function(){o===n.length?(r=void 0,t.complete()):e(i.from(n[o++]))}})}(e),function(){r&&(r.unsubscribe(),r=void 0)}}))}},{key:"flatMap",value:function(e){var t=this;if("function"!=typeof e)throw new TypeError(e+" is not a function");var n=p(this);return new n((function(r){var i=[],o=t.subscribe({next:function(t){if(e)try{t=e(t)}catch(e){return r.error(e)}var o=n.from(t).subscribe({next:function(e){r.next(e)},error:function(e){r.error(e)},complete:function(){var e=i.indexOf(o);e>=0&&i.splice(e,1),s()}});i.push(o)},error:function(e){r.error(e)},complete:function(){s()}});function s(){o.closed&&0===i.length&&r.complete()}return function(){i.forEach((function(e){return e.unsubscribe()})),o.unsubscribe()}}))}},{key:u,value:function(){return this}}],[{key:"from",value:function(t){var n="function"==typeof this?this:e;if(null==t)throw new TypeError(t+" is not an object");var r=f(t,u);if(r){var i=r.call(t);if(Object(i)!==i)throw new TypeError(i+" is not an object");return h(i)&&i.constructor===n?i:new n((function(e){return i.subscribe(e)}))}if(s("iterator")&&(r=f(t,c)))return new n((function(e){y((function(){if(!e.closed){var n=!0,i=!1,o=void 0;try{for(var s,a=r.call(t)[Symbol.iterator]();!(n=(s=a.next()).done);n=!0){var c=s.value;if(e.next(c),e.closed)return}}catch(e){i=!0,o=e}finally{try{n||null==a.return||a.return()}finally{if(i)throw o}}e.complete()}}))}));if(Array.isArray(t))return new n((function(e){y((function(){if(!e.closed){for(var n=0;n<t.length;++n)if(e.next(t[n]),e.closed)return;e.complete()}}))}));throw new TypeError(t+" is not observable")}},{key:"of",value:function(){for(var t=arguments.length,n=new Array(t),r=0;r<t;r++)n[r]=arguments[r];var i="function"==typeof this?this:e;return new i((function(e){y((function(){if(!e.closed){for(var t=0;t<n.length;++t)if(e.next(n[t]),e.closed)return;e.complete()}}))}))}},{key:l,get:function(){return this}}]),e}();t.Observable=S,o()&&Object.defineProperty(S,Symbol("extensions"),{value:{symbol:u,hostReportError:d},configurable:!0})},9099:(e,t,n)=>{"use strict";n.r(t),n.d(t,{Client:()=>y,Election:()=>g,IdentificationKeys:()=>ee,KeyCeremonyComponent:()=>te,MESSAGE_PROCESSED:()=>$,MESSAGE_RECEIVED:()=>Y,MessageIdentifier:()=>b,TallyComponent:()=>ne,Trustee:()=>z,Voter:()=>X});var r=n(1439),i=n(4206),o=n.n(i),s=n(9120),a=n.n(s),c=n(1798),u=n.n(c),l=n(5073),f=n.n(l),p=n(5053),h=n.n(p);class d{constructor({apiEndpointUrl:e,headers:t}){const n=new r.HttpLink({uri:e,headers:t});this.apolloClient=new r.fe({link:n,cache:new r.h4})}async getLogEntry({electionUniqueId:e,contentHash:t}){return(await this.apolloClient.query({query:f(),variables:{electionUniqueId:e,contentHash:t}})).data.logEntry}async getElectionLogEntries({electionUniqueId:e,after:t}){return(await this.apolloClient.query({query:o(),variables:{electionUniqueId:e,after:t},fetchPolicy:"no-cache"})).data.election.logEntries}async processKeyCeremonyStep({messageId:e,signedData:t}){const n=await this.apolloClient.mutate({mutation:a(),variables:{messageId:e,signedData:t}});if(n.data.processKeyCeremonyStep.error)throw new Error(n.data.processKeyCeremonyStep.error);return n.data.processKeyCeremonyStep.pendingMessage}async getPendingMessageByMessageId({messageId:e}){return(await this.apolloClient.query({query:h(),variables:{messageId:e}})).data.pendingMessage}async processTallyStep({messageId:e,signedData:t}){const n=await this.apolloClient.mutate({mutation:u(),variables:{messageId:e,signedData:t}});if(n.data.processTallyStep.error)throw new Error(n.data.processTallyStep.error);return n.data.processTallyStep.pendingMessage}}class y{constructor(e){this.apiClient=new d(e)}getLogEntry({electionUniqueId:e,contentHash:t}){return this.apiClient.getLogEntry({electionUniqueId:e,contentHash:t})}getElectionLogEntries(e){return this.apiClient.getElectionLogEntries(e)}processKeyCeremonyStep(e){return this.apiClient.processKeyCeremonyStep(e)}processTallyStep(e){return this.apiClient.processTallyStep(e)}waitForPendingMessageToBeProcessed(e,t=1e3){return new Promise((n=>{const r=setInterval((()=>{this.apiClient.getPendingMessageByMessageId({messageId:e}).then((e=>{"enqueued"!==e.status&&(clearInterval(r),n(e))}))}),t)}))}}const v="t",m=["a","b","v",v];class b{static parse(e){const[t,n]=e.split("+"),[r,i,o,s]=t.split(".",4),[a,c]=n.split(".",2),u=s?"."+s:"";if(!m.includes(a))throw new Error("Invalid message identifier format");return{electionId:`${r}.${i}`,type:o,subtype:s,typeSubtype:`${o}${u}`,author:{type:a,id:c}}}static format(e,t,n,r){return`${e}.${t}+${n}.${r}`}}class g{constructor({uniqueId:e,bulletinBoardClient:t,options:n}){this.uniqueId=e,this.bulletinBoardClient=t,this.logEntries=[],this.subscriptionId=null,this.options=n||{waitUntilNextCheck:2e3}}async subscribeToLogEntriesChanges(){this.unsubscribeToLogEntriesChanges(),await this.getLogEntries(),this.subscriptionId=setInterval((()=>{this.getLogEntries()}),this.options.waitUntilNextCheck)}unsubscribeToLogEntriesChanges(){null!==this.subscriptionId&&(clearInterval(this.subscriptionId),this.subscriptionId=null)}getLastMessageFromTrustee(e){for(let t=this.logEntries.length-1;t>=0;t--){const n=this.logEntries[t],r=b.parse(n.messageId);if(r.author.type===v&&r.author.id===e)return n}return null}getLogEntries(){const e=this.logEntries[this.logEntries.length-1],t=e&&e.id||null;return new Promise((e=>{this.bulletinBoardClient.getElectionLogEntries({electionUniqueId:this.uniqueId,after:t}).then((t=>{t.length&&(this.logEntries=[...this.logEntries,...t]),e()}))}))}}class k{constructor({trusteeId:e}){this.trusteeId=e,this.electionId=null,this.status=0,this.electionPublicKey=0}processMessage(e,t){const n=b.parse(e);switch(this.status){case 0:if("start_key_ceremony"===n.type)return this.status=1,this.electionId=n.electionId,this.electionPublicKey=2*Math.floor(50+200*Math.random())+1,{message_id:b.format(this.electionId,"key_ceremony.step_1",v,this.trusteeId),content:JSON.stringify({election_public_key:this.electionPublicKey,owner_id:this.trusteeId})};break;case 1:"end_key_ceremony"===n.type&&(this.status=2);break;case 2:"start_tally"===n.type&&(this.status=3);break;case 3:if("tally.cast"===n.typeSubtype){const e=JSON.parse(t.content);for(const[t,n]of Object.entries(e))for(const[r,i]of Object.entries(n))e[t][r]=i%this.electionPublicKey*this.electionPublicKey;return{message_id:b.format(this.electionId,"tally.share",v,this.trusteeId),content:JSON.stringify({owner_id:this.trusteeId,contests:e})}}"end_tally"===n.type&&(this.status=4)}}needsToBeRestored(e){return e&&0===this.status}backup(){return JSON.stringify(this)}restore(e,t){if(!this.needsToBeRestored(t))return console.warn("Restore not needed"),!1;const n=JSON.parse(e);if(n.trusteeId!==this.trusteeId)return console.warn("Invalid trustee id"),!1;if(t&&0===n.status)return console.warn("Invalid restored status"),!1;try{Object.assign(this,n)}catch(e){return console.error(e),!1}return!0}isKeyCeremonyDone(){return this.status>=2}isTallyDone(){return this.status>=4}}var E=n(655);function S(e){return"function"==typeof e}var w=!1,T={Promise:void 0,set useDeprecatedSynchronousErrorHandling(e){e&&(new Error).stack;w=e},get useDeprecatedSynchronousErrorHandling(){return w}};function _(e){setTimeout((function(){throw e}),0)}var O={closed:!0,next:function(e){},error:function(e){if(T.useDeprecatedSynchronousErrorHandling)throw e;_(e)},complete:function(){}},I=function(){return Array.isArray||function(e){return e&&"number"==typeof e.length}}();var N=function(){function e(e){return Error.call(this),this.message=e?e.length+" errors occurred during unsubscription:\n"+e.map((function(e,t){return t+1+") "+e.toString()})).join("\n "):"",this.name="UnsubscriptionError",this.errors=e,this}return e.prototype=Object.create(Error.prototype),e}(),x=function(){function e(e){this.closed=!1,this._parentOrParents=null,this._subscriptions=null,e&&(this._ctorUnsubscribe=!0,this._unsubscribe=e)}return e.prototype.unsubscribe=function(){var t;if(!this.closed){var n,r=this,i=r._parentOrParents,o=r._ctorUnsubscribe,s=r._unsubscribe,a=r._subscriptions;if(this.closed=!0,this._parentOrParents=null,this._subscriptions=null,i instanceof e)i.remove(this);else if(null!==i)for(var c=0;c<i.length;++c){i[c].remove(this)}if(S(s)){o&&(this._unsubscribe=void 0);try{s.call(this)}catch(e){t=e instanceof N?D(e.errors):[e]}}if(I(a)){c=-1;for(var u=a.length;++c<u;){var l=a[c];if(null!==(n=l)&&"object"==typeof n)try{l.unsubscribe()}catch(e){t=t||[],e instanceof N?t=t.concat(D(e.errors)):t.push(e)}}}if(t)throw new N(t)}},e.prototype.add=function(t){var n=t;if(!t)return e.EMPTY;switch(typeof t){case"function":n=new e(t);case"object":if(n===this||n.closed||"function"!=typeof n.unsubscribe)return n;if(this.closed)return n.unsubscribe(),n;if(!(n instanceof e)){var r=n;(n=new e)._subscriptions=[r]}break;default:throw new Error("unrecognized teardown "+t+" added to Subscription.")}var i=n._parentOrParents;if(null===i)n._parentOrParents=this;else if(i instanceof e){if(i===this)return n;n._parentOrParents=[i,this]}else{if(-1!==i.indexOf(this))return n;i.push(this)}var o=this._subscriptions;return null===o?this._subscriptions=[n]:o.push(n),n},e.prototype.remove=function(e){var t=this._subscriptions;if(t){var n=t.indexOf(e);-1!==n&&t.splice(n,1)}},e.EMPTY=function(e){return e.closed=!0,e}(new e),e}();function D(e){return e.reduce((function(e,t){return e.concat(t instanceof N?t.errors:t)}),[])}var A=function(){return"function"==typeof Symbol?Symbol("rxSubscriber"):"@@rxSubscriber_"+Math.random()}(),C=function(e){function t(n,r,i){var o=e.call(this)||this;switch(o.syncErrorValue=null,o.syncErrorThrown=!1,o.syncErrorThrowable=!1,o.isStopped=!1,arguments.length){case 0:o.destination=O;break;case 1:if(!n){o.destination=O;break}if("object"==typeof n){n instanceof t?(o.syncErrorThrowable=n.syncErrorThrowable,o.destination=n,n.add(o)):(o.syncErrorThrowable=!0,o.destination=new R(o,n));break}default:o.syncErrorThrowable=!0,o.destination=new R(o,n,r,i)}return o}return E.ZT(t,e),t.prototype[A]=function(){return this},t.create=function(e,n,r){var i=new t(e,n,r);return i.syncErrorThrowable=!1,i},t.prototype.next=function(e){this.isStopped||this._next(e)},t.prototype.error=function(e){this.isStopped||(this.isStopped=!0,this._error(e))},t.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},t.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,e.prototype.unsubscribe.call(this))},t.prototype._next=function(e){this.destination.next(e)},t.prototype._error=function(e){this.destination.error(e),this.unsubscribe()},t.prototype._complete=function(){this.destination.complete(),this.unsubscribe()},t.prototype._unsubscribeAndRecycle=function(){var e=this._parentOrParents;return this._parentOrParents=null,this.unsubscribe(),this.closed=!1,this.isStopped=!1,this._parentOrParents=e,this},t}(x),R=function(e){function t(t,n,r,i){var o,s=e.call(this)||this;s._parentSubscriber=t;var a=s;return S(n)?o=n:n&&(o=n.next,r=n.error,i=n.complete,n!==O&&(S((a=Object.create(n)).unsubscribe)&&s.add(a.unsubscribe.bind(a)),a.unsubscribe=s.unsubscribe.bind(s))),s._context=a,s._next=o,s._error=r,s._complete=i,s}return E.ZT(t,e),t.prototype.next=function(e){if(!this.isStopped&&this._next){var t=this._parentSubscriber;T.useDeprecatedSynchronousErrorHandling&&t.syncErrorThrowable?this.__tryOrSetError(t,this._next,e)&&this.unsubscribe():this.__tryOrUnsub(this._next,e)}},t.prototype.error=function(e){if(!this.isStopped){var t=this._parentSubscriber,n=T.useDeprecatedSynchronousErrorHandling;if(this._error)n&&t.syncErrorThrowable?(this.__tryOrSetError(t,this._error,e),this.unsubscribe()):(this.__tryOrUnsub(this._error,e),this.unsubscribe());else if(t.syncErrorThrowable)n?(t.syncErrorValue=e,t.syncErrorThrown=!0):_(e),this.unsubscribe();else{if(this.unsubscribe(),n)throw e;_(e)}}},t.prototype.complete=function(){var e=this;if(!this.isStopped){var t=this._parentSubscriber;if(this._complete){var n=function(){return e._complete.call(e._context)};T.useDeprecatedSynchronousErrorHandling&&t.syncErrorThrowable?(this.__tryOrSetError(t,n),this.unsubscribe()):(this.__tryOrUnsub(n),this.unsubscribe())}else this.unsubscribe()}},t.prototype.__tryOrUnsub=function(e,t){try{e.call(this._context,t)}catch(e){if(this.unsubscribe(),T.useDeprecatedSynchronousErrorHandling)throw e;_(e)}},t.prototype.__tryOrSetError=function(e,t,n){if(!T.useDeprecatedSynchronousErrorHandling)throw new Error("bad call");try{t.call(this._context,n)}catch(t){return T.useDeprecatedSynchronousErrorHandling?(e.syncErrorValue=t,e.syncErrorThrown=!0,!0):(_(t),!0)}return!1},t.prototype._unsubscribe=function(){var e=this._parentSubscriber;this._context=null,this._parentSubscriber=null,e.unsubscribe()},t}(C);var P=function(){return"function"==typeof Symbol&&Symbol.observable||"@@observable"}();function F(e){return e}function M(e){return 0===e.length?F:1===e.length?e[0]:function(t){return e.reduce((function(e,t){return t(e)}),t)}}var K=function(){function e(e){this._isScalar=!1,e&&(this._subscribe=e)}return e.prototype.lift=function(t){var n=new e;return n.source=this,n.operator=t,n},e.prototype.subscribe=function(e,t,n){var r=this.operator,i=function(e,t,n){if(e){if(e instanceof C)return e;if(e[A])return e[A]()}return e||t||n?new C(e,t,n):new C(O)}(e,t,n);if(r?i.add(r.call(i,this.source)):i.add(this.source||T.useDeprecatedSynchronousErrorHandling&&!i.syncErrorThrowable?this._subscribe(i):this._trySubscribe(i)),T.useDeprecatedSynchronousErrorHandling&&i.syncErrorThrowable&&(i.syncErrorThrowable=!1,i.syncErrorThrown))throw i.syncErrorValue;return i},e.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){T.useDeprecatedSynchronousErrorHandling&&(e.syncErrorThrown=!0,e.syncErrorValue=t),!function(e){for(;e;){var t=e,n=t.closed,r=t.destination,i=t.isStopped;if(n||i)return!1;e=r&&r instanceof C?r:null}return!0}(e)?console.warn(t):e.error(t)}},e.prototype.forEach=function(e,t){var n=this;return new(t=L(t))((function(t,r){var i;i=n.subscribe((function(t){try{e(t)}catch(e){r(e),i&&i.unsubscribe()}}),r,t)}))},e.prototype._subscribe=function(e){var t=this.source;return t&&t.subscribe(e)},e.prototype[P]=function(){return this},e.prototype.pipe=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return 0===e.length?this:M(e)(this)},e.prototype.toPromise=function(e){var t=this;return new(e=L(e))((function(e,n){var r;t.subscribe((function(e){return r=e}),(function(e){return n(e)}),(function(){return e(r)}))}))},e.create=function(t){return new e(t)},e}();function L(e){if(e||(e=T.Promise||Promise),!e)throw new Error("no Promise impl found");return e}var j=function(){function e(){return Error.call(this),this.message="object unsubscribed",this.name="ObjectUnsubscribedError",this}return e.prototype=Object.create(Error.prototype),e}(),q=function(e){function t(t,n){var r=e.call(this)||this;return r.subject=t,r.subscriber=n,r.closed=!1,r}return E.ZT(t,e),t.prototype.unsubscribe=function(){if(!this.closed){this.closed=!0;var e=this.subject,t=e.observers;if(this.subject=null,t&&0!==t.length&&!e.isStopped&&!e.closed){var n=t.indexOf(this.subscriber);-1!==n&&t.splice(n,1)}}},t}(x),V=function(e){function t(t){var n=e.call(this,t)||this;return n.destination=t,n}return E.ZT(t,e),t}(C),B=function(e){function t(){var t=e.call(this)||this;return t.observers=[],t.closed=!1,t.isStopped=!1,t.hasError=!1,t.thrownError=null,t}return E.ZT(t,e),t.prototype[A]=function(){return new V(this)},t.prototype.lift=function(e){var t=new Q(this,this);return t.operator=e,t},t.prototype.next=function(e){if(this.closed)throw new j;if(!this.isStopped)for(var t=this.observers,n=t.length,r=t.slice(),i=0;i<n;i++)r[i].next(e)},t.prototype.error=function(e){if(this.closed)throw new j;this.hasError=!0,this.thrownError=e,this.isStopped=!0;for(var t=this.observers,n=t.length,r=t.slice(),i=0;i<n;i++)r[i].error(e);this.observers.length=0},t.prototype.complete=function(){if(this.closed)throw new j;this.isStopped=!0;for(var e=this.observers,t=e.length,n=e.slice(),r=0;r<t;r++)n[r].complete();this.observers.length=0},t.prototype.unsubscribe=function(){this.isStopped=!0,this.closed=!0,this.observers=null},t.prototype._trySubscribe=function(t){if(this.closed)throw new j;return e.prototype._trySubscribe.call(this,t)},t.prototype._subscribe=function(e){if(this.closed)throw new j;return this.hasError?(e.error(this.thrownError),x.EMPTY):this.isStopped?(e.complete(),x.EMPTY):(this.observers.push(e),new q(this,e))},t.prototype.asObservable=function(){var e=new K;return e.source=this,e},t.create=function(e,t){return new Q(e,t)},t}(K),Q=function(e){function t(t,n){var r=e.call(this)||this;return r.destination=t,r.source=n,r}return E.ZT(t,e),t.prototype.next=function(e){var t=this.destination;t&&t.next&&t.next(e)},t.prototype.error=function(e){var t=this.destination;t&&t.error&&this.destination.error(e)},t.prototype.complete=function(){var e=this.destination;e&&e.complete&&this.destination.complete()},t.prototype._subscribe=function(e){return this.source?this.source.subscribe(e):x.EMPTY},t}(B);function U(){}var G=function(){function e(e,t,n){this.nextOrObserver=e,this.error=t,this.complete=n}return e.prototype.call=function(e,t){return t.subscribe(new J(e,this.nextOrObserver,this.error,this.complete))},e}(),J=function(e){function t(t,n,r,i){var o=e.call(this,t)||this;return o._tapNext=U,o._tapError=U,o._tapComplete=U,o._tapError=r||U,o._tapComplete=i||U,S(n)?(o._context=o,o._tapNext=n):n&&(o._context=n,o._tapNext=n.next||U,o._tapError=n.error||U,o._tapComplete=n.complete||U),o}return E.ZT(t,e),t.prototype._next=function(e){try{this._tapNext.call(this._context,e)}catch(e){return void this.destination.error(e)}this.destination.next(e)},t.prototype._error=function(e){try{this._tapError.call(this._context,e)}catch(e){return void this.destination.error(e)}this.destination.error(e)},t.prototype._complete=function(){try{this._tapComplete.call(this._context)}catch(e){return void this.destination.error(e)}return this.destination.complete()},t}(C);const Y="[Message] Received",$="[Message] Processed";class H{constructor(){this.events=new B}subscribe(e){return this.events.pipe((t=e,function(e){return e.lift(new G(t,n,r))})).subscribe();var t,n,r}broadcastMessageReceived(e){this.events.next({type:Y,message:e})}broadcastMessageProcessed(e,t){this.events.next({type:$,message:e,result:t})}}class W{async parse(e){if(await this.verify(e)){const t=e.split(".")[1].replace(/-/g,"+").replace(/_/g,"/"),n=decodeURIComponent(atob(t).split("").map((function(e){return"%"+("00"+e.charCodeAt(0).toString(16)).slice(-2)})).join(""));return JSON.parse(n)}throw new Error(e+" is not valid.")}verify(e){return Promise.resolve(!0)}}class z{constructor({uniqueId:e,bulletinBoardClient:t,identificationKeys:n,election:r,options:i}){this.uniqueId=e,this.bulletinBoardClient=t,this.identificationKeys=n,this.election=r,this.options=i||{waitUntilNextCheck:100},this.wrapper=new k({trusteeId:e}),this.parser=new W,this.events=new H,this.nextLogEntryIndexToProcess=0,this.lastMessageProcessedWithResult=null,this.hasSetupKeyCeremony=!1}setup(){return this.election.subscribeToLogEntriesChanges()}tearDown(){this.election.unsubscribeToLogEntriesChanges()}async*setupKeyCeremony(){let e;for(;!e;)e=await this.waitForNextLogEntryResult();return yield this.wrapper.backup(),await this.processKeyCeremonyStep(e),this.hasSetupKeyCeremony=!0,this.hasSetupKeyCeremony}async runKeyCeremony(){if(!this.hasSetupKeyCeremony)throw new Error("The key ceremony has not been setup yet");if(this.needsToBeRestored())throw new Error("You need to restore the wrapper state to continue");return this.waitForNextLogEntryResult().then((async e=>(await this.processKeyCeremonyStep(e),this.wrapper.isKeyCeremonyDone()?this.tearDown():this.runKeyCeremony())))}async runTally(){if(this.needsToBeRestored())throw new Error("You need to restore the wrapper state to continue");return this.waitForNextLogEntryResult().then((async e=>(await this.processTallyStep(e),this.wrapper.isTallyDone()?this.tearDown():this.runTally())))}needsToBeRestored(){const e=this.election.getLastMessageFromTrustee(this.uniqueId);return e&&this.wrapper.needsToBeRestored(e.messageId)}restore(e){const t=this.election.getLastMessageFromTrustee(this.uniqueId);return this.hasSetupKeyCeremony=t&&this.wrapper.restore(e,t.messageId),this.hasSetupKeyCeremony}waitForNextLogEntryResult(){return new Promise((e=>{const t=setInterval((async()=>{const{logEntries:n}=this.election;if(n.length>this.nextLogEntryIndexToProcess){const n=await this.processNextLogEntry();clearInterval(t),e(n)}}),this.options.waitUntilNextCheck)}))}async processNextLogEntry(){const{logEntries:e}=this.election,t=e[this.nextLogEntryIndexToProcess];this.events.broadcastMessageReceived(t);const n=t.signedData&&await this.parser.parse(t.signedData),r=await this.wrapper.processMessage(t.messageId,n);return this.events.broadcastMessageProcessed(t,r),this.nextLogEntryIndexToProcess+=1,r}async processKeyCeremonyStep(e){if(e&&!this.isMessageAlreadyLogged(e)){const t=await this.signMessage(e);return this.bulletinBoardClient.processKeyCeremonyStep({messageId:e.message_id,signedData:t})}}async processTallyStep(e){if(e&&!this.isMessageAlreadyLogged(e)){const t=await this.signMessage(e);return this.bulletinBoardClient.processTallyStep({messageId:e.message_id,signedData:t})}}isMessageAlreadyLogged({message_id:e}){const{logEntries:t}=this.election;return t.find((t=>t.messageId===e))}signMessage(e){return this.identificationKeys.sign({iat:Math.floor(new Date/1e3),...e})}}class Z{constructor({voterId:e}){this.voterId=e,this.jointElectionKey=null,this.contests={}}processMessage(e,t){switch(b.parse(e).type){case"create_election":this.contests=t.description.contests;break;case"end_key_ceremony":{const e=JSON.parse(t.content);this.jointElectionKey=e.joint_election_key;break}}}async encrypt(e){return new Promise((e=>setTimeout(e,500))).then((()=>{if(this.jointElectionKey)return JSON.stringify(this.createBallot(e));console.warn("Invalid election status.")}))}createBallot(e){return{ballot_style:"ballot-style",contests:this.contests.map((({object_id:t,ballot_selections:n})=>({object_id:t,ballot_selections:n.map((n=>{const r=e[t].includes(n.object_id)?1:0;return{object_id:n.object_id,ciphertext:r+Math.floor(500*Math.random())*this.jointElectionKey}}))})))}}}class X{constructor({id:e,electionContext:t,bulletinBoardClient:n}){this.id=e,this.electionContext=t,this.bulletinBoardClient=n,this.wrapper=new Z({voterId:e}),this.parser=new W}setup(){return this.bulletinBoardClient.getElectionLogEntries({electionUniqueId:this.electionContext.id}).then((async e=>{for(const t of e){const e=await this.parser.parse(t.signedData);this.wrapper.processMessage(t.messageId,e)}}))}encrypt(e){return this.wrapper.encrypt(e)}verifyVote(e){const{id:t}=this.electionContext;return this.bulletinBoardClient.getLogEntry({electionUniqueId:t,contentHash:e})}}class ee{constructor(e,t){this.format="jwk",this.algorithm={name:"RSASSA-PKCS1-v1_5",modulusLength:4096,publicExponent:new Uint8Array([1,0,1]),hash:{name:"SHA-256"}},this.usages=["sign"],this.publicKeyAttrs=["alg","e","kty","n"],this.jwtHeader=this._encode64(JSON.stringify({alg:"RS256",typ:"JWT"})),this.trusteeUniqueId=e,this.privateKey=null,this.publicKey=null,this.storedPublicKey=JSON.parse(t||null),this.keyIdentifier=e+"-private-key",this.browserSupport=this._checkBrowserSupport(),this.textEncoder=new TextEncoder("utf-8"),this.dbName="identification_keys",this.dbVersion=1,this.presentPromise=this._read()}present(e){this.presentPromise.then((()=>{this._matchesStoredPublicKey(this.publicKey)?e(this.browserSupport&&null!==this.privateKey):this.reset().then(e(!1))}))}async generate(){return!(!this.browserSupport||this.storedPublicKey)&&new Promise(((e,t)=>{try{return this.crypto.subtle.generateKey(this.algorithm,!0,this.usages).then((t=>this.crypto.subtle.exportKey(this.format,t.privateKey).then((t=>{this.publicKey=this._publicKeyFromPrivateKey(t);const n=document.createElement("a");return n.setAttribute("href","data:text/plain;charset=utf-8,"+encodeURIComponent(JSON.stringify(t))),n.setAttribute("download",this.keyIdentifier+".jwk"),n.style.display="none",document.body.appendChild(n),n.click(),document.body.removeChild(n),e()})).catch(this._handleErrors)))}catch(e){return t(e)}}))}async upload(e){return!(!this.browserSupport||null!==this.privateKey)&&new Promise(((t,n)=>{if(e)this.onUploadInputChange(e,t,n);else{const e=document.createElement("input");e.setAttribute("type","file"),e.setAttribute("accept",".jwk"),e.style.display="none",document.body.appendChild(e),e.addEventListener("change",(r=>{document.body.removeChild(e),this.onUploadInputChange(r,t,n)})),e.click()}}))}onUploadInputChange(e,t,n){const r=new FileReader;r.readAsText(e.target.files[0]),r.onload=e=>{let r="";try{r=JSON.parse(e.target.result)}catch(e){return n("invalid_format")}return this.crypto.subtle.importKey(this.format,r,this.algorithm,!1,this.usages).then((e=>{const i=this._publicKeyFromPrivateKey(r);this._matchesStoredPublicKey(i)?(this.publicKey=i,this.privateKey=e,this._save().then((()=>t(!0))).catch((()=>{n("could not be saved")}))):n("invalid_public_key")})).catch((()=>{n("invalid_key")}))}}reset(){return this.privateKey=this.publicKey=null,this._clear()}async sign(e){if(!this.browserSupport||null===this.privateKey)return!1;const t=`${this.jwtHeader}.${this._encode64(JSON.stringify(e))}`,n=await this.crypto.subtle.sign(this.algorithm,this.privateKey,this.textEncoder.encode(t));return`${t}.${btoa(Reflect.apply(String.fromCharCode,null,new Uint8Array(n))).replace(/[=]/g,"").replace(/\+/g,"-").replace(/\//g,"_")}`}_checkBrowserSupport(){return this.indexedDB=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB,this.crypto=window.crypto||window.msCrypto,window.indexedDB&&window.crypto}_handleErrors(e){throw e}_publicKeyFromPrivateKey(e){return Object.keys(e).filter((e=>this.publicKeyAttrs.includes(e))).reduce(((t,n)=>(t[n]=e[n],t)),{})}_matchesStoredPublicKey(e){if(e&&this.storedPublicKey){const{n:t,e:n,kty:r}=e,{n:i,e:o,kty:s}=this.storedPublicKey;return t===i&&n===o&&r===s}return!1}_encode64(e){return btoa(unescape(encodeURIComponent(e))).replace(/[=]/g,"").replace(/\+/g,"-").replace(/\//g,"_")}async _read(){return this._useDb("readonly",(e=>{e.get(this.keyIdentifier).onsuccess=e=>{e.target.result&&(this.privateKey=e.target.result.privateKey,this.publicKey=e.target.result.publicKey)}}))}async _save(){return this._useDb("readwrite",(e=>{e.add({privateKey:this.privateKey,publicKey:this.publicKey},this.keyIdentifier)}))}async _clear(){return this._useDb("readwrite",(e=>{e.delete(this.keyIdentifier)}))}async _useDb(e,t){return new Promise(((n,r)=>{let i=null;const o=this.indexedDB.open(this.dbName,this.dbVersion);o.onerror=e=>{i=null,r(e)},o.onupgradeneeded=()=>{i=o.result,i.createObjectStore("IdentificationKeys")},o.onsuccess=()=>{i=o.result;const r=i.transaction(["IdentificationKeys"],e);t(r.objectStore("IdentificationKeys")),r.oncomplete=()=>{i.close(),n()}}}))}}class te{constructor({bulletinBoardClientParams:e,electionUniqueId:t,trusteeUniqueId:n,trusteeIdentificationKeys:r}){const i=new y(e);this.election=new g({uniqueId:t,bulletinBoardClient:i}),this.trustee=new z({uniqueId:n,bulletinBoardClient:i,identificationKeys:r,election:this.election})}async bindEvents({onSetup:e,onEvent:t,onBindRestoreButton:n,onBindStartButton:r,onRestore:i,onComplete:o,onStart:s,onTrusteeNeedsToBeRestored:a,onBackupNeeded:c,onBindBackupButton:u,onBackupStarted:l}){this.trustee.events.subscribe(t),n((e=>{const t=e.target.files[0],n=new FileReader;n.onload=async({target:e})=>{const t=e.result;this.trustee.restore(t)&&(i(),await this.trustee.runKeyCeremony(),o())},n.readAsText(t)})),r((async e=>{if(e.preventDefault(),s(),this.trustee.needsToBeRestored())a();else{const e=this.trustee.setupKeyCeremony(),{value:t}=await e.next();c(),u(t,`${this.trustee.uniqueId}-election-${this.election.uniqueId}.bak`,(async()=>{l(),await e.next(),await this.trustee.runKeyCeremony(),o()}))}})),await this.trustee.setup(),e()}}class ne{constructor({bulletinBoardClientParams:e,electionUniqueId:t,trusteeUniqueId:n,trusteeIdentificationKeys:r}){const i=new y(e);this.election=new g({uniqueId:t,bulletinBoardClient:i}),this.trustee=new z({uniqueId:n,bulletinBoardClient:i,identificationKeys:r,election:this.election})}async bindEvents({onSetup:e,onEvent:t,onBindRestoreButton:n,onBindStartButton:r,onRestore:i,onComplete:o,onStart:s,onTrusteeNeedsToBeRestored:a}){this.trustee.events.subscribe(t),r((async e=>{e.preventDefault(),s(),this.trustee.needsToBeRestored()?a():(await this.trustee.runTally(),o())})),n((e=>{const t=e.target.files[0],n=new FileReader;n.onload=async({target:e})=>{const t=e.result;this.trustee.restore(t)&&(i(),await this.trustee.runTally(),o())},n.readAsText(t)})),await this.trustee.setup(),e()}}}},t={};function n(r){if(t[r])return t[r].exports;var i=t[r]={id:r,loaded:!1,exports:{}};return e[r](i,i.exports,n),i.loaded=!0,i.exports}return n.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return n.d(t,{a:t}),t},n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),n.hmd=e=>((e=Object.create(e)).children||(e.children=[]),Object.defineProperty(e,"exports",{enumerable:!0,set:()=>{throw new Error("ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: "+e.id)}}),e),n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n(9099)})();
16
+ var r=function(e,t){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])})(e,t)};function i(e,t){function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var o=function(){return(o=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)};function s(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(e);i<r.length;i++)t.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(e,r[i])&&(n[r[i]]=e[r[i]])}return n}function a(e,t,n,r){return new(n||(n=Promise))((function(i,o){function s(e){try{c(r.next(e))}catch(e){o(e)}}function a(e){try{c(r.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,a)}c((r=r.apply(e,t||[])).next())}))}function c(e,t){var n,r,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;s;)try{if(n=1,r&&(i=2&o[0]?r.return:o[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,o[1])).done)return i;switch(r=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,r=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=s.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){s.label=o[1];break}if(6===o[0]&&s.label<i[1]){s.label=i[1],i=o;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(o);break}i[2]&&s.ops.pop(),s.trys.pop();continue}o=t.call(e,s)}catch(e){o=[6,e],r=0}finally{n=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,a])}}}function u(){for(var e=0,t=0,n=arguments.length;t<n;t++)e+=arguments[t].length;var r=Array(e),i=0;for(t=0;t<n;t++)for(var o=arguments[t],s=0,a=o.length;s<a;s++,i++)r[i]=o[s];return r}},9329:(e,t,n)=>{e.exports=n(516).Observable},516:(e,t)=>{"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function r(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function i(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e}t.Observable=void 0;var o=function(){return"function"==typeof Symbol},s=function(e){return o()&&Boolean(Symbol[e])},a=function(e){return s(e)?Symbol[e]:"@@"+e};o()&&!s("observable")&&(Symbol.observable=Symbol("observable"));var c=a("iterator"),u=a("observable"),l=a("species");function f(e,t){var n=e[t];if(null!=n){if("function"!=typeof n)throw new TypeError(n+" is not a function");return n}}function p(e){var t=e.constructor;return void 0!==t&&null===(t=t[l])&&(t=void 0),void 0!==t?t:S}function h(e){return e instanceof S}function d(e){d.log?d.log(e):setTimeout((function(){throw e}))}function y(e){Promise.resolve().then((function(){try{e()}catch(e){d(e)}}))}function v(e){var t=e._cleanup;if(void 0!==t&&(e._cleanup=void 0,t))try{if("function"==typeof t)t();else{var n=f(t,"unsubscribe");n&&n.call(t)}}catch(e){d(e)}}function m(e){e._observer=void 0,e._queue=void 0,e._state="closed"}function b(e,t,n){e._state="running";var r=e._observer;try{var i=f(r,t);switch(t){case"next":i&&i.call(r,n);break;case"error":if(m(e),!i)throw n;i.call(r,n);break;case"complete":m(e),i&&i.call(r)}}catch(e){d(e)}"closed"===e._state?v(e):"running"===e._state&&(e._state="ready")}function g(e,t,n){if("closed"!==e._state){if("buffering"!==e._state)return"ready"!==e._state?(e._state="buffering",e._queue=[{type:t,value:n}],void y((function(){return function(e){var t=e._queue;if(t){e._queue=void 0,e._state="ready";for(var n=0;n<t.length&&(b(e,t[n].type,t[n].value),"closed"!==e._state);++n);}}(e)}))):void b(e,t,n);e._queue.push({type:t,value:n})}}var k=function(){function e(t,r){n(this,e),this._cleanup=void 0,this._observer=t,this._queue=void 0,this._state="initializing";var i=new E(this);try{this._cleanup=r.call(void 0,i)}catch(e){i.error(e)}"initializing"===this._state&&(this._state="ready")}return i(e,[{key:"unsubscribe",value:function(){"closed"!==this._state&&(m(this),v(this))}},{key:"closed",get:function(){return"closed"===this._state}}]),e}(),E=function(){function e(t){n(this,e),this._subscription=t}return i(e,[{key:"next",value:function(e){g(this._subscription,"next",e)}},{key:"error",value:function(e){g(this._subscription,"error",e)}},{key:"complete",value:function(){g(this._subscription,"complete")}},{key:"closed",get:function(){return"closed"===this._subscription._state}}]),e}(),S=function(){function e(t){if(n(this,e),!(this instanceof e))throw new TypeError("Observable cannot be called as a function");if("function"!=typeof t)throw new TypeError("Observable initializer must be a function");this._subscriber=t}return i(e,[{key:"subscribe",value:function(e){return"object"==typeof e&&null!==e||(e={next:e,error:arguments[1],complete:arguments[2]}),new k(e,this._subscriber)}},{key:"forEach",value:function(e){var t=this;return new Promise((function(n,r){if("function"==typeof e)var i=t.subscribe({next:function(t){try{e(t,o)}catch(e){r(e),i.unsubscribe()}},error:r,complete:n});else r(new TypeError(e+" is not a function"));function o(){i.unsubscribe(),n()}}))}},{key:"map",value:function(e){var t=this;if("function"!=typeof e)throw new TypeError(e+" is not a function");return new(p(this))((function(n){return t.subscribe({next:function(t){try{t=e(t)}catch(e){return n.error(e)}n.next(t)},error:function(e){n.error(e)},complete:function(){n.complete()}})}))}},{key:"filter",value:function(e){var t=this;if("function"!=typeof e)throw new TypeError(e+" is not a function");return new(p(this))((function(n){return t.subscribe({next:function(t){try{if(!e(t))return}catch(e){return n.error(e)}n.next(t)},error:function(e){n.error(e)},complete:function(){n.complete()}})}))}},{key:"reduce",value:function(e){var t=this;if("function"!=typeof e)throw new TypeError(e+" is not a function");var n=p(this),r=arguments.length>1,i=!1,o=arguments[1],s=o;return new n((function(n){return t.subscribe({next:function(t){var o=!i;if(i=!0,!o||r)try{s=e(s,t)}catch(e){return n.error(e)}else s=t},error:function(e){n.error(e)},complete:function(){if(!i&&!r)return n.error(new TypeError("Cannot reduce an empty sequence"));n.next(s),n.complete()}})}))}},{key:"concat",value:function(){for(var e=this,t=arguments.length,n=new Array(t),r=0;r<t;r++)n[r]=arguments[r];var i=p(this);return new i((function(t){var r,o=0;return function e(s){r=s.subscribe({next:function(e){t.next(e)},error:function(e){t.error(e)},complete:function(){o===n.length?(r=void 0,t.complete()):e(i.from(n[o++]))}})}(e),function(){r&&(r.unsubscribe(),r=void 0)}}))}},{key:"flatMap",value:function(e){var t=this;if("function"!=typeof e)throw new TypeError(e+" is not a function");var n=p(this);return new n((function(r){var i=[],o=t.subscribe({next:function(t){if(e)try{t=e(t)}catch(e){return r.error(e)}var o=n.from(t).subscribe({next:function(e){r.next(e)},error:function(e){r.error(e)},complete:function(){var e=i.indexOf(o);e>=0&&i.splice(e,1),s()}});i.push(o)},error:function(e){r.error(e)},complete:function(){s()}});function s(){o.closed&&0===i.length&&r.complete()}return function(){i.forEach((function(e){return e.unsubscribe()})),o.unsubscribe()}}))}},{key:u,value:function(){return this}}],[{key:"from",value:function(t){var n="function"==typeof this?this:e;if(null==t)throw new TypeError(t+" is not an object");var r=f(t,u);if(r){var i=r.call(t);if(Object(i)!==i)throw new TypeError(i+" is not an object");return h(i)&&i.constructor===n?i:new n((function(e){return i.subscribe(e)}))}if(s("iterator")&&(r=f(t,c)))return new n((function(e){y((function(){if(!e.closed){var n=!0,i=!1,o=void 0;try{for(var s,a=r.call(t)[Symbol.iterator]();!(n=(s=a.next()).done);n=!0){var c=s.value;if(e.next(c),e.closed)return}}catch(e){i=!0,o=e}finally{try{n||null==a.return||a.return()}finally{if(i)throw o}}e.complete()}}))}));if(Array.isArray(t))return new n((function(e){y((function(){if(!e.closed){for(var n=0;n<t.length;++n)if(e.next(t[n]),e.closed)return;e.complete()}}))}));throw new TypeError(t+" is not observable")}},{key:"of",value:function(){for(var t=arguments.length,n=new Array(t),r=0;r<t;r++)n[r]=arguments[r];var i="function"==typeof this?this:e;return new i((function(e){y((function(){if(!e.closed){for(var t=0;t<n.length;++t)if(e.next(n[t]),e.closed)return;e.complete()}}))}))}},{key:l,get:function(){return this}}]),e}();t.Observable=S,o()&&Object.defineProperty(S,Symbol("extensions"),{value:{symbol:u,hostReportError:d},configurable:!0})},1341:(e,t,n)=>{"use strict";n.r(t),n.d(t,{Client:()=>y,Election:()=>g,IdentificationKeys:()=>ee,KeyCeremonyComponent:()=>te,MESSAGE_PROCESSED:()=>$,MESSAGE_RECEIVED:()=>Y,MessageIdentifier:()=>b,TallyComponent:()=>ne,Trustee:()=>z,VoteComponent:()=>re,Voter:()=>X});var r=n(1439),i=n(4206),o=n.n(i),s=n(9120),a=n.n(s),c=n(1798),u=n.n(c),l=n(5073),f=n.n(l),p=n(5053),h=n.n(p);class d{constructor({apiEndpointUrl:e,headers:t}){const n=new r.HttpLink({uri:e,headers:t});this.apolloClient=new r.fe({link:n,cache:new r.h4})}async getLogEntry({electionUniqueId:e,contentHash:t}){return(await this.apolloClient.query({query:f(),variables:{electionUniqueId:e,contentHash:t}})).data.logEntry}async getElectionLogEntries({electionUniqueId:e,after:t}){return(await this.apolloClient.query({query:o(),variables:{electionUniqueId:e,after:t},fetchPolicy:"no-cache"})).data.election.logEntries}async processKeyCeremonyStep({messageId:e,signedData:t}){const n=await this.apolloClient.mutate({mutation:a(),variables:{messageId:e,signedData:t}});if(n.data.processKeyCeremonyStep.error)throw new Error(n.data.processKeyCeremonyStep.error);return n.data.processKeyCeremonyStep.pendingMessage}async getPendingMessageByMessageId({messageId:e}){return(await this.apolloClient.query({query:h(),variables:{messageId:e}})).data.pendingMessage}async processTallyStep({messageId:e,signedData:t}){const n=await this.apolloClient.mutate({mutation:u(),variables:{messageId:e,signedData:t}});if(n.data.processTallyStep.error)throw new Error(n.data.processTallyStep.error);return n.data.processTallyStep.pendingMessage}}class y{constructor(e){this.apiClient=new d(e)}getLogEntry({electionUniqueId:e,contentHash:t}){return this.apiClient.getLogEntry({electionUniqueId:e,contentHash:t})}getElectionLogEntries(e){return this.apiClient.getElectionLogEntries(e)}processKeyCeremonyStep(e){return this.apiClient.processKeyCeremonyStep(e)}processTallyStep(e){return this.apiClient.processTallyStep(e)}waitForPendingMessageToBeProcessed(e,t=1e3){return new Promise((n=>{const r=setInterval((()=>{this.apiClient.getPendingMessageByMessageId({messageId:e}).then((e=>{"enqueued"!==e.status&&(clearInterval(r),n(e))}))}),t)}))}}const v="t",m=["a","b","v",v];class b{static parse(e){const[t,n]=e.split("+"),[r,i,o,s]=t.split(".",4),[a,c]=n.split(".",2),u=s?"."+s:"";if(!m.includes(a))throw new Error("Invalid message identifier format");return{electionId:`${r}.${i}`,type:o,subtype:s,typeSubtype:`${o}${u}`,author:{type:a,id:c}}}static format(e,t,n,r){return`${e}.${t}+${n}.${r}`}}class g{constructor({uniqueId:e,bulletinBoardClient:t,options:n}){this.uniqueId=e,this.bulletinBoardClient=t,this.logEntries=[],this.subscriptionId=null,this.options=n||{waitUntilNextCheck:2e3}}async subscribeToLogEntriesChanges(){this.unsubscribeToLogEntriesChanges(),await this.getLogEntries(),this.subscriptionId=setInterval((()=>{this.getLogEntries()}),this.options.waitUntilNextCheck)}unsubscribeToLogEntriesChanges(){null!==this.subscriptionId&&(clearInterval(this.subscriptionId),this.subscriptionId=null)}getLastMessageFromTrustee(e){for(let t=this.logEntries.length-1;t>=0;t--){const n=this.logEntries[t],r=b.parse(n.messageId);if(r.author.type===v&&r.author.id===e)return n}return null}getLogEntries(){const e=this.logEntries[this.logEntries.length-1],t=e&&e.id||null;return new Promise((e=>{this.bulletinBoardClient.getElectionLogEntries({electionUniqueId:this.uniqueId,after:t}).then((t=>{t.length&&(this.logEntries=[...this.logEntries,...t]),e()}))}))}}class k{constructor({trusteeId:e}){this.trusteeId=e,this.electionId=null,this.status=0,this.electionPublicKey=0}processMessage(e,t){const n=b.parse(e);switch(this.status){case 0:if("start_key_ceremony"===n.type)return this.status=1,this.electionId=n.electionId,this.electionPublicKey=2*Math.floor(50+200*Math.random())+1,{message_id:b.format(this.electionId,"key_ceremony.step_1",v,this.trusteeId),content:JSON.stringify({election_public_key:this.electionPublicKey,owner_id:this.trusteeId})};break;case 1:"end_key_ceremony"===n.type&&(this.status=2);break;case 2:"start_tally"===n.type&&(this.status=3);break;case 3:if("tally.cast"===n.typeSubtype){const e=JSON.parse(t.content);for(const[t,n]of Object.entries(e))for(const[r,i]of Object.entries(n))e[t][r]=i%this.electionPublicKey*this.electionPublicKey;return{message_id:b.format(this.electionId,"tally.share",v,this.trusteeId),content:JSON.stringify({owner_id:this.trusteeId,contests:e})}}"end_tally"===n.type&&(this.status=4)}}needsToBeRestored(e){return e&&0===this.status}backup(){return JSON.stringify(this)}restore(e,t){if(!this.needsToBeRestored(t))return console.warn("Restore not needed"),!1;const n=JSON.parse(e);if(n.trusteeId!==this.trusteeId)return console.warn("Invalid trustee id"),!1;if(t&&0===n.status)return console.warn("Invalid restored status"),!1;try{Object.assign(this,n)}catch(e){return console.error(e),!1}return!0}isKeyCeremonyDone(){return this.status>=2}isTallyDone(){return this.status>=4}}var E=n(655);function S(e){return"function"==typeof e}var w=!1,T={Promise:void 0,set useDeprecatedSynchronousErrorHandling(e){e&&(new Error).stack;w=e},get useDeprecatedSynchronousErrorHandling(){return w}};function _(e){setTimeout((function(){throw e}),0)}var O={closed:!0,next:function(e){},error:function(e){if(T.useDeprecatedSynchronousErrorHandling)throw e;_(e)},complete:function(){}},I=function(){return Array.isArray||function(e){return e&&"number"==typeof e.length}}();var N=function(){function e(e){return Error.call(this),this.message=e?e.length+" errors occurred during unsubscription:\n"+e.map((function(e,t){return t+1+") "+e.toString()})).join("\n "):"",this.name="UnsubscriptionError",this.errors=e,this}return e.prototype=Object.create(Error.prototype),e}(),x=function(){function e(e){this.closed=!1,this._parentOrParents=null,this._subscriptions=null,e&&(this._ctorUnsubscribe=!0,this._unsubscribe=e)}return e.prototype.unsubscribe=function(){var t;if(!this.closed){var n,r=this,i=r._parentOrParents,o=r._ctorUnsubscribe,s=r._unsubscribe,a=r._subscriptions;if(this.closed=!0,this._parentOrParents=null,this._subscriptions=null,i instanceof e)i.remove(this);else if(null!==i)for(var c=0;c<i.length;++c){i[c].remove(this)}if(S(s)){o&&(this._unsubscribe=void 0);try{s.call(this)}catch(e){t=e instanceof N?D(e.errors):[e]}}if(I(a)){c=-1;for(var u=a.length;++c<u;){var l=a[c];if(null!==(n=l)&&"object"==typeof n)try{l.unsubscribe()}catch(e){t=t||[],e instanceof N?t=t.concat(D(e.errors)):t.push(e)}}}if(t)throw new N(t)}},e.prototype.add=function(t){var n=t;if(!t)return e.EMPTY;switch(typeof t){case"function":n=new e(t);case"object":if(n===this||n.closed||"function"!=typeof n.unsubscribe)return n;if(this.closed)return n.unsubscribe(),n;if(!(n instanceof e)){var r=n;(n=new e)._subscriptions=[r]}break;default:throw new Error("unrecognized teardown "+t+" added to Subscription.")}var i=n._parentOrParents;if(null===i)n._parentOrParents=this;else if(i instanceof e){if(i===this)return n;n._parentOrParents=[i,this]}else{if(-1!==i.indexOf(this))return n;i.push(this)}var o=this._subscriptions;return null===o?this._subscriptions=[n]:o.push(n),n},e.prototype.remove=function(e){var t=this._subscriptions;if(t){var n=t.indexOf(e);-1!==n&&t.splice(n,1)}},e.EMPTY=function(e){return e.closed=!0,e}(new e),e}();function D(e){return e.reduce((function(e,t){return e.concat(t instanceof N?t.errors:t)}),[])}var A=function(){return"function"==typeof Symbol?Symbol("rxSubscriber"):"@@rxSubscriber_"+Math.random()}(),C=function(e){function t(n,r,i){var o=e.call(this)||this;switch(o.syncErrorValue=null,o.syncErrorThrown=!1,o.syncErrorThrowable=!1,o.isStopped=!1,arguments.length){case 0:o.destination=O;break;case 1:if(!n){o.destination=O;break}if("object"==typeof n){n instanceof t?(o.syncErrorThrowable=n.syncErrorThrowable,o.destination=n,n.add(o)):(o.syncErrorThrowable=!0,o.destination=new R(o,n));break}default:o.syncErrorThrowable=!0,o.destination=new R(o,n,r,i)}return o}return E.ZT(t,e),t.prototype[A]=function(){return this},t.create=function(e,n,r){var i=new t(e,n,r);return i.syncErrorThrowable=!1,i},t.prototype.next=function(e){this.isStopped||this._next(e)},t.prototype.error=function(e){this.isStopped||(this.isStopped=!0,this._error(e))},t.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},t.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,e.prototype.unsubscribe.call(this))},t.prototype._next=function(e){this.destination.next(e)},t.prototype._error=function(e){this.destination.error(e),this.unsubscribe()},t.prototype._complete=function(){this.destination.complete(),this.unsubscribe()},t.prototype._unsubscribeAndRecycle=function(){var e=this._parentOrParents;return this._parentOrParents=null,this.unsubscribe(),this.closed=!1,this.isStopped=!1,this._parentOrParents=e,this},t}(x),R=function(e){function t(t,n,r,i){var o,s=e.call(this)||this;s._parentSubscriber=t;var a=s;return S(n)?o=n:n&&(o=n.next,r=n.error,i=n.complete,n!==O&&(S((a=Object.create(n)).unsubscribe)&&s.add(a.unsubscribe.bind(a)),a.unsubscribe=s.unsubscribe.bind(s))),s._context=a,s._next=o,s._error=r,s._complete=i,s}return E.ZT(t,e),t.prototype.next=function(e){if(!this.isStopped&&this._next){var t=this._parentSubscriber;T.useDeprecatedSynchronousErrorHandling&&t.syncErrorThrowable?this.__tryOrSetError(t,this._next,e)&&this.unsubscribe():this.__tryOrUnsub(this._next,e)}},t.prototype.error=function(e){if(!this.isStopped){var t=this._parentSubscriber,n=T.useDeprecatedSynchronousErrorHandling;if(this._error)n&&t.syncErrorThrowable?(this.__tryOrSetError(t,this._error,e),this.unsubscribe()):(this.__tryOrUnsub(this._error,e),this.unsubscribe());else if(t.syncErrorThrowable)n?(t.syncErrorValue=e,t.syncErrorThrown=!0):_(e),this.unsubscribe();else{if(this.unsubscribe(),n)throw e;_(e)}}},t.prototype.complete=function(){var e=this;if(!this.isStopped){var t=this._parentSubscriber;if(this._complete){var n=function(){return e._complete.call(e._context)};T.useDeprecatedSynchronousErrorHandling&&t.syncErrorThrowable?(this.__tryOrSetError(t,n),this.unsubscribe()):(this.__tryOrUnsub(n),this.unsubscribe())}else this.unsubscribe()}},t.prototype.__tryOrUnsub=function(e,t){try{e.call(this._context,t)}catch(e){if(this.unsubscribe(),T.useDeprecatedSynchronousErrorHandling)throw e;_(e)}},t.prototype.__tryOrSetError=function(e,t,n){if(!T.useDeprecatedSynchronousErrorHandling)throw new Error("bad call");try{t.call(this._context,n)}catch(t){return T.useDeprecatedSynchronousErrorHandling?(e.syncErrorValue=t,e.syncErrorThrown=!0,!0):(_(t),!0)}return!1},t.prototype._unsubscribe=function(){var e=this._parentSubscriber;this._context=null,this._parentSubscriber=null,e.unsubscribe()},t}(C);var P=function(){return"function"==typeof Symbol&&Symbol.observable||"@@observable"}();function F(e){return e}function M(e){return 0===e.length?F:1===e.length?e[0]:function(t){return e.reduce((function(e,t){return t(e)}),t)}}var K=function(){function e(e){this._isScalar=!1,e&&(this._subscribe=e)}return e.prototype.lift=function(t){var n=new e;return n.source=this,n.operator=t,n},e.prototype.subscribe=function(e,t,n){var r=this.operator,i=function(e,t,n){if(e){if(e instanceof C)return e;if(e[A])return e[A]()}return e||t||n?new C(e,t,n):new C(O)}(e,t,n);if(r?i.add(r.call(i,this.source)):i.add(this.source||T.useDeprecatedSynchronousErrorHandling&&!i.syncErrorThrowable?this._subscribe(i):this._trySubscribe(i)),T.useDeprecatedSynchronousErrorHandling&&i.syncErrorThrowable&&(i.syncErrorThrowable=!1,i.syncErrorThrown))throw i.syncErrorValue;return i},e.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){T.useDeprecatedSynchronousErrorHandling&&(e.syncErrorThrown=!0,e.syncErrorValue=t),!function(e){for(;e;){var t=e,n=t.closed,r=t.destination,i=t.isStopped;if(n||i)return!1;e=r&&r instanceof C?r:null}return!0}(e)?console.warn(t):e.error(t)}},e.prototype.forEach=function(e,t){var n=this;return new(t=L(t))((function(t,r){var i;i=n.subscribe((function(t){try{e(t)}catch(e){r(e),i&&i.unsubscribe()}}),r,t)}))},e.prototype._subscribe=function(e){var t=this.source;return t&&t.subscribe(e)},e.prototype[P]=function(){return this},e.prototype.pipe=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return 0===e.length?this:M(e)(this)},e.prototype.toPromise=function(e){var t=this;return new(e=L(e))((function(e,n){var r;t.subscribe((function(e){return r=e}),(function(e){return n(e)}),(function(){return e(r)}))}))},e.create=function(t){return new e(t)},e}();function L(e){if(e||(e=T.Promise||Promise),!e)throw new Error("no Promise impl found");return e}var j=function(){function e(){return Error.call(this),this.message="object unsubscribed",this.name="ObjectUnsubscribedError",this}return e.prototype=Object.create(Error.prototype),e}(),q=function(e){function t(t,n){var r=e.call(this)||this;return r.subject=t,r.subscriber=n,r.closed=!1,r}return E.ZT(t,e),t.prototype.unsubscribe=function(){if(!this.closed){this.closed=!0;var e=this.subject,t=e.observers;if(this.subject=null,t&&0!==t.length&&!e.isStopped&&!e.closed){var n=t.indexOf(this.subscriber);-1!==n&&t.splice(n,1)}}},t}(x),V=function(e){function t(t){var n=e.call(this,t)||this;return n.destination=t,n}return E.ZT(t,e),t}(C),B=function(e){function t(){var t=e.call(this)||this;return t.observers=[],t.closed=!1,t.isStopped=!1,t.hasError=!1,t.thrownError=null,t}return E.ZT(t,e),t.prototype[A]=function(){return new V(this)},t.prototype.lift=function(e){var t=new Q(this,this);return t.operator=e,t},t.prototype.next=function(e){if(this.closed)throw new j;if(!this.isStopped)for(var t=this.observers,n=t.length,r=t.slice(),i=0;i<n;i++)r[i].next(e)},t.prototype.error=function(e){if(this.closed)throw new j;this.hasError=!0,this.thrownError=e,this.isStopped=!0;for(var t=this.observers,n=t.length,r=t.slice(),i=0;i<n;i++)r[i].error(e);this.observers.length=0},t.prototype.complete=function(){if(this.closed)throw new j;this.isStopped=!0;for(var e=this.observers,t=e.length,n=e.slice(),r=0;r<t;r++)n[r].complete();this.observers.length=0},t.prototype.unsubscribe=function(){this.isStopped=!0,this.closed=!0,this.observers=null},t.prototype._trySubscribe=function(t){if(this.closed)throw new j;return e.prototype._trySubscribe.call(this,t)},t.prototype._subscribe=function(e){if(this.closed)throw new j;return this.hasError?(e.error(this.thrownError),x.EMPTY):this.isStopped?(e.complete(),x.EMPTY):(this.observers.push(e),new q(this,e))},t.prototype.asObservable=function(){var e=new K;return e.source=this,e},t.create=function(e,t){return new Q(e,t)},t}(K),Q=function(e){function t(t,n){var r=e.call(this)||this;return r.destination=t,r.source=n,r}return E.ZT(t,e),t.prototype.next=function(e){var t=this.destination;t&&t.next&&t.next(e)},t.prototype.error=function(e){var t=this.destination;t&&t.error&&this.destination.error(e)},t.prototype.complete=function(){var e=this.destination;e&&e.complete&&this.destination.complete()},t.prototype._subscribe=function(e){return this.source?this.source.subscribe(e):x.EMPTY},t}(B);function U(){}var G=function(){function e(e,t,n){this.nextOrObserver=e,this.error=t,this.complete=n}return e.prototype.call=function(e,t){return t.subscribe(new J(e,this.nextOrObserver,this.error,this.complete))},e}(),J=function(e){function t(t,n,r,i){var o=e.call(this,t)||this;return o._tapNext=U,o._tapError=U,o._tapComplete=U,o._tapError=r||U,o._tapComplete=i||U,S(n)?(o._context=o,o._tapNext=n):n&&(o._context=n,o._tapNext=n.next||U,o._tapError=n.error||U,o._tapComplete=n.complete||U),o}return E.ZT(t,e),t.prototype._next=function(e){try{this._tapNext.call(this._context,e)}catch(e){return void this.destination.error(e)}this.destination.next(e)},t.prototype._error=function(e){try{this._tapError.call(this._context,e)}catch(e){return void this.destination.error(e)}this.destination.error(e)},t.prototype._complete=function(){try{this._tapComplete.call(this._context)}catch(e){return void this.destination.error(e)}return this.destination.complete()},t}(C);const Y="[Message] Received",$="[Message] Processed";class H{constructor(){this.events=new B}subscribe(e){return this.events.pipe((t=e,function(e){return e.lift(new G(t,n,r))})).subscribe();var t,n,r}broadcastMessageReceived(e){this.events.next({type:Y,message:e})}broadcastMessageProcessed(e,t){this.events.next({type:$,message:e,result:t})}}class W{async parse(e){if(await this.verify(e)){const t=e.split(".")[1].replace(/-/g,"+").replace(/_/g,"/"),n=decodeURIComponent(atob(t).split("").map((function(e){return"%"+("00"+e.charCodeAt(0).toString(16)).slice(-2)})).join(""));return JSON.parse(n)}throw new Error(e+" is not valid.")}verify(e){return Promise.resolve(!0)}}class z{constructor({uniqueId:e,bulletinBoardClient:t,identificationKeys:n,election:r,options:i}){this.uniqueId=e,this.bulletinBoardClient=t,this.identificationKeys=n,this.election=r,this.options=i||{waitUntilNextCheck:100},this.wrapper=new k({trusteeId:e}),this.parser=new W,this.events=new H,this.nextLogEntryIndexToProcess=0,this.lastMessageProcessedWithResult=null,this.hasSetupKeyCeremony=!1}setup(){return this.election.subscribeToLogEntriesChanges()}tearDown(){this.election.unsubscribeToLogEntriesChanges()}async*setupKeyCeremony(){let e;for(;!e;)e=await this.waitForNextLogEntryResult();return yield this.wrapper.backup(),await this.processKeyCeremonyStep(e),this.hasSetupKeyCeremony=!0,this.hasSetupKeyCeremony}async runKeyCeremony(){if(!this.hasSetupKeyCeremony)throw new Error("The key ceremony has not been setup yet");if(this.needsToBeRestored())throw new Error("You need to restore the wrapper state to continue");return this.waitForNextLogEntryResult().then((async e=>(await this.processKeyCeremonyStep(e),this.wrapper.isKeyCeremonyDone()?this.tearDown():this.runKeyCeremony())))}async runTally(){if(this.needsToBeRestored())throw new Error("You need to restore the wrapper state to continue");return this.waitForNextLogEntryResult().then((async e=>(await this.processTallyStep(e),this.wrapper.isTallyDone()?this.tearDown():this.runTally())))}needsToBeRestored(){const e=this.election.getLastMessageFromTrustee(this.uniqueId);return e&&this.wrapper.needsToBeRestored(e.messageId)}restore(e){const t=this.election.getLastMessageFromTrustee(this.uniqueId);return this.hasSetupKeyCeremony=t&&this.wrapper.restore(e,t.messageId),this.hasSetupKeyCeremony}waitForNextLogEntryResult(){return new Promise((e=>{const t=setInterval((async()=>{const{logEntries:n}=this.election;if(n.length>this.nextLogEntryIndexToProcess){const n=await this.processNextLogEntry();clearInterval(t),e(n)}}),this.options.waitUntilNextCheck)}))}async processNextLogEntry(){const{logEntries:e}=this.election,t=e[this.nextLogEntryIndexToProcess];this.events.broadcastMessageReceived(t);const n=t.signedData&&await this.parser.parse(t.signedData),r=await this.wrapper.processMessage(t.messageId,n);return this.events.broadcastMessageProcessed(t,r),this.nextLogEntryIndexToProcess+=1,r}async processKeyCeremonyStep(e){if(e&&!this.isMessageAlreadyLogged(e)){const t=await this.signMessage(e);return this.bulletinBoardClient.processKeyCeremonyStep({messageId:e.message_id,signedData:t})}}async processTallyStep(e){if(e&&!this.isMessageAlreadyLogged(e)){const t=await this.signMessage(e);return this.bulletinBoardClient.processTallyStep({messageId:e.message_id,signedData:t})}}isMessageAlreadyLogged({message_id:e}){const{logEntries:t}=this.election;return t.find((t=>t.messageId===e))}signMessage(e){return this.identificationKeys.sign({iat:Math.floor(new Date/1e3),...e})}}class Z{constructor({voterId:e}){this.voterId=e,this.jointElectionKey=null,this.contests={}}processMessage(e,t){switch(b.parse(e).type){case"create_election":this.contests=t.description.contests;break;case"end_key_ceremony":{const e=JSON.parse(t.content);this.jointElectionKey=e.joint_election_key;break}}}async encrypt(e){return new Promise((e=>setTimeout(e,500))).then((()=>{if(this.jointElectionKey)return JSON.stringify(this.createBallot(e));console.warn("Invalid election status.")}))}createBallot(e){return{ballot_style:"ballot-style",contests:this.contests.map((({object_id:t,ballot_selections:n})=>({object_id:t,ballot_selections:n.map((n=>{const r=e[t]&&e[t].includes(n.object_id)?1:0;return{object_id:n.object_id,ciphertext:r+Math.floor(500*Math.random())*this.jointElectionKey}}))})))}}}class X{constructor({uniqueId:e,election:t,bulletinBoardClient:n}){this.uniqueId=e,this.election=t,this.bulletinBoardClient=n,this.wrapper=new Z({voterId:e}),this.parser=new W}setup(){return this.bulletinBoardClient.getElectionLogEntries({electionUniqueId:this.election.uniqueId}).then((async e=>{for(const t of e){const e=await this.parser.parse(t.signedData);this.wrapper.processMessage(t.messageId,e)}}))}async encrypt(e){const t=await this.wrapper.encrypt(e);return window.crypto.subtle.digest("SHA-256",(new TextEncoder).encode(t)).then((e=>{const n=Array.from(new Uint8Array(e)).map((e=>e.toString(16).padStart(2,"0"))).join("");return{encryptedVote:t,encryptedVoteHash:n}}))}verifyVote(e){const{uniqueId:t}=this.election;return this.bulletinBoardClient.getLogEntry({electionUniqueId:t,contentHash:e})}}class ee{constructor(e,t){this.format="jwk",this.algorithm={name:"RSASSA-PKCS1-v1_5",modulusLength:4096,publicExponent:new Uint8Array([1,0,1]),hash:{name:"SHA-256"}},this.usages=["sign"],this.publicKeyAttrs=["alg","e","kty","n"],this.jwtHeader=this._encode64(JSON.stringify({alg:"RS256",typ:"JWT"})),this.trusteeUniqueId=e,this.privateKey=null,this.publicKey=null,this.storedPublicKey=JSON.parse(t||null),this.keyIdentifier=e+"-private-key",this.browserSupport=this._checkBrowserSupport(),this.textEncoder=new TextEncoder("utf-8"),this.dbName="identification_keys",this.dbVersion=1,this.presentPromise=this._read()}present(e){this.presentPromise.then((()=>{this._matchesStoredPublicKey(this.publicKey)?e(this.browserSupport&&null!==this.privateKey):this.reset().then(e(!1))}))}async generate(){return!(!this.browserSupport||this.storedPublicKey)&&new Promise(((e,t)=>{try{return this.crypto.subtle.generateKey(this.algorithm,!0,this.usages).then((t=>this.crypto.subtle.exportKey(this.format,t.privateKey).then((t=>{this.publicKey=this._publicKeyFromPrivateKey(t);const n=document.createElement("a");return n.setAttribute("href","data:text/plain;charset=utf-8,"+encodeURIComponent(JSON.stringify(t))),n.setAttribute("download",this.keyIdentifier+".jwk"),n.style.display="none",document.body.appendChild(n),n.click(),document.body.removeChild(n),e()})).catch(this._handleErrors)))}catch(e){return t(e)}}))}async upload(e){return!(!this.browserSupport||null!==this.privateKey)&&new Promise(((t,n)=>{if(e)this.onUploadInputChange(e,t,n);else{const e=document.createElement("input");e.setAttribute("type","file"),e.setAttribute("accept",".jwk"),e.style.display="none",document.body.appendChild(e),e.addEventListener("change",(r=>{document.body.removeChild(e),this.onUploadInputChange(r,t,n)})),e.click()}}))}onUploadInputChange(e,t,n){const r=new FileReader;r.readAsText(e.target.files[0]),r.onload=e=>{let r="";try{r=JSON.parse(e.target.result)}catch(e){return n("invalid_format")}return this.crypto.subtle.importKey(this.format,r,this.algorithm,!1,this.usages).then((e=>{const i=this._publicKeyFromPrivateKey(r);this._matchesStoredPublicKey(i)?(this.publicKey=i,this.privateKey=e,this._save().then((()=>t(!0))).catch((()=>{n("could not be saved")}))):n("invalid_public_key")})).catch((()=>{n("invalid_key")}))}}reset(){return this.privateKey=this.publicKey=null,this._clear()}async sign(e){if(!this.browserSupport||null===this.privateKey)return!1;const t=`${this.jwtHeader}.${this._encode64(JSON.stringify(e))}`,n=await this.crypto.subtle.sign(this.algorithm,this.privateKey,this.textEncoder.encode(t));return`${t}.${btoa(Reflect.apply(String.fromCharCode,null,new Uint8Array(n))).replace(/[=]/g,"").replace(/\+/g,"-").replace(/\//g,"_")}`}_checkBrowserSupport(){return this.indexedDB=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB,this.crypto=window.crypto||window.msCrypto,window.indexedDB&&window.crypto}_handleErrors(e){throw e}_publicKeyFromPrivateKey(e){return Object.keys(e).filter((e=>this.publicKeyAttrs.includes(e))).reduce(((t,n)=>(t[n]=e[n],t)),{})}_matchesStoredPublicKey(e){if(e&&this.storedPublicKey){const{n:t,e:n,kty:r}=e,{n:i,e:o,kty:s}=this.storedPublicKey;return t===i&&n===o&&r===s}return!1}_encode64(e){return btoa(unescape(encodeURIComponent(e))).replace(/[=]/g,"").replace(/\+/g,"-").replace(/\//g,"_")}async _read(){return this._useDb("readonly",(e=>{e.get(this.keyIdentifier).onsuccess=e=>{e.target.result&&(this.privateKey=e.target.result.privateKey,this.publicKey=e.target.result.publicKey)}}))}async _save(){return this._useDb("readwrite",(e=>{e.add({privateKey:this.privateKey,publicKey:this.publicKey},this.keyIdentifier)}))}async _clear(){return this._useDb("readwrite",(e=>{e.delete(this.keyIdentifier)}))}async _useDb(e,t){return new Promise(((n,r)=>{let i=null;const o=this.indexedDB.open(this.dbName,this.dbVersion);o.onerror=e=>{i=null,r(e)},o.onupgradeneeded=()=>{i=o.result,i.createObjectStore("IdentificationKeys")},o.onsuccess=()=>{i=o.result;const r=i.transaction(["IdentificationKeys"],e);t(r.objectStore("IdentificationKeys")),r.oncomplete=()=>{i.close(),n()}}}))}}class te{constructor({bulletinBoardClientParams:e,electionUniqueId:t,trusteeUniqueId:n,trusteeIdentificationKeys:r}){const i=new y(e);this.election=new g({uniqueId:t,bulletinBoardClient:i}),this.trustee=new z({uniqueId:n,bulletinBoardClient:i,identificationKeys:r,election:this.election})}async bindEvents({onSetup:e,onEvent:t,onBindRestoreButton:n,onBindStartButton:r,onRestore:i,onComplete:o,onStart:s,onTrusteeNeedsToBeRestored:a,onBackupNeeded:c,onBindBackupButton:u,onBackupStarted:l}){this.trustee.events.subscribe(t),n((e=>{const t=e.target.files[0],n=new FileReader;n.onload=async({target:e})=>{const t=e.result;this.trustee.restore(t)&&(i(),await this.trustee.runKeyCeremony(),o())},n.readAsText(t)})),r((async e=>{if(e.preventDefault(),s(),this.trustee.needsToBeRestored())a();else{const e=this.trustee.setupKeyCeremony(),{value:t}=await e.next();c(),u(t,`${this.trustee.uniqueId}-election-${this.election.uniqueId}.bak`,(async()=>{l(),await e.next(),await this.trustee.runKeyCeremony(),o()}))}})),await this.trustee.setup(),e()}}class ne{constructor({bulletinBoardClientParams:e,electionUniqueId:t,trusteeUniqueId:n,trusteeIdentificationKeys:r}){const i=new y(e);this.election=new g({uniqueId:t,bulletinBoardClient:i}),this.trustee=new z({uniqueId:n,bulletinBoardClient:i,identificationKeys:r,election:this.election})}async bindEvents({onSetup:e,onEvent:t,onBindRestoreButton:n,onBindStartButton:r,onRestore:i,onComplete:o,onStart:s,onTrusteeNeedsToBeRestored:a}){this.trustee.events.subscribe(t),r((async e=>{e.preventDefault(),s(),this.trustee.needsToBeRestored()?a():(await this.trustee.runTally(),o())})),n((e=>{const t=e.target.files[0],n=new FileReader;n.onload=async({target:e})=>{const t=e.result;this.trustee.restore(t)&&(i(),await this.trustee.runTally(),o())},n.readAsText(t)})),await this.trustee.setup(),e()}}class re{constructor({bulletinBoardClientParams:e,electionUniqueId:t,voterUniqueId:n}){this.bulletinBoardClient=new y(e);const r=new g({uniqueId:t,bulletinBoardClient:this.bulletinBoardClient});this.voter=new X({uniqueId:n,bulletinBoardClient:this.bulletinBoardClient,election:r})}async bindEvents({onSetup:e,onBindStartButton:t,onStart:n,onVoteValidation:r,onVoteEncrypted:i,onComplete:o,onInvalid:s}){t((()=>{n(),r((e=>{this.voter.encrypt(e).then((async e=>{i(e)})).then((()=>{o()}))}),(()=>{s()}))})),await this.voter.setup(),e()}}}},t={};function n(r){if(t[r])return t[r].exports;var i=t[r]={id:r,loaded:!1,exports:{}};return e[r](i,i.exports,n),i.loaded=!0,i.exports}return n.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return n.d(t,{a:t}),t},n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),n.hmd=e=>((e=Object.create(e)).children||(e.children=[]),Object.defineProperty(e,"exports",{enumerable:!0,set:()=>{throw new Error("ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: "+e.id)}}),e),n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n(1341)})();
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Decidim
4
4
  module BulletinBoard
5
- VERSION = "0.9.1"
5
+ VERSION = "0.9.2"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decidim-bulletin_board
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Morcillo