@nexus-cross/crossx-sdk-wagmi 2.0.1-beta.2 → 2.0.2-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.
- package/dist/connector.d.ts +8 -0
- package/dist/connector.d.ts.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +160 -150
- package/package.json +2 -2
package/dist/connector.d.ts
CHANGED
|
@@ -9,6 +9,10 @@ export interface CROSSxConnectorOptions {
|
|
|
9
9
|
getStoredWalletIndex?: () => number;
|
|
10
10
|
/** 활성 지갑 인덱스 변경 시 호출되는 콜백. 미설정 시 localStorage 자동 사용. */
|
|
11
11
|
onWalletIndexChanged?: (index: number) => void;
|
|
12
|
+
/** 저장된 지갑 주소를 반환하는 콜백. 미설정 시 localStorage 자동 사용. */
|
|
13
|
+
getStoredWalletAddress?: () => string | null;
|
|
14
|
+
/** 활성 지갑 주소 변경 시 호출되는 콜백. 미설정 시 localStorage 자동 사용. */
|
|
15
|
+
onWalletAddressChanged?: (address: string) => void;
|
|
12
16
|
/**
|
|
13
17
|
* 로그인 모달에서 외부 지갑 행 클릭 시 호출되는 콜백.
|
|
14
18
|
*
|
|
@@ -30,6 +34,10 @@ export type CrossxConnectorOptions = Omit<SDKConfig, 'useMockWallet'> & {
|
|
|
30
34
|
getStoredWalletIndex?: () => number;
|
|
31
35
|
/** 활성 지갑 인덱스 변경 시 호출되는 콜백. 미설정 시 localStorage 자동 사용. */
|
|
32
36
|
onWalletIndexChanged?: (index: number) => void;
|
|
37
|
+
/** 저장된 지갑 주소를 반환하는 콜백. 미설정 시 localStorage 자동 사용. */
|
|
38
|
+
getStoredWalletAddress?: () => string | null;
|
|
39
|
+
/** 활성 지갑 주소 변경 시 호출되는 콜백. 미설정 시 localStorage 자동 사용. */
|
|
40
|
+
onWalletAddressChanged?: (address: string) => void;
|
|
33
41
|
/**
|
|
34
42
|
* 로그인 모달에서 외부 지갑 행 클릭 시 호출되는 콜백.
|
|
35
43
|
*
|
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;;;;;;;;;;;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;;;;;;;;;;;;;OAaG;IACH,sBAAsB,CAAC,EAAE,MAAM,IAAI,CAAC;CACrC,CAAC;
|
|
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"}
|
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 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;
|
package/dist/index.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
import { createConnector as N } from "wagmi";
|
|
2
|
-
import { UserRejectedRequestError as
|
|
3
|
-
import { ChainId as
|
|
4
|
-
function
|
|
5
|
-
return parseInt(
|
|
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) {
|
|
5
|
+
return parseInt(s.split(":")[1] ?? "1", 10);
|
|
6
6
|
}
|
|
7
|
-
function
|
|
8
|
-
return `eip155:${
|
|
7
|
+
function A(s) {
|
|
8
|
+
return `eip155:${s}`;
|
|
9
9
|
}
|
|
10
|
-
function
|
|
11
|
-
return "0x" +
|
|
10
|
+
function _(s) {
|
|
11
|
+
return "0x" + s.toString(16);
|
|
12
12
|
}
|
|
13
|
-
function
|
|
14
|
-
const t =
|
|
15
|
-
for (let n = 0; n <
|
|
16
|
-
|
|
17
|
-
return new TextDecoder().decode(
|
|
13
|
+
function R(s) {
|
|
14
|
+
const t = s.startsWith("0x") ? s.slice(2) : s, i = new Uint8Array(t.length / 2);
|
|
15
|
+
for (let n = 0; n < i.length; n++)
|
|
16
|
+
i[n] = parseInt(t.substring(n * 2, n * 2 + 2), 16);
|
|
17
|
+
return new TextDecoder().decode(i);
|
|
18
18
|
}
|
|
19
|
-
class
|
|
20
|
-
constructor(t,
|
|
21
|
-
this.sdk = t, this.listeners = /* @__PURE__ */ new Map(), this.walletIndex = 0, this.currentAccounts = [], this.off = this.removeListener.bind(this), this.chainId = typeof
|
|
22
|
-
this.setWalletIndex(
|
|
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]);
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
/** connector가 내부적으로 호출. 외부에서 사용하지 않습니다. */
|
|
@@ -27,19 +27,19 @@ class U {
|
|
|
27
27
|
this.walletIndexChangeCallback = t;
|
|
28
28
|
}
|
|
29
29
|
// ── EIP-1193 request ──────────────────────────────────────────────────────
|
|
30
|
-
async request({ method: t, params:
|
|
30
|
+
async request({ method: t, params: i }) {
|
|
31
31
|
try {
|
|
32
|
-
return await this._handleRequest(t,
|
|
32
|
+
return await this._handleRequest(t, i);
|
|
33
33
|
} catch (n) {
|
|
34
|
-
throw n instanceof k && n.code ===
|
|
34
|
+
throw n instanceof k && n.code === v.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
|
-
async _handleRequest(t,
|
|
41
|
-
var
|
|
42
|
-
const n =
|
|
40
|
+
async _handleRequest(t, i) {
|
|
41
|
+
var l;
|
|
42
|
+
const n = i ?? [];
|
|
43
43
|
switch (t) {
|
|
44
44
|
case "eth_requestAccounts":
|
|
45
45
|
case "eth_accounts": {
|
|
@@ -49,21 +49,21 @@ class U {
|
|
|
49
49
|
return e ? [e.address] : [];
|
|
50
50
|
}
|
|
51
51
|
case "eth_chainId":
|
|
52
|
-
return
|
|
52
|
+
return _(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") ?
|
|
56
|
+
const [e] = n, r = e.startsWith("0x") ? R(e) : e;
|
|
57
57
|
return (await this.sdk.signMessage(
|
|
58
|
-
|
|
58
|
+
A(this.chainId),
|
|
59
59
|
r,
|
|
60
60
|
{ index: this.walletIndex }
|
|
61
61
|
)).signature;
|
|
62
62
|
}
|
|
63
63
|
case "eth_sign": {
|
|
64
|
-
const [, e] = n, r = e.startsWith("0x") ?
|
|
64
|
+
const [, e] = n, r = e.startsWith("0x") ? R(e) : e;
|
|
65
65
|
return (await this.sdk.signMessage(
|
|
66
|
-
|
|
66
|
+
A(this.chainId),
|
|
67
67
|
r,
|
|
68
68
|
{ index: this.walletIndex }
|
|
69
69
|
)).signature;
|
|
@@ -71,11 +71,11 @@ class U {
|
|
|
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,
|
|
75
|
-
return (
|
|
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;
|
|
76
76
|
}
|
|
77
77
|
case "eth_sendTransaction": {
|
|
78
|
-
const [e] = n, r =
|
|
78
|
+
const [e] = n, r = A(this.chainId);
|
|
79
79
|
return (await this.sdk.sendTransaction(r, {
|
|
80
80
|
from: e.from,
|
|
81
81
|
to: e.to,
|
|
@@ -91,25 +91,25 @@ class U {
|
|
|
91
91
|
}
|
|
92
92
|
case "wallet_switchEthereumChain": {
|
|
93
93
|
const [{ chainId: e }] = n, r = parseInt(e, 16);
|
|
94
|
-
return r !== this.chainId && (this.chainId = r, this.emit("chainChanged",
|
|
94
|
+
return r !== this.chainId && (this.chainId = r, this.emit("chainChanged", _(r))), 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, A(this.chainId));
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
// ── EIP-1193 이벤트 ───────────────────────────────────────────────────────
|
|
103
|
-
on(t,
|
|
104
|
-
return this.listeners.has(t) || this.listeners.set(t, /* @__PURE__ */ new Set()), this.listeners.get(t).add(
|
|
103
|
+
on(t, i) {
|
|
104
|
+
return this.listeners.has(t) || this.listeners.set(t, /* @__PURE__ */ new Set()), this.listeners.get(t).add(i), this;
|
|
105
105
|
}
|
|
106
|
-
removeListener(t,
|
|
106
|
+
removeListener(t, i) {
|
|
107
107
|
var n;
|
|
108
|
-
return (n = this.listeners.get(t)) == null || n.delete(
|
|
108
|
+
return (n = this.listeners.get(t)) == null || n.delete(i), this;
|
|
109
109
|
}
|
|
110
|
-
emit(t, ...
|
|
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((l) => l(...i));
|
|
113
113
|
}
|
|
114
114
|
// ── Connector 전용 헬퍼 ───────────────────────────────────────────────────
|
|
115
115
|
notifyAccountsChanged(t) {
|
|
@@ -122,15 +122,15 @@ class U {
|
|
|
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", _(t)));
|
|
126
126
|
}
|
|
127
127
|
/**
|
|
128
128
|
* 활성 지갑 인덱스 설정.
|
|
129
129
|
* Provider가 SDK 메서드를 호출할 때 이 인덱스를 전달합니다.
|
|
130
130
|
*/
|
|
131
131
|
setWalletIndex(t) {
|
|
132
|
-
var
|
|
133
|
-
this.walletIndex = t, (
|
|
132
|
+
var i;
|
|
133
|
+
this.walletIndex = t, (i = this.walletIndexChangeCallback) == null || i.call(this, t);
|
|
134
134
|
}
|
|
135
135
|
getWalletIndex() {
|
|
136
136
|
return this.walletIndex;
|
|
@@ -139,179 +139,189 @@ class U {
|
|
|
139
139
|
return [...this.currentAccounts];
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
|
-
function
|
|
143
|
-
return typeof
|
|
142
|
+
function F(s) {
|
|
143
|
+
return typeof s == "number" ? s : parseInt(s.split(":")[1] ?? "1", 10);
|
|
144
144
|
}
|
|
145
|
-
const
|
|
146
|
-
function
|
|
145
|
+
const O = "crossx-wallet-index";
|
|
146
|
+
function j() {
|
|
147
147
|
try {
|
|
148
|
-
const
|
|
149
|
-
return
|
|
148
|
+
const s = typeof window < "u" ? localStorage.getItem(O) : null;
|
|
149
|
+
return s && Number(s) || 0;
|
|
150
150
|
} catch {
|
|
151
151
|
return 0;
|
|
152
152
|
}
|
|
153
153
|
}
|
|
154
|
-
function
|
|
154
|
+
function z(s) {
|
|
155
155
|
try {
|
|
156
|
-
typeof window < "u" && localStorage.setItem(
|
|
156
|
+
typeof window < "u" && localStorage.setItem(O, String(s));
|
|
157
157
|
} catch {
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
|
-
function
|
|
160
|
+
function b() {
|
|
161
161
|
try {
|
|
162
|
-
typeof window < "u" && localStorage.removeItem(
|
|
162
|
+
typeof window < "u" && localStorage.removeItem(O);
|
|
163
163
|
} catch {
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
166
|
+
const P = "crossx-wallet-address";
|
|
167
|
+
function K() {
|
|
168
|
+
try {
|
|
169
|
+
return typeof window < "u" ? localStorage.getItem(P) : null;
|
|
170
|
+
} catch {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function H(s) {
|
|
175
|
+
try {
|
|
176
|
+
typeof window < "u" && localStorage.setItem(P, s);
|
|
177
|
+
} catch {
|
|
178
|
+
}
|
|
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);
|
|
171
185
|
}
|
|
172
|
-
const
|
|
173
|
-
return
|
|
186
|
+
const w = q(c);
|
|
187
|
+
return J({ sdk: w, defaultChainId: t, getStoredWalletIndex: i, onWalletIndexChanged: n, getStoredWalletAddress: l, onWalletAddressChanged: e, openConnectOtherWallet: r });
|
|
174
188
|
}
|
|
175
|
-
function
|
|
189
|
+
function J(s) {
|
|
176
190
|
const {
|
|
177
191
|
sdk: t,
|
|
178
|
-
defaultChainId:
|
|
179
|
-
getStoredWalletIndex: n =
|
|
180
|
-
onWalletIndexChanged:
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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;
|
|
200
|
+
function u() {
|
|
201
|
+
return g || (g = new M(t, w), g._setWalletIndexChangeCallback(l)), g;
|
|
186
202
|
}
|
|
187
|
-
return N((
|
|
203
|
+
return N((f) => ({
|
|
188
204
|
id: "crossx",
|
|
189
205
|
name: "CROSSx Wallet",
|
|
190
206
|
type: "crossx",
|
|
191
207
|
async setup() {
|
|
192
|
-
if (!
|
|
193
|
-
const
|
|
194
|
-
await t.initialize({ preferredWalletIndex:
|
|
195
|
-
if (
|
|
196
|
-
const
|
|
197
|
-
|
|
198
|
-
}), t.on("authChanged", (
|
|
199
|
-
if (!(
|
|
200
|
-
if (
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
accounts: [
|
|
204
|
-
chainId:
|
|
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()
|
|
205
221
|
});
|
|
206
|
-
} else
|
|
207
|
-
}),
|
|
222
|
+
} else h.isAuthenticated || (u().notifyDisconnect(), b(), l(0), f.emitter.emit("disconnect"));
|
|
223
|
+
}), c && t.on("connectExternalWallet", c);
|
|
208
224
|
}
|
|
209
|
-
const
|
|
210
|
-
|
|
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));
|
|
211
227
|
},
|
|
212
228
|
// ── connect ─────────────────────────────────────────────────────────────
|
|
213
|
-
async connect({ chainId:
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
f = !0;
|
|
229
|
+
async connect({ chainId: a, isReconnecting: x } = {}) {
|
|
230
|
+
const o = u(), h = a ?? w;
|
|
231
|
+
p = !0;
|
|
217
232
|
try {
|
|
218
|
-
if (
|
|
219
|
-
const
|
|
220
|
-
if (
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
} catch {
|
|
232
|
-
}
|
|
233
|
-
await t.signOut().catch(() => {
|
|
234
|
-
}), a.notifyDisconnect();
|
|
235
|
-
}
|
|
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();
|
|
236
243
|
}
|
|
237
|
-
|
|
244
|
+
const y = e();
|
|
245
|
+
let C;
|
|
238
246
|
try {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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);
|
|
242
252
|
}
|
|
243
|
-
if (!
|
|
244
|
-
throw new Error(
|
|
245
|
-
const
|
|
246
|
-
if (
|
|
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)
|
|
247
257
|
throw new Error("No address found after sign-in");
|
|
248
|
-
const
|
|
249
|
-
(
|
|
250
|
-
)
|
|
251
|
-
return
|
|
252
|
-
accounts:
|
|
253
|
-
chainId:
|
|
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
|
|
254
264
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
255
265
|
};
|
|
256
266
|
} finally {
|
|
257
|
-
|
|
267
|
+
p = !1;
|
|
258
268
|
}
|
|
259
269
|
},
|
|
260
270
|
// ── disconnect ──────────────────────────────────────────────────────────
|
|
261
271
|
async disconnect() {
|
|
262
|
-
|
|
272
|
+
S = !0;
|
|
263
273
|
try {
|
|
264
|
-
const
|
|
265
|
-
await t.signOut(),
|
|
274
|
+
const a = u();
|
|
275
|
+
await t.signOut(), a.notifyDisconnect(), b(), l(0), f.emitter.emit("disconnect");
|
|
266
276
|
} finally {
|
|
267
|
-
|
|
277
|
+
S = !1;
|
|
268
278
|
}
|
|
269
279
|
},
|
|
270
280
|
// ── getAccounts ─────────────────────────────────────────────────────────
|
|
271
281
|
async getAccounts() {
|
|
272
|
-
const
|
|
273
|
-
if (
|
|
274
|
-
const
|
|
275
|
-
return
|
|
282
|
+
const x = u().getCurrentAccounts();
|
|
283
|
+
if (x.length > 0) return x;
|
|
284
|
+
const o = await t.getAddress();
|
|
285
|
+
return o ? [o.address] : [];
|
|
276
286
|
},
|
|
277
287
|
// ── getChainId ──────────────────────────────────────────────────────────
|
|
278
288
|
async getChainId() {
|
|
279
|
-
return
|
|
289
|
+
return u().getCurrentChainId();
|
|
280
290
|
},
|
|
281
291
|
// ── getProvider ─────────────────────────────────────────────────────────
|
|
282
|
-
async getProvider({ chainId:
|
|
283
|
-
return
|
|
292
|
+
async getProvider({ chainId: a } = {}) {
|
|
293
|
+
return u();
|
|
284
294
|
},
|
|
285
295
|
// ── isAuthorized ────────────────────────────────────────────────────────
|
|
286
296
|
async isAuthorized() {
|
|
287
297
|
return await t.whenReady(), t.isAuthenticated();
|
|
288
298
|
},
|
|
289
299
|
// ── switchChain ─────────────────────────────────────────────────────────
|
|
290
|
-
async switchChain({ chainId:
|
|
291
|
-
|
|
292
|
-
const
|
|
293
|
-
if (!
|
|
294
|
-
throw new Error(`Chain ${
|
|
295
|
-
return
|
|
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;
|
|
296
306
|
},
|
|
297
307
|
// ── 이벤트 핸들러 ───────────────────────────────────────────────────────
|
|
298
|
-
onAccountsChanged(
|
|
299
|
-
|
|
300
|
-
accounts:
|
|
308
|
+
onAccountsChanged(a) {
|
|
309
|
+
a.length === 0 ? f.emitter.emit("disconnect") : f.emitter.emit("change", {
|
|
310
|
+
accounts: a
|
|
301
311
|
});
|
|
302
312
|
},
|
|
303
|
-
onChainChanged(
|
|
304
|
-
|
|
305
|
-
chainId: Number(
|
|
313
|
+
onChainChanged(a) {
|
|
314
|
+
f.emitter.emit("change", {
|
|
315
|
+
chainId: Number(a)
|
|
306
316
|
});
|
|
307
317
|
},
|
|
308
318
|
onDisconnect() {
|
|
309
|
-
|
|
319
|
+
f.emitter.emit("disconnect");
|
|
310
320
|
}
|
|
311
321
|
}));
|
|
312
322
|
}
|
|
313
323
|
export {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
324
|
+
M as CROSSxEIP1193Provider,
|
|
325
|
+
J as createCROSSxConnector,
|
|
326
|
+
X as crossxConnector
|
|
317
327
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexus-cross/crossx-sdk-wagmi",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2-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.0.
|
|
20
|
+
"@nexus-cross/crossx-sdk-core": "2.0.2-beta.1"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"viem": ">=2.0.0",
|