@canton-network/dapp-sdk 0.24.0 → 0.26.0
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/README.md +15 -10
- package/dist/adapter/extension-adapter.d.ts +12 -1
- package/dist/adapter/extension-adapter.d.ts.map +1 -1
- package/dist/adapter/index.d.ts +1 -0
- package/dist/adapter/index.d.ts.map +1 -1
- package/dist/adapter/injected-adapter.d.ts +26 -0
- package/dist/adapter/injected-adapter.d.ts.map +1 -0
- package/dist/adapter/remote-adapter.d.ts.map +1 -1
- package/dist/announce-discovery.d.ts +17 -0
- package/dist/announce-discovery.d.ts.map +1 -0
- package/dist/client.d.ts +5 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/dapp-api/rpc-gen/index.d.ts +3 -0
- package/dist/dapp-api/rpc-gen/index.d.ts.map +1 -1
- package/dist/dapp-api/rpc-gen/typings.d.ts +27 -7
- package/dist/dapp-api/rpc-gen/typings.d.ts.map +1 -1
- package/dist/index.cjs +393 -69
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +392 -73
- package/dist/index.js.map +1 -1
- package/dist/injected-discovery.d.ts +9 -0
- package/dist/injected-discovery.d.ts.map +1 -0
- package/dist/sdk-controller.d.ts +1 -0
- package/dist/sdk-controller.d.ts.map +1 -1
- package/dist/sdk.d.ts +20 -2
- package/dist/sdk.d.ts.map +1 -1
- package/dist/util.d.ts.map +1 -1
- package/package.json +16 -10
package/dist/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { injectProvider, DappAsyncProvider
|
|
2
|
-
import { WalletEvent, DiscoverResult } from '@canton-network/core-types';
|
|
3
|
-
import { pickWallet, popup } from '@canton-network/core-wallet-ui-components';
|
|
1
|
+
import { DappSyncProvider, injectProvider, DappAsyncProvider } from '@canton-network/core-provider-dapp';
|
|
2
|
+
import { CANTON_ANNOUNCE_PROVIDER_EVENT, CANTON_REQUEST_PROVIDER_EVENT, WalletEvent, DiscoverResult } from '@canton-network/core-types';
|
|
3
|
+
import { pickWallet, notifyWalletPickerConnected, notifyWalletPickerError, waitForWalletPickerRetrySelection, popup } from '@canton-network/core-wallet-ui-components';
|
|
4
4
|
import { DiscoveryClient } from '@canton-network/core-wallet-discovery';
|
|
5
5
|
export { DiscoveryClient, DiscoveryError, EventEmitter, NotConnectedError, SessionExpiredError, TimeoutError, UserRejectedError, WalletNotFoundError, WalletNotInstalledError } from '@canton-network/core-wallet-discovery';
|
|
6
|
+
import { WindowTransport } from '@canton-network/core-rpc-transport';
|
|
6
7
|
import * as coreWalletDappRpcClient from '@canton-network/core-wallet-dapp-rpc-client';
|
|
7
8
|
export { coreWalletDappRpcClient as dappAPI };
|
|
8
9
|
|
|
@@ -55,7 +56,6 @@ var removeKernelSession = () => {
|
|
|
55
56
|
var clearAllLocalState = ({
|
|
56
57
|
closePopup
|
|
57
58
|
} = {}) => {
|
|
58
|
-
window.canton = void 0;
|
|
59
59
|
removeKernelSession();
|
|
60
60
|
removeKernelDiscovery();
|
|
61
61
|
if (closePopup) {
|
|
@@ -107,6 +107,9 @@ var DappClient = class {
|
|
|
107
107
|
onAccountsChanged(listener) {
|
|
108
108
|
this.provider.on("accountsChanged", listener);
|
|
109
109
|
}
|
|
110
|
+
onConnected(listener) {
|
|
111
|
+
this.provider.on("connected", listener);
|
|
112
|
+
}
|
|
110
113
|
onTxChanged(listener) {
|
|
111
114
|
this.provider.on("txChanged", listener);
|
|
112
115
|
}
|
|
@@ -119,6 +122,9 @@ var DappClient = class {
|
|
|
119
122
|
listener
|
|
120
123
|
);
|
|
121
124
|
}
|
|
125
|
+
removeOnConnected(listener) {
|
|
126
|
+
this.provider.removeListener("connected", listener);
|
|
127
|
+
}
|
|
122
128
|
removeOnTxChanged(listener) {
|
|
123
129
|
this.provider.removeListener("txChanged", listener);
|
|
124
130
|
}
|
|
@@ -131,7 +137,8 @@ var DappClient = class {
|
|
|
131
137
|
window.postMessage(
|
|
132
138
|
{
|
|
133
139
|
type: WalletEvent.SPLICE_WALLET_EXT_OPEN,
|
|
134
|
-
url: userUrl
|
|
140
|
+
url: userUrl,
|
|
141
|
+
target: this.options.target
|
|
135
142
|
},
|
|
136
143
|
"*"
|
|
137
144
|
);
|
|
@@ -147,22 +154,33 @@ var DappClient = class {
|
|
|
147
154
|
clearAllLocalState({ closePopup: true });
|
|
148
155
|
}
|
|
149
156
|
}
|
|
157
|
+
async isConnected() {
|
|
158
|
+
return this.provider.request({ method: "isConnected" });
|
|
159
|
+
}
|
|
150
160
|
};
|
|
151
161
|
var BROWSER_PROVIDER_ID = "browser";
|
|
152
162
|
var EXTENSION_DETECT_TIMEOUT_MS = 2e3;
|
|
153
163
|
var ExtensionAdapter = class {
|
|
154
|
-
constructor() {
|
|
155
|
-
__publicField(this, "providerId"
|
|
156
|
-
__publicField(this, "name"
|
|
164
|
+
constructor(config = {}) {
|
|
165
|
+
__publicField(this, "providerId");
|
|
166
|
+
__publicField(this, "name");
|
|
157
167
|
__publicField(this, "type", "browser");
|
|
158
168
|
__publicField(this, "icon");
|
|
169
|
+
__publicField(this, "description");
|
|
170
|
+
__publicField(this, "target");
|
|
171
|
+
this.providerId = config.providerId ?? BROWSER_PROVIDER_ID;
|
|
172
|
+
this.name = config.name ?? "Browser Extension";
|
|
173
|
+
this.icon = config.icon;
|
|
174
|
+
this.description = config.description ?? "Connect via the Splice Wallet browser extension";
|
|
175
|
+
this.target = config.target;
|
|
159
176
|
}
|
|
160
177
|
getInfo() {
|
|
161
178
|
return {
|
|
162
179
|
providerId: this.providerId,
|
|
163
180
|
name: this.name,
|
|
164
181
|
type: this.type,
|
|
165
|
-
description:
|
|
182
|
+
description: this.description,
|
|
183
|
+
icon: this.icon
|
|
166
184
|
};
|
|
167
185
|
}
|
|
168
186
|
async detect() {
|
|
@@ -173,7 +191,7 @@ var ExtensionAdapter = class {
|
|
|
173
191
|
resolve(false);
|
|
174
192
|
}, EXTENSION_DETECT_TIMEOUT_MS);
|
|
175
193
|
const handler = (event) => {
|
|
176
|
-
if (event.data?.type === WalletEvent.SPLICE_WALLET_EXT_ACK) {
|
|
194
|
+
if (event.data?.type === WalletEvent.SPLICE_WALLET_EXT_ACK && (!this.target || event.data?.target === this.target)) {
|
|
177
195
|
clearTimeout(timeout);
|
|
178
196
|
window.removeEventListener("message", handler);
|
|
179
197
|
resolve(true);
|
|
@@ -181,24 +199,88 @@ var ExtensionAdapter = class {
|
|
|
181
199
|
};
|
|
182
200
|
window.addEventListener("message", handler);
|
|
183
201
|
window.postMessage(
|
|
184
|
-
{
|
|
202
|
+
{
|
|
203
|
+
type: WalletEvent.SPLICE_WALLET_EXT_READY,
|
|
204
|
+
target: this.target
|
|
205
|
+
},
|
|
185
206
|
"*"
|
|
186
207
|
);
|
|
187
208
|
});
|
|
188
209
|
}
|
|
189
210
|
provider() {
|
|
211
|
+
if (this.target) {
|
|
212
|
+
return new DappSyncProvider(
|
|
213
|
+
new WindowTransport(window, { target: this.target })
|
|
214
|
+
);
|
|
215
|
+
}
|
|
190
216
|
return new DappSyncProvider();
|
|
191
217
|
}
|
|
192
218
|
teardown() {
|
|
193
219
|
}
|
|
194
220
|
async restore() {
|
|
195
|
-
|
|
221
|
+
const kernel = getKernelDiscovery();
|
|
222
|
+
const kernelMatches = kernel?.walletType === "extension" && (kernel.providerId === void 0 || kernel.providerId === this.providerId);
|
|
223
|
+
const provider = this.provider();
|
|
224
|
+
if (kernelMatches) {
|
|
225
|
+
try {
|
|
226
|
+
await provider.request({ method: "connect" });
|
|
227
|
+
} catch {
|
|
228
|
+
}
|
|
229
|
+
}
|
|
196
230
|
try {
|
|
197
|
-
const provider = new DappSyncProvider();
|
|
198
231
|
const status2 = await provider.request({ method: "status" });
|
|
199
232
|
if (status2.connection.isConnected) {
|
|
200
233
|
return provider;
|
|
201
234
|
}
|
|
235
|
+
} catch {
|
|
236
|
+
if (kernelMatches) {
|
|
237
|
+
return provider;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
// src/adapter/injected-adapter.ts
|
|
245
|
+
var InjectedAdapter = class {
|
|
246
|
+
constructor(config) {
|
|
247
|
+
__publicField(this, "providerId");
|
|
248
|
+
__publicField(this, "name");
|
|
249
|
+
__publicField(this, "type", "browser");
|
|
250
|
+
__publicField(this, "icon");
|
|
251
|
+
__publicField(this, "providerInstance");
|
|
252
|
+
__publicField(this, "description");
|
|
253
|
+
this.providerId = `browser:${config.id}`;
|
|
254
|
+
this.name = config.name;
|
|
255
|
+
this.icon = config.icon;
|
|
256
|
+
this.providerInstance = config.provider;
|
|
257
|
+
this.description = config.description;
|
|
258
|
+
}
|
|
259
|
+
getInfo() {
|
|
260
|
+
return {
|
|
261
|
+
providerId: this.providerId,
|
|
262
|
+
name: this.name,
|
|
263
|
+
type: this.type,
|
|
264
|
+
description: this.description ?? "Connect via an injected CIP-103 provider (window namespace)",
|
|
265
|
+
icon: this.icon
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
async detect() {
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
provider() {
|
|
272
|
+
return this.providerInstance;
|
|
273
|
+
}
|
|
274
|
+
teardown() {
|
|
275
|
+
}
|
|
276
|
+
async restore() {
|
|
277
|
+
try {
|
|
278
|
+
const status2 = await this.providerInstance.request({
|
|
279
|
+
method: "status"
|
|
280
|
+
});
|
|
281
|
+
if (status2.connection.isConnected) {
|
|
282
|
+
return this.providerInstance;
|
|
283
|
+
}
|
|
202
284
|
} catch {
|
|
203
285
|
}
|
|
204
286
|
return null;
|
|
@@ -211,6 +293,7 @@ function buildController(methods) {
|
|
|
211
293
|
status: methods.status,
|
|
212
294
|
connect: methods.connect,
|
|
213
295
|
disconnect: methods.disconnect,
|
|
296
|
+
isConnected: methods.isConnected,
|
|
214
297
|
getActiveNetwork: methods.getActiveNetwork,
|
|
215
298
|
prepareExecute: methods.prepareExecute,
|
|
216
299
|
prepareExecuteAndWait: methods.prepareExecuteAndWait,
|
|
@@ -270,6 +353,11 @@ var dappSDKController = (provider) => rpc_gen_default({
|
|
|
270
353
|
method: "disconnect"
|
|
271
354
|
});
|
|
272
355
|
},
|
|
356
|
+
isConnected: async () => {
|
|
357
|
+
return await provider.request({
|
|
358
|
+
method: "isConnected"
|
|
359
|
+
});
|
|
360
|
+
},
|
|
273
361
|
ledgerApi: async (params) => provider.request({
|
|
274
362
|
method: "ledgerApi",
|
|
275
363
|
params
|
|
@@ -337,17 +425,16 @@ var dappSDKController = (provider) => rpc_gen_default({
|
|
|
337
425
|
txChanged: async () => {
|
|
338
426
|
throw new Error("Only for events.");
|
|
339
427
|
},
|
|
340
|
-
getActiveNetwork:
|
|
341
|
-
|
|
342
|
-
},
|
|
343
|
-
signMessage:
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
}
|
|
428
|
+
getActiveNetwork: async () => provider.request({
|
|
429
|
+
method: "getActiveNetwork"
|
|
430
|
+
}),
|
|
431
|
+
signMessage: async (params) => provider.request({
|
|
432
|
+
method: "signMessage",
|
|
433
|
+
params
|
|
434
|
+
}),
|
|
435
|
+
getPrimaryAccount: async () => provider.request({
|
|
436
|
+
method: "getPrimaryAccount"
|
|
437
|
+
})
|
|
351
438
|
});
|
|
352
439
|
|
|
353
440
|
// src/adapter/remote-adapter.ts
|
|
@@ -373,7 +460,8 @@ var RemoteAdapter = class {
|
|
|
373
460
|
type: this.type,
|
|
374
461
|
description: this.description,
|
|
375
462
|
icon: this.icon,
|
|
376
|
-
url: this.rpcUrl
|
|
463
|
+
url: this.rpcUrl,
|
|
464
|
+
reuseGlobalWalletPopup: true
|
|
377
465
|
};
|
|
378
466
|
}
|
|
379
467
|
async detect() {
|
|
@@ -452,6 +540,8 @@ var RemoteMappedProvider = class {
|
|
|
452
540
|
return controller.connect();
|
|
453
541
|
case "disconnect":
|
|
454
542
|
return controller.disconnect();
|
|
543
|
+
case "isConnected":
|
|
544
|
+
return controller.isConnected();
|
|
455
545
|
case "ledgerApi":
|
|
456
546
|
return controller.ledgerApi(args.params);
|
|
457
547
|
case "prepareExecute":
|
|
@@ -462,6 +552,8 @@ var RemoteMappedProvider = class {
|
|
|
462
552
|
return controller.prepareExecuteAndWait(args.params);
|
|
463
553
|
case "getPrimaryAccount":
|
|
464
554
|
return controller.getPrimaryAccount();
|
|
555
|
+
case "getActiveNetwork":
|
|
556
|
+
return controller.getActiveNetwork();
|
|
465
557
|
default:
|
|
466
558
|
throw new Error("Unsupported method");
|
|
467
559
|
}
|
|
@@ -487,6 +579,75 @@ var gateways_default = [
|
|
|
487
579
|
}
|
|
488
580
|
];
|
|
489
581
|
|
|
582
|
+
// src/injected-discovery.ts
|
|
583
|
+
var DEFAULT_ROOTS = ["canton"];
|
|
584
|
+
var isProviderLike = (obj) => {
|
|
585
|
+
if (typeof obj !== "object" || obj === null) return false;
|
|
586
|
+
const p = obj;
|
|
587
|
+
return typeof p.request === "function" && typeof p.on === "function" && typeof p.emit === "function" && typeof p.removeListener === "function";
|
|
588
|
+
};
|
|
589
|
+
function discoverInjectedProviders(roots = DEFAULT_ROOTS) {
|
|
590
|
+
if (typeof window === "undefined") return [];
|
|
591
|
+
const discovered = [];
|
|
592
|
+
const seen = /* @__PURE__ */ new Set();
|
|
593
|
+
const win = window;
|
|
594
|
+
for (const root of roots) {
|
|
595
|
+
const candidate = win[root];
|
|
596
|
+
if (candidate === void 0 || candidate === null) continue;
|
|
597
|
+
if (isProviderLike(candidate) && !seen.has(candidate)) {
|
|
598
|
+
seen.add(candidate);
|
|
599
|
+
discovered.push({
|
|
600
|
+
id: root,
|
|
601
|
+
provider: candidate,
|
|
602
|
+
sourceRoot: root
|
|
603
|
+
});
|
|
604
|
+
continue;
|
|
605
|
+
}
|
|
606
|
+
if (typeof candidate === "object") {
|
|
607
|
+
for (const [key, value] of Object.entries(
|
|
608
|
+
candidate
|
|
609
|
+
)) {
|
|
610
|
+
if (isProviderLike(value) && !seen.has(value)) {
|
|
611
|
+
seen.add(value);
|
|
612
|
+
discovered.push({
|
|
613
|
+
id: `${root}.${key}`,
|
|
614
|
+
provider: value,
|
|
615
|
+
sourceRoot: root
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
return discovered;
|
|
622
|
+
}
|
|
623
|
+
async function requestAnnouncedProviders(options) {
|
|
624
|
+
if (typeof window === "undefined") return [];
|
|
625
|
+
const timeoutMs = 300;
|
|
626
|
+
const discovered = /* @__PURE__ */ new Map();
|
|
627
|
+
const handler = (event) => {
|
|
628
|
+
const ce = event;
|
|
629
|
+
const detail = ce.detail;
|
|
630
|
+
if (!detail?.id || !detail.name) return;
|
|
631
|
+
if (discovered.has(detail.id)) return;
|
|
632
|
+
discovered.set(detail.id, {
|
|
633
|
+
id: detail.id,
|
|
634
|
+
name: detail.name,
|
|
635
|
+
icon: detail.icon,
|
|
636
|
+
target: detail.target
|
|
637
|
+
});
|
|
638
|
+
};
|
|
639
|
+
window.addEventListener(CANTON_ANNOUNCE_PROVIDER_EVENT, handler);
|
|
640
|
+
try {
|
|
641
|
+
window.dispatchEvent(
|
|
642
|
+
new CustomEvent(CANTON_REQUEST_PROVIDER_EVENT, { detail: {} })
|
|
643
|
+
);
|
|
644
|
+
await new Promise((resolve) => setTimeout(resolve, timeoutMs));
|
|
645
|
+
} finally {
|
|
646
|
+
window.removeEventListener(CANTON_ANNOUNCE_PROVIDER_EVENT, handler);
|
|
647
|
+
}
|
|
648
|
+
return Array.from(discovered.values());
|
|
649
|
+
}
|
|
650
|
+
|
|
490
651
|
// src/sdk.ts
|
|
491
652
|
var DappSDK = class {
|
|
492
653
|
constructor(options) {
|
|
@@ -512,6 +673,46 @@ var DappSDK = class {
|
|
|
512
673
|
}
|
|
513
674
|
}
|
|
514
675
|
}
|
|
676
|
+
async registerInjectedNamespaceAdapters(discovery) {
|
|
677
|
+
const existingIds = new Set(
|
|
678
|
+
discovery.listAdapters().map((a) => a.providerId)
|
|
679
|
+
);
|
|
680
|
+
const injected = discoverInjectedProviders();
|
|
681
|
+
for (const item of injected) {
|
|
682
|
+
const id = `browser:${item.id}`;
|
|
683
|
+
if (existingIds.has(id)) continue;
|
|
684
|
+
const key = `${item.id} (injected)`;
|
|
685
|
+
const adapter = new InjectedAdapter({
|
|
686
|
+
id: item.id,
|
|
687
|
+
name: key,
|
|
688
|
+
provider: item.provider,
|
|
689
|
+
description: `Injected provider from window.${item.id}`
|
|
690
|
+
});
|
|
691
|
+
discovery.registerAdapter(adapter);
|
|
692
|
+
existingIds.add(id);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
async registerAnnouncedAdapters(discovery) {
|
|
696
|
+
const existingIds = new Set(
|
|
697
|
+
discovery.listAdapters().map((a) => a.providerId)
|
|
698
|
+
);
|
|
699
|
+
const announced = await requestAnnouncedProviders();
|
|
700
|
+
for (const item of announced) {
|
|
701
|
+
const id = `browser:ext:${item.id}`;
|
|
702
|
+
if (existingIds.has(id)) continue;
|
|
703
|
+
const adapter = new ExtensionAdapter({
|
|
704
|
+
providerId: id,
|
|
705
|
+
name: item.name,
|
|
706
|
+
icon: item.icon,
|
|
707
|
+
description: "Connect via a browser extension wallet",
|
|
708
|
+
target: item.target ?? item.id
|
|
709
|
+
});
|
|
710
|
+
if (await adapter.detect()) {
|
|
711
|
+
discovery.registerAdapter(adapter);
|
|
712
|
+
existingIds.add(id);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
}
|
|
515
716
|
async ensureDiscovery(config) {
|
|
516
717
|
const defaultAdapters = config?.defaultAdapters ?? createDefaultAdapters(gateways_default);
|
|
517
718
|
if (this.discovery) {
|
|
@@ -520,6 +721,20 @@ var DappSDK = class {
|
|
|
520
721
|
this.discovery,
|
|
521
722
|
config?.additionalAdapters
|
|
522
723
|
);
|
|
724
|
+
await this.registerInjectedNamespaceAdapters(this.discovery);
|
|
725
|
+
await this.registerAnnouncedAdapters(this.discovery);
|
|
726
|
+
await this.discovery.restorePersistedSessionIfNeeded();
|
|
727
|
+
if (!this.client) {
|
|
728
|
+
const session2 = this.discovery.getActiveSession();
|
|
729
|
+
if (session2) {
|
|
730
|
+
const providerType = session2.adapter.getInfo().type;
|
|
731
|
+
const target = session2.adapter instanceof ExtensionAdapter ? session2.adapter.target : void 0;
|
|
732
|
+
this.client = new DappClient(session2.provider, {
|
|
733
|
+
providerType,
|
|
734
|
+
target
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
}
|
|
523
738
|
return this.discovery;
|
|
524
739
|
}
|
|
525
740
|
const initialAdapters = await this.collectDetectedAdapters([
|
|
@@ -530,10 +745,17 @@ var DappSDK = class {
|
|
|
530
745
|
walletPicker: this.walletPicker,
|
|
531
746
|
adapters: initialAdapters
|
|
532
747
|
});
|
|
748
|
+
await this.registerInjectedNamespaceAdapters(this.discovery);
|
|
749
|
+
await this.registerAnnouncedAdapters(this.discovery);
|
|
750
|
+
await this.discovery.restorePersistedSessionIfNeeded();
|
|
533
751
|
const session = this.discovery.getActiveSession();
|
|
534
752
|
if (session) {
|
|
535
753
|
const providerType = session.adapter.getInfo().type;
|
|
536
|
-
|
|
754
|
+
const target = session.adapter instanceof ExtensionAdapter ? session.adapter.target : void 0;
|
|
755
|
+
this.client = new DappClient(session.provider, {
|
|
756
|
+
providerType,
|
|
757
|
+
target
|
|
758
|
+
});
|
|
537
759
|
}
|
|
538
760
|
return this.discovery;
|
|
539
761
|
}
|
|
@@ -567,59 +789,137 @@ var DappSDK = class {
|
|
|
567
789
|
} catch {
|
|
568
790
|
}
|
|
569
791
|
}
|
|
792
|
+
getHttpStatusCode(error) {
|
|
793
|
+
const asNumber = (value) => typeof value === "number" ? value : void 0;
|
|
794
|
+
if (typeof error !== "object" || error === null) return void 0;
|
|
795
|
+
const obj = error;
|
|
796
|
+
const response = obj.response;
|
|
797
|
+
const cause = obj.cause;
|
|
798
|
+
return asNumber(obj.status) ?? asNumber(obj.statusCode) ?? asNumber(response?.status) ?? asNumber(cause?.status) ?? asNumber(cause?.statusCode);
|
|
799
|
+
}
|
|
800
|
+
formatConnectionErrorMessage(error) {
|
|
801
|
+
const fallbackMessage = "Failed to connect wallet";
|
|
802
|
+
const baseMessage = error instanceof Error && error.message.trim().length > 0 ? error.message : fallbackMessage;
|
|
803
|
+
const statusCode = this.getHttpStatusCode(error);
|
|
804
|
+
if (!statusCode) return baseMessage;
|
|
805
|
+
const lowerMessage = baseMessage.toLowerCase();
|
|
806
|
+
if (lowerMessage.includes(`http ${statusCode}`) || lowerMessage.includes(`status ${statusCode}`)) {
|
|
807
|
+
return baseMessage;
|
|
808
|
+
}
|
|
809
|
+
return `${baseMessage} (HTTP ${statusCode})`;
|
|
810
|
+
}
|
|
570
811
|
requireClient() {
|
|
571
812
|
if (!this.client)
|
|
572
813
|
throw new Error("Not connected \u2014 call connect() first");
|
|
573
814
|
return this.client;
|
|
574
815
|
}
|
|
816
|
+
/**
|
|
817
|
+
* Returns the raw connected provider instance (if any).
|
|
818
|
+
*
|
|
819
|
+
* This is useful for advanced integrations that need to call methods that
|
|
820
|
+
* are not wrapped by the higher-level SDK helpers.
|
|
821
|
+
*/
|
|
822
|
+
getConnectedProvider() {
|
|
823
|
+
const session = this.discovery?.getActiveSession();
|
|
824
|
+
if (!session) return null;
|
|
825
|
+
return session.provider;
|
|
826
|
+
}
|
|
575
827
|
async connect(options) {
|
|
576
828
|
await this.ensureInit(options);
|
|
577
829
|
const discovery = this.discovery;
|
|
830
|
+
await this.registerInjectedNamespaceAdapters(discovery);
|
|
831
|
+
await this.registerAnnouncedAdapters(discovery);
|
|
578
832
|
await this.registerAdapters(discovery, options?.defaultAdapters);
|
|
579
833
|
await this.registerAdapters(discovery, options?.additionalAdapters);
|
|
580
834
|
clearAllLocalState();
|
|
581
835
|
const entries = discovery.listAdapters().filter((a) => !this.dynamicAdapterIds.has(a.providerId)).map((a) => {
|
|
582
|
-
const
|
|
836
|
+
const info = a.getInfo();
|
|
583
837
|
return {
|
|
584
|
-
providerId:
|
|
585
|
-
name:
|
|
586
|
-
type:
|
|
587
|
-
description:
|
|
588
|
-
icon:
|
|
589
|
-
url:
|
|
838
|
+
providerId: info.providerId,
|
|
839
|
+
name: info.name,
|
|
840
|
+
type: info.type,
|
|
841
|
+
description: info.description,
|
|
842
|
+
icon: info.icon,
|
|
843
|
+
url: info.url,
|
|
844
|
+
reuseGlobalWalletPopup: info.reuseGlobalWalletPopup
|
|
590
845
|
};
|
|
591
846
|
});
|
|
592
|
-
const
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
const
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
847
|
+
const initialSelection = await this.walletPicker(entries);
|
|
848
|
+
const connectionAttempts = new EventTarget();
|
|
849
|
+
return new Promise((resolve, reject) => {
|
|
850
|
+
const cleanup = () => {
|
|
851
|
+
connectionAttempts.removeEventListener("attempt", onAttempt);
|
|
852
|
+
};
|
|
853
|
+
const onAttempt = async (event) => {
|
|
854
|
+
const picked = event.detail;
|
|
855
|
+
let targetId = picked.providerId;
|
|
856
|
+
if (picked.type === "remote" && picked.url) {
|
|
857
|
+
const existing = discovery.listAdapters().find((a) => a.providerId === targetId);
|
|
858
|
+
if (!existing) {
|
|
859
|
+
const adapter = new RemoteAdapter({
|
|
860
|
+
name: picked.name,
|
|
861
|
+
rpcUrl: picked.url
|
|
862
|
+
});
|
|
863
|
+
discovery.registerAdapter(adapter);
|
|
864
|
+
this.dynamicAdapterIds.add(adapter.providerId);
|
|
865
|
+
targetId = adapter.providerId;
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
try {
|
|
869
|
+
await discovery.connect(targetId);
|
|
870
|
+
const session = discovery.getActiveSession();
|
|
871
|
+
if (!session) {
|
|
872
|
+
throw new Error(
|
|
873
|
+
"Connection succeeded but no active session"
|
|
874
|
+
);
|
|
875
|
+
}
|
|
876
|
+
const info = session.adapter.getInfo();
|
|
877
|
+
this.client = new DappClient(session.provider, {
|
|
878
|
+
providerType: info.type,
|
|
879
|
+
target: session.adapter instanceof ExtensionAdapter ? session.adapter.target : void 0
|
|
880
|
+
});
|
|
881
|
+
const s = await this.client.status();
|
|
882
|
+
if (s.connection.isConnected) {
|
|
883
|
+
if (info.type === "remote" && info.url) {
|
|
884
|
+
setKernelDiscovery({
|
|
885
|
+
walletType: "remote",
|
|
886
|
+
url: info.url
|
|
887
|
+
});
|
|
888
|
+
this.saveRecentGateway(info.name, info.url);
|
|
889
|
+
} else if (info.type === "browser") {
|
|
890
|
+
setKernelDiscovery({
|
|
891
|
+
walletType: "extension",
|
|
892
|
+
providerId: info.providerId
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
notifyWalletPickerConnected(info.reuseGlobalWalletPopup);
|
|
897
|
+
cleanup();
|
|
898
|
+
resolve(s.connection);
|
|
899
|
+
} catch (error) {
|
|
900
|
+
const message = this.formatConnectionErrorMessage(error);
|
|
901
|
+
notifyWalletPickerError(message);
|
|
902
|
+
this.client = null;
|
|
903
|
+
try {
|
|
904
|
+
const retrySelection = await waitForWalletPickerRetrySelection();
|
|
905
|
+
connectionAttempts.dispatchEvent(
|
|
906
|
+
new CustomEvent("attempt", {
|
|
907
|
+
detail: retrySelection
|
|
908
|
+
})
|
|
909
|
+
);
|
|
910
|
+
} catch (retryError) {
|
|
911
|
+
cleanup();
|
|
912
|
+
reject(retryError);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
};
|
|
916
|
+
connectionAttempts.addEventListener("attempt", onAttempt);
|
|
917
|
+
connectionAttempts.dispatchEvent(
|
|
918
|
+
new CustomEvent("attempt", {
|
|
919
|
+
detail: initialSelection
|
|
920
|
+
})
|
|
921
|
+
);
|
|
620
922
|
});
|
|
621
|
-
const s = await this.client.status();
|
|
622
|
-
return s.connection;
|
|
623
923
|
}
|
|
624
924
|
async disconnect() {
|
|
625
925
|
if (this.client) {
|
|
@@ -634,6 +934,17 @@ var DappSDK = class {
|
|
|
634
934
|
}
|
|
635
935
|
return null;
|
|
636
936
|
}
|
|
937
|
+
async isConnected() {
|
|
938
|
+
if (this.client) {
|
|
939
|
+
return this.client.isConnected();
|
|
940
|
+
}
|
|
941
|
+
return {
|
|
942
|
+
isConnected: false,
|
|
943
|
+
isNetworkConnected: false,
|
|
944
|
+
reason: "Unauthenticated",
|
|
945
|
+
networkReason: "Unauthenticated"
|
|
946
|
+
};
|
|
947
|
+
}
|
|
637
948
|
async status() {
|
|
638
949
|
await this.ensureInit();
|
|
639
950
|
return this.requireClient().status();
|
|
@@ -659,6 +970,9 @@ var DappSDK = class {
|
|
|
659
970
|
async onAccountsChanged(listener) {
|
|
660
971
|
this.requireClient().onAccountsChanged(listener);
|
|
661
972
|
}
|
|
973
|
+
async onConnected(listener) {
|
|
974
|
+
this.requireClient().onConnected(listener);
|
|
975
|
+
}
|
|
662
976
|
async onTxChanged(listener) {
|
|
663
977
|
this.requireClient().onTxChanged(listener);
|
|
664
978
|
}
|
|
@@ -670,6 +984,10 @@ var DappSDK = class {
|
|
|
670
984
|
if (!this.client) return;
|
|
671
985
|
this.client.removeOnAccountsChanged(listener);
|
|
672
986
|
}
|
|
987
|
+
async removeOnConnected(listener) {
|
|
988
|
+
if (!this.client) return;
|
|
989
|
+
this.client.removeOnConnected(listener);
|
|
990
|
+
}
|
|
673
991
|
async removeOnTxChanged(listener) {
|
|
674
992
|
if (!this.client) return;
|
|
675
993
|
this.client.removeOnTxChanged(listener);
|
|
@@ -684,30 +1002,31 @@ var connect = (options) => {
|
|
|
684
1002
|
});
|
|
685
1003
|
};
|
|
686
1004
|
var disconnect = () => sdk.disconnect();
|
|
1005
|
+
var isConnected = () => sdk.isConnected();
|
|
687
1006
|
var status = () => sdk.status();
|
|
688
1007
|
var listAccounts = () => sdk.listAccounts();
|
|
689
1008
|
var prepareExecute = (params) => sdk.prepareExecute(params);
|
|
690
1009
|
var prepareExecuteAndWait = (params) => sdk.prepareExecuteAndWait(params);
|
|
691
1010
|
var ledgerApi = (params) => sdk.ledgerApi(params);
|
|
692
1011
|
var open = () => sdk.open();
|
|
1012
|
+
var getConnectedProvider = () => sdk.getConnectedProvider();
|
|
693
1013
|
var onStatusChanged = (listener) => sdk.onStatusChanged(listener);
|
|
694
1014
|
var onAccountsChanged = (listener) => sdk.onAccountsChanged(listener);
|
|
1015
|
+
var onConnected = (listener) => sdk.onConnected(listener);
|
|
695
1016
|
var onTxChanged = (listener) => sdk.onTxChanged(listener);
|
|
696
1017
|
var removeOnStatusChanged = (listener) => sdk.removeOnStatusChanged(listener);
|
|
697
1018
|
var removeOnAccountsChanged = (listener) => sdk.removeOnAccountsChanged(listener);
|
|
1019
|
+
var removeOnConnected = (listener) => sdk.removeOnConnected(listener);
|
|
698
1020
|
var removeOnTxChanged = (listener) => sdk.removeOnTxChanged(listener);
|
|
699
1021
|
function createDefaultAdapters(defaultGatewayConfigs) {
|
|
700
|
-
return
|
|
701
|
-
new
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
})
|
|
707
|
-
)
|
|
708
|
-
];
|
|
1022
|
+
return defaultGatewayConfigs.map(
|
|
1023
|
+
(config) => new RemoteAdapter({
|
|
1024
|
+
...config,
|
|
1025
|
+
icon: config.icon ?? CANTON_LOGO_PNG
|
|
1026
|
+
})
|
|
1027
|
+
);
|
|
709
1028
|
}
|
|
710
1029
|
|
|
711
|
-
export { CANTON_LOGO_PNG, DappClient, DappSDK, ErrorCode, ExtensionAdapter, RemoteAdapter, WALLET_GATEWAY_ICON, connect, sdk as dappSDK, disconnect, ledgerApi, listAccounts, onAccountsChanged, onStatusChanged, onTxChanged, open, prepareExecute, prepareExecuteAndWait, removeOnAccountsChanged, removeOnStatusChanged, removeOnTxChanged, status };
|
|
1030
|
+
export { CANTON_LOGO_PNG, DappClient, DappSDK, ErrorCode, ExtensionAdapter, InjectedAdapter, RemoteAdapter, WALLET_GATEWAY_ICON, connect, sdk as dappSDK, disconnect, getConnectedProvider, isConnected, ledgerApi, listAccounts, onAccountsChanged, onConnected, onStatusChanged, onTxChanged, open, prepareExecute, prepareExecuteAndWait, removeOnAccountsChanged, removeOnConnected, removeOnStatusChanged, removeOnTxChanged, status };
|
|
712
1031
|
//# sourceMappingURL=index.js.map
|
|
713
1032
|
//# sourceMappingURL=index.js.map
|