UNPKG

@woleet/woleet-widget

Version:

Woleet verification widget

3 lines (2 loc) 11.1 kB
"use strict";!function(e){function t(e){if(e&&"string"!=typeof e)throw new Error("Invalid parameter type");var t={state:"initial",hash:null},n=function(e){return function(t,n){return Object.defineProperty(e,t,{enumerable:!1,value:n})}},r=function(e){if(!e instanceof Element)throw new TypeError;var t=e,r=this,i=function(){return r},o=n(this);o("target",function(){return t}),o("attr",function(e,n){return i(n?t.setAttribute(e,n):t.removeAttribute(e))}),o("removeClass",function(e){return i(Array.isArray(e)?e.forEach(function(e){return t.classList.remove(e)}):t.classList.remove(e))}),o("addClass",function(e){return i(Array.isArray(e)?e.forEach(function(e){return t.classList.add(e)}):t.classList.add(e))}),o("text",function(e,n){return i(n?t.innerText+=e:t.innerText=e)}),o("html",function(e,n){return i(n?t.innerHTML+=e:t.innerHTML=e)}),o("link",function(e){return i(r.text(e).attr("href",e))}),o("clear",function(){return i(r.text(""),r.attr("href",null))}),o("show",function(){return r.removeClass("hidden")}),o("hide",function(){return r.addClass("hidden")}),o("style",function(e){if(Array.isArray(e))return e.map(function(e){return t.style[e]});if("string"==typeof e)return t.style[e];for(var n in e)t.style[n]=e[n]}),o("on",function(e,n,r){return i(t.addEventListener(e,n,r))}),o("toDom",function(){var e=r.target();for(var n in r){var i=r[n];try{Array.isArray(i)?i.forEach(function(t){return e.appendChild(t.toDom())}):e.appendChild(i.toDom())}catch(e){console.warn(n,t,r[n],e)}}return e})},i=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"div",t=arguments[1],n=new r(document.createElement(e));return t&&n.addClass(t),n},o=i("div","widget"),a=o.head=i("div","head");a.logo=i("div","woleet-logo"),a.x=i("div","floatright"),a.x.receipt=i("button",["receipt-button","clickable"]).on("click",b).text("Drop proof"),a.x.reset=i("div",["reset","mini-button","clickable"]).on("click",_),a.x.cancel=i("div",["cancel","mini-button","clickable"]).on("click",k);var s=o.body=i("div","body"),c=s.hashZone=i("div","hashZone");c.hashProgessContainer=i("div","progressBarContainer"),c.percentage=i("span").text("Hashing... 0%");var d=c.hashProgessContainer.progressBar=i("div","progressBar"),u=s.infoZone=i("div","infoZone");u.items=[],n(u)("addItem",function(){var e=this,t=i("div","infoZoneItem");return t.mainTextZone=i("div",["text","small"]),t.subTextZone=i("div",["text","x-small"]),t.byTextZone=i("span",["text","x-small"]).text("by "),t.signTextZone=i("a",["link","x-small"]),t.identityTextZone=i("div",["text","x-small"]),t.warnTextZone=i("div",["text","x-small","warn"]),t.on("click",function(){return function(e,t){if(!y)return;if(T===e)return w();T=e,t.forEach(function(e){e.removeClass("expanded"),e.addClass("reduced")}),e.addClass("expanded"),e.removeClass("reduced")}(t,e.items)}),this.items.push(t),t}),n(u)("flush",function(){return this.html(""),this.items=[],this});var h=s.dropZone=i("div","dropZoneContainer");h.inputContainer=i("div"),h.inputContainer.mainTextZone=i("div",["text","small"]),h.inputContainer.subTextZone=i("div",["text","x-small"]),h.inputContainer.input=i("input",["dropZone","clickable"]).attr("type","file").on("change",x);var l=o.foot=i("div","foot");function f(){p(),h.inputContainer.mainTextZone.text("Drop the file to verify"),u.removeClass(["ok","error","info"]),u.hide(),h.show(),c.hide(),a.x.reset.hide(),a.x.cancel.hide(),a.x.receipt.hide(),l.hide()}function p(){h.inputContainer.mainTextZone.clear(),h.inputContainer.subTextZone.clear(),u.flush()}l.expand=i("div",["expand","expand-button","clickable"]).on("click",w),f(),e&&x.call({files:[e]});var m=new woleet.file.Hasher;function x(){var e=this.files[0];if(e){"done"===t.state&&g("init");var n=function(e){var t=100*e.progress;t!=t&&(t=0),t=t.toFixed(0),d.style({width:t+"%"}),c.percentage.text("Hashing... "+t+"%")};if("needReceipt"===t.state)return(r=e,new Promise(function(e,t){var n=new FileReader;n.onloadend=function(n){try{e(JSON.parse(n.target.result))}catch(e){t(new Error("unable_to_parse_json"))}},n.readAsText(r)})).then(function(e){return woleet.verify.DAB(t.hash,e,n)}).then(function(e){if("verified"!==e.code)throw new Error(e.code);g("ok",e),t.state="done"}).catch(function(e){g("error",e)});var r,i=function(e){return g("pending","Verifying proof(s)... "),woleet.verify.WoleetDAB(e,n).then(function(e){t.state="done",function(e){p(),e.length?(e.length>1&&(l.expand.text(e.length+"+"),l.expand.removeClass("up-arrow"),l.show()),e.forEach(function(e,t){"verified"===e.code?v("ok",e,t):v("error",e.code,t)}),u.toDom()):(t.state="needReceipt",g("need-receipt"))}(e.sort(function(e,t){return t.timestamp>e.timestamp?-1:t.timestamp<e.timestamp?1:0}))}).catch(function(e){e.hasOwnProperty("code")||"need-receipt"===e.message?(t.state="needReceipt",g("need-receipt")):g("error",e)})};return"string"==typeof e?(t.hash=e,i(t.hash)):(g("hashing"),new Promise(function(r,o){m.start(e),m.on("progress",n),m.on("error",function(e){return g("error",e.error||e.message)}),m.on("result",function(e){t.hash=e.result,n({progress:0}),r(i(t.hash))})}))}}function b(){p(),l.hide(),t.state="needReceipt",g("need-receipt"),h.inputContainer.mainTextZone.text("Drop the proof receipt"),h.inputContainer.subTextZone.clear()}function v(e,n,r){u.show();var i=u.addItem();switch(i.addClass(r?"reduced":"expanded"),h.hide(),c.hide(),a.x.cancel.hide(),e){case"ok":var o=n.receipt.signature,s=o?o.pubKey:null;if(n.confirmations){var d=function(e){return e.toLocaleDateString("en",{year:"numeric",month:"short",day:"numeric",hour:"numeric",minute:"numeric"})}(n.timestamp);i.mainTextZone.text((s?"Signed":"Timestamped")+" on "+d),i.addClass("ok")}else i.mainTextZone.text("Proof not yet verifiable"),i.subTextZone.text("The proof receipt's transaction is not yet confirmed (try again later)"),i.addClass("info");var l=n.identityVerificationStatus,f=l?Object.assign({},l.signedIdentity,l.identity):null;l&&"verified"===l.code&&f?(i.byTextZone.addClass("link"),o.identityURL&&i.signTextZone.link(o.identityURL+"?pubKey="+s+"&leftData=random"+(o.signedIdentity?"&signedIdentity="+encodeURIComponent(o.signedIdentity):"")),f&&f.commonName?(i.signTextZone.text(""+f.commonName),i.identityTextZone.html((f.organization||"")+(f.organization&&f.organizationalUnit?" - ":"")+(f.organizationalUnit||"")+"<br>\n "+(f.locality||"")+(f.locality&&f.country?" - ":"")+(f.country||""))):i.signTextZone.text(""+o.identityURL),i.byTextZone.show()):s?(i.signTextZone.text(""+s),i.byTextZone.show()):i.byTextZone.hide(),l&&l.code&&"verified"!==l.code&&i.warnTextZone.text("Cannot verify identity ("+l.code+")"),h.attr("disabled",!0),a.x.receipt.show();break;case"error":"needReceipt"===t.state&&a.x.receipt.show();var p=n instanceof Error?n.message:n,m=function(e){var t={};switch(e){case"need-receipt":t.main="No public proof found at Woleet",t.sub="No public proof receipt found at Woleet: you must provide one to verify this file";break;case"file_matched_but_anchor_not_yet_processed":t.main="The proof is not yet verifiable",t.sub="A public proof receipt has been found at Woleet, but is not yet verifiable (try again later)";break;case"target_hash_mismatch":t.main="The proof mismatches the file",t.sub="The proof receipt's target hash attribute doesn't match the file hash";break;case"unable_to_parse_json":t.main="The proof cannot be parsed",t.sub="The file you provided is not a valid Chainpoint 1 or 2 proof receipt";break;case"merkle_root_mismatch":t.main="The proof is corrupted",t.sub="The proof receipt's merkle root attribute does not match the proof result";break;case"non_sha256_target_proof_element":t.main="The proof is corrupted",t.sub="An attribute in the proof (parent or left or right) in not a SHA256 hash";break;case"invalid_parent_in_proof_element":t.main="The proof is corrupted",t.sub="A parent in the proof is not the SHA256 of its children";break;case"invalid_receipt_format":t.main="The proof is corrupted",t.sub="The proof receipt does not conform to the Chainpoint 1 or 2 format";break;case"invalid_target_proof":t.main="The proof is corrupted",t.sub="The proof receipt's merkle proof attribute is invalid";break;case"op_return_mismatches_merkle_root":t.main="The receipt is corrupted",t.sub="The transaction's OP_RETURN mismatches the proof receipt's merkle root";break;case"http_error":t.main="Unexpected HTTP error",t.sub="An unexpected HTTP error occurred during the verification process";break;case"tx_not_found":t.main="The transaction cannot be found",t.sub="The proof receipt's transaction doesn't exist in the blockchain";break;case"tx_not_confirmed":t.main="The transaction is not confirmed",t.sub="The proof receipt's transaction is not yet confirmed by the blockchain";break;case"invalid_receipt_signature":t.main="The signature is invalid",t.sub="The proof receipt's signature is not valid";break;case"invalid_receipt_signature_format":t.main="The signature is corrupted",t.sub="The proof receipt does not conform to the Chainpoint 1 or 2 format with signature extension";break;case"file_too_big_to_be_hashed_without_worker":t.main="Cannot hash without worker",t.sub="The file is too big to be hashed without worker";break;default:console.trace("unexpected case",e),t.main=e,t.sub="Unexpected case"}return t}(p);i.mainTextZone.text(m.main),i.subTextZone.text(m.sub),i.byTextZone.hide(),i.warnTextZone.hide(),i.addClass("file_matched_but_anchor_not_yet_processed"!==p?"error":"info")}a.x.reset.show()}function g(e,t){switch(e){case"ok":p(),v("ok",t,0),u.toDom();break;case"need-receipt":p(),u.hide(),h.show(),c.hide(),a.x.cancel.hide(),a.x.receipt.hide(),h.inputContainer.mainTextZone.text("No public proof found at Woleet"),h.inputContainer.subTextZone.text("Please provide a proof receipt"),a.x.reset.show();break;case"error":p(),v("error",t,0),u.toDom();break;case"hashing":p(),a.x.cancel.show(),a.x.receipt.hide(),u.hide(),h.hide(),c.show();break;case"pending":p(),u.hide(),h.show(),c.hide(),a.x.cancel.hide(),a.x.receipt.hide(),h.inputContainer.mainTextZone.text(t),a.x.reset.show();break;case"init":default:f()}}var y=!1,T=null;function w(){var e=u.items;y?(s.removeClass("expanded"),e.forEach(function(e){return e.removeClass("clickable")}),l.expand.text(e.length+"+"),l.expand.removeClass("up-arrow"),y=!1):(s.addClass("expanded"),e.forEach(function(e){return e.addClass("clickable")}),l.expand.text(""),l.expand.addClass("up-arrow"),y=!0)}function _(){g("init"),m.cancel(),t.hash=null,t.state="initial"}function k(){_()}this.setInputFile=function(e){if("needReceipt"===t.state)throw new Error("Current state is needReceipt");var n={files:[e]};return x.call(n)},this.setReceipt=function(e){if(b(),"needReceipt"!==t.state)throw new Error("Current state must be needReceipt");var n={files:[e]};return x.call(n)},this.reset=_,this.cancelHash=k,this.toDom=function(){return o.toDom()}}document.addEventListener("DOMContentLoaded",function(){for(var e=document.getElementsByClassName("woleet-widget"),n=0;n<e.length;n++){var r=e[n],i=new t(r.getAttribute("data-hash"));r.appendChild(i.toDom())}}),e.Widget=t}(window); //# sourceMappingURL=woleet-widget.min.js.map