@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.cjs
CHANGED
|
@@ -4,6 +4,7 @@ var coreProviderDapp = require('@canton-network/core-provider-dapp');
|
|
|
4
4
|
var coreTypes = require('@canton-network/core-types');
|
|
5
5
|
var coreWalletUiComponents = require('@canton-network/core-wallet-ui-components');
|
|
6
6
|
var coreWalletDiscovery = require('@canton-network/core-wallet-discovery');
|
|
7
|
+
var coreRpcTransport = require('@canton-network/core-rpc-transport');
|
|
7
8
|
var coreWalletDappRpcClient = require('@canton-network/core-wallet-dapp-rpc-client');
|
|
8
9
|
|
|
9
10
|
function _interopNamespace(e) {
|
|
@@ -75,7 +76,6 @@ var removeKernelSession = () => {
|
|
|
75
76
|
var clearAllLocalState = ({
|
|
76
77
|
closePopup
|
|
77
78
|
} = {}) => {
|
|
78
|
-
window.canton = void 0;
|
|
79
79
|
removeKernelSession();
|
|
80
80
|
removeKernelDiscovery();
|
|
81
81
|
if (closePopup) {
|
|
@@ -127,6 +127,9 @@ var DappClient = class {
|
|
|
127
127
|
onAccountsChanged(listener) {
|
|
128
128
|
this.provider.on("accountsChanged", listener);
|
|
129
129
|
}
|
|
130
|
+
onConnected(listener) {
|
|
131
|
+
this.provider.on("connected", listener);
|
|
132
|
+
}
|
|
130
133
|
onTxChanged(listener) {
|
|
131
134
|
this.provider.on("txChanged", listener);
|
|
132
135
|
}
|
|
@@ -139,6 +142,9 @@ var DappClient = class {
|
|
|
139
142
|
listener
|
|
140
143
|
);
|
|
141
144
|
}
|
|
145
|
+
removeOnConnected(listener) {
|
|
146
|
+
this.provider.removeListener("connected", listener);
|
|
147
|
+
}
|
|
142
148
|
removeOnTxChanged(listener) {
|
|
143
149
|
this.provider.removeListener("txChanged", listener);
|
|
144
150
|
}
|
|
@@ -151,7 +157,8 @@ var DappClient = class {
|
|
|
151
157
|
window.postMessage(
|
|
152
158
|
{
|
|
153
159
|
type: coreTypes.WalletEvent.SPLICE_WALLET_EXT_OPEN,
|
|
154
|
-
url: userUrl
|
|
160
|
+
url: userUrl,
|
|
161
|
+
target: this.options.target
|
|
155
162
|
},
|
|
156
163
|
"*"
|
|
157
164
|
);
|
|
@@ -167,22 +174,33 @@ var DappClient = class {
|
|
|
167
174
|
clearAllLocalState({ closePopup: true });
|
|
168
175
|
}
|
|
169
176
|
}
|
|
177
|
+
async isConnected() {
|
|
178
|
+
return this.provider.request({ method: "isConnected" });
|
|
179
|
+
}
|
|
170
180
|
};
|
|
171
181
|
var BROWSER_PROVIDER_ID = "browser";
|
|
172
182
|
var EXTENSION_DETECT_TIMEOUT_MS = 2e3;
|
|
173
183
|
var ExtensionAdapter = class {
|
|
174
|
-
constructor() {
|
|
175
|
-
__publicField(this, "providerId"
|
|
176
|
-
__publicField(this, "name"
|
|
184
|
+
constructor(config = {}) {
|
|
185
|
+
__publicField(this, "providerId");
|
|
186
|
+
__publicField(this, "name");
|
|
177
187
|
__publicField(this, "type", "browser");
|
|
178
188
|
__publicField(this, "icon");
|
|
189
|
+
__publicField(this, "description");
|
|
190
|
+
__publicField(this, "target");
|
|
191
|
+
this.providerId = config.providerId ?? BROWSER_PROVIDER_ID;
|
|
192
|
+
this.name = config.name ?? "Browser Extension";
|
|
193
|
+
this.icon = config.icon;
|
|
194
|
+
this.description = config.description ?? "Connect via the Splice Wallet browser extension";
|
|
195
|
+
this.target = config.target;
|
|
179
196
|
}
|
|
180
197
|
getInfo() {
|
|
181
198
|
return {
|
|
182
199
|
providerId: this.providerId,
|
|
183
200
|
name: this.name,
|
|
184
201
|
type: this.type,
|
|
185
|
-
description:
|
|
202
|
+
description: this.description,
|
|
203
|
+
icon: this.icon
|
|
186
204
|
};
|
|
187
205
|
}
|
|
188
206
|
async detect() {
|
|
@@ -193,7 +211,7 @@ var ExtensionAdapter = class {
|
|
|
193
211
|
resolve(false);
|
|
194
212
|
}, EXTENSION_DETECT_TIMEOUT_MS);
|
|
195
213
|
const handler = (event) => {
|
|
196
|
-
if (event.data?.type === coreTypes.WalletEvent.SPLICE_WALLET_EXT_ACK) {
|
|
214
|
+
if (event.data?.type === coreTypes.WalletEvent.SPLICE_WALLET_EXT_ACK && (!this.target || event.data?.target === this.target)) {
|
|
197
215
|
clearTimeout(timeout);
|
|
198
216
|
window.removeEventListener("message", handler);
|
|
199
217
|
resolve(true);
|
|
@@ -201,24 +219,88 @@ var ExtensionAdapter = class {
|
|
|
201
219
|
};
|
|
202
220
|
window.addEventListener("message", handler);
|
|
203
221
|
window.postMessage(
|
|
204
|
-
{
|
|
222
|
+
{
|
|
223
|
+
type: coreTypes.WalletEvent.SPLICE_WALLET_EXT_READY,
|
|
224
|
+
target: this.target
|
|
225
|
+
},
|
|
205
226
|
"*"
|
|
206
227
|
);
|
|
207
228
|
});
|
|
208
229
|
}
|
|
209
230
|
provider() {
|
|
231
|
+
if (this.target) {
|
|
232
|
+
return new coreProviderDapp.DappSyncProvider(
|
|
233
|
+
new coreRpcTransport.WindowTransport(window, { target: this.target })
|
|
234
|
+
);
|
|
235
|
+
}
|
|
210
236
|
return new coreProviderDapp.DappSyncProvider();
|
|
211
237
|
}
|
|
212
238
|
teardown() {
|
|
213
239
|
}
|
|
214
240
|
async restore() {
|
|
215
|
-
|
|
241
|
+
const kernel = getKernelDiscovery();
|
|
242
|
+
const kernelMatches = kernel?.walletType === "extension" && (kernel.providerId === void 0 || kernel.providerId === this.providerId);
|
|
243
|
+
const provider = this.provider();
|
|
244
|
+
if (kernelMatches) {
|
|
245
|
+
try {
|
|
246
|
+
await provider.request({ method: "connect" });
|
|
247
|
+
} catch {
|
|
248
|
+
}
|
|
249
|
+
}
|
|
216
250
|
try {
|
|
217
|
-
const provider = new coreProviderDapp.DappSyncProvider();
|
|
218
251
|
const status2 = await provider.request({ method: "status" });
|
|
219
252
|
if (status2.connection.isConnected) {
|
|
220
253
|
return provider;
|
|
221
254
|
}
|
|
255
|
+
} catch {
|
|
256
|
+
if (kernelMatches) {
|
|
257
|
+
return provider;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
// src/adapter/injected-adapter.ts
|
|
265
|
+
var InjectedAdapter = class {
|
|
266
|
+
constructor(config) {
|
|
267
|
+
__publicField(this, "providerId");
|
|
268
|
+
__publicField(this, "name");
|
|
269
|
+
__publicField(this, "type", "browser");
|
|
270
|
+
__publicField(this, "icon");
|
|
271
|
+
__publicField(this, "providerInstance");
|
|
272
|
+
__publicField(this, "description");
|
|
273
|
+
this.providerId = `browser:${config.id}`;
|
|
274
|
+
this.name = config.name;
|
|
275
|
+
this.icon = config.icon;
|
|
276
|
+
this.providerInstance = config.provider;
|
|
277
|
+
this.description = config.description;
|
|
278
|
+
}
|
|
279
|
+
getInfo() {
|
|
280
|
+
return {
|
|
281
|
+
providerId: this.providerId,
|
|
282
|
+
name: this.name,
|
|
283
|
+
type: this.type,
|
|
284
|
+
description: this.description ?? "Connect via an injected CIP-103 provider (window namespace)",
|
|
285
|
+
icon: this.icon
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
async detect() {
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
provider() {
|
|
292
|
+
return this.providerInstance;
|
|
293
|
+
}
|
|
294
|
+
teardown() {
|
|
295
|
+
}
|
|
296
|
+
async restore() {
|
|
297
|
+
try {
|
|
298
|
+
const status2 = await this.providerInstance.request({
|
|
299
|
+
method: "status"
|
|
300
|
+
});
|
|
301
|
+
if (status2.connection.isConnected) {
|
|
302
|
+
return this.providerInstance;
|
|
303
|
+
}
|
|
222
304
|
} catch {
|
|
223
305
|
}
|
|
224
306
|
return null;
|
|
@@ -231,6 +313,7 @@ function buildController(methods) {
|
|
|
231
313
|
status: methods.status,
|
|
232
314
|
connect: methods.connect,
|
|
233
315
|
disconnect: methods.disconnect,
|
|
316
|
+
isConnected: methods.isConnected,
|
|
234
317
|
getActiveNetwork: methods.getActiveNetwork,
|
|
235
318
|
prepareExecute: methods.prepareExecute,
|
|
236
319
|
prepareExecuteAndWait: methods.prepareExecuteAndWait,
|
|
@@ -290,6 +373,11 @@ var dappSDKController = (provider) => rpc_gen_default({
|
|
|
290
373
|
method: "disconnect"
|
|
291
374
|
});
|
|
292
375
|
},
|
|
376
|
+
isConnected: async () => {
|
|
377
|
+
return await provider.request({
|
|
378
|
+
method: "isConnected"
|
|
379
|
+
});
|
|
380
|
+
},
|
|
293
381
|
ledgerApi: async (params) => provider.request({
|
|
294
382
|
method: "ledgerApi",
|
|
295
383
|
params
|
|
@@ -357,17 +445,16 @@ var dappSDKController = (provider) => rpc_gen_default({
|
|
|
357
445
|
txChanged: async () => {
|
|
358
446
|
throw new Error("Only for events.");
|
|
359
447
|
},
|
|
360
|
-
getActiveNetwork:
|
|
361
|
-
|
|
362
|
-
},
|
|
363
|
-
signMessage:
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
}
|
|
448
|
+
getActiveNetwork: async () => provider.request({
|
|
449
|
+
method: "getActiveNetwork"
|
|
450
|
+
}),
|
|
451
|
+
signMessage: async (params) => provider.request({
|
|
452
|
+
method: "signMessage",
|
|
453
|
+
params
|
|
454
|
+
}),
|
|
455
|
+
getPrimaryAccount: async () => provider.request({
|
|
456
|
+
method: "getPrimaryAccount"
|
|
457
|
+
})
|
|
371
458
|
});
|
|
372
459
|
|
|
373
460
|
// src/adapter/remote-adapter.ts
|
|
@@ -393,7 +480,8 @@ var RemoteAdapter = class {
|
|
|
393
480
|
type: this.type,
|
|
394
481
|
description: this.description,
|
|
395
482
|
icon: this.icon,
|
|
396
|
-
url: this.rpcUrl
|
|
483
|
+
url: this.rpcUrl,
|
|
484
|
+
reuseGlobalWalletPopup: true
|
|
397
485
|
};
|
|
398
486
|
}
|
|
399
487
|
async detect() {
|
|
@@ -472,6 +560,8 @@ var RemoteMappedProvider = class {
|
|
|
472
560
|
return controller.connect();
|
|
473
561
|
case "disconnect":
|
|
474
562
|
return controller.disconnect();
|
|
563
|
+
case "isConnected":
|
|
564
|
+
return controller.isConnected();
|
|
475
565
|
case "ledgerApi":
|
|
476
566
|
return controller.ledgerApi(args.params);
|
|
477
567
|
case "prepareExecute":
|
|
@@ -482,6 +572,8 @@ var RemoteMappedProvider = class {
|
|
|
482
572
|
return controller.prepareExecuteAndWait(args.params);
|
|
483
573
|
case "getPrimaryAccount":
|
|
484
574
|
return controller.getPrimaryAccount();
|
|
575
|
+
case "getActiveNetwork":
|
|
576
|
+
return controller.getActiveNetwork();
|
|
485
577
|
default:
|
|
486
578
|
throw new Error("Unsupported method");
|
|
487
579
|
}
|
|
@@ -507,6 +599,75 @@ var gateways_default = [
|
|
|
507
599
|
}
|
|
508
600
|
];
|
|
509
601
|
|
|
602
|
+
// src/injected-discovery.ts
|
|
603
|
+
var DEFAULT_ROOTS = ["canton"];
|
|
604
|
+
var isProviderLike = (obj) => {
|
|
605
|
+
if (typeof obj !== "object" || obj === null) return false;
|
|
606
|
+
const p = obj;
|
|
607
|
+
return typeof p.request === "function" && typeof p.on === "function" && typeof p.emit === "function" && typeof p.removeListener === "function";
|
|
608
|
+
};
|
|
609
|
+
function discoverInjectedProviders(roots = DEFAULT_ROOTS) {
|
|
610
|
+
if (typeof window === "undefined") return [];
|
|
611
|
+
const discovered = [];
|
|
612
|
+
const seen = /* @__PURE__ */ new Set();
|
|
613
|
+
const win = window;
|
|
614
|
+
for (const root of roots) {
|
|
615
|
+
const candidate = win[root];
|
|
616
|
+
if (candidate === void 0 || candidate === null) continue;
|
|
617
|
+
if (isProviderLike(candidate) && !seen.has(candidate)) {
|
|
618
|
+
seen.add(candidate);
|
|
619
|
+
discovered.push({
|
|
620
|
+
id: root,
|
|
621
|
+
provider: candidate,
|
|
622
|
+
sourceRoot: root
|
|
623
|
+
});
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
if (typeof candidate === "object") {
|
|
627
|
+
for (const [key, value] of Object.entries(
|
|
628
|
+
candidate
|
|
629
|
+
)) {
|
|
630
|
+
if (isProviderLike(value) && !seen.has(value)) {
|
|
631
|
+
seen.add(value);
|
|
632
|
+
discovered.push({
|
|
633
|
+
id: `${root}.${key}`,
|
|
634
|
+
provider: value,
|
|
635
|
+
sourceRoot: root
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
return discovered;
|
|
642
|
+
}
|
|
643
|
+
async function requestAnnouncedProviders(options) {
|
|
644
|
+
if (typeof window === "undefined") return [];
|
|
645
|
+
const timeoutMs = 300;
|
|
646
|
+
const discovered = /* @__PURE__ */ new Map();
|
|
647
|
+
const handler = (event) => {
|
|
648
|
+
const ce = event;
|
|
649
|
+
const detail = ce.detail;
|
|
650
|
+
if (!detail?.id || !detail.name) return;
|
|
651
|
+
if (discovered.has(detail.id)) return;
|
|
652
|
+
discovered.set(detail.id, {
|
|
653
|
+
id: detail.id,
|
|
654
|
+
name: detail.name,
|
|
655
|
+
icon: detail.icon,
|
|
656
|
+
target: detail.target
|
|
657
|
+
});
|
|
658
|
+
};
|
|
659
|
+
window.addEventListener(coreTypes.CANTON_ANNOUNCE_PROVIDER_EVENT, handler);
|
|
660
|
+
try {
|
|
661
|
+
window.dispatchEvent(
|
|
662
|
+
new CustomEvent(coreTypes.CANTON_REQUEST_PROVIDER_EVENT, { detail: {} })
|
|
663
|
+
);
|
|
664
|
+
await new Promise((resolve) => setTimeout(resolve, timeoutMs));
|
|
665
|
+
} finally {
|
|
666
|
+
window.removeEventListener(coreTypes.CANTON_ANNOUNCE_PROVIDER_EVENT, handler);
|
|
667
|
+
}
|
|
668
|
+
return Array.from(discovered.values());
|
|
669
|
+
}
|
|
670
|
+
|
|
510
671
|
// src/sdk.ts
|
|
511
672
|
var DappSDK = class {
|
|
512
673
|
constructor(options) {
|
|
@@ -532,6 +693,46 @@ var DappSDK = class {
|
|
|
532
693
|
}
|
|
533
694
|
}
|
|
534
695
|
}
|
|
696
|
+
async registerInjectedNamespaceAdapters(discovery) {
|
|
697
|
+
const existingIds = new Set(
|
|
698
|
+
discovery.listAdapters().map((a) => a.providerId)
|
|
699
|
+
);
|
|
700
|
+
const injected = discoverInjectedProviders();
|
|
701
|
+
for (const item of injected) {
|
|
702
|
+
const id = `browser:${item.id}`;
|
|
703
|
+
if (existingIds.has(id)) continue;
|
|
704
|
+
const key = `${item.id} (injected)`;
|
|
705
|
+
const adapter = new InjectedAdapter({
|
|
706
|
+
id: item.id,
|
|
707
|
+
name: key,
|
|
708
|
+
provider: item.provider,
|
|
709
|
+
description: `Injected provider from window.${item.id}`
|
|
710
|
+
});
|
|
711
|
+
discovery.registerAdapter(adapter);
|
|
712
|
+
existingIds.add(id);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
async registerAnnouncedAdapters(discovery) {
|
|
716
|
+
const existingIds = new Set(
|
|
717
|
+
discovery.listAdapters().map((a) => a.providerId)
|
|
718
|
+
);
|
|
719
|
+
const announced = await requestAnnouncedProviders();
|
|
720
|
+
for (const item of announced) {
|
|
721
|
+
const id = `browser:ext:${item.id}`;
|
|
722
|
+
if (existingIds.has(id)) continue;
|
|
723
|
+
const adapter = new ExtensionAdapter({
|
|
724
|
+
providerId: id,
|
|
725
|
+
name: item.name,
|
|
726
|
+
icon: item.icon,
|
|
727
|
+
description: "Connect via a browser extension wallet",
|
|
728
|
+
target: item.target ?? item.id
|
|
729
|
+
});
|
|
730
|
+
if (await adapter.detect()) {
|
|
731
|
+
discovery.registerAdapter(adapter);
|
|
732
|
+
existingIds.add(id);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
}
|
|
535
736
|
async ensureDiscovery(config) {
|
|
536
737
|
const defaultAdapters = config?.defaultAdapters ?? createDefaultAdapters(gateways_default);
|
|
537
738
|
if (this.discovery) {
|
|
@@ -540,6 +741,20 @@ var DappSDK = class {
|
|
|
540
741
|
this.discovery,
|
|
541
742
|
config?.additionalAdapters
|
|
542
743
|
);
|
|
744
|
+
await this.registerInjectedNamespaceAdapters(this.discovery);
|
|
745
|
+
await this.registerAnnouncedAdapters(this.discovery);
|
|
746
|
+
await this.discovery.restorePersistedSessionIfNeeded();
|
|
747
|
+
if (!this.client) {
|
|
748
|
+
const session2 = this.discovery.getActiveSession();
|
|
749
|
+
if (session2) {
|
|
750
|
+
const providerType = session2.adapter.getInfo().type;
|
|
751
|
+
const target = session2.adapter instanceof ExtensionAdapter ? session2.adapter.target : void 0;
|
|
752
|
+
this.client = new DappClient(session2.provider, {
|
|
753
|
+
providerType,
|
|
754
|
+
target
|
|
755
|
+
});
|
|
756
|
+
}
|
|
757
|
+
}
|
|
543
758
|
return this.discovery;
|
|
544
759
|
}
|
|
545
760
|
const initialAdapters = await this.collectDetectedAdapters([
|
|
@@ -550,10 +765,17 @@ var DappSDK = class {
|
|
|
550
765
|
walletPicker: this.walletPicker,
|
|
551
766
|
adapters: initialAdapters
|
|
552
767
|
});
|
|
768
|
+
await this.registerInjectedNamespaceAdapters(this.discovery);
|
|
769
|
+
await this.registerAnnouncedAdapters(this.discovery);
|
|
770
|
+
await this.discovery.restorePersistedSessionIfNeeded();
|
|
553
771
|
const session = this.discovery.getActiveSession();
|
|
554
772
|
if (session) {
|
|
555
773
|
const providerType = session.adapter.getInfo().type;
|
|
556
|
-
|
|
774
|
+
const target = session.adapter instanceof ExtensionAdapter ? session.adapter.target : void 0;
|
|
775
|
+
this.client = new DappClient(session.provider, {
|
|
776
|
+
providerType,
|
|
777
|
+
target
|
|
778
|
+
});
|
|
557
779
|
}
|
|
558
780
|
return this.discovery;
|
|
559
781
|
}
|
|
@@ -587,59 +809,137 @@ var DappSDK = class {
|
|
|
587
809
|
} catch {
|
|
588
810
|
}
|
|
589
811
|
}
|
|
812
|
+
getHttpStatusCode(error) {
|
|
813
|
+
const asNumber = (value) => typeof value === "number" ? value : void 0;
|
|
814
|
+
if (typeof error !== "object" || error === null) return void 0;
|
|
815
|
+
const obj = error;
|
|
816
|
+
const response = obj.response;
|
|
817
|
+
const cause = obj.cause;
|
|
818
|
+
return asNumber(obj.status) ?? asNumber(obj.statusCode) ?? asNumber(response?.status) ?? asNumber(cause?.status) ?? asNumber(cause?.statusCode);
|
|
819
|
+
}
|
|
820
|
+
formatConnectionErrorMessage(error) {
|
|
821
|
+
const fallbackMessage = "Failed to connect wallet";
|
|
822
|
+
const baseMessage = error instanceof Error && error.message.trim().length > 0 ? error.message : fallbackMessage;
|
|
823
|
+
const statusCode = this.getHttpStatusCode(error);
|
|
824
|
+
if (!statusCode) return baseMessage;
|
|
825
|
+
const lowerMessage = baseMessage.toLowerCase();
|
|
826
|
+
if (lowerMessage.includes(`http ${statusCode}`) || lowerMessage.includes(`status ${statusCode}`)) {
|
|
827
|
+
return baseMessage;
|
|
828
|
+
}
|
|
829
|
+
return `${baseMessage} (HTTP ${statusCode})`;
|
|
830
|
+
}
|
|
590
831
|
requireClient() {
|
|
591
832
|
if (!this.client)
|
|
592
833
|
throw new Error("Not connected \u2014 call connect() first");
|
|
593
834
|
return this.client;
|
|
594
835
|
}
|
|
836
|
+
/**
|
|
837
|
+
* Returns the raw connected provider instance (if any).
|
|
838
|
+
*
|
|
839
|
+
* This is useful for advanced integrations that need to call methods that
|
|
840
|
+
* are not wrapped by the higher-level SDK helpers.
|
|
841
|
+
*/
|
|
842
|
+
getConnectedProvider() {
|
|
843
|
+
const session = this.discovery?.getActiveSession();
|
|
844
|
+
if (!session) return null;
|
|
845
|
+
return session.provider;
|
|
846
|
+
}
|
|
595
847
|
async connect(options) {
|
|
596
848
|
await this.ensureInit(options);
|
|
597
849
|
const discovery = this.discovery;
|
|
850
|
+
await this.registerInjectedNamespaceAdapters(discovery);
|
|
851
|
+
await this.registerAnnouncedAdapters(discovery);
|
|
598
852
|
await this.registerAdapters(discovery, options?.defaultAdapters);
|
|
599
853
|
await this.registerAdapters(discovery, options?.additionalAdapters);
|
|
600
854
|
clearAllLocalState();
|
|
601
855
|
const entries = discovery.listAdapters().filter((a) => !this.dynamicAdapterIds.has(a.providerId)).map((a) => {
|
|
602
|
-
const
|
|
856
|
+
const info = a.getInfo();
|
|
603
857
|
return {
|
|
604
|
-
providerId:
|
|
605
|
-
name:
|
|
606
|
-
type:
|
|
607
|
-
description:
|
|
608
|
-
icon:
|
|
609
|
-
url:
|
|
858
|
+
providerId: info.providerId,
|
|
859
|
+
name: info.name,
|
|
860
|
+
type: info.type,
|
|
861
|
+
description: info.description,
|
|
862
|
+
icon: info.icon,
|
|
863
|
+
url: info.url,
|
|
864
|
+
reuseGlobalWalletPopup: info.reuseGlobalWalletPopup
|
|
610
865
|
};
|
|
611
866
|
});
|
|
612
|
-
const
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
const
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
867
|
+
const initialSelection = await this.walletPicker(entries);
|
|
868
|
+
const connectionAttempts = new EventTarget();
|
|
869
|
+
return new Promise((resolve, reject) => {
|
|
870
|
+
const cleanup = () => {
|
|
871
|
+
connectionAttempts.removeEventListener("attempt", onAttempt);
|
|
872
|
+
};
|
|
873
|
+
const onAttempt = async (event) => {
|
|
874
|
+
const picked = event.detail;
|
|
875
|
+
let targetId = picked.providerId;
|
|
876
|
+
if (picked.type === "remote" && picked.url) {
|
|
877
|
+
const existing = discovery.listAdapters().find((a) => a.providerId === targetId);
|
|
878
|
+
if (!existing) {
|
|
879
|
+
const adapter = new RemoteAdapter({
|
|
880
|
+
name: picked.name,
|
|
881
|
+
rpcUrl: picked.url
|
|
882
|
+
});
|
|
883
|
+
discovery.registerAdapter(adapter);
|
|
884
|
+
this.dynamicAdapterIds.add(adapter.providerId);
|
|
885
|
+
targetId = adapter.providerId;
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
try {
|
|
889
|
+
await discovery.connect(targetId);
|
|
890
|
+
const session = discovery.getActiveSession();
|
|
891
|
+
if (!session) {
|
|
892
|
+
throw new Error(
|
|
893
|
+
"Connection succeeded but no active session"
|
|
894
|
+
);
|
|
895
|
+
}
|
|
896
|
+
const info = session.adapter.getInfo();
|
|
897
|
+
this.client = new DappClient(session.provider, {
|
|
898
|
+
providerType: info.type,
|
|
899
|
+
target: session.adapter instanceof ExtensionAdapter ? session.adapter.target : void 0
|
|
900
|
+
});
|
|
901
|
+
const s = await this.client.status();
|
|
902
|
+
if (s.connection.isConnected) {
|
|
903
|
+
if (info.type === "remote" && info.url) {
|
|
904
|
+
setKernelDiscovery({
|
|
905
|
+
walletType: "remote",
|
|
906
|
+
url: info.url
|
|
907
|
+
});
|
|
908
|
+
this.saveRecentGateway(info.name, info.url);
|
|
909
|
+
} else if (info.type === "browser") {
|
|
910
|
+
setKernelDiscovery({
|
|
911
|
+
walletType: "extension",
|
|
912
|
+
providerId: info.providerId
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
coreWalletUiComponents.notifyWalletPickerConnected(info.reuseGlobalWalletPopup);
|
|
917
|
+
cleanup();
|
|
918
|
+
resolve(s.connection);
|
|
919
|
+
} catch (error) {
|
|
920
|
+
const message = this.formatConnectionErrorMessage(error);
|
|
921
|
+
coreWalletUiComponents.notifyWalletPickerError(message);
|
|
922
|
+
this.client = null;
|
|
923
|
+
try {
|
|
924
|
+
const retrySelection = await coreWalletUiComponents.waitForWalletPickerRetrySelection();
|
|
925
|
+
connectionAttempts.dispatchEvent(
|
|
926
|
+
new CustomEvent("attempt", {
|
|
927
|
+
detail: retrySelection
|
|
928
|
+
})
|
|
929
|
+
);
|
|
930
|
+
} catch (retryError) {
|
|
931
|
+
cleanup();
|
|
932
|
+
reject(retryError);
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
};
|
|
936
|
+
connectionAttempts.addEventListener("attempt", onAttempt);
|
|
937
|
+
connectionAttempts.dispatchEvent(
|
|
938
|
+
new CustomEvent("attempt", {
|
|
939
|
+
detail: initialSelection
|
|
940
|
+
})
|
|
941
|
+
);
|
|
640
942
|
});
|
|
641
|
-
const s = await this.client.status();
|
|
642
|
-
return s.connection;
|
|
643
943
|
}
|
|
644
944
|
async disconnect() {
|
|
645
945
|
if (this.client) {
|
|
@@ -654,6 +954,17 @@ var DappSDK = class {
|
|
|
654
954
|
}
|
|
655
955
|
return null;
|
|
656
956
|
}
|
|
957
|
+
async isConnected() {
|
|
958
|
+
if (this.client) {
|
|
959
|
+
return this.client.isConnected();
|
|
960
|
+
}
|
|
961
|
+
return {
|
|
962
|
+
isConnected: false,
|
|
963
|
+
isNetworkConnected: false,
|
|
964
|
+
reason: "Unauthenticated",
|
|
965
|
+
networkReason: "Unauthenticated"
|
|
966
|
+
};
|
|
967
|
+
}
|
|
657
968
|
async status() {
|
|
658
969
|
await this.ensureInit();
|
|
659
970
|
return this.requireClient().status();
|
|
@@ -679,6 +990,9 @@ var DappSDK = class {
|
|
|
679
990
|
async onAccountsChanged(listener) {
|
|
680
991
|
this.requireClient().onAccountsChanged(listener);
|
|
681
992
|
}
|
|
993
|
+
async onConnected(listener) {
|
|
994
|
+
this.requireClient().onConnected(listener);
|
|
995
|
+
}
|
|
682
996
|
async onTxChanged(listener) {
|
|
683
997
|
this.requireClient().onTxChanged(listener);
|
|
684
998
|
}
|
|
@@ -690,6 +1004,10 @@ var DappSDK = class {
|
|
|
690
1004
|
if (!this.client) return;
|
|
691
1005
|
this.client.removeOnAccountsChanged(listener);
|
|
692
1006
|
}
|
|
1007
|
+
async removeOnConnected(listener) {
|
|
1008
|
+
if (!this.client) return;
|
|
1009
|
+
this.client.removeOnConnected(listener);
|
|
1010
|
+
}
|
|
693
1011
|
async removeOnTxChanged(listener) {
|
|
694
1012
|
if (!this.client) return;
|
|
695
1013
|
this.client.removeOnTxChanged(listener);
|
|
@@ -704,28 +1022,29 @@ var connect = (options) => {
|
|
|
704
1022
|
});
|
|
705
1023
|
};
|
|
706
1024
|
var disconnect = () => sdk.disconnect();
|
|
1025
|
+
var isConnected = () => sdk.isConnected();
|
|
707
1026
|
var status = () => sdk.status();
|
|
708
1027
|
var listAccounts = () => sdk.listAccounts();
|
|
709
1028
|
var prepareExecute = (params) => sdk.prepareExecute(params);
|
|
710
1029
|
var prepareExecuteAndWait = (params) => sdk.prepareExecuteAndWait(params);
|
|
711
1030
|
var ledgerApi = (params) => sdk.ledgerApi(params);
|
|
712
1031
|
var open = () => sdk.open();
|
|
1032
|
+
var getConnectedProvider = () => sdk.getConnectedProvider();
|
|
713
1033
|
var onStatusChanged = (listener) => sdk.onStatusChanged(listener);
|
|
714
1034
|
var onAccountsChanged = (listener) => sdk.onAccountsChanged(listener);
|
|
1035
|
+
var onConnected = (listener) => sdk.onConnected(listener);
|
|
715
1036
|
var onTxChanged = (listener) => sdk.onTxChanged(listener);
|
|
716
1037
|
var removeOnStatusChanged = (listener) => sdk.removeOnStatusChanged(listener);
|
|
717
1038
|
var removeOnAccountsChanged = (listener) => sdk.removeOnAccountsChanged(listener);
|
|
1039
|
+
var removeOnConnected = (listener) => sdk.removeOnConnected(listener);
|
|
718
1040
|
var removeOnTxChanged = (listener) => sdk.removeOnTxChanged(listener);
|
|
719
1041
|
function createDefaultAdapters(defaultGatewayConfigs) {
|
|
720
|
-
return
|
|
721
|
-
new
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
})
|
|
727
|
-
)
|
|
728
|
-
];
|
|
1042
|
+
return defaultGatewayConfigs.map(
|
|
1043
|
+
(config) => new RemoteAdapter({
|
|
1044
|
+
...config,
|
|
1045
|
+
icon: config.icon ?? CANTON_LOGO_PNG
|
|
1046
|
+
})
|
|
1047
|
+
);
|
|
729
1048
|
}
|
|
730
1049
|
|
|
731
1050
|
Object.defineProperty(exports, "DiscoveryClient", {
|
|
@@ -770,20 +1089,25 @@ exports.DappClient = DappClient;
|
|
|
770
1089
|
exports.DappSDK = DappSDK;
|
|
771
1090
|
exports.ErrorCode = ErrorCode;
|
|
772
1091
|
exports.ExtensionAdapter = ExtensionAdapter;
|
|
1092
|
+
exports.InjectedAdapter = InjectedAdapter;
|
|
773
1093
|
exports.RemoteAdapter = RemoteAdapter;
|
|
774
1094
|
exports.WALLET_GATEWAY_ICON = WALLET_GATEWAY_ICON;
|
|
775
1095
|
exports.connect = connect;
|
|
776
1096
|
exports.dappSDK = sdk;
|
|
777
1097
|
exports.disconnect = disconnect;
|
|
1098
|
+
exports.getConnectedProvider = getConnectedProvider;
|
|
1099
|
+
exports.isConnected = isConnected;
|
|
778
1100
|
exports.ledgerApi = ledgerApi;
|
|
779
1101
|
exports.listAccounts = listAccounts;
|
|
780
1102
|
exports.onAccountsChanged = onAccountsChanged;
|
|
1103
|
+
exports.onConnected = onConnected;
|
|
781
1104
|
exports.onStatusChanged = onStatusChanged;
|
|
782
1105
|
exports.onTxChanged = onTxChanged;
|
|
783
1106
|
exports.open = open;
|
|
784
1107
|
exports.prepareExecute = prepareExecute;
|
|
785
1108
|
exports.prepareExecuteAndWait = prepareExecuteAndWait;
|
|
786
1109
|
exports.removeOnAccountsChanged = removeOnAccountsChanged;
|
|
1110
|
+
exports.removeOnConnected = removeOnConnected;
|
|
787
1111
|
exports.removeOnStatusChanged = removeOnStatusChanged;
|
|
788
1112
|
exports.removeOnTxChanged = removeOnTxChanged;
|
|
789
1113
|
exports.status = status;
|