@lydianpay/lydianconnect 0.0.0-managed → 1.0.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/LICENSE +21 -0
- package/README.md +50 -0
- package/dist/index.cjs +307 -217
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +32 -163
- package/dist/index.d.ts +32 -163
- package/dist/index.js +308 -213
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -2,20 +2,26 @@ import SignClient from '@walletconnect/sign-client';
|
|
|
2
2
|
import { getSdkError } from '@walletconnect/utils';
|
|
3
3
|
|
|
4
4
|
// src/core/manager.ts
|
|
5
|
-
var
|
|
6
|
-
constructor(
|
|
5
|
+
var ConnectManager = class {
|
|
6
|
+
constructor(connectors) {
|
|
7
7
|
this.byWallet = /* @__PURE__ */ new Map();
|
|
8
8
|
this.connections = /* @__PURE__ */ new Map();
|
|
9
9
|
this.handlers = /* @__PURE__ */ new Set();
|
|
10
10
|
this.unsubs = [];
|
|
11
11
|
this.started = false;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
const fallbacks = connectors.filter((c) => c.walletIds.length === 0);
|
|
13
|
+
if (fallbacks.length !== 1) {
|
|
14
|
+
throw new Error(
|
|
15
|
+
`LydianConnect requires exactly one fallback connector (empty walletIds); found ${fallbacks.length}`
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
this.fallback = fallbacks[0];
|
|
19
|
+
this.all = connectors;
|
|
20
|
+
for (const connector of connectors) {
|
|
15
21
|
for (const id of connector.walletIds) this.byWallet.set(id, connector);
|
|
16
22
|
}
|
|
17
23
|
}
|
|
18
|
-
/** Wire connector event forwarding, once.
|
|
24
|
+
/** Wire connector event forwarding, once. */
|
|
19
25
|
async init() {
|
|
20
26
|
if (this.started) return;
|
|
21
27
|
this.started = true;
|
|
@@ -23,18 +29,17 @@ var LydianConnect = class {
|
|
|
23
29
|
this.unsubs.push(connector.on((event) => this.handle(event)));
|
|
24
30
|
}
|
|
25
31
|
}
|
|
26
|
-
/** SDK connector if one claims this walletId, else the
|
|
32
|
+
/** SDK connector if one claims this walletId, else the fallback. The host app is oblivious. */
|
|
27
33
|
resolve(walletId) {
|
|
28
34
|
return this.byWallet.get(walletId) ?? this.fallback;
|
|
29
35
|
}
|
|
30
|
-
async connect(
|
|
36
|
+
async connect(req) {
|
|
31
37
|
if (!this.started) await this.init();
|
|
32
|
-
const connection = await this.resolve(
|
|
33
|
-
this.connections.set(
|
|
34
|
-
this.emit({ type: "connect", walletId:
|
|
38
|
+
const connection = await this.resolve(req.walletId).connect(req);
|
|
39
|
+
this.connections.set(req.walletId, connection);
|
|
40
|
+
this.emit({ type: "connect", walletId: req.walletId, connection });
|
|
35
41
|
return connection;
|
|
36
42
|
}
|
|
37
|
-
/** Resume any live sessions across all connectors (e.g. after a page reload). */
|
|
38
43
|
async restore() {
|
|
39
44
|
if (!this.started) await this.init();
|
|
40
45
|
const restored = [];
|
|
@@ -60,7 +65,6 @@ var LydianConnect = class {
|
|
|
60
65
|
await Promise.allSettled([...this.connections.values()].map((c) => c.disconnect()));
|
|
61
66
|
this.connections.clear();
|
|
62
67
|
}
|
|
63
|
-
/** Disconnect everything and clear all connector-held state. */
|
|
64
68
|
async reset() {
|
|
65
69
|
await this.disconnect();
|
|
66
70
|
await Promise.allSettled(this.all.map((c) => c.reset()));
|
|
@@ -78,6 +82,28 @@ var LydianConnect = class {
|
|
|
78
82
|
}
|
|
79
83
|
};
|
|
80
84
|
|
|
85
|
+
// src/core/namespace.ts
|
|
86
|
+
var DEFAULT_EVM_METHODS = [
|
|
87
|
+
"eth_sendTransaction",
|
|
88
|
+
"personal_sign",
|
|
89
|
+
"eth_signTypedData_v4",
|
|
90
|
+
"wallet_switchEthereumChain"
|
|
91
|
+
];
|
|
92
|
+
var DEFAULT_EVM_EVENTS = ["chainChanged", "accountsChanged"];
|
|
93
|
+
function buildNamespace(chainId) {
|
|
94
|
+
const caip = typeof chainId === "number" ? `eip155:${chainId}` : chainId;
|
|
95
|
+
const [name, reference] = caip.split(":");
|
|
96
|
+
if (name !== "eip155" || !reference || !/^\d+$/.test(reference) || Number(reference) <= 0) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
`Unsupported chain "${caip}". lydianconnect is EVM-only for now \u2014 pass a positive number or an "eip155:<id>" string.`
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
name,
|
|
103
|
+
value: { chains: [caip], methods: DEFAULT_EVM_METHODS, events: DEFAULT_EVM_EVENTS }
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
81
107
|
// src/core/connector.ts
|
|
82
108
|
var BaseConnector = class {
|
|
83
109
|
constructor() {
|
|
@@ -98,6 +124,227 @@ var BaseConnector = class {
|
|
|
98
124
|
}
|
|
99
125
|
};
|
|
100
126
|
|
|
127
|
+
// src/core/app.ts
|
|
128
|
+
function deriveUrl() {
|
|
129
|
+
return typeof window !== "undefined" ? window.location.origin : "";
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// src/connectors/metamask/connector.ts
|
|
133
|
+
var WALLET_ID = "metamask";
|
|
134
|
+
var MetaMaskConnector = class extends BaseConnector {
|
|
135
|
+
constructor(app) {
|
|
136
|
+
super();
|
|
137
|
+
this.app = app;
|
|
138
|
+
this.type = "sdk";
|
|
139
|
+
this.walletIds = [WALLET_ID];
|
|
140
|
+
this.sdk = null;
|
|
141
|
+
this.provider = null;
|
|
142
|
+
this.listenersBound = false;
|
|
143
|
+
}
|
|
144
|
+
isAvailable() {
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
async getProvider() {
|
|
148
|
+
if (this.provider) return this.provider;
|
|
149
|
+
const { MetaMaskSDK } = await import('@metamask/sdk');
|
|
150
|
+
const dappMetadata = {
|
|
151
|
+
name: this.app.appName,
|
|
152
|
+
url: deriveUrl(),
|
|
153
|
+
...this.app.icon ? { iconUrl: this.app.icon } : {}
|
|
154
|
+
};
|
|
155
|
+
const sdk = new MetaMaskSDK({ dappMetadata });
|
|
156
|
+
await sdk.init();
|
|
157
|
+
const provider = sdk.getProvider();
|
|
158
|
+
if (!provider) throw new Error("MetaMask provider unavailable");
|
|
159
|
+
this.sdk = sdk;
|
|
160
|
+
this.provider = provider;
|
|
161
|
+
this.bindEvents(provider);
|
|
162
|
+
return provider;
|
|
163
|
+
}
|
|
164
|
+
bindEvents(provider) {
|
|
165
|
+
if (this.listenersBound) return;
|
|
166
|
+
this.listenersBound = true;
|
|
167
|
+
provider.on("chainChanged", (hex) => {
|
|
168
|
+
this.emit({ type: "chainChanged", walletId: WALLET_ID, chainId: toCaip(String(hex)) });
|
|
169
|
+
});
|
|
170
|
+
provider.on("accountsChanged", (accounts) => {
|
|
171
|
+
const account = Array.isArray(accounts) ? accounts[0] : void 0;
|
|
172
|
+
if (account) this.emit({ type: "accountsChanged", walletId: WALLET_ID, account });
|
|
173
|
+
else this.emit({ type: "disconnect", walletId: WALLET_ID });
|
|
174
|
+
});
|
|
175
|
+
provider.on("disconnect", () => this.emit({ type: "disconnect", walletId: WALLET_ID }));
|
|
176
|
+
}
|
|
177
|
+
async connect(req) {
|
|
178
|
+
const provider = await this.getProvider();
|
|
179
|
+
const accounts = await provider.request({ method: "eth_requestAccounts" });
|
|
180
|
+
const account = accounts?.[0];
|
|
181
|
+
if (!account) throw new Error("MetaMask returned no account");
|
|
182
|
+
await this.ensureChain(provider, req);
|
|
183
|
+
const chainId = toCaip(await provider.request({ method: "eth_chainId" }));
|
|
184
|
+
return this.toConnection(provider, account, chainId);
|
|
185
|
+
}
|
|
186
|
+
async restore() {
|
|
187
|
+
const provider = await this.getProvider().catch(() => null);
|
|
188
|
+
if (!provider) return [];
|
|
189
|
+
const accounts = await provider.request({ method: "eth_accounts" }).catch(() => []);
|
|
190
|
+
const account = accounts?.[0];
|
|
191
|
+
if (!account) return [];
|
|
192
|
+
const chainId = toCaip(
|
|
193
|
+
await provider.request({ method: "eth_chainId" }).catch(() => "0x1")
|
|
194
|
+
);
|
|
195
|
+
return [this.toConnection(provider, account, chainId)];
|
|
196
|
+
}
|
|
197
|
+
async reset() {
|
|
198
|
+
await this.sdk?.terminate();
|
|
199
|
+
this.provider = null;
|
|
200
|
+
this.listenersBound = false;
|
|
201
|
+
}
|
|
202
|
+
/** Best-effort switch to the requested chain; leaves the wallet as-is if it's unknown to it. */
|
|
203
|
+
async ensureChain(provider, req) {
|
|
204
|
+
const wanted = req.namespace.value.chains?.[0];
|
|
205
|
+
const wantedNum = wanted ? Number(wanted.split(":")[1]) : NaN;
|
|
206
|
+
if (!Number.isFinite(wantedNum) || wantedNum <= 0) return;
|
|
207
|
+
const current = parseInt(
|
|
208
|
+
await provider.request({ method: "eth_chainId" }).catch(() => "0x0") ?? "0x0",
|
|
209
|
+
16
|
|
210
|
+
);
|
|
211
|
+
if (current === wantedNum) return;
|
|
212
|
+
await provider.request({
|
|
213
|
+
method: "wallet_switchEthereumChain",
|
|
214
|
+
params: [{ chainId: `0x${wantedNum.toString(16)}` }]
|
|
215
|
+
}).catch(() => {
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
toConnection(provider, account, chainId) {
|
|
219
|
+
return {
|
|
220
|
+
walletId: WALLET_ID,
|
|
221
|
+
account,
|
|
222
|
+
chainId,
|
|
223
|
+
request: async (args) => await provider.request({ method: args.method, params: args.params }),
|
|
224
|
+
disconnect: async () => {
|
|
225
|
+
await this.sdk?.terminate();
|
|
226
|
+
this.provider = null;
|
|
227
|
+
this.listenersBound = false;
|
|
228
|
+
this.emit({ type: "disconnect", walletId: WALLET_ID });
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
function toCaip(hexChainId) {
|
|
234
|
+
if (!hexChainId) return "eip155:1";
|
|
235
|
+
const num = hexChainId.startsWith("0x") ? parseInt(hexChainId, 16) : Number(hexChainId);
|
|
236
|
+
return `eip155:${Number.isFinite(num) ? num : 1}`;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// src/connectors/coinbase/connector.ts
|
|
240
|
+
function rpc(provider, method, params) {
|
|
241
|
+
return provider.request({ method, params });
|
|
242
|
+
}
|
|
243
|
+
var WALLET_ID2 = "coinbase";
|
|
244
|
+
var CoinbaseConnector = class extends BaseConnector {
|
|
245
|
+
constructor(app) {
|
|
246
|
+
super();
|
|
247
|
+
this.app = app;
|
|
248
|
+
this.type = "sdk";
|
|
249
|
+
this.walletIds = [WALLET_ID2];
|
|
250
|
+
this.provider = null;
|
|
251
|
+
this.listenersBound = false;
|
|
252
|
+
}
|
|
253
|
+
isAvailable() {
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
256
|
+
async getProvider(appChainIds) {
|
|
257
|
+
if (this.provider) return this.provider;
|
|
258
|
+
const { CoinbaseWalletSDK } = await import('@coinbase/wallet-sdk');
|
|
259
|
+
const sdk = new CoinbaseWalletSDK({
|
|
260
|
+
appName: this.app.appName,
|
|
261
|
+
appLogoUrl: this.app.icon,
|
|
262
|
+
appChainIds
|
|
263
|
+
// one-time SDK-init hint; ensureChain enforces the actual chain per connect
|
|
264
|
+
});
|
|
265
|
+
const provider = sdk.makeWeb3Provider();
|
|
266
|
+
this.provider = provider;
|
|
267
|
+
this.bindEvents(provider);
|
|
268
|
+
return provider;
|
|
269
|
+
}
|
|
270
|
+
bindEvents(provider) {
|
|
271
|
+
if (this.listenersBound) return;
|
|
272
|
+
this.listenersBound = true;
|
|
273
|
+
provider.on("chainChanged", (hex) => {
|
|
274
|
+
this.emit({ type: "chainChanged", walletId: WALLET_ID2, chainId: toCaip2(String(hex)) });
|
|
275
|
+
});
|
|
276
|
+
provider.on("accountsChanged", (accounts) => {
|
|
277
|
+
const account = Array.isArray(accounts) ? accounts[0] : void 0;
|
|
278
|
+
if (account) this.emit({ type: "accountsChanged", walletId: WALLET_ID2, account });
|
|
279
|
+
else this.emit({ type: "disconnect", walletId: WALLET_ID2 });
|
|
280
|
+
});
|
|
281
|
+
provider.on("disconnect", () => this.emit({ type: "disconnect", walletId: WALLET_ID2 }));
|
|
282
|
+
}
|
|
283
|
+
async connect(req) {
|
|
284
|
+
const provider = await this.getProvider(evmChainIds(req));
|
|
285
|
+
const accounts = await rpc(provider, "eth_requestAccounts");
|
|
286
|
+
const account = accounts?.[0];
|
|
287
|
+
if (!account) throw new Error("Coinbase Wallet returned no account");
|
|
288
|
+
await this.ensureChain(provider, req);
|
|
289
|
+
const chainId = toCaip2(await rpc(provider, "eth_chainId"));
|
|
290
|
+
return this.toConnection(provider, account, chainId);
|
|
291
|
+
}
|
|
292
|
+
async restore() {
|
|
293
|
+
const provider = await this.getProvider().catch(() => null);
|
|
294
|
+
if (!provider) return [];
|
|
295
|
+
const accounts = await rpc(provider, "eth_accounts").catch(() => []);
|
|
296
|
+
const account = accounts?.[0];
|
|
297
|
+
if (!account) return [];
|
|
298
|
+
const chainId = toCaip2(await rpc(provider, "eth_chainId").catch(() => "0x1"));
|
|
299
|
+
return [this.toConnection(provider, account, chainId)];
|
|
300
|
+
}
|
|
301
|
+
async reset() {
|
|
302
|
+
await this.provider?.disconnect().catch(() => {
|
|
303
|
+
});
|
|
304
|
+
this.provider = null;
|
|
305
|
+
this.listenersBound = false;
|
|
306
|
+
}
|
|
307
|
+
async ensureChain(provider, req) {
|
|
308
|
+
const wanted = req.namespace.value.chains?.[0];
|
|
309
|
+
const wantedNum = wanted ? Number(wanted.split(":")[1]) : NaN;
|
|
310
|
+
if (!Number.isFinite(wantedNum) || wantedNum <= 0) return;
|
|
311
|
+
const current = parseInt(
|
|
312
|
+
await rpc(provider, "eth_chainId").catch(() => "0x0") ?? "0x0",
|
|
313
|
+
16
|
|
314
|
+
);
|
|
315
|
+
if (current === wantedNum) return;
|
|
316
|
+
await rpc(provider, "wallet_switchEthereumChain", [{ chainId: `0x${wantedNum.toString(16)}` }]).catch(
|
|
317
|
+
() => {
|
|
318
|
+
}
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
toConnection(provider, account, chainId) {
|
|
322
|
+
return {
|
|
323
|
+
walletId: WALLET_ID2,
|
|
324
|
+
account,
|
|
325
|
+
chainId,
|
|
326
|
+
request: (args) => rpc(provider, args.method, args.params),
|
|
327
|
+
disconnect: async () => {
|
|
328
|
+
await provider.disconnect().catch(() => {
|
|
329
|
+
});
|
|
330
|
+
this.provider = null;
|
|
331
|
+
this.listenersBound = false;
|
|
332
|
+
this.emit({ type: "disconnect", walletId: WALLET_ID2 });
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
function toCaip2(hexChainId) {
|
|
338
|
+
if (!hexChainId) return "eip155:1";
|
|
339
|
+
const num = hexChainId.startsWith("0x") ? parseInt(hexChainId, 16) : Number(hexChainId);
|
|
340
|
+
return `eip155:${Number.isFinite(num) ? num : 1}`;
|
|
341
|
+
}
|
|
342
|
+
function evmChainIds(req) {
|
|
343
|
+
const wanted = req.namespace.value.chains?.[0];
|
|
344
|
+
const n = wanted ? Number(wanted.split(":")[1]) : NaN;
|
|
345
|
+
return Number.isFinite(n) && n > 0 ? [n] : void 0;
|
|
346
|
+
}
|
|
347
|
+
|
|
101
348
|
// src/connectors/walletconnect/wallet-links.generated.ts
|
|
102
349
|
var GENERATED_WALLET_LINKS = {
|
|
103
350
|
metamask: { "scheme": "metamask://wc", "universal": "https://metamask.app.link/wc", "store": { "ios": "https://apps.apple.com/us/app/metamask/id1438144202", "android": "https://play.google.com/store/apps/details?id=io.metamask" } },
|
|
@@ -190,9 +437,10 @@ function detectAppOpen(timeoutMs) {
|
|
|
190
437
|
// src/connectors/walletconnect/connector.ts
|
|
191
438
|
var STORAGE_KEY = "lydianconnect.wc.topics";
|
|
192
439
|
var WalletConnectConnector = class extends BaseConnector {
|
|
193
|
-
constructor(
|
|
440
|
+
constructor(app, options) {
|
|
194
441
|
super();
|
|
195
|
-
this.
|
|
442
|
+
this.app = app;
|
|
443
|
+
this.options = options;
|
|
196
444
|
this.type = "walletconnect";
|
|
197
445
|
this.walletIds = [];
|
|
198
446
|
// claims any wallet not served by an SDK connector
|
|
@@ -200,7 +448,7 @@ var WalletConnectConnector = class extends BaseConnector {
|
|
|
200
448
|
this.topics = /* @__PURE__ */ new Map();
|
|
201
449
|
this.warm = null;
|
|
202
450
|
this.warming = null;
|
|
203
|
-
this.links = { ...DEFAULT_WALLET_LINKS, ...
|
|
451
|
+
this.links = { ...DEFAULT_WALLET_LINKS, ...options.walletLinks ?? {} };
|
|
204
452
|
}
|
|
205
453
|
isAvailable() {
|
|
206
454
|
return true;
|
|
@@ -208,9 +456,14 @@ var WalletConnectConnector = class extends BaseConnector {
|
|
|
208
456
|
async getClient() {
|
|
209
457
|
if (this.client) return this.client;
|
|
210
458
|
const client = await SignClient.init({
|
|
211
|
-
projectId: this.
|
|
212
|
-
metadata:
|
|
213
|
-
|
|
459
|
+
projectId: this.options.projectId,
|
|
460
|
+
metadata: {
|
|
461
|
+
name: this.app.appName,
|
|
462
|
+
description: this.app.description ?? this.app.appName,
|
|
463
|
+
url: deriveUrl(),
|
|
464
|
+
icons: this.app.icon ? [this.app.icon] : []
|
|
465
|
+
},
|
|
466
|
+
relayUrl: this.options.relayUrl
|
|
214
467
|
});
|
|
215
468
|
client.on("session_delete", ({ topic }) => {
|
|
216
469
|
const walletId = this.walletForTopic(topic);
|
|
@@ -260,7 +513,7 @@ var WalletConnectConnector = class extends BaseConnector {
|
|
|
260
513
|
const link = this.links[req.walletId];
|
|
261
514
|
if (link) {
|
|
262
515
|
openWallet(link, warm.uri);
|
|
263
|
-
void detectAppOpen(this.
|
|
516
|
+
void detectAppOpen(this.options.openTimeoutMs ?? 2e3).then((opened) => {
|
|
264
517
|
if (!opened) this.emit({ type: "wallet_open_failed", walletId: req.walletId, uri: warm.uri });
|
|
265
518
|
});
|
|
266
519
|
}
|
|
@@ -399,211 +652,53 @@ function withSignal(promise, signal, onAbort) {
|
|
|
399
652
|
});
|
|
400
653
|
}
|
|
401
654
|
|
|
402
|
-
// src/
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
isAvailable() {
|
|
415
|
-
return true;
|
|
416
|
-
}
|
|
417
|
-
async getProvider() {
|
|
418
|
-
if (this.provider) return this.provider;
|
|
419
|
-
const { MetaMaskSDK } = await import('@metamask/sdk');
|
|
420
|
-
const sdk = new MetaMaskSDK({
|
|
421
|
-
dappMetadata: this.config.dappMetadata,
|
|
422
|
-
...this.config.sdkOptions
|
|
423
|
-
});
|
|
424
|
-
await sdk.init();
|
|
425
|
-
const provider = sdk.getProvider();
|
|
426
|
-
if (!provider) throw new Error("MetaMask provider unavailable");
|
|
427
|
-
this.sdk = sdk;
|
|
428
|
-
this.provider = provider;
|
|
429
|
-
this.bindEvents(provider);
|
|
430
|
-
return provider;
|
|
431
|
-
}
|
|
432
|
-
bindEvents(provider) {
|
|
433
|
-
if (this.listenersBound) return;
|
|
434
|
-
this.listenersBound = true;
|
|
435
|
-
provider.on("chainChanged", (hex) => {
|
|
436
|
-
this.emit({ type: "chainChanged", walletId: WALLET_ID, chainId: toCaip(String(hex)) });
|
|
437
|
-
});
|
|
438
|
-
provider.on("accountsChanged", (accounts) => {
|
|
439
|
-
const account = Array.isArray(accounts) ? accounts[0] : void 0;
|
|
440
|
-
if (account) this.emit({ type: "accountsChanged", walletId: WALLET_ID, account });
|
|
441
|
-
else this.emit({ type: "disconnect", walletId: WALLET_ID });
|
|
442
|
-
});
|
|
443
|
-
provider.on("disconnect", () => this.emit({ type: "disconnect", walletId: WALLET_ID }));
|
|
444
|
-
}
|
|
445
|
-
async connect(req) {
|
|
446
|
-
const provider = await this.getProvider();
|
|
447
|
-
const accounts = await provider.request({ method: "eth_requestAccounts" });
|
|
448
|
-
const account = accounts?.[0];
|
|
449
|
-
if (!account) throw new Error("MetaMask returned no account");
|
|
450
|
-
await this.ensureChain(provider, req);
|
|
451
|
-
const chainId = toCaip(await provider.request({ method: "eth_chainId" }));
|
|
452
|
-
return this.toConnection(provider, account, chainId);
|
|
453
|
-
}
|
|
454
|
-
async restore() {
|
|
455
|
-
const provider = await this.getProvider().catch(() => null);
|
|
456
|
-
if (!provider) return [];
|
|
457
|
-
const accounts = await provider.request({ method: "eth_accounts" }).catch(() => []);
|
|
458
|
-
const account = accounts?.[0];
|
|
459
|
-
if (!account) return [];
|
|
460
|
-
const chainId = toCaip(
|
|
461
|
-
await provider.request({ method: "eth_chainId" }).catch(() => "0x1")
|
|
462
|
-
);
|
|
463
|
-
return [this.toConnection(provider, account, chainId)];
|
|
464
|
-
}
|
|
465
|
-
async reset() {
|
|
466
|
-
await this.sdk?.terminate();
|
|
467
|
-
this.provider = null;
|
|
468
|
-
this.listenersBound = false;
|
|
469
|
-
}
|
|
470
|
-
/** Best-effort switch to the requested chain; leaves the wallet as-is if it's unknown to it. */
|
|
471
|
-
async ensureChain(provider, req) {
|
|
472
|
-
const wanted = req.namespace.value.chains?.[0];
|
|
473
|
-
const wantedNum = wanted ? Number(wanted.split(":")[1]) : NaN;
|
|
474
|
-
if (!Number.isFinite(wantedNum) || wantedNum <= 0) return;
|
|
475
|
-
const current = parseInt(
|
|
476
|
-
await provider.request({ method: "eth_chainId" }).catch(() => "0x0") ?? "0x0",
|
|
477
|
-
16
|
|
478
|
-
);
|
|
479
|
-
if (current === wantedNum) return;
|
|
480
|
-
await provider.request({
|
|
481
|
-
method: "wallet_switchEthereumChain",
|
|
482
|
-
params: [{ chainId: `0x${wantedNum.toString(16)}` }]
|
|
483
|
-
}).catch(() => {
|
|
484
|
-
});
|
|
485
|
-
}
|
|
486
|
-
toConnection(provider, account, chainId) {
|
|
487
|
-
return {
|
|
488
|
-
walletId: WALLET_ID,
|
|
489
|
-
account,
|
|
490
|
-
chainId,
|
|
491
|
-
request: async (args) => await provider.request({ method: args.method, params: args.params }),
|
|
492
|
-
disconnect: async () => {
|
|
493
|
-
await this.sdk?.terminate();
|
|
494
|
-
this.provider = null;
|
|
495
|
-
this.listenersBound = false;
|
|
496
|
-
this.emit({ type: "disconnect", walletId: WALLET_ID });
|
|
497
|
-
}
|
|
498
|
-
};
|
|
499
|
-
}
|
|
500
|
-
};
|
|
501
|
-
function toCaip(hexChainId) {
|
|
502
|
-
if (!hexChainId) return "eip155:1";
|
|
503
|
-
const num = hexChainId.startsWith("0x") ? parseInt(hexChainId, 16) : Number(hexChainId);
|
|
504
|
-
return `eip155:${Number.isFinite(num) ? num : 1}`;
|
|
655
|
+
// src/core/lydian-connect.ts
|
|
656
|
+
function createConnectors(config) {
|
|
657
|
+
const app = {
|
|
658
|
+
appName: config.appName,
|
|
659
|
+
icon: config.icon,
|
|
660
|
+
description: config.description
|
|
661
|
+
};
|
|
662
|
+
return [
|
|
663
|
+
new MetaMaskConnector(app),
|
|
664
|
+
new CoinbaseConnector(app),
|
|
665
|
+
new WalletConnectConnector(app, { projectId: config.walletConnectProjectId })
|
|
666
|
+
];
|
|
505
667
|
}
|
|
506
|
-
|
|
507
|
-
// src/connectors/coinbase/connector.ts
|
|
508
|
-
function rpc(provider, method, params) {
|
|
509
|
-
return provider.request({ method, params });
|
|
510
|
-
}
|
|
511
|
-
var WALLET_ID2 = "coinbase";
|
|
512
|
-
var CoinbaseConnector = class extends BaseConnector {
|
|
668
|
+
var LydianConnect = class {
|
|
513
669
|
constructor(config) {
|
|
514
|
-
|
|
515
|
-
this.config = config;
|
|
516
|
-
this.type = "sdk";
|
|
517
|
-
this.walletIds = [WALLET_ID2];
|
|
518
|
-
this.provider = null;
|
|
519
|
-
this.listenersBound = false;
|
|
670
|
+
this.manager = new ConnectManager(createConnectors(config));
|
|
520
671
|
}
|
|
521
|
-
|
|
522
|
-
return
|
|
672
|
+
init() {
|
|
673
|
+
return this.manager.init();
|
|
523
674
|
}
|
|
524
|
-
async
|
|
525
|
-
if (
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
675
|
+
async connect(input) {
|
|
676
|
+
if (input.chainId === void 0 || input.chainId === null) {
|
|
677
|
+
throw new Error('connect() requires a chainId (a number like 137 or an "eip155:<id>" string)');
|
|
678
|
+
}
|
|
679
|
+
return this.manager.connect({
|
|
680
|
+
walletId: input.walletId,
|
|
681
|
+
namespace: buildNamespace(input.chainId),
|
|
682
|
+
signal: input.signal
|
|
531
683
|
});
|
|
532
|
-
const provider = sdk.makeWeb3Provider();
|
|
533
|
-
this.provider = provider;
|
|
534
|
-
this.bindEvents(provider);
|
|
535
|
-
return provider;
|
|
536
684
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
this.listenersBound = true;
|
|
540
|
-
provider.on("chainChanged", (hex) => {
|
|
541
|
-
this.emit({ type: "chainChanged", walletId: WALLET_ID2, chainId: toCaip2(String(hex)) });
|
|
542
|
-
});
|
|
543
|
-
provider.on("accountsChanged", (accounts) => {
|
|
544
|
-
const account = Array.isArray(accounts) ? accounts[0] : void 0;
|
|
545
|
-
if (account) this.emit({ type: "accountsChanged", walletId: WALLET_ID2, account });
|
|
546
|
-
else this.emit({ type: "disconnect", walletId: WALLET_ID2 });
|
|
547
|
-
});
|
|
548
|
-
provider.on("disconnect", () => this.emit({ type: "disconnect", walletId: WALLET_ID2 }));
|
|
685
|
+
restore() {
|
|
686
|
+
return this.manager.restore();
|
|
549
687
|
}
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
const accounts = await rpc(provider, "eth_requestAccounts");
|
|
553
|
-
const account = accounts?.[0];
|
|
554
|
-
if (!account) throw new Error("Coinbase Wallet returned no account");
|
|
555
|
-
await this.ensureChain(provider, req);
|
|
556
|
-
const chainId = toCaip2(await rpc(provider, "eth_chainId"));
|
|
557
|
-
return this.toConnection(provider, account, chainId);
|
|
688
|
+
disconnect(walletId) {
|
|
689
|
+
return this.manager.disconnect(walletId);
|
|
558
690
|
}
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
if (!provider) return [];
|
|
562
|
-
const accounts = await rpc(provider, "eth_accounts").catch(() => []);
|
|
563
|
-
const account = accounts?.[0];
|
|
564
|
-
if (!account) return [];
|
|
565
|
-
const chainId = toCaip2(await rpc(provider, "eth_chainId").catch(() => "0x1"));
|
|
566
|
-
return [this.toConnection(provider, account, chainId)];
|
|
691
|
+
reset() {
|
|
692
|
+
return this.manager.reset();
|
|
567
693
|
}
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
});
|
|
571
|
-
this.provider = null;
|
|
572
|
-
this.listenersBound = false;
|
|
573
|
-
}
|
|
574
|
-
async ensureChain(provider, req) {
|
|
575
|
-
const wanted = req.namespace.value.chains?.[0];
|
|
576
|
-
const wantedNum = wanted ? Number(wanted.split(":")[1]) : NaN;
|
|
577
|
-
if (!Number.isFinite(wantedNum) || wantedNum <= 0) return;
|
|
578
|
-
const current = parseInt(await rpc(provider, "eth_chainId").catch(() => "0x0"), 16);
|
|
579
|
-
if (current === wantedNum) return;
|
|
580
|
-
await rpc(provider, "wallet_switchEthereumChain", [{ chainId: `0x${wantedNum.toString(16)}` }]).catch(
|
|
581
|
-
() => {
|
|
582
|
-
}
|
|
583
|
-
);
|
|
694
|
+
get(walletId) {
|
|
695
|
+
return this.manager.get(walletId);
|
|
584
696
|
}
|
|
585
|
-
|
|
586
|
-
return
|
|
587
|
-
walletId: WALLET_ID2,
|
|
588
|
-
account,
|
|
589
|
-
chainId,
|
|
590
|
-
request: (args) => rpc(provider, args.method, args.params),
|
|
591
|
-
disconnect: async () => {
|
|
592
|
-
await provider.disconnect().catch(() => {
|
|
593
|
-
});
|
|
594
|
-
this.provider = null;
|
|
595
|
-
this.listenersBound = false;
|
|
596
|
-
this.emit({ type: "disconnect", walletId: WALLET_ID2 });
|
|
597
|
-
}
|
|
598
|
-
};
|
|
697
|
+
on(handler) {
|
|
698
|
+
return this.manager.on(handler);
|
|
599
699
|
}
|
|
600
700
|
};
|
|
601
|
-
function toCaip2(hexChainId) {
|
|
602
|
-
if (!hexChainId) return "eip155:1";
|
|
603
|
-
const num = hexChainId.startsWith("0x") ? parseInt(hexChainId, 16) : Number(hexChainId);
|
|
604
|
-
return `eip155:${Number.isFinite(num) ? num : 1}`;
|
|
605
|
-
}
|
|
606
701
|
|
|
607
|
-
export {
|
|
702
|
+
export { LydianConnect };
|
|
608
703
|
//# sourceMappingURL=index.js.map
|
|
609
704
|
//# sourceMappingURL=index.js.map
|