@hashgraph/hedera-wallet-connect 2.0.4-canary.f71fa76.0 → 2.0.5-canary.09073be.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/README.md +141 -8
- package/package.json +4 -2
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -21
- package/dist/lib/dapp/DAppSigner.d.ts +0 -60
- package/dist/lib/dapp/DAppSigner.js +0 -301
- package/dist/lib/dapp/SessionNotFoundError.d.ts +0 -3
- package/dist/lib/dapp/SessionNotFoundError.js +0 -6
- package/dist/lib/dapp/index.d.ts +0 -216
- package/dist/lib/dapp/index.js +0 -580
- package/dist/lib/index.d.ts +0 -3
- package/dist/lib/index.js +0 -22
- package/dist/lib/shared/chainIds.d.ts +0 -11
- package/dist/lib/shared/chainIds.js +0 -31
- package/dist/lib/shared/errors.d.ts +0 -48
- package/dist/lib/shared/errors.js +0 -51
- package/dist/lib/shared/events.d.ts +0 -7
- package/dist/lib/shared/events.js +0 -27
- package/dist/lib/shared/extensionController.d.ts +0 -21
- package/dist/lib/shared/extensionController.js +0 -42
- package/dist/lib/shared/index.d.ts +0 -7
- package/dist/lib/shared/index.js +0 -26
- package/dist/lib/shared/logger.d.ts +0 -18
- package/dist/lib/shared/logger.js +0 -33
- package/dist/lib/shared/methods.d.ts +0 -12
- package/dist/lib/shared/methods.js +0 -32
- package/dist/lib/shared/payloads.d.ts +0 -101
- package/dist/lib/shared/payloads.js +0 -20
- package/dist/lib/shared/utils.d.ts +0 -275
- package/dist/lib/shared/utils.js +0 -420
- package/dist/lib/wallet/index.d.ts +0 -39
- package/dist/lib/wallet/index.js +0 -306
- package/dist/lib/wallet/provider.d.ts +0 -17
- package/dist/lib/wallet/provider.js +0 -58
- package/dist/lib/wallet/types.d.ts +0 -31
- package/dist/lib/wallet/types.js +0 -20
- package/dist/reown/adapter.d.ts +0 -59
- package/dist/reown/adapter.js +0 -261
- package/dist/reown/connectors/HederaConnector.d.ts +0 -31
- package/dist/reown/connectors/HederaConnector.js +0 -38
- package/dist/reown/connectors/index.d.ts +0 -1
- package/dist/reown/connectors/index.js +0 -1
- package/dist/reown/index.d.ts +0 -4
- package/dist/reown/index.js +0 -4
- package/dist/reown/providers/EIP155Provider.d.ts +0 -32
- package/dist/reown/providers/EIP155Provider.js +0 -187
- package/dist/reown/providers/HIP820Provider.d.ts +0 -26
- package/dist/reown/providers/HIP820Provider.js +0 -69
- package/dist/reown/providers/HederaProvider.d.ts +0 -164
- package/dist/reown/providers/HederaProvider.js +0 -471
- package/dist/reown/providers/index.d.ts +0 -3
- package/dist/reown/providers/index.js +0 -3
- package/dist/reown/utils/account.d.ts +0 -2
- package/dist/reown/utils/account.js +0 -20
- package/dist/reown/utils/chains.d.ts +0 -18
- package/dist/reown/utils/chains.js +0 -152
- package/dist/reown/utils/constants.d.ts +0 -16
- package/dist/reown/utils/constants.js +0 -18
- package/dist/reown/utils/helpers.d.ts +0 -12
- package/dist/reown/utils/helpers.js +0 -25
- package/dist/reown/utils/index.d.ts +0 -5
- package/dist/reown/utils/index.js +0 -5
- package/dist/reown/utils/types.d.ts +0 -9
- package/dist/reown/utils/types.js +0 -1
- package/dist/reown/wallets/EIP155Wallet.d.ts +0 -46
- package/dist/reown/wallets/EIP155Wallet.js +0 -124
- package/dist/reown/wallets/HIP820Wallet.d.ts +0 -53
- package/dist/reown/wallets/HIP820Wallet.js +0 -236
- package/dist/reown/wallets/index.d.ts +0 -2
- package/dist/reown/wallets/index.js +0 -2
package/dist/lib/dapp/index.js
DELETED
|
@@ -1,580 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
*
|
|
3
|
-
* Hedera Wallet Connect
|
|
4
|
-
*
|
|
5
|
-
* Copyright (C) 2023 Hedera Hashgraph, LLC
|
|
6
|
-
*
|
|
7
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
-
* you may not use this file except in compliance with the License.
|
|
9
|
-
* You may obtain a copy of the License at
|
|
10
|
-
*
|
|
11
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
-
*
|
|
13
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
-
* See the License for the specific language governing permissions and
|
|
17
|
-
* limitations under the License.
|
|
18
|
-
*
|
|
19
|
-
*/
|
|
20
|
-
import { LedgerId, Transaction } from '@hashgraph/sdk';
|
|
21
|
-
import { WalletConnectModal } from '@walletconnect/modal';
|
|
22
|
-
import SignClient from '@walletconnect/sign-client';
|
|
23
|
-
import { getSdkError } from '@walletconnect/utils';
|
|
24
|
-
import { DefaultLogger } from '../shared/logger';
|
|
25
|
-
import { HederaJsonRpcMethod, accountAndLedgerFromSession, networkNamespaces, extensionConnect, findExtensions, } from '../shared';
|
|
26
|
-
import { DAppSigner } from './DAppSigner';
|
|
27
|
-
export * from './DAppSigner';
|
|
28
|
-
export { SessionNotFoundError } from './SessionNotFoundError';
|
|
29
|
-
export class DAppConnector {
|
|
30
|
-
/**
|
|
31
|
-
* Initializes the DAppConnector instance.
|
|
32
|
-
* @param metadata - SignClientTypes.Metadata object for the DApp metadata.
|
|
33
|
-
* @param network - LedgerId representing the network (default: LedgerId.TESTNET).
|
|
34
|
-
* @param projectId - Project ID for the WalletConnect client.
|
|
35
|
-
* @param methods - Array of supported methods for the DApp (optional).
|
|
36
|
-
* @param events - Array of supported events for the DApp (optional).
|
|
37
|
-
* @param chains - Array of supported chains for the DApp (optional).
|
|
38
|
-
* @param logLevel - Logging level for the DAppConnector (optional).
|
|
39
|
-
*/
|
|
40
|
-
constructor(metadata, network, projectId, methods, events, chains, logLevel = 'debug') {
|
|
41
|
-
this.network = LedgerId.TESTNET;
|
|
42
|
-
this.supportedMethods = [];
|
|
43
|
-
this.supportedEvents = [];
|
|
44
|
-
this.supportedChains = [];
|
|
45
|
-
this.extensions = [];
|
|
46
|
-
this.onSessionIframeCreated = null;
|
|
47
|
-
this.signers = [];
|
|
48
|
-
this.isInitializing = false;
|
|
49
|
-
this.abortableConnect = async (callback) => {
|
|
50
|
-
return new Promise(async (resolve, reject) => {
|
|
51
|
-
const pairTimeoutMs = 480000;
|
|
52
|
-
const timeout = setTimeout(() => {
|
|
53
|
-
this.walletConnectModal.closeModal();
|
|
54
|
-
reject(new Error(`Connect timed out after ${pairTimeoutMs}(ms)`));
|
|
55
|
-
}, pairTimeoutMs);
|
|
56
|
-
try {
|
|
57
|
-
return resolve(await callback());
|
|
58
|
-
}
|
|
59
|
-
catch (error) {
|
|
60
|
-
reject(error);
|
|
61
|
-
}
|
|
62
|
-
finally {
|
|
63
|
-
clearTimeout(timeout);
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
};
|
|
67
|
-
this.logger = new DefaultLogger(logLevel);
|
|
68
|
-
this.dAppMetadata = metadata;
|
|
69
|
-
this.network = network;
|
|
70
|
-
this.projectId = projectId;
|
|
71
|
-
this.supportedMethods = methods !== null && methods !== void 0 ? methods : Object.values(HederaJsonRpcMethod);
|
|
72
|
-
this.supportedEvents = events !== null && events !== void 0 ? events : [];
|
|
73
|
-
this.supportedChains = chains !== null && chains !== void 0 ? chains : [];
|
|
74
|
-
this.extensions = [];
|
|
75
|
-
this.walletConnectModal = new WalletConnectModal({
|
|
76
|
-
projectId: projectId,
|
|
77
|
-
chains: chains,
|
|
78
|
-
});
|
|
79
|
-
findExtensions((metadata, isIframe) => {
|
|
80
|
-
this.extensions.push(Object.assign(Object.assign({}, metadata), { available: true, availableInIframe: isIframe }));
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Sets the logging level for the DAppConnector
|
|
85
|
-
* @param level - The logging level to set
|
|
86
|
-
*/
|
|
87
|
-
setLogLevel(level) {
|
|
88
|
-
if (this.logger instanceof DefaultLogger) {
|
|
89
|
-
this.logger.setLogLevel(level);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Initializes the DAppConnector instance.
|
|
94
|
-
* @param logger - `BaseLogger` for logging purposes (optional).
|
|
95
|
-
*/
|
|
96
|
-
async init({ logger } = {}) {
|
|
97
|
-
try {
|
|
98
|
-
this.isInitializing = true;
|
|
99
|
-
if (!this.projectId) {
|
|
100
|
-
throw new Error('Project ID is not defined');
|
|
101
|
-
}
|
|
102
|
-
this.walletConnectClient = await SignClient.init({
|
|
103
|
-
logger,
|
|
104
|
-
relayUrl: 'wss://relay.walletconnect.com',
|
|
105
|
-
projectId: this.projectId,
|
|
106
|
-
metadata: this.dAppMetadata,
|
|
107
|
-
});
|
|
108
|
-
const existingSessions = this.walletConnectClient.session.getAll();
|
|
109
|
-
if (existingSessions.length > 0)
|
|
110
|
-
this.signers = existingSessions.flatMap((session) => this.createSigners(session));
|
|
111
|
-
else
|
|
112
|
-
this.checkIframeConnect();
|
|
113
|
-
this.walletConnectClient.on('session_event', this.handleSessionEvent.bind(this));
|
|
114
|
-
this.walletConnectClient.on('session_update', this.handleSessionUpdate.bind(this));
|
|
115
|
-
this.walletConnectClient.on('session_delete', this.handleSessionDelete.bind(this));
|
|
116
|
-
// Listen for custom session_delete events from DAppSigner
|
|
117
|
-
this.walletConnectClient.core.events.on('session_delete', this.handleSessionDelete.bind(this));
|
|
118
|
-
this.walletConnectClient.core.pairing.events.on('pairing_delete', this.handlePairingDelete.bind(this));
|
|
119
|
-
}
|
|
120
|
-
catch (e) {
|
|
121
|
-
this.logger.error('Error initializing DAppConnector:', e);
|
|
122
|
-
}
|
|
123
|
-
finally {
|
|
124
|
-
this.isInitializing = false;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Retrieves a DAppSigner for the specified Hedera Account ID.
|
|
129
|
-
*
|
|
130
|
-
* @param {AccountId} accountId - The Hedera Account ID to find the associated signer.
|
|
131
|
-
* @returns {DAppSigner} - The signer object of type {@link DAppSigner} corresponding to the provided account ID.
|
|
132
|
-
* @throws {Error} - If no signer is found for the provided account ID.
|
|
133
|
-
*/
|
|
134
|
-
getSigner(accountId) {
|
|
135
|
-
if (this.isInitializing) {
|
|
136
|
-
throw new Error('DAppConnector is not initialized yet. Try again later.');
|
|
137
|
-
}
|
|
138
|
-
const signer = this.signers.find((signer) => signer.getAccountId().equals(accountId));
|
|
139
|
-
if (!signer)
|
|
140
|
-
throw new Error('Signer is not found for this accountId');
|
|
141
|
-
return signer;
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Initiates the WalletConnect connection flow using a QR code.
|
|
145
|
-
* @param pairingTopic - The pairing topic for the connection (optional).
|
|
146
|
-
* @param throwErrorOnReject - Whether to show an error when the user rejects the pairing (default: false).
|
|
147
|
-
* @returns {Promise<SessionTypes.Struct>} - A Promise that resolves when the connection process is complete.
|
|
148
|
-
*/
|
|
149
|
-
async openModal(pairingTopic, throwErrorOnReject = false) {
|
|
150
|
-
try {
|
|
151
|
-
const { uri, approval } = await this.connectURI(pairingTopic);
|
|
152
|
-
this.walletConnectModal.openModal({ uri });
|
|
153
|
-
const session = await new Promise(async (resolve, reject) => {
|
|
154
|
-
if (throwErrorOnReject) {
|
|
155
|
-
this.walletConnectModal.subscribeModal((state) => {
|
|
156
|
-
// the modal was closed so reject the promise
|
|
157
|
-
if (!state.open) {
|
|
158
|
-
reject(new Error('User rejected pairing'));
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
try {
|
|
163
|
-
const approvedSession = await approval();
|
|
164
|
-
await this.onSessionConnected(approvedSession);
|
|
165
|
-
resolve(approvedSession);
|
|
166
|
-
}
|
|
167
|
-
catch (error) {
|
|
168
|
-
reject(error);
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
return session;
|
|
172
|
-
}
|
|
173
|
-
finally {
|
|
174
|
-
this.walletConnectModal.closeModal();
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Initiates the WallecConnect connection flow using URI.
|
|
179
|
-
* @param pairingTopic - The pairing topic for the connection (optional).
|
|
180
|
-
* @param extensionId - The id for the extension used to connect (optional).
|
|
181
|
-
* @returns A Promise that resolves when the connection process is complete.
|
|
182
|
-
*/
|
|
183
|
-
async connect(launchCallback, pairingTopic, extensionId) {
|
|
184
|
-
return this.abortableConnect(async () => {
|
|
185
|
-
var _a;
|
|
186
|
-
const { uri, approval } = await this.connectURI(pairingTopic);
|
|
187
|
-
if (!uri)
|
|
188
|
-
throw new Error('URI is not defined');
|
|
189
|
-
launchCallback(uri);
|
|
190
|
-
const session = await approval();
|
|
191
|
-
if (extensionId) {
|
|
192
|
-
const sessionProperties = Object.assign(Object.assign({}, session.sessionProperties), { extensionId });
|
|
193
|
-
session.sessionProperties = sessionProperties;
|
|
194
|
-
await ((_a = this.walletConnectClient) === null || _a === void 0 ? void 0 : _a.session.update(session.topic, {
|
|
195
|
-
sessionProperties,
|
|
196
|
-
}));
|
|
197
|
-
}
|
|
198
|
-
await this.onSessionConnected(session);
|
|
199
|
-
return session;
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* Initiates the WallecConnect connection flow sending a message to the extension.
|
|
204
|
-
* @param extensionId - The id for the extension used to connect.
|
|
205
|
-
* @param pairingTopic - The pairing topic for the connection (optional).
|
|
206
|
-
* @returns A Promise that resolves when the connection process is complete.
|
|
207
|
-
*/
|
|
208
|
-
async connectExtension(extensionId, pairingTopic) {
|
|
209
|
-
const extension = this.extensions.find((ext) => ext.id === extensionId);
|
|
210
|
-
if (!extension || !extension.available)
|
|
211
|
-
throw new Error('Extension is not available');
|
|
212
|
-
return this.connect((uri) => {
|
|
213
|
-
extensionConnect(extension.id, extension.availableInIframe, uri);
|
|
214
|
-
}, pairingTopic, extension.availableInIframe ? undefined : extensionId);
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Validates the session by checking if the session exists and is valid.
|
|
218
|
-
* Also ensures the signer exists for the session.
|
|
219
|
-
* @param topic - The topic of the session to validate.
|
|
220
|
-
* @returns {boolean} - True if the session exists and has a valid signer, false otherwise.
|
|
221
|
-
*/
|
|
222
|
-
validateSession(topic) {
|
|
223
|
-
try {
|
|
224
|
-
if (!this.walletConnectClient) {
|
|
225
|
-
return false;
|
|
226
|
-
}
|
|
227
|
-
const session = this.walletConnectClient.session.get(topic);
|
|
228
|
-
const hasSigner = this.signers.some((signer) => signer.topic === topic);
|
|
229
|
-
if (!session) {
|
|
230
|
-
// If session doesn't exist but we have a signer for it, clean up
|
|
231
|
-
if (hasSigner) {
|
|
232
|
-
this.logger.warn(`Signer exists but no session found for topic: ${topic}`);
|
|
233
|
-
this.handleSessionDelete({ topic });
|
|
234
|
-
}
|
|
235
|
-
return false;
|
|
236
|
-
}
|
|
237
|
-
if (!hasSigner) {
|
|
238
|
-
this.logger.warn(`Session exists but no signer found for topic: ${topic}`);
|
|
239
|
-
return false;
|
|
240
|
-
}
|
|
241
|
-
return true;
|
|
242
|
-
}
|
|
243
|
-
catch (e) {
|
|
244
|
-
this.logger.error('Error validating session:', e);
|
|
245
|
-
return false;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Validates the session and refreshes the signers by removing the invalid ones.
|
|
250
|
-
*/
|
|
251
|
-
validateAndRefreshSigners() {
|
|
252
|
-
this.signers = this.signers.filter((signer) => this.validateSession(signer.topic));
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Initiates the WallecConnect connection if the wallet in iframe mode is detected.
|
|
256
|
-
*/
|
|
257
|
-
async checkIframeConnect() {
|
|
258
|
-
const extension = this.extensions.find((ext) => ext.availableInIframe);
|
|
259
|
-
if (extension) {
|
|
260
|
-
const session = await this.connectExtension(extension.id);
|
|
261
|
-
if (this.onSessionIframeCreated)
|
|
262
|
-
this.onSessionIframeCreated(session);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* Disconnects the current session associated with the specified topic.
|
|
267
|
-
* @param topic - The topic of the session to disconnect.
|
|
268
|
-
* @returns A Promise that resolves when the session is disconnected.
|
|
269
|
-
*/
|
|
270
|
-
async disconnect(topic) {
|
|
271
|
-
try {
|
|
272
|
-
if (!this.walletConnectClient) {
|
|
273
|
-
throw new Error('WalletConnect is not initialized');
|
|
274
|
-
}
|
|
275
|
-
await this.walletConnectClient.disconnect({
|
|
276
|
-
topic: topic,
|
|
277
|
-
reason: getSdkError('USER_DISCONNECTED'),
|
|
278
|
-
});
|
|
279
|
-
return true;
|
|
280
|
-
}
|
|
281
|
-
catch (e) {
|
|
282
|
-
this.logger.error('Either the session was already disconnected or the topic is invalid', e);
|
|
283
|
-
return false;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
/**
|
|
287
|
-
* Disconnects all active sessions and pairings.
|
|
288
|
-
*
|
|
289
|
-
* Throws error when WalletConnect is not initialized or there are no active sessions/pairings.
|
|
290
|
-
* @returns A Promise that resolves when all active sessions and pairings are disconnected.
|
|
291
|
-
*/
|
|
292
|
-
async disconnectAll() {
|
|
293
|
-
if (!this.walletConnectClient) {
|
|
294
|
-
throw new Error('WalletConnect is not initialized');
|
|
295
|
-
}
|
|
296
|
-
const sessions = this.walletConnectClient.session.getAll();
|
|
297
|
-
const pairings = this.walletConnectClient.core.pairing.getPairings();
|
|
298
|
-
if (!(sessions === null || sessions === void 0 ? void 0 : sessions.length) && !(pairings === null || pairings === void 0 ? void 0 : pairings.length)) {
|
|
299
|
-
throw new Error('There is no active session/pairing. Connect to the wallet at first.');
|
|
300
|
-
}
|
|
301
|
-
const disconnectionPromises = [];
|
|
302
|
-
// disconnect sessions
|
|
303
|
-
for (const session of this.walletConnectClient.session.getAll()) {
|
|
304
|
-
this.logger.info(`Disconnecting from session: ${session}`);
|
|
305
|
-
const promise = this.disconnect(session.topic);
|
|
306
|
-
disconnectionPromises.push(promise);
|
|
307
|
-
}
|
|
308
|
-
// disconnect pairings
|
|
309
|
-
//https://docs.walletconnect.com/api/core/pairing
|
|
310
|
-
for (const pairing of pairings) {
|
|
311
|
-
const promise = this.disconnect(pairing.topic);
|
|
312
|
-
disconnectionPromises.push(promise);
|
|
313
|
-
}
|
|
314
|
-
await Promise.all(disconnectionPromises);
|
|
315
|
-
this.signers = [];
|
|
316
|
-
}
|
|
317
|
-
createSigners(session) {
|
|
318
|
-
const allNamespaceAccounts = accountAndLedgerFromSession(session);
|
|
319
|
-
return allNamespaceAccounts.map(({ account, network }) => {
|
|
320
|
-
var _a;
|
|
321
|
-
return new DAppSigner(account, this.walletConnectClient, session.topic, network, (_a = session.sessionProperties) === null || _a === void 0 ? void 0 : _a.extensionId, this.logger instanceof DefaultLogger ? this.logger.getLogLevel() : 'debug');
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
async onSessionConnected(session) {
|
|
325
|
-
const newSigners = this.createSigners(session);
|
|
326
|
-
// Filter out any existing signers with duplicate AccountIds
|
|
327
|
-
for (const newSigner of newSigners) {
|
|
328
|
-
// We check if any signers have the same account, extension + metadata name.
|
|
329
|
-
const existingSigners = this.signers.filter((currentSigner) => {
|
|
330
|
-
var _a, _b;
|
|
331
|
-
const matchingAccountId = ((_a = currentSigner === null || currentSigner === void 0 ? void 0 : currentSigner.getAccountId()) === null || _a === void 0 ? void 0 : _a.toString()) === ((_b = newSigner === null || newSigner === void 0 ? void 0 : newSigner.getAccountId()) === null || _b === void 0 ? void 0 : _b.toString());
|
|
332
|
-
const matchingExtensionId = newSigner.extensionId === currentSigner.extensionId;
|
|
333
|
-
const newSignerMetadata = newSigner.getMetadata();
|
|
334
|
-
const existingSignerMetadata = currentSigner.getMetadata();
|
|
335
|
-
const metadataNameMatch = (newSignerMetadata === null || newSignerMetadata === void 0 ? void 0 : newSignerMetadata.name) === (existingSignerMetadata === null || existingSignerMetadata === void 0 ? void 0 : existingSignerMetadata.name);
|
|
336
|
-
if (currentSigner.topic === newSigner.topic) {
|
|
337
|
-
this.logger.error('The topic was already connected. This is a weird error. Please report it.', newSigner.getAccountId().toString());
|
|
338
|
-
}
|
|
339
|
-
return matchingAccountId && matchingExtensionId && metadataNameMatch;
|
|
340
|
-
});
|
|
341
|
-
// Any dupes get disconnected + removed from the signers array.
|
|
342
|
-
for (const existingSigner of existingSigners) {
|
|
343
|
-
this.logger.debug(`Disconnecting duplicate signer for account ${existingSigner.getAccountId().toString()}`);
|
|
344
|
-
await this.disconnect(existingSigner.topic);
|
|
345
|
-
this.signers = this.signers.filter((s) => s.topic !== existingSigner.topic);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
// Add new signers after all duplicates have been cleaned up
|
|
349
|
-
this.signers.push(...newSigners);
|
|
350
|
-
this.logger.debug(`Current signers after connection: ${this.signers
|
|
351
|
-
.map((s) => `${s.getAccountId().toString()}:${s.topic}`)
|
|
352
|
-
.join(', ')}`);
|
|
353
|
-
}
|
|
354
|
-
async connectURI(pairingTopic) {
|
|
355
|
-
if (!this.walletConnectClient) {
|
|
356
|
-
throw new Error('WalletConnect is not initialized');
|
|
357
|
-
}
|
|
358
|
-
return this.walletConnectClient.connect({
|
|
359
|
-
pairingTopic,
|
|
360
|
-
requiredNamespaces: networkNamespaces(this.network, this.supportedMethods, this.supportedEvents),
|
|
361
|
-
});
|
|
362
|
-
}
|
|
363
|
-
async request({ method, params, }) {
|
|
364
|
-
var _a, _b, _c;
|
|
365
|
-
let signer;
|
|
366
|
-
this.logger.debug(`Requesting method: ${method} with params: ${JSON.stringify(params)}`);
|
|
367
|
-
if (params === null || params === void 0 ? void 0 : params.signerAccountId) {
|
|
368
|
-
// Extract the actual account ID from the hedera:<network>:<address> format
|
|
369
|
-
const actualAccountId = (_b = (_a = params === null || params === void 0 ? void 0 : params.signerAccountId) === null || _a === void 0 ? void 0 : _a.split(':')) === null || _b === void 0 ? void 0 : _b.pop();
|
|
370
|
-
signer = this.signers.find((s) => { var _a; return ((_a = s === null || s === void 0 ? void 0 : s.getAccountId()) === null || _a === void 0 ? void 0 : _a.toString()) === actualAccountId; });
|
|
371
|
-
this.logger.debug(`Found signer: ${(_c = signer === null || signer === void 0 ? void 0 : signer.getAccountId()) === null || _c === void 0 ? void 0 : _c.toString()}`);
|
|
372
|
-
if (!signer) {
|
|
373
|
-
throw new Error(`Signer not found for account ID: ${params === null || params === void 0 ? void 0 : params.signerAccountId}. Did you use the correct format? e.g hedera:<network>:<address> `);
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
else {
|
|
377
|
-
signer = this.signers[this.signers.length - 1];
|
|
378
|
-
}
|
|
379
|
-
if (!signer) {
|
|
380
|
-
throw new Error('There is no active session. Connect to the wallet at first.');
|
|
381
|
-
}
|
|
382
|
-
this.logger.debug(`Using signer: ${signer.getAccountId().toString()}: ${signer.topic} - about to request.`);
|
|
383
|
-
return await signer.request({
|
|
384
|
-
method: method,
|
|
385
|
-
params: params,
|
|
386
|
-
});
|
|
387
|
-
}
|
|
388
|
-
/**
|
|
389
|
-
* Retrieves the node addresses associated with the current Hedera network.
|
|
390
|
-
*
|
|
391
|
-
* When there is no active session or an error occurs during the request.
|
|
392
|
-
* @returns Promise\<{@link GetNodeAddressesResult}\>
|
|
393
|
-
*/
|
|
394
|
-
async getNodeAddresses() {
|
|
395
|
-
return await this.request({
|
|
396
|
-
method: HederaJsonRpcMethod.GetNodeAddresses,
|
|
397
|
-
params: undefined,
|
|
398
|
-
});
|
|
399
|
-
}
|
|
400
|
-
/**
|
|
401
|
-
* Executes a transaction on the Hedera network.
|
|
402
|
-
*
|
|
403
|
-
* @param {ExecuteTransactionParams} params - The parameters of type {@link ExecuteTransactionParams | `ExecuteTransactionParams`} required for the transaction execution.
|
|
404
|
-
* @param {string[]} params.signedTransaction - Array of Base64-encoded `Transaction`'s
|
|
405
|
-
* @returns Promise\<{@link ExecuteTransactionResult}\>
|
|
406
|
-
* @example
|
|
407
|
-
* Use helper `transactionToBase64String` to encode `Transaction` to Base64 string
|
|
408
|
-
* ```ts
|
|
409
|
-
* const params = {
|
|
410
|
-
* signedTransaction: [transactionToBase64String(transaction)]
|
|
411
|
-
* }
|
|
412
|
-
*
|
|
413
|
-
* const result = await dAppConnector.executeTransaction(params)
|
|
414
|
-
* ```
|
|
415
|
-
*/
|
|
416
|
-
async executeTransaction(params) {
|
|
417
|
-
return await this.request({
|
|
418
|
-
method: HederaJsonRpcMethod.ExecuteTransaction,
|
|
419
|
-
params,
|
|
420
|
-
});
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* Signs a provided `message` with provided `signerAccountId`.
|
|
424
|
-
*
|
|
425
|
-
* @param {SignMessageParams} params - The parameters of type {@link SignMessageParams | `SignMessageParams`} required for signing message.
|
|
426
|
-
* @param {string} params.signerAccountId - a signer Hedera Account identifier in {@link https://hips.hedera.com/hip/hip-30 | HIP-30} (`<nework>:<shard>.<realm>.<num>`) form.
|
|
427
|
-
* @param {string} params.message - a plain UTF-8 string
|
|
428
|
-
* @returns Promise\<{@link SignMessageResult}\>
|
|
429
|
-
* @example
|
|
430
|
-
* ```ts
|
|
431
|
-
* const params = {
|
|
432
|
-
* signerAccountId: 'hedera:testnet:0.0.12345',
|
|
433
|
-
* message: 'Hello World!'
|
|
434
|
-
* }
|
|
435
|
-
*
|
|
436
|
-
* const result = await dAppConnector.signMessage(params)
|
|
437
|
-
* ```
|
|
438
|
-
*/
|
|
439
|
-
async signMessage(params) {
|
|
440
|
-
return await this.request({
|
|
441
|
-
method: HederaJsonRpcMethod.SignMessage,
|
|
442
|
-
params,
|
|
443
|
-
});
|
|
444
|
-
}
|
|
445
|
-
/**
|
|
446
|
-
* Signs and send `Query` on the Hedera network.
|
|
447
|
-
*
|
|
448
|
-
* @param {SignAndExecuteQueryParams} params - The parameters of type {@link SignAndExecuteQueryParams | `SignAndExecuteQueryParams`} required for the Query execution.
|
|
449
|
-
* @param {string} params.signerAccountId - a signer Hedera Account identifier in {@link https://hips.hedera.com/hip/hip-30 | HIP-30} (`<nework>:<shard>.<realm>.<num>`) form.
|
|
450
|
-
* @param {string} params.query - `Query` object represented as Base64 string
|
|
451
|
-
* @returns Promise\<{@link SignAndExecuteQueryResult}\>
|
|
452
|
-
* @example
|
|
453
|
-
* Use helper `queryToBase64String` to encode `Query` to Base64 string
|
|
454
|
-
* ```ts
|
|
455
|
-
* const params = {
|
|
456
|
-
* signerAccountId: '0.0.12345',
|
|
457
|
-
* query: queryToBase64String(query),
|
|
458
|
-
* }
|
|
459
|
-
*
|
|
460
|
-
* const result = await dAppConnector.signAndExecuteQuery(params)
|
|
461
|
-
* ```
|
|
462
|
-
*/
|
|
463
|
-
async signAndExecuteQuery(params) {
|
|
464
|
-
return await this.request({
|
|
465
|
-
method: HederaJsonRpcMethod.SignAndExecuteQuery,
|
|
466
|
-
params,
|
|
467
|
-
});
|
|
468
|
-
}
|
|
469
|
-
/**
|
|
470
|
-
* Signs and executes Transactions on the Hedera network.
|
|
471
|
-
*
|
|
472
|
-
* @param {SignAndExecuteTransactionParams} params - The parameters of type {@link SignAndExecuteTransactionParams | `SignAndExecuteTransactionParams`} required for `Transaction` signing and execution.
|
|
473
|
-
* @param {string} params.signerAccountId - a signer Hedera Account identifier in {@link https://hips.hedera.com/hip/hip-30 | HIP-30} (`<nework>:<shard>.<realm>.<num>`) form.
|
|
474
|
-
* @param {string[]} params.transaction - Array of Base64-encoded `Transaction`'s
|
|
475
|
-
* @returns Promise\<{@link SignAndExecuteTransactionResult}\>
|
|
476
|
-
* @example
|
|
477
|
-
* Use helper `transactionToBase64String` to encode `Transaction` to Base64 string
|
|
478
|
-
* ```ts
|
|
479
|
-
* const params = {
|
|
480
|
-
* signerAccountId: '0.0.12345'
|
|
481
|
-
* transaction: [transactionToBase64String(transaction)]
|
|
482
|
-
* }
|
|
483
|
-
*
|
|
484
|
-
* const result = await dAppConnector.signAndExecuteTransaction(params)
|
|
485
|
-
* ```
|
|
486
|
-
*/
|
|
487
|
-
async signAndExecuteTransaction(params) {
|
|
488
|
-
return await this.request({
|
|
489
|
-
method: HederaJsonRpcMethod.SignAndExecuteTransaction,
|
|
490
|
-
params,
|
|
491
|
-
});
|
|
492
|
-
}
|
|
493
|
-
/**
|
|
494
|
-
* Signs and executes Transactions on the Hedera network.
|
|
495
|
-
*
|
|
496
|
-
* @param {SignTransactionParams} params - The parameters of type {@link SignTransactionParams | `SignTransactionParams`} required for `Transaction` signing.
|
|
497
|
-
* @param {string} params.signerAccountId - a signer Hedera Account identifier in {@link https://hips.hedera.com/hip/hip-30 | HIP-30} (`<nework>:<shard>.<realm>.<num>`) form.
|
|
498
|
-
* @param {Transaction | string} params.transactionBody - a built Transaction object, or a base64 string of a transaction body( HIP-820).
|
|
499
|
-
* HIP-820 calls for a base64 encoded proto.TransactionBody and many wallets support a serialized Transaction object generated by the Hedera Javascript SDK.
|
|
500
|
-
* Both options are supported here for backwards compatibility.
|
|
501
|
-
* @returns Promise\<{@link SignTransactionResult}\>
|
|
502
|
-
* @example
|
|
503
|
-
* ```ts
|
|
504
|
-
*
|
|
505
|
-
* const params = {
|
|
506
|
-
* signerAccountId: '0.0.12345',
|
|
507
|
-
* transactionBody
|
|
508
|
-
* }
|
|
509
|
-
*
|
|
510
|
-
* const result = await dAppConnector.signTransaction(params)
|
|
511
|
-
* ```
|
|
512
|
-
*/
|
|
513
|
-
async signTransaction(params) {
|
|
514
|
-
var _a, _b;
|
|
515
|
-
if (typeof (params === null || params === void 0 ? void 0 : params.transactionBody) === 'string') {
|
|
516
|
-
this.logger.warn('Transaction body is a string. This is not recommended, please migrate to passing a transaction object directly.');
|
|
517
|
-
return await this.request({
|
|
518
|
-
method: HederaJsonRpcMethod.SignTransaction,
|
|
519
|
-
params,
|
|
520
|
-
});
|
|
521
|
-
}
|
|
522
|
-
if ((params === null || params === void 0 ? void 0 : params.transactionBody) instanceof Transaction) {
|
|
523
|
-
const signerAccountId = (_b = (_a = params === null || params === void 0 ? void 0 : params.signerAccountId) === null || _a === void 0 ? void 0 : _a.split(':')) === null || _b === void 0 ? void 0 : _b.pop();
|
|
524
|
-
const accountSigner = this.signers.find((signer) => { var _a; return ((_a = signer === null || signer === void 0 ? void 0 : signer.getAccountId()) === null || _a === void 0 ? void 0 : _a.toString()) === signerAccountId; });
|
|
525
|
-
if (!accountSigner) {
|
|
526
|
-
throw new Error(`No signer found for account ${signerAccountId}`);
|
|
527
|
-
}
|
|
528
|
-
if (!(params === null || params === void 0 ? void 0 : params.transactionBody)) {
|
|
529
|
-
throw new Error('No transaction provided');
|
|
530
|
-
}
|
|
531
|
-
return await accountSigner.signTransaction(params.transactionBody);
|
|
532
|
-
}
|
|
533
|
-
throw new Error('Transaction sent in incorrect format. Ensure transaction body is either a base64 transaction body or Transaction object.');
|
|
534
|
-
}
|
|
535
|
-
handleSessionEvent(args) {
|
|
536
|
-
this.logger.debug('Session event received:', args);
|
|
537
|
-
this.validateAndRefreshSigners();
|
|
538
|
-
}
|
|
539
|
-
handleSessionUpdate({ topic, params, }) {
|
|
540
|
-
const { namespaces } = params;
|
|
541
|
-
const _session = this.walletConnectClient.session.get(topic);
|
|
542
|
-
const updatedSession = Object.assign(Object.assign({}, _session), { namespaces });
|
|
543
|
-
this.logger.info('Session updated:', updatedSession);
|
|
544
|
-
this.signers = this.signers.filter((signer) => signer.topic !== topic);
|
|
545
|
-
this.signers.push(...this.createSigners(updatedSession));
|
|
546
|
-
}
|
|
547
|
-
handleSessionDelete(event) {
|
|
548
|
-
this.logger.info('Session deleted:', event);
|
|
549
|
-
let deletedSigner = false;
|
|
550
|
-
this.signers = this.signers.filter((signer) => {
|
|
551
|
-
if (signer.topic !== event.topic) {
|
|
552
|
-
return true;
|
|
553
|
-
}
|
|
554
|
-
deletedSigner = true;
|
|
555
|
-
return false;
|
|
556
|
-
});
|
|
557
|
-
// prevent emitting disconnected event if signers is untouched.
|
|
558
|
-
if (deletedSigner) {
|
|
559
|
-
try {
|
|
560
|
-
this.disconnect(event.topic);
|
|
561
|
-
}
|
|
562
|
-
catch (e) {
|
|
563
|
-
this.logger.error('Error disconnecting session:', e);
|
|
564
|
-
}
|
|
565
|
-
this.logger.info('Session deleted and signer removed');
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
handlePairingDelete(event) {
|
|
569
|
-
this.logger.info('Pairing deleted:', event);
|
|
570
|
-
this.signers = this.signers.filter((signer) => signer.topic !== event.topic);
|
|
571
|
-
try {
|
|
572
|
-
this.disconnect(event.topic);
|
|
573
|
-
}
|
|
574
|
-
catch (e) {
|
|
575
|
-
this.logger.error('Error disconnecting pairing:', e);
|
|
576
|
-
}
|
|
577
|
-
this.logger.info('Pairing deleted by wallet');
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
export default DAppConnector;
|
package/dist/lib/index.d.ts
DELETED
package/dist/lib/index.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
*
|
|
3
|
-
* Hedera Wallet Connect
|
|
4
|
-
*
|
|
5
|
-
* Copyright (C) 2023 Hedera Hashgraph, LLC
|
|
6
|
-
*
|
|
7
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
-
* you may not use this file except in compliance with the License.
|
|
9
|
-
* You may obtain a copy of the License at
|
|
10
|
-
*
|
|
11
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
-
*
|
|
13
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
-
* See the License for the specific language governing permissions and
|
|
17
|
-
* limitations under the License.
|
|
18
|
-
*
|
|
19
|
-
*/
|
|
20
|
-
export * from './shared';
|
|
21
|
-
export { default as Wallet } from './wallet';
|
|
22
|
-
export * from './dapp';
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Enum representing different Hedera network chain IDs.
|
|
3
|
-
*
|
|
4
|
-
* @see {@link https://github.com/hashgraph/hedera-improvement-proposal/blob/main/HIP/hip-820.md#specification | HIP-820 Specification}
|
|
5
|
-
*/
|
|
6
|
-
export declare enum HederaChainId {
|
|
7
|
-
Mainnet = "hedera:mainnet",
|
|
8
|
-
Testnet = "hedera:testnet",
|
|
9
|
-
Previewnet = "hedera:previewnet",
|
|
10
|
-
Devnet = "hedera:devnet"
|
|
11
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
*
|
|
3
|
-
* Hedera Wallet Connect
|
|
4
|
-
*
|
|
5
|
-
* Copyright (C) 2023 Hedera Hashgraph, LLC
|
|
6
|
-
*
|
|
7
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
-
* you may not use this file except in compliance with the License.
|
|
9
|
-
* You may obtain a copy of the License at
|
|
10
|
-
*
|
|
11
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
-
*
|
|
13
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
-
* See the License for the specific language governing permissions and
|
|
17
|
-
* limitations under the License.
|
|
18
|
-
*
|
|
19
|
-
*/
|
|
20
|
-
/**
|
|
21
|
-
* Enum representing different Hedera network chain IDs.
|
|
22
|
-
*
|
|
23
|
-
* @see {@link https://github.com/hashgraph/hedera-improvement-proposal/blob/main/HIP/hip-820.md#specification | HIP-820 Specification}
|
|
24
|
-
*/
|
|
25
|
-
export var HederaChainId;
|
|
26
|
-
(function (HederaChainId) {
|
|
27
|
-
HederaChainId["Mainnet"] = "hedera:mainnet";
|
|
28
|
-
HederaChainId["Testnet"] = "hedera:testnet";
|
|
29
|
-
HederaChainId["Previewnet"] = "hedera:previewnet";
|
|
30
|
-
HederaChainId["Devnet"] = "hedera:devnet";
|
|
31
|
-
})(HederaChainId || (HederaChainId = {}));
|