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