This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb3-input.js
14 lines (14 loc) · 14.6 KB
/
web3-input.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
(()=>{var A=Object.create,m=Object.defineProperty,O=Object.getPrototypeOf,P=Object.prototype.hasOwnProperty,I=Object.getOwnPropertyNames,x=Object.getOwnPropertyDescriptor;var g=Object.assign,N=a=>m(a,"__esModule",{value:!0});var U=(a,e)=>()=>(e||(e={exports:{}},a(e.exports,e)),e.exports);var $=(a,e,s)=>{if(N(a),e&&typeof e=="object"||typeof e=="function")for(let t of I(e))!P.call(a,t)&&t!=="default"&&m(a,t,{get:()=>e[t],enumerable:!(s=x(e,t))||s.enumerable});return a},B=a=>a&&a.__esModule?a:$(m(a!=null?A(O(a)):{},"default",{value:a,enumerable:!0}),a);var C=U((F,v)=>{"use strict";function R({mustBeMetaMask:a=!1,silent:e=!1,timeout:s=3e3}={}){n();let t=!1;return new Promise(i=>{window.ethereum?r():(window.addEventListener("ethereum#initialized",r,{once:!0}),setTimeout(()=>{r()},s));function r(){if(t)return;t=!0,window.removeEventListener("ethereum#initialized",r);let{ethereum:c}=window;if(c&&(!a||c.isMetaMask))i(c);else{let o=a&&c?"Non-MetaMask window.ethereum detected.":"Unable to detect window.ethereum.";!e&&console.error("@metamask/detect-provider:",o),i(null)}}});function n(){if(typeof a!="boolean")throw new Error("@metamask/detect-provider: Expected option 'mustBeMetaMask' to be a boolean.");if(typeof e!="boolean")throw new Error("@metamask/detect-provider: Expected option 'silent' to be a boolean.");if(typeof s!="number")throw new Error("@metamask/detect-provider: Expected option 'timeout' to be a number.")}}v.exports=R});var j=B(C());var D=function(a){let e=arguments,s=this,t,n,i=1,r="",c="",o=[0],l=h=>{i===1&&(h||(r=r.replace(/^\s*\n\s*|\s*\n\s*$/g,"")))?o.push(h?e[h]:r):i===3&&(h||r)?(o[1]=h?e[h]:r,i=2):i===2&&r==="..."&&h?o[2]=Object.assign(o[2]||{},e[h]):i===2&&r&&!h?(o[2]=o[2]||{})[r]=!0:i>=5&&(i===5?((o[2]=o[2]||{})[n]=h?r?r+e[h]:e[h]:r,i=6):(h||r)&&(o[2][n]+=h?r+e[h]:r)),r=""};for(let h=0;h<a.length;h++){h&&(i===1&&l(),l(h));for(let d=0;d<a[h].length;d++)t=a[h][d],i===1?t==="<"?(l(),o=[o,"",null],i=3):r+=t:i===4?r==="--"&&t===">"?(i=1,r=""):r=t+r[0]:c?t===c?c="":r+=t:t==='"'||t==="'"?c=t:t===">"?(l(),i=1):i&&(t==="="?(i=5,n=r,r=""):t==="/"&&(i<5||a[h][d+1]===">")?(l(),i===3&&(o=o[0]),i=o,(o=o[0]).push(s.apply(null,i.slice(1))),i=0):t===" "||t===" "||t===`
`||t==="\r"?(l(),i=2):r+=t),i===3&&r==="!--"&&(i=4,o=o[0])}return l(),o.length>2?o.slice(1):o[1]},u=D.bind((a,e,...s)=>{let t=null,n=!1;["svg","use","symbol","path","g","defs","title"].indexOf(a)>-1?(t=document.createElementNS("http://www.w3.org/2000/svg",a),n=!0):t=document.createElement(a);for(let i in e)if(i.slice(0,2)==="on"){let r=i.slice(2),c=e[i];if(c==null)continue;t.addEventListener(r,c)}else if(["disabled","selected","checked"].indexOf(i)>-1)e[i]&&t.setAttribute(i,i);else if(i==="xlinkHref"&&n)e[i]&&t.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",e[i]);else if(i==="href"&&n)e[i]&&t.setAttributeNS("http://www.w3.org/1999/xlink","href",e[i]);else{if(e[i]==null)continue;t.setAttribute(i,e[i])}return s.length===0?t:(s.flat(1).forEach(i=>{i instanceof Node?t.appendChild(i):t.appendChild(document.createTextNode(i))}),a==null?t.children:t)});function p(a,e){for(;e.firstChild;)e.removeChild(e.firstChild);Array.isArray(a)?a.forEach(s=>e.appendChild(s)):e.appendChild(a)}function b(a){return a.replace(/([A-Z])/g,"-$1").toLowerCase()}function y(a){return(typeof a=="object"||typeof a=="function")&&typeof a.then=="function"}function _(a,e){let s=function(t){if(!t)return[];let n=[];return Object.keys(t).forEach(i=>{(t[i].observed==null||t[i].observed)&&n.push(b(i))}),n}(e.props);globalThis.customElements.get(a)||globalThis.customElements.define(a,class extends globalThis.HTMLElement{static get observedAttributes(){return s}get componentTagName(){return a}connectedCallback(){this.connectedCallbackCount==null&&(this.connectedCallbackCount=0),this.connectedCallbackCount=this.connectedCallbackCount+1,this._checkInit(),this._preprocess()}disconnectedCallback(){typeof this.removed=="function"&&(this.removed(),this.isRemovedCalled=!0)}attributeChangedCallback(){this.connectedCallbackCount!=null&&(this._checkInit(),this._preprocess())}get initialised(){return this._props&&this._state&&this._computed&&this.templateRenderer}_checkInit(){this.initialised||this._init(e)}_init(t){if(this._props=t.props||{},this._computed=t.computed||{},t.state&&typeof t.state!="function")throw new Error("State must be a function!");this._state=t.state||{},this.computedCache={},this.status="render",this.connectedCallbackCount=0,this.props=this._processProps(),typeof this._state=="function"&&(this._state=this._state.bind(this)()),this.state=this._monitorState(this._state),this.root=this._processRoot(t.root),this.slots=this._processSlots(),this.render=t.render||null,this.templateRenderer=t.renderer,this.template=null,this.created=t.created||null,this.mounted=t.mounted||null,this.updated=t.updated||null,this.removed=t.removed||null,this.isCreatedCalled=!1,this.isMountedCalled=!1,this.isRemovedCalled=!1,this.emit=(n,i)=>{(function(r,c,o={}){let l=Object.assign({},{bubbles:!0,cancelable:!0,composed:!1},o),h;"composed"in CustomEvent.prototype?h=new CustomEvent(c,l):(h=document.createEvent("CustomEvent"),h.initCustomEvent(c,l.bubbles,l.cancelable,l.detail),Object.defineProperty(h,"composed",{value:l.composed})),r.dispatchEvent(h)})(this,n,{detail:i})},this.setState=(n,i)=>{let r=o=>{if(!o||typeof o!="object")return;let l=this.updated;i&&(this.updated=()=>{setTimeout(i),this.updated=l||void 0}),this.status="transaction";for(let h in o)this.state[h]&&this.state[h]===o[h]||(this.state[h]=o[h]);this.status="render",this._processRender()},c=n(this.state);y(c)?c.then(r):r(c)},this._processMethodsAndComputedProps(t),this._processInstanceProps(this._props),typeof this.created!="function"||this.isCreatedCalled||(this.created(),this.isCreatedCalled=!0)}_processProps(){let t={};return Object.keys(this._props).forEach(n=>{let i={},r=this._props[n],c=this._getAttribute(n),o=null;if(r.default!=null&&(o=r.default),r.required&&c==null)o!=null?(console.info(`No biggie, the required prop '${n}' has no value set, so the default has been set`),i[n]=o):(i[n]=null,console.error(`The required prop '${n}' has no value set`));else switch(r.type){case String:default:i[n]=c||o;break;case Number:i[n]=c!=null?parseFloat(c):o!=null?o:0;break;case Boolean:i[n]=c!=null?c.toString()==="true":o!=null&&o;break;case Object:try{i[n]=c!=null?JSON.parse(c):o!=null?o:void 0}catch(l){i[n]=o!=null?o:void 0,console.error("An object parse issue occurred",l)}}t[n]=i[n],this._instanceProps&&this._instanceProps[n]&&(t[n]=this._instanceProps[n])}),t}_processMethodsAndComputedProps(t){let n=this,i=Object.keys(t);i.length&&i.forEach(r=>{n[r]||typeof t[r]!="function"||(n[r]=t[r].bind(n)),r==="computed"&&this._processComputed(t[r])})}_processRoot(t){switch(t){case"standard":default:return this;case"shadow":return this.attachShadow({mode:"open"});case"shadow:closed":return this.attachShadow({mode:"closed"})}}_processComputed(t){let n=this,i=Object.keys(t);i.length&&i.forEach(r=>{n[r]?console.warn(`Computed property '${r}' already exists on the component instance`):Object.defineProperty(n,r,{get:()=>(n.computedCache[r]||(n.computedCache[r]=t[r].bind(n)()),n.computedCache[r])})})}_processRender(){let t=this.render?this.render():void 0;t&&(this.template=t,this._updateRender())}_monitorState(t){let n=this;return new Proxy(t,{set:(i,r,c)=>(i[r]===c||(i[r]=c,n.computedCache={},n.status==="render"&&n._processRender()),!0)})}_processSlots(){let t=this.childNodes,n={default:[]};return t.length>0&&[...t].forEach(i=>{let r=i.getAttribute?i.getAttribute("slot"):null;r?n[r]=i:n.default.push(i)}),n}_getAttribute(t){try{return this.getAttribute(b(t))}catch(n){return console.error("A get prop error occurred",n),""}}_processInstanceProps(t){let n=this,i=Object.keys(t);t&&i.forEach(r=>{let c;n[r]&&(c=n[r],delete n[r]),Object.defineProperty(n,r,{get(){return this._instanceProps&&this._instanceProps[r]?this._instanceProps[r]:this.getAttribute(b(r))},set(o){return this._instanceProps||(this._instanceProps={}),this._instanceProps[r]=o,this.setAttribute(b(r),typeof o=="object"?JSON.stringify(o):o.toString()),!0},enumerable:!0}),c&&(n[r]=c)})}_preprocess(){this.computedCache={},this.props=this._processProps(),this._processRender()}_updateRender(){this.template&&(y(this.template)?this.template.then(t=>{this.templateRenderer(t,this.root),this._callLifecycleMethods()}).catch(t=>console.error("A component render error occurred",t)):(this.templateRenderer(this.template,this.root),this._callLifecycleMethods()))}_callLifecycleMethods(){typeof this.mounted!="function"||this.isMountedCalled||this.mounted(),this.isMountedCalled=!0,typeof this.updated=="function"&&this.isMountedCalled&&this.updated()}})}function f(a,e){return g(g({},e),{created(){this.subscribeCallback=()=>{this.computedCache={},this._processRender()},this.setStore(a),e.created&&e.created.call(this)},mounted(){this._subscribeToStores(!1),e.mounted&&e.mounted.call(this)},updated(){this._subscribeToStores(!1),e.updated&&e.updated.call(this)},removed(){this._unsubscribeFromStores(),e.removed&&e.removed.call(this)},setStore(s){this.store=s,this._subscribeToStores()},_subscribeToStores(s=!0){this.store&&this.store.subscribe&&typeof this.store.subscribe=="function"&&!this.unsubscribe?(this.unsubscribe=this.store.subscribe(this.subscribeCallback),s&&this.subscribeCallback()):this.store&&typeof this.store=="object"&&!this.store.subscribe&&(this.unsubscribe={},Object.keys(this.store).forEach(t=>{this.store[t]&&this.store[t].subscribe&&typeof this.store[t].subscribe=="function"&&!this.unsubscribe[t]&&(this.unsubscribe[t]=this.store[t].subscribe(this.subscribeCallback))}),s&&this.subscribeCallback())},_unsubscribeFromStores(){this.store&&this.unsubscribe&&typeof this.unsubscribe=="object"?(Object.keys(this.unsubscribe).forEach(s=>{this.unsubscribe[s]()}),this.unsubscribe=null):this.store&&this.unsubscribe&&typeof this.unsubscribe=="function"&&(this.unsubscribe(),this.unsubscribe=null)}})}var w=class{constructor(e,s){this.namespace=e,this.storage=s}setState(e){e?(this.storage.setItem(`${this.namespace}:state`,typeof e=="string"?e:JSON.stringify(e)),this.storage.setItem(`${this.namespace}:lastUpdated`,new Date().getTime().toString())):this.removeState()}getState(){let e=this.storage.getItem(`${this.namespace}:state`);return e?JSON.parse(e):void 0}lastUpdated(){let e=this.storage.getItem(`${this.namespace}:lastUpdated`);return e?parseInt(e,10):void 0}removeState(){this.storage.removeItem(`${this.namespace}:state`),this.storage.removeItem(`${this.namespace}:lastUpdated`)}};function M(a,e="session"){return new w(a,e==="local"?globalThis.localStorage:globalThis.sessionStorage)}var T=class{constructor(e){let s=this;s.state={},s.getterCache={},s.status="resting",s.transaction=!1,s.transactionCache={},s.callbacks=[],this._processActions(e);let t=e.initialState||{};if(s.copyOfInitialState=s._copyValue(t),s.ttl=-1,s.lastUpdatedState={},e.ttl&&(s.ttl=e.ttl,Object.keys(s.copyOfInitialState).forEach(n=>s.lastUpdatedState[n]=new Date().getTime())),e.persist){s.persist=typeof e.persist=="string"?M(e.persist):e.persist;let n=s.persist.getState(),i=s.persist.lastUpdated();n&&i&&(s.ttl===-1||s._lastUpdatedTimeDiff(i)<s.ttl)&&(t=n)}this._processState(t)}_processActions(e){let s=this,t=Object.keys(e);t.length&&t.forEach(n=>{s[n]||typeof e[n]!="function"||(s[n]=e[n].bind(s))})}_processState(e){let s=this;s.state=new Proxy(e,{set:(t,n,i)=>(s.transaction&&!s.transactionCache[n]&&(s.transactionCache[n]=s._copyValue(t[n])),t[n]=i,s.lastUpdatedState[n]=new Date().getTime(),s.getterCache={},s.transaction||(s.persist&&s.persist.setState(s.state),s.status="resting",s._processCallbacks(s.state)),!0),get:(t,n)=>s.ttl>-1&&s._lastUpdatedTimeDiff(s.lastUpdatedState[n])>s.ttl?(s.persist&&s.persist.removeState(),s.copyOfInitialState[n]):t[n]})}_lastUpdatedTimeDiff(e){return Math.round((new Date().getTime()-e)/1e3)}setState(e){let s=n=>{if(!n||typeof n!="object")return;let i=this.transaction;i||(this.transactionCache={},this.transaction=!0);for(let r in n)this.state[r]&&this.state[r]===n[r]||(this.state[r]=n[r]);i||(this.transaction=!1,this.persist&&this.persist.setState(this.state),this._processCallbacks(this.state))},t=e(this.state);y(t)?t.then(s):s(t)}getState(e){if(e){if(!this.getterCache[e]){let s=(Array.isArray(e)?e:e.match(/([^[.\]])+/g)).reduce((t,n)=>t&&t[n],this.state);if(s==null)return;this.getterCache[e]=s}return this.getterCache[e]}}_processCallbacks(e){return!!this.callbacks.length&&(this.callbacks.forEach(s=>s(e)),!0)}subscribe(e){if(typeof e!="function")throw new Error("Dude, you can only subscribe to store changes with a valid function");return this.callbacks.push(e),()=>{this.callbacks=this.callbacks.filter(s=>s!==e)}}_copyValue(e){return e&&JSON.parse(JSON.stringify(e))}clear(e=!0){this.getterCache={},this.transactionCache={},this.lastUpdatedState={},this.persist&&this.persist.removeState(),this.transaction=!0,this.status="clear";let s=this._copyValue(this.copyOfInitialState);for(let t in s)this.state[t]=s[t];this.transaction=!1,this.status="resting",e&&this._processCallbacks(this.state)}};function k(a,e){let s=V(a);return s||(s=new T(e),globalThis.__ficusjs__=globalThis.__ficusjs__||{},globalThis.__ficusjs__.store=globalThis.__ficusjs__.store||{},globalThis.__ficusjs__.store[a]=s,s)}function V(a){if(globalThis.__ficusjs__&&globalThis.__ficusjs__.store&&globalThis.__ficusjs__.store[a])return globalThis.__ficusjs__.store[a]}var S=k("web3.sign",{initialState:{account:null,signature:null,web3:{provider:null}},setAccount(a){this.state.account=a},setSignature(a){this.state.signature=a}});_("web3-connect-metamask",f(S,{renderer:p,async handleConnect(){let a=await E(),e=await J(a);this.store.setAccount(e),console.info(`Account connected: ${e}`)},render(){return u`
<button type="button" onclick=${this.handleConnect}>Connect Wallet</button>
`}}));_("web3-sign-button",f(S,{renderer:p,props:{message:{type:String,required:!0}},async handleSign(){let{message:a}=this.props,{account:e}=this.store.state,s=await E(),t=await L(s,e,a);this.store.setSignature(t)},render(){return u`
<button type="button" onclick=${this.handleSign}>Sign</button>
`}}));_("web3-sign-msg",f(S,{renderer:p,props:{message:{type:String,required:!0}},render(){let{account:a,signature:e}=this.store.state,{message:s}=this.props;return u`
<div>
<p>Message: ${s}</p>
<p>Signature: ${e}</p>
${a?"":u`<web3-connect-metamask><//>`}
${a?u`<web3-sign-button message=${s}><//>`:""}
</div>
`}}));async function E(){return await j.default()}async function J(a){if(a)return(await ethereum.request({method:"eth_requestAccounts"}))[0];window.alert("Only works with Metamask")}async function L(a,e,s){return new Promise((t,n)=>{a.sendAsync({method:"personal_sign",params:[s,e],from:e},function(i,r){i&&n(i),t(r.result.substring(2))})})}})();
//# sourceMappingURL=web3-input.js.map