@human.tech/waap-sdk 1.0.2 → 1.0.4-staging.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- import{silkWalletAppOrigin2Staging as e,silkWalletAppOrigin2 as t,rpcMethodRequiresUI as n}from"@human.tech/waap-constants";export{rpcMethodRequiresUI}from"@human.tech/waap-constants";import{EventEmitter as i}from"events";import{SILK_MESSAGE_TYPE as a,SILK_NOTIFICATION as s,generateMessageId as o,SILK_METHOD as r,JSON_RPC_METHOD as l}from"@human.tech/waap-interface-core";export{SILK_METHOD,SILK_METHOD as WAAP_METHOD}from"@human.tech/waap-interface-core";import{EthersAdapter as c}from"@reown/appkit-adapter-ethers";function d(){return d=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)({}).hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e},d.apply(null,arguments)}class h{constructor(n,i){this.iframeWindow=null,this.walletOrigin=void 0,this.iframeWindow=n,this.walletOrigin=i?e:t}postSilkNotification(e){this.iframeWindow.postMessage({target:"silk",messageType:a.notification,data:e},this.walletOrigin)}postReadyNotification(){this.postSilkNotification(s.ready)}postConnectNotification(){this.postSilkNotification(s.connect)}pingIframe(){return new Promise((e,t)=>{setTimeout(()=>t("Wallet ping timed out."),1e4);const n=t=>{"silk"===t.data.target&&t.data.messageType===a.notification&&t.data.data===s.ready&&(window.removeEventListener("message",n),e())};window.addEventListener("message",n),this.postReadyNotification()})}async postSilkRequest(e){const t=d({},e,{target:"silk",messageType:a.request}),n=await o(t);return this.iframeWindow.postMessage(d({},t,{id:n}),this.walletOrigin),n}addListener(e){const{handleResponse:t,handleRequest:n,onReceiveConnectNotif:i,onAccountChanged:o}=e;window.addEventListener("message",e=>{var r,l,c;try{if("undefined"!=typeof process&&process,e.origin!==this.walletOrigin)return}catch(t){if(e.origin!==this.walletOrigin)return}if((null==(r=e.data)?void 0:r.messageType)===a.request)n(e.data);else if((null==(l=e.data)?void 0:l.messageType)===a.response)t(e.data);else if((null==(c=e.data)?void 0:c.messageType)===a.notification){var d,h;(null==(d=e.data)?void 0:d.data)===s.connect?null==i||i():(null==(h=e.data)||null==(h=h.data)?void 0:h.type)===s.account_changed&&o&&o(e.data.data.account)}})}}var u;!function(e){e.show_modal="show_modal",e.hide_modal="hide_modal"}(u||(u={}));class g extends i{constructor(){super()}}async function m(e,t,n,i){const a=e.method,s=e.params,o=await n.postSilkRequest({method:a,params:s,interactionRequired:t});return new Promise((e,t)=>{i.once(o,n=>{n.error?t(n.error):e(n.data)})})}const p={styles:{darkMode:!1}};class w{static setProjectId(e){this.projectId=e}static getProjectIdFromEnv(){try{if("undefined"!=typeof process&&process.env)return process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID||process.env.VITE_WALLET_CONNECT_PROJECT_ID||process.env.REACT_APP_WALLET_CONNECT_PROJECT_ID||process.env.WALLET_CONNECT_PROJECT_ID}catch(e){return}}static async getInstance(){if("undefined"==typeof window||"undefined"==typeof document)return null;const{createAppKit:e}=await import("@reown/appkit"),{mainnet:t,sepolia:n,optimism:i,arbitrum:a,polygon:s,gnosis:o,avalanche:r,aurora:l,fantom:d,base:h,celo:u}=await import("@reown/appkit/networks");try{const g={name:"Human Wallet",description:"Silk lets you create human keys for web3 wallets that are recoverable with private identity proofs and secured by zero trust protocols.",url:window.location.origin,icons:["https://imagedelivery.net/_aTEfDRm7z3tKgu9JhfeKA/f11f5753-616a-4aa0-2aee-9b75befea700/sm"]},m=this.projectId||this.getProjectIdFromEnv();return m?(this.projectId=m,this.instance||(this.instance=e({adapters:[new c],projectId:m,metadata:g,networks:[t,n,a,i,s,o,r,l,d,h,u],features:{socials:!1,email:!1}})),this.instance):(console.error('WalletConnect project ID not found. Please set it using one of these methods:\n1. Pass walletConnectProjectId when calling initSilk\n2. Call SilkWalletConnect.setProjectId("your-project-id")\n3. Set WALLET_CONNECT_PROJECT_ID (NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID for Next projects) environment variable'),null)}catch(e){return console.error("Error importing or initializing WalletConnect:",e),null}}static async listenToAccountChangeEvent(){await new Promise(e=>{this.instance.subscribeAccount(t=>{null!=t&&t.isConnected&&e()})})}static async waitForReadyAndCheckConnection(){if(!this.instance)return!1;let e=!1,t=0;for(;!e&&t<15;){const n=t<5?500:1e3;await new Promise(e=>setTimeout(e,n)),e=this.instance.getIsConnectedState(),t++,console.log(`WalletConnect connection check attempt ${t}: ${e}`)}return e}static isConnected(){var e,t;return null!=(e=null==(t=this.instance)?void 0:t.getIsConnectedState())&&e}static getWalletProvider(){var e,t;return null!=(e=null==(t=this.instance)?void 0:t.getWalletProvider())?e:null}}w.instance=null,w.projectId=void 0,w.chains=[1,10,100,137,8453,43114,42161,42220,84532,11155111];class f extends i{constructor(e,t,n,a,s,o){super(),this.isSilk=!0,this.isWaaP=!0,this.connected=!1,this.walletMessageManager=void 0,this.uiMessageManager=void 0,this.config=void 0,this.currentAccount=null,this.walletConnectProvider=null,this.persistedMethods=void 0,this.internalEventEmitter=new i,this.uiMessageManager=new g,this.walletMessageManager=new h(e,!!s),this.config=d({},p,n||{}),this.persistedMethods={login:this.login.bind(this),logout:this.logout.bind(this),getLoginMethod:this.getLoginMethod.bind(this),walletMessageManager:this.walletMessageManager,internalEventEmitter:this.internalEventEmitter,uiMessageManager:this.uiMessageManager,portal:this.portal.bind(this),requestEmail:this.requestEmail.bind(this),requestSBT:this.requestSBT.bind(this),orcid:this.orcid.bind(this),enable:this.enable.bind(this),isConnected:this.isConnected.bind(this),toggleDarkMode:this.toggleDarkMode.bind(this),silkRequest:this.request.bind(this),waaPRequest:this.request.bind(this)},this.walletMessageManager.addListener({onReceiveConnectNotif:()=>{console.log("onReceiveConnectNotif"),this.connected=!0,this.emit("connect")},handleResponse:e=>this.internalEventEmitter.emit(e.id,e),handleRequest:e=>{e.method===r.show_modal?this.uiMessageManager.emit(u.show_modal):e.method===r.hide_modal&&this.uiMessageManager.emit(u.hide_modal)},onAccountChanged:e=>{this.currentAccount!==e&&(this.currentAccount=e,this.emit("accountsChanged",e?[e]:[]))}}),t&&this.walletMessageManager.pingIframe().then(()=>m({method:r.set_points_referral_code,params:[t]},!1,this.walletMessageManager,this.internalEventEmitter)).catch(e=>{console.error("Error setting referral code in Silk iframe:",e)}),a&&this.walletMessageManager.pingIframe().then(()=>m({method:r.set_project,params:[a,window.location.origin]},!1,this.walletMessageManager,this.internalEventEmitter)),n&&this.walletMessageManager.pingIframe().then(()=>m({method:r.set_custom_config,params:[n]},!1,this.walletMessageManager,this.internalEventEmitter)).catch(e=>{console.error("Error setting custom config in Silk iframe:",e)}),o&&this.walletMessageManager.pingIframe().then(()=>m({method:r.force_iframe,params:[!0]},!1,this.walletMessageManager,this.internalEventEmitter)).catch(e=>{console.error("Error forcing iframe in Silk iframe:",e)})}async initializeWalletConnect(){try{this.walletConnectProvider=await w.getInstance(),console.log("WalletConnect provider initialized")}catch(e){console.error("Failed to initialize WalletConnect provider:",e),this.walletConnectProvider=null}}async enable(){return this.request({method:"eth_requestAccounts"})}isConnected(){return this.connected}async request(e){if(!Object.keys(l).includes(e.method))return Promise.reject({error:`Not implemented (${e.method})`});const t=await this.attemptAutoConnect(e.method);if(null!==t)return t;if(function(e){try{var t;if(!e||!Array.isArray(e))return!1;const n=JSON.parse("string"==typeof e[1]?e[1]:JSON.stringify(e[1]));return"SafeTx"===n.primaryType&&void 0!==(null==(t=n.domain)?void 0:t.verifyingContract)&&n.message&&"to"in n.message&&"value"in n.message&&"safeTxGas"in n.message&&"baseGas"in n.message&&"gasPrice"in n.message&&"nonce"in n.message}catch(e){return!1}}(e.params)&&e.method!==l.eth_signTypedData_v4)return Promise.reject({error:"Safe transactions must be signed using eth_signTypedData_v4"});await this.walletMessageManager.pingIframe();const i=await m(e,n(e.method),this.walletMessageManager,this.internalEventEmitter);if((e.method===l.wallet_switchEthereumChain||e.method===l.wallet_addEthereumChain)&&null===i){var a;this.emit("chainChanged",null==(a=e.params)||null==(a=a[0])?void 0:a.chainId);try{const t=await this.request({method:"eth_accounts"});var s,o;t&&t.length>0&&(this.currentAccount=t[0],this.emit("accountsChanged",t),this.emit("connect",{chainId:null==(s=e.params)||null==(s=s[0])?void 0:s.chainId}),console.log("[EthereumProvider] Re-emitted accounts and connect after chain switch:",{accounts:t,chainId:null==(o=e.params)||null==(o=o[0])?void 0:o.chainId}))}catch(t){var r;console.warn("[EthereumProvider] Failed to get accounts after chain switch:",t),this.currentAccount&&(this.emit("accountsChanged",[this.currentAccount]),this.emit("connect",{chainId:null==(r=e.params)||null==(r=r[0])?void 0:r.chainId}))}}return i}async login(e){var t;if("undefined"==typeof window||"undefined"==typeof document)return null;null!=(t=this.config.authenticationMethods)&&t.includes("wallet")&&!this.walletConnectProvider&&await this.initializeWalletConnect();const n=e||window.ethereum,i=n&&function(e){return!(null==e||!e.isMetaMask||e.isApexWallet||e.isAvalanche||e.isBitKeep||e.isBlockWallet||e.isMathWallet||e.isOkxWallet||e.isOKExWallet||e.isOneInchIOSWallet||e.isOneInchAndroidWallet||e.isOpera||e.isPortal||e.isRabby||e.isDefiant||e.isTokenPocket||e.isTokenary||e.isZerion)}(n)?"MetaMask":"Browser Wallet";await this.walletMessageManager.pingIframe();const a=await m({method:r.login,params:[i]},!0,this.walletMessageManager,this.internalEventEmitter);if("string"==typeof a&&a.includes("waap")&&(localStorage.setItem(`${window.location.origin}-connected`,"waap"),this.setupWaaP()),"string"==typeof a&&a.includes("injected")&&n&&(localStorage.setItem(`${window.location.origin}-connected`,"injected"),this.setupInjectedWallet(n)),"string"==typeof a&&a.includes("walletconnect")){localStorage.setItem(`${window.location.origin}-connected`,"walletconnect"),w.isConnected()||(await this.walletConnectProvider.open({view:"Connect"}),await w.listenToAccountChangeEvent());const e=w.getWalletProvider();this.setupWalletConnect(e)}return a}async logout(){await this.walletMessageManager.pingIframe(),this.cleanupInvalidLoginState();try{this.walletConnectProvider&&await this.walletConnectProvider.disconnect()}catch(e){console.error("Error disconnecting from WalletConnect:",e)}return m({method:r.logout},!1,this.walletMessageManager,this.internalEventEmitter)}cleanupInvalidLoginState(){try{localStorage.removeItem(`${window.location.origin}-connected`)}catch(e){console.warn("Could not clean up login state:",e)}}setupWaaP(){const e=d({},this.persistedMethods,{isWaaP:!0,request:this.request.bind(this)});window.silk=e,window.waap=e}setupInjectedWallet(e){var t,n,i,a,s;const o=d({},this.persistedMethods,e,{isSilk:!0,connected:null!=(t=e.connected)&&t,on:null!=(n=null==(i=e.on)?void 0:i.bind(e))?n:()=>{},removeListener:null!=(a=null==(s=e.removeListener)?void 0:s.bind(e))?a:()=>{},request:e.request.bind(e)});window.silk=o,window.waap=o}setupWalletConnect(e){var t,n,i,a;const s=d({},this.persistedMethods,e,{isSilk:!0,connected:!0,on:null!=(t=null==(n=e.on)?void 0:n.bind(e))?t:()=>{},removeListener:null!=(i=null==(a=e.removeListener)?void 0:a.bind(e))?i:()=>{},request:e.request.bind(e)});window.silk=s,window.waap=s}setupAutoConnectInjected(e){var t,n,i,a,s;const o=d({},this.persistedMethods,e,{isSilk:!1,connected:null!=(t=null==e.isConnected?void 0:e.isConnected())&&t,on:null!=(n=null==(i=e.on)?void 0:i.bind(e))?n:()=>{},removeListener:null!=(a=null==(s=e.removeListener)?void 0:s.bind(e))?a:()=>{},request:e.request.bind(e)});window.silk=o,window.waap=o}setupAutoConnectWalletConnect(e){var t,n,i,a;const s=d({},this.persistedMethods,e,{isSilk:!1,connected:!0,on:null!=(t=null==(n=e.on)?void 0:n.bind(e))?t:()=>{},removeListener:null!=(i=null==(a=e.removeListener)?void 0:a.bind(e))?i:()=>{},request:e.request.bind(e)});window.silk=s,window.waap=s}async attemptAutoConnect(e){var t=this;if("eth_requestAccounts"!==e&&"eth_accounts"!==e)return null;this.walletMessageManager.pingIframe().then(async function(){await m({method:r.auto_conn,params:[window.location.origin]},!1,t.walletMessageManager,t.internalEventEmitter)});const n=this.getLoginMethod();if("injected"===n&&window.ethereum)try{return this.setupAutoConnectInjected(window.ethereum),window.ethereum.request({method:"eth_requestAccounts"})}catch(e){return console.warn("Injected wallet auto-connect failed:",e),this.cleanupInvalidLoginState(),null}else if("walletconnect"===n)try{if(this.walletConnectProvider||(this.walletConnectProvider=await w.getInstance()),await w.waitForReadyAndCheckConnection()){const e=w.getWalletProvider();return this.setupAutoConnectWalletConnect(e),e.request({method:"eth_requestAccounts"})}return console.log("WalletConnect not connected, cleaning up and falling through to normal flow"),this.cleanupInvalidLoginState(),null}catch(e){return console.warn("WalletConnect auto-connect failed:",e),this.cleanupInvalidLoginState(),null}return null}getLoginMethod(){if("undefined"==typeof window||"undefined"==typeof localStorage)return null;try{const e=localStorage.getItem(`${window.location.origin}-connected`);if(!e)return null;if(!["waap","human","injected","walletconnect"].includes(e))return this.cleanupInvalidLoginState(),null;switch(e){case"injected":return window.ethereum?"injected":(this.cleanupInvalidLoginState(),null);case"walletconnect":try{return w.isConnected()?"walletconnect":(this.cleanupInvalidLoginState(),null)}catch(e){return this.cleanupInvalidLoginState(),null}case"waap":return"waap";case"human":return"human";default:return this.cleanupInvalidLoginState(),null}}catch(e){return console.warn("Error checking login method:",e),this.cleanupInvalidLoginState(),null}}async toggleDarkMode(){this.config.styles.darkMode=!this.config.styles.darkMode,this.walletMessageManager.pingIframe().then(()=>m({method:r.set_custom_config,params:[this.config]},!1,this.walletMessageManager,this.internalEventEmitter)).catch(e=>{console.error("Error setting custom config in Silk iframe:",e)})}async safe(){this.walletMessageManager.pingIframe().then(()=>m({method:r.safe},!0,this.walletMessageManager,this.internalEventEmitter))}async orcid(){this.walletMessageManager.pingIframe().then(()=>m({method:r.orcid},!0,this.walletMessageManager,this.internalEventEmitter))}async portal(e,t,n){let i;switch(await this.walletMessageManager.pingIframe(),t){case"get":i="gastank"===e?r.gastank_get_settings:r.customization_get;break;case"update":i="gastank"===e?r.gastank_update_settings:r.customization_update;break;case"topup":i=r.gastank_topup;break;case"init":i=r.project_init}return m({method:i,params:[n,window.location.origin]},"get"!==t&&"customization"!==e,this.walletMessageManager,this.internalEventEmitter)}async requestEmail(){return await this.walletMessageManager.pingIframe(),m({method:r.silk_requestEmail},!0,this.walletMessageManager,this.internalEventEmitter)}async requestSBT(e){return await this.walletMessageManager.pingIframe(),m({method:r.silk_requestSbt,params:[e]},!0,this.walletMessageManager,this.internalEventEmitter)}}const y="waap-wallet",v="waap-component-container",M="undefined"!=typeof window&&"undefined"!=typeof document&&"undefined"!=typeof HTMLElement;let E;function C(){return!(!M||!document.getElementById(v)&&!document.querySelector(y))}function I(){return M?document.getElementById(v)||document.querySelector(y):null}M?(E=class extends HTMLElement{constructor(){super(),this.style.display&&""!==this.style.display||(this.style.display="flex",this.style.alignItems="center",this.style.justifyContent="center"),this.style.width&&""!==this.style.width||(this.style.width="100%"),this.style.height&&""!==this.style.height||(this.style.height="100%"),this.id=v}connectedCallback(){this.setAttribute("data-waap-mounted","true")}disconnectedCallback(){this.removeAttribute("data-waap-mounted")}},"undefined"==typeof customElements||customElements.get(y)||customElements.define(y,E)):E=class{};const _="waap-wallet-iframe-container",k="waap-wallet-iframe-wrapper",b="waap-wallet-iframe",P=()=>document.getElementById(_)||document.getElementById("silk-wallet-iframe-container"),T=()=>document.getElementById(k)||document.getElementById("silk-wallet-iframe-wrapper"),S=()=>document.getElementById(b)||document.getElementById("silk-wallet-iframe"),W=_,L=k,q=b,A=n=>{const i=window.document.createElement("iframe");return i.id=q,i.src=`${n?e:t}/iframe`,console.log("iframe.src: ",i.src),i.style.width="100%",i.style.height="100%",i.style.border="none",i.style.borderRadius="24px",i.style.backgroundColor="transparent",i.style.background="transparent",i.style.padding="0",i.style.margin="0",i},j=(n,i)=>{const a=i=>{if(i.origin!==(n?e:t))return;if(!i.data||"object"!=typeof i.data)return;if("silk-iframe-size"!==i.data.type)return;const a=i.data.height,s=i.data.width;if("number"!=typeof a||"number"!=typeof s)return;if(a<50||a>2e3)return;if(s<50||s>2e3)return;if(!Number.isFinite(a)||!Number.isFinite(s))return;const o=T();o&&(o.style.height=`${Math.floor(a)}px`,o.style.width=`${Math.floor(s)}px`)};window.addEventListener("message",a),i.__silkCleanup=()=>{window.removeEventListener("message",a)}},x=(e={})=>{var t;t=!!e.useStaging,C()?(e=>{const t=I();if(!t)throw new Error("WaaP component not found in DOM");if(S())return;const n=window.document.createElement("div");n.id=L,n.style.position="relative",n.style.display="flex",n.style.alignItems="center",n.style.justifyContent="center",n.style.padding="0",n.style.margin="0",n.style.height="600px",n.style.width="380px";const i=A(e);j(e,i),setTimeout(()=>{const e=T();e&&(e.style.height="600px",e.style.width="380px")},100),n.appendChild(i),t.appendChild(n)})(t):(e=>{if(P())return;const t=window.document.createElement("div");t.id=W,t.style.position="fixed",t.style.top="0",t.style.left="0",t.style.right="0",t.style.bottom="0",t.style.width="100%",t.style.height="100%",t.style.display="flex",t.style.alignItems="center",t.style.justifyContent="center",t.style.backgroundColor="rgba(0, 0, 0, 0.5)",t.style.zIndex="99999",t.style.display="none";const n=window.document.createElement("div");n.id=L,n.style.position="relative",n.style.display="flex",n.style.alignItems="center",n.style.justifyContent="center",n.style.padding="0",n.style.margin="0",n.style.height="600px",n.style.width="380px",t.appendChild(n);const i=A(e);j(e,i),setTimeout(()=>{const e=T();e&&(e.style.height="600px",e.style.width="380px")},100),n.appendChild(i),window.document.body.appendChild(t)})(t);const n=S();if(!n)throw new Error("WaaP iframe does not exist on page");const i=C();P(),I(),T(),e.walletConnectProjectId&&w.setProjectId(e.walletConnectProjectId);const{referralCode:a,config:s,project:o,forceIframe:r}=e,l=new f(n.contentWindow,a,s||{},o||{},e.useStaging,r);let c=i?"component":"modal";return l.uiMessageManager.on(u.show_modal,()=>{const e=C(),t=e?"component":"modal",n=T();if(t!==c&&n)if(c=t,e){const e=I();e&&e.appendChild(n)}else{let e=P();e||(e=window.document.createElement("div"),e.id=W,e.style.position="fixed",e.style.top="0",e.style.left="0",e.style.right="0",e.style.bottom="0",e.style.width="100%",e.style.height="100%",e.style.display="flex",e.style.alignItems="center",e.style.justifyContent="center",e.style.backgroundColor="rgba(0, 0, 0, 0.5)",e.style.zIndex="99999",window.document.body.appendChild(e)),e.appendChild(n)}const i=P();e&&n?n.style.display="flex":i&&(i.style.display="flex")}),l.uiMessageManager.on(u.hide_modal,()=>{const e=C(),t=T(),n=P();e&&t?t.style.display="none":n&&(n.style.display="none")}),window.silk=l,window.waap=l,l};class O extends i{constructor(e){super(),this.isSilk=!0,this.connected=!1,this.config=void 0,this.config=d({},p,e||{})}sendToExtension(e){return new Promise((t,n)=>{window.postMessage({type:"FROM_PAGE",request:e},"*"),window.addEventListener("message",e=>{"FROM_EXTENSION"===e.data.type&&(e.data.error?n(e.data.error):t(e.data.result))})})}async request(e){if(!Object.keys(l).includes(e.method))return Promise.reject({error:`Not implemented (${e.method})`});const t=await this.sendToExtension(e);var n;return e.method!==l.wallet_switchEthereumChain&&e.method!==l.wallet_addEthereumChain||null!==t||this.emit("chainChanged",null==(n=e.params)||null==(n=n[0])?void 0:n.chainId),t}async enable(){return this.request({method:"eth_requestAccounts"})}isConnected(){return this.connected}async login(){return this.sendToExtension({method:r.login})}async requestSBT(e){return this.sendToExtension({method:r.silk_requestSbt,params:[e]})}async toggleDarkMode(){this.config.styles.darkMode=!this.config.styles.darkMode,await this.sendToExtension({method:r.set_custom_config,params:[this.config]})}async safe(){return this.sendToExtension({method:r.safe})}}export{f as EthereumProvider,O as EthereumProviderExtension,w as SilkWalletConnect,g as UIMessageManager,u as UI_EVENT_NAMES,E as WaaP,w as WaaPWalletConnect,h as WalletMessageManager,m as handleWalletRequestAndResponse,x as initWaaP};
1
+ import{silkWalletAppOrigin2Staging as e,silkWalletAppOrigin2 as t,rpcMethodRequiresUI as n}from"@human.tech/waap-constants";export{rpcMethodRequiresUI}from"@human.tech/waap-constants";import{EventEmitter as i}from"events";import{SILK_MESSAGE_TYPE as a,SILK_NOTIFICATION as s,generateMessageId as o,SILK_METHOD as r,JSON_RPC_METHOD as l}from"@human.tech/waap-interface-core";export{SILK_METHOD,SILK_METHOD as WAAP_METHOD}from"@human.tech/waap-interface-core";import{EthersAdapter as c}from"@reown/appkit-adapter-ethers";import{useState as d,useRef as u,useEffect as h,useCallback as g}from"react";function p(){return p=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)({}).hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e},p.apply(null,arguments)}var m;!function(e){e.show_modal="show_modal",e.hide_modal="hide_modal"}(m||(m={}));class w extends i{constructor(){super()}}async function f(e,t,n,i,a){const s=e.method,o=e.params,r=await n.postSilkRequest({method:s,params:o,interactionRequired:t,silkOptions:a});return new Promise((e,t)=>{i.once(r,n=>{n.error?t(n.error):e(n.data)})})}const y={styles:{darkMode:!1}};class v{static setProjectId(e){this.projectId=e}static getProjectIdFromEnv(){try{if("undefined"!=typeof process&&process.env)return process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID||process.env.VITE_WALLET_CONNECT_PROJECT_ID||process.env.REACT_APP_WALLET_CONNECT_PROJECT_ID||process.env.WALLET_CONNECT_PROJECT_ID}catch(e){return}}static async getInstance(){if("undefined"==typeof window||"undefined"==typeof document)return null;const{createAppKit:e}=await import("@reown/appkit"),{mainnet:t,sepolia:n,optimism:i,arbitrum:a,polygon:s,gnosis:o,avalanche:r,aurora:l,fantom:d,base:u,celo:h}=await import("@reown/appkit/networks");try{const g={name:"Human Wallet",description:"Silk lets you create human keys for web3 wallets that are recoverable with private identity proofs and secured by zero trust protocols.",url:window.location.origin,icons:["https://imagedelivery.net/_aTEfDRm7z3tKgu9JhfeKA/f11f5753-616a-4aa0-2aee-9b75befea700/sm"]},p=this.projectId||this.getProjectIdFromEnv();return p?(this.projectId=p,this.instance||(this.instance=e({adapters:[new c],projectId:p,metadata:g,networks:[t,n,a,i,s,o,r,l,d,u,h],features:{socials:!1,email:!1}})),this.instance):(console.error('WalletConnect project ID not found. Please set it using one of these methods:\n1. Pass walletConnectProjectId when calling initSilk\n2. Call SilkWalletConnect.setProjectId("your-project-id")\n3. Set WALLET_CONNECT_PROJECT_ID (NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID for Next projects) environment variable'),null)}catch(e){return console.error("Error importing or initializing WalletConnect:",e),null}}static async listenToAccountChangeEvent(){await new Promise(e=>{this.instance.subscribeAccount(t=>{null!=t&&t.isConnected&&e()})})}static async waitForReadyAndCheckConnection(){if(!this.instance)return!1;let e=!1,t=0;for(;!e&&t<15;){const n=t<5?500:1e3;await new Promise(e=>setTimeout(e,n)),e=this.instance.getIsConnectedState(),t++,console.log(`WalletConnect connection check attempt ${t}: ${e}`)}return e}static isConnected(){var e,t;return null!=(e=null==(t=this.instance)?void 0:t.getIsConnectedState())&&e}static getWalletProvider(){var e,t;return null!=(e=null==(t=this.instance)?void 0:t.getWalletProvider())?e:null}}v.instance=null,v.projectId=void 0,v.chains=[1,10,100,137,8453,43114,42161,42220,84532,11155111];const M=["silkOptions"];class _{constructor(n,i){this.iframeWindow=null,this.walletOrigin=void 0,this.iframeWindow=n,this.walletOrigin=i?e:t}postSilkNotification(e){this.iframeWindow.postMessage({target:"silk",messageType:a.notification,data:e},this.walletOrigin)}postReadyNotification(){this.postSilkNotification(s.ready)}postConnectNotification(){this.postSilkNotification(s.connect)}pingIframe(){return new Promise((e,t)=>{setTimeout(()=>t("Wallet ping timed out."),1e4);const n=t=>{"silk"===t.data.target&&t.data.messageType===a.notification&&t.data.data===s.ready&&(window.removeEventListener("message",n),e())};window.addEventListener("message",n),this.postReadyNotification()})}async postSilkRequest(e){const{silkOptions:t}=e,n=p({},function(e,t){if(null==e)return{};var n={};for(var i in e)if({}.hasOwnProperty.call(e,i)){if(-1!==t.indexOf(i))continue;n[i]=e[i]}return n}(e,M),{target:"silk",messageType:a.request},t&&{silkOptions:t}),i=await o(n);return this.iframeWindow.postMessage(p({},n,{id:i}),this.walletOrigin),i}addListener(e){const{handleResponse:t,handleRequest:n,onReceiveConnectNotif:i,onAccountChanged:o,onAsyncTxNotification:r}=e;window.addEventListener("message",e=>{var l,c,d;try{if("undefined"!=typeof process&&process,e.origin!==this.walletOrigin)return}catch(t){if(e.origin!==this.walletOrigin)return}if((null==(l=e.data)?void 0:l.messageType)===a.request)n(e.data);else if((null==(c=e.data)?void 0:c.messageType)===a.response)t(e.data);else if((null==(d=e.data)?void 0:d.messageType)===a.notification){var u,h,g,p,m;(null==(u=e.data)?void 0:u.data)===s.connect?null==i||i():(null==(h=e.data)||null==(h=h.data)?void 0:h.type)===s.account_changed&&o?o(e.data.data.account):(null!=(g=e.data)&&null!=(g=g.data)&&null!=(g=g.type)&&g.startsWith("waap_sign_")||null!=(p=e.data)&&null!=(p=p.data)&&null!=(p=p.type)&&p.startsWith("waap_2fa_")||null!=(m=e.data)&&null!=(m=m.data)&&null!=(m=m.type)&&m.startsWith("waap_tx_"))&&r&&r({type:e.data.data.type,payload:e.data.data.payload})}})}}class E extends i{constructor(e,t,n,a,s,o=!1){super(),this.isSilk=!0,this.isWaaP=!0,this.connected=!1,this.walletMessageManager=void 0,this.uiMessageManager=void 0,this.config=void 0,this.currentAccount=null,this.walletConnectProvider=null,this.asyncSigning=!1,this.persistedMethods=void 0,this.internalEventEmitter=new i,this.asyncSigning=o,this.uiMessageManager=new w,this.walletMessageManager=new _(e,!!s),this.config=p({},y,n||{}),this.persistedMethods={login:this.login.bind(this),logout:this.logout.bind(this),getLoginMethod:this.getLoginMethod.bind(this),walletMessageManager:this.walletMessageManager,internalEventEmitter:this.internalEventEmitter,uiMessageManager:this.uiMessageManager,portal:this.portal.bind(this),requestEmail:this.requestEmail.bind(this),requestSBT:this.requestSBT.bind(this),orcid:this.orcid.bind(this),enable:this.enable.bind(this),isConnected:this.isConnected.bind(this),toggleDarkMode:this.toggleDarkMode.bind(this),silkRequest:this.request.bind(this),waaPRequest:this.request.bind(this)},this.walletMessageManager.addListener({onReceiveConnectNotif:()=>{console.log("onReceiveConnectNotif"),this.connected=!0,this.emit("connect")},handleResponse:e=>this.internalEventEmitter.emit(e.id,e),handleRequest:e=>{e.method===r.show_modal?this.uiMessageManager.emit(m.show_modal):e.method===r.hide_modal&&this.uiMessageManager.emit(m.hide_modal)},onAccountChanged:e=>{this.currentAccount!==e&&(this.currentAccount=e,this.emit("accountsChanged",e?[e]:[]))},onAsyncTxNotification:e=>{this.emit(e.type,e.payload)}}),t&&this.walletMessageManager.pingIframe().then(()=>f({method:r.set_points_referral_code,params:[t]},!1,this.walletMessageManager,this.internalEventEmitter)).catch(e=>{console.error("Error setting referral code in Silk iframe:",e)}),a&&this.walletMessageManager.pingIframe().then(()=>f({method:r.set_project,params:[a,window.location.origin]},!1,this.walletMessageManager,this.internalEventEmitter)),n&&this.walletMessageManager.pingIframe().then(()=>f({method:r.set_custom_config,params:[n]},!1,this.walletMessageManager,this.internalEventEmitter)).catch(e=>{console.error("Error setting custom config in Silk iframe:",e)})}async initializeWalletConnect(){try{this.walletConnectProvider=await v.getInstance(),console.log("WalletConnect provider initialized")}catch(e){console.error("Failed to initialize WalletConnect provider:",e),this.walletConnectProvider=null}}async enable(){return this.request({method:"eth_requestAccounts"})}isConnected(){return this.connected}async request(e){if(!Object.keys(l).includes(e.method))return Promise.reject({error:`Not implemented (${e.method})`});const t=await this.attemptAutoConnect(e.method);if(null!==t)return t;if(function(e){try{var t;if(!e||!Array.isArray(e))return!1;const n=JSON.parse("string"==typeof e[1]?e[1]:JSON.stringify(e[1]));return"SafeTx"===n.primaryType&&void 0!==(null==(t=n.domain)?void 0:t.verifyingContract)&&n.message&&"to"in n.message&&"value"in n.message&&"safeTxGas"in n.message&&"baseGas"in n.message&&"gasPrice"in n.message&&"nonce"in n.message}catch(e){return!1}}(e.params)&&e.method!==l.eth_signTypedData_v4)return Promise.reject({error:"Safe transactions must be signed using eth_signTypedData_v4"});await this.walletMessageManager.pingIframe();const i=(e.method===l.eth_sendTransaction||e.method===l.eth_signTransaction||e.method===l.eth_signTypedData_v4||e.method===l.personal_sign)&&this.asyncSigning||e.async?{asyncSigning:!0}:void 0,a=await f(e,n(e.method),this.walletMessageManager,this.internalEventEmitter,i);if((e.method===l.wallet_switchEthereumChain||e.method===l.wallet_addEthereumChain)&&null===a){var s;this.emit("chainChanged",null==(s=e.params)||null==(s=s[0])?void 0:s.chainId);try{const t=await this.request({method:"eth_accounts"});var o,r;t&&t.length>0&&(this.currentAccount=t[0],this.emit("accountsChanged",t),this.emit("connect",{chainId:null==(o=e.params)||null==(o=o[0])?void 0:o.chainId}),console.log("[EthereumProvider] Re-emitted accounts and connect after chain switch:",{accounts:t,chainId:null==(r=e.params)||null==(r=r[0])?void 0:r.chainId}))}catch(t){var c;console.warn("[EthereumProvider] Failed to get accounts after chain switch:",t),this.currentAccount&&(this.emit("accountsChanged",[this.currentAccount]),this.emit("connect",{chainId:null==(c=e.params)||null==(c=c[0])?void 0:c.chainId}))}}return a}async login(e){var t;if("undefined"==typeof window||"undefined"==typeof document)return null;null!=(t=this.config.authenticationMethods)&&t.includes("wallet")&&!this.walletConnectProvider&&await this.initializeWalletConnect();const n=e||window.ethereum,i=n&&function(e){return!(null==e||!e.isMetaMask||e.isApexWallet||e.isAvalanche||e.isBitKeep||e.isBlockWallet||e.isMathWallet||e.isOkxWallet||e.isOKExWallet||e.isOneInchIOSWallet||e.isOneInchAndroidWallet||e.isOpera||e.isPortal||e.isRabby||e.isDefiant||e.isTokenPocket||e.isTokenary||e.isZerion)}(n)?"MetaMask":"Browser Wallet";await this.walletMessageManager.pingIframe();const a=await f({method:r.login,params:[i]},!0,this.walletMessageManager,this.internalEventEmitter);if("string"==typeof a&&a.includes("waap")&&(localStorage.setItem(`${window.location.origin}-connected`,"waap"),this.setupWaaP()),"string"==typeof a&&a.includes("injected")&&n&&(localStorage.setItem(`${window.location.origin}-connected`,"injected"),this.setupInjectedWallet(n)),"string"==typeof a&&a.includes("walletconnect")){localStorage.setItem(`${window.location.origin}-connected`,"walletconnect"),v.isConnected()||(await this.walletConnectProvider.open({view:"Connect"}),await v.listenToAccountChangeEvent());const e=v.getWalletProvider();this.setupWalletConnect(e)}return a}async logout(){await this.walletMessageManager.pingIframe(),this.cleanupInvalidLoginState();try{this.walletConnectProvider&&await this.walletConnectProvider.disconnect()}catch(e){console.error("Error disconnecting from WalletConnect:",e)}return f({method:r.logout},!1,this.walletMessageManager,this.internalEventEmitter)}cleanupInvalidLoginState(){try{localStorage.removeItem(`${window.location.origin}-connected`)}catch(e){console.warn("Could not clean up login state:",e)}}setupWaaP(){const e=p({},this.persistedMethods,{isWaaP:!0,on:this.on.bind(this),removeListener:this.removeListener.bind(this),request:this.request.bind(this)});window.silk=e,window.waap=e}setupInjectedWallet(e){var t,n,i,a,s;const o=p({},this.persistedMethods,e,{isSilk:!0,connected:null!=(t=e.connected)&&t,on:null!=(n=null==(i=e.on)?void 0:i.bind(e))?n:()=>{},removeListener:null!=(a=null==(s=e.removeListener)?void 0:s.bind(e))?a:()=>{},request:e.request.bind(e)});window.silk=o,window.waap=o}setupWalletConnect(e){var t,n,i,a;const s=p({},this.persistedMethods,e,{isSilk:!0,connected:!0,on:null!=(t=null==(n=e.on)?void 0:n.bind(e))?t:()=>{},removeListener:null!=(i=null==(a=e.removeListener)?void 0:a.bind(e))?i:()=>{},request:e.request.bind(e)});window.silk=s,window.waap=s}setupAutoConnectInjected(e){var t,n,i,a,s;const o=p({},this.persistedMethods,e,{isSilk:!1,connected:null!=(t=null==e.isConnected?void 0:e.isConnected())&&t,on:null!=(n=null==(i=e.on)?void 0:i.bind(e))?n:()=>{},removeListener:null!=(a=null==(s=e.removeListener)?void 0:s.bind(e))?a:()=>{},request:e.request.bind(e)});window.silk=o,window.waap=o}setupAutoConnectWalletConnect(e){var t,n,i,a;const s=p({},this.persistedMethods,e,{isSilk:!1,connected:!0,on:null!=(t=null==(n=e.on)?void 0:n.bind(e))?t:()=>{},removeListener:null!=(i=null==(a=e.removeListener)?void 0:a.bind(e))?i:()=>{},request:e.request.bind(e)});window.silk=s,window.waap=s}async attemptAutoConnect(e){var t=this;if("eth_requestAccounts"!==e&&"eth_accounts"!==e)return null;this.walletMessageManager.pingIframe().then(async function(){await f({method:r.auto_conn,params:[window.location.origin]},!1,t.walletMessageManager,t.internalEventEmitter)});const n=this.getLoginMethod();if("injected"===n&&window.ethereum)try{return this.setupAutoConnectInjected(window.ethereum),window.ethereum.request({method:"eth_requestAccounts"})}catch(e){return console.warn("Injected wallet auto-connect failed:",e),this.cleanupInvalidLoginState(),null}else if("walletconnect"===n)try{if(this.walletConnectProvider||(this.walletConnectProvider=await v.getInstance()),await v.waitForReadyAndCheckConnection()){const e=v.getWalletProvider();return this.setupAutoConnectWalletConnect(e),e.request({method:"eth_requestAccounts"})}return console.log("WalletConnect not connected, cleaning up and falling through to normal flow"),this.cleanupInvalidLoginState(),null}catch(e){return console.warn("WalletConnect auto-connect failed:",e),this.cleanupInvalidLoginState(),null}return null}getLoginMethod(){if("undefined"==typeof window||"undefined"==typeof localStorage)return null;try{const e=localStorage.getItem(`${window.location.origin}-connected`);if(!e)return null;if(!["waap","human","injected","walletconnect"].includes(e))return this.cleanupInvalidLoginState(),null;switch(e){case"injected":return window.ethereum?"injected":(this.cleanupInvalidLoginState(),null);case"walletconnect":try{return v.isConnected()?"walletconnect":(this.cleanupInvalidLoginState(),null)}catch(e){return this.cleanupInvalidLoginState(),null}case"waap":return"waap";case"human":return"human";default:return this.cleanupInvalidLoginState(),null}}catch(e){return console.warn("Error checking login method:",e),this.cleanupInvalidLoginState(),null}}async toggleDarkMode(){this.config.styles.darkMode=!this.config.styles.darkMode,this.walletMessageManager.pingIframe().then(()=>f({method:r.set_custom_config,params:[this.config]},!1,this.walletMessageManager,this.internalEventEmitter)).catch(e=>{console.error("Error setting custom config in Silk iframe:",e)})}async safe(){this.walletMessageManager.pingIframe().then(()=>f({method:r.safe},!0,this.walletMessageManager,this.internalEventEmitter))}async orcid(){this.walletMessageManager.pingIframe().then(()=>f({method:r.orcid},!0,this.walletMessageManager,this.internalEventEmitter))}async portal(e,t,n){let i;switch(await this.walletMessageManager.pingIframe(),t){case"get":i="gastank"===e?r.gastank_get_settings:r.customization_get;break;case"update":i="gastank"===e?r.gastank_update_settings:r.customization_update;break;case"topup":i=r.gastank_topup;break;case"init":i=r.project_init}return f({method:i,params:[n,window.location.origin]},"get"!==t&&"customization"!==e,this.walletMessageManager,this.internalEventEmitter)}async requestEmail(){return await this.walletMessageManager.pingIframe(),f({method:r.silk_requestEmail},!0,this.walletMessageManager,this.internalEventEmitter)}async requestSBT(e){return await this.walletMessageManager.pingIframe(),f({method:r.silk_requestSbt,params:[e]},!0,this.walletMessageManager,this.internalEventEmitter)}}const C="waap-wallet",I="waap-component-container",T="undefined"!=typeof window&&"undefined"!=typeof document&&"undefined"!=typeof HTMLElement;let k;function x(){return!(!T||!document.getElementById(I)&&!document.querySelector(C))}function b(){return T?document.getElementById(I)||document.querySelector(C):null}T?(k=class extends HTMLElement{constructor(){super(),this.style.display&&""!==this.style.display||(this.style.display="flex",this.style.alignItems="center",this.style.justifyContent="center"),this.style.width&&""!==this.style.width||(this.style.width="100%"),this.style.height&&""!==this.style.height||(this.style.height="100%"),this.id=I}connectedCallback(){this.setAttribute("data-waap-mounted","true")}disconnectedCallback(){this.removeAttribute("data-waap-mounted")}},"undefined"==typeof customElements||customElements.get(C)||customElements.define(C,k)):k=class{};const P="waap-wallet-iframe-container",L="waap-wallet-iframe-wrapper",S="waap-wallet-iframe",W=()=>document.getElementById(P)||document.getElementById("silk-wallet-iframe-container"),q=()=>document.getElementById(L)||document.getElementById("silk-wallet-iframe-wrapper"),A=()=>document.getElementById(S)||document.getElementById("silk-wallet-iframe"),O=P,j=L,N=S,R=n=>{const i=window.document.createElement("iframe");return i.id=N,i.src=`${n?e:t}/iframe`,console.log("iframe.src: ",i.src),i.style.width="100%",i.style.height="100%",i.style.border="none",i.style.borderRadius="24px",i.style.backgroundColor="transparent",i.style.background="transparent",i.style.padding="0",i.style.margin="0",i},D=(n,i)=>{const a=i=>{if(i.origin!==(n?e:t))return;if(!i.data||"object"!=typeof i.data)return;if("silk-iframe-size"!==i.data.type)return;const a=i.data.height,s=i.data.width;if("number"!=typeof a||"number"!=typeof s)return;if(a<50||a>2e3)return;if(s<50||s>2e3)return;if(!Number.isFinite(a)||!Number.isFinite(s))return;const o=q();o&&(o.style.height=`${Math.floor(a)}px`,o.style.width=`${Math.floor(s)}px`)};window.addEventListener("message",a),i.__silkCleanup=()=>{window.removeEventListener("message",a)}},B=(e={})=>{var t;t=!!e.useStaging,x()?(e=>{const t=b();if(!t)throw new Error("WaaP component not found in DOM");if(A())return;const n=window.document.createElement("div");n.id=j,n.style.position="relative",n.style.display="flex",n.style.alignItems="center",n.style.justifyContent="center",n.style.padding="0",n.style.margin="0",n.style.height="600px",n.style.width="380px";const i=R(e);D(e,i),setTimeout(()=>{const e=q();e&&(e.style.height="600px",e.style.width="380px")},100),n.appendChild(i),t.appendChild(n)})(t):(e=>{if(W())return;const t=window.document.createElement("div");t.id=O,t.style.position="fixed",t.style.top="0",t.style.left="0",t.style.right="0",t.style.bottom="0",t.style.width="100%",t.style.height="100%",t.style.display="flex",t.style.alignItems="center",t.style.justifyContent="center",t.style.backgroundColor="rgba(0, 0, 0, 0.5)",t.style.zIndex="99999",t.style.display="none";const n=window.document.createElement("div");n.id=j,n.style.position="relative",n.style.display="flex",n.style.alignItems="center",n.style.justifyContent="center",n.style.padding="0",n.style.margin="0",n.style.height="600px",n.style.width="380px",t.appendChild(n);const i=R(e);D(e,i),setTimeout(()=>{const e=q();e&&(e.style.height="600px",e.style.width="380px")},100),n.appendChild(i),window.document.body.appendChild(t)})(t);const n=A();if(!n)throw new Error("WaaP iframe does not exist on page");const i=x();e.walletConnectProjectId&&v.setProjectId(e.walletConnectProjectId);const{referralCode:a,config:s,project:o,asyncSigning:r}=e,l=new E(n.contentWindow,a,s||{},o||{},e.useStaging,r||!1);let c=i?"component":"modal";return l.uiMessageManager.on(m.show_modal,()=>{const e=x(),t=e?"component":"modal",n=q();if(t!==c&&n)if(c=t,e){const e=b();e&&e.appendChild(n)}else{let e=W();e||(e=window.document.createElement("div"),e.id=O,e.style.position="fixed",e.style.top="0",e.style.left="0",e.style.right="0",e.style.bottom="0",e.style.width="100%",e.style.height="100%",e.style.display="flex",e.style.alignItems="center",e.style.justifyContent="center",e.style.backgroundColor="rgba(0, 0, 0, 0.5)",e.style.zIndex="99999",window.document.body.appendChild(e)),e.appendChild(n)}const i=W();e&&n?n.style.display="flex":i&&(i.style.display="flex")}),l.uiMessageManager.on(m.hide_modal,()=>{const e=x(),t=q(),n=W();e&&t?t.style.display="none":n&&(n.style.display="none")}),window.silk=l,window.waap=l,l};class z extends i{constructor(e){super(),this.isSilk=!0,this.connected=!1,this.config=void 0,this.config=p({},y,e||{})}sendToExtension(e){return new Promise((t,n)=>{window.postMessage({type:"FROM_PAGE",request:e},"*"),window.addEventListener("message",e=>{"FROM_EXTENSION"===e.data.type&&(e.data.error?n(e.data.error):t(e.data.result))})})}async request(e){if(!Object.keys(l).includes(e.method))return Promise.reject({error:`Not implemented (${e.method})`});const t=await this.sendToExtension(e);var n;return e.method!==l.wallet_switchEthereumChain&&e.method!==l.wallet_addEthereumChain||null!==t||this.emit("chainChanged",null==(n=e.params)||null==(n=n[0])?void 0:n.chainId),t}async enable(){return this.request({method:"eth_requestAccounts"})}isConnected(){return this.connected}async login(){return this.sendToExtension({method:r.login})}async requestSBT(e){return this.sendToExtension({method:r.silk_requestSbt,params:[e]})}async toggleDarkMode(){this.config.styles.darkMode=!this.config.styles.darkMode,await this.sendToExtension({method:r.set_custom_config,params:[this.config]})}async safe(){return this.sendToExtension({method:r.safe})}}function F(e){const[t,n]=d(new Map),i=u(e);h(()=>{i.current=e},[e]),h(()=>{const e=window.waap||window.silk;if(!e)return void console.warn("[useWaapTransaction] WaaP provider not found. Make sure initWaaP() was called.");const t=e=>{var t;n(t=>{const n=new Map(t);return n.set(e.pendingTxId,{status:"pending",pendingTxId:e.pendingTxId,txRequest:e.txRequest}),n}),null==(t=i.current)||null==t.onPending||t.onPending(e)},a=e=>{var t;n(t=>{const n=new Map(t),i=n.get(e.pendingTxId);return i&&n.set(e.pendingTxId,p({},i,{status:"2fa"})),n}),null==(t=i.current)||null==t.on2faRequired||t.on2faRequired(e)},s=e=>{var t;n(t=>{const n=new Map(t),i=n.get(e.pendingTxId);return i&&n.set(e.pendingTxId,p({},i,{status:"signed",signature:e.signature,serializedTx:e.serializedTx})),n}),null==(t=i.current)||null==t.onSigned||t.onSigned(e)},o=e=>{var t;n(t=>{const n=new Map(t),i=n.get(e.pendingTxId);return i&&n.set(e.pendingTxId,p({},i,{status:"failed",error:e.error})),n}),null==(t=i.current)||null==t.onSignFailed||t.onSignFailed(e)},r=e=>{var t;n(t=>{const n=new Map(t),i=n.get(e.pendingTxId);return i&&n.set(e.pendingTxId,p({},i,{status:"tx_pending",txHash:e.txHash})),n}),null==(t=i.current)||null==t.onTxPending||t.onTxPending(e)},l=e=>{var t;n(t=>{const n=new Map(t),i=n.get(e.pendingTxId);return i&&n.set(e.pendingTxId,p({},i,{status:"confirmed",txHash:e.txHash,receipt:e.receipt})),n}),null==(t=i.current)||null==t.onConfirmed||t.onConfirmed(e)},c=e=>{var t;n(t=>{const n=new Map(t),i=n.get(e.pendingTxId);return i&&n.set(e.pendingTxId,p({},i,{status:"failed",error:e.error,stage:e.stage})),n}),null==(t=i.current)||null==t.onFailed||t.onFailed(e)};return e.on("waap_sign_pending",t),e.on("waap_2fa_required",a),e.on("waap_sign_complete",s),e.on("waap_sign_failed",o),e.on("waap_tx_pending",r),e.on("waap_tx_confirmed",l),e.on("waap_tx_failed",c),()=>{null==e.removeListener||e.removeListener("waap_sign_pending",t),null==e.removeListener||e.removeListener("waap_2fa_required",a),null==e.removeListener||e.removeListener("waap_sign_complete",s),null==e.removeListener||e.removeListener("waap_sign_failed",o),null==e.removeListener||e.removeListener("waap_tx_pending",r),null==e.removeListener||e.removeListener("waap_tx_confirmed",l),null==e.removeListener||e.removeListener("waap_tx_failed",c)}},[]);const a=g(async e=>{const t=window.waap||window.silk;if(!t)throw new Error("WaaP provider not found");return console.log("sendTransaction",e),t.request({method:"eth_sendTransaction",params:e})},[]),s=g(async e=>{const t=window.waap||window.silk;if(!t)throw new Error("WaaP provider not found");return t.request({method:"eth_signTransaction",params:e})},[]),o=Array.from(t.values()).some(e=>"pending"===e.status||"signing"===e.status||"2fa"===e.status||"signed"===e.status||"tx_pending"===e.status);return{sendTransaction:a,signTransaction:s,pendingTransactions:t,isAnyPending:o}}export{E as EthereumProvider,z as EthereumProviderExtension,v as SilkWalletConnect,w as UIMessageManager,m as UI_EVENT_NAMES,k as WaaP,v as WaaPWalletConnect,_ as WalletMessageManager,f as handleWalletRequestAndResponse,B as initWaaP,F as useWaapTransaction};
2
2
  //# sourceMappingURL=index.modern.mjs.map