@sundaeswap/wallet-lite 0.0.87 → 0.0.88-dev.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/cjs/classes/WalletObserver.class.js +64 -56
- package/dist/cjs/classes/WalletObserver.class.js.map +1 -1
- package/dist/cjs/react-components/WalletObserverProvider/hooks/useWalletObserverState.js +47 -39
- package/dist/cjs/react-components/WalletObserverProvider/hooks/useWalletObserverState.js.map +1 -1
- package/dist/cjs/react-components/hooks/useWalletObserver.js +2 -1
- package/dist/cjs/react-components/hooks/useWalletObserver.js.map +1 -1
- package/dist/esm/classes/WalletObserver.class.js +51 -44
- package/dist/esm/classes/WalletObserver.class.js.map +1 -1
- package/dist/esm/react-components/WalletObserverProvider/hooks/useWalletObserverState.js +9 -3
- package/dist/esm/react-components/WalletObserverProvider/hooks/useWalletObserverState.js.map +1 -1
- package/dist/esm/react-components/hooks/useWalletObserver.js +2 -1
- package/dist/esm/react-components/hooks/useWalletObserver.js.map +1 -1
- package/dist/types/classes/WalletObserver.class.d.ts +2 -2
- package/dist/types/classes/WalletObserver.class.d.ts.map +1 -1
- package/dist/types/react-components/WalletObserverProvider/hooks/useWalletObserverState.d.ts +1 -0
- package/dist/types/react-components/WalletObserverProvider/hooks/useWalletObserverState.d.ts.map +1 -1
- package/dist/types/react-components/hooks/useWalletObserver.d.ts.map +1 -1
- package/dist/types/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +4 -3
- package/src/classes/WalletObserver.class.ts +60 -50
- package/src/classes/__tests__/WalletObserver.test.ts +15 -4
- package/src/react-components/WalletObserverProvider/hooks/useWalletObserverState.ts +10 -3
- package/src/react-components/hooks/useWalletObserver.ts +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sundaeswap/wallet-lite",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.88-dev.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"LICENSE.md"
|
|
23
23
|
],
|
|
24
24
|
"engines": {
|
|
25
|
-
"node": ">=
|
|
25
|
+
"node": ">=20.0.0"
|
|
26
26
|
},
|
|
27
27
|
"scripts": {
|
|
28
28
|
"clean": "rm -rf ./dist",
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"build": "bun clean && bun types && bun build:esm && bun build:cjs && bun set-cjs",
|
|
33
33
|
"watch": "bun clean && bun run build && bun build:esm --watch",
|
|
34
34
|
"set-cjs": "echo '{ \"type\": \"commonjs\" }' > ./dist/cjs/package.json",
|
|
35
|
-
"version": "standard-version"
|
|
35
|
+
"version": "standard-version",
|
|
36
|
+
"version:canary": "standard-version --prerelease dev"
|
|
36
37
|
},
|
|
37
38
|
"dependencies": {
|
|
38
39
|
"@babel/cli": "^7.24.7",
|
|
@@ -215,6 +215,7 @@ export class WalletObserver<
|
|
|
215
215
|
*/
|
|
216
216
|
syncApi = async (
|
|
217
217
|
activeWallet?: string,
|
|
218
|
+
abortSignal?: AbortSignal,
|
|
218
219
|
): Promise<Cip30WalletApi | undefined> => {
|
|
219
220
|
if (!activeWallet && !this.activeWallet) {
|
|
220
221
|
throw new Error(
|
|
@@ -227,6 +228,10 @@ export class WalletObserver<
|
|
|
227
228
|
let attempts = 0;
|
|
228
229
|
let shouldContinue = true;
|
|
229
230
|
|
|
231
|
+
abortSignal?.addEventListener("abort", () => {
|
|
232
|
+
shouldContinue = false;
|
|
233
|
+
});
|
|
234
|
+
|
|
230
235
|
while (shouldContinue) {
|
|
231
236
|
if (attempts === 10) {
|
|
232
237
|
throw new Error(
|
|
@@ -237,11 +242,6 @@ export class WalletObserver<
|
|
|
237
242
|
try {
|
|
238
243
|
const cardano = window?.cardano || window?.parent?.cardano;
|
|
239
244
|
const api = await cardano?.[selectedWallet]?.enable();
|
|
240
|
-
|
|
241
|
-
if (!api) {
|
|
242
|
-
throw Error;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
245
|
this.api = api;
|
|
246
246
|
this.network = await api.getNetworkId();
|
|
247
247
|
shouldContinue = false;
|
|
@@ -250,6 +250,7 @@ export class WalletObserver<
|
|
|
250
250
|
[
|
|
251
251
|
"user canceled connection",
|
|
252
252
|
"User declined to sign the data.",
|
|
253
|
+
"Attempt to connect timed out after 10 seconds",
|
|
253
254
|
].includes((e as Error)?.message) ||
|
|
254
255
|
(e as ApiError)?.code === APIErrorCode.Refused
|
|
255
256
|
) {
|
|
@@ -284,67 +285,76 @@ export class WalletObserver<
|
|
|
284
285
|
*/
|
|
285
286
|
connectWallet = async (
|
|
286
287
|
extension: string,
|
|
288
|
+
abortSignal?: AbortSignal,
|
|
287
289
|
): Promise<IWalletObserverSync<AssetMetadata> | Error> => {
|
|
288
290
|
const start = performance.now();
|
|
289
291
|
this.dispatch(EWalletObserverEvents.CONNECT_WALLET_START);
|
|
290
292
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
+
try {
|
|
294
|
+
let attempts = 0;
|
|
295
|
+
let extensionObject = window.cardano?.[extension];
|
|
293
296
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
297
|
+
// Disconnect any CIP45 connections.
|
|
298
|
+
if (!extension?.includes("p2p")) {
|
|
299
|
+
this.peerConnectInstance?.shutdownServer();
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
while (typeof extensionObject === "undefined") {
|
|
303
|
+
if (attempts === 40) {
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
298
306
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
307
|
+
await new Promise((res) =>
|
|
308
|
+
setTimeout(res, (this._options.connectTimeout as number) / 40),
|
|
309
|
+
);
|
|
310
|
+
extensionObject = window.cardano?.[extension];
|
|
311
|
+
attempts++;
|
|
302
312
|
}
|
|
303
313
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
extensionObject = window.cardano?.[extension];
|
|
308
|
-
attempts++;
|
|
309
|
-
}
|
|
314
|
+
if (!extensionObject) {
|
|
315
|
+
throw new Error("Wallet extension not found in the global context.");
|
|
316
|
+
}
|
|
310
317
|
|
|
311
|
-
|
|
312
|
-
this.
|
|
313
|
-
|
|
314
|
-
|
|
318
|
+
this.activeWallet = extension;
|
|
319
|
+
const api = await this.syncApi(extension, abortSignal);
|
|
320
|
+
if (!api) {
|
|
321
|
+
throw new Error("API could not be found.");
|
|
322
|
+
}
|
|
315
323
|
|
|
316
|
-
|
|
317
|
-
await this.syncApi(extension);
|
|
318
|
-
const data = await this.sync();
|
|
324
|
+
const data = await this.sync();
|
|
319
325
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
326
|
+
if (this._options.persistence) {
|
|
327
|
+
if (data.usedAddresses instanceof Error) {
|
|
328
|
+
data.usedAddresses.cause =
|
|
329
|
+
"Could not get a list of used addresses from the wallet when trying to save the connection.";
|
|
330
|
+
throw data.usedAddresses;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const seed: IWalletObserverSeed = {
|
|
334
|
+
activeWallet: extension,
|
|
335
|
+
mainAddress: data.usedAddresses[0],
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
window.localStorage.setItem(
|
|
339
|
+
WalletObserver.PERSISTENCE_CACHE_KEY,
|
|
340
|
+
JSON.stringify(seed),
|
|
341
|
+
);
|
|
325
342
|
}
|
|
326
343
|
|
|
327
|
-
|
|
344
|
+
this.dispatch(EWalletObserverEvents.CONNECT_WALLET_END, {
|
|
345
|
+
...data,
|
|
328
346
|
activeWallet: extension,
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
JSON.stringify(seed),
|
|
335
|
-
);
|
|
336
|
-
}
|
|
347
|
+
});
|
|
348
|
+
const end = performance.now();
|
|
349
|
+
if (this._options.debug) {
|
|
350
|
+
console.log(`connectWallet: ${end - start}ms`);
|
|
351
|
+
}
|
|
337
352
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
const end = performance.now();
|
|
343
|
-
if (this._options.debug) {
|
|
344
|
-
console.log(`connectWallet: ${end - start}ms`);
|
|
353
|
+
return data;
|
|
354
|
+
} catch (e) {
|
|
355
|
+
this.dispatch(EWalletObserverEvents.CONNECT_WALLET_END);
|
|
356
|
+
throw e;
|
|
345
357
|
}
|
|
346
|
-
|
|
347
|
-
return data;
|
|
348
358
|
};
|
|
349
359
|
|
|
350
360
|
getCip45Instance = async () => {
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
EWalletObserverEvents,
|
|
12
12
|
TMetadataResolverFunc,
|
|
13
13
|
TWalletObserverOptions,
|
|
14
|
+
WalletBalanceMap,
|
|
14
15
|
} from "../../index.js";
|
|
15
16
|
import * as getLibModules from "../../utils/getLibs.js";
|
|
16
17
|
import { WalletObserver } from "../WalletObserver.class.js";
|
|
@@ -140,7 +141,7 @@ describe("WalletObserver", async () => {
|
|
|
140
141
|
).toBeNull();
|
|
141
142
|
expect(observer.activeWallet).toEqual("eternl");
|
|
142
143
|
expect(observer.api).toBeDefined();
|
|
143
|
-
expect(spiedOnSyncApi).toHaveBeenNthCalledWith(1, "eternl");
|
|
144
|
+
expect(spiedOnSyncApi).toHaveBeenNthCalledWith(1, "eternl", undefined);
|
|
144
145
|
|
|
145
146
|
const syncResults = await observer.sync();
|
|
146
147
|
expect(spiedDispatch).toHaveBeenNthCalledWith(
|
|
@@ -173,7 +174,12 @@ describe("WalletObserver", async () => {
|
|
|
173
174
|
}),
|
|
174
175
|
);
|
|
175
176
|
|
|
176
|
-
expect(syncResults.balanceMap
|
|
177
|
+
expect(syncResults.balanceMap).toBeInstanceOf(
|
|
178
|
+
WalletBalanceMap<IAssetAmountMetadata>,
|
|
179
|
+
);
|
|
180
|
+
expect(
|
|
181
|
+
(syncResults.balanceMap as WalletBalanceMap<IAssetAmountMetadata>).size,
|
|
182
|
+
).toEqual(assetIds.length);
|
|
177
183
|
expect(syncResults.network).toBe(network);
|
|
178
184
|
expect(syncResults.unusedAddresses).toEqual(unusedAddresses);
|
|
179
185
|
expect(syncResults.usedAddresses).toEqual(usedAddresses);
|
|
@@ -192,11 +198,16 @@ describe("WalletObserver", async () => {
|
|
|
192
198
|
|
|
193
199
|
expect(observer.activeWallet).toEqual("eternl");
|
|
194
200
|
expect(observer.api).toBeDefined();
|
|
195
|
-
expect(spiedOnSyncApi).toHaveBeenNthCalledWith(1, "eternl");
|
|
201
|
+
expect(spiedOnSyncApi).toHaveBeenNthCalledWith(1, "eternl", undefined);
|
|
196
202
|
|
|
197
203
|
const syncResults = await observer.sync();
|
|
198
204
|
|
|
199
|
-
expect(syncResults.balanceMap
|
|
205
|
+
expect(syncResults.balanceMap).toBeInstanceOf(
|
|
206
|
+
WalletBalanceMap<IAssetAmountMetadata>,
|
|
207
|
+
);
|
|
208
|
+
expect(
|
|
209
|
+
(syncResults.balanceMap as WalletBalanceMap<IAssetAmountMetadata>).size,
|
|
210
|
+
).toEqual(assetIds.length);
|
|
200
211
|
expect(syncResults.network).toBe(network);
|
|
201
212
|
expect(syncResults.unusedAddresses).toEqual(unusedAddresses);
|
|
202
213
|
expect(syncResults.usedAddresses).toEqual(usedAddresses);
|
|
@@ -19,6 +19,7 @@ export const useWalletObserverState = <
|
|
|
19
19
|
>(
|
|
20
20
|
observer: WalletObserver<AssetMetadata>,
|
|
21
21
|
) => {
|
|
22
|
+
const [abortController] = useState(new AbortController());
|
|
22
23
|
const [activeWallet, setActiveWallet] = useState<string>();
|
|
23
24
|
const [adaBalance, setAdaBalance] = useState<AssetAmount<AssetMetadata>>(
|
|
24
25
|
new AssetAmount<AssetMetadata>(0n),
|
|
@@ -59,7 +60,12 @@ export const useWalletObserverState = <
|
|
|
59
60
|
setFeeAddress(undefined);
|
|
60
61
|
setIsCip45(false);
|
|
61
62
|
setWillAutoConnect(false);
|
|
62
|
-
|
|
63
|
+
abortController?.abort();
|
|
64
|
+
}, [observer, abortController]);
|
|
65
|
+
|
|
66
|
+
const cancelConnectWallet = useCallback(() => {
|
|
67
|
+
return abortController.abort();
|
|
68
|
+
}, [abortController]);
|
|
63
69
|
|
|
64
70
|
const connectWallet = useCallback(
|
|
65
71
|
async (wallet: string) => {
|
|
@@ -67,12 +73,12 @@ export const useWalletObserverState = <
|
|
|
67
73
|
setSwitching(() => true);
|
|
68
74
|
}
|
|
69
75
|
|
|
70
|
-
const data = await observer.connectWallet(wallet);
|
|
76
|
+
const data = await observer.connectWallet(wallet, abortController.signal);
|
|
71
77
|
await syncWallet(data instanceof Error ? undefined : data);
|
|
72
78
|
setSwitching(() => false);
|
|
73
79
|
return observer.api;
|
|
74
80
|
},
|
|
75
|
-
[observer, setSwitching],
|
|
81
|
+
[observer, setSwitching, abortController],
|
|
76
82
|
);
|
|
77
83
|
|
|
78
84
|
const syncWallet = useCallback(
|
|
@@ -201,6 +207,7 @@ export const useWalletObserverState = <
|
|
|
201
207
|
balance,
|
|
202
208
|
collateral,
|
|
203
209
|
connectWallet,
|
|
210
|
+
cancelConnectWallet,
|
|
204
211
|
disconnect,
|
|
205
212
|
errorSyncing,
|
|
206
213
|
feeAddress,
|
|
@@ -29,6 +29,7 @@ export const useWalletObserver = <
|
|
|
29
29
|
balance: state.balance,
|
|
30
30
|
collateral: state.collateral,
|
|
31
31
|
connectWallet: state.connectWallet,
|
|
32
|
+
cancelConnectWallet: state.cancelConnectWallet,
|
|
32
33
|
disconnect: state.disconnect,
|
|
33
34
|
errorSyncing: state.errorSyncing,
|
|
34
35
|
feeAddress: state.feeAddress,
|
|
@@ -51,6 +52,7 @@ export const useWalletObserver = <
|
|
|
51
52
|
state.balance.size,
|
|
52
53
|
state.collateral,
|
|
53
54
|
state.connectWallet,
|
|
55
|
+
state.cancelConnectWallet,
|
|
54
56
|
state.disconnect,
|
|
55
57
|
state.errorSyncing,
|
|
56
58
|
state.feeAddress,
|