@nexus-cross/crossx-sdk-wagmi 2.2.0-beta.1 → 2.2.0-beta.3
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 +4 -0
- package/dist/connector.d.ts.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +149 -142
- package/package.json +2 -2
package/dist/connector.d.ts
CHANGED
|
@@ -13,6 +13,8 @@ export interface CROSSxConnectorOptions {
|
|
|
13
13
|
getStoredWalletAddress?: () => string | null;
|
|
14
14
|
/** 활성 지갑 주소 변경 시 호출되는 콜백. 미설정 시 localStorage 자동 사용. */
|
|
15
15
|
onWalletAddressChanged?: (address: string) => void;
|
|
16
|
+
/** disconnect/signOut 시 저장된 지갑 주소를 제거하는 콜백. 미설정 시 localStorage 자동 사용. */
|
|
17
|
+
clearStoredWalletAddress?: () => void;
|
|
16
18
|
/**
|
|
17
19
|
* 로그인 모달에서 외부 지갑 행 클릭 시 호출되는 콜백.
|
|
18
20
|
*
|
|
@@ -38,6 +40,8 @@ export type CrossxConnectorOptions = Omit<SDKConfig, 'useMockWallet'> & {
|
|
|
38
40
|
getStoredWalletAddress?: () => string | null;
|
|
39
41
|
/** 활성 지갑 주소 변경 시 호출되는 콜백. 미설정 시 localStorage 자동 사용. */
|
|
40
42
|
onWalletAddressChanged?: (address: string) => void;
|
|
43
|
+
/** disconnect/signOut 시 저장된 지갑 주소를 제거하는 콜백. 미설정 시 localStorage 자동 사용. */
|
|
44
|
+
clearStoredWalletAddress?: () => void;
|
|
41
45
|
/**
|
|
42
46
|
* 로그인 모달에서 외부 지갑 행 클릭 시 호출되는 콜백.
|
|
43
47
|
*
|
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,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AC,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,8GAyRpE"}
|
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 j=require("wagmi"),z=require("viem"),x=require("@nexus-cross/crossx-sdk-core");function F(s){return parseInt(s.split(":")[1]??"1",10)}function A(s){return`eip155:${s}`}function b(s){return"0x"+s.toString(16)}function N(s){const e=s.startsWith("0x")?s.slice(2):s,r=new Uint8Array(e.length/2);for(let n=0;n<r.length;n++)r[n]=parseInt(e.substring(n*2,n*2+2),16);return new TextDecoder().decode(r)}class G{constructor(e,r=x.ChainId.CROSS_MAINNET){this.sdk=e,this.listeners=new Map,this.walletIndex=0,this.currentAccounts=[],this.off=this.removeListener.bind(this),this.chainId=typeof r=="number"?r:F(r),this.sdk.on("addressChanged",({address:n,index:d})=>{this.setWalletIndex(d),this.notifyAccountsChanged([n])})}_setWalletIndexChangeCallback(e){this.walletIndexChangeCallback=e}async request({method:e,params:r}){try{return await this._handleRequest(e,r)}catch(n){throw n instanceof x.CROSSxError&&n.code===x.ErrorCode.USER_REJECTED?Object.assign(new Error("User rejected the request"),{code:4001,data:n.details}):n}}async _handleRequest(e,r){var d;const n=r??[];switch(e){case"eth_requestAccounts":case"eth_accounts":{if(this.currentAccounts.length>0)return[...this.currentAccounts];const t=await this.sdk.getAddress();return t?[t.address]:[]}case"eth_chainId":return b(this.chainId);case"net_version":return String(this.chainId);case"personal_sign":{const[t]=n,c=t.startsWith("0x")?N(t):t;return(await this.sdk.signMessage(A(this.chainId),c,{index:this.walletIndex})).signature}case"eth_sign":{const[,t]=n,c=t.startsWith("0x")?N(t):t;return(await this.sdk.signMessage(A(this.chainId),c,{index:this.walletIndex})).signature}case"eth_signTypedData":case"eth_signTypedData_v3":case"eth_signTypedData_v4":{const[,t]=n,c=typeof t=="string"?JSON.parse(t):t,l=(d=c==null?void 0:c.domain)==null?void 0:d.chainId,u=l==null||l===0,m=u?0:Number(l),f={index:this.walletIndex};return(u?await this.sdk.signTypedDataOffchain(c,f):await this.sdk.signTypedData(A(m),c,f)).signature}case"eth_sendTransaction":{const[t]=n,c=A(this.chainId);return(await this.sdk.sendTransaction(c,{from:t.from,to:t.to,value:t.value,data:t.data,gasLimit:t.gasLimit??t.gas,gasPrice:t.gasPrice,maxFeePerGas:t.maxFeePerGas,maxPriorityFeePerGas:t.maxPriorityFeePerGas,nonce:t.nonce!==void 0?parseInt(String(t.nonce),16):void 0,chainId:this.chainId},{index:this.walletIndex})).txHash}case"wallet_switchEthereumChain":{const[{chainId:t}]=n,c=parseInt(t,16);return c!==this.chainId&&(this.chainId=c,this.emit("chainChanged",b(c))),null}case"wallet_addEthereumChain":return null;default:return this.sdk.walletRpc(e,n,A(this.chainId))}}on(e,r){return this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e).add(r),this}removeListener(e,r){var n;return(n=this.listeners.get(e))==null||n.delete(r),this}emit(e,...r){var n;(n=this.listeners.get(e))==null||n.forEach(d=>d(...r))}notifyAccountsChanged(e){this.currentAccounts=e,this.emit("accountsChanged",e)}notifyDisconnect(e){this.currentAccounts=[],this.walletIndex=0,this.emit("disconnect",e)}getCurrentChainId(){return this.chainId}setChainId(e){e!==this.chainId&&(this.chainId=e,this.emit("chainChanged",b(e)))}setWalletIndex(e){var r;this.walletIndex=e,(r=this.walletIndexChangeCallback)==null||r.call(this,e)}getWalletIndex(){return this.walletIndex}getCurrentAccounts(){return[...this.currentAccounts]}}function K(s){return typeof s=="number"?s:parseInt(s.split(":")[1]??"1",10)}const k="crossx-wallet-index";function H(){try{const s=typeof window<"u"?localStorage.getItem(k):null;return s&&Number(s)||0}catch{return 0}}function J(s){try{typeof window<"u"&&localStorage.setItem(k,String(s))}catch{}}function q(){try{typeof window<"u"&&localStorage.removeItem(k)}catch{}}const v="crossx-wallet-address";function Y(){try{return typeof window<"u"?localStorage.getItem(v):null}catch{return null}}function $(s){try{typeof window<"u"&&localStorage.setItem(v,s)}catch{}}function Q(){try{typeof window<"u"&&localStorage.removeItem(v)}catch{}}function X(s){const{defaultChainId:e,getStoredWalletIndex:r,onWalletIndexChanged:n,getStoredWalletAddress:d,onWalletAddressChanged:t,clearStoredWalletAddress:c,openConnectOtherWallet:l,...u}=s;if(l){const f=u.connectOtherWallets,S=Array.isArray(f)&&f.length>0,y=u.showConnectOtherWallets===!0;!S&&!y&&(u.showConnectOtherWallets=!0)}const m=x.createCROSSxSDK(u);return M({sdk:m,defaultChainId:e,getStoredWalletIndex:r,onWalletIndexChanged:n,getStoredWalletAddress:d,onWalletAddressChanged:t,clearStoredWalletAddress:c,openConnectOtherWallet:l})}function M(s){const{sdk:e,defaultChainId:r=x.ChainId.CROSS_MAINNET,getStoredWalletIndex:n=H,onWalletIndexChanged:d=J,getStoredWalletAddress:t=Y,onWalletAddressChanged:c=$,clearStoredWalletAddress:l=Q,openConnectOtherWallet:u}=s,m=K(r);let f=null,S=!1,y=null,D=!1,W=!1,O=!1;function g(){return f||(f=new G(e,m),f._setWalletIndexChangeCallback(d)),f}return j.createConnector(C=>{function T(a){if(D)return;const h=g();h.on("accountsChanged",a.onAccountsChanged.bind(a)),h.on("chainChanged",a.onChainChanged.bind(a)),h.on("disconnect",a.onDisconnect.bind(a)),D=!0}async function P(){if(!S)return y||(y=(async()=>{const a=n(),h=t()??void 0;await e.initialize({preferredWalletIndex:a,preferredWalletAddress:h}),e.on("addressChanged",({address:i,index:I})=>{if(W)return;const E=g();E.setWalletIndex(I),i&&(E.notifyAccountsChanged([i]),c(i))}),e.on("authChanged",i=>{if(!(W||O))if(i.isAuthenticated&&i.address){const I=g();I.notifyAccountsChanged([i.address]),C.emitter.emit("connect",{accounts:[i.address],chainId:I.getCurrentChainId()})}else i.isAuthenticated||(g().notifyDisconnect(),q(),l(),d(0),C.emitter.emit("disconnect"))}),u&&e.on("connectExternalWallet",u),S=!0})().finally(()=>{y=null}),y)}return{id:"crossx",name:"CROSSx Wallet",type:"crossx",async setup(){await P(),T(this)},async connect({chainId:a,isReconnecting:h}={}){await P(),T(this);const i=g(),I=a??m;W=!0;try{if((h||e.isAuthenticated())&&e.isAuthenticated()){const o=await e.getAddress();if(o)return i.setWalletIndex(o.index),i.notifyAccountsChanged([o.address]),{accounts:[o.address],chainId:I};await e.signOut().catch(()=>{}),i.notifyDisconnect()}const E=t();let p;try{p=await e.signInWithCreate({preferredWalletAddress:E??void 0})}catch(o){throw o instanceof x.CROSSxError&&o.code===x.ErrorCode.EXTERNAL_WALLET_REQUESTED?(i.notifyDisconnect(),new z.UserRejectedRequestError(o)):(e.isAuthenticated()||(i.notifyDisconnect(),C.emitter.emit("disconnect")),o)}if(!p.success)throw new Error(p.error??"CROSSx sign-in failed");const w=p.address,L=p.addresses.map(o=>o.address).filter(o=>!w||o.toLowerCase()!==w.toLowerCase()),_=w?[w,...L]:L;if(_.length===0)throw new Error("No address found after sign-in");const R=p.addresses.find(o=>w&&o.address.toLowerCase()===w.toLowerCase()),U=(R==null?void 0:R.index)??0;return i.setWalletIndex(U),i.notifyAccountsChanged(_),w&&c(w),{accounts:_,chainId:I}}finally{W=!1}},async disconnect(){O=!0;try{const a=g();await e.signOut(),a.notifyDisconnect(),q(),l(),d(0),C.emitter.emit("disconnect")}finally{O=!1}},async getAccounts(){const h=g().getCurrentAccounts();if(h.length>0)return h;const i=await e.getAddress();return i?[i.address]:[]},async getChainId(){return g().getCurrentChainId()},async getProvider({chainId:a}={}){return g()},async isAuthorized(){return await e.whenReady(),e.isAuthenticated()},async switchChain({chainId:a}){g().setChainId(a),C.emitter.emit("change",{chainId:a});const i=C.chains.find(I=>I.id===a);if(!i)throw new Error(`Chain ${a} is not configured`);return i},onAccountsChanged(a){a.length===0?C.emitter.emit("disconnect"):C.emitter.emit("change",{accounts:a})},onChainChanged(a){C.emitter.emit("change",{chainId:Number(a)})},onDisconnect(){C.emitter.emit("disconnect")}}})}exports.CROSSxEIP1193Provider=G;exports.createCROSSxConnector=M;exports.crossxConnector=X;
|
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 z } from "wagmi";
|
|
2
|
+
import { UserRejectedRequestError as F } from "viem";
|
|
3
|
+
import { ChainId as G, CROSSxError as q, ErrorCode as U, createCROSSxSDK as j } from "@nexus-cross/crossx-sdk-core";
|
|
4
|
+
function K(s) {
|
|
5
5
|
return parseInt(s.split(":")[1] ?? "1", 10);
|
|
6
6
|
}
|
|
7
|
-
function
|
|
7
|
+
function A(s) {
|
|
8
8
|
return `eip155:${s}`;
|
|
9
9
|
}
|
|
10
|
-
function
|
|
10
|
+
function D(s) {
|
|
11
11
|
return "0x" + s.toString(16);
|
|
12
12
|
}
|
|
13
|
-
function
|
|
14
|
-
const t = s.startsWith("0x") ? s.slice(2) : s,
|
|
15
|
-
for (let n = 0; n <
|
|
16
|
-
|
|
17
|
-
return new TextDecoder().decode(
|
|
13
|
+
function L(s) {
|
|
14
|
+
const t = s.startsWith("0x") ? s.slice(2) : s, r = new Uint8Array(t.length / 2);
|
|
15
|
+
for (let n = 0; n < r.length; n++)
|
|
16
|
+
r[n] = parseInt(t.substring(n * 2, n * 2 + 2), 16);
|
|
17
|
+
return new TextDecoder().decode(r);
|
|
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 H {
|
|
20
|
+
constructor(t, r = G.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 r == "number" ? r : K(r), this.sdk.on("addressChanged", ({ address: n, index: o }) => {
|
|
22
|
+
this.setWalletIndex(o), this.notifyAccountsChanged([n]);
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
/** connector가 내부적으로 호출. 외부에서 사용하지 않습니다. */
|
|
@@ -27,19 +27,19 @@ class K {
|
|
|
27
27
|
this.walletIndexChangeCallback = t;
|
|
28
28
|
}
|
|
29
29
|
// ── EIP-1193 request ──────────────────────────────────────────────────────
|
|
30
|
-
async request({ method: t, params:
|
|
30
|
+
async request({ method: t, params: r }) {
|
|
31
31
|
try {
|
|
32
|
-
return await this._handleRequest(t,
|
|
32
|
+
return await this._handleRequest(t, r);
|
|
33
33
|
} catch (n) {
|
|
34
|
-
throw n instanceof
|
|
34
|
+
throw n instanceof q && n.code === U.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, r) {
|
|
41
|
+
var o;
|
|
42
|
+
const n = r ?? [];
|
|
43
43
|
switch (t) {
|
|
44
44
|
case "eth_requestAccounts":
|
|
45
45
|
case "eth_accounts": {
|
|
@@ -49,21 +49,21 @@ class K {
|
|
|
49
49
|
return e ? [e.address] : [];
|
|
50
50
|
}
|
|
51
51
|
case "eth_chainId":
|
|
52
|
-
return
|
|
52
|
+
return D(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") ? L(e) : e;
|
|
57
57
|
return (await this.sdk.signMessage(
|
|
58
|
-
|
|
58
|
+
A(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") ? L(e) : e;
|
|
65
65
|
return (await this.sdk.signMessage(
|
|
66
|
-
|
|
66
|
+
A(this.chainId),
|
|
67
67
|
c,
|
|
68
68
|
{ index: this.walletIndex }
|
|
69
69
|
)).signature;
|
|
@@ -71,11 +71,11 @@ class K {
|
|
|
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, l = (o = c == null ? void 0 : c.domain) == null ? void 0 : o.chainId, u = l == null || l === 0, p = u ? 0 : Number(l), f = { index: this.walletIndex };
|
|
75
|
+
return (u ? await this.sdk.signTypedDataOffchain(c, f) : await this.sdk.signTypedData(A(p), c, f)).signature;
|
|
76
76
|
}
|
|
77
77
|
case "eth_sendTransaction": {
|
|
78
|
-
const [e] = n, c =
|
|
78
|
+
const [e] = n, c = A(this.chainId);
|
|
79
79
|
return (await this.sdk.sendTransaction(c, {
|
|
80
80
|
from: e.from,
|
|
81
81
|
to: e.to,
|
|
@@ -91,25 +91,25 @@ class K {
|
|
|
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", D(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, 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, r) {
|
|
104
|
+
return this.listeners.has(t) || this.listeners.set(t, /* @__PURE__ */ new Set()), this.listeners.get(t).add(r), this;
|
|
105
105
|
}
|
|
106
|
-
removeListener(t,
|
|
106
|
+
removeListener(t, r) {
|
|
107
107
|
var n;
|
|
108
|
-
return (n = this.listeners.get(t)) == null || n.delete(
|
|
108
|
+
return (n = this.listeners.get(t)) == null || n.delete(r), this;
|
|
109
109
|
}
|
|
110
|
-
emit(t, ...
|
|
110
|
+
emit(t, ...r) {
|
|
111
111
|
var n;
|
|
112
|
-
(n = this.listeners.get(t)) == null || n.forEach((
|
|
112
|
+
(n = this.listeners.get(t)) == null || n.forEach((o) => o(...r));
|
|
113
113
|
}
|
|
114
114
|
// ── Connector 전용 헬퍼 ───────────────────────────────────────────────────
|
|
115
115
|
notifyAccountsChanged(t) {
|
|
@@ -122,15 +122,15 @@ class K {
|
|
|
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", D(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 r;
|
|
133
|
+
this.walletIndex = t, (r = this.walletIndexChangeCallback) == null || r.call(this, t);
|
|
134
134
|
}
|
|
135
135
|
getWalletIndex() {
|
|
136
136
|
return this.walletIndex;
|
|
@@ -139,201 +139,208 @@ class K {
|
|
|
139
139
|
return [...this.currentAccounts];
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
|
-
function
|
|
142
|
+
function J(s) {
|
|
143
143
|
return typeof s == "number" ? s : parseInt(s.split(":")[1] ?? "1", 10);
|
|
144
144
|
}
|
|
145
|
-
const
|
|
146
|
-
function
|
|
145
|
+
const R = "crossx-wallet-index";
|
|
146
|
+
function Y() {
|
|
147
147
|
try {
|
|
148
|
-
const s = typeof window < "u" ? localStorage.getItem(
|
|
148
|
+
const s = typeof window < "u" ? localStorage.getItem(R) : null;
|
|
149
149
|
return s && Number(s) || 0;
|
|
150
150
|
} catch {
|
|
151
151
|
return 0;
|
|
152
152
|
}
|
|
153
153
|
}
|
|
154
|
-
function
|
|
154
|
+
function $(s) {
|
|
155
155
|
try {
|
|
156
|
-
typeof window < "u" && localStorage.setItem(
|
|
156
|
+
typeof window < "u" && localStorage.setItem(R, String(s));
|
|
157
157
|
} catch {
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
|
-
function
|
|
160
|
+
function N() {
|
|
161
161
|
try {
|
|
162
|
-
typeof window < "u" && localStorage.removeItem(
|
|
162
|
+
typeof window < "u" && localStorage.removeItem(R);
|
|
163
163
|
} catch {
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
|
-
const
|
|
167
|
-
function
|
|
166
|
+
const k = "crossx-wallet-address";
|
|
167
|
+
function Q() {
|
|
168
168
|
try {
|
|
169
|
-
return typeof window < "u" ? localStorage.getItem(
|
|
169
|
+
return typeof window < "u" ? localStorage.getItem(k) : null;
|
|
170
170
|
} catch {
|
|
171
171
|
return null;
|
|
172
172
|
}
|
|
173
173
|
}
|
|
174
|
-
function
|
|
174
|
+
function X(s) {
|
|
175
|
+
try {
|
|
176
|
+
typeof window < "u" && localStorage.setItem(k, s);
|
|
177
|
+
} catch {
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function B() {
|
|
175
181
|
try {
|
|
176
|
-
typeof window < "u" && localStorage.
|
|
182
|
+
typeof window < "u" && localStorage.removeItem(k);
|
|
177
183
|
} catch {
|
|
178
184
|
}
|
|
179
185
|
}
|
|
180
|
-
function
|
|
181
|
-
const { defaultChainId: t, getStoredWalletIndex:
|
|
182
|
-
if (
|
|
183
|
-
const
|
|
184
|
-
!
|
|
186
|
+
function nt(s) {
|
|
187
|
+
const { defaultChainId: t, getStoredWalletIndex: r, onWalletIndexChanged: n, getStoredWalletAddress: o, onWalletAddressChanged: e, clearStoredWalletAddress: c, openConnectOtherWallet: l, ...u } = s;
|
|
188
|
+
if (l) {
|
|
189
|
+
const f = u.connectOtherWallets, x = Array.isArray(f) && f.length > 0, m = u.showConnectOtherWallets === !0;
|
|
190
|
+
!x && !m && (u.showConnectOtherWallets = !0);
|
|
185
191
|
}
|
|
186
|
-
const
|
|
187
|
-
return
|
|
192
|
+
const p = j(u);
|
|
193
|
+
return V({ sdk: p, defaultChainId: t, getStoredWalletIndex: r, onWalletIndexChanged: n, getStoredWalletAddress: o, onWalletAddressChanged: e, clearStoredWalletAddress: c, openConnectOtherWallet: l });
|
|
188
194
|
}
|
|
189
|
-
function
|
|
195
|
+
function V(s) {
|
|
190
196
|
const {
|
|
191
197
|
sdk: t,
|
|
192
|
-
defaultChainId:
|
|
193
|
-
getStoredWalletIndex: n =
|
|
194
|
-
onWalletIndexChanged:
|
|
195
|
-
getStoredWalletAddress: e =
|
|
196
|
-
onWalletAddressChanged: c =
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
198
|
+
defaultChainId: r = G.CROSS_MAINNET,
|
|
199
|
+
getStoredWalletIndex: n = Y,
|
|
200
|
+
onWalletIndexChanged: o = $,
|
|
201
|
+
getStoredWalletAddress: e = Q,
|
|
202
|
+
onWalletAddressChanged: c = X,
|
|
203
|
+
clearStoredWalletAddress: l = B,
|
|
204
|
+
openConnectOtherWallet: u
|
|
205
|
+
} = s, p = J(r);
|
|
206
|
+
let f = null, x = !1, m = null, b = !1, S = !1, E = !1;
|
|
207
|
+
function g() {
|
|
208
|
+
return f || (f = new H(t, p), f._setWalletIndexChangeCallback(o)), f;
|
|
202
209
|
}
|
|
203
|
-
return
|
|
204
|
-
function
|
|
205
|
-
if (
|
|
206
|
-
const
|
|
207
|
-
|
|
210
|
+
return z((C) => {
|
|
211
|
+
function T(a) {
|
|
212
|
+
if (b) return;
|
|
213
|
+
const h = g();
|
|
214
|
+
h.on("accountsChanged", a.onAccountsChanged.bind(a)), h.on("chainChanged", a.onChainChanged.bind(a)), h.on("disconnect", a.onDisconnect.bind(a)), b = !0;
|
|
208
215
|
}
|
|
209
|
-
async function
|
|
210
|
-
if (!
|
|
211
|
-
return
|
|
212
|
-
const
|
|
213
|
-
await t.initialize({ preferredWalletIndex:
|
|
214
|
-
if (
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
}), t.on("authChanged", (
|
|
218
|
-
if (!(
|
|
219
|
-
if (
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
accounts: [
|
|
223
|
-
chainId:
|
|
216
|
+
async function v() {
|
|
217
|
+
if (!x)
|
|
218
|
+
return m || (m = (async () => {
|
|
219
|
+
const a = n(), h = e() ?? void 0;
|
|
220
|
+
await t.initialize({ preferredWalletIndex: a, preferredWalletAddress: h }), t.on("addressChanged", ({ address: i, index: I }) => {
|
|
221
|
+
if (S) return;
|
|
222
|
+
const W = g();
|
|
223
|
+
W.setWalletIndex(I), i && (W.notifyAccountsChanged([i]), c(i));
|
|
224
|
+
}), t.on("authChanged", (i) => {
|
|
225
|
+
if (!(S || E))
|
|
226
|
+
if (i.isAuthenticated && i.address) {
|
|
227
|
+
const I = g();
|
|
228
|
+
I.notifyAccountsChanged([i.address]), C.emitter.emit("connect", {
|
|
229
|
+
accounts: [i.address],
|
|
230
|
+
chainId: I.getCurrentChainId()
|
|
224
231
|
});
|
|
225
|
-
} else
|
|
226
|
-
}),
|
|
232
|
+
} else i.isAuthenticated || (g().notifyDisconnect(), N(), l(), o(0), C.emitter.emit("disconnect"));
|
|
233
|
+
}), u && t.on("connectExternalWallet", u), x = !0;
|
|
227
234
|
})().finally(() => {
|
|
228
|
-
|
|
229
|
-
}),
|
|
235
|
+
m = null;
|
|
236
|
+
}), m);
|
|
230
237
|
}
|
|
231
238
|
return {
|
|
232
239
|
id: "crossx",
|
|
233
240
|
name: "CROSSx Wallet",
|
|
234
241
|
type: "crossx",
|
|
235
242
|
async setup() {
|
|
236
|
-
await
|
|
243
|
+
await v(), T(this);
|
|
237
244
|
},
|
|
238
245
|
// ── connect ─────────────────────────────────────────────────────────────
|
|
239
|
-
async connect({ chainId:
|
|
240
|
-
await
|
|
241
|
-
const
|
|
242
|
-
|
|
246
|
+
async connect({ chainId: a, isReconnecting: h } = {}) {
|
|
247
|
+
await v(), T(this);
|
|
248
|
+
const i = g(), I = a ?? p;
|
|
249
|
+
S = !0;
|
|
243
250
|
try {
|
|
244
|
-
if ((
|
|
251
|
+
if ((h || t.isAuthenticated()) && t.isAuthenticated()) {
|
|
245
252
|
const d = await t.getAddress();
|
|
246
253
|
if (d)
|
|
247
|
-
return
|
|
254
|
+
return i.setWalletIndex(d.index), i.notifyAccountsChanged([d.address]), {
|
|
248
255
|
accounts: [d.address],
|
|
249
|
-
chainId:
|
|
256
|
+
chainId: I
|
|
250
257
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
251
258
|
};
|
|
252
259
|
await t.signOut().catch(() => {
|
|
253
|
-
}),
|
|
260
|
+
}), i.notifyDisconnect();
|
|
254
261
|
}
|
|
255
|
-
const
|
|
262
|
+
const W = e();
|
|
256
263
|
let y;
|
|
257
264
|
try {
|
|
258
265
|
y = await t.signInWithCreate({
|
|
259
|
-
preferredWalletAddress:
|
|
266
|
+
preferredWalletAddress: W ?? void 0
|
|
260
267
|
});
|
|
261
268
|
} catch (d) {
|
|
262
|
-
throw d instanceof
|
|
269
|
+
throw d instanceof q && d.code === U.EXTERNAL_WALLET_REQUESTED ? (i.notifyDisconnect(), new F(d)) : (t.isAuthenticated() || (i.notifyDisconnect(), C.emitter.emit("disconnect")), d);
|
|
263
270
|
}
|
|
264
271
|
if (!y.success)
|
|
265
272
|
throw new Error(y.error ?? "CROSSx sign-in failed");
|
|
266
|
-
const w = y.address,
|
|
267
|
-
if (
|
|
273
|
+
const w = y.address, P = y.addresses.map((d) => d.address).filter((d) => !w || d.toLowerCase() !== w.toLowerCase()), _ = w ? [w, ...P] : P;
|
|
274
|
+
if (_.length === 0)
|
|
268
275
|
throw new Error("No address found after sign-in");
|
|
269
|
-
const
|
|
276
|
+
const O = y.addresses.find(
|
|
270
277
|
(d) => w && d.address.toLowerCase() === w.toLowerCase()
|
|
271
|
-
),
|
|
272
|
-
return
|
|
273
|
-
accounts:
|
|
274
|
-
chainId:
|
|
278
|
+
), M = (O == null ? void 0 : O.index) ?? 0;
|
|
279
|
+
return i.setWalletIndex(M), i.notifyAccountsChanged(_), w && c(w), {
|
|
280
|
+
accounts: _,
|
|
281
|
+
chainId: I
|
|
275
282
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
276
283
|
};
|
|
277
284
|
} finally {
|
|
278
|
-
|
|
285
|
+
S = !1;
|
|
279
286
|
}
|
|
280
287
|
},
|
|
281
288
|
// ── disconnect ──────────────────────────────────────────────────────────
|
|
282
289
|
async disconnect() {
|
|
283
|
-
|
|
290
|
+
E = !0;
|
|
284
291
|
try {
|
|
285
|
-
const
|
|
286
|
-
await t.signOut(),
|
|
292
|
+
const a = g();
|
|
293
|
+
await t.signOut(), a.notifyDisconnect(), N(), l(), o(0), C.emitter.emit("disconnect");
|
|
287
294
|
} finally {
|
|
288
|
-
|
|
295
|
+
E = !1;
|
|
289
296
|
}
|
|
290
297
|
},
|
|
291
298
|
// ── getAccounts ─────────────────────────────────────────────────────────
|
|
292
299
|
async getAccounts() {
|
|
293
|
-
const
|
|
294
|
-
if (
|
|
295
|
-
const
|
|
296
|
-
return
|
|
300
|
+
const h = g().getCurrentAccounts();
|
|
301
|
+
if (h.length > 0) return h;
|
|
302
|
+
const i = await t.getAddress();
|
|
303
|
+
return i ? [i.address] : [];
|
|
297
304
|
},
|
|
298
305
|
// ── getChainId ──────────────────────────────────────────────────────────
|
|
299
306
|
async getChainId() {
|
|
300
|
-
return
|
|
307
|
+
return g().getCurrentChainId();
|
|
301
308
|
},
|
|
302
309
|
// ── getProvider ─────────────────────────────────────────────────────────
|
|
303
|
-
async getProvider({ chainId:
|
|
304
|
-
return
|
|
310
|
+
async getProvider({ chainId: a } = {}) {
|
|
311
|
+
return g();
|
|
305
312
|
},
|
|
306
313
|
// ── isAuthorized ────────────────────────────────────────────────────────
|
|
307
314
|
async isAuthorized() {
|
|
308
315
|
return await t.whenReady(), t.isAuthenticated();
|
|
309
316
|
},
|
|
310
317
|
// ── switchChain ─────────────────────────────────────────────────────────
|
|
311
|
-
async switchChain({ chainId:
|
|
312
|
-
|
|
313
|
-
const
|
|
314
|
-
if (!
|
|
315
|
-
throw new Error(`Chain ${
|
|
316
|
-
return
|
|
318
|
+
async switchChain({ chainId: a }) {
|
|
319
|
+
g().setChainId(a), C.emitter.emit("change", { chainId: a });
|
|
320
|
+
const i = C.chains.find((I) => I.id === a);
|
|
321
|
+
if (!i)
|
|
322
|
+
throw new Error(`Chain ${a} is not configured`);
|
|
323
|
+
return i;
|
|
317
324
|
},
|
|
318
325
|
// ── 이벤트 핸들러 ───────────────────────────────────────────────────────
|
|
319
|
-
onAccountsChanged(
|
|
320
|
-
|
|
321
|
-
accounts:
|
|
326
|
+
onAccountsChanged(a) {
|
|
327
|
+
a.length === 0 ? C.emitter.emit("disconnect") : C.emitter.emit("change", {
|
|
328
|
+
accounts: a
|
|
322
329
|
});
|
|
323
330
|
},
|
|
324
|
-
onChainChanged(
|
|
325
|
-
|
|
326
|
-
chainId: Number(
|
|
331
|
+
onChainChanged(a) {
|
|
332
|
+
C.emitter.emit("change", {
|
|
333
|
+
chainId: Number(a)
|
|
327
334
|
});
|
|
328
335
|
},
|
|
329
336
|
onDisconnect() {
|
|
330
|
-
|
|
337
|
+
C.emitter.emit("disconnect");
|
|
331
338
|
}
|
|
332
339
|
};
|
|
333
340
|
});
|
|
334
341
|
}
|
|
335
342
|
export {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
343
|
+
H as CROSSxEIP1193Provider,
|
|
344
|
+
V as createCROSSxConnector,
|
|
345
|
+
nt as crossxConnector
|
|
339
346
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexus-cross/crossx-sdk-wagmi",
|
|
3
|
-
"version": "2.2.0-beta.
|
|
3
|
+
"version": "2.2.0-beta.3",
|
|
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.2.0-beta.
|
|
20
|
+
"@nexus-cross/crossx-sdk-core": "^2.2.0-beta.3"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"@nexus-cross/crossx-sdk-core": "^2.0.0",
|