@safe-global/api-kit 2.5.9 → 3.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 (40) hide show
  1. package/dist/cjs/index.cjs +993 -0
  2. package/dist/esm/index.mjs +960 -0
  3. package/dist/src/SafeApiKit.d.ts +10 -1
  4. package/dist/src/SafeApiKit.d.ts.map +1 -0
  5. package/dist/src/index.d.ts +1 -0
  6. package/dist/src/index.d.ts.map +1 -0
  7. package/dist/src/types/safeTransactionServiceTypes.d.ts +3 -0
  8. package/dist/src/types/safeTransactionServiceTypes.d.ts.map +1 -0
  9. package/dist/src/utils/config.d.ts +1 -0
  10. package/dist/src/utils/config.d.ts.map +1 -0
  11. package/dist/src/utils/constants.d.ts +1 -0
  12. package/dist/src/utils/constants.d.ts.map +1 -0
  13. package/dist/src/utils/httpRequests.d.ts +1 -0
  14. package/dist/src/utils/httpRequests.d.ts.map +1 -0
  15. package/dist/src/utils/index.d.ts +1 -0
  16. package/dist/src/utils/index.d.ts.map +1 -0
  17. package/dist/src/utils/safeOperation.d.ts +5 -2
  18. package/dist/src/utils/safeOperation.d.ts.map +1 -0
  19. package/dist/src/utils/signDelegate.d.ts +1 -0
  20. package/dist/src/utils/signDelegate.d.ts.map +1 -0
  21. package/package.json +17 -8
  22. package/dist/src/SafeApiKit.js +0 -804
  23. package/dist/src/SafeApiKit.js.map +0 -1
  24. package/dist/src/index.js +0 -23
  25. package/dist/src/index.js.map +0 -1
  26. package/dist/src/types/safeTransactionServiceTypes.js +0 -3
  27. package/dist/src/types/safeTransactionServiceTypes.js.map +0 -1
  28. package/dist/src/utils/config.js +0 -25
  29. package/dist/src/utils/config.js.map +0 -1
  30. package/dist/src/utils/constants.js +0 -5
  31. package/dist/src/utils/constants.js.map +0 -1
  32. package/dist/src/utils/httpRequests.js +0 -59
  33. package/dist/src/utils/httpRequests.js.map +0 -1
  34. package/dist/src/utils/index.js +0 -7
  35. package/dist/src/utils/index.js.map +0 -1
  36. package/dist/src/utils/safeOperation.js +0 -19
  37. package/dist/src/utils/safeOperation.js.map +0 -1
  38. package/dist/src/utils/signDelegate.js +0 -25
  39. package/dist/src/utils/signDelegate.js.map +0 -1
  40. package/dist/tsconfig.build.tsbuildinfo +0 -1
@@ -0,0 +1,960 @@
1
+ // src/utils/httpRequests.ts
2
+ import fetch from "node-fetch";
3
+ async function sendRequest({ url, method, body }) {
4
+ const response = await fetch(url, {
5
+ method,
6
+ headers: {
7
+ Accept: "application/json",
8
+ "Content-Type": "application/json"
9
+ },
10
+ body: JSON.stringify(body)
11
+ });
12
+ let jsonResponse;
13
+ try {
14
+ jsonResponse = await response.json();
15
+ } catch (error) {
16
+ if (!response.ok) {
17
+ throw new Error(response.statusText);
18
+ }
19
+ }
20
+ if (response.ok) {
21
+ return jsonResponse;
22
+ }
23
+ if (jsonResponse.data) {
24
+ throw new Error(jsonResponse.data);
25
+ }
26
+ if (jsonResponse.detail) {
27
+ throw new Error(jsonResponse.detail);
28
+ }
29
+ if (jsonResponse.message) {
30
+ throw new Error(jsonResponse.message);
31
+ }
32
+ if (jsonResponse.nonFieldErrors) {
33
+ throw new Error(jsonResponse.nonFieldErrors);
34
+ }
35
+ if (jsonResponse.delegate) {
36
+ throw new Error(jsonResponse.delegate);
37
+ }
38
+ if (jsonResponse.safe) {
39
+ throw new Error(jsonResponse.safe);
40
+ }
41
+ if (jsonResponse.delegator) {
42
+ throw new Error(jsonResponse.delegator);
43
+ }
44
+ throw new Error(response.statusText);
45
+ }
46
+
47
+ // src/utils/signDelegate.ts
48
+ async function signDelegate(walletClient, delegateAddress, chainId) {
49
+ const domain = {
50
+ name: "Safe Transaction Service",
51
+ version: "1.0",
52
+ chainId: Number(chainId)
53
+ };
54
+ const types = {
55
+ Delegate: [
56
+ { name: "delegateAddress", type: "address" },
57
+ { name: "totp", type: "uint256" }
58
+ ]
59
+ };
60
+ const totp = Math.floor(Date.now() / 1e3 / 3600);
61
+ return walletClient.signTypedData({
62
+ domain,
63
+ types,
64
+ primaryType: "Delegate",
65
+ message: { delegateAddress, totp }
66
+ });
67
+ }
68
+
69
+ // src/SafeApiKit.ts
70
+ import { validateEip3770Address, validateEthereumAddress } from "@safe-global/protocol-kit";
71
+
72
+ // src/utils/config.ts
73
+ var TRANSACTION_SERVICE_URLS = {
74
+ "1": "https://safe-transaction-mainnet.safe.global/api",
75
+ "10": "https://safe-transaction-optimism.safe.global/api",
76
+ "56": "https://safe-transaction-bsc.safe.global/api",
77
+ "100": "https://safe-transaction-gnosis-chain.safe.global/api",
78
+ "137": "https://safe-transaction-polygon.safe.global/api",
79
+ "196": "https://safe-transaction-xlayer.safe.global/api",
80
+ "324": "https://safe-transaction-zksync.safe.global/api",
81
+ "1101": "https://safe-transaction-zkevm.safe.global/api",
82
+ "5000": "https://safe-transaction-mantle.safe.global/api",
83
+ "8453": "https://safe-transaction-base.safe.global/api",
84
+ "42161": "https://safe-transaction-arbitrum.safe.global/api",
85
+ "42220": "https://safe-transaction-celo.safe.global/api",
86
+ "43114": "https://safe-transaction-avalanche.safe.global/api",
87
+ "59144": "https://safe-transaction-linea.safe.global/api",
88
+ "81457": "https://safe-transaction-blast.safe.global/api",
89
+ "84532": "https://safe-transaction-base-sepolia.safe.global/api",
90
+ "534352": "https://safe-transaction-scroll.safe.global/api",
91
+ "11155111": "https://safe-transaction-sepolia.safe.global/api",
92
+ "1313161554": "https://safe-transaction-aurora.safe.global/api"
93
+ };
94
+
95
+ // src/utils/constants.ts
96
+ var EMPTY_DATA = "0x";
97
+
98
+ // src/utils/index.ts
99
+ var isEmptyData = (input) => !input || input === EMPTY_DATA;
100
+
101
+ // src/utils/safeOperation.ts
102
+ var getAddSafeOperationProps = async (safeOperation) => {
103
+ const userOperation = safeOperation.getUserOperation();
104
+ userOperation.signature = safeOperation.encodedSignatures();
105
+ return {
106
+ entryPoint: safeOperation.options.entryPoint,
107
+ moduleAddress: safeOperation.options.moduleAddress,
108
+ safeAddress: userOperation.sender,
109
+ userOperation,
110
+ options: {
111
+ validAfter: safeOperation.options.validAfter,
112
+ validUntil: safeOperation.options.validUntil
113
+ }
114
+ };
115
+ };
116
+ var isSafeOperation = (obj) => {
117
+ return "signatures" in obj && "getUserOperation" in obj && "getHash" in obj;
118
+ };
119
+
120
+ // src/SafeApiKit.ts
121
+ var SafeApiKit = class {
122
+ #chainId;
123
+ #txServiceBaseUrl;
124
+ constructor({ chainId, txServiceUrl }) {
125
+ this.#chainId = chainId;
126
+ if (txServiceUrl) {
127
+ this.#txServiceBaseUrl = txServiceUrl;
128
+ } else {
129
+ const url = TRANSACTION_SERVICE_URLS[chainId.toString()];
130
+ if (!url) {
131
+ throw new TypeError(
132
+ `There is no transaction service available for chainId ${chainId}. Please set the txServiceUrl property to use a custom transaction service.`
133
+ );
134
+ }
135
+ this.#txServiceBaseUrl = url;
136
+ }
137
+ }
138
+ #isValidAddress(address) {
139
+ try {
140
+ validateEthereumAddress(address);
141
+ return true;
142
+ } catch {
143
+ return false;
144
+ }
145
+ }
146
+ #getEip3770Address(fullAddress) {
147
+ return validateEip3770Address(fullAddress, this.#chainId);
148
+ }
149
+ /**
150
+ * Returns the information and configuration of the service.
151
+ *
152
+ * @returns The information and configuration of the service
153
+ */
154
+ async getServiceInfo() {
155
+ return sendRequest({
156
+ url: `${this.#txServiceBaseUrl}/v1/about`,
157
+ method: "get" /* Get */
158
+ });
159
+ }
160
+ /**
161
+ * Returns the list of Safe singletons.
162
+ *
163
+ * @returns The list of Safe singletons
164
+ */
165
+ async getServiceSingletonsInfo() {
166
+ return sendRequest({
167
+ url: `${this.#txServiceBaseUrl}/v1/about/singletons`,
168
+ method: "get" /* Get */
169
+ });
170
+ }
171
+ /**
172
+ * Decodes the specified Safe transaction data.
173
+ *
174
+ * @param data - The Safe transaction data. '0x' prefixed hexadecimal string.
175
+ * @param to - The address of the receiving contract. If provided, the decoded data will be more accurate, as in case of an ABI collision the Safe Transaction Service would know which ABI to use
176
+ * @returns The transaction data decoded
177
+ * @throws "Invalid data"
178
+ * @throws "Not Found"
179
+ * @throws "Ensure this field has at least 1 hexadecimal chars (not counting 0x)."
180
+ */
181
+ async decodeData(data, to) {
182
+ if (data === "") {
183
+ throw new Error("Invalid data");
184
+ }
185
+ const dataDecoderRequest = { data };
186
+ if (to) {
187
+ dataDecoderRequest.to = to;
188
+ }
189
+ return sendRequest({
190
+ url: `${this.#txServiceBaseUrl}/v1/data-decoder/`,
191
+ method: "post" /* Post */,
192
+ body: dataDecoderRequest
193
+ });
194
+ }
195
+ /**
196
+ * Returns the list of Safes where the address provided is an owner.
197
+ *
198
+ * @param ownerAddress - The owner address
199
+ * @returns The list of Safes where the address provided is an owner
200
+ * @throws "Invalid owner address"
201
+ * @throws "Checksum address validation failed"
202
+ */
203
+ async getSafesByOwner(ownerAddress) {
204
+ if (ownerAddress === "") {
205
+ throw new Error("Invalid owner address");
206
+ }
207
+ const { address } = this.#getEip3770Address(ownerAddress);
208
+ return sendRequest({
209
+ url: `${this.#txServiceBaseUrl}/v1/owners/${address}/safes/`,
210
+ method: "get" /* Get */
211
+ });
212
+ }
213
+ /**
214
+ * Returns the list of Safes where the module address provided is enabled.
215
+ *
216
+ * @param moduleAddress - The Safe module address
217
+ * @returns The list of Safe addresses where the module provided is enabled
218
+ * @throws "Invalid module address"
219
+ * @throws "Module address checksum not valid"
220
+ */
221
+ async getSafesByModule(moduleAddress) {
222
+ if (moduleAddress === "") {
223
+ throw new Error("Invalid module address");
224
+ }
225
+ const { address } = this.#getEip3770Address(moduleAddress);
226
+ return sendRequest({
227
+ url: `${this.#txServiceBaseUrl}/v1/modules/${address}/safes/`,
228
+ method: "get" /* Get */
229
+ });
230
+ }
231
+ /**
232
+ * Returns all the information of a Safe transaction.
233
+ *
234
+ * @param safeTxHash - Hash of the Safe transaction
235
+ * @returns The information of a Safe transaction
236
+ * @throws "Invalid safeTxHash"
237
+ * @throws "Not found."
238
+ */
239
+ async getTransaction(safeTxHash) {
240
+ if (safeTxHash === "") {
241
+ throw new Error("Invalid safeTxHash");
242
+ }
243
+ return sendRequest({
244
+ url: `${this.#txServiceBaseUrl}/v1/multisig-transactions/${safeTxHash}/`,
245
+ method: "get" /* Get */
246
+ });
247
+ }
248
+ /**
249
+ * Returns the list of confirmations for a given a Safe transaction.
250
+ *
251
+ * @param safeTxHash - The hash of the Safe transaction
252
+ * @returns The list of confirmations
253
+ * @throws "Invalid safeTxHash"
254
+ */
255
+ async getTransactionConfirmations(safeTxHash) {
256
+ if (safeTxHash === "") {
257
+ throw new Error("Invalid safeTxHash");
258
+ }
259
+ return sendRequest({
260
+ url: `${this.#txServiceBaseUrl}/v1/multisig-transactions/${safeTxHash}/confirmations/`,
261
+ method: "get" /* Get */
262
+ });
263
+ }
264
+ /**
265
+ * Adds a confirmation for a Safe transaction.
266
+ *
267
+ * @param safeTxHash - Hash of the Safe transaction that will be confirmed
268
+ * @param signature - Signature of the transaction
269
+ * @returns
270
+ * @throws "Invalid safeTxHash"
271
+ * @throws "Invalid signature"
272
+ * @throws "Malformed data"
273
+ * @throws "Error processing data"
274
+ */
275
+ async confirmTransaction(safeTxHash, signature) {
276
+ if (safeTxHash === "") {
277
+ throw new Error("Invalid safeTxHash");
278
+ }
279
+ if (signature === "") {
280
+ throw new Error("Invalid signature");
281
+ }
282
+ return sendRequest({
283
+ url: `${this.#txServiceBaseUrl}/v1/multisig-transactions/${safeTxHash}/confirmations/`,
284
+ method: "post" /* Post */,
285
+ body: {
286
+ signature
287
+ }
288
+ });
289
+ }
290
+ /**
291
+ * Returns the information and configuration of the provided Safe address.
292
+ *
293
+ * @param safeAddress - The Safe address
294
+ * @returns The information and configuration of the provided Safe address
295
+ * @throws "Invalid Safe address"
296
+ * @throws "Checksum address validation failed"
297
+ */
298
+ async getSafeInfo(safeAddress) {
299
+ if (safeAddress === "") {
300
+ throw new Error("Invalid Safe address");
301
+ }
302
+ const { address } = this.#getEip3770Address(safeAddress);
303
+ return sendRequest({
304
+ url: `${this.#txServiceBaseUrl}/v1/safes/${address}/`,
305
+ method: "get" /* Get */
306
+ }).then((response) => {
307
+ if (!response?.singleton) {
308
+ const { masterCopy, ...rest } = response;
309
+ return { ...rest, singleton: masterCopy };
310
+ }
311
+ return response;
312
+ });
313
+ }
314
+ /**
315
+ * Returns the list of delegates.
316
+ *
317
+ * @param getSafeDelegateProps - Properties to filter the returned list of delegates
318
+ * @returns The list of delegates
319
+ * @throws "Checksum address validation failed"
320
+ */
321
+ async getSafeDelegates({
322
+ safeAddress,
323
+ delegateAddress,
324
+ delegatorAddress,
325
+ label,
326
+ limit,
327
+ offset
328
+ }) {
329
+ const url = new URL(`${this.#txServiceBaseUrl}/v2/delegates`);
330
+ if (safeAddress) {
331
+ const { address: safe } = this.#getEip3770Address(safeAddress);
332
+ url.searchParams.set("safe", safe);
333
+ }
334
+ if (delegateAddress) {
335
+ const { address: delegate } = this.#getEip3770Address(delegateAddress);
336
+ url.searchParams.set("delegate", delegate);
337
+ }
338
+ if (delegatorAddress) {
339
+ const { address: delegator } = this.#getEip3770Address(delegatorAddress);
340
+ url.searchParams.set("delegator", delegator);
341
+ }
342
+ if (label) {
343
+ url.searchParams.set("label", label);
344
+ }
345
+ if (limit != null) {
346
+ url.searchParams.set("limit", limit.toString());
347
+ }
348
+ if (offset != null) {
349
+ url.searchParams.set("offset", offset.toString());
350
+ }
351
+ return sendRequest({
352
+ url: url.toString(),
353
+ method: "get" /* Get */
354
+ });
355
+ }
356
+ /**
357
+ * Adds a new delegate for a given Safe address.
358
+ *
359
+ * @param addSafeDelegateProps - The configuration of the new delegate
360
+ * @returns
361
+ * @throws "Invalid Safe delegate address"
362
+ * @throws "Invalid Safe delegator address"
363
+ * @throws "Invalid label"
364
+ * @throws "Checksum address validation failed"
365
+ * @throws "Address <delegate_address> is not checksumed"
366
+ * @throws "Safe=<safe_address> does not exist or it's still not indexed"
367
+ * @throws "Signing owner is not an owner of the Safe"
368
+ */
369
+ async addSafeDelegate({
370
+ safeAddress,
371
+ delegateAddress,
372
+ delegatorAddress,
373
+ label,
374
+ signer
375
+ }) {
376
+ if (delegateAddress === "") {
377
+ throw new Error("Invalid Safe delegate address");
378
+ }
379
+ if (delegatorAddress === "") {
380
+ throw new Error("Invalid Safe delegator address");
381
+ }
382
+ if (label === "") {
383
+ throw new Error("Invalid label");
384
+ }
385
+ const { address: delegate } = this.#getEip3770Address(delegateAddress);
386
+ const { address: delegator } = this.#getEip3770Address(delegatorAddress);
387
+ const signature = await signDelegate(signer, delegate, this.#chainId);
388
+ const body = {
389
+ safe: safeAddress ? this.#getEip3770Address(safeAddress).address : null,
390
+ delegate,
391
+ delegator,
392
+ label,
393
+ signature
394
+ };
395
+ return sendRequest({
396
+ url: `${this.#txServiceBaseUrl}/v2/delegates/`,
397
+ method: "post" /* Post */,
398
+ body
399
+ });
400
+ }
401
+ /**
402
+ * Removes a delegate for a given Safe address.
403
+ *
404
+ * @param deleteSafeDelegateProps - The configuration for the delegate that will be removed
405
+ * @returns
406
+ * @throws "Invalid Safe delegate address"
407
+ * @throws "Invalid Safe delegator address"
408
+ * @throws "Checksum address validation failed"
409
+ * @throws "Signing owner is not an owner of the Safe"
410
+ * @throws "Not found"
411
+ */
412
+ async removeSafeDelegate({
413
+ delegateAddress,
414
+ delegatorAddress,
415
+ signer
416
+ }) {
417
+ if (delegateAddress === "") {
418
+ throw new Error("Invalid Safe delegate address");
419
+ }
420
+ if (delegatorAddress === "") {
421
+ throw new Error("Invalid Safe delegator address");
422
+ }
423
+ const { address: delegate } = this.#getEip3770Address(delegateAddress);
424
+ const { address: delegator } = this.#getEip3770Address(delegatorAddress);
425
+ const signature = await signDelegate(signer, delegate, this.#chainId);
426
+ return sendRequest({
427
+ url: `${this.#txServiceBaseUrl}/v2/delegates/${delegate}`,
428
+ method: "delete" /* Delete */,
429
+ body: {
430
+ delegator,
431
+ signature
432
+ }
433
+ });
434
+ }
435
+ /**
436
+ * Returns the creation information of a Safe.
437
+ *
438
+ * @param safeAddress - The Safe address
439
+ * @returns The creation information of a Safe
440
+ * @throws "Invalid Safe address"
441
+ * @throws "Safe creation not found"
442
+ * @throws "Checksum address validation failed"
443
+ * @throws "Problem connecting to Ethereum network"
444
+ */
445
+ async getSafeCreationInfo(safeAddress) {
446
+ if (safeAddress === "") {
447
+ throw new Error("Invalid Safe address");
448
+ }
449
+ const { address } = this.#getEip3770Address(safeAddress);
450
+ return sendRequest({
451
+ url: `${this.#txServiceBaseUrl}/v1/safes/${address}/creation/`,
452
+ method: "get" /* Get */
453
+ }).then((response) => {
454
+ if (!response?.singleton) {
455
+ const { masterCopy, ...rest } = response;
456
+ return { ...rest, singleton: masterCopy };
457
+ }
458
+ return response;
459
+ });
460
+ }
461
+ /**
462
+ * Estimates the safeTxGas for a given Safe multi-signature transaction.
463
+ *
464
+ * @param safeAddress - The Safe address
465
+ * @param safeTransaction - The Safe transaction to estimate
466
+ * @returns The safeTxGas for the given Safe transaction
467
+ * @throws "Invalid Safe address"
468
+ * @throws "Data not valid"
469
+ * @throws "Safe not found"
470
+ * @throws "Tx not valid"
471
+ */
472
+ async estimateSafeTransaction(safeAddress, safeTransaction) {
473
+ if (safeAddress === "") {
474
+ throw new Error("Invalid Safe address");
475
+ }
476
+ const { address } = this.#getEip3770Address(safeAddress);
477
+ return sendRequest({
478
+ url: `${this.#txServiceBaseUrl}/v1/safes/${address}/multisig-transactions/estimations/`,
479
+ method: "post" /* Post */,
480
+ body: safeTransaction
481
+ });
482
+ }
483
+ /**
484
+ * Creates a new multi-signature transaction with its confirmations and stores it in the Safe Transaction Service.
485
+ *
486
+ * @param proposeTransactionConfig - The configuration of the proposed transaction
487
+ * @returns The hash of the Safe transaction proposed
488
+ * @throws "Invalid Safe address"
489
+ * @throws "Invalid safeTxHash"
490
+ * @throws "Invalid data"
491
+ * @throws "Invalid ethereum address/User is not an owner/Invalid signature/Nonce already executed/Sender is not an owner"
492
+ */
493
+ async proposeTransaction({
494
+ safeAddress,
495
+ safeTransactionData,
496
+ safeTxHash,
497
+ senderAddress,
498
+ senderSignature,
499
+ origin
500
+ }) {
501
+ if (safeAddress === "") {
502
+ throw new Error("Invalid Safe address");
503
+ }
504
+ const { address: safe } = this.#getEip3770Address(safeAddress);
505
+ const { address: sender } = this.#getEip3770Address(senderAddress);
506
+ if (safeTxHash === "") {
507
+ throw new Error("Invalid safeTxHash");
508
+ }
509
+ return sendRequest({
510
+ url: `${this.#txServiceBaseUrl}/v1/safes/${safe}/multisig-transactions/`,
511
+ method: "post" /* Post */,
512
+ body: {
513
+ ...safeTransactionData,
514
+ contractTransactionHash: safeTxHash,
515
+ sender,
516
+ signature: senderSignature,
517
+ origin
518
+ }
519
+ });
520
+ }
521
+ /**
522
+ * Returns the history of incoming transactions of a Safe account.
523
+ *
524
+ * @param safeAddress - The Safe address
525
+ * @returns The history of incoming transactions
526
+ * @throws "Invalid Safe address"
527
+ * @throws "Checksum address validation failed"
528
+ */
529
+ async getIncomingTransactions(safeAddress) {
530
+ if (safeAddress === "") {
531
+ throw new Error("Invalid Safe address");
532
+ }
533
+ const { address } = this.#getEip3770Address(safeAddress);
534
+ return sendRequest({
535
+ url: `${this.#txServiceBaseUrl}/v1/safes/${address}/incoming-transfers?executed=true`,
536
+ method: "get" /* Get */
537
+ });
538
+ }
539
+ /**
540
+ * Returns the history of module transactions of a Safe account.
541
+ *
542
+ * @param safeAddress - The Safe address
543
+ * @returns The history of module transactions
544
+ * @throws "Invalid Safe address"
545
+ * @throws "Invalid data"
546
+ * @throws "Invalid ethereum address"
547
+ */
548
+ async getModuleTransactions(safeAddress) {
549
+ if (safeAddress === "") {
550
+ throw new Error("Invalid Safe address");
551
+ }
552
+ const { address } = this.#getEip3770Address(safeAddress);
553
+ return sendRequest({
554
+ url: `${this.#txServiceBaseUrl}/v1/safes/${address}/module-transactions/`,
555
+ method: "get" /* Get */
556
+ });
557
+ }
558
+ /**
559
+ * Returns the history of multi-signature transactions of a Safe account.
560
+ *
561
+ * @param safeAddress - The Safe address
562
+ * @returns The history of multi-signature transactions
563
+ * @throws "Invalid Safe address"
564
+ * @throws "Checksum address validation failed"
565
+ */
566
+ async getMultisigTransactions(safeAddress) {
567
+ if (safeAddress === "") {
568
+ throw new Error("Invalid Safe address");
569
+ }
570
+ const { address } = this.#getEip3770Address(safeAddress);
571
+ return sendRequest({
572
+ url: `${this.#txServiceBaseUrl}/v1/safes/${address}/multisig-transactions/`,
573
+ method: "get" /* Get */
574
+ });
575
+ }
576
+ async getPendingTransactions(safeAddress, propsOrCurrentNonce = {}) {
577
+ if (safeAddress === "") {
578
+ throw new Error("Invalid Safe address");
579
+ }
580
+ let currentNonce;
581
+ let hasConfirmations;
582
+ let ordering;
583
+ let limit;
584
+ let offset;
585
+ if (typeof propsOrCurrentNonce === "object") {
586
+ ;
587
+ ({ currentNonce, hasConfirmations, ordering, limit, offset } = propsOrCurrentNonce);
588
+ } else {
589
+ console.warn(
590
+ "Deprecated: Use `currentNonce` inside an object instead. See `PendingTransactionsOptions`."
591
+ );
592
+ currentNonce = propsOrCurrentNonce;
593
+ }
594
+ const { address } = this.#getEip3770Address(safeAddress);
595
+ const nonce = currentNonce ? currentNonce : (await this.getSafeInfo(address)).nonce;
596
+ const url = new URL(
597
+ `${this.#txServiceBaseUrl}/v1/safes/${address}/multisig-transactions/?executed=false&nonce__gte=${nonce}`
598
+ );
599
+ if (hasConfirmations) {
600
+ url.searchParams.set("has_confirmations", hasConfirmations.toString());
601
+ }
602
+ if (ordering) {
603
+ url.searchParams.set("ordering", ordering);
604
+ }
605
+ if (limit != null) {
606
+ url.searchParams.set("limit", limit.toString());
607
+ }
608
+ if (offset != null) {
609
+ url.searchParams.set("offset", offset.toString());
610
+ }
611
+ return sendRequest({
612
+ url: url.toString(),
613
+ method: "get" /* Get */
614
+ });
615
+ }
616
+ /**
617
+ * Returns a list of transactions for a Safe. The list has different structures depending on the transaction type
618
+ *
619
+ * @param safeAddress - The Safe address
620
+ * @returns The list of transactions waiting for the confirmation of the Safe owners
621
+ * @throws "Invalid Safe address"
622
+ * @throws "Checksum address validation failed"
623
+ */
624
+ async getAllTransactions(safeAddress, options) {
625
+ if (safeAddress === "") {
626
+ throw new Error("Invalid Safe address");
627
+ }
628
+ const { address } = this.#getEip3770Address(safeAddress);
629
+ const url = new URL(`${this.#txServiceBaseUrl}/v1/safes/${address}/all-transactions/`);
630
+ const trusted = options?.trusted?.toString() || "true";
631
+ url.searchParams.set("trusted", trusted);
632
+ const queued = options?.queued?.toString() || "true";
633
+ url.searchParams.set("queued", queued);
634
+ const executed = options?.executed?.toString() || "false";
635
+ url.searchParams.set("executed", executed);
636
+ return sendRequest({
637
+ url: url.toString(),
638
+ method: "get" /* Get */
639
+ });
640
+ }
641
+ /**
642
+ * Returns the right nonce to propose a new transaction after the last pending transaction.
643
+ *
644
+ * @param safeAddress - The Safe address
645
+ * @returns The right nonce to propose a new transaction after the last pending transaction
646
+ * @throws "Invalid Safe address"
647
+ * @throws "Invalid data"
648
+ * @throws "Invalid ethereum address"
649
+ */
650
+ async getNextNonce(safeAddress) {
651
+ if (safeAddress === "") {
652
+ throw new Error("Invalid Safe address");
653
+ }
654
+ const { address } = this.#getEip3770Address(safeAddress);
655
+ const pendingTransactions = await this.getPendingTransactions(address);
656
+ if (pendingTransactions.results.length > 0) {
657
+ const maxNonce = pendingTransactions.results.reduce((acc, tx) => {
658
+ const curr = BigInt(tx.nonce);
659
+ return curr > acc ? curr : acc;
660
+ }, 0n);
661
+ return (maxNonce + 1n).toString();
662
+ }
663
+ const safeInfo = await this.getSafeInfo(address);
664
+ return safeInfo.nonce;
665
+ }
666
+ /**
667
+ * Returns the list of all the ERC20 tokens handled by the Safe.
668
+ *
669
+ * @returns The list of all the ERC20 tokens
670
+ */
671
+ async getTokenList() {
672
+ return sendRequest({
673
+ url: `${this.#txServiceBaseUrl}/v1/tokens/`,
674
+ method: "get" /* Get */
675
+ });
676
+ }
677
+ /**
678
+ * Returns the information of a given ERC20 token.
679
+ *
680
+ * @param tokenAddress - The token address
681
+ * @returns The information of the given ERC20 token
682
+ * @throws "Invalid token address"
683
+ * @throws "Checksum address validation failed"
684
+ */
685
+ async getToken(tokenAddress) {
686
+ if (tokenAddress === "") {
687
+ throw new Error("Invalid token address");
688
+ }
689
+ const { address } = this.#getEip3770Address(tokenAddress);
690
+ return sendRequest({
691
+ url: `${this.#txServiceBaseUrl}/v1/tokens/${address}/`,
692
+ method: "get" /* Get */
693
+ });
694
+ }
695
+ /**
696
+ * Get a message by its safe message hash
697
+ * @param messageHash The Safe message hash
698
+ * @returns The message
699
+ */
700
+ async getMessage(messageHash) {
701
+ if (!messageHash) {
702
+ throw new Error("Invalid messageHash");
703
+ }
704
+ return sendRequest({
705
+ url: `${this.#txServiceBaseUrl}/v1/messages/${messageHash}/`,
706
+ method: "get" /* Get */
707
+ });
708
+ }
709
+ /**
710
+ * Get the list of messages associated to a Safe account
711
+ * @param safeAddress The safe address
712
+ * @param options The options to filter the list of messages
713
+ * @returns The paginated list of messages
714
+ */
715
+ async getMessages(safeAddress, { ordering, limit, offset } = {}) {
716
+ if (!this.#isValidAddress(safeAddress)) {
717
+ throw new Error("Invalid safeAddress");
718
+ }
719
+ const url = new URL(`${this.#txServiceBaseUrl}/v1/safes/${safeAddress}/messages/`);
720
+ if (ordering) {
721
+ url.searchParams.set("ordering", ordering);
722
+ }
723
+ if (limit != null) {
724
+ url.searchParams.set("limit", limit.toString());
725
+ }
726
+ if (offset != null) {
727
+ url.searchParams.set("offset", offset.toString());
728
+ }
729
+ return sendRequest({
730
+ url: url.toString(),
731
+ method: "get" /* Get */
732
+ });
733
+ }
734
+ /**
735
+ * Creates a new message with an initial signature
736
+ * Add more signatures from other owners using addMessageSignature()
737
+ * @param safeAddress The safe address
738
+ * @param options The raw message to add, signature and safeAppId if any
739
+ */
740
+ async addMessage(safeAddress, addMessageProps) {
741
+ if (!this.#isValidAddress(safeAddress)) {
742
+ throw new Error("Invalid safeAddress");
743
+ }
744
+ return sendRequest({
745
+ url: `${this.#txServiceBaseUrl}/v1/safes/${safeAddress}/messages/`,
746
+ method: "post" /* Post */,
747
+ body: addMessageProps
748
+ });
749
+ }
750
+ /**
751
+ * Add a signature to an existing message
752
+ * @param messageHash The safe message hash
753
+ * @param signature The signature
754
+ */
755
+ async addMessageSignature(messageHash, signature) {
756
+ if (!messageHash || !signature) {
757
+ throw new Error("Invalid messageHash or signature");
758
+ }
759
+ return sendRequest({
760
+ url: `${this.#txServiceBaseUrl}/v1/messages/${messageHash}/signatures/`,
761
+ method: "post" /* Post */,
762
+ body: {
763
+ signature
764
+ }
765
+ });
766
+ }
767
+ /**
768
+ * Get the SafeOperations that were sent from a particular address.
769
+ * @param getSafeOperationsProps - The parameters to filter the list of SafeOperations
770
+ * @throws "Safe address must not be empty"
771
+ * @throws "Invalid Ethereum address {safeAddress}"
772
+ * @returns The SafeOperations sent from the given Safe's address
773
+ */
774
+ async getSafeOperationsByAddress({
775
+ safeAddress,
776
+ executed,
777
+ hasConfirmations,
778
+ ordering,
779
+ limit,
780
+ offset
781
+ }) {
782
+ if (!safeAddress) {
783
+ throw new Error("Safe address must not be empty");
784
+ }
785
+ const { address } = this.#getEip3770Address(safeAddress);
786
+ const url = new URL(`${this.#txServiceBaseUrl}/v1/safes/${address}/safe-operations/`);
787
+ if (ordering) {
788
+ url.searchParams.set("ordering", ordering);
789
+ }
790
+ if (limit != null) {
791
+ url.searchParams.set("limit", limit.toString());
792
+ }
793
+ if (offset != null) {
794
+ url.searchParams.set("offset", offset.toString());
795
+ }
796
+ if (hasConfirmations != null) {
797
+ url.searchParams.set("has_confirmations", hasConfirmations.toString());
798
+ }
799
+ if (executed != null) {
800
+ url.searchParams.set("executed", executed.toString());
801
+ }
802
+ return sendRequest({
803
+ url: url.toString(),
804
+ method: "get" /* Get */
805
+ });
806
+ }
807
+ /**
808
+ * Get the SafeOperations that are pending to send to the bundler
809
+ * @param getSafeOperationsProps - The parameters to filter the list of SafeOperations
810
+ * @throws "Safe address must not be empty"
811
+ * @throws "Invalid Ethereum address {safeAddress}"
812
+ * @returns The pending SafeOperations
813
+ */
814
+ async getPendingSafeOperations(props) {
815
+ return this.getSafeOperationsByAddress({
816
+ ...props,
817
+ executed: false
818
+ });
819
+ }
820
+ /**
821
+ * Get a SafeOperation by its hash.
822
+ * @param safeOperationHash The SafeOperation hash
823
+ * @throws "SafeOperation hash must not be empty"
824
+ * @throws "Not found."
825
+ * @returns The SafeOperation
826
+ */
827
+ async getSafeOperation(safeOperationHash) {
828
+ if (!safeOperationHash) {
829
+ throw new Error("SafeOperation hash must not be empty");
830
+ }
831
+ return sendRequest({
832
+ url: `${this.#txServiceBaseUrl}/v1/safe-operations/${safeOperationHash}/`,
833
+ method: "get" /* Get */
834
+ });
835
+ }
836
+ /**
837
+ * Create a new 4337 SafeOperation for a Safe.
838
+ * @param addSafeOperationProps - The configuration of the SafeOperation
839
+ * @throws "Safe address must not be empty"
840
+ * @throws "Invalid Safe address {safeAddress}"
841
+ * @throws "Module address must not be empty"
842
+ * @throws "Invalid module address {moduleAddress}"
843
+ * @throws "Signature must not be empty"
844
+ */
845
+ async addSafeOperation(safeOperation) {
846
+ let safeAddress, moduleAddress;
847
+ let addSafeOperationProps;
848
+ if (isSafeOperation(safeOperation)) {
849
+ addSafeOperationProps = await getAddSafeOperationProps(safeOperation);
850
+ } else {
851
+ addSafeOperationProps = safeOperation;
852
+ }
853
+ const {
854
+ entryPoint,
855
+ moduleAddress: moduleAddressProp,
856
+ options,
857
+ safeAddress: safeAddressProp,
858
+ userOperation
859
+ } = addSafeOperationProps;
860
+ if (!safeAddressProp) {
861
+ throw new Error("Safe address must not be empty");
862
+ }
863
+ try {
864
+ safeAddress = this.#getEip3770Address(safeAddressProp).address;
865
+ } catch (err) {
866
+ throw new Error(`Invalid Safe address ${safeAddressProp}`);
867
+ }
868
+ if (!moduleAddressProp) {
869
+ throw new Error("Module address must not be empty");
870
+ }
871
+ try {
872
+ moduleAddress = this.#getEip3770Address(moduleAddressProp).address;
873
+ } catch (err) {
874
+ throw new Error(`Invalid module address ${moduleAddressProp}`);
875
+ }
876
+ if (isEmptyData(userOperation.signature)) {
877
+ throw new Error("Signature must not be empty");
878
+ }
879
+ const getISOString = (date) => !date ? null : new Date(date * 1e3).toISOString();
880
+ const userOperationV06 = userOperation;
881
+ return sendRequest({
882
+ url: `${this.#txServiceBaseUrl}/v1/safes/${safeAddress}/safe-operations/`,
883
+ method: "post" /* Post */,
884
+ body: {
885
+ initCode: isEmptyData(userOperationV06.initCode) ? null : userOperationV06.initCode,
886
+ nonce: userOperation.nonce,
887
+ callData: userOperation.callData,
888
+ callGasLimit: userOperation.callGasLimit.toString(),
889
+ verificationGasLimit: userOperation.verificationGasLimit.toString(),
890
+ preVerificationGas: userOperation.preVerificationGas.toString(),
891
+ maxFeePerGas: userOperation.maxFeePerGas.toString(),
892
+ maxPriorityFeePerGas: userOperation.maxPriorityFeePerGas.toString(),
893
+ paymasterAndData: isEmptyData(userOperationV06.paymasterAndData) ? null : userOperationV06.paymasterAndData,
894
+ entryPoint,
895
+ validAfter: getISOString(options?.validAfter),
896
+ validUntil: getISOString(options?.validUntil),
897
+ signature: userOperation.signature,
898
+ moduleAddress
899
+ }
900
+ });
901
+ }
902
+ /**
903
+ * Returns the list of confirmations for a given a SafeOperation.
904
+ *
905
+ * @param safeOperationHash - The hash of the SafeOperation to get confirmations for
906
+ * @param getSafeOperationConfirmationsOptions - Additional options for fetching the list of confirmations
907
+ * @returns The list of confirmations
908
+ * @throws "Invalid SafeOperation hash"
909
+ * @throws "Invalid data"
910
+ */
911
+ async getSafeOperationConfirmations(safeOperationHash, { limit, offset } = {}) {
912
+ if (!safeOperationHash) {
913
+ throw new Error("Invalid SafeOperation hash");
914
+ }
915
+ const url = new URL(
916
+ `${this.#txServiceBaseUrl}/v1/safe-operations/${safeOperationHash}/confirmations/`
917
+ );
918
+ if (limit != null) {
919
+ url.searchParams.set("limit", limit.toString());
920
+ }
921
+ if (offset != null) {
922
+ url.searchParams.set("offset", offset.toString());
923
+ }
924
+ return sendRequest({
925
+ url: url.toString(),
926
+ method: "get" /* Get */
927
+ });
928
+ }
929
+ /**
930
+ * Adds a confirmation for a SafeOperation.
931
+ *
932
+ * @param safeOperationHash The SafeOperation hash
933
+ * @param signature - Signature of the SafeOperation
934
+ * @returns
935
+ * @throws "Invalid SafeOperation hash"
936
+ * @throws "Invalid signature"
937
+ * @throws "Malformed data"
938
+ * @throws "Error processing data"
939
+ */
940
+ async confirmSafeOperation(safeOperationHash, signature) {
941
+ if (!safeOperationHash) {
942
+ throw new Error("Invalid SafeOperation hash");
943
+ }
944
+ if (!signature) {
945
+ throw new Error("Invalid signature");
946
+ }
947
+ return sendRequest({
948
+ url: `${this.#txServiceBaseUrl}/v1/safe-operations/${safeOperationHash}/confirmations/`,
949
+ method: "post" /* Post */,
950
+ body: { signature }
951
+ });
952
+ }
953
+ };
954
+ var SafeApiKit_default = SafeApiKit;
955
+
956
+ // src/index.ts
957
+ var src_default = SafeApiKit_default;
958
+ export {
959
+ src_default as default
960
+ };