@dynamic-labs/global-wallet-client 4.0.0-alpha.48 → 4.0.0-alpha.49
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/CHANGELOG.md +29 -0
- package/_virtual/_tslib.cjs +36 -0
- package/_virtual/_tslib.js +32 -0
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +24 -4
- package/src/ethereum.cjs +12 -0
- package/src/ethereum.d.ts +1 -0
- package/src/ethereum.js +3 -0
- package/src/index.cjs +2 -2
- package/src/index.d.ts +1 -1
- package/src/index.js +1 -1
- package/src/lib/actions/connectAction/createConnectAction.cjs +38 -0
- package/src/lib/actions/connectAction/createConnectAction.d.ts +10 -0
- package/src/lib/actions/connectAction/createConnectAction.js +34 -0
- package/src/lib/actions/connectAction/index.d.ts +1 -0
- package/src/lib/actions/disconnectAction/createDisconnectAction.cjs +11 -0
- package/src/lib/actions/disconnectAction/createDisconnectAction.d.ts +7 -0
- package/src/lib/actions/disconnectAction/createDisconnectAction.js +7 -0
- package/src/lib/actions/disconnectAction/index.d.ts +1 -0
- package/src/lib/constants/logger.cjs +10 -0
- package/src/lib/constants/logger.d.ts +2 -0
- package/src/lib/constants/logger.js +6 -0
- package/src/lib/createGlobalWalletClient.cjs +44 -0
- package/src/lib/createGlobalWalletClient.d.ts +54 -0
- package/src/lib/createGlobalWalletClient.js +40 -0
- package/src/lib/ethereum/functions/announceEIP6963Provider/announceEIP6963Provider.cjs +29 -0
- package/src/lib/ethereum/functions/announceEIP6963Provider/announceEIP6963Provider.d.ts +12 -0
- package/src/lib/ethereum/functions/announceEIP6963Provider/announceEIP6963Provider.js +25 -0
- package/src/lib/ethereum/functions/announceEIP6963Provider/index.d.ts +1 -0
- package/src/lib/ethereum/functions/eip1193Provider/createEIP1193Provider.cjs +88 -0
- package/src/lib/ethereum/functions/eip1193Provider/createEIP1193Provider.d.ts +3 -0
- package/src/lib/ethereum/functions/eip1193Provider/createEIP1193Provider.js +84 -0
- package/src/lib/ethereum/functions/eip1193Provider/index.d.ts +1 -0
- package/src/lib/ethereum/index.d.ts +2 -0
- package/src/lib/functions/createClientEventEmitter/createClientEventEmitter.cjs +10 -0
- package/src/lib/functions/createClientEventEmitter/createClientEventEmitter.d.ts +6 -0
- package/src/lib/functions/createClientEventEmitter/createClientEventEmitter.js +6 -0
- package/src/lib/functions/createClientEventEmitter/index.d.ts +1 -0
- package/src/lib/functions/createPopupOpener/createPopupOpener.cjs +52 -0
- package/src/lib/functions/createPopupOpener/createPopupOpener.d.ts +15 -0
- package/src/lib/functions/createPopupOpener/createPopupOpener.js +48 -0
- package/src/lib/functions/createPopupOpener/index.d.ts +1 -0
- package/src/lib/functions/mergeAndEncryptParams/index.d.ts +1 -0
- package/src/lib/functions/mergeAndEncryptParams/mergeAndEncryptParams.cjs +23 -0
- package/src/lib/functions/mergeAndEncryptParams/mergeAndEncryptParams.d.ts +2 -0
- package/src/lib/functions/mergeAndEncryptParams/mergeAndEncryptParams.js +19 -0
- package/src/lib/functions/mergeURLSearchParams/index.d.ts +1 -0
- package/src/lib/functions/mergeURLSearchParams/mergeURLSearchParams.cjs +17 -0
- package/src/lib/functions/mergeURLSearchParams/mergeURLSearchParams.d.ts +6 -0
- package/src/lib/functions/mergeURLSearchParams/mergeURLSearchParams.js +13 -0
- package/src/lib/functions/onPopupClose/index.d.ts +1 -0
- package/src/lib/functions/onPopupClose/onPopupClose.cjs +22 -0
- package/src/lib/functions/onPopupClose/onPopupClose.d.ts +8 -0
- package/src/lib/functions/onPopupClose/onPopupClose.js +18 -0
- package/src/lib/functions/waitForPopupResponse/index.d.ts +1 -0
- package/src/lib/functions/waitForPopupResponse/waitForPopupResponse.cjs +55 -0
- package/src/lib/functions/waitForPopupResponse/waitForPopupResponse.d.ts +9 -0
- package/src/lib/functions/waitForPopupResponse/waitForPopupResponse.js +51 -0
- package/src/lib/store/createClientStore.cjs +67 -0
- package/src/lib/store/createClientStore.d.ts +34 -0
- package/src/lib/store/createClientStore.js +63 -0
- package/src/lib/store/index.d.ts +1 -0
- package/src/lib/types.d.ts +37 -0
- package/src/lib/global-wallet-client.cjs +0 -8
- package/src/lib/global-wallet-client.d.ts +0 -1
- package/src/lib/global-wallet-client.js +0 -4
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../../../_virtual/_tslib.cjs');
|
|
7
|
+
var viem = require('viem');
|
|
8
|
+
var eventemitter3 = require('eventemitter3');
|
|
9
|
+
|
|
10
|
+
const methodsHandledByPopup = ['personal_sign', 'eth_sendTransaction'];
|
|
11
|
+
const createEIP1193Provider = (client) => {
|
|
12
|
+
const eventEmitter = new eventemitter3.EventEmitter();
|
|
13
|
+
client.on('connect', () => {
|
|
14
|
+
var _a;
|
|
15
|
+
const chainId = (_a = client.ethereum) === null || _a === void 0 ? void 0 : _a.network;
|
|
16
|
+
if (!chainId)
|
|
17
|
+
return;
|
|
18
|
+
eventEmitter.emit('connect', {
|
|
19
|
+
chainId: viem.toHex(parseInt(chainId, 10)),
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
client.on('disconnect', () => {
|
|
23
|
+
eventEmitter.emit('disconnect', new viem.ProviderDisconnectedError(new Error('User disconnected')));
|
|
24
|
+
});
|
|
25
|
+
return {
|
|
26
|
+
on: eventEmitter.on.bind(eventEmitter),
|
|
27
|
+
removeListener: eventEmitter.removeListener.bind(eventEmitter),
|
|
28
|
+
request: ((_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ method, params }) {
|
|
29
|
+
var _b, _c, _d;
|
|
30
|
+
const wallets = (_c = (_b = client === null || client === void 0 ? void 0 : client.ethereum) === null || _b === void 0 ? void 0 : _b.wallets) !== null && _c !== void 0 ? _c : [];
|
|
31
|
+
const connectedAddresses = wallets.map((wallet) => wallet.address);
|
|
32
|
+
// Define if user is connected with ethereum wallets
|
|
33
|
+
const isConnected = connectedAddresses.length > 0;
|
|
34
|
+
if (method === 'eth_accounts') {
|
|
35
|
+
return isConnected ? connectedAddresses : [];
|
|
36
|
+
}
|
|
37
|
+
if (method === 'eth_requestAccounts') {
|
|
38
|
+
if (isConnected) {
|
|
39
|
+
return connectedAddresses;
|
|
40
|
+
}
|
|
41
|
+
return client
|
|
42
|
+
.connect({ chain: 'evm' })
|
|
43
|
+
.then(({ address }) => [address]);
|
|
44
|
+
}
|
|
45
|
+
if (method === 'wallet_revokePermissions') {
|
|
46
|
+
client.disconnect();
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
if (method === 'wallet_requestPermissions') {
|
|
50
|
+
if (isConnected) {
|
|
51
|
+
return connectedAddresses;
|
|
52
|
+
}
|
|
53
|
+
return client
|
|
54
|
+
.connect({ chain: 'evm' })
|
|
55
|
+
.then(({ address }) => [address]);
|
|
56
|
+
}
|
|
57
|
+
if (method === 'eth_chainId') {
|
|
58
|
+
return (_d = client.ethereum) === null || _d === void 0 ? void 0 : _d.network;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Opens the popup to request user confirmation for the request
|
|
62
|
+
* and to execute the request
|
|
63
|
+
*/
|
|
64
|
+
if (methodsHandledByPopup.includes(method) && params) {
|
|
65
|
+
const { data } = yield client
|
|
66
|
+
.invoke({
|
|
67
|
+
params: {
|
|
68
|
+
method,
|
|
69
|
+
params,
|
|
70
|
+
},
|
|
71
|
+
pathname: 'eth-request',
|
|
72
|
+
})
|
|
73
|
+
.catch((err) => {
|
|
74
|
+
if (err.message === 'Popup closed') {
|
|
75
|
+
throw new viem.UserRejectedRequestError(err);
|
|
76
|
+
}
|
|
77
|
+
throw err;
|
|
78
|
+
});
|
|
79
|
+
return data;
|
|
80
|
+
}
|
|
81
|
+
throw new viem.UnsupportedProviderMethodError(new Error('Method not implemented'), {
|
|
82
|
+
method,
|
|
83
|
+
});
|
|
84
|
+
})),
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
exports.createEIP1193Provider = createEIP1193Provider;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../../../_virtual/_tslib.js';
|
|
3
|
+
import { toHex, ProviderDisconnectedError, UserRejectedRequestError, UnsupportedProviderMethodError } from 'viem';
|
|
4
|
+
import { EventEmitter } from 'eventemitter3';
|
|
5
|
+
|
|
6
|
+
const methodsHandledByPopup = ['personal_sign', 'eth_sendTransaction'];
|
|
7
|
+
const createEIP1193Provider = (client) => {
|
|
8
|
+
const eventEmitter = new EventEmitter();
|
|
9
|
+
client.on('connect', () => {
|
|
10
|
+
var _a;
|
|
11
|
+
const chainId = (_a = client.ethereum) === null || _a === void 0 ? void 0 : _a.network;
|
|
12
|
+
if (!chainId)
|
|
13
|
+
return;
|
|
14
|
+
eventEmitter.emit('connect', {
|
|
15
|
+
chainId: toHex(parseInt(chainId, 10)),
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
client.on('disconnect', () => {
|
|
19
|
+
eventEmitter.emit('disconnect', new ProviderDisconnectedError(new Error('User disconnected')));
|
|
20
|
+
});
|
|
21
|
+
return {
|
|
22
|
+
on: eventEmitter.on.bind(eventEmitter),
|
|
23
|
+
removeListener: eventEmitter.removeListener.bind(eventEmitter),
|
|
24
|
+
request: ((_a) => __awaiter(void 0, [_a], void 0, function* ({ method, params }) {
|
|
25
|
+
var _b, _c, _d;
|
|
26
|
+
const wallets = (_c = (_b = client === null || client === void 0 ? void 0 : client.ethereum) === null || _b === void 0 ? void 0 : _b.wallets) !== null && _c !== void 0 ? _c : [];
|
|
27
|
+
const connectedAddresses = wallets.map((wallet) => wallet.address);
|
|
28
|
+
// Define if user is connected with ethereum wallets
|
|
29
|
+
const isConnected = connectedAddresses.length > 0;
|
|
30
|
+
if (method === 'eth_accounts') {
|
|
31
|
+
return isConnected ? connectedAddresses : [];
|
|
32
|
+
}
|
|
33
|
+
if (method === 'eth_requestAccounts') {
|
|
34
|
+
if (isConnected) {
|
|
35
|
+
return connectedAddresses;
|
|
36
|
+
}
|
|
37
|
+
return client
|
|
38
|
+
.connect({ chain: 'evm' })
|
|
39
|
+
.then(({ address }) => [address]);
|
|
40
|
+
}
|
|
41
|
+
if (method === 'wallet_revokePermissions') {
|
|
42
|
+
client.disconnect();
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
if (method === 'wallet_requestPermissions') {
|
|
46
|
+
if (isConnected) {
|
|
47
|
+
return connectedAddresses;
|
|
48
|
+
}
|
|
49
|
+
return client
|
|
50
|
+
.connect({ chain: 'evm' })
|
|
51
|
+
.then(({ address }) => [address]);
|
|
52
|
+
}
|
|
53
|
+
if (method === 'eth_chainId') {
|
|
54
|
+
return (_d = client.ethereum) === null || _d === void 0 ? void 0 : _d.network;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Opens the popup to request user confirmation for the request
|
|
58
|
+
* and to execute the request
|
|
59
|
+
*/
|
|
60
|
+
if (methodsHandledByPopup.includes(method) && params) {
|
|
61
|
+
const { data } = yield client
|
|
62
|
+
.invoke({
|
|
63
|
+
params: {
|
|
64
|
+
method,
|
|
65
|
+
params,
|
|
66
|
+
},
|
|
67
|
+
pathname: 'eth-request',
|
|
68
|
+
})
|
|
69
|
+
.catch((err) => {
|
|
70
|
+
if (err.message === 'Popup closed') {
|
|
71
|
+
throw new UserRejectedRequestError(err);
|
|
72
|
+
}
|
|
73
|
+
throw err;
|
|
74
|
+
});
|
|
75
|
+
return data;
|
|
76
|
+
}
|
|
77
|
+
throw new UnsupportedProviderMethodError(new Error('Method not implemented'), {
|
|
78
|
+
method,
|
|
79
|
+
});
|
|
80
|
+
})),
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export { createEIP1193Provider };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createEIP1193Provider } from './createEIP1193Provider';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var eventemitter3 = require('eventemitter3');
|
|
7
|
+
|
|
8
|
+
const createClientEventEmitter = () => new eventemitter3.EventEmitter();
|
|
9
|
+
|
|
10
|
+
exports.createClientEventEmitter = createClientEventEmitter;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createClientEventEmitter } from './createClientEventEmitter';
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../../_virtual/_tslib.cjs');
|
|
7
|
+
var utils = require('@dynamic-labs/utils');
|
|
8
|
+
var waitForPopupResponse = require('../waitForPopupResponse/waitForPopupResponse.cjs');
|
|
9
|
+
var onPopupClose = require('../onPopupClose/onPopupClose.cjs');
|
|
10
|
+
var mergeAndEncryptParams = require('../mergeAndEncryptParams/mergeAndEncryptParams.cjs');
|
|
11
|
+
|
|
12
|
+
const createPopupOpener = ({ environmentId, popupInfo, store }) => (_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ params, pathname, }) {
|
|
13
|
+
var _b, _c;
|
|
14
|
+
const popupFinalUrl = new URL(popupInfo.url);
|
|
15
|
+
const activeConnection = store.getActiveConnection();
|
|
16
|
+
const resultDeferredPromise = new utils.DeferredPromise();
|
|
17
|
+
const popupWidth = (_b = popupInfo.width) !== null && _b !== void 0 ? _b : 420;
|
|
18
|
+
const popupHeight = (_c = popupInfo.height) !== null && _c !== void 0 ? _c : 520;
|
|
19
|
+
// Setup popup URL and data
|
|
20
|
+
popupFinalUrl.hash = `/${pathname}`;
|
|
21
|
+
popupFinalUrl.searchParams.set('provider_env_id', environmentId);
|
|
22
|
+
popupFinalUrl.searchParams.set('requester_origin', window.location.origin);
|
|
23
|
+
// Encrypt params if active connection is present
|
|
24
|
+
if (params) {
|
|
25
|
+
yield mergeAndEncryptParams.mergeAndEncryptParams(popupFinalUrl, params, activeConnection);
|
|
26
|
+
}
|
|
27
|
+
// Open popup
|
|
28
|
+
const popup = window.open('', '_blank', `width=${popupWidth},height=${popupHeight}`);
|
|
29
|
+
if (popup === null) {
|
|
30
|
+
throw new Error('Failed to open popup');
|
|
31
|
+
}
|
|
32
|
+
waitForPopupResponse.waitForPopupResponse({
|
|
33
|
+
decode: (data) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
if (activeConnection && utils.isEncryptedMessage(data)) {
|
|
35
|
+
const decryptedData = yield utils.decryptMessage(activeConnection.sharedSecret, data.encryptedMessage, data.iv);
|
|
36
|
+
return JSON.parse(decryptedData);
|
|
37
|
+
}
|
|
38
|
+
return data;
|
|
39
|
+
}),
|
|
40
|
+
onError: (error) => resultDeferredPromise.reject(error),
|
|
41
|
+
onResponse: (response) => resultDeferredPromise.resolve(response.data),
|
|
42
|
+
popupUrl: popupFinalUrl.toString(),
|
|
43
|
+
});
|
|
44
|
+
onPopupClose.onPopupClose(popup, () => {
|
|
45
|
+
resultDeferredPromise.reject(new Error('Popup closed'));
|
|
46
|
+
});
|
|
47
|
+
// Load popup
|
|
48
|
+
popup.location.assign(popupFinalUrl.toString());
|
|
49
|
+
return resultDeferredPromise.promise.finally(popup.close);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
exports.createPopupOpener = createPopupOpener;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { GetPopupActionFuncArgs, GetPopupActionName, GetPopupActionResult, IPopupAction } from '@dynamic-labs/types';
|
|
2
|
+
import { PopupInfo } from '../../types';
|
|
3
|
+
import { ClientStore } from '../../store/createClientStore';
|
|
4
|
+
type CreatePopupOpenerProps = {
|
|
5
|
+
environmentId: string;
|
|
6
|
+
popupInfo: PopupInfo;
|
|
7
|
+
store: ClientStore;
|
|
8
|
+
};
|
|
9
|
+
type OpenPopupProps<TAction extends IPopupAction> = {
|
|
10
|
+
params: GetPopupActionFuncArgs<TAction>;
|
|
11
|
+
pathname: GetPopupActionName<TAction>;
|
|
12
|
+
};
|
|
13
|
+
export declare const createPopupOpener: ({ environmentId, popupInfo, store }: CreatePopupOpenerProps) => <TPopupAction extends IPopupAction>({ params, pathname, }: OpenPopupProps<TPopupAction>) => Promise<GetPopupActionResult<TPopupAction>>;
|
|
14
|
+
export type OpenPopup = ReturnType<typeof createPopupOpener>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../../_virtual/_tslib.js';
|
|
3
|
+
import { DeferredPromise, isEncryptedMessage, decryptMessage } from '@dynamic-labs/utils';
|
|
4
|
+
import { waitForPopupResponse } from '../waitForPopupResponse/waitForPopupResponse.js';
|
|
5
|
+
import { onPopupClose } from '../onPopupClose/onPopupClose.js';
|
|
6
|
+
import { mergeAndEncryptParams } from '../mergeAndEncryptParams/mergeAndEncryptParams.js';
|
|
7
|
+
|
|
8
|
+
const createPopupOpener = ({ environmentId, popupInfo, store }) => (_a) => __awaiter(void 0, [_a], void 0, function* ({ params, pathname, }) {
|
|
9
|
+
var _b, _c;
|
|
10
|
+
const popupFinalUrl = new URL(popupInfo.url);
|
|
11
|
+
const activeConnection = store.getActiveConnection();
|
|
12
|
+
const resultDeferredPromise = new DeferredPromise();
|
|
13
|
+
const popupWidth = (_b = popupInfo.width) !== null && _b !== void 0 ? _b : 420;
|
|
14
|
+
const popupHeight = (_c = popupInfo.height) !== null && _c !== void 0 ? _c : 520;
|
|
15
|
+
// Setup popup URL and data
|
|
16
|
+
popupFinalUrl.hash = `/${pathname}`;
|
|
17
|
+
popupFinalUrl.searchParams.set('provider_env_id', environmentId);
|
|
18
|
+
popupFinalUrl.searchParams.set('requester_origin', window.location.origin);
|
|
19
|
+
// Encrypt params if active connection is present
|
|
20
|
+
if (params) {
|
|
21
|
+
yield mergeAndEncryptParams(popupFinalUrl, params, activeConnection);
|
|
22
|
+
}
|
|
23
|
+
// Open popup
|
|
24
|
+
const popup = window.open('', '_blank', `width=${popupWidth},height=${popupHeight}`);
|
|
25
|
+
if (popup === null) {
|
|
26
|
+
throw new Error('Failed to open popup');
|
|
27
|
+
}
|
|
28
|
+
waitForPopupResponse({
|
|
29
|
+
decode: (data) => __awaiter(void 0, void 0, void 0, function* () {
|
|
30
|
+
if (activeConnection && isEncryptedMessage(data)) {
|
|
31
|
+
const decryptedData = yield decryptMessage(activeConnection.sharedSecret, data.encryptedMessage, data.iv);
|
|
32
|
+
return JSON.parse(decryptedData);
|
|
33
|
+
}
|
|
34
|
+
return data;
|
|
35
|
+
}),
|
|
36
|
+
onError: (error) => resultDeferredPromise.reject(error),
|
|
37
|
+
onResponse: (response) => resultDeferredPromise.resolve(response.data),
|
|
38
|
+
popupUrl: popupFinalUrl.toString(),
|
|
39
|
+
});
|
|
40
|
+
onPopupClose(popup, () => {
|
|
41
|
+
resultDeferredPromise.reject(new Error('Popup closed'));
|
|
42
|
+
});
|
|
43
|
+
// Load popup
|
|
44
|
+
popup.location.assign(popupFinalUrl.toString());
|
|
45
|
+
return resultDeferredPromise.promise.finally(popup.close);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
export { createPopupOpener };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createPopupOpener } from './createPopupOpener';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { mergeAndEncryptParams } from './mergeAndEncryptParams';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../../_virtual/_tslib.cjs');
|
|
7
|
+
var utils = require('@dynamic-labs/utils');
|
|
8
|
+
var mergeURLSearchParams = require('../mergeURLSearchParams/mergeURLSearchParams.cjs');
|
|
9
|
+
|
|
10
|
+
const mergeAndEncryptParams = (url, params, activeConnection) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
11
|
+
if (activeConnection) {
|
|
12
|
+
const encryptedParams = yield utils.encryptMessage(activeConnection.sharedSecret, JSON.stringify(params));
|
|
13
|
+
mergeURLSearchParams.mergeURLSearchParams(url.searchParams, new URLSearchParams({
|
|
14
|
+
encrypted_message: encryptedParams.encryptedMessage,
|
|
15
|
+
iv: encryptedParams.iv,
|
|
16
|
+
}));
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
mergeURLSearchParams.mergeURLSearchParams(url.searchParams, new URLSearchParams(params));
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
exports.mergeAndEncryptParams = mergeAndEncryptParams;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../../_virtual/_tslib.js';
|
|
3
|
+
import { encryptMessage } from '@dynamic-labs/utils';
|
|
4
|
+
import { mergeURLSearchParams } from '../mergeURLSearchParams/mergeURLSearchParams.js';
|
|
5
|
+
|
|
6
|
+
const mergeAndEncryptParams = (url, params, activeConnection) => __awaiter(void 0, void 0, void 0, function* () {
|
|
7
|
+
if (activeConnection) {
|
|
8
|
+
const encryptedParams = yield encryptMessage(activeConnection.sharedSecret, JSON.stringify(params));
|
|
9
|
+
mergeURLSearchParams(url.searchParams, new URLSearchParams({
|
|
10
|
+
encrypted_message: encryptedParams.encryptedMessage,
|
|
11
|
+
iv: encryptedParams.iv,
|
|
12
|
+
}));
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
mergeURLSearchParams(url.searchParams, new URLSearchParams(params));
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export { mergeAndEncryptParams };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { mergeURLSearchParams } from './mergeURLSearchParams';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Merges URL search parameters from a source into a target URLSearchParams object
|
|
8
|
+
* @param target - The URLSearchParams object to merge into
|
|
9
|
+
* @param source - The URLSearchParams object to merge from
|
|
10
|
+
*/
|
|
11
|
+
const mergeURLSearchParams = (target, source) => {
|
|
12
|
+
source.forEach((value, key) => {
|
|
13
|
+
target.set(key, value);
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
exports.mergeURLSearchParams = mergeURLSearchParams;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merges URL search parameters from a source into a target URLSearchParams object
|
|
3
|
+
* @param target - The URLSearchParams object to merge into
|
|
4
|
+
* @param source - The URLSearchParams object to merge from
|
|
5
|
+
*/
|
|
6
|
+
export declare const mergeURLSearchParams: (target: URLSearchParams, source: URLSearchParams) => void;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/**
|
|
3
|
+
* Merges URL search parameters from a source into a target URLSearchParams object
|
|
4
|
+
* @param target - The URLSearchParams object to merge into
|
|
5
|
+
* @param source - The URLSearchParams object to merge from
|
|
6
|
+
*/
|
|
7
|
+
const mergeURLSearchParams = (target, source) => {
|
|
8
|
+
source.forEach((value, key) => {
|
|
9
|
+
target.set(key, value);
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { mergeURLSearchParams };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { onPopupClose } from './onPopupClose';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Monitors a popup window and executes a callback when it closes
|
|
8
|
+
* @param popup - The popup window to monitor
|
|
9
|
+
* @param callback - Function to execute when the popup closes
|
|
10
|
+
* @param interval - The interval at which to check if the popup is closed,
|
|
11
|
+
* default is 50ms
|
|
12
|
+
*/
|
|
13
|
+
const onPopupClose = (popup, callback, interval = 50) => {
|
|
14
|
+
const intervalId = setInterval(() => {
|
|
15
|
+
if (popup.closed) {
|
|
16
|
+
clearInterval(intervalId);
|
|
17
|
+
callback();
|
|
18
|
+
}
|
|
19
|
+
}, interval);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
exports.onPopupClose = onPopupClose;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Monitors a popup window and executes a callback when it closes
|
|
3
|
+
* @param popup - The popup window to monitor
|
|
4
|
+
* @param callback - Function to execute when the popup closes
|
|
5
|
+
* @param interval - The interval at which to check if the popup is closed,
|
|
6
|
+
* default is 50ms
|
|
7
|
+
*/
|
|
8
|
+
export declare const onPopupClose: (popup: Window, callback: () => void, interval?: number) => void;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/**
|
|
3
|
+
* Monitors a popup window and executes a callback when it closes
|
|
4
|
+
* @param popup - The popup window to monitor
|
|
5
|
+
* @param callback - Function to execute when the popup closes
|
|
6
|
+
* @param interval - The interval at which to check if the popup is closed,
|
|
7
|
+
* default is 50ms
|
|
8
|
+
*/
|
|
9
|
+
const onPopupClose = (popup, callback, interval = 50) => {
|
|
10
|
+
const intervalId = setInterval(() => {
|
|
11
|
+
if (popup.closed) {
|
|
12
|
+
clearInterval(intervalId);
|
|
13
|
+
callback();
|
|
14
|
+
}
|
|
15
|
+
}, interval);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { onPopupClose };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { waitForPopupResponse } from './waitForPopupResponse';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../../_virtual/_tslib.cjs');
|
|
7
|
+
var logger = require('../../constants/logger.cjs');
|
|
8
|
+
|
|
9
|
+
const waitForPopupResponse = ({ popupUrl, decode, onResponse, onError, }) => {
|
|
10
|
+
const handleOnWindowEvent = (event) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
11
|
+
if (event.origin !== new URL(popupUrl).origin)
|
|
12
|
+
return;
|
|
13
|
+
let parsedData = null;
|
|
14
|
+
try {
|
|
15
|
+
parsedData = JSON.parse(event.data);
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
logger.logger.debug('Error parsing data from WebExtension iFrame event', err);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const decodedMessage = decode ? yield decode(parsedData) : parsedData;
|
|
22
|
+
const { error, success } = parseData(decodedMessage);
|
|
23
|
+
if (!error && !success) {
|
|
24
|
+
logger.logger.debug('Error validating event data schema from popup response event', parsedData);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (success) {
|
|
28
|
+
onResponse(success);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
32
|
+
onError(new Error(error.message));
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
window.addEventListener('message', handleOnWindowEvent);
|
|
36
|
+
return () => {
|
|
37
|
+
window.removeEventListener('message', handleOnWindowEvent);
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
const parseData = (data) => {
|
|
41
|
+
if (!data || typeof data !== 'object') {
|
|
42
|
+
return {};
|
|
43
|
+
}
|
|
44
|
+
// Check if it's a success response
|
|
45
|
+
if (data.name && data.data) {
|
|
46
|
+
return { success: data };
|
|
47
|
+
}
|
|
48
|
+
// Check if it's an error response
|
|
49
|
+
if (data.message) {
|
|
50
|
+
return { error: data };
|
|
51
|
+
}
|
|
52
|
+
return {};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
exports.waitForPopupResponse = waitForPopupResponse;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { IPopupActionSuccessResponse } from '@dynamic-labs/types';
|
|
2
|
+
type WaitForPopupResponseProps = {
|
|
3
|
+
popupUrl: string;
|
|
4
|
+
decode?: (data: any) => Promise<any>;
|
|
5
|
+
onResponse: (response: IPopupActionSuccessResponse) => void;
|
|
6
|
+
onError: (error: Error) => void;
|
|
7
|
+
};
|
|
8
|
+
export declare const waitForPopupResponse: ({ popupUrl, decode, onResponse, onError, }: WaitForPopupResponseProps) => () => void;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../../_virtual/_tslib.js';
|
|
3
|
+
import { logger } from '../../constants/logger.js';
|
|
4
|
+
|
|
5
|
+
const waitForPopupResponse = ({ popupUrl, decode, onResponse, onError, }) => {
|
|
6
|
+
const handleOnWindowEvent = (event) => __awaiter(void 0, void 0, void 0, function* () {
|
|
7
|
+
if (event.origin !== new URL(popupUrl).origin)
|
|
8
|
+
return;
|
|
9
|
+
let parsedData = null;
|
|
10
|
+
try {
|
|
11
|
+
parsedData = JSON.parse(event.data);
|
|
12
|
+
}
|
|
13
|
+
catch (err) {
|
|
14
|
+
logger.debug('Error parsing data from WebExtension iFrame event', err);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const decodedMessage = decode ? yield decode(parsedData) : parsedData;
|
|
18
|
+
const { error, success } = parseData(decodedMessage);
|
|
19
|
+
if (!error && !success) {
|
|
20
|
+
logger.debug('Error validating event data schema from popup response event', parsedData);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (success) {
|
|
24
|
+
onResponse(success);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
28
|
+
onError(new Error(error.message));
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
window.addEventListener('message', handleOnWindowEvent);
|
|
32
|
+
return () => {
|
|
33
|
+
window.removeEventListener('message', handleOnWindowEvent);
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
const parseData = (data) => {
|
|
37
|
+
if (!data || typeof data !== 'object') {
|
|
38
|
+
return {};
|
|
39
|
+
}
|
|
40
|
+
// Check if it's a success response
|
|
41
|
+
if (data.name && data.data) {
|
|
42
|
+
return { success: data };
|
|
43
|
+
}
|
|
44
|
+
// Check if it's an error response
|
|
45
|
+
if (data.message) {
|
|
46
|
+
return { error: data };
|
|
47
|
+
}
|
|
48
|
+
return {};
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export { waitForPopupResponse };
|