@metamask/notification-example-snap
Version:
MetaMask example snap demonstrating the use of `snap_notify`
1 lines • 16.7 kB
JavaScript
(()=>{var e={12:e=>{e.exports=i,i.default=i,i.stable=d,i.stableStringify=d;var t="[...]",r="[Circular]",n=[],o=[];function s(){return{depthLimit:Number.MAX_SAFE_INTEGER,edgesLimit:Number.MAX_SAFE_INTEGER}}function i(e,t,r,i){var a;void 0===i&&(i=s()),c(e,"",0,[],void 0,0,i);try{a=0===o.length?JSON.stringify(e,t,r):JSON.stringify(e,l(t),r)}catch(e){return JSON.stringify("[unable to serialize, circular reference is too complex to analyze]")}finally{for(;0!==n.length;){var u=n.pop();4===u.length?Object.defineProperty(u[0],u[1],u[3]):u[0][u[1]]=u[2]}}return a}function a(e,t,r,s){var i=Object.getOwnPropertyDescriptor(s,r);void 0!==i.get?i.configurable?(Object.defineProperty(s,r,{value:e}),n.push([s,r,t,i])):o.push([t,r,e]):(s[r]=e,n.push([s,r,t]))}function c(e,n,o,s,i,u,d){var f;if(u+=1,"object"==typeof e&&null!==e){for(f=0;f<s.length;f++)if(s[f]===e)return void a(r,e,n,i);if(void 0!==d.depthLimit&&u>d.depthLimit)return void a(t,e,n,i);if(void 0!==d.edgesLimit&&o+1>d.edgesLimit)return void a(t,e,n,i);if(s.push(e),Array.isArray(e))for(f=0;f<e.length;f++)c(e[f],f,f,s,e,u,d);else{var l=Object.keys(e);for(f=0;f<l.length;f++){var p=l[f];c(e[p],p,f,s,e,u,d)}}s.pop()}}function u(e,t){return e<t?-1:e>t?1:0}function d(e,t,r,i){void 0===i&&(i=s());var a,c=f(e,"",0,[],void 0,0,i)||e;try{a=0===o.length?JSON.stringify(c,t,r):JSON.stringify(c,l(t),r)}catch(e){return JSON.stringify("[unable to serialize, circular reference is too complex to analyze]")}finally{for(;0!==n.length;){var u=n.pop();4===u.length?Object.defineProperty(u[0],u[1],u[3]):u[0][u[1]]=u[2]}}return a}function f(e,o,s,i,c,d,l){var p;if(d+=1,"object"==typeof e&&null!==e){for(p=0;p<i.length;p++)if(i[p]===e)return void a(r,e,o,c);try{if("function"==typeof e.toJSON)return}catch(e){return}if(void 0!==l.depthLimit&&d>l.depthLimit)return void a(t,e,o,c);if(void 0!==l.edgesLimit&&s+1>l.edgesLimit)return void a(t,e,o,c);if(i.push(e),Array.isArray(e))for(p=0;p<e.length;p++)f(e[p],p,p,i,e,d,l);else{var h={},m=Object.keys(e).sort(u);for(p=0;p<m.length;p++){var y=m[p];f(e[y],y,p,i,e,d,l),h[y]=e[y]}if(void 0===c)return h;n.push([c,o,e]),c[o]=h}i.pop()}}function l(e){return e=void 0!==e?e:function(e,t){return t},function(t,r){if(o.length>0)for(var n=0;n<o.length;n++){var s=o[n];if(s[1]===t&&s[0]===r){r=s[2],o.splice(n,1);break}}return e.call(this,t,r)}}}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var s=t[n]={exports:{}};return e[n](s,s.exports,r),s.exports}r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var n={};(()=>{"use strict";function e(e,t,r){if("string"==typeof e)throw new Error(`An HTML element ("${String(e)}") was used in a Snap component, which is not supported by Snaps UI. Please use one of the supported Snap components.`);if(!e)throw new Error("A JSX fragment was used in a Snap component, which is not supported by Snaps UI. Please use one of the supported Snap components.");return e({...t,key:r})}var t;function o(e){return Boolean(e)&&"object"==typeof e&&!Array.isArray(e)}r.r(n),r.d(n,{onRpcRequest:()=>$e}),function(e){e.InApp="inApp",e.Native="native"}(t||(t={}));const s=(e,t)=>Object.hasOwnProperty.call(e,t);var i;!function(e){e[e.Null=4]="Null",e[e.Comma=1]="Comma",e[e.Wrapper=1]="Wrapper",e[e.True=4]="True",e[e.False=5]="False",e[e.Quote=1]="Quote",e[e.Colon=1]="Colon",e[e.Date=24]="Date"}(i=i||(i={}));var a=r(12);class c extends TypeError{constructor(e,t){let r;const{message:n,explanation:o,...s}=e,{path:i}=e,a=0===i.length?n:`At path: ${i.join(".")} -- ${n}`;super(o??a),null!=o&&(this.cause=a),Object.assign(this,s),this.name=this.constructor.name,this.failures=()=>r??(r=[e,...t()])}}function u(e){return"object"==typeof e&&null!==e}function d(e){return"symbol"==typeof e?e.toString():"string"==typeof e?JSON.stringify(e):`${e}`}function f(e,t,r,n){if(!0===e)return;!1===e?e={}:"string"==typeof e&&(e={message:e});const{path:o,branch:s}=t,{type:i}=r,{refinement:a,message:c=`Expected a value of type \`${i}\`${a?` with refinement \`${a}\``:""}, but received: \`${d(n)}\``}=e;return{value:n,type:i,refinement:a,key:o[o.length-1],path:o,branch:s,...e,message:c}}function*l(e,t,r,n){(function(e){return u(e)&&"function"==typeof e[Symbol.iterator]})(e)||(e=[e]);for(const o of e){const e=f(o,t,r,n);e&&(yield e)}}function*p(e,t,r={}){const{path:n=[],branch:o=[e],coerce:s=!1,mask:i=!1}=r,a={path:n,branch:o};if(s&&(e=t.coercer(e,a),i&&"type"!==t.type&&u(t.schema)&&u(e)&&!Array.isArray(e)))for(const r in e)void 0===t.schema[r]&&delete e[r];let c="valid";for(const n of t.validator(e,a))n.explanation=r.message,c="not_valid",yield[n,void 0];for(let[d,f,l]of t.entries(e,a)){const t=p(f,l,{path:void 0===d?n:[...n,d],branch:void 0===d?o:[...o,f],coerce:s,mask:i,message:r.message});for(const r of t)r[0]?(c=null===r[0].refinement||void 0===r[0].refinement?"not_valid":"not_refined",yield[r[0],void 0]):s&&(f=r[1],void 0===d?e=f:e instanceof Map?e.set(d,f):e instanceof Set?e.add(f):u(e)&&(void 0!==f||d in e)&&(e[d]=f))}if("not_valid"!==c)for(const n of t.refiner(e,a))n.explanation=r.message,c="not_refined",yield[n,void 0];"valid"===c&&(yield[void 0,e])}class h{constructor(e){const{type:t,schema:r,validator:n,refiner:o,coercer:s=(e=>e),entries:i=function*(){}}=e;this.type=t,this.schema=r,this.entries=i,this.coercer=s,this.validator=n?(e,t)=>l(n(e,t),t,this,e):()=>[],this.refiner=o?(e,t)=>l(o(e,t),t,this,e):()=>[]}assert(e,t){return function(e,t,r){const n=v(e,t,{message:r});if(n[0])throw n[0]}(e,this,t)}create(e,t){return m(e,this,t)}is(e){return y(e,this)}mask(e,t){return function(e,t,r){const n=v(e,t,{coerce:!0,mask:!0,message:r});if(n[0])throw n[0];return n[1]}(e,this,t)}validate(e,t={}){return v(e,this,t)}}function m(e,t,r){const n=v(e,t,{coerce:!0,message:r});if(n[0])throw n[0];return n[1]}function y(e,t){return!v(e,t)[0]}function v(e,t,r={}){const n=p(e,t,r),o=function(e){const{done:t,value:r}=e.next();return t?void 0:r}(n);if(o[0]){return[new c(o[0],(function*(){for(const e of n)e[0]&&(yield e[0])})),void 0]}return[void 0,o[1]]}function g(e,t){return new h({type:e,schema:null,validator:t})}function b(){return g("never",(()=>!1))}function w(e){const t=e?Object.keys(e):[],r=b();return new h({type:"object",schema:e??null,*entries(n){if(e&&u(n)){const o=new Set(Object.keys(n));for(const r of t)o.delete(r),yield[r,n[r],e[r]];for(const e of o)yield[e,n[e],r]}},validator:e=>u(e)||`Expected an object, but received: ${d(e)}`,coercer:e=>u(e)?{...e}:e})}function O(e){return new h({...e,validator:(t,r)=>void 0===t||e.validator(t,r),refiner:(t,r)=>void 0===t||e.refiner(t,r)})}function j(){return g("string",(e=>"string"==typeof e||`Expected a string, but received: ${d(e)}`))}function S(e){const t=e.map((e=>e.type)).join(" | ");return new h({type:"union",schema:null,coercer(t){for(const r of e){const[e,n]=r.validate(t,{coerce:!0});if(!e)return n}return t},validator(r,n){const o=[];for(const t of e){const[...e]=p(r,t,n),[s]=e;if(!s?.[0])return[];for(const[t]of e)t&&o.push(t)}return[`Expected the value to satisfy a union of \`${t}\`, but received: ${d(r)}`,...o]}})}function N(e,t,r){return new h({...e,coercer:(n,o)=>y(n,t)?e.coercer(r(n,o),o):e.coercer(n,o)})}function E(e,t,r){return new h({...e,*refiner(n,o){yield*e.refiner(n,o);const s=l(r(n,o),o,e,n);for(const e of s)yield{...e,refinement:t}}})}const k=e=>w(e);function A({path:e,branch:t}){const r=e[e.length-1];return s(t[t.length-2],r)}function P(e){return new h({...e,type:`optional ${e.type}`,validator:(t,r)=>!A(r)||e.validator(t,r),refiner:(t,r)=>!A(r)||e.refiner(t,r)})}function x(e){if(null===e||"boolean"==typeof e||"string"==typeof e)return!0;if("number"==typeof e&&Number.isFinite(e))return!0;if("object"==typeof e){let t=!0;if(Array.isArray(e)){for(let r=0;r<e.length;r++)if(!x(e[r])){t=!1;break}return t}const r=Object.entries(e);for(let e=0;e<r.length;e++)if("string"!=typeof r[e][0]||!x(r[e][1])){t=!1;break}return t}return!1}const I=g("JSON",(e=>x(e))),J=N(I,E(g("any",(()=>!0)),"JSON",(e=>y(e,I))),(e=>JSON.parse(JSON.stringify(e,((e,t)=>{if("__proto__"!==e&&"constructor"!==e)return t})))));function R(e){try{return function(e){m(e,J)}(e),!0}catch{return!1}}const T=function(e){const t=d(e),r=typeof e;return new h({type:"literal",schema:"string"===r||"number"===r||"boolean"===r?e:null,validator:r=>r===e||`Expected the literal \`${t}\`, but received: ${d(r)}`})}("2.0"),_=(M=S([g("number",(e=>"number"==typeof e&&!isNaN(e)||`Expected a number, but received: ${d(e)}`)),j()]),new h({...M,validator:(e,t)=>null===e||M.validator(e,t),refiner:(e,t)=>null===e||M.refiner(e,t)}));var M;const $=k({code:g("integer",(e=>"number"==typeof e&&!isNaN(e)&&Number.isInteger(e)||`Expected an integer, but received: ${d(e)}`)),message:j(),data:P(J),stack:P(j())}),q=S([(L=j(),z=J,new h({type:"record",schema:null,*entries(e){if(u(e))for(const t in e){const r=e[t];yield[t,t,L],yield[t,r,z]}},validator:e=>u(e)||`Expected an object, but received: ${d(e)}`})),(C=J,new h({type:"array",schema:C,*entries(e){if(C&&Array.isArray(e))for(const[t,r]of e.entries())yield[t,r,C]},coercer:e=>Array.isArray(e)?e.slice():e,validator:e=>Array.isArray(e)||`Expected an array value, but received: ${d(e)}`}))]);var C,L,z;k({id:_,jsonrpc:T,method:j(),params:P(q)}),k({jsonrpc:T,method:j(),params:P(q)});w({id:_,jsonrpc:T,result:O(g("unknown",(()=>!0))),error:O($)});const F=k({id:_,jsonrpc:T,result:J}),U=k({id:_,jsonrpc:T,error:$});S([F,U]);const W={invalidInput:-32e3,resourceNotFound:-32001,resourceUnavailable:-32002,transactionRejected:-32003,methodNotSupported:-32004,limitExceeded:-32005,parse:-32700,invalidRequest:-32600,methodNotFound:-32601,invalidParams:-32602,internal:-32603},D={userRejectedRequest:4001,unauthorized:4100,unsupportedMethod:4200,disconnected:4900,chainDisconnected:4901},H={"-32700":{standard:"JSON RPC 2.0",message:"Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text."},"-32600":{standard:"JSON RPC 2.0",message:"The JSON sent is not a valid Request object."},"-32601":{standard:"JSON RPC 2.0",message:"The method does not exist / is not available."},"-32602":{standard:"JSON RPC 2.0",message:"Invalid method parameter(s)."},"-32603":{standard:"JSON RPC 2.0",message:"Internal JSON-RPC error."},"-32000":{standard:"EIP-1474",message:"Invalid input."},"-32001":{standard:"EIP-1474",message:"Resource not found."},"-32002":{standard:"EIP-1474",message:"Resource unavailable."},"-32003":{standard:"EIP-1474",message:"Transaction rejected."},"-32004":{standard:"EIP-1474",message:"Method not supported."},"-32005":{standard:"EIP-1474",message:"Request limit exceeded."},4001:{standard:"EIP-1193",message:"User rejected the request."},4100:{standard:"EIP-1193",message:"The requested account and/or method has not been authorized by the user."},4200:{standard:"EIP-1193",message:"The requested method is not supported by this Ethereum provider."},4900:{standard:"EIP-1193",message:"The provider is disconnected from all chains."},4901:{standard:"EIP-1193",message:"The provider is disconnected from the specified chain."}},G=W.internal,X="Unspecified error message. This is a bug, please report it.",B=(Q(G),"Unspecified server error.");function Q(e,t=X){if(function(e){return Number.isInteger(e)}(e)){const t=e.toString();if(s(H,t))return H[t].message;if(function(e){return e>=-32099&&e<=-32e3}(e))return B}return t}function K(e){return Array.isArray(e)?e.map((e=>R(e)?e:o(e)?V(e):null)):o(e)?V(e):R(e)?e:null}function V(e){return Object.getOwnPropertyNames(e).reduce(((t,r)=>{const n=e[r];return R(n)&&(t[r]=n),t}),{})}const Y=function(e){return e?.__esModule?e.default:e}(a);class Z extends Error{constructor(e,t,r){if(!Number.isInteger(e))throw new Error('"code" must be an integer.');if(!t||"string"!=typeof t)throw new Error('"message" must be a non-empty string.');!function(e){return o(e)&&s(e,"cause")&&o(e.cause)}(r)?super(t):(super(t,{cause:r.cause}),s(this,"cause")||Object.assign(this,{cause:r.cause})),void 0!==r&&(this.data=r),this.code=e}serialize(){const e={code:this.code,message:this.message};return void 0!==this.data&&(e.data=this.data,function(e){if("object"!=typeof e||null===e)return!1;try{let t=e;for(;null!==Object.getPrototypeOf(t);)t=Object.getPrototypeOf(t);return Object.getPrototypeOf(e)===t}catch(e){return!1}}(this.data)&&(e.data.cause=K(this.data.cause))),this.stack&&(e.stack=this.stack),e}toString(){return Y(this.serialize(),te,2)}}class ee extends Z{constructor(e,t,r){if(!function(e){return Number.isInteger(e)&&e>=1e3&&e<=4999}(e))throw new Error('"code" must be an integer such that: 1000 <= code <= 4999');super(e,t,r)}}function te(e,t){if("[Circular]"!==t)return t}const re=e=>ge(W.parse,e),ne=e=>ge(W.invalidRequest,e),oe=e=>ge(W.invalidParams,e),se=e=>ge(W.methodNotFound,e),ie=e=>ge(W.internal,e),ae=e=>ge(W.invalidInput,e),ce=e=>ge(W.resourceNotFound,e),ue=e=>ge(W.resourceUnavailable,e),de=e=>ge(W.transactionRejected,e),fe=e=>ge(W.methodNotSupported,e),le=e=>ge(W.limitExceeded,e),pe=e=>be(D.userRejectedRequest,e),he=e=>be(D.unauthorized,e),me=e=>be(D.unsupportedMethod,e),ye=e=>be(D.disconnected,e),ve=e=>be(D.chainDisconnected,e);function ge(e,t){const[r,n]=we(t);return new Z(e,r??Q(e),n)}function be(e,t){const[r,n]=we(t);return new ee(e,r??Q(e),n)}function we(e){if(e){if("string"==typeof e)return[e];if("object"==typeof e&&!Array.isArray(e)){const{message:t,data:r}=e;if(t&&"string"!=typeof t)throw new Error("Must specify string message.");return[t??void 0,r]}}return[]}function Oe(e){return o(e)&&s(e,"data")&&"object"==typeof e.data&&null!==e.data&&R(e.data)&&!Array.isArray(e.data)?e.data:{}}var je,Se,Ne,Ee,ke=function(e,t,r,n,o){if("m"===n)throw new TypeError("Private method is not writable");if("a"===n&&!o)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!o:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===n?o.call(e,r):o?o.value=r:t.set(e,r),r},Ae=function(e,t,r,n){if("a"===r&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?n:"a"===r?n.call(e):n?n.value:t.get(e)};class Pe extends Error{constructor(e,t={}){const r=function(e){return o(e)&&s(e,"message")&&"string"==typeof e.message?e.message:String(e)}(e);super(r),je.set(this,void 0),Se.set(this,void 0),Ne.set(this,void 0),Ee.set(this,void 0),ke(this,Se,r,"f"),ke(this,je,function(e){return o(e)&&s(e,"code")&&"number"==typeof e.code&&Number.isInteger(e.code)?e.code:-32603}(e),"f");const n={...Oe(e),...t};Object.keys(n).length>0&&ke(this,Ne,n,"f"),ke(this,Ee,super.stack,"f")}get name(){return"SnapError"}get code(){return Ae(this,je,"f")}get message(){return Ae(this,Se,"f")}get data(){return Ae(this,Ne,"f")}get stack(){return Ae(this,Ee,"f")}toJSON(){return{code:-31002,message:"Snap Error",data:{cause:{code:this.code,message:this.message,stack:this.stack,...this.data?{data:this.data}:{}}}}}serialize(){return this.toJSON()}}function xe(e){return class extends Pe{constructor(t,r){if("object"==typeof t){const r=e();return void super({code:r.code,message:r.message,data:t})}const n=e(t);super({code:n.code,message:n.message,data:r})}}}je=new WeakMap,Se=new WeakMap,Ne=new WeakMap,Ee=new WeakMap;xe(ie),xe(ae),xe(oe),xe(ne),xe(le);const Ie=xe(se);xe(fe),xe(re),xe(ce),xe(ue),xe(de),xe(ve),xe(ye),xe(he),xe(me),xe(pe);function Je(e){return Object.fromEntries(Object.entries(e).filter((([,e])=>void 0!==e)))}function Re(e){return t=>{const{key:r=null,...n}=t;return{type:e,props:Je(n),key:r}}}const Te=Re("Box"),_e=Re("Row"),Me=Re("Address"),$e=async({request:r})=>{switch(r.method){case"inApp":return await snap.request({method:"snap_notify",params:{type:t.InApp,message:"Hello from within MetaMask! [This](https://snaps.metamask.io/) is a what a link looks like."}});case"native":return await snap.request({method:"snap_notify",params:{type:t.Native,message:"Hello from the browser!"}});case"inApp-expanded":return await snap.request({method:"snap_notify",params:{type:t.InApp,message:"Hello from MetaMask, click here for an expanded view!",title:"Hello World!",content:e(Te,{children:e(_e,{label:"From",variant:"warning",tooltip:"This address has been deemed dangerous.",children:e(Me,{address:"0x1234567890123456789012345678901234567890"})})}),footerLink:{text:"Go home",href:"metamask://client/"}}});default:throw new Ie({method:r.method})}}})(),module.exports=n})();