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