@nexus-cross/crossx-sdk-wagmi 2.1.1 → 2.2.0-beta.1

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 +1 @@
1
- {"version":3,"file":"connector.d.ts","sourceRoot":"","sources":["../src/connector.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAIH,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAEzE,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAInD,oCAAoC;AACpC,MAAM,WAAW,sBAAsB;IACrC,GAAG,EAAE,SAAS,CAAC;IACf,cAAc,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACjC,qDAAqD;IACrD,oBAAoB,CAAC,EAAE,MAAM,MAAM,CAAC;IACpC,wDAAwD;IACxD,oBAAoB,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/C,oDAAoD;IACpD,sBAAsB,CAAC,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IAC7C,uDAAuD;IACvD,sBAAsB,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACnD;;;;;;;;;;;OAWG;IACH,sBAAsB,CAAC,EAAE,MAAM,IAAI,CAAC;CACrC;AAED,uCAAuC;AACvC,MAAM,MAAM,sBAAsB,GAAG,IAAI,CAAC,SAAS,EAAE,eAAe,CAAC,GAAG;IACtE,cAAc,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACjC,qDAAqD;IACrD,oBAAoB,CAAC,EAAE,MAAM,MAAM,CAAC;IACpC,wDAAwD;IACxD,oBAAoB,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/C,oDAAoD;IACpD,sBAAsB,CAAC,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IAC7C,uDAAuD;IACvD,sBAAsB,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACnD;;;;;;;;;;;;;OAaG;IACH,sBAAsB,CAAC,EAAE,MAAM,IAAI,CAAC;CACrC,CAAC;AAoDF,wBAAgB,eAAe,CAAC,OAAO,EAAE,sBAAsB,8GAc9D;AAID,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,sBAAsB,8GA0PpE"}
1
+ {"version":3,"file":"connector.d.ts","sourceRoot":"","sources":["../src/connector.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAIH,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAEzE,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAInD,oCAAoC;AACpC,MAAM,WAAW,sBAAsB;IACrC,GAAG,EAAE,SAAS,CAAC;IACf,cAAc,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACjC,qDAAqD;IACrD,oBAAoB,CAAC,EAAE,MAAM,MAAM,CAAC;IACpC,wDAAwD;IACxD,oBAAoB,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/C,oDAAoD;IACpD,sBAAsB,CAAC,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IAC7C,uDAAuD;IACvD,sBAAsB,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACnD;;;;;;;;;;;OAWG;IACH,sBAAsB,CAAC,EAAE,MAAM,IAAI,CAAC;CACrC;AAED,uCAAuC;AACvC,MAAM,MAAM,sBAAsB,GAAG,IAAI,CAAC,SAAS,EAAE,eAAe,CAAC,GAAG;IACtE,cAAc,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACjC,qDAAqD;IACrD,oBAAoB,CAAC,EAAE,MAAM,MAAM,CAAC;IACpC,wDAAwD;IACxD,oBAAoB,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/C,oDAAoD;IACpD,sBAAsB,CAAC,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IAC7C,uDAAuD;IACvD,sBAAsB,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACnD;;;;;;;;;;;;;OAaG;IACH,sBAAsB,CAAC,EAAE,MAAM,IAAI,CAAC;CACrC,CAAC;AAoDF,wBAAgB,eAAe,CAAC,OAAO,EAAE,sBAAsB,8GAc9D;AAID,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,sBAAsB,8GAsRpE"}
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const N=require("wagmi"),q=require("viem"),S=require("@nexus-cross/crossx-sdk-core");function G(s){return parseInt(s.split(":")[1]??"1",10)}function A(s){return`eip155:${s}`}function _(s){return"0x"+s.toString(16)}function D(s){const t=s.startsWith("0x")?s.slice(2):s,r=new Uint8Array(t.length/2);for(let n=0;n<r.length;n++)r[n]=parseInt(t.substring(n*2,n*2+2),16);return new TextDecoder().decode(r)}class k{constructor(t,r=S.ChainId.CROSS_MAINNET){this.sdk=t,this.listeners=new Map,this.walletIndex=0,this.currentAccounts=[],this.off=this.removeListener.bind(this),this.chainId=typeof r=="number"?r:G(r),this.sdk.on("addressChanged",({address:n,index:l})=>{this.setWalletIndex(l),this.notifyAccountsChanged([n])})}_setWalletIndexChangeCallback(t){this.walletIndexChangeCallback=t}async request({method:t,params:r}){try{return await this._handleRequest(t,r)}catch(n){throw n instanceof S.CROSSxError&&n.code===S.ErrorCode.USER_REJECTED?Object.assign(new Error("User rejected the request"),{code:4001,data:n.details}):n}}async _handleRequest(t,r){var l;const n=r??[];switch(t){case"eth_requestAccounts":case"eth_accounts":{if(this.currentAccounts.length>0)return[...this.currentAccounts];const e=await this.sdk.getAddress();return e?[e.address]:[]}case"eth_chainId":return _(this.chainId);case"net_version":return String(this.chainId);case"personal_sign":{const[e]=n,i=e.startsWith("0x")?D(e):e;return(await this.sdk.signMessage(A(this.chainId),i,{index:this.walletIndex})).signature}case"eth_sign":{const[,e]=n,i=e.startsWith("0x")?D(e):e;return(await this.sdk.signMessage(A(this.chainId),i,{index:this.walletIndex})).signature}case"eth_signTypedData":case"eth_signTypedData_v3":case"eth_signTypedData_v4":{const[,e]=n,i=typeof e=="string"?JSON.parse(e):e,c=(l=i==null?void 0:i.domain)==null?void 0:l.chainId,w=c==null||c===0,f=w?0:Number(c),x={index:this.walletIndex};return(w?await this.sdk.signTypedDataOffchain(i,x):await this.sdk.signTypedData(A(f),i,x)).signature}case"eth_sendTransaction":{const[e]=n,i=A(this.chainId);return(await this.sdk.sendTransaction(i,{from:e.from,to:e.to,value:e.value,data:e.data,gasLimit:e.gasLimit??e.gas,gasPrice:e.gasPrice,maxFeePerGas:e.maxFeePerGas,maxPriorityFeePerGas:e.maxPriorityFeePerGas,nonce:e.nonce!==void 0?parseInt(String(e.nonce),16):void 0,chainId:this.chainId},{index:this.walletIndex})).txHash}case"wallet_switchEthereumChain":{const[{chainId:e}]=n,i=parseInt(e,16);return i!==this.chainId&&(this.chainId=i,this.emit("chainChanged",_(i))),null}case"wallet_addEthereumChain":return null;default:return this.sdk.walletRpc(t,n,A(this.chainId))}}on(t,r){return this.listeners.has(t)||this.listeners.set(t,new Set),this.listeners.get(t).add(r),this}removeListener(t,r){var n;return(n=this.listeners.get(t))==null||n.delete(r),this}emit(t,...r){var n;(n=this.listeners.get(t))==null||n.forEach(l=>l(...r))}notifyAccountsChanged(t){this.currentAccounts=t,this.emit("accountsChanged",t)}notifyDisconnect(t){this.currentAccounts=[],this.walletIndex=0,this.emit("disconnect",t)}getCurrentChainId(){return this.chainId}setChainId(t){t!==this.chainId&&(this.chainId=t,this.emit("chainChanged",_(t)))}setWalletIndex(t){var r;this.walletIndex=t,(r=this.walletIndexChangeCallback)==null||r.call(this,t)}getWalletIndex(){return this.walletIndex}getCurrentAccounts(){return[...this.currentAccounts]}}function M(s){return typeof s=="number"?s:parseInt(s.split(":")[1]??"1",10)}const R="crossx-wallet-index";function U(){try{const s=typeof window<"u"?localStorage.getItem(R):null;return s&&Number(s)||0}catch{return 0}}function j(s){try{typeof window<"u"&&localStorage.setItem(R,String(s))}catch{}}function T(){try{typeof window<"u"&&localStorage.removeItem(R)}catch{}}const v="crossx-wallet-address";function F(){try{return typeof window<"u"?localStorage.getItem(v):null}catch{return null}}function z(s){try{typeof window<"u"&&localStorage.setItem(v,s)}catch{}}function K(s){const{defaultChainId:t,getStoredWalletIndex:r,onWalletIndexChanged:n,getStoredWalletAddress:l,onWalletAddressChanged:e,openConnectOtherWallet:i,...c}=s;if(i){const f=c.connectOtherWallets,x=Array.isArray(f)&&f.length>0,m=c.showConnectOtherWallets===!0;!x&&!m&&(c.showConnectOtherWallets=!0)}const w=S.createCROSSxSDK(c);return P({sdk:w,defaultChainId:t,getStoredWalletIndex:r,onWalletIndexChanged:n,getStoredWalletAddress:l,onWalletAddressChanged:e,openConnectOtherWallet:i})}function P(s){const{sdk:t,defaultChainId:r=S.ChainId.CROSS_MAINNET,getStoredWalletIndex:n=U,onWalletIndexChanged:l=j,getStoredWalletAddress:e=F,onWalletAddressChanged:i=z,openConnectOtherWallet:c}=s,w=M(r);let f=null,x=!1,m=!1,W=!1;function u(){return f||(f=new k(t,w),f._setWalletIndexChangeCallback(l)),f}return N.createConnector(g=>({id:"crossx",name:"CROSSx Wallet",type:"crossx",async setup(){if(!x){const y=n(),o=e()??void 0;await t.initialize({preferredWalletIndex:y,preferredWalletAddress:o}),x=!0,t.on("addressChanged",({address:h,index:p})=>{if(m)return;const C=u();C.setWalletIndex(p),h&&(C.notifyAccountsChanged([h]),i(h))}),t.on("authChanged",h=>{if(!(m||W))if(h.isAuthenticated&&h.address){const p=u();p.notifyAccountsChanged([h.address]),g.emitter.emit("connect",{accounts:[h.address],chainId:p.getCurrentChainId()})}else h.isAuthenticated||(u().notifyDisconnect(),T(),l(0),g.emitter.emit("disconnect"))}),c&&t.on("connectExternalWallet",c)}const a=u();a.on("accountsChanged",this.onAccountsChanged.bind(this)),a.on("chainChanged",this.onChainChanged.bind(this)),a.on("disconnect",this.onDisconnect.bind(this))},async connect({chainId:a,isReconnecting:y}={}){const o=u(),h=a??w;m=!0;try{if((y||t.isAuthenticated())&&t.isAuthenticated()){const d=await t.getAddress();if(d)return o.setWalletIndex(d.index),o.notifyAccountsChanged([d.address]),{accounts:[d.address],chainId:h};await t.signOut().catch(()=>{}),o.notifyDisconnect()}const p=e();let C;try{C=await t.signInWithCreate({preferredWalletAddress:p??void 0})}catch(d){throw d instanceof S.CROSSxError&&d.code===S.ErrorCode.EXTERNAL_WALLET_REQUESTED?(o.notifyDisconnect(),new q.UserRejectedRequestError(d)):(t.isAuthenticated()||(o.notifyDisconnect(),g.emitter.emit("disconnect")),d)}if(!C.success)throw new Error(C.error??"CROSSx sign-in failed");const I=C.address,b=C.addresses.map(d=>d.address).filter(d=>!I||d.toLowerCase()!==I.toLowerCase()),E=I?[I,...b]:b;if(E.length===0)throw new Error("No address found after sign-in");const O=C.addresses.find(d=>I&&d.address.toLowerCase()===I.toLowerCase()),L=(O==null?void 0:O.index)??0;return o.setWalletIndex(L),o.notifyAccountsChanged(E),I&&i(I),{accounts:E,chainId:h}}finally{m=!1}},async disconnect(){W=!0;try{const a=u();await t.signOut(),a.notifyDisconnect(),T(),l(0),g.emitter.emit("disconnect")}finally{W=!1}},async getAccounts(){const y=u().getCurrentAccounts();if(y.length>0)return y;const o=await t.getAddress();return o?[o.address]:[]},async getChainId(){return u().getCurrentChainId()},async getProvider({chainId:a}={}){return u()},async isAuthorized(){return await t.whenReady(),t.isAuthenticated()},async switchChain({chainId:a}){u().setChainId(a),g.emitter.emit("change",{chainId:a});const o=g.chains.find(h=>h.id===a);if(!o)throw new Error(`Chain ${a} is not configured`);return o},onAccountsChanged(a){a.length===0?g.emitter.emit("disconnect"):g.emitter.emit("change",{accounts:a})},onChainChanged(a){g.emitter.emit("change",{chainId:Number(a)})},onDisconnect(){g.emitter.emit("disconnect")}}))}exports.CROSSxEIP1193Provider=k;exports.createCROSSxConnector=P;exports.crossxConnector=K;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const U=require("wagmi"),j=require("viem"),m=require("@nexus-cross/crossx-sdk-core");function z(s){return parseInt(s.split(":")[1]??"1",10)}function S(s){return`eip155:${s}`}function R(s){return"0x"+s.toString(16)}function P(s){const t=s.startsWith("0x")?s.slice(2):s,r=new Uint8Array(t.length/2);for(let n=0;n<r.length;n++)r[n]=parseInt(t.substring(n*2,n*2+2),16);return new TextDecoder().decode(r)}class N{constructor(t,r=m.ChainId.CROSS_MAINNET){this.sdk=t,this.listeners=new Map,this.walletIndex=0,this.currentAccounts=[],this.off=this.removeListener.bind(this),this.chainId=typeof r=="number"?r:z(r),this.sdk.on("addressChanged",({address:n,index:h})=>{this.setWalletIndex(h),this.notifyAccountsChanged([n])})}_setWalletIndexChangeCallback(t){this.walletIndexChangeCallback=t}async request({method:t,params:r}){try{return await this._handleRequest(t,r)}catch(n){throw n instanceof m.CROSSxError&&n.code===m.ErrorCode.USER_REJECTED?Object.assign(new Error("User rejected the request"),{code:4001,data:n.details}):n}}async _handleRequest(t,r){var h;const n=r??[];switch(t){case"eth_requestAccounts":case"eth_accounts":{if(this.currentAccounts.length>0)return[...this.currentAccounts];const e=await this.sdk.getAddress();return e?[e.address]:[]}case"eth_chainId":return R(this.chainId);case"net_version":return String(this.chainId);case"personal_sign":{const[e]=n,c=e.startsWith("0x")?P(e):e;return(await this.sdk.signMessage(S(this.chainId),c,{index:this.walletIndex})).signature}case"eth_sign":{const[,e]=n,c=e.startsWith("0x")?P(e):e;return(await this.sdk.signMessage(S(this.chainId),c,{index:this.walletIndex})).signature}case"eth_signTypedData":case"eth_signTypedData_v3":case"eth_signTypedData_v4":{const[,e]=n,c=typeof e=="string"?JSON.parse(e):e,o=(h=c==null?void 0:c.domain)==null?void 0:h.chainId,x=o==null||o===0,C=x?0:Number(o),y={index:this.walletIndex};return(x?await this.sdk.signTypedDataOffchain(c,y):await this.sdk.signTypedData(S(C),c,y)).signature}case"eth_sendTransaction":{const[e]=n,c=S(this.chainId);return(await this.sdk.sendTransaction(c,{from:e.from,to:e.to,value:e.value,data:e.data,gasLimit:e.gasLimit??e.gas,gasPrice:e.gasPrice,maxFeePerGas:e.maxFeePerGas,maxPriorityFeePerGas:e.maxPriorityFeePerGas,nonce:e.nonce!==void 0?parseInt(String(e.nonce),16):void 0,chainId:this.chainId},{index:this.walletIndex})).txHash}case"wallet_switchEthereumChain":{const[{chainId:e}]=n,c=parseInt(e,16);return c!==this.chainId&&(this.chainId=c,this.emit("chainChanged",R(c))),null}case"wallet_addEthereumChain":return null;default:return this.sdk.walletRpc(t,n,S(this.chainId))}}on(t,r){return this.listeners.has(t)||this.listeners.set(t,new Set),this.listeners.get(t).add(r),this}removeListener(t,r){var n;return(n=this.listeners.get(t))==null||n.delete(r),this}emit(t,...r){var n;(n=this.listeners.get(t))==null||n.forEach(h=>h(...r))}notifyAccountsChanged(t){this.currentAccounts=t,this.emit("accountsChanged",t)}notifyDisconnect(t){this.currentAccounts=[],this.walletIndex=0,this.emit("disconnect",t)}getCurrentChainId(){return this.chainId}setChainId(t){t!==this.chainId&&(this.chainId=t,this.emit("chainChanged",R(t)))}setWalletIndex(t){var r;this.walletIndex=t,(r=this.walletIndexChangeCallback)==null||r.call(this,t)}getWalletIndex(){return this.walletIndex}getCurrentAccounts(){return[...this.currentAccounts]}}function F(s){return typeof s=="number"?s:parseInt(s.split(":")[1]??"1",10)}const b="crossx-wallet-index";function K(){try{const s=typeof window<"u"?localStorage.getItem(b):null;return s&&Number(s)||0}catch{return 0}}function H(s){try{typeof window<"u"&&localStorage.setItem(b,String(s))}catch{}}function L(){try{typeof window<"u"&&localStorage.removeItem(b)}catch{}}const q="crossx-wallet-address";function J(){try{return typeof window<"u"?localStorage.getItem(q):null}catch{return null}}function Y(s){try{typeof window<"u"&&localStorage.setItem(q,s)}catch{}}function $(s){const{defaultChainId:t,getStoredWalletIndex:r,onWalletIndexChanged:n,getStoredWalletAddress:h,onWalletAddressChanged:e,openConnectOtherWallet:c,...o}=s;if(c){const C=o.connectOtherWallets,y=Array.isArray(C)&&C.length>0,I=o.showConnectOtherWallets===!0;!y&&!I&&(o.showConnectOtherWallets=!0)}const x=m.createCROSSxSDK(o);return G({sdk:x,defaultChainId:t,getStoredWalletIndex:r,onWalletIndexChanged:n,getStoredWalletAddress:h,onWalletAddressChanged:e,openConnectOtherWallet:c})}function G(s){const{sdk:t,defaultChainId:r=m.ChainId.CROSS_MAINNET,getStoredWalletIndex:n=K,onWalletIndexChanged:h=H,getStoredWalletAddress:e=J,onWalletAddressChanged:c=Y,openConnectOtherWallet:o}=s,x=F(r);let C=null,y=!1,I=null,k=!1,A=!1,W=!1;function u(){return C||(C=new N(t,x),C._setWalletIndexChangeCallback(h)),C}return U.createConnector(f=>{function D(i){if(k)return;const l=u();l.on("accountsChanged",i.onAccountsChanged.bind(i)),l.on("chainChanged",i.onChainChanged.bind(i)),l.on("disconnect",i.onDisconnect.bind(i)),k=!0}async function v(){if(!y)return I||(I=(async()=>{const i=n(),l=e()??void 0;await t.initialize({preferredWalletIndex:i,preferredWalletAddress:l}),t.on("addressChanged",({address:a,index:g})=>{if(A)return;const E=u();E.setWalletIndex(g),a&&(E.notifyAccountsChanged([a]),c(a))}),t.on("authChanged",a=>{if(!(A||W))if(a.isAuthenticated&&a.address){const g=u();g.notifyAccountsChanged([a.address]),f.emitter.emit("connect",{accounts:[a.address],chainId:g.getCurrentChainId()})}else a.isAuthenticated||(u().notifyDisconnect(),L(),h(0),f.emitter.emit("disconnect"))}),o&&t.on("connectExternalWallet",o),y=!0})().finally(()=>{I=null}),I)}return{id:"crossx",name:"CROSSx Wallet",type:"crossx",async setup(){await v(),D(this)},async connect({chainId:i,isReconnecting:l}={}){await v(),D(this);const a=u(),g=i??x;A=!0;try{if((l||t.isAuthenticated())&&t.isAuthenticated()){const d=await t.getAddress();if(d)return a.setWalletIndex(d.index),a.notifyAccountsChanged([d.address]),{accounts:[d.address],chainId:g};await t.signOut().catch(()=>{}),a.notifyDisconnect()}const E=e();let p;try{p=await t.signInWithCreate({preferredWalletAddress:E??void 0})}catch(d){throw d instanceof m.CROSSxError&&d.code===m.ErrorCode.EXTERNAL_WALLET_REQUESTED?(a.notifyDisconnect(),new j.UserRejectedRequestError(d)):(t.isAuthenticated()||(a.notifyDisconnect(),f.emitter.emit("disconnect")),d)}if(!p.success)throw new Error(p.error??"CROSSx sign-in failed");const w=p.address,T=p.addresses.map(d=>d.address).filter(d=>!w||d.toLowerCase()!==w.toLowerCase()),O=w?[w,...T]:T;if(O.length===0)throw new Error("No address found after sign-in");const _=p.addresses.find(d=>w&&d.address.toLowerCase()===w.toLowerCase()),M=(_==null?void 0:_.index)??0;return a.setWalletIndex(M),a.notifyAccountsChanged(O),w&&c(w),{accounts:O,chainId:g}}finally{A=!1}},async disconnect(){W=!0;try{const i=u();await t.signOut(),i.notifyDisconnect(),L(),h(0),f.emitter.emit("disconnect")}finally{W=!1}},async getAccounts(){const l=u().getCurrentAccounts();if(l.length>0)return l;const a=await t.getAddress();return a?[a.address]:[]},async getChainId(){return u().getCurrentChainId()},async getProvider({chainId:i}={}){return u()},async isAuthorized(){return await t.whenReady(),t.isAuthenticated()},async switchChain({chainId:i}){u().setChainId(i),f.emitter.emit("change",{chainId:i});const a=f.chains.find(g=>g.id===i);if(!a)throw new Error(`Chain ${i} is not configured`);return a},onAccountsChanged(i){i.length===0?f.emitter.emit("disconnect"):f.emitter.emit("change",{accounts:i})},onChainChanged(i){f.emitter.emit("change",{chainId:Number(i)})},onDisconnect(){f.emitter.emit("disconnect")}}})}exports.CROSSxEIP1193Provider=N;exports.createCROSSxConnector=G;exports.crossxConnector=$;
package/dist/index.js CHANGED
@@ -1,25 +1,25 @@
1
- import { createConnector as N } from "wagmi";
2
- import { UserRejectedRequestError as G } from "viem";
3
- import { ChainId as T, CROSSxError as k, ErrorCode as v, createCROSSxSDK as q } from "@nexus-cross/crossx-sdk-core";
4
- function U(s) {
1
+ import { createConnector as M } from "wagmi";
2
+ import { UserRejectedRequestError as z } from "viem";
3
+ import { ChainId as L, CROSSxError as N, ErrorCode as G, createCROSSxSDK as F } from "@nexus-cross/crossx-sdk-core";
4
+ function j(s) {
5
5
  return parseInt(s.split(":")[1] ?? "1", 10);
6
6
  }
7
- function A(s) {
7
+ function x(s) {
8
8
  return `eip155:${s}`;
9
9
  }
10
- function _(s) {
10
+ function O(s) {
11
11
  return "0x" + s.toString(16);
12
12
  }
13
- function R(s) {
13
+ function v(s) {
14
14
  const t = s.startsWith("0x") ? s.slice(2) : s, i = new Uint8Array(t.length / 2);
15
15
  for (let n = 0; n < i.length; n++)
16
16
  i[n] = parseInt(t.substring(n * 2, n * 2 + 2), 16);
17
17
  return new TextDecoder().decode(i);
18
18
  }
19
- class M {
20
- constructor(t, i = T.CROSS_MAINNET) {
21
- this.sdk = t, this.listeners = /* @__PURE__ */ new Map(), this.walletIndex = 0, this.currentAccounts = [], this.off = this.removeListener.bind(this), this.chainId = typeof i == "number" ? i : U(i), this.sdk.on("addressChanged", ({ address: n, index: l }) => {
22
- this.setWalletIndex(l), this.notifyAccountsChanged([n]);
19
+ class K {
20
+ constructor(t, i = L.CROSS_MAINNET) {
21
+ this.sdk = t, this.listeners = /* @__PURE__ */ new Map(), this.walletIndex = 0, this.currentAccounts = [], this.off = this.removeListener.bind(this), this.chainId = typeof i == "number" ? i : j(i), this.sdk.on("addressChanged", ({ address: n, index: h }) => {
22
+ this.setWalletIndex(h), this.notifyAccountsChanged([n]);
23
23
  });
24
24
  }
25
25
  /** connector가 내부적으로 호출. 외부에서 사용하지 않습니다. */
@@ -31,14 +31,14 @@ class M {
31
31
  try {
32
32
  return await this._handleRequest(t, i);
33
33
  } catch (n) {
34
- throw n instanceof k && n.code === v.USER_REJECTED ? Object.assign(
34
+ throw n instanceof N && n.code === G.USER_REJECTED ? Object.assign(
35
35
  new Error("User rejected the request"),
36
36
  { code: 4001, data: n.details }
37
37
  ) : n;
38
38
  }
39
39
  }
40
40
  async _handleRequest(t, i) {
41
- var l;
41
+ var h;
42
42
  const n = i ?? [];
43
43
  switch (t) {
44
44
  case "eth_requestAccounts":
@@ -49,34 +49,34 @@ class M {
49
49
  return e ? [e.address] : [];
50
50
  }
51
51
  case "eth_chainId":
52
- return _(this.chainId);
52
+ return O(this.chainId);
53
53
  case "net_version":
54
54
  return String(this.chainId);
55
55
  case "personal_sign": {
56
- const [e] = n, r = e.startsWith("0x") ? R(e) : e;
56
+ const [e] = n, c = e.startsWith("0x") ? v(e) : e;
57
57
  return (await this.sdk.signMessage(
58
- A(this.chainId),
59
- r,
58
+ x(this.chainId),
59
+ c,
60
60
  { index: this.walletIndex }
61
61
  )).signature;
62
62
  }
63
63
  case "eth_sign": {
64
- const [, e] = n, r = e.startsWith("0x") ? R(e) : e;
64
+ const [, e] = n, c = e.startsWith("0x") ? v(e) : e;
65
65
  return (await this.sdk.signMessage(
66
- A(this.chainId),
67
- r,
66
+ x(this.chainId),
67
+ c,
68
68
  { index: this.walletIndex }
69
69
  )).signature;
70
70
  }
71
71
  case "eth_signTypedData":
72
72
  case "eth_signTypedData_v3":
73
73
  case "eth_signTypedData_v4": {
74
- const [, e] = n, r = typeof e == "string" ? JSON.parse(e) : e, c = (l = r == null ? void 0 : r.domain) == null ? void 0 : l.chainId, w = c == null || c === 0, g = w ? 0 : Number(c), m = { index: this.walletIndex };
75
- return (w ? await this.sdk.signTypedDataOffchain(r, m) : await this.sdk.signTypedData(A(g), r, m)).signature;
74
+ const [, e] = n, c = typeof e == "string" ? JSON.parse(e) : e, o = (h = c == null ? void 0 : c.domain) == null ? void 0 : h.chainId, m = o == null || o === 0, C = m ? 0 : Number(o), p = { index: this.walletIndex };
75
+ return (m ? await this.sdk.signTypedDataOffchain(c, p) : await this.sdk.signTypedData(x(C), c, p)).signature;
76
76
  }
77
77
  case "eth_sendTransaction": {
78
- const [e] = n, r = A(this.chainId);
79
- return (await this.sdk.sendTransaction(r, {
78
+ const [e] = n, c = x(this.chainId);
79
+ return (await this.sdk.sendTransaction(c, {
80
80
  from: e.from,
81
81
  to: e.to,
82
82
  value: e.value,
@@ -90,13 +90,13 @@ class M {
90
90
  }, { index: this.walletIndex })).txHash;
91
91
  }
92
92
  case "wallet_switchEthereumChain": {
93
- const [{ chainId: e }] = n, r = parseInt(e, 16);
94
- return r !== this.chainId && (this.chainId = r, this.emit("chainChanged", _(r))), null;
93
+ const [{ chainId: e }] = n, c = parseInt(e, 16);
94
+ return c !== this.chainId && (this.chainId = c, this.emit("chainChanged", O(c))), null;
95
95
  }
96
96
  case "wallet_addEthereumChain":
97
97
  return null;
98
98
  default:
99
- return this.sdk.walletRpc(t, n, A(this.chainId));
99
+ return this.sdk.walletRpc(t, n, x(this.chainId));
100
100
  }
101
101
  }
102
102
  // ── EIP-1193 이벤트 ───────────────────────────────────────────────────────
@@ -109,7 +109,7 @@ class M {
109
109
  }
110
110
  emit(t, ...i) {
111
111
  var n;
112
- (n = this.listeners.get(t)) == null || n.forEach((l) => l(...i));
112
+ (n = this.listeners.get(t)) == null || n.forEach((h) => h(...i));
113
113
  }
114
114
  // ── Connector 전용 헬퍼 ───────────────────────────────────────────────────
115
115
  notifyAccountsChanged(t) {
@@ -122,7 +122,7 @@ class M {
122
122
  return this.chainId;
123
123
  }
124
124
  setChainId(t) {
125
- t !== this.chainId && (this.chainId = t, this.emit("chainChanged", _(t)));
125
+ t !== this.chainId && (this.chainId = t, this.emit("chainChanged", O(t)));
126
126
  }
127
127
  /**
128
128
  * 활성 지갑 인덱스 설정.
@@ -139,189 +139,201 @@ class M {
139
139
  return [...this.currentAccounts];
140
140
  }
141
141
  }
142
- function F(s) {
142
+ function H(s) {
143
143
  return typeof s == "number" ? s : parseInt(s.split(":")[1] ?? "1", 10);
144
144
  }
145
- const O = "crossx-wallet-index";
146
- function j() {
145
+ const D = "crossx-wallet-index";
146
+ function J() {
147
147
  try {
148
- const s = typeof window < "u" ? localStorage.getItem(O) : null;
148
+ const s = typeof window < "u" ? localStorage.getItem(D) : null;
149
149
  return s && Number(s) || 0;
150
150
  } catch {
151
151
  return 0;
152
152
  }
153
153
  }
154
- function z(s) {
154
+ function Y(s) {
155
155
  try {
156
- typeof window < "u" && localStorage.setItem(O, String(s));
156
+ typeof window < "u" && localStorage.setItem(D, String(s));
157
157
  } catch {
158
158
  }
159
159
  }
160
- function b() {
160
+ function P() {
161
161
  try {
162
- typeof window < "u" && localStorage.removeItem(O);
162
+ typeof window < "u" && localStorage.removeItem(D);
163
163
  } catch {
164
164
  }
165
165
  }
166
- const P = "crossx-wallet-address";
167
- function K() {
166
+ const q = "crossx-wallet-address";
167
+ function $() {
168
168
  try {
169
- return typeof window < "u" ? localStorage.getItem(P) : null;
169
+ return typeof window < "u" ? localStorage.getItem(q) : null;
170
170
  } catch {
171
171
  return null;
172
172
  }
173
173
  }
174
- function H(s) {
174
+ function Q(s) {
175
175
  try {
176
- typeof window < "u" && localStorage.setItem(P, s);
176
+ typeof window < "u" && localStorage.setItem(q, s);
177
177
  } catch {
178
178
  }
179
179
  }
180
- function X(s) {
181
- const { defaultChainId: t, getStoredWalletIndex: i, onWalletIndexChanged: n, getStoredWalletAddress: l, onWalletAddressChanged: e, openConnectOtherWallet: r, ...c } = s;
182
- if (r) {
183
- const g = c.connectOtherWallets, m = Array.isArray(g) && g.length > 0, p = c.showConnectOtherWallets === !0;
184
- !m && !p && (c.showConnectOtherWallets = !0);
180
+ function tt(s) {
181
+ const { defaultChainId: t, getStoredWalletIndex: i, onWalletIndexChanged: n, getStoredWalletAddress: h, onWalletAddressChanged: e, openConnectOtherWallet: c, ...o } = s;
182
+ if (c) {
183
+ const C = o.connectOtherWallets, p = Array.isArray(C) && C.length > 0, I = o.showConnectOtherWallets === !0;
184
+ !p && !I && (o.showConnectOtherWallets = !0);
185
185
  }
186
- const w = q(c);
187
- return J({ sdk: w, defaultChainId: t, getStoredWalletIndex: i, onWalletIndexChanged: n, getStoredWalletAddress: l, onWalletAddressChanged: e, openConnectOtherWallet: r });
186
+ const m = F(o);
187
+ return X({ sdk: m, defaultChainId: t, getStoredWalletIndex: i, onWalletIndexChanged: n, getStoredWalletAddress: h, onWalletAddressChanged: e, openConnectOtherWallet: c });
188
188
  }
189
- function J(s) {
189
+ function X(s) {
190
190
  const {
191
191
  sdk: t,
192
- defaultChainId: i = T.CROSS_MAINNET,
193
- getStoredWalletIndex: n = j,
194
- onWalletIndexChanged: l = z,
195
- getStoredWalletAddress: e = K,
196
- onWalletAddressChanged: r = H,
197
- openConnectOtherWallet: c
198
- } = s, w = F(i);
199
- let g = null, m = !1, p = !1, S = !1;
192
+ defaultChainId: i = L.CROSS_MAINNET,
193
+ getStoredWalletIndex: n = J,
194
+ onWalletIndexChanged: h = Y,
195
+ getStoredWalletAddress: e = $,
196
+ onWalletAddressChanged: c = Q,
197
+ openConnectOtherWallet: o
198
+ } = s, m = H(i);
199
+ let C = null, p = !1, I = null, R = !1, A = !1, W = !1;
200
200
  function u() {
201
- return g || (g = new M(t, w), g._setWalletIndexChangeCallback(l)), g;
201
+ return C || (C = new K(t, m), C._setWalletIndexChangeCallback(h)), C;
202
202
  }
203
- return N((f) => ({
204
- id: "crossx",
205
- name: "CROSSx Wallet",
206
- type: "crossx",
207
- async setup() {
208
- if (!m) {
209
- const x = n(), o = e() ?? void 0;
210
- await t.initialize({ preferredWalletIndex: x, preferredWalletAddress: o }), m = !0, t.on("addressChanged", ({ address: h, index: y }) => {
211
- if (p) return;
212
- const C = u();
213
- C.setWalletIndex(y), h && (C.notifyAccountsChanged([h]), r(h));
214
- }), t.on("authChanged", (h) => {
215
- if (!(p || S))
216
- if (h.isAuthenticated && h.address) {
217
- const y = u();
218
- y.notifyAccountsChanged([h.address]), f.emitter.emit("connect", {
219
- accounts: [h.address],
220
- chainId: y.getCurrentChainId()
221
- });
222
- } else h.isAuthenticated || (u().notifyDisconnect(), b(), l(0), f.emitter.emit("disconnect"));
223
- }), c && t.on("connectExternalWallet", c);
224
- }
225
- const a = u();
226
- a.on("accountsChanged", this.onAccountsChanged.bind(this)), a.on("chainChanged", this.onChainChanged.bind(this)), a.on("disconnect", this.onDisconnect.bind(this));
227
- },
228
- // ── connect ─────────────────────────────────────────────────────────────
229
- async connect({ chainId: a, isReconnecting: x } = {}) {
230
- const o = u(), h = a ?? w;
231
- p = !0;
232
- try {
233
- if ((x || t.isAuthenticated()) && t.isAuthenticated()) {
234
- const d = await t.getAddress();
235
- if (d)
236
- return o.setWalletIndex(d.index), o.notifyAccountsChanged([d.address]), {
237
- accounts: [d.address],
238
- chainId: h
239
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
240
- };
241
- await t.signOut().catch(() => {
242
- }), o.notifyDisconnect();
203
+ return M((f) => {
204
+ function k(r) {
205
+ if (R) return;
206
+ const l = u();
207
+ l.on("accountsChanged", r.onAccountsChanged.bind(r)), l.on("chainChanged", r.onChainChanged.bind(r)), l.on("disconnect", r.onDisconnect.bind(r)), R = !0;
208
+ }
209
+ async function b() {
210
+ if (!p)
211
+ return I || (I = (async () => {
212
+ const r = n(), l = e() ?? void 0;
213
+ await t.initialize({ preferredWalletIndex: r, preferredWalletAddress: l }), t.on("addressChanged", ({ address: a, index: g }) => {
214
+ if (A) return;
215
+ const S = u();
216
+ S.setWalletIndex(g), a && (S.notifyAccountsChanged([a]), c(a));
217
+ }), t.on("authChanged", (a) => {
218
+ if (!(A || W))
219
+ if (a.isAuthenticated && a.address) {
220
+ const g = u();
221
+ g.notifyAccountsChanged([a.address]), f.emitter.emit("connect", {
222
+ accounts: [a.address],
223
+ chainId: g.getCurrentChainId()
224
+ });
225
+ } else a.isAuthenticated || (u().notifyDisconnect(), P(), h(0), f.emitter.emit("disconnect"));
226
+ }), o && t.on("connectExternalWallet", o), p = !0;
227
+ })().finally(() => {
228
+ I = null;
229
+ }), I);
230
+ }
231
+ return {
232
+ id: "crossx",
233
+ name: "CROSSx Wallet",
234
+ type: "crossx",
235
+ async setup() {
236
+ await b(), k(this);
237
+ },
238
+ // ── connect ─────────────────────────────────────────────────────────────
239
+ async connect({ chainId: r, isReconnecting: l } = {}) {
240
+ await b(), k(this);
241
+ const a = u(), g = r ?? m;
242
+ A = !0;
243
+ try {
244
+ if ((l || t.isAuthenticated()) && t.isAuthenticated()) {
245
+ const d = await t.getAddress();
246
+ if (d)
247
+ return a.setWalletIndex(d.index), a.notifyAccountsChanged([d.address]), {
248
+ accounts: [d.address],
249
+ chainId: g
250
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
251
+ };
252
+ await t.signOut().catch(() => {
253
+ }), a.notifyDisconnect();
254
+ }
255
+ const S = e();
256
+ let y;
257
+ try {
258
+ y = await t.signInWithCreate({
259
+ preferredWalletAddress: S ?? void 0
260
+ });
261
+ } catch (d) {
262
+ throw d instanceof N && d.code === G.EXTERNAL_WALLET_REQUESTED ? (a.notifyDisconnect(), new z(d)) : (t.isAuthenticated() || (a.notifyDisconnect(), f.emitter.emit("disconnect")), d);
263
+ }
264
+ if (!y.success)
265
+ throw new Error(y.error ?? "CROSSx sign-in failed");
266
+ const w = y.address, T = y.addresses.map((d) => d.address).filter((d) => !w || d.toLowerCase() !== w.toLowerCase()), E = w ? [w, ...T] : T;
267
+ if (E.length === 0)
268
+ throw new Error("No address found after sign-in");
269
+ const _ = y.addresses.find(
270
+ (d) => w && d.address.toLowerCase() === w.toLowerCase()
271
+ ), U = (_ == null ? void 0 : _.index) ?? 0;
272
+ return a.setWalletIndex(U), a.notifyAccountsChanged(E), w && c(w), {
273
+ accounts: E,
274
+ chainId: g
275
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
276
+ };
277
+ } finally {
278
+ A = !1;
243
279
  }
244
- const y = e();
245
- let C;
280
+ },
281
+ // ── disconnect ──────────────────────────────────────────────────────────
282
+ async disconnect() {
283
+ W = !0;
246
284
  try {
247
- C = await t.signInWithCreate({
248
- preferredWalletAddress: y ?? void 0
249
- });
250
- } catch (d) {
251
- throw d instanceof k && d.code === v.EXTERNAL_WALLET_REQUESTED ? (o.notifyDisconnect(), new G(d)) : (t.isAuthenticated() || (o.notifyDisconnect(), f.emitter.emit("disconnect")), d);
285
+ const r = u();
286
+ await t.signOut(), r.notifyDisconnect(), P(), h(0), f.emitter.emit("disconnect");
287
+ } finally {
288
+ W = !1;
252
289
  }
253
- if (!C.success)
254
- throw new Error(C.error ?? "CROSSx sign-in failed");
255
- const I = C.address, D = C.addresses.map((d) => d.address).filter((d) => !I || d.toLowerCase() !== I.toLowerCase()), W = I ? [I, ...D] : D;
256
- if (W.length === 0)
257
- throw new Error("No address found after sign-in");
258
- const E = C.addresses.find(
259
- (d) => I && d.address.toLowerCase() === I.toLowerCase()
260
- ), L = (E == null ? void 0 : E.index) ?? 0;
261
- return o.setWalletIndex(L), o.notifyAccountsChanged(W), I && r(I), {
262
- accounts: W,
263
- chainId: h
264
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
265
- };
266
- } finally {
267
- p = !1;
290
+ },
291
+ // ── getAccounts ─────────────────────────────────────────────────────────
292
+ async getAccounts() {
293
+ const l = u().getCurrentAccounts();
294
+ if (l.length > 0) return l;
295
+ const a = await t.getAddress();
296
+ return a ? [a.address] : [];
297
+ },
298
+ // ── getChainId ──────────────────────────────────────────────────────────
299
+ async getChainId() {
300
+ return u().getCurrentChainId();
301
+ },
302
+ // ── getProvider ─────────────────────────────────────────────────────────
303
+ async getProvider({ chainId: r } = {}) {
304
+ return u();
305
+ },
306
+ // ── isAuthorized ────────────────────────────────────────────────────────
307
+ async isAuthorized() {
308
+ return await t.whenReady(), t.isAuthenticated();
309
+ },
310
+ // ── switchChain ─────────────────────────────────────────────────────────
311
+ async switchChain({ chainId: r }) {
312
+ u().setChainId(r), f.emitter.emit("change", { chainId: r });
313
+ const a = f.chains.find((g) => g.id === r);
314
+ if (!a)
315
+ throw new Error(`Chain ${r} is not configured`);
316
+ return a;
317
+ },
318
+ // ── 이벤트 핸들러 ───────────────────────────────────────────────────────
319
+ onAccountsChanged(r) {
320
+ r.length === 0 ? f.emitter.emit("disconnect") : f.emitter.emit("change", {
321
+ accounts: r
322
+ });
323
+ },
324
+ onChainChanged(r) {
325
+ f.emitter.emit("change", {
326
+ chainId: Number(r)
327
+ });
328
+ },
329
+ onDisconnect() {
330
+ f.emitter.emit("disconnect");
268
331
  }
269
- },
270
- // ── disconnect ──────────────────────────────────────────────────────────
271
- async disconnect() {
272
- S = !0;
273
- try {
274
- const a = u();
275
- await t.signOut(), a.notifyDisconnect(), b(), l(0), f.emitter.emit("disconnect");
276
- } finally {
277
- S = !1;
278
- }
279
- },
280
- // ── getAccounts ─────────────────────────────────────────────────────────
281
- async getAccounts() {
282
- const x = u().getCurrentAccounts();
283
- if (x.length > 0) return x;
284
- const o = await t.getAddress();
285
- return o ? [o.address] : [];
286
- },
287
- // ── getChainId ──────────────────────────────────────────────────────────
288
- async getChainId() {
289
- return u().getCurrentChainId();
290
- },
291
- // ── getProvider ─────────────────────────────────────────────────────────
292
- async getProvider({ chainId: a } = {}) {
293
- return u();
294
- },
295
- // ── isAuthorized ────────────────────────────────────────────────────────
296
- async isAuthorized() {
297
- return await t.whenReady(), t.isAuthenticated();
298
- },
299
- // ── switchChain ─────────────────────────────────────────────────────────
300
- async switchChain({ chainId: a }) {
301
- u().setChainId(a), f.emitter.emit("change", { chainId: a });
302
- const o = f.chains.find((h) => h.id === a);
303
- if (!o)
304
- throw new Error(`Chain ${a} is not configured`);
305
- return o;
306
- },
307
- // ── 이벤트 핸들러 ───────────────────────────────────────────────────────
308
- onAccountsChanged(a) {
309
- a.length === 0 ? f.emitter.emit("disconnect") : f.emitter.emit("change", {
310
- accounts: a
311
- });
312
- },
313
- onChainChanged(a) {
314
- f.emitter.emit("change", {
315
- chainId: Number(a)
316
- });
317
- },
318
- onDisconnect() {
319
- f.emitter.emit("disconnect");
320
- }
321
- }));
332
+ };
333
+ });
322
334
  }
323
335
  export {
324
- M as CROSSxEIP1193Provider,
325
- J as createCROSSxConnector,
326
- X as crossxConnector
336
+ K as CROSSxEIP1193Provider,
337
+ X as createCROSSxConnector,
338
+ tt as crossxConnector
327
339
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nexus-cross/crossx-sdk-wagmi",
3
- "version": "2.1.1",
3
+ "version": "2.2.0-beta.1",
4
4
  "description": "CROSSx SDK wagmi Connector — OAuth 임베디드 지갑을 wagmi에 통합",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -17,7 +17,7 @@
17
17
  "dist"
18
18
  ],
19
19
  "dependencies": {
20
- "@nexus-cross/crossx-sdk-core": "^2.1.1"
20
+ "@nexus-cross/crossx-sdk-core": "^2.2.0-beta.1"
21
21
  },
22
22
  "peerDependencies": {
23
23
  "@nexus-cross/crossx-sdk-core": "^2.0.0",