@dynamic-labs/global-wallet-client 4.0.0-alpha.51 → 4.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/CHANGELOG.md +81 -1
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +11 -7
- package/src/lib/actions/connectAction/createConnectAction.cjs +24 -12
- package/src/lib/actions/connectAction/createConnectAction.d.ts +4 -3
- package/src/lib/actions/connectAction/createConnectAction.js +24 -12
- package/src/lib/createGlobalWalletClient.cjs +6 -6
- package/src/lib/createGlobalWalletClient.d.ts +11 -6
- package/src/lib/createGlobalWalletClient.js +6 -6
- package/src/lib/ethereum/functions/eip1193Provider/createEIP1193Provider.cjs +88 -56
- package/src/lib/ethereum/functions/eip1193Provider/createEIP1193Provider.js +89 -57
- package/src/lib/ethereum/functions/isNetworkSupported/index.d.ts +1 -0
- package/src/lib/ethereum/functions/isNetworkSupported/isNetworkSupported.cjs +8 -0
- package/src/lib/ethereum/functions/isNetworkSupported/isNetworkSupported.d.ts +3 -0
- package/src/lib/ethereum/functions/isNetworkSupported/isNetworkSupported.js +4 -0
- package/src/lib/functions/createEthereumModule/createEthereumModule.cjs +24 -0
- package/src/lib/functions/createEthereumModule/createEthereumModule.d.ts +11 -0
- package/src/lib/functions/createEthereumModule/createEthereumModule.js +20 -0
- package/src/lib/functions/createEthereumModule/index.d.ts +1 -0
- package/src/lib/functions/createPopupOpener/createPopupOpener.cjs +77 -38
- package/src/lib/functions/createPopupOpener/createPopupOpener.d.ts +2 -1
- package/src/lib/functions/createPopupOpener/createPopupOpener.js +77 -38
- package/src/lib/functions/createSolanaModule/createSolanaModule.cjs +17 -0
- package/src/lib/functions/createSolanaModule/createSolanaModule.d.ts +9 -0
- package/src/lib/functions/createSolanaModule/createSolanaModule.js +13 -0
- package/src/lib/functions/createSolanaModule/index.d.ts +1 -0
- package/src/lib/functions/mergeAndEncryptParams/mergeAndEncryptParams.d.ts +2 -2
- package/src/lib/solana/functions/convertBaseWalletToWalletAccount/convertBaseWalletToWalletAccount.cjs +36 -0
- package/src/lib/solana/functions/convertBaseWalletToWalletAccount/convertBaseWalletToWalletAccount.d.ts +4 -0
- package/src/lib/solana/functions/convertBaseWalletToWalletAccount/convertBaseWalletToWalletAccount.js +32 -0
- package/src/lib/solana/functions/convertBaseWalletToWalletAccount/index.d.ts +1 -0
- package/src/lib/solana/functions/createSolanaSignAndSendTransactionFeature/createSolanaSignAndSendTransactionFeature.cjs +14 -0
- package/src/lib/solana/functions/createSolanaSignAndSendTransactionFeature/createSolanaSignAndSendTransactionFeature.d.ts +2 -0
- package/src/lib/solana/functions/createSolanaSignAndSendTransactionFeature/createSolanaSignAndSendTransactionFeature.js +10 -0
- package/src/lib/solana/functions/createSolanaSignAndSendTransactionFeature/index.d.ts +1 -0
- package/src/lib/solana/functions/createSolanaSignMessageFeature/createSolanaSignMessageFeature.cjs +29 -0
- package/src/lib/solana/functions/createSolanaSignMessageFeature/createSolanaSignMessageFeature.d.ts +3 -0
- package/src/lib/solana/functions/createSolanaSignMessageFeature/createSolanaSignMessageFeature.js +25 -0
- package/src/lib/solana/functions/createSolanaSignMessageFeature/index.d.ts +1 -0
- package/src/lib/solana/functions/createSolanaSignTransactionFeature/createSolanaSignTransactionFeature.cjs +14 -0
- package/src/lib/solana/functions/createSolanaSignTransactionFeature/createSolanaSignTransactionFeature.d.ts +2 -0
- package/src/lib/solana/functions/createSolanaSignTransactionFeature/createSolanaSignTransactionFeature.js +10 -0
- package/src/lib/solana/functions/createSolanaSignTransactionFeature/index.d.ts +1 -0
- package/src/lib/solana/functions/createSolanaWallet/createSolanaWallet.cjs +47 -0
- package/src/lib/solana/functions/createSolanaWallet/createSolanaWallet.d.ts +17 -0
- package/src/lib/solana/functions/createSolanaWallet/createSolanaWallet.js +43 -0
- package/src/lib/solana/functions/createSolanaWallet/index.d.ts +1 -0
- package/src/lib/solana/functions/createStandardConnectFeature/createStandardConnectFeature.cjs +21 -0
- package/src/lib/solana/functions/createStandardConnectFeature/createStandardConnectFeature.d.ts +3 -0
- package/src/lib/solana/functions/createStandardConnectFeature/createStandardConnectFeature.js +17 -0
- package/src/lib/solana/functions/createStandardConnectFeature/index.d.ts +1 -0
- package/src/lib/solana/functions/createStandardDisconnectFeature/createStandardDisconnectFeature.cjs +13 -0
- package/src/lib/solana/functions/createStandardDisconnectFeature/createStandardDisconnectFeature.d.ts +3 -0
- package/src/lib/solana/functions/createStandardDisconnectFeature/createStandardDisconnectFeature.js +9 -0
- package/src/lib/solana/functions/createStandardDisconnectFeature/index.d.ts +1 -0
- package/src/lib/solana/functions/createStandardEventsFeature/createStandardEventsFeature.cjs +38 -0
- package/src/lib/solana/functions/createStandardEventsFeature/createStandardEventsFeature.d.ts +13 -0
- package/src/lib/solana/functions/createStandardEventsFeature/createStandardEventsFeature.js +34 -0
- package/src/lib/solana/functions/createStandardEventsFeature/index.d.ts +1 -0
- package/src/lib/solana/functions/getSolanaChains/getSolanaChains.cjs +13 -0
- package/src/lib/solana/functions/getSolanaChains/getSolanaChains.d.ts +2 -0
- package/src/lib/solana/functions/getSolanaChains/getSolanaChains.js +9 -0
- package/src/lib/solana/functions/getSolanaChains/index.d.ts +1 -0
- package/src/lib/solana/index.d.ts +1 -0
- package/src/lib/store/createClientStore.cjs +37 -15
- package/src/lib/store/createClientStore.d.ts +51 -13
- package/src/lib/store/createClientStore.js +37 -15
- package/src/lib/types.d.ts +0 -28
- package/src/solana.cjs +10 -0
- package/src/solana.d.ts +1 -0
- package/src/solana.js +2 -0
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
import { __awaiter } from '../../../../../_virtual/_tslib.js';
|
|
3
|
-
import { toHex, ProviderDisconnectedError,
|
|
3
|
+
import { toHex, ProviderDisconnectedError, UnsupportedProviderMethodError, SwitchChainError, fromHex, UserRejectedRequestError } from 'viem';
|
|
4
4
|
import { EventEmitter } from 'eventemitter3';
|
|
5
|
+
import { isNetworkSupported } from '../isNetworkSupported/isNetworkSupported.js';
|
|
5
6
|
|
|
6
|
-
const methodsHandledByPopup = ['personal_sign', 'eth_sendTransaction'];
|
|
7
7
|
const createEIP1193Provider = (client) => {
|
|
8
8
|
const eventEmitter = new EventEmitter();
|
|
9
9
|
client.on('connect', () => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (!chainId)
|
|
10
|
+
const currentNetwork = client.ethereum.currentNetworkId;
|
|
11
|
+
if (!currentNetwork)
|
|
13
12
|
return;
|
|
14
13
|
eventEmitter.emit('connect', {
|
|
15
|
-
chainId: toHex(
|
|
14
|
+
chainId: toHex(currentNetwork),
|
|
16
15
|
});
|
|
17
16
|
});
|
|
18
17
|
client.on('disconnect', () => {
|
|
@@ -22,57 +21,23 @@ const createEIP1193Provider = (client) => {
|
|
|
22
21
|
on: eventEmitter.on.bind(eventEmitter),
|
|
23
22
|
removeListener: eventEmitter.removeListener.bind(eventEmitter),
|
|
24
23
|
request: ((_a) => __awaiter(void 0, [_a], void 0, function* ({ method, params }) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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;
|
|
24
|
+
const methodHandlerMap = {
|
|
25
|
+
eth_accounts: () => handleEthAccounts(client),
|
|
26
|
+
eth_chainId: () => handleEthChainId(client),
|
|
27
|
+
eth_requestAccounts: () => handleEthRequestAccounts(client),
|
|
28
|
+
eth_sendTransaction: () => handleMethodInPopup({
|
|
29
|
+
client,
|
|
30
|
+
method: 'eth_sendTransaction',
|
|
31
|
+
params,
|
|
32
|
+
}),
|
|
33
|
+
personal_sign: () => handleMethodInPopup({ client, method: 'personal_sign', params }),
|
|
34
|
+
wallet_requestPermissions: () => handleEthRequestAccounts(client),
|
|
35
|
+
wallet_revokePermissions: () => handleWalletRevokePermissions(client),
|
|
36
|
+
wallet_switchEthereumChain: () => handleWalletSwitchEthereumChain(client, eventEmitter, params),
|
|
37
|
+
};
|
|
38
|
+
const handler = methodHandlerMap[method];
|
|
39
|
+
if (handler) {
|
|
40
|
+
return handler();
|
|
76
41
|
}
|
|
77
42
|
throw new UnsupportedProviderMethodError(new Error('Method not implemented'), {
|
|
78
43
|
method,
|
|
@@ -80,5 +45,72 @@ const createEIP1193Provider = (client) => {
|
|
|
80
45
|
})),
|
|
81
46
|
};
|
|
82
47
|
};
|
|
48
|
+
// Utility functions
|
|
49
|
+
const isConnected = (client) => {
|
|
50
|
+
var _a, _b;
|
|
51
|
+
const wallets = (_b = (_a = client === null || client === void 0 ? void 0 : client.ethereum) === null || _a === void 0 ? void 0 : _a.wallets) !== null && _b !== void 0 ? _b : [];
|
|
52
|
+
return wallets.length > 0;
|
|
53
|
+
};
|
|
54
|
+
const getConnectedAddresses = (client) => {
|
|
55
|
+
var _a, _b;
|
|
56
|
+
const wallets = (_b = (_a = client === null || client === void 0 ? void 0 : client.ethereum) === null || _a === void 0 ? void 0 : _a.wallets) !== null && _b !== void 0 ? _b : [];
|
|
57
|
+
return wallets.map((wallet) => wallet.address);
|
|
58
|
+
};
|
|
59
|
+
// Method handlers
|
|
60
|
+
const handleEthAccounts = (client) => isConnected(client) ? getConnectedAddresses(client) : [];
|
|
61
|
+
const handleEthRequestAccounts = (client) => {
|
|
62
|
+
if (isConnected(client)) {
|
|
63
|
+
return getConnectedAddresses(client);
|
|
64
|
+
}
|
|
65
|
+
return client.connect({ chain: 'evm' }).then(({ address }) => [address]);
|
|
66
|
+
};
|
|
67
|
+
const handleWalletRevokePermissions = (client) => {
|
|
68
|
+
client.disconnect();
|
|
69
|
+
return null;
|
|
70
|
+
};
|
|
71
|
+
const handleEthChainId = (client) => {
|
|
72
|
+
if (client.ethereum.currentNetworkId) {
|
|
73
|
+
return toHex(client.ethereum.currentNetworkId);
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
};
|
|
77
|
+
const handleWalletSwitchEthereumChain = (client, eventEmitter, params) => {
|
|
78
|
+
var _a, _b;
|
|
79
|
+
if (!Array.isArray(params) || params.length === 0 || !params[0].chainId) {
|
|
80
|
+
throw new Error('Invalid parameters for wallet_switchEthereumChain');
|
|
81
|
+
}
|
|
82
|
+
const [{ chainId }] = params;
|
|
83
|
+
const supportedNetworks = (_b = (_a = client === null || client === void 0 ? void 0 : client.ethereum) === null || _a === void 0 ? void 0 : _a.supportedNetworks) !== null && _b !== void 0 ? _b : [];
|
|
84
|
+
if (supportedNetworks.length === 0) {
|
|
85
|
+
throw new SwitchChainError(new Error('No supported networks'));
|
|
86
|
+
}
|
|
87
|
+
if (!isNetworkSupported(fromHex(chainId, 'number'), supportedNetworks)) {
|
|
88
|
+
throw new SwitchChainError(new Error('Chain not supported'));
|
|
89
|
+
}
|
|
90
|
+
client.ethereum.switchNetwork(fromHex(chainId, 'number'));
|
|
91
|
+
eventEmitter.emit('chainChanged', chainId);
|
|
92
|
+
return null;
|
|
93
|
+
};
|
|
94
|
+
const handleMethodInPopup = (_a) => __awaiter(void 0, [_a], void 0, function* ({ client, method, params, }) {
|
|
95
|
+
if (!client.ethereum.currentNetworkId) {
|
|
96
|
+
throw new Error('No network id found');
|
|
97
|
+
}
|
|
98
|
+
const { data } = yield client
|
|
99
|
+
.invoke({
|
|
100
|
+
params: {
|
|
101
|
+
chainId: client.ethereum.currentNetworkId,
|
|
102
|
+
method,
|
|
103
|
+
params,
|
|
104
|
+
},
|
|
105
|
+
pathname: 'eth-request',
|
|
106
|
+
})
|
|
107
|
+
.catch((err) => {
|
|
108
|
+
if (err.message === 'Popup closed') {
|
|
109
|
+
throw new UserRejectedRequestError(err);
|
|
110
|
+
}
|
|
111
|
+
throw err;
|
|
112
|
+
});
|
|
113
|
+
return data;
|
|
114
|
+
});
|
|
83
115
|
|
|
84
116
|
export { createEIP1193Provider };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { isNetworkSupported } from './isNetworkSupported';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
const isNetworkSupported = (chainId, supportedNetworks) => supportedNetworks.some((network) => network.chainId === chainId);
|
|
7
|
+
|
|
8
|
+
exports.isNetworkSupported = isNetworkSupported;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
const createEthereumModule = ({ store }) => ({
|
|
7
|
+
get currentNetworkId() {
|
|
8
|
+
var _a, _b;
|
|
9
|
+
return (_b = (_a = store.ethereum) === null || _a === void 0 ? void 0 : _a.currentNetworkId) !== null && _b !== void 0 ? _b : null;
|
|
10
|
+
},
|
|
11
|
+
get supportedNetworks() {
|
|
12
|
+
var _a, _b;
|
|
13
|
+
return (_b = (_a = store.ethereum) === null || _a === void 0 ? void 0 : _a.supportedNetworks) !== null && _b !== void 0 ? _b : [];
|
|
14
|
+
},
|
|
15
|
+
switchNetwork: (network) => {
|
|
16
|
+
store.switchEthereumNetwork(network);
|
|
17
|
+
},
|
|
18
|
+
get wallets() {
|
|
19
|
+
var _a;
|
|
20
|
+
return (_a = store.wallets.filter(({ chain }) => chain === 'EVM')) !== null && _a !== void 0 ? _a : [];
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
exports.createEthereumModule = createEthereumModule;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ClientStore } from '../../store';
|
|
2
|
+
export declare const createEthereumModule: ({ store }: {
|
|
3
|
+
store: ClientStore;
|
|
4
|
+
}) => {
|
|
5
|
+
readonly currentNetworkId: number | null;
|
|
6
|
+
readonly supportedNetworks: {
|
|
7
|
+
chainId: number;
|
|
8
|
+
}[];
|
|
9
|
+
switchNetwork: (network: number) => void;
|
|
10
|
+
readonly wallets: import("dist/packages/types/src").BaseWallet[];
|
|
11
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
const createEthereumModule = ({ store }) => ({
|
|
3
|
+
get currentNetworkId() {
|
|
4
|
+
var _a, _b;
|
|
5
|
+
return (_b = (_a = store.ethereum) === null || _a === void 0 ? void 0 : _a.currentNetworkId) !== null && _b !== void 0 ? _b : null;
|
|
6
|
+
},
|
|
7
|
+
get supportedNetworks() {
|
|
8
|
+
var _a, _b;
|
|
9
|
+
return (_b = (_a = store.ethereum) === null || _a === void 0 ? void 0 : _a.supportedNetworks) !== null && _b !== void 0 ? _b : [];
|
|
10
|
+
},
|
|
11
|
+
switchNetwork: (network) => {
|
|
12
|
+
store.switchEthereumNetwork(network);
|
|
13
|
+
},
|
|
14
|
+
get wallets() {
|
|
15
|
+
var _a;
|
|
16
|
+
return (_a = store.wallets.filter(({ chain }) => chain === 'EVM')) !== null && _a !== void 0 ? _a : [];
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export { createEthereumModule };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createEthereumModule } from './createEthereumModule';
|
|
@@ -9,44 +9,83 @@ var waitForPopupResponse = require('../waitForPopupResponse/waitForPopupResponse
|
|
|
9
9
|
var onPopupClose = require('../onPopupClose/onPopupClose.cjs');
|
|
10
10
|
var mergeAndEncryptParams = require('../mergeAndEncryptParams/mergeAndEncryptParams.cjs');
|
|
11
11
|
|
|
12
|
-
const createPopupOpener = ({ environmentId, popupInfo, store
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
12
|
+
const createPopupOpener = ({ environmentId, popupInfo, store, popupActionTimeout = 1000, // Wait page up to 1 second for new actions before closing
|
|
13
|
+
}) => {
|
|
14
|
+
let popup = null;
|
|
15
|
+
let didOpenPopup = false;
|
|
16
|
+
let lastActionId = null;
|
|
17
|
+
const closePopup = () => {
|
|
18
|
+
popup === null || popup === void 0 ? void 0 : popup.close();
|
|
19
|
+
popup = null;
|
|
20
|
+
didOpenPopup = false;
|
|
21
|
+
lastActionId = null;
|
|
22
|
+
};
|
|
23
|
+
return (_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ params, pathname, }) {
|
|
24
|
+
var _b, _c;
|
|
25
|
+
// Used as a unique identifier for the action
|
|
26
|
+
const actionId = new Date().getTime();
|
|
27
|
+
lastActionId = actionId;
|
|
28
|
+
const popupFinalUrl = new URL(popupInfo.url);
|
|
29
|
+
const activeConnection = store.getActiveConnection();
|
|
30
|
+
const resultDeferredPromise = new utils.DeferredPromise();
|
|
31
|
+
const popupWidth = (_b = popupInfo.width) !== null && _b !== void 0 ? _b : 420;
|
|
32
|
+
const popupHeight = (_c = popupInfo.height) !== null && _c !== void 0 ? _c : 520;
|
|
33
|
+
// Setup popup URL and data
|
|
34
|
+
popupFinalUrl.hash = `/${pathname}`;
|
|
35
|
+
popupFinalUrl.searchParams.set('provider_env_id', environmentId);
|
|
36
|
+
popupFinalUrl.searchParams.set('requester_origin', window.location.origin);
|
|
37
|
+
// Encrypt params if active connection is present
|
|
38
|
+
if (params) {
|
|
39
|
+
yield mergeAndEncryptParams.mergeAndEncryptParams(popupFinalUrl, params, activeConnection);
|
|
40
|
+
}
|
|
41
|
+
if (popup === null) {
|
|
42
|
+
// Open popup
|
|
43
|
+
popup = window.open('', '_blank', `width=${popupWidth},height=${popupHeight}`);
|
|
44
|
+
}
|
|
45
|
+
if (popup === null) {
|
|
46
|
+
throw new Error('Failed to open popup');
|
|
47
|
+
}
|
|
48
|
+
waitForPopupResponse.waitForPopupResponse({
|
|
49
|
+
decode: (data) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
50
|
+
if (activeConnection && utils.isEncryptedMessage(data)) {
|
|
51
|
+
const decryptedData = yield utils.decryptMessage(activeConnection.sharedSecret, data.encryptedMessage, data.iv);
|
|
52
|
+
return JSON.parse(decryptedData);
|
|
53
|
+
}
|
|
54
|
+
return data;
|
|
55
|
+
}),
|
|
56
|
+
onError: (error) => resultDeferredPromise.reject(error),
|
|
57
|
+
onResponse: (response) => resultDeferredPromise.resolve(response.data),
|
|
58
|
+
popupUrl: popupFinalUrl.toString(),
|
|
59
|
+
});
|
|
60
|
+
onPopupClose.onPopupClose(popup, () => {
|
|
61
|
+
resultDeferredPromise.reject(new Error('Popup closed'));
|
|
62
|
+
});
|
|
63
|
+
/**
|
|
64
|
+
* Assign action to popup
|
|
65
|
+
*/
|
|
66
|
+
if (didOpenPopup) {
|
|
67
|
+
popup === null || popup === void 0 ? void 0 : popup.postMessage({
|
|
68
|
+
actionPath: popupFinalUrl.search + popupFinalUrl.hash,
|
|
69
|
+
event: 'new-action',
|
|
70
|
+
}, popupFinalUrl.origin);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
// Load popup
|
|
74
|
+
popup.location.assign(popupFinalUrl.toString());
|
|
75
|
+
}
|
|
76
|
+
didOpenPopup = true;
|
|
77
|
+
return resultDeferredPromise.promise.finally(() => {
|
|
78
|
+
/**
|
|
79
|
+
* Sets a timeout to close the popup.
|
|
80
|
+
* It will only no new actions were fired.
|
|
81
|
+
*/
|
|
82
|
+
setTimeout(() => {
|
|
83
|
+
if (lastActionId === actionId) {
|
|
84
|
+
closePopup();
|
|
85
|
+
}
|
|
86
|
+
}, popupActionTimeout);
|
|
87
|
+
});
|
|
43
88
|
});
|
|
44
|
-
|
|
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
|
-
});
|
|
89
|
+
};
|
|
51
90
|
|
|
52
91
|
exports.createPopupOpener = createPopupOpener;
|
|
@@ -5,11 +5,12 @@ type CreatePopupOpenerProps = {
|
|
|
5
5
|
environmentId: string;
|
|
6
6
|
popupInfo: PopupInfo;
|
|
7
7
|
store: ClientStore;
|
|
8
|
+
popupActionTimeout?: number;
|
|
8
9
|
};
|
|
9
10
|
type OpenPopupProps<TAction extends IPopupAction> = {
|
|
10
11
|
params: GetPopupActionFuncArgs<TAction>;
|
|
11
12
|
pathname: GetPopupActionName<TAction>;
|
|
12
13
|
};
|
|
13
|
-
export declare const createPopupOpener: ({ environmentId, popupInfo, store }: CreatePopupOpenerProps) => <TPopupAction extends IPopupAction>({ params, pathname, }: OpenPopupProps<TPopupAction>) => Promise<GetPopupActionResult<TPopupAction>>;
|
|
14
|
+
export declare const createPopupOpener: ({ environmentId, popupInfo, store, popupActionTimeout, }: CreatePopupOpenerProps) => <TPopupAction extends IPopupAction>({ params, pathname, }: OpenPopupProps<TPopupAction>) => Promise<GetPopupActionResult<TPopupAction>>;
|
|
14
15
|
export type OpenPopup = ReturnType<typeof createPopupOpener>;
|
|
15
16
|
export {};
|
|
@@ -5,44 +5,83 @@ import { waitForPopupResponse } from '../waitForPopupResponse/waitForPopupRespon
|
|
|
5
5
|
import { onPopupClose } from '../onPopupClose/onPopupClose.js';
|
|
6
6
|
import { mergeAndEncryptParams } from '../mergeAndEncryptParams/mergeAndEncryptParams.js';
|
|
7
7
|
|
|
8
|
-
const createPopupOpener = ({ environmentId, popupInfo, store
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
8
|
+
const createPopupOpener = ({ environmentId, popupInfo, store, popupActionTimeout = 1000, // Wait page up to 1 second for new actions before closing
|
|
9
|
+
}) => {
|
|
10
|
+
let popup = null;
|
|
11
|
+
let didOpenPopup = false;
|
|
12
|
+
let lastActionId = null;
|
|
13
|
+
const closePopup = () => {
|
|
14
|
+
popup === null || popup === void 0 ? void 0 : popup.close();
|
|
15
|
+
popup = null;
|
|
16
|
+
didOpenPopup = false;
|
|
17
|
+
lastActionId = null;
|
|
18
|
+
};
|
|
19
|
+
return (_a) => __awaiter(void 0, [_a], void 0, function* ({ params, pathname, }) {
|
|
20
|
+
var _b, _c;
|
|
21
|
+
// Used as a unique identifier for the action
|
|
22
|
+
const actionId = new Date().getTime();
|
|
23
|
+
lastActionId = actionId;
|
|
24
|
+
const popupFinalUrl = new URL(popupInfo.url);
|
|
25
|
+
const activeConnection = store.getActiveConnection();
|
|
26
|
+
const resultDeferredPromise = new DeferredPromise();
|
|
27
|
+
const popupWidth = (_b = popupInfo.width) !== null && _b !== void 0 ? _b : 420;
|
|
28
|
+
const popupHeight = (_c = popupInfo.height) !== null && _c !== void 0 ? _c : 520;
|
|
29
|
+
// Setup popup URL and data
|
|
30
|
+
popupFinalUrl.hash = `/${pathname}`;
|
|
31
|
+
popupFinalUrl.searchParams.set('provider_env_id', environmentId);
|
|
32
|
+
popupFinalUrl.searchParams.set('requester_origin', window.location.origin);
|
|
33
|
+
// Encrypt params if active connection is present
|
|
34
|
+
if (params) {
|
|
35
|
+
yield mergeAndEncryptParams(popupFinalUrl, params, activeConnection);
|
|
36
|
+
}
|
|
37
|
+
if (popup === null) {
|
|
38
|
+
// Open popup
|
|
39
|
+
popup = window.open('', '_blank', `width=${popupWidth},height=${popupHeight}`);
|
|
40
|
+
}
|
|
41
|
+
if (popup === null) {
|
|
42
|
+
throw new Error('Failed to open popup');
|
|
43
|
+
}
|
|
44
|
+
waitForPopupResponse({
|
|
45
|
+
decode: (data) => __awaiter(void 0, void 0, void 0, function* () {
|
|
46
|
+
if (activeConnection && isEncryptedMessage(data)) {
|
|
47
|
+
const decryptedData = yield decryptMessage(activeConnection.sharedSecret, data.encryptedMessage, data.iv);
|
|
48
|
+
return JSON.parse(decryptedData);
|
|
49
|
+
}
|
|
50
|
+
return data;
|
|
51
|
+
}),
|
|
52
|
+
onError: (error) => resultDeferredPromise.reject(error),
|
|
53
|
+
onResponse: (response) => resultDeferredPromise.resolve(response.data),
|
|
54
|
+
popupUrl: popupFinalUrl.toString(),
|
|
55
|
+
});
|
|
56
|
+
onPopupClose(popup, () => {
|
|
57
|
+
resultDeferredPromise.reject(new Error('Popup closed'));
|
|
58
|
+
});
|
|
59
|
+
/**
|
|
60
|
+
* Assign action to popup
|
|
61
|
+
*/
|
|
62
|
+
if (didOpenPopup) {
|
|
63
|
+
popup === null || popup === void 0 ? void 0 : popup.postMessage({
|
|
64
|
+
actionPath: popupFinalUrl.search + popupFinalUrl.hash,
|
|
65
|
+
event: 'new-action',
|
|
66
|
+
}, popupFinalUrl.origin);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
// Load popup
|
|
70
|
+
popup.location.assign(popupFinalUrl.toString());
|
|
71
|
+
}
|
|
72
|
+
didOpenPopup = true;
|
|
73
|
+
return resultDeferredPromise.promise.finally(() => {
|
|
74
|
+
/**
|
|
75
|
+
* Sets a timeout to close the popup.
|
|
76
|
+
* It will only no new actions were fired.
|
|
77
|
+
*/
|
|
78
|
+
setTimeout(() => {
|
|
79
|
+
if (lastActionId === actionId) {
|
|
80
|
+
closePopup();
|
|
81
|
+
}
|
|
82
|
+
}, popupActionTimeout);
|
|
83
|
+
});
|
|
39
84
|
});
|
|
40
|
-
|
|
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
|
-
});
|
|
85
|
+
};
|
|
47
86
|
|
|
48
87
|
export { createPopupOpener };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
const createSolanaModule = ({ store }) => ({
|
|
7
|
+
get currentNetwork() {
|
|
8
|
+
var _a, _b;
|
|
9
|
+
return (_b = (_a = store.solana) === null || _a === void 0 ? void 0 : _a.currentNetwork) !== null && _b !== void 0 ? _b : null;
|
|
10
|
+
},
|
|
11
|
+
get wallets() {
|
|
12
|
+
var _a;
|
|
13
|
+
return (_a = store.wallets.filter(({ chain }) => chain === 'SOL')) !== null && _a !== void 0 ? _a : [];
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
exports.createSolanaModule = createSolanaModule;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ClientStore } from '../../store';
|
|
2
|
+
type CreateSolanaModuleProps = {
|
|
3
|
+
store: ClientStore;
|
|
4
|
+
};
|
|
5
|
+
export declare const createSolanaModule: ({ store }: CreateSolanaModuleProps) => {
|
|
6
|
+
readonly currentNetwork: string | null;
|
|
7
|
+
readonly wallets: import("dist/packages/types/src").BaseWallet[];
|
|
8
|
+
};
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
const createSolanaModule = ({ store }) => ({
|
|
3
|
+
get currentNetwork() {
|
|
4
|
+
var _a, _b;
|
|
5
|
+
return (_b = (_a = store.solana) === null || _a === void 0 ? void 0 : _a.currentNetwork) !== null && _b !== void 0 ? _b : null;
|
|
6
|
+
},
|
|
7
|
+
get wallets() {
|
|
8
|
+
var _a;
|
|
9
|
+
return (_a = store.wallets.filter(({ chain }) => chain === 'SOL')) !== null && _a !== void 0 ? _a : [];
|
|
10
|
+
},
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export { createSolanaModule };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createSolanaModule } from './createSolanaModule';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const mergeAndEncryptParams: (url: URL, params: Record<string, string>, activeConnection:
|
|
1
|
+
import { GlobalWalletConnection } from '@dynamic-labs/types';
|
|
2
|
+
export declare const mergeAndEncryptParams: (url: URL, params: Record<string, string>, activeConnection: GlobalWalletConnection | null) => Promise<void>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var web3_js = require('@solana/web3.js');
|
|
7
|
+
var getSolanaChains = require('../getSolanaChains/getSolanaChains.cjs');
|
|
8
|
+
|
|
9
|
+
const convertBaseWalletToWalletAccount = (wallet, client) => {
|
|
10
|
+
try {
|
|
11
|
+
const publicKey = new web3_js.PublicKey(wallet.address);
|
|
12
|
+
// Validate the public key is valid base58
|
|
13
|
+
if (!web3_js.PublicKey.isOnCurve(publicKey.toBytes())) {
|
|
14
|
+
throw new Error('Invalid public key');
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
address: publicKey.toBase58(),
|
|
18
|
+
get chains() {
|
|
19
|
+
return getSolanaChains.getSolanaChains(client);
|
|
20
|
+
},
|
|
21
|
+
features: [
|
|
22
|
+
'solana:signAndSendTransaction',
|
|
23
|
+
'solana:signTransaction',
|
|
24
|
+
'standard:connect',
|
|
25
|
+
'standard:disconnect',
|
|
26
|
+
'standard:events',
|
|
27
|
+
],
|
|
28
|
+
publicKey: publicKey.toBytes(),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
throw new Error(`Invalid Solana address: ${wallet.address}`);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
exports.convertBaseWalletToWalletAccount = convertBaseWalletToWalletAccount;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { WalletAccount } from '@wallet-standard/base';
|
|
2
|
+
import { BaseWallet } from '@dynamic-labs/types';
|
|
3
|
+
import { GlobalWalletClient } from '../../../createGlobalWalletClient';
|
|
4
|
+
export declare const convertBaseWalletToWalletAccount: (wallet: BaseWallet, client: GlobalWalletClient) => WalletAccount;
|