Last active
March 14, 2026 11:45
-
-
Save frol/c7dc74de8d6527a4ef872d08f582ea70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var E=Object.defineProperty;var _=(a,o,d)=>o in a?E(a,o,{enumerable:!0,configurable:!0,writable:!0,value:d}):a[o]=d;var u=(a,o,d)=>_(a,typeof o!="symbol"?o+"":o,d);(function(){"use strict";const m={mainnet:"http://localhost:3000"},I={mainnet:"https://rpc.mainnet.near.org"};function T(r){const e=new TextEncoder().encode(r);let t="";for(let n=0;n<e.length;++n)t+=String.fromCharCode(e[n]);return btoa(t)}async function y(r,e,t){const s=await(await fetch(r,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({jsonrpc:"2.0",id:1,method:"tx",params:{tx_hash:e,sender_account_id:t,wait_until:"NONE"}})})).json();if(s.error)throw new Error(s.error.message||JSON.stringify(s.error));return s.result}class A{constructor(e,t){u(this,"walletUrl");u(this,"signedAccountId");u(this,"network");this.walletUrl=e,this.signedAccountId=window.localStorage.getItem("trezu:signedAccountId")||"",this.network=t}getAccountId(){return this.signedAccountId}isSignedIn(){return!!this.signedAccountId}signOut(){this.signedAccountId="",window.localStorage.removeItem("trezu:signedAccountId")}async requestSignIn(){const e=new URL(`${this.walletUrl}/wallet`);return e.searchParams.set("action","sign_in"),e.searchParams.set("network",this.network),e.searchParams.set("callbackUrl",window.selector.location),await this.handlePopup(e.toString(),t=>{const n=t.accountId;if(n)return this.signedAccountId=n,window.localStorage.setItem("trezu:signedAccountId",n),[{accountId:n,publicKey:t.publicKey||""}];throw new Error("Invalid response from Trezu Wallet")})}async signAndSendTransactions(e){const t=new URL(`${this.walletUrl}/wallet`);return t.searchParams.set("action","sign_transactions"),t.searchParams.set("network",this.network),t.searchParams.set("callbackUrl",window.selector.location),t.searchParams.set("transactions",T(JSON.stringify(e))),t.searchParams.set("signerId",this.signedAccountId),await this.handlePopup(t.toString(),async n=>{if(n.transactionHashes){const s=n.transactionHashes.split(","),c=I[this.network]||I.mainnet;return await Promise.all(s.map(l=>y(c,l,this.signedAccountId)))}return[]})}async signAndSendTransaction(e){return(await this.signAndSendTransactions([e]))[0]}async handlePopup(e,t){const n=window.innerWidth||screen.width,s=window.innerHeight||screen.height,c=(n-520)/2,p=(s-700)/2,l=window.selector.open(e,"TrezuWallet",`width=520,height=700,top=${p},left=${c}`);return await l.windowIdPromise?new Promise((S,w)=>{let h;const P=U=>{const i=U.data;!i||!i.type||!i.type.startsWith("trezu:")||i.type==="trezu:result"&&(i.status==="success"?(g(),l.close(),Promise.resolve(t(i)).then(S,w)):i.status==="failure"&&(g(),l.close(),w(new Error(i.errorMessage||"Operation failed"))))},g=()=>{window.removeEventListener("message",P),h!==void 0&&clearInterval(h)};window.addEventListener("message",P),h=setInterval(()=>{l.closed&&(g(),w(new Error("User closed the window")))},300)}):(await window.selector.ui.whenApprove({title:"Request action",button:"Open Trezu Wallet"}),await this.handlePopup(e,t))}}const f={mainnet:new A(m.mainnet,"mainnet")};(async()=>{const r=t=>{const n=f[t];if(!n)throw new Error(`Unsupported network: ${t}`);return n},e=async t=>{const n=r(t);if(!n.isSignedIn())return[];const s=n.getAccountId();return s?[{accountId:s,publicKey:""}]:[]};return{async signIn({network:t}){const n=r(t);return n.isSignedIn()||await n.requestSignIn(),e(t)},async signOut({network:t}){r(t).signOut()},async getAccounts({network:t}){return e(t)},async verifyOwner(){throw new Error("Method not supported by Trezu Wallet")},async signMessage(){throw new Error("Method not supported by Trezu Wallet")},async signAndSendTransaction({receiverId:t,actions:n,network:s}){const c=r(s);if(!c.isSignedIn())throw new Error("Wallet not signed in");return c.signAndSendTransaction({receiverId:t,actions:n})},async signAndSendTransactions({transactions:t,network:n}){const s=r(n);if(!s.isSignedIn())throw new Error("Wallet not signed in");return s.signAndSendTransactions(t)}}})().then(r=>{window.selector.ready(r)})})(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For context, see: NEAR-DevHub/trezu#359