@dynamic-labs/ethereum 2.0.0-alpha.3 → 2.0.0-alpha.4

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.
@@ -1,201 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var _tslib = require('../../../_virtual/_tslib.cjs');
6
- var Client = require('@walletconnect/client');
7
- var viem = require('viem');
8
- var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
9
- var utils = require('@dynamic-labs/utils');
10
-
11
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
-
13
- var Client__default = /*#__PURE__*/_interopDefaultLegacy(Client);
14
-
15
- const initClient = (key, bridge, settings) => {
16
- const storageId = `walletconnect-${key}`;
17
- const session = localStorage.getItem(storageId);
18
- const clientArgs = session
19
- ? { session: JSON.parse(session), storageId }
20
- : { bridge, storageId };
21
- return new Client__default["default"](Object.assign(Object.assign({}, clientArgs), settings));
22
- };
23
- /**
24
- * Attach event handlers to WalletConnect events.
25
- */
26
- const setupWalletConnectEventListeners = (walletConnector, client) => {
27
- if (!client) {
28
- return;
29
- }
30
- let prevAccount;
31
- let prevChain;
32
- if (client.connected) {
33
- // eslint-disable-next-line prefer-destructuring
34
- prevAccount = client.accounts[0];
35
- prevChain = client.chainId;
36
- }
37
- client.on('disconnect', () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
38
- walletConnector.emit('disconnect');
39
- }));
40
- client.on('session_update', (_, payload) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
41
- // eslint-disable-next-line prefer-destructuring
42
- const { accounts, chainId } = payload.params[0];
43
- const didAccountChange = !walletConnectorCore.isSameAddress(prevAccount || '', accounts[0], 'eip155');
44
- const didChainChange = prevChain !== chainId;
45
- // eslint-disable-next-line prefer-destructuring
46
- prevAccount = accounts[0];
47
- prevChain = chainId;
48
- if (didAccountChange) {
49
- walletConnector.emit('accountChange', { accounts });
50
- }
51
- if (didChainChange) {
52
- walletConnector.emit('chainChange', { chain: String(chainId) });
53
- }
54
- }));
55
- };
56
- const teardownWalletConnectEventListeners = (client) => {
57
- client.off('disconnect');
58
- client.off('session_update');
59
- };
60
- /**
61
- * Initialize a client from a stored session and terminate the connection.
62
- */
63
- const killWalletConnectSession = (client) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
64
- try {
65
- if (client === null || client === void 0 ? void 0 : client.connected) {
66
- yield client.killSession();
67
- }
68
- }
69
- catch (e) {
70
- walletConnectorCore.logger.debug(e);
71
- }
72
- });
73
- const createSession = (client) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
74
- return new Promise((resolve, reject) => {
75
- client.on('connect', (error, payload) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
76
- if (error)
77
- throw error;
78
- resolve(payload);
79
- }));
80
- client.on('disconnect', (error, payload) => {
81
- reject(error || payload.params[0].message);
82
- });
83
- });
84
- });
85
- const fetchWalletConnectEVMPublicAddress = (metadata, wcClient, deepLinkPreference, opts) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
86
- var _a;
87
- if (wcClient.connected) {
88
- const [accountPublicAddress] = wcClient.accounts;
89
- return accountPublicAddress;
90
- }
91
- // createSession will trigger the QR code...
92
- yield wcClient.createSession();
93
- walletConnectorCore.performPlatformSpecificConnectionMethod(wcClient.uri, metadata, {
94
- onDesktopUri: opts === null || opts === void 0 ? void 0 : opts.onDesktopUri,
95
- onDisplayUri: opts === null || opts === void 0 ? void 0 : opts.onDisplayUri,
96
- }, deepLinkPreference);
97
- const payload = yield createSession(wcClient);
98
- (_a = opts === null || opts === void 0 ? void 0 : opts.onConnect) === null || _a === void 0 ? void 0 : _a.call(opts, payload);
99
- const [accountPublicAddress] = payload.params[0].accounts;
100
- return accountPublicAddress;
101
- });
102
- const signWalletConnectPersonalMessage = (messageToSign, metadata, client, deepLinkPreference, rpcProvider) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
103
- var _b, _c;
104
- const isCryptoWallet = ((_b = client === null || client === void 0 ? void 0 : client.peerMeta) === null || _b === void 0 ? void 0 : _b.name) === 'Crypto.com | DeFi Wallet' ||
105
- ((_c = client === null || client === void 0 ? void 0 : client.peerMeta) === null || _c === void 0 ? void 0 : _c.name) === 'DeFi Wallet';
106
- if (!client || !client.connected) {
107
- return;
108
- }
109
- const [accountPublicAddress] = client.accounts;
110
- if (utils.isMobile()) {
111
- const deepLink = walletConnectorCore.getDeepLink({
112
- metadata,
113
- mode: 'regular',
114
- preference: deepLinkPreference,
115
- uri: client.uri,
116
- });
117
- window.location.href = deepLink;
118
- }
119
- try {
120
- // This delay is required for Crypto.com DeFi wallet, when there is no delay the app
121
- // will not open the pop-up to sign. That seems to be a limitation in the standalone client
122
- // so the delay is required to allow the users to sign the message
123
- if (isCryptoWallet && utils.isMobile()) {
124
- yield sleep(4000);
125
- }
126
- else {
127
- // The delay of 1 second is necessary for some of the wallets to open the sign UI in the
128
- // correct order, without this some wallets like Trust or OKX will no open properly,
129
- // and not allowing the user to sign the message
130
- yield sleep(1000);
131
- }
132
- const signature = yield client.signPersonalMessage([
133
- messageToSign,
134
- accountPublicAddress,
135
- ]);
136
- yield waitForSafeTransactionOrTimeout(accountPublicAddress, signature, messageToSign, client, rpcProvider);
137
- return signature;
138
- }
139
- catch (e) {
140
- walletConnectorCore.logger.debug(e);
141
- throw e;
142
- }
143
- });
144
- const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
145
- // Successful value as defined by the EIP
146
- // https://eips.ethereum.org/EIPS/eip-1271#specification
147
- const MAGIC_VALUE = '0x1626ba7e';
148
- const IS_VALID_SIGNATURE_ABI = [
149
- 'function isValidSignature(bytes32 _message, bytes _signature) public view returns (bytes4)',
150
- ];
151
- const waitForSafeTransactionOrTimeout = (accountPublicAddress, signature, messageToSign, client, rpcProvider) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
152
- var _d;
153
- if (signature === '0x' &&
154
- // this is what wallet connect client returns there's no `safe` or `Safe`
155
- // exact string anywhere, so this seems like the best proxy
156
- ((_d = client.peerMeta) === null || _d === void 0 ? void 0 : _d.name) === 'WalletConnect Safe App') {
157
- if (!rpcProvider) {
158
- return;
159
- }
160
- const safeTransactionPromise = waitForSafeTransaction(accountPublicAddress, signature, messageToSign, yield rpcProvider());
161
- const timeoutPromise = new Promise((resolve) => {
162
- setTimeout(resolve, 120000);
163
- });
164
- yield Promise.race([safeTransactionPromise, timeoutPromise]);
165
- }
166
- });
167
- // this is a hack for safe
168
- // before sending the signature downstream, we need to make sure
169
- // the transaction is recorded first on the blockchain
170
- // redcoast verify WILL fail if it attempts to verify the signature
171
- // that has not yet been properly processed!
172
- const waitForSafeTransaction = (accountPublicAddress, signature, messageToSign, rpcProvider) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
173
- // wait for safe wallet to finish txn on the blockchain contract
174
- for (let i = 0; i < 120; i++) {
175
- try {
176
- // this will result in an exception if the transaction is still not ready
177
- // we need to catch it below
178
- const result = yield (rpcProvider === null || rpcProvider === void 0 ? void 0 : rpcProvider.readContract({
179
- abi: IS_VALID_SIGNATURE_ABI,
180
- address: accountPublicAddress,
181
- args: [viem.hashMessage(messageToSign), signature],
182
- functionName: 'isValidSignature',
183
- }));
184
- if (result === MAGIC_VALUE)
185
- return;
186
- }
187
- catch (err) {
188
- walletConnectorCore.logger.info('Safe transaction cannot be validated yet. Retrying.');
189
- }
190
- // try again after 2 seconds
191
- yield sleep(2000);
192
- }
193
- });
194
-
195
- exports.createSession = createSession;
196
- exports.fetchWalletConnectEVMPublicAddress = fetchWalletConnectEVMPublicAddress;
197
- exports.initClient = initClient;
198
- exports.killWalletConnectSession = killWalletConnectSession;
199
- exports.setupWalletConnectEventListeners = setupWalletConnectEventListeners;
200
- exports.signWalletConnectPersonalMessage = signWalletConnectPersonalMessage;
201
- exports.teardownWalletConnectEventListeners = teardownWalletConnectEventListeners;
@@ -1,17 +0,0 @@
1
- import Client from '@walletconnect/client';
2
- import { PublicClient } from 'viem';
3
- import { FetchPublicAddressOpts, PayloadParams, DeepLinkVariant, WalletConnector } from '@dynamic-labs/wallet-connector-core';
4
- import { WalletSchema } from '@dynamic-labs/wallet-book';
5
- export declare const initClient: (key: string, bridge: string, settings?: any) => Client;
6
- /**
7
- * Attach event handlers to WalletConnect events.
8
- */
9
- export declare const setupWalletConnectEventListeners: (walletConnector: WalletConnector, client: Client) => void;
10
- export declare const teardownWalletConnectEventListeners: (client: Client) => void;
11
- /**
12
- * Initialize a client from a stored session and terminate the connection.
13
- */
14
- export declare const killWalletConnectSession: (client: Client) => Promise<void>;
15
- export declare const createSession: (client: Client) => Promise<PayloadParams>;
16
- export declare const fetchWalletConnectEVMPublicAddress: (metadata: WalletSchema, wcClient: Client, deepLinkPreference: DeepLinkVariant, opts?: FetchPublicAddressOpts) => Promise<string | undefined>;
17
- export declare const signWalletConnectPersonalMessage: (messageToSign: string, metadata: WalletSchema, client: Client, deepLinkPreference: DeepLinkVariant, rpcProvider?: () => Promise<PublicClient | undefined>) => Promise<string | undefined>;
@@ -1,187 +0,0 @@
1
- import { __awaiter } from '../../../_virtual/_tslib.js';
2
- import Client from '@walletconnect/client';
3
- import { hashMessage } from 'viem';
4
- import { isSameAddress, logger, performPlatformSpecificConnectionMethod, getDeepLink } from '@dynamic-labs/wallet-connector-core';
5
- import { isMobile } from '@dynamic-labs/utils';
6
-
7
- const initClient = (key, bridge, settings) => {
8
- const storageId = `walletconnect-${key}`;
9
- const session = localStorage.getItem(storageId);
10
- const clientArgs = session
11
- ? { session: JSON.parse(session), storageId }
12
- : { bridge, storageId };
13
- return new Client(Object.assign(Object.assign({}, clientArgs), settings));
14
- };
15
- /**
16
- * Attach event handlers to WalletConnect events.
17
- */
18
- const setupWalletConnectEventListeners = (walletConnector, client) => {
19
- if (!client) {
20
- return;
21
- }
22
- let prevAccount;
23
- let prevChain;
24
- if (client.connected) {
25
- // eslint-disable-next-line prefer-destructuring
26
- prevAccount = client.accounts[0];
27
- prevChain = client.chainId;
28
- }
29
- client.on('disconnect', () => __awaiter(void 0, void 0, void 0, function* () {
30
- walletConnector.emit('disconnect');
31
- }));
32
- client.on('session_update', (_, payload) => __awaiter(void 0, void 0, void 0, function* () {
33
- // eslint-disable-next-line prefer-destructuring
34
- const { accounts, chainId } = payload.params[0];
35
- const didAccountChange = !isSameAddress(prevAccount || '', accounts[0], 'eip155');
36
- const didChainChange = prevChain !== chainId;
37
- // eslint-disable-next-line prefer-destructuring
38
- prevAccount = accounts[0];
39
- prevChain = chainId;
40
- if (didAccountChange) {
41
- walletConnector.emit('accountChange', { accounts });
42
- }
43
- if (didChainChange) {
44
- walletConnector.emit('chainChange', { chain: String(chainId) });
45
- }
46
- }));
47
- };
48
- const teardownWalletConnectEventListeners = (client) => {
49
- client.off('disconnect');
50
- client.off('session_update');
51
- };
52
- /**
53
- * Initialize a client from a stored session and terminate the connection.
54
- */
55
- const killWalletConnectSession = (client) => __awaiter(void 0, void 0, void 0, function* () {
56
- try {
57
- if (client === null || client === void 0 ? void 0 : client.connected) {
58
- yield client.killSession();
59
- }
60
- }
61
- catch (e) {
62
- logger.debug(e);
63
- }
64
- });
65
- const createSession = (client) => __awaiter(void 0, void 0, void 0, function* () {
66
- return new Promise((resolve, reject) => {
67
- client.on('connect', (error, payload) => __awaiter(void 0, void 0, void 0, function* () {
68
- if (error)
69
- throw error;
70
- resolve(payload);
71
- }));
72
- client.on('disconnect', (error, payload) => {
73
- reject(error || payload.params[0].message);
74
- });
75
- });
76
- });
77
- const fetchWalletConnectEVMPublicAddress = (metadata, wcClient, deepLinkPreference, opts) => __awaiter(void 0, void 0, void 0, function* () {
78
- var _a;
79
- if (wcClient.connected) {
80
- const [accountPublicAddress] = wcClient.accounts;
81
- return accountPublicAddress;
82
- }
83
- // createSession will trigger the QR code...
84
- yield wcClient.createSession();
85
- performPlatformSpecificConnectionMethod(wcClient.uri, metadata, {
86
- onDesktopUri: opts === null || opts === void 0 ? void 0 : opts.onDesktopUri,
87
- onDisplayUri: opts === null || opts === void 0 ? void 0 : opts.onDisplayUri,
88
- }, deepLinkPreference);
89
- const payload = yield createSession(wcClient);
90
- (_a = opts === null || opts === void 0 ? void 0 : opts.onConnect) === null || _a === void 0 ? void 0 : _a.call(opts, payload);
91
- const [accountPublicAddress] = payload.params[0].accounts;
92
- return accountPublicAddress;
93
- });
94
- const signWalletConnectPersonalMessage = (messageToSign, metadata, client, deepLinkPreference, rpcProvider) => __awaiter(void 0, void 0, void 0, function* () {
95
- var _b, _c;
96
- const isCryptoWallet = ((_b = client === null || client === void 0 ? void 0 : client.peerMeta) === null || _b === void 0 ? void 0 : _b.name) === 'Crypto.com | DeFi Wallet' ||
97
- ((_c = client === null || client === void 0 ? void 0 : client.peerMeta) === null || _c === void 0 ? void 0 : _c.name) === 'DeFi Wallet';
98
- if (!client || !client.connected) {
99
- return;
100
- }
101
- const [accountPublicAddress] = client.accounts;
102
- if (isMobile()) {
103
- const deepLink = getDeepLink({
104
- metadata,
105
- mode: 'regular',
106
- preference: deepLinkPreference,
107
- uri: client.uri,
108
- });
109
- window.location.href = deepLink;
110
- }
111
- try {
112
- // This delay is required for Crypto.com DeFi wallet, when there is no delay the app
113
- // will not open the pop-up to sign. That seems to be a limitation in the standalone client
114
- // so the delay is required to allow the users to sign the message
115
- if (isCryptoWallet && isMobile()) {
116
- yield sleep(4000);
117
- }
118
- else {
119
- // The delay of 1 second is necessary for some of the wallets to open the sign UI in the
120
- // correct order, without this some wallets like Trust or OKX will no open properly,
121
- // and not allowing the user to sign the message
122
- yield sleep(1000);
123
- }
124
- const signature = yield client.signPersonalMessage([
125
- messageToSign,
126
- accountPublicAddress,
127
- ]);
128
- yield waitForSafeTransactionOrTimeout(accountPublicAddress, signature, messageToSign, client, rpcProvider);
129
- return signature;
130
- }
131
- catch (e) {
132
- logger.debug(e);
133
- throw e;
134
- }
135
- });
136
- const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
137
- // Successful value as defined by the EIP
138
- // https://eips.ethereum.org/EIPS/eip-1271#specification
139
- const MAGIC_VALUE = '0x1626ba7e';
140
- const IS_VALID_SIGNATURE_ABI = [
141
- 'function isValidSignature(bytes32 _message, bytes _signature) public view returns (bytes4)',
142
- ];
143
- const waitForSafeTransactionOrTimeout = (accountPublicAddress, signature, messageToSign, client, rpcProvider) => __awaiter(void 0, void 0, void 0, function* () {
144
- var _d;
145
- if (signature === '0x' &&
146
- // this is what wallet connect client returns there's no `safe` or `Safe`
147
- // exact string anywhere, so this seems like the best proxy
148
- ((_d = client.peerMeta) === null || _d === void 0 ? void 0 : _d.name) === 'WalletConnect Safe App') {
149
- if (!rpcProvider) {
150
- return;
151
- }
152
- const safeTransactionPromise = waitForSafeTransaction(accountPublicAddress, signature, messageToSign, yield rpcProvider());
153
- const timeoutPromise = new Promise((resolve) => {
154
- setTimeout(resolve, 120000);
155
- });
156
- yield Promise.race([safeTransactionPromise, timeoutPromise]);
157
- }
158
- });
159
- // this is a hack for safe
160
- // before sending the signature downstream, we need to make sure
161
- // the transaction is recorded first on the blockchain
162
- // redcoast verify WILL fail if it attempts to verify the signature
163
- // that has not yet been properly processed!
164
- const waitForSafeTransaction = (accountPublicAddress, signature, messageToSign, rpcProvider) => __awaiter(void 0, void 0, void 0, function* () {
165
- // wait for safe wallet to finish txn on the blockchain contract
166
- for (let i = 0; i < 120; i++) {
167
- try {
168
- // this will result in an exception if the transaction is still not ready
169
- // we need to catch it below
170
- const result = yield (rpcProvider === null || rpcProvider === void 0 ? void 0 : rpcProvider.readContract({
171
- abi: IS_VALID_SIGNATURE_ABI,
172
- address: accountPublicAddress,
173
- args: [hashMessage(messageToSign), signature],
174
- functionName: 'isValidSignature',
175
- }));
176
- if (result === MAGIC_VALUE)
177
- return;
178
- }
179
- catch (err) {
180
- logger.info('Safe transaction cannot be validated yet. Retrying.');
181
- }
182
- // try again after 2 seconds
183
- yield sleep(2000);
184
- }
185
- });
186
-
187
- export { createSession, fetchWalletConnectEVMPublicAddress, initClient, killWalletConnectSession, setupWalletConnectEventListeners, signWalletConnectPersonalMessage, teardownWalletConnectEventListeners };
@@ -1 +0,0 @@
1
- export * from './client';
@@ -1,4 +0,0 @@
1
- import Client from '@walletconnect/client';
2
- export type InitializeWalletConnectClientOpts = {
3
- bridge?: Client['bridge'];
4
- };