@safe-global/sdk-starter-kit 1.1.5 → 2.0.0-alpha.1

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.
Files changed (60) hide show
  1. package/dist/cjs/index.cjs +908 -0
  2. package/dist/esm/index.mjs +877 -0
  3. package/dist/src/BaseClient.d.ts +1 -0
  4. package/dist/src/BaseClient.d.ts.map +1 -0
  5. package/dist/src/SafeClient.d.ts +1 -0
  6. package/dist/src/SafeClient.d.ts.map +1 -0
  7. package/dist/src/constants.d.ts +2 -0
  8. package/dist/src/constants.d.ts.map +1 -0
  9. package/dist/src/extensions/index.d.ts +1 -0
  10. package/dist/src/extensions/index.d.ts.map +1 -0
  11. package/dist/src/extensions/messages/SafeMessageClient.d.ts +1 -0
  12. package/dist/src/extensions/messages/SafeMessageClient.d.ts.map +1 -0
  13. package/dist/src/extensions/messages/offChainMessages.d.ts +1 -0
  14. package/dist/src/extensions/messages/offChainMessages.d.ts.map +1 -0
  15. package/dist/src/extensions/messages/onChainMessages.d.ts +1 -0
  16. package/dist/src/extensions/messages/onChainMessages.d.ts.map +1 -0
  17. package/dist/src/extensions/safe-operations/SafeOperationClient.d.ts +1 -0
  18. package/dist/src/extensions/safe-operations/SafeOperationClient.d.ts.map +1 -0
  19. package/dist/src/extensions/safe-operations/safeOperations.d.ts +1 -0
  20. package/dist/src/extensions/safe-operations/safeOperations.d.ts.map +1 -0
  21. package/dist/src/index.d.ts +1 -0
  22. package/dist/src/index.d.ts.map +1 -0
  23. package/dist/src/types.d.ts +1 -0
  24. package/dist/src/types.d.ts.map +1 -0
  25. package/dist/src/utils/index.d.ts +7 -6
  26. package/dist/src/utils/index.d.ts.map +1 -0
  27. package/dist/src/utils/proposeTransaction.d.ts +1 -0
  28. package/dist/src/utils/proposeTransaction.d.ts.map +1 -0
  29. package/dist/src/utils/sendTransaction.d.ts +1 -0
  30. package/dist/src/utils/sendTransaction.d.ts.map +1 -0
  31. package/package.json +17 -8
  32. package/dist/src/BaseClient.js +0 -123
  33. package/dist/src/BaseClient.js.map +0 -1
  34. package/dist/src/SafeClient.js +0 -215
  35. package/dist/src/SafeClient.js.map +0 -1
  36. package/dist/src/constants.js +0 -37
  37. package/dist/src/constants.js.map +0 -1
  38. package/dist/src/extensions/index.js +0 -20
  39. package/dist/src/extensions/index.js.map +0 -1
  40. package/dist/src/extensions/messages/SafeMessageClient.js +0 -161
  41. package/dist/src/extensions/messages/SafeMessageClient.js.map +0 -1
  42. package/dist/src/extensions/messages/offChainMessages.js +0 -52
  43. package/dist/src/extensions/messages/offChainMessages.js.map +0 -1
  44. package/dist/src/extensions/messages/onChainMessages.js +0 -48
  45. package/dist/src/extensions/messages/onChainMessages.js.map +0 -1
  46. package/dist/src/extensions/safe-operations/SafeOperationClient.js +0 -127
  47. package/dist/src/extensions/safe-operations/SafeOperationClient.js.map +0 -1
  48. package/dist/src/extensions/safe-operations/safeOperations.js +0 -85
  49. package/dist/src/extensions/safe-operations/safeOperations.js.map +0 -1
  50. package/dist/src/index.js +0 -112
  51. package/dist/src/index.js.map +0 -1
  52. package/dist/src/types.js +0 -3
  53. package/dist/src/types.js.map +0 -1
  54. package/dist/src/utils/index.js +0 -57
  55. package/dist/src/utils/index.js.map +0 -1
  56. package/dist/src/utils/proposeTransaction.js +0 -29
  57. package/dist/src/utils/proposeTransaction.js.map +0 -1
  58. package/dist/src/utils/sendTransaction.js +0 -28
  59. package/dist/src/utils/sendTransaction.js.map +0 -1
  60. package/dist/tsconfig.build.tsbuildinfo +0 -1
@@ -0,0 +1,908 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ SafeClient: () => SafeClient,
34
+ createSafeClient: () => createSafeClient,
35
+ offChainMessages: () => offChainMessages,
36
+ onChainMessages: () => onChainMessages,
37
+ safeOperations: () => safeOperations
38
+ });
39
+ module.exports = __toCommonJS(src_exports);
40
+ var import_protocol_kit6 = __toESM(require("@safe-global/protocol-kit"));
41
+ var import_api_kit = __toESM(require("@safe-global/api-kit"));
42
+
43
+ // src/utils/index.ts
44
+ var import_protocol_kit2 = require("@safe-global/protocol-kit");
45
+
46
+ // src/constants.ts
47
+ var DEFAULT_SAFE_MODULES_VERSION = "0.2.0";
48
+ var DEFAULT_DEPLOYMENT_TYPE = "canonical";
49
+ var TRANSACTION_EXECUTED = "The transaction has been executed, check the ethereumTxHash in the transactions property to view it on the corresponding blockchain explorer";
50
+ var TRANSACTION_SAVED = "The transaction was not executed on-chain yet. There are pending signatures and you need to confirm it with other Safe owners first. Use the confirm(safeTxHash) method with other signer connected to the client";
51
+ var OFFCHAIN_MESSAGE_SAVED = "The message was stored using the Safe Transaction Service, you need to confirm it with other Safe owners in order to make it valid. Use the confirmMessage(messageHash) method with other signer connected to the client";
52
+ var OFFCHAIN_MESSAGE_CONFIRMED = "The message was stored using Safe services and now is confirmed and valid";
53
+ var SAFE_OPERATION_SAVED = "The UserOperation was stored using the Safe Transaction Service as an SafeOperation, you need to confirm it with other Safe owners in order to make it valid. Use the confirmSafeOperation(safeOperationHash) method with other signer connected to the client";
54
+ var SAFE_OPERATION_SENT_TO_BUNDLER = "The SafeOperation was sent to the bundler for being processed. Check the userOperationHash in the safeOperations property to see the SafeOperation in the corresponding explorer";
55
+ var SAFE_DEPLOYED = "A new Safe account was deployed, check the ethereumTxHash in the safeAccountDeployment property to view it on the corresponding blockchain explorer";
56
+ var MESSAGES = {
57
+ ["DEPLOYED_AND_EXECUTED" /* DEPLOYED_AND_EXECUTED */]: `${SAFE_DEPLOYED}. ${TRANSACTION_EXECUTED}`,
58
+ ["DEPLOYED_AND_PENDING_SIGNATURES" /* DEPLOYED_AND_PENDING_SIGNATURES */]: `${SAFE_DEPLOYED}. ${TRANSACTION_SAVED}`,
59
+ ["EXECUTED" /* EXECUTED */]: TRANSACTION_EXECUTED,
60
+ ["PENDING_SIGNATURES" /* PENDING_SIGNATURES */]: TRANSACTION_SAVED,
61
+ ["MESSAGE_PENDING_SIGNATURES" /* MESSAGE_PENDING_SIGNATURES */]: OFFCHAIN_MESSAGE_SAVED,
62
+ ["MESSAGE_CONFIRMED" /* MESSAGE_CONFIRMED */]: OFFCHAIN_MESSAGE_CONFIRMED,
63
+ ["DEPLOYED_AND_MESSAGE_PENDING_SIGNATURES" /* DEPLOYED_AND_MESSAGE_PENDING_SIGNATURES */]: `${SAFE_DEPLOYED}. ${OFFCHAIN_MESSAGE_SAVED}`,
64
+ ["DEPLOYED_AND_MESSAGE_CONFIRMED" /* DEPLOYED_AND_MESSAGE_CONFIRMED */]: `${SAFE_DEPLOYED}. ${OFFCHAIN_MESSAGE_CONFIRMED}`,
65
+ ["SAFE_OPERATION_EXECUTED" /* SAFE_OPERATION_EXECUTED */]: SAFE_OPERATION_SENT_TO_BUNDLER,
66
+ ["SAFE_OPERATION_PENDING_SIGNATURES" /* SAFE_OPERATION_PENDING_SIGNATURES */]: SAFE_OPERATION_SAVED
67
+ };
68
+
69
+ // src/utils/sendTransaction.ts
70
+ var import_actions = require("viem/actions");
71
+ var sendTransaction = async ({
72
+ transaction,
73
+ protocolKit
74
+ }) => {
75
+ const signer = await protocolKit.getSafeProvider().getExternalSigner();
76
+ const client = protocolKit.getSafeProvider().getExternalProvider();
77
+ if (!signer)
78
+ throw new Error("SafeProvider must be initialized with a signer to use this function");
79
+ const hash = await signer.sendTransaction({
80
+ to: transaction.to,
81
+ data: transaction.data,
82
+ value: BigInt(transaction.value),
83
+ account: signer.account
84
+ });
85
+ const receipt = await (0, import_actions.waitForTransactionReceipt)(client, { hash });
86
+ return receipt.transactionHash;
87
+ };
88
+
89
+ // src/utils/proposeTransaction.ts
90
+ var import_protocol_kit = require("@safe-global/protocol-kit");
91
+ var proposeTransaction = async ({
92
+ safeTransaction,
93
+ protocolKit,
94
+ apiKit
95
+ }) => {
96
+ safeTransaction = await protocolKit.signTransaction(safeTransaction);
97
+ const signerAddress = await protocolKit.getSafeProvider().getSignerAddress() || "0x";
98
+ const ethSig = safeTransaction.getSignature(signerAddress);
99
+ const safeTxHash = await protocolKit.getTransactionHash(safeTransaction);
100
+ const txOptions = {
101
+ safeAddress: await protocolKit.getAddress(),
102
+ safeTransactionData: safeTransaction.data,
103
+ safeTxHash,
104
+ senderAddress: signerAddress,
105
+ senderSignature: (0, import_protocol_kit.buildSignatureBytes)([ethSig])
106
+ };
107
+ await apiKit.proposeTransaction(txOptions);
108
+ return safeTxHash;
109
+ };
110
+
111
+ // src/utils/index.ts
112
+ var isValidAddress = (address) => {
113
+ try {
114
+ (0, import_protocol_kit2.validateEthereumAddress)(address);
115
+ return true;
116
+ } catch {
117
+ return false;
118
+ }
119
+ };
120
+ var isValidSafeConfig = (config) => {
121
+ if (!config.owners || !config.threshold) return false;
122
+ return true;
123
+ };
124
+ var waitSafeTxReceipt = async (txResult) => {
125
+ const receipt = txResult.transactionResponse ? await txResult.transactionResponse.wait() : void 0;
126
+ return receipt;
127
+ };
128
+ var createSafeClientResult = ({
129
+ status,
130
+ safeAddress,
131
+ deploymentTxHash,
132
+ safeTxHash,
133
+ txHash,
134
+ messageHash,
135
+ userOperationHash,
136
+ safeOperationHash
137
+ }) => {
138
+ return {
139
+ safeAddress,
140
+ description: MESSAGES[status],
141
+ status,
142
+ transactions: txHash || safeTxHash ? { ethereumTxHash: txHash, safeTxHash } : void 0,
143
+ messages: messageHash ? { messageHash } : void 0,
144
+ safeOperations: userOperationHash || safeOperationHash ? { userOperationHash, safeOperationHash } : void 0,
145
+ safeAccountDeployment: deploymentTxHash ? { ethereumTxHash: deploymentTxHash } : void 0
146
+ };
147
+ };
148
+
149
+ // src/BaseClient.ts
150
+ var BaseClient = class {
151
+ constructor(protocolKit, apiKit) {
152
+ this.protocolKit = protocolKit;
153
+ this.apiKit = apiKit;
154
+ }
155
+ /**
156
+ * Returns the Safe address.
157
+ *
158
+ * @returns {string} The Safe address
159
+ */
160
+ async getAddress() {
161
+ return this.protocolKit.getAddress();
162
+ }
163
+ /**
164
+ * Checks if the current Safe is deployed.
165
+ *
166
+ * @returns {boolean} if the Safe contract is deployed
167
+ */
168
+ async isDeployed() {
169
+ return this.protocolKit.isSafeDeployed();
170
+ }
171
+ /**
172
+ * Checks if a specific address is an owner of the current Safe.
173
+ *
174
+ * @param {string} ownerAddress - The account address
175
+ * @returns {boolean} TRUE if the account is an owner
176
+ */
177
+ async isOwner(ownerAddress) {
178
+ return this.protocolKit.isOwner(ownerAddress);
179
+ }
180
+ /**
181
+ * Returns the list of Safe owner accounts.
182
+ *
183
+ * @returns The list of owners
184
+ */
185
+ async getOwners() {
186
+ return this.protocolKit.getOwners();
187
+ }
188
+ /**
189
+ * Returns the Safe threshold.
190
+ *
191
+ * @returns {number} The Safe threshold
192
+ */
193
+ async getThreshold() {
194
+ return this.protocolKit.getThreshold();
195
+ }
196
+ /**
197
+ * Returns the Safe nonce.
198
+ *
199
+ * @returns {number} The Safe nonce
200
+ */
201
+ async getNonce() {
202
+ return this.protocolKit.getNonce();
203
+ }
204
+ /**
205
+ * Returns a list of owners who have approved a specific Safe transaction.
206
+ *
207
+ * @param {string} txHash - The Safe transaction hash
208
+ * @returns {string[]} The list of owners
209
+ */
210
+ async getOwnersWhoApprovedTransaction(txHash) {
211
+ return this.protocolKit.getOwnersWhoApprovedTx(txHash);
212
+ }
213
+ /**
214
+ * Encodes the data for adding a new owner to the Safe.
215
+ *
216
+ * @param {AddOwnerTxParams | AddPasskeyOwnerTxParams} addOwnerParams - The parameters for adding a new owner
217
+ * @returns {TransactionBase} The encoded data
218
+ */
219
+ async createAddOwnerTransaction(addOwnerParams) {
220
+ const addOwnerTransaction = await this.protocolKit.createAddOwnerTx(addOwnerParams);
221
+ return this.#buildTransaction(addOwnerTransaction);
222
+ }
223
+ /**
224
+ * Encodes the data for removing an owner from the Safe.
225
+ *
226
+ * @param {RemoveOwnerTxParams | RemovePasskeyOwnerTxParams} removeOwnerParams - The parameters for removing an owner
227
+ * @returns {TransactionBase} The encoded data
228
+ */
229
+ async createRemoveOwnerTransaction(removeOwnerParams) {
230
+ const removeOwnerTransaction = await this.protocolKit.createRemoveOwnerTx(removeOwnerParams);
231
+ return this.#buildTransaction(removeOwnerTransaction);
232
+ }
233
+ /**
234
+ * Encodes the data for swapping an owner in the Safe.
235
+ *
236
+ * @param {SwapOwnerTxParams} swapParams - The parameters for swapping an owner
237
+ * @returns {TransactionBase} The encoded data
238
+ */
239
+ async createSwapOwnerTransaction(swapParams) {
240
+ const swapOwnerTransaction = await this.protocolKit.createSwapOwnerTx(swapParams);
241
+ return this.#buildTransaction(swapOwnerTransaction);
242
+ }
243
+ /**
244
+ * Encodes the data for changing the Safe threshold.
245
+ *
246
+ * @param {ChangeThresholdTxParams} changeThresholdParams - The parameters for changing the Safe threshold
247
+ * @returns {TransactionBase} The encoded data
248
+ */
249
+ async createChangeThresholdTransaction(changeThresholdParams) {
250
+ const changeThresholdTransaction = await this.protocolKit.createChangeThresholdTx(
251
+ changeThresholdParams.threshold
252
+ );
253
+ return this.#buildTransaction(changeThresholdTransaction);
254
+ }
255
+ async #buildTransaction(safeTransaction) {
256
+ return {
257
+ to: safeTransaction.data.to,
258
+ value: safeTransaction.data.value,
259
+ data: safeTransaction.data.data
260
+ };
261
+ }
262
+ };
263
+
264
+ // src/SafeClient.ts
265
+ var SafeClient = class extends BaseClient {
266
+ constructor(protocolKit, apiKit) {
267
+ super(protocolKit, apiKit);
268
+ }
269
+ /**
270
+ * Sends transactions through the Safe protocol.
271
+ * You can send an array to transactions { to, value, data} that we will convert to a transaction batch
272
+ *
273
+ * @param {SendTransactionProps} props The SendTransactionProps object.
274
+ * @param {TransactionBase[]} props.transactions An array of transactions to be sent.
275
+ * @param {string} props.transactions[].to The recipient address of the transaction.
276
+ * @param {string} props.transactions[].value The value of the transaction.
277
+ * @param {string} props.transactions[].data The data of the transaction.
278
+ * @param {string} props.from The sender address of the transaction.
279
+ * @param {number | string} props.gasLimit The gas limit of the transaction.
280
+ * @param {number | string} props.gasPrice The gas price of the transaction.
281
+ * @param {number | string} props.maxFeePerGas The max fee per gas of the transaction.
282
+ * @param {number | string} props.maxPriorityFeePerGas The max priority fee per gas of the transaction.
283
+ * @param {number} props.nonce The nonce of the transaction.
284
+ * @returns {Promise<SafeClientResult>} A promise that resolves to the result of the transaction.
285
+ */
286
+ async send({
287
+ transactions,
288
+ ...transactionOptions
289
+ }) {
290
+ const isSafeDeployed = await this.protocolKit.isSafeDeployed();
291
+ const isMultisigSafe = await this.protocolKit.getThreshold() > 1;
292
+ const safeTransaction = await this.protocolKit.createTransaction({ transactions });
293
+ if (isSafeDeployed) {
294
+ if (isMultisigSafe) {
295
+ return this.#proposeTransaction({ safeTransaction });
296
+ } else {
297
+ return this.#executeTransaction({ safeTransaction, ...transactionOptions });
298
+ }
299
+ } else {
300
+ if (isMultisigSafe) {
301
+ return this.#deployAndProposeTransaction({ safeTransaction, ...transactionOptions });
302
+ } else {
303
+ return this.#deployAndExecuteTransaction({ safeTransaction, ...transactionOptions });
304
+ }
305
+ }
306
+ }
307
+ /**
308
+ * Confirms a transaction by its safe transaction hash.
309
+ *
310
+ * @param {ConfirmTransactionProps} props The ConfirmTransactionProps object.
311
+ * @param {string} props.safeTxHash The hash of the safe transaction to confirm.
312
+ * @returns {Promise<SafeClientResult>} A promise that resolves to the result of the confirmed transaction.
313
+ * @throws {Error} If the transaction confirmation fails.
314
+ */
315
+ async confirm({ safeTxHash }) {
316
+ let transactionResponse = await this.apiKit.getTransaction(safeTxHash);
317
+ const safeAddress = await this.protocolKit.getAddress();
318
+ const signedTransaction = await this.protocolKit.signTransaction(transactionResponse);
319
+ await this.apiKit.confirmTransaction(safeTxHash, signedTransaction.encodedSignatures());
320
+ transactionResponse = await this.apiKit.getTransaction(safeTxHash);
321
+ if (transactionResponse.confirmations && transactionResponse.confirmationsRequired === transactionResponse.confirmations.length) {
322
+ const executedTransactionResponse = await this.protocolKit.executeTransaction(transactionResponse);
323
+ await waitSafeTxReceipt(executedTransactionResponse);
324
+ return createSafeClientResult({
325
+ status: "EXECUTED" /* EXECUTED */,
326
+ safeAddress,
327
+ txHash: executedTransactionResponse.hash,
328
+ safeTxHash
329
+ });
330
+ }
331
+ return createSafeClientResult({
332
+ status: "PENDING_SIGNATURES" /* PENDING_SIGNATURES */,
333
+ safeAddress,
334
+ safeTxHash
335
+ });
336
+ }
337
+ /**
338
+ * Retrieves the pending transactions for the current safe address.
339
+ *
340
+ * @async
341
+ * @returns {Promise<SafeMultisigTransactionListResponse>} A promise that resolves to an array of pending transactions.
342
+ * @throws {Error} If there is an issue retrieving the safe address or pending transactions.
343
+ */
344
+ async getPendingTransactions() {
345
+ const safeAddress = await this.protocolKit.getAddress();
346
+ return this.apiKit.getPendingTransactions(safeAddress);
347
+ }
348
+ extend(extendFunc) {
349
+ const result = extendFunc(this);
350
+ if (result instanceof Promise) {
351
+ return result.then((extensions) => Object.assign(this, extensions));
352
+ } else {
353
+ return Object.assign(this, result);
354
+ }
355
+ }
356
+ /**
357
+ * Deploys and executes a transaction in one step.
358
+ *
359
+ * @param {SafeTransaction} safeTransaction The safe transaction to be executed
360
+ * @param {TransactionOptions} options Optional transaction options
361
+ * @returns A promise that resolves to the result of the transaction
362
+ */
363
+ async #deployAndExecuteTransaction({
364
+ safeTransaction,
365
+ ...transactionOptions
366
+ }) {
367
+ safeTransaction = await this.protocolKit.signTransaction(safeTransaction);
368
+ const transactionBatchWithDeployment = await this.protocolKit.wrapSafeTransactionIntoDeploymentBatch(
369
+ safeTransaction,
370
+ transactionOptions
371
+ );
372
+ const hash = await sendTransaction({
373
+ transaction: transactionBatchWithDeployment,
374
+ protocolKit: this.protocolKit
375
+ });
376
+ await this.#reconnectSafe();
377
+ return createSafeClientResult({
378
+ safeAddress: await this.protocolKit.getAddress(),
379
+ status: "DEPLOYED_AND_EXECUTED" /* DEPLOYED_AND_EXECUTED */,
380
+ deploymentTxHash: hash,
381
+ txHash: hash
382
+ });
383
+ }
384
+ /**
385
+ * Deploys and proposes a transaction in one step.
386
+ *
387
+ * @param {SafeTransaction} safeTransaction The safe transaction to be proposed
388
+ * @param {TransactionOptions} transactionOptions Optional transaction options
389
+ * @returns A promise that resolves to the result of the transaction
390
+ */
391
+ async #deployAndProposeTransaction({
392
+ safeTransaction,
393
+ ...transactionOptions
394
+ }) {
395
+ const safeDeploymentTransaction = await this.protocolKit.createSafeDeploymentTransaction();
396
+ const hash = await sendTransaction({
397
+ transaction: { ...safeDeploymentTransaction, ...transactionOptions },
398
+ protocolKit: this.protocolKit
399
+ });
400
+ await this.#reconnectSafe();
401
+ safeTransaction = await this.protocolKit.signTransaction(safeTransaction);
402
+ const safeTxHash = await proposeTransaction({
403
+ safeTransaction,
404
+ protocolKit: this.protocolKit,
405
+ apiKit: this.apiKit
406
+ });
407
+ return createSafeClientResult({
408
+ safeAddress: await this.protocolKit.getAddress(),
409
+ status: "DEPLOYED_AND_PENDING_SIGNATURES" /* DEPLOYED_AND_PENDING_SIGNATURES */,
410
+ deploymentTxHash: hash,
411
+ safeTxHash
412
+ });
413
+ }
414
+ /**
415
+ * Executes a transaction.
416
+ *
417
+ * @param {SafeTransaction} safeTransaction The safe transaction to be executed
418
+ * @param {TransactionOptions} transactionOptions Optional transaction options
419
+ * @returns A promise that resolves to the result of the transaction
420
+ */
421
+ async #executeTransaction({
422
+ safeTransaction,
423
+ ...transactionOptions
424
+ }) {
425
+ safeTransaction = await this.protocolKit.signTransaction(safeTransaction);
426
+ const { hash } = await this.protocolKit.executeTransaction(safeTransaction, transactionOptions);
427
+ return createSafeClientResult({
428
+ safeAddress: await this.protocolKit.getAddress(),
429
+ status: "EXECUTED" /* EXECUTED */,
430
+ txHash: hash
431
+ });
432
+ }
433
+ /**
434
+ * Proposes a transaction to the Safe.
435
+ * @param { SafeTransaction } safeTransaction The safe transaction to propose
436
+ * @returns The SafeClientResult
437
+ */
438
+ async #proposeTransaction({ safeTransaction }) {
439
+ const safeTxHash = await proposeTransaction({
440
+ safeTransaction,
441
+ protocolKit: this.protocolKit,
442
+ apiKit: this.apiKit
443
+ });
444
+ return createSafeClientResult({
445
+ safeAddress: await this.protocolKit.getAddress(),
446
+ status: "PENDING_SIGNATURES" /* PENDING_SIGNATURES */,
447
+ safeTxHash
448
+ });
449
+ }
450
+ async #reconnectSafe() {
451
+ this.protocolKit = await this.protocolKit.connect({
452
+ provider: this.protocolKit.getSafeProvider().provider,
453
+ signer: this.protocolKit.getSafeProvider().signer,
454
+ safeAddress: await this.protocolKit.getAddress()
455
+ });
456
+ }
457
+ };
458
+
459
+ // src/extensions/messages/onChainMessages.ts
460
+ var import_protocol_kit3 = require("@safe-global/protocol-kit");
461
+ var import_types_kit = require("@safe-global/types-kit");
462
+ function onChainMessages() {
463
+ return (client) => ({
464
+ /**
465
+ * Creates and sends a message as a regular transaction using the SignMessageLib contract
466
+ * The message can be a string or an EIP712TypedData object
467
+ * As this method creates a new transaction you can confirm it using the safeTxHash and the confirm() method and
468
+ * retrieve the pending transactions using the getPendingTransactions() method from the general client
469
+ * @param {SendOnChainMessageProps} props The message properties
470
+ * @returns {Promise<SafeClientResult>} A SafeClientResult. You can get the safeTxHash to confirm from the transaction property
471
+ */
472
+ async sendOnChainMessage(props) {
473
+ const { message, ...transactionOptions } = props;
474
+ const signMessageLibContract = await (0, import_protocol_kit3.getSignMessageLibContract)({
475
+ safeProvider: client.protocolKit.getSafeProvider(),
476
+ safeVersion: client.protocolKit.getContractVersion()
477
+ });
478
+ const transaction = {
479
+ to: signMessageLibContract.getAddress(),
480
+ value: "0",
481
+ data: signMessageLibContract.encode("signMessage", [(0, import_protocol_kit3.hashSafeMessage)(message)]),
482
+ operation: import_types_kit.OperationType.DelegateCall
483
+ };
484
+ return client.send({ transactions: [transaction], ...transactionOptions });
485
+ }
486
+ });
487
+ }
488
+
489
+ // src/extensions/messages/SafeMessageClient.ts
490
+ var import_protocol_kit4 = require("@safe-global/protocol-kit");
491
+ var SafeMessageClient = class {
492
+ /**
493
+ * @constructor
494
+ * @param {Safe} protocolKit A Safe instance
495
+ * @param {SafeApiKit} apiKit A SafeApiKit instance
496
+ */
497
+ constructor(protocolKit, apiKit) {
498
+ this.protocolKit = protocolKit;
499
+ this.apiKit = apiKit;
500
+ }
501
+ /**
502
+ * Send off-chain messages using the Transaction service
503
+ *
504
+ * @param {SendOffChainMessageProps} props The message properties
505
+ * @param {string | EIP712TypedData} props.message The message to be sent. Can be a raw string or an EIP712TypedData object
506
+ * @returns {Promise<SafeClientResult>} A SafeClientResult. You can get the messageHash to confirmMessage() afterwards from the messages property
507
+ */
508
+ async sendMessage({ message }) {
509
+ const isSafeDeployed = await this.protocolKit.isSafeDeployed();
510
+ const safeMessage = this.protocolKit.createMessage(message);
511
+ if (isSafeDeployed) {
512
+ return this.#addMessage({ safeMessage });
513
+ } else {
514
+ return this.#deployAndAddMessage({ safeMessage });
515
+ }
516
+ }
517
+ /**
518
+ * Confirms an off-chain message using the Transaction service
519
+ *
520
+ * @param {ConfirmOffChainMessageProps} props The confirmation properties
521
+ * @param {string} props.messageHash The messageHash. Returned from the sendMessage() method inside the SafeClientResult messages property
522
+ * @returns {Promise<SafeClientResult>} A SafeClientResult with the result of the confirmation
523
+ */
524
+ async confirmMessage({ messageHash }) {
525
+ let messageResponse = await this.apiKit.getMessage(messageHash);
526
+ const safeAddress = await this.protocolKit.getAddress();
527
+ const threshold = await this.protocolKit.getThreshold();
528
+ let safeMessage = this.protocolKit.createMessage(messageResponse.message);
529
+ safeMessage = await this.protocolKit.signMessage(safeMessage);
530
+ await this.apiKit.addMessageSignature(messageHash, safeMessage.encodedSignatures());
531
+ messageResponse = await this.apiKit.getMessage(messageHash);
532
+ return createSafeClientResult({
533
+ status: messageResponse.confirmations.length === threshold ? "MESSAGE_CONFIRMED" /* MESSAGE_CONFIRMED */ : "MESSAGE_PENDING_SIGNATURES" /* MESSAGE_PENDING_SIGNATURES */,
534
+ safeAddress,
535
+ messageHash
536
+ });
537
+ }
538
+ /**
539
+ * Get the list of pending off-chain messages. This messages can be confirmed using the confirmMessage() method
540
+ *
541
+ * @param {ListOptions} options The pagination options
542
+ * @returns {Promise<SafeMessageListResponse>} A list of pending messages
543
+ */
544
+ async getPendingMessages(options) {
545
+ const safeAddress = await this.protocolKit.getAddress();
546
+ return this.apiKit.getMessages(safeAddress, options);
547
+ }
548
+ /**
549
+ * Deploys a new Safe account based on the provided config and adds a message using the Transaction service
550
+ * - If the Safe threshold > 1, we need to deploy the Safe account first and afterwards add the message
551
+ * The message should be confirmed with other owners using the confirmMessage() method until the threshold is reached in order to be valid
552
+ * - If the threshold = 1, we can deploy the Safe account and add the message in one step. The message will be valid immediately
553
+ *
554
+ * @param {SafeTransaction} safeMessage The safe message
555
+ * @returns {Promise<SafeClientResult>} The SafeClientResult
556
+ */
557
+ async #deployAndAddMessage({
558
+ safeMessage
559
+ }) {
560
+ let deploymentTxHash;
561
+ const threshold = await this.protocolKit.getThreshold();
562
+ const safeDeploymentTransaction = await this.protocolKit.createSafeDeploymentTransaction();
563
+ try {
564
+ deploymentTxHash = await sendTransaction({
565
+ transaction: safeDeploymentTransaction,
566
+ protocolKit: this.protocolKit
567
+ });
568
+ await this.#updateProtocolKitWithDeployedSafe();
569
+ } catch (error) {
570
+ throw new Error("Could not deploy the Safe account");
571
+ }
572
+ try {
573
+ const { messages } = await this.#addMessage({ safeMessage });
574
+ const messageResponse = await this.apiKit.getMessage(messages?.messageHash || "0x");
575
+ return createSafeClientResult({
576
+ safeAddress: await this.protocolKit.getAddress(),
577
+ status: messageResponse.confirmations.length === threshold ? "DEPLOYED_AND_MESSAGE_CONFIRMED" /* DEPLOYED_AND_MESSAGE_CONFIRMED */ : "DEPLOYED_AND_MESSAGE_PENDING_SIGNATURES" /* DEPLOYED_AND_MESSAGE_PENDING_SIGNATURES */,
578
+ deploymentTxHash,
579
+ messageHash: messages?.messageHash
580
+ });
581
+ } catch (error) {
582
+ throw new Error("Could not add a new off-chain message to the Safe account");
583
+ }
584
+ }
585
+ /**
586
+ * Add a new off-chain message using the Transaction service
587
+ * - If the threshold > 1, remember to confirmMessage() after sendMessage()
588
+ * - If the threshold = 1, then the message is confirmed and valid immediately
589
+ *
590
+ * @param {SafeMessage} safeMessage The message
591
+ * @returns {Promise<SafeClientResult>} The SafeClientResult
592
+ */
593
+ async #addMessage({ safeMessage }) {
594
+ const safeAddress = await this.protocolKit.getAddress();
595
+ const threshold = await this.protocolKit.getThreshold();
596
+ const signedMessage = await this.protocolKit.signMessage(safeMessage);
597
+ const messageHash = await this.protocolKit.getSafeMessageHash((0, import_protocol_kit4.hashSafeMessage)(safeMessage.data));
598
+ try {
599
+ await this.apiKit.addMessage(safeAddress, {
600
+ message: safeMessage.data,
601
+ signature: signedMessage.encodedSignatures()
602
+ });
603
+ } catch (error) {
604
+ throw new Error("Could not add a new off-chain message to the Safe account");
605
+ }
606
+ const message = await this.apiKit.getMessage(messageHash);
607
+ return createSafeClientResult({
608
+ safeAddress: await this.protocolKit.getAddress(),
609
+ status: message.confirmations.length === threshold ? "MESSAGE_CONFIRMED" /* MESSAGE_CONFIRMED */ : "MESSAGE_PENDING_SIGNATURES" /* MESSAGE_PENDING_SIGNATURES */,
610
+ messageHash
611
+ });
612
+ }
613
+ /**
614
+ * This method updates the Safe instance with the deployed Safe account
615
+ */
616
+ async #updateProtocolKitWithDeployedSafe() {
617
+ this.protocolKit = await this.protocolKit.connect({
618
+ provider: this.protocolKit.getSafeProvider().provider,
619
+ signer: this.protocolKit.getSafeProvider().signer,
620
+ safeAddress: await this.protocolKit.getAddress()
621
+ });
622
+ }
623
+ };
624
+
625
+ // src/extensions/messages/offChainMessages.ts
626
+ function offChainMessages() {
627
+ return (client) => {
628
+ const safeMessageClient = new SafeMessageClient(client.protocolKit, client.apiKit);
629
+ return {
630
+ /**
631
+ * Creates an off-chain message using the Transaction service
632
+ *
633
+ * @param {SendOffChainMessageProps} props The message properties
634
+ * @returns {Promise<SafeClientResult>} A SafeClientResult. You can get the messageHash to confirmMessage() afterwards from the messages property */
635
+ async sendOffChainMessage(props) {
636
+ return safeMessageClient.sendMessage(props);
637
+ },
638
+ /**
639
+ * Confirms an off-chain message using the Transaction service
640
+ *
641
+ * @param {ConfirmOffChainMessageProps} props The confirmation properties
642
+ * @returns {Promise<SafeClientResult>} A SafeClientResult with the result of the confirmation
643
+ */
644
+ async confirmOffChainMessage(props) {
645
+ return safeMessageClient.confirmMessage(props);
646
+ },
647
+ /**
648
+ * Get the list of pending off-chain messages. This messages can be confirmed using the confirmMessage() method
649
+ *
650
+ * @param {ListOptions} options The pagination options
651
+ * @returns {Promise<SafeMessageListResponse>} A list of pending messages
652
+ */
653
+ async getPendingOffChainMessages(options) {
654
+ return safeMessageClient.getPendingMessages(options);
655
+ }
656
+ };
657
+ };
658
+ }
659
+
660
+ // src/extensions/safe-operations/safeOperations.ts
661
+ var import_relay_kit = require("@safe-global/relay-kit");
662
+
663
+ // src/extensions/safe-operations/SafeOperationClient.ts
664
+ var import_protocol_kit5 = require("@safe-global/protocol-kit");
665
+ var SafeOperationClient = class {
666
+ constructor(safe4337Pack, apiKit) {
667
+ this.protocolKit = safe4337Pack.protocolKit;
668
+ this.apiKit = apiKit;
669
+ this.safe4337Pack = safe4337Pack;
670
+ }
671
+ /**
672
+ * Send SafeOperations from a group of transactions.
673
+ * This method will convert your transactions in a batch and:
674
+ * - If the threshold > 1 it will save for later the SafeOperation using the Transaction service
675
+ * You must confirmSafeOperation() with other owners
676
+ * - If the threshold = 1 the SafeOperation can be submitted to the bundler so it will execute it immediately
677
+ *
678
+ * @param {Safe4337CreateTransactionProps} props The Safe4337CreateTransactionProps object
679
+ * @param {SafeTransaction[]} props.transactions An array of transactions to be batched
680
+ * @param {TransactionOptions} [props.amountToApprove] The amount to approve for the SafeOperation
681
+ * @param {TransactionOptions} [props.validUntil] The validUntil timestamp for the SafeOperation
682
+ * @param {TransactionOptions} [props.validAfter] The validAfter timestamp for the SafeOperation
683
+ * @param {TransactionOptions} [props.feeEstimator] The feeEstimator to calculate the fees
684
+ * @returns {Promise<SafeClientResult>} A promise that resolves with the status of the SafeOperation
685
+ */
686
+ async sendSafeOperation({
687
+ transactions,
688
+ ...sendSafeOperationOptions
689
+ }) {
690
+ const safeAddress = await this.protocolKit.getAddress();
691
+ const isMultisigSafe = await this.protocolKit.getThreshold() > 1;
692
+ let safeOperation = await this.safe4337Pack.createTransaction({
693
+ transactions,
694
+ options: sendSafeOperationOptions
695
+ });
696
+ safeOperation = await this.safe4337Pack.signSafeOperation(safeOperation);
697
+ if (isMultisigSafe) {
698
+ await this.apiKit.addSafeOperation(safeOperation);
699
+ const safeOperationHash = safeOperation.getHash();
700
+ return createSafeClientResult({
701
+ safeAddress,
702
+ status: "SAFE_OPERATION_PENDING_SIGNATURES" /* SAFE_OPERATION_PENDING_SIGNATURES */,
703
+ safeOperationHash
704
+ });
705
+ }
706
+ const userOperationHash = await this.safe4337Pack.executeTransaction({
707
+ executable: safeOperation
708
+ });
709
+ await this.#waitForOperationToFinish({ userOperationHash });
710
+ return createSafeClientResult({
711
+ safeAddress,
712
+ status: "SAFE_OPERATION_EXECUTED" /* SAFE_OPERATION_EXECUTED */,
713
+ userOperationHash,
714
+ safeOperationHash: safeOperation.getHash()
715
+ });
716
+ }
717
+ /**
718
+ * Confirms the stored safeOperation
719
+ *
720
+ * @param {ConfirmSafeOperationProps} props The confirmation properties
721
+ * @param {string} props.safeOperationHash The hash of the safe operation to confirm.
722
+ * The safeOperationHash can be extracted from the SafeClientResult of the sendSafeOperation method under the safeOperations property
723
+ * You must confirmSafeOperation() with the other owners and once the threshold is reached the SafeOperation will be sent to the bundler
724
+ * @returns {Promise<SafeClientResult>} A promise that resolves to the result of the safeOperation.
725
+ */
726
+ async confirmSafeOperation({
727
+ safeOperationHash
728
+ }) {
729
+ const safeAddress = await this.protocolKit.getAddress();
730
+ const threshold = await this.protocolKit.getThreshold();
731
+ await this.apiKit.confirmSafeOperation(
732
+ safeOperationHash,
733
+ (0, import_protocol_kit5.buildSignatureBytes)([await this.protocolKit.signHash(safeOperationHash)])
734
+ );
735
+ const confirmedSafeOperation = await this.apiKit.getSafeOperation(safeOperationHash);
736
+ if (confirmedSafeOperation?.confirmations?.length === threshold) {
737
+ const userOperationHash = await this.safe4337Pack.executeTransaction({
738
+ executable: confirmedSafeOperation
739
+ });
740
+ await this.#waitForOperationToFinish({ userOperationHash });
741
+ return createSafeClientResult({
742
+ status: "SAFE_OPERATION_EXECUTED" /* SAFE_OPERATION_EXECUTED */,
743
+ safeAddress,
744
+ userOperationHash,
745
+ safeOperationHash
746
+ });
747
+ }
748
+ return createSafeClientResult({
749
+ status: "SAFE_OPERATION_PENDING_SIGNATURES" /* SAFE_OPERATION_PENDING_SIGNATURES */,
750
+ safeAddress,
751
+ safeOperationHash
752
+ });
753
+ }
754
+ /**
755
+ * Retrieves the pending Safe operations for the current Safe account
756
+ *
757
+ * @async
758
+ * @param {ListOptions} options The pagination options
759
+ * @returns {Promise<GetSafeOperationListResponse>} A promise that resolves to an array of pending Safe operations.
760
+ * @throws {Error} If there is an issue retrieving the safe address or pending Safe operations.
761
+ */
762
+ async getPendingSafeOperations(options) {
763
+ const safeAddress = await this.protocolKit.getAddress();
764
+ return this.apiKit.getPendingSafeOperations({ safeAddress, ...options });
765
+ }
766
+ /**
767
+ * Helper method to wait for the operation to finish
768
+ * @param userOperationHash The userOperationHash to wait for. This comes from the bundler and can be obtained from the
769
+ * SafeClientResult method under the safeOperations property
770
+ */
771
+ async #waitForOperationToFinish({
772
+ userOperationHash
773
+ }) {
774
+ let userOperationReceipt = null;
775
+ while (!userOperationReceipt) {
776
+ await new Promise((resolve) => setTimeout(resolve, 2e3));
777
+ userOperationReceipt = await this.safe4337Pack.getUserOperationReceipt(userOperationHash);
778
+ }
779
+ }
780
+ };
781
+
782
+ // src/extensions/safe-operations/safeOperations.ts
783
+ function safeOperations({ bundlerUrl }, paymasterOptions) {
784
+ return async (client) => {
785
+ const { provider, signer } = client.protocolKit.getSafeProvider();
786
+ const isSafeDeployed = await client.protocolKit.isSafeDeployed();
787
+ let options;
788
+ if (isSafeDeployed) {
789
+ const safeAddress = await client.protocolKit.getAddress();
790
+ options = {
791
+ safeAddress
792
+ };
793
+ } else {
794
+ const { safeDeploymentConfig, safeAccountConfig } = client.protocolKit.getPredictedSafe();
795
+ options = {
796
+ owners: safeAccountConfig.owners,
797
+ threshold: safeAccountConfig.threshold,
798
+ ...safeDeploymentConfig
799
+ };
800
+ }
801
+ const safe4337Pack = await import_relay_kit.Safe4337Pack.init({
802
+ provider,
803
+ signer,
804
+ safeModulesVersion: DEFAULT_SAFE_MODULES_VERSION,
805
+ bundlerUrl,
806
+ options,
807
+ paymasterOptions
808
+ });
809
+ client.protocolKit = safe4337Pack.protocolKit;
810
+ const safeOperationClient = new SafeOperationClient(safe4337Pack, client.apiKit);
811
+ return {
812
+ /**
813
+ * Send SafeOperations from a group of transactions.
814
+ * This method will convert your transactions in a batch and:
815
+ * - If the threshold > 1 it will save for later the SafeOperation using the Transaction service
816
+ * You must confirmSafeOperation() with other owners
817
+ * - If the threshold = 1 the SafeOperation can be submitted to the bundler so it will execute it immediately
818
+ *
819
+ * @param {Safe4337CreateTransactionProps} props The Safe4337CreateTransactionProps object
820
+ * @returns {Promise<SafeClientResult>} A promise that resolves with the status of the SafeOperation
821
+ */
822
+ async sendSafeOperation(props) {
823
+ return safeOperationClient.sendSafeOperation(props);
824
+ },
825
+ /**
826
+ * Confirms the stored safeOperation
827
+ *
828
+ * @param {ConfirmSafeOperationProps} props The ConfirmSafeOperationProps object
829
+ * @returns {Promise<SafeClientResult>} A promise that resolves to the result of the safeOperation.
830
+ */
831
+ async confirmSafeOperation(props) {
832
+ return safeOperationClient.confirmSafeOperation(props);
833
+ },
834
+ /**
835
+ * Retrieves the pending Safe operations for the current Safe account
836
+ *
837
+ * @async
838
+ * @param {ListOptions} options The pagination options
839
+ * @returns {Promise<GetSafeOperationListResponse>} A promise that resolves to an array of pending Safe operations.
840
+ * @throws {Error} If there is an issue retrieving the safe address or pending Safe operations.
841
+ */
842
+ async getPendingSafeOperations(options2) {
843
+ return safeOperationClient.getPendingSafeOperations(options2);
844
+ }
845
+ };
846
+ };
847
+ }
848
+
849
+ // src/index.ts
850
+ async function createSafeClient(config) {
851
+ const protocolKit = await getProtocolKitInstance(config);
852
+ const apiKit = await getApiKitInstance(protocolKit, config);
853
+ if (!protocolKit || !apiKit) throw new Error("Failed to create a kit instances");
854
+ return new SafeClient(protocolKit, apiKit);
855
+ }
856
+ async function getProtocolKitInstance(config) {
857
+ if (config.safeAddress && isValidAddress(config.safeAddress)) {
858
+ return import_protocol_kit6.default.init({
859
+ provider: config.provider,
860
+ signer: config.signer,
861
+ safeAddress: config.safeAddress
862
+ });
863
+ } else if (config.safeOptions && isValidSafeConfig(config.safeOptions)) {
864
+ let protocolKit;
865
+ const initConfig = {
866
+ provider: config.provider,
867
+ signer: config.signer,
868
+ predictedSafe: {
869
+ safeAccountConfig: {
870
+ owners: config.safeOptions.owners,
871
+ threshold: config.safeOptions.threshold
872
+ },
873
+ safeDeploymentConfig: {
874
+ saltNonce: config.safeOptions.saltNonce,
875
+ deploymentType: DEFAULT_DEPLOYMENT_TYPE
876
+ }
877
+ }
878
+ };
879
+ try {
880
+ protocolKit = await import_protocol_kit6.default.init(initConfig);
881
+ } catch (error) {
882
+ const isDeploymentTypeUnresolvedError = error instanceof Error && error.message && error.message.startsWith("Invalid") && error.message.includes("contract address");
883
+ if (isDeploymentTypeUnresolvedError && initConfig.predictedSafe.safeDeploymentConfig?.deploymentType) {
884
+ delete initConfig.predictedSafe.safeDeploymentConfig.deploymentType;
885
+ protocolKit = await import_protocol_kit6.default.init(initConfig);
886
+ } else {
887
+ throw error;
888
+ }
889
+ }
890
+ const isSafeDeployed = await protocolKit.isSafeDeployed();
891
+ if (isSafeDeployed) {
892
+ return import_protocol_kit6.default.init({
893
+ provider: config.provider,
894
+ signer: config.signer,
895
+ safeAddress: await protocolKit.getAddress()
896
+ });
897
+ }
898
+ return protocolKit;
899
+ } else {
900
+ throw new Error(
901
+ "Invalid configuration: either a valid safeAddress or valid safeOptions must be provided."
902
+ );
903
+ }
904
+ }
905
+ async function getApiKitInstance(protocolKit, config) {
906
+ const chainId = await protocolKit.getChainId();
907
+ return new import_api_kit.default({ chainId, txServiceUrl: config.txServiceUrl });
908
+ }