@canton-network/dapp-sdk 0.25.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/dist/client.d.ts +3 -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 +3 -2
- package/dist/dapp-api/rpc-gen/typings.d.ts.map +1 -1
- package/dist/index.cjs +143 -42
- 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 +142 -44
- package/dist/index.js.map +1 -1
- package/dist/sdk-controller.d.ts +1 -0
- package/dist/sdk-controller.d.ts.map +1 -1
- package/dist/sdk.d.ts +8 -0
- package/dist/sdk.d.ts.map +1 -1
- package/package.json +16 -11
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DappSyncProvider, injectProvider, DappAsyncProvider } from '@canton-network/core-provider-dapp';
|
|
2
2
|
import { CANTON_ANNOUNCE_PROVIDER_EVENT, CANTON_REQUEST_PROVIDER_EVENT, WalletEvent, DiscoverResult } from '@canton-network/core-types';
|
|
3
|
-
import { pickWallet, popup } from '@canton-network/core-wallet-ui-components';
|
|
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
6
|
import { WindowTransport } from '@canton-network/core-rpc-transport';
|
|
@@ -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
|
}
|
|
@@ -148,6 +154,9 @@ var DappClient = class {
|
|
|
148
154
|
clearAllLocalState({ closePopup: true });
|
|
149
155
|
}
|
|
150
156
|
}
|
|
157
|
+
async isConnected() {
|
|
158
|
+
return this.provider.request({ method: "isConnected" });
|
|
159
|
+
}
|
|
151
160
|
};
|
|
152
161
|
var BROWSER_PROVIDER_ID = "browser";
|
|
153
162
|
var EXTENSION_DETECT_TIMEOUT_MS = 2e3;
|
|
@@ -284,6 +293,7 @@ function buildController(methods) {
|
|
|
284
293
|
status: methods.status,
|
|
285
294
|
connect: methods.connect,
|
|
286
295
|
disconnect: methods.disconnect,
|
|
296
|
+
isConnected: methods.isConnected,
|
|
287
297
|
getActiveNetwork: methods.getActiveNetwork,
|
|
288
298
|
prepareExecute: methods.prepareExecute,
|
|
289
299
|
prepareExecuteAndWait: methods.prepareExecuteAndWait,
|
|
@@ -343,6 +353,11 @@ var dappSDKController = (provider) => rpc_gen_default({
|
|
|
343
353
|
method: "disconnect"
|
|
344
354
|
});
|
|
345
355
|
},
|
|
356
|
+
isConnected: async () => {
|
|
357
|
+
return await provider.request({
|
|
358
|
+
method: "isConnected"
|
|
359
|
+
});
|
|
360
|
+
},
|
|
346
361
|
ledgerApi: async (params) => provider.request({
|
|
347
362
|
method: "ledgerApi",
|
|
348
363
|
params
|
|
@@ -525,6 +540,8 @@ var RemoteMappedProvider = class {
|
|
|
525
540
|
return controller.connect();
|
|
526
541
|
case "disconnect":
|
|
527
542
|
return controller.disconnect();
|
|
543
|
+
case "isConnected":
|
|
544
|
+
return controller.isConnected();
|
|
528
545
|
case "ledgerApi":
|
|
529
546
|
return controller.ledgerApi(args.params);
|
|
530
547
|
case "prepareExecute":
|
|
@@ -772,6 +789,25 @@ var DappSDK = class {
|
|
|
772
789
|
} catch {
|
|
773
790
|
}
|
|
774
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
|
+
}
|
|
775
811
|
requireClient() {
|
|
776
812
|
if (!this.client)
|
|
777
813
|
throw new Error("Not connected \u2014 call connect() first");
|
|
@@ -797,52 +833,93 @@ var DappSDK = class {
|
|
|
797
833
|
await this.registerAdapters(discovery, options?.additionalAdapters);
|
|
798
834
|
clearAllLocalState();
|
|
799
835
|
const entries = discovery.listAdapters().filter((a) => !this.dynamicAdapterIds.has(a.providerId)).map((a) => {
|
|
800
|
-
const
|
|
836
|
+
const info = a.getInfo();
|
|
801
837
|
return {
|
|
802
|
-
providerId:
|
|
803
|
-
name:
|
|
804
|
-
type:
|
|
805
|
-
description:
|
|
806
|
-
icon:
|
|
807
|
-
url:
|
|
808
|
-
reuseGlobalWalletPopup:
|
|
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
|
|
809
845
|
};
|
|
810
846
|
});
|
|
811
|
-
const
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
const
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
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
|
+
);
|
|
843
922
|
});
|
|
844
|
-
const s = await this.client.status();
|
|
845
|
-
return s.connection;
|
|
846
923
|
}
|
|
847
924
|
async disconnect() {
|
|
848
925
|
if (this.client) {
|
|
@@ -857,6 +934,17 @@ var DappSDK = class {
|
|
|
857
934
|
}
|
|
858
935
|
return null;
|
|
859
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
|
+
}
|
|
860
948
|
async status() {
|
|
861
949
|
await this.ensureInit();
|
|
862
950
|
return this.requireClient().status();
|
|
@@ -882,6 +970,9 @@ var DappSDK = class {
|
|
|
882
970
|
async onAccountsChanged(listener) {
|
|
883
971
|
this.requireClient().onAccountsChanged(listener);
|
|
884
972
|
}
|
|
973
|
+
async onConnected(listener) {
|
|
974
|
+
this.requireClient().onConnected(listener);
|
|
975
|
+
}
|
|
885
976
|
async onTxChanged(listener) {
|
|
886
977
|
this.requireClient().onTxChanged(listener);
|
|
887
978
|
}
|
|
@@ -893,6 +984,10 @@ var DappSDK = class {
|
|
|
893
984
|
if (!this.client) return;
|
|
894
985
|
this.client.removeOnAccountsChanged(listener);
|
|
895
986
|
}
|
|
987
|
+
async removeOnConnected(listener) {
|
|
988
|
+
if (!this.client) return;
|
|
989
|
+
this.client.removeOnConnected(listener);
|
|
990
|
+
}
|
|
896
991
|
async removeOnTxChanged(listener) {
|
|
897
992
|
if (!this.client) return;
|
|
898
993
|
this.client.removeOnTxChanged(listener);
|
|
@@ -907,6 +1002,7 @@ var connect = (options) => {
|
|
|
907
1002
|
});
|
|
908
1003
|
};
|
|
909
1004
|
var disconnect = () => sdk.disconnect();
|
|
1005
|
+
var isConnected = () => sdk.isConnected();
|
|
910
1006
|
var status = () => sdk.status();
|
|
911
1007
|
var listAccounts = () => sdk.listAccounts();
|
|
912
1008
|
var prepareExecute = (params) => sdk.prepareExecute(params);
|
|
@@ -916,9 +1012,11 @@ var open = () => sdk.open();
|
|
|
916
1012
|
var getConnectedProvider = () => sdk.getConnectedProvider();
|
|
917
1013
|
var onStatusChanged = (listener) => sdk.onStatusChanged(listener);
|
|
918
1014
|
var onAccountsChanged = (listener) => sdk.onAccountsChanged(listener);
|
|
1015
|
+
var onConnected = (listener) => sdk.onConnected(listener);
|
|
919
1016
|
var onTxChanged = (listener) => sdk.onTxChanged(listener);
|
|
920
1017
|
var removeOnStatusChanged = (listener) => sdk.removeOnStatusChanged(listener);
|
|
921
1018
|
var removeOnAccountsChanged = (listener) => sdk.removeOnAccountsChanged(listener);
|
|
1019
|
+
var removeOnConnected = (listener) => sdk.removeOnConnected(listener);
|
|
922
1020
|
var removeOnTxChanged = (listener) => sdk.removeOnTxChanged(listener);
|
|
923
1021
|
function createDefaultAdapters(defaultGatewayConfigs) {
|
|
924
1022
|
return defaultGatewayConfigs.map(
|
|
@@ -929,6 +1027,6 @@ function createDefaultAdapters(defaultGatewayConfigs) {
|
|
|
929
1027
|
);
|
|
930
1028
|
}
|
|
931
1029
|
|
|
932
|
-
export { CANTON_LOGO_PNG, DappClient, DappSDK, ErrorCode, ExtensionAdapter, InjectedAdapter, RemoteAdapter, WALLET_GATEWAY_ICON, connect, sdk as dappSDK, disconnect, getConnectedProvider, 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 };
|
|
933
1031
|
//# sourceMappingURL=index.js.map
|
|
934
1032
|
//# sourceMappingURL=index.js.map
|