@canton-network/dapp-sdk 0.25.0 → 1.0.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 +145 -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 +144 -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":
|
|
@@ -537,6 +554,8 @@ var RemoteMappedProvider = class {
|
|
|
537
554
|
return controller.getPrimaryAccount();
|
|
538
555
|
case "getActiveNetwork":
|
|
539
556
|
return controller.getActiveNetwork();
|
|
557
|
+
case "signMessage":
|
|
558
|
+
return controller.signMessage(args.params);
|
|
540
559
|
default:
|
|
541
560
|
throw new Error("Unsupported method");
|
|
542
561
|
}
|
|
@@ -772,6 +791,25 @@ var DappSDK = class {
|
|
|
772
791
|
} catch {
|
|
773
792
|
}
|
|
774
793
|
}
|
|
794
|
+
getHttpStatusCode(error) {
|
|
795
|
+
const asNumber = (value) => typeof value === "number" ? value : void 0;
|
|
796
|
+
if (typeof error !== "object" || error === null) return void 0;
|
|
797
|
+
const obj = error;
|
|
798
|
+
const response = obj.response;
|
|
799
|
+
const cause = obj.cause;
|
|
800
|
+
return asNumber(obj.status) ?? asNumber(obj.statusCode) ?? asNumber(response?.status) ?? asNumber(cause?.status) ?? asNumber(cause?.statusCode);
|
|
801
|
+
}
|
|
802
|
+
formatConnectionErrorMessage(error) {
|
|
803
|
+
const fallbackMessage = "Failed to connect wallet";
|
|
804
|
+
const baseMessage = error instanceof Error && error.message.trim().length > 0 ? error.message : fallbackMessage;
|
|
805
|
+
const statusCode = this.getHttpStatusCode(error);
|
|
806
|
+
if (!statusCode) return baseMessage;
|
|
807
|
+
const lowerMessage = baseMessage.toLowerCase();
|
|
808
|
+
if (lowerMessage.includes(`http ${statusCode}`) || lowerMessage.includes(`status ${statusCode}`)) {
|
|
809
|
+
return baseMessage;
|
|
810
|
+
}
|
|
811
|
+
return `${baseMessage} (HTTP ${statusCode})`;
|
|
812
|
+
}
|
|
775
813
|
requireClient() {
|
|
776
814
|
if (!this.client)
|
|
777
815
|
throw new Error("Not connected \u2014 call connect() first");
|
|
@@ -797,52 +835,93 @@ var DappSDK = class {
|
|
|
797
835
|
await this.registerAdapters(discovery, options?.additionalAdapters);
|
|
798
836
|
clearAllLocalState();
|
|
799
837
|
const entries = discovery.listAdapters().filter((a) => !this.dynamicAdapterIds.has(a.providerId)).map((a) => {
|
|
800
|
-
const
|
|
838
|
+
const info = a.getInfo();
|
|
801
839
|
return {
|
|
802
|
-
providerId:
|
|
803
|
-
name:
|
|
804
|
-
type:
|
|
805
|
-
description:
|
|
806
|
-
icon:
|
|
807
|
-
url:
|
|
808
|
-
reuseGlobalWalletPopup:
|
|
840
|
+
providerId: info.providerId,
|
|
841
|
+
name: info.name,
|
|
842
|
+
type: info.type,
|
|
843
|
+
description: info.description,
|
|
844
|
+
icon: info.icon,
|
|
845
|
+
url: info.url,
|
|
846
|
+
reuseGlobalWalletPopup: info.reuseGlobalWalletPopup
|
|
809
847
|
};
|
|
810
848
|
});
|
|
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
|
-
|
|
849
|
+
const initialSelection = await this.walletPicker(entries);
|
|
850
|
+
const connectionAttempts = new EventTarget();
|
|
851
|
+
return new Promise((resolve, reject) => {
|
|
852
|
+
const cleanup = () => {
|
|
853
|
+
connectionAttempts.removeEventListener("attempt", onAttempt);
|
|
854
|
+
};
|
|
855
|
+
const onAttempt = async (event) => {
|
|
856
|
+
const picked = event.detail;
|
|
857
|
+
let targetId = picked.providerId;
|
|
858
|
+
if (picked.type === "remote" && picked.url) {
|
|
859
|
+
const existing = discovery.listAdapters().find((a) => a.providerId === targetId);
|
|
860
|
+
if (!existing) {
|
|
861
|
+
const adapter = new RemoteAdapter({
|
|
862
|
+
name: picked.name,
|
|
863
|
+
rpcUrl: picked.url
|
|
864
|
+
});
|
|
865
|
+
discovery.registerAdapter(adapter);
|
|
866
|
+
this.dynamicAdapterIds.add(adapter.providerId);
|
|
867
|
+
targetId = adapter.providerId;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
try {
|
|
871
|
+
await discovery.connect(targetId);
|
|
872
|
+
const session = discovery.getActiveSession();
|
|
873
|
+
if (!session) {
|
|
874
|
+
throw new Error(
|
|
875
|
+
"Connection succeeded but no active session"
|
|
876
|
+
);
|
|
877
|
+
}
|
|
878
|
+
const info = session.adapter.getInfo();
|
|
879
|
+
this.client = new DappClient(session.provider, {
|
|
880
|
+
providerType: info.type,
|
|
881
|
+
target: session.adapter instanceof ExtensionAdapter ? session.adapter.target : void 0
|
|
882
|
+
});
|
|
883
|
+
const s = await this.client.status();
|
|
884
|
+
if (s.connection.isConnected) {
|
|
885
|
+
if (info.type === "remote" && info.url) {
|
|
886
|
+
setKernelDiscovery({
|
|
887
|
+
walletType: "remote",
|
|
888
|
+
url: info.url
|
|
889
|
+
});
|
|
890
|
+
this.saveRecentGateway(info.name, info.url);
|
|
891
|
+
} else if (info.type === "browser") {
|
|
892
|
+
setKernelDiscovery({
|
|
893
|
+
walletType: "extension",
|
|
894
|
+
providerId: info.providerId
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
notifyWalletPickerConnected(info.reuseGlobalWalletPopup);
|
|
899
|
+
cleanup();
|
|
900
|
+
resolve(s.connection);
|
|
901
|
+
} catch (error) {
|
|
902
|
+
const message = this.formatConnectionErrorMessage(error);
|
|
903
|
+
notifyWalletPickerError(message);
|
|
904
|
+
this.client = null;
|
|
905
|
+
try {
|
|
906
|
+
const retrySelection = await waitForWalletPickerRetrySelection();
|
|
907
|
+
connectionAttempts.dispatchEvent(
|
|
908
|
+
new CustomEvent("attempt", {
|
|
909
|
+
detail: retrySelection
|
|
910
|
+
})
|
|
911
|
+
);
|
|
912
|
+
} catch (retryError) {
|
|
913
|
+
cleanup();
|
|
914
|
+
reject(retryError);
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
};
|
|
918
|
+
connectionAttempts.addEventListener("attempt", onAttempt);
|
|
919
|
+
connectionAttempts.dispatchEvent(
|
|
920
|
+
new CustomEvent("attempt", {
|
|
921
|
+
detail: initialSelection
|
|
922
|
+
})
|
|
923
|
+
);
|
|
843
924
|
});
|
|
844
|
-
const s = await this.client.status();
|
|
845
|
-
return s.connection;
|
|
846
925
|
}
|
|
847
926
|
async disconnect() {
|
|
848
927
|
if (this.client) {
|
|
@@ -857,6 +936,17 @@ var DappSDK = class {
|
|
|
857
936
|
}
|
|
858
937
|
return null;
|
|
859
938
|
}
|
|
939
|
+
async isConnected() {
|
|
940
|
+
if (this.client) {
|
|
941
|
+
return this.client.isConnected();
|
|
942
|
+
}
|
|
943
|
+
return {
|
|
944
|
+
isConnected: false,
|
|
945
|
+
isNetworkConnected: false,
|
|
946
|
+
reason: "Unauthenticated",
|
|
947
|
+
networkReason: "Unauthenticated"
|
|
948
|
+
};
|
|
949
|
+
}
|
|
860
950
|
async status() {
|
|
861
951
|
await this.ensureInit();
|
|
862
952
|
return this.requireClient().status();
|
|
@@ -882,6 +972,9 @@ var DappSDK = class {
|
|
|
882
972
|
async onAccountsChanged(listener) {
|
|
883
973
|
this.requireClient().onAccountsChanged(listener);
|
|
884
974
|
}
|
|
975
|
+
async onConnected(listener) {
|
|
976
|
+
this.requireClient().onConnected(listener);
|
|
977
|
+
}
|
|
885
978
|
async onTxChanged(listener) {
|
|
886
979
|
this.requireClient().onTxChanged(listener);
|
|
887
980
|
}
|
|
@@ -893,6 +986,10 @@ var DappSDK = class {
|
|
|
893
986
|
if (!this.client) return;
|
|
894
987
|
this.client.removeOnAccountsChanged(listener);
|
|
895
988
|
}
|
|
989
|
+
async removeOnConnected(listener) {
|
|
990
|
+
if (!this.client) return;
|
|
991
|
+
this.client.removeOnConnected(listener);
|
|
992
|
+
}
|
|
896
993
|
async removeOnTxChanged(listener) {
|
|
897
994
|
if (!this.client) return;
|
|
898
995
|
this.client.removeOnTxChanged(listener);
|
|
@@ -907,6 +1004,7 @@ var connect = (options) => {
|
|
|
907
1004
|
});
|
|
908
1005
|
};
|
|
909
1006
|
var disconnect = () => sdk.disconnect();
|
|
1007
|
+
var isConnected = () => sdk.isConnected();
|
|
910
1008
|
var status = () => sdk.status();
|
|
911
1009
|
var listAccounts = () => sdk.listAccounts();
|
|
912
1010
|
var prepareExecute = (params) => sdk.prepareExecute(params);
|
|
@@ -916,9 +1014,11 @@ var open = () => sdk.open();
|
|
|
916
1014
|
var getConnectedProvider = () => sdk.getConnectedProvider();
|
|
917
1015
|
var onStatusChanged = (listener) => sdk.onStatusChanged(listener);
|
|
918
1016
|
var onAccountsChanged = (listener) => sdk.onAccountsChanged(listener);
|
|
1017
|
+
var onConnected = (listener) => sdk.onConnected(listener);
|
|
919
1018
|
var onTxChanged = (listener) => sdk.onTxChanged(listener);
|
|
920
1019
|
var removeOnStatusChanged = (listener) => sdk.removeOnStatusChanged(listener);
|
|
921
1020
|
var removeOnAccountsChanged = (listener) => sdk.removeOnAccountsChanged(listener);
|
|
1021
|
+
var removeOnConnected = (listener) => sdk.removeOnConnected(listener);
|
|
922
1022
|
var removeOnTxChanged = (listener) => sdk.removeOnTxChanged(listener);
|
|
923
1023
|
function createDefaultAdapters(defaultGatewayConfigs) {
|
|
924
1024
|
return defaultGatewayConfigs.map(
|
|
@@ -929,6 +1029,6 @@ function createDefaultAdapters(defaultGatewayConfigs) {
|
|
|
929
1029
|
);
|
|
930
1030
|
}
|
|
931
1031
|
|
|
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 };
|
|
1032
|
+
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
1033
|
//# sourceMappingURL=index.js.map
|
|
934
1034
|
//# sourceMappingURL=index.js.map
|