@shelby-protocol/sdk 0.2.4 → 0.3.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 (124) hide show
  1. package/dist/browser/index.d.ts +13 -4
  2. package/dist/browser/index.mjs +3719 -140
  3. package/dist/{clay-codes-DHP-bYcP.d.ts → clay-codes-DdXABBDx.d.ts} +0 -7
  4. package/dist/core/aptos-explorer.mjs +46 -5
  5. package/dist/core/blobs.d.ts +1 -1
  6. package/dist/core/blobs.mjs +12 -4
  7. package/dist/core/chunk.mjs +58 -9
  8. package/dist/core/clients/ShelbyBlobClient.d.ts +9 -44
  9. package/dist/core/clients/ShelbyBlobClient.mjs +1084 -17
  10. package/dist/core/clients/ShelbyClient.d.ts +12 -2
  11. package/dist/core/clients/ShelbyClient.mjs +2915 -23
  12. package/dist/core/clients/ShelbyClientConfig.d.ts +8 -0
  13. package/dist/core/clients/ShelbyClientConfig.mjs +0 -1
  14. package/dist/core/clients/ShelbyMetadataClient.d.ts +0 -18
  15. package/dist/core/clients/ShelbyMetadataClient.mjs +306 -6
  16. package/dist/core/clients/ShelbyMicropaymentChannelClient.d.ts +7 -49
  17. package/dist/core/clients/ShelbyMicropaymentChannelClient.mjs +839 -13
  18. package/dist/core/clients/ShelbyPlacementGroupClient.mjs +307 -8
  19. package/dist/core/clients/ShelbyRPCClient.d.ts +107 -4
  20. package/dist/core/clients/ShelbyRPCClient.mjs +1261 -12
  21. package/dist/core/clients/index.d.ts +3 -2
  22. package/dist/core/clients/index.mjs +3421 -40
  23. package/dist/core/clients/utils.mjs +65 -6
  24. package/dist/core/commitments.d.ts +3 -12
  25. package/dist/core/commitments.mjs +270 -11
  26. package/dist/core/constants.d.ts +4 -4
  27. package/dist/core/constants.mjs +39 -16
  28. package/dist/core/erasure/clay-codes.d.ts +1 -1
  29. package/dist/core/erasure/clay-codes.mjs +129 -4
  30. package/dist/core/erasure/constants.mjs +33 -10
  31. package/dist/core/erasure/default.d.ts +1 -1
  32. package/dist/core/erasure/default.mjs +191 -9
  33. package/dist/core/erasure/index.d.ts +1 -1
  34. package/dist/core/erasure/index.mjs +266 -23
  35. package/dist/core/erasure/provider.d.ts +1 -1
  36. package/dist/core/erasure/reed-solomon.d.ts +1 -1
  37. package/dist/core/erasure/reed-solomon.mjs +68 -4
  38. package/dist/core/erasure/utils.d.ts +1 -1
  39. package/dist/core/erasure/utils.mjs +0 -2
  40. package/dist/core/errors.mjs +29 -8
  41. package/dist/core/index.d.ts +4 -4
  42. package/dist/core/index.mjs +3713 -140
  43. package/dist/core/layout.d.ts +1 -1
  44. package/dist/core/layout.mjs +37 -5
  45. package/dist/core/networks.mjs +10 -5
  46. package/dist/core/operations/generated/sdk.d.ts +9 -0
  47. package/dist/core/operations/generated/sdk.mjs +183 -18
  48. package/dist/core/operations/index.mjs +284 -24
  49. package/dist/core/promises.mjs +4 -4
  50. package/dist/core/rpc-responses.d.ts +38 -1
  51. package/dist/core/rpc-responses.mjs +31 -8
  52. package/dist/core/shelby-explorer.mjs +31 -6
  53. package/dist/core/strings.mjs +0 -2
  54. package/dist/core/types/blobs.d.ts +1 -1
  55. package/dist/core/types/blobs.mjs +0 -1
  56. package/dist/core/types/index.d.ts +1 -1
  57. package/dist/core/types/index.mjs +230 -9
  58. package/dist/core/types/payments.d.ts +6 -6
  59. package/dist/core/types/payments.mjs +230 -5
  60. package/dist/core/types/placement_groups.mjs +0 -1
  61. package/dist/core/types/storage_providers.mjs +0 -1
  62. package/dist/core/utils.mjs +94 -8
  63. package/dist/node/clients/ShelbyNodeClient.d.ts +2 -1
  64. package/dist/node/clients/ShelbyNodeClient.mjs +2919 -24
  65. package/dist/node/clients/index.d.ts +2 -1
  66. package/dist/node/clients/index.mjs +2920 -26
  67. package/dist/node/index.d.ts +4 -4
  68. package/dist/node/index.mjs +3736 -147
  69. package/dist/node/parallel/commitment_worker.d.ts +9 -0
  70. package/dist/node/parallel/commitment_worker.mjs +298 -0
  71. package/dist/node/parallel/commitment_worker_pool.d.ts +35 -0
  72. package/dist/node/parallel/commitment_worker_pool.mjs +286 -0
  73. package/dist/node/parallel/default_commitment_worker_pool.d.ts +24 -0
  74. package/dist/node/parallel/default_commitment_worker_pool.mjs +302 -0
  75. package/dist/node/parallel/index.d.ts +11 -0
  76. package/dist/node/parallel/index.mjs +563 -0
  77. package/dist/node/parallel/parallel_commitments.d.ts +32 -0
  78. package/dist/node/parallel/parallel_commitments.mjs +561 -0
  79. package/dist/node/parallel/worker_pool.d.ts +74 -0
  80. package/dist/node/parallel/worker_pool.mjs +248 -0
  81. package/dist/node/testUtil.mjs +8 -4
  82. package/package.json +9 -6
  83. package/dist/chunk-3NRBHSMQ.mjs +0 -12
  84. package/dist/chunk-3PCG7PNP.mjs +0 -375
  85. package/dist/chunk-7OV5ZYW6.mjs +0 -55
  86. package/dist/chunk-7P6ASYW6.mjs +0 -9
  87. package/dist/chunk-A4IG6GSE.mjs +0 -21
  88. package/dist/chunk-AD2G3QYD.mjs +0 -0
  89. package/dist/chunk-AEDVYYGD.mjs +0 -11
  90. package/dist/chunk-AUQDI5BS.mjs +0 -43
  91. package/dist/chunk-BDSW5PHM.mjs +0 -37
  92. package/dist/chunk-BKL7NCUB.mjs +0 -412
  93. package/dist/chunk-BUWAD67L.mjs +0 -505
  94. package/dist/chunk-BXEVML7N.mjs +0 -169
  95. package/dist/chunk-C6RQ3AEU.mjs +0 -51
  96. package/dist/chunk-CQ6QPIZK.mjs +0 -37
  97. package/dist/chunk-D6GQHO6G.mjs +0 -15
  98. package/dist/chunk-EM67QTMR.mjs +0 -0
  99. package/dist/chunk-I6NG5GNL.mjs +0 -8
  100. package/dist/chunk-IE6LYVIA.mjs +0 -26
  101. package/dist/chunk-IKRWBDG5.mjs +0 -42
  102. package/dist/chunk-JTXYKO3U.mjs +0 -38
  103. package/dist/chunk-KYQRWJ3U.mjs +0 -49
  104. package/dist/chunk-MB7C7VQF.mjs +0 -0
  105. package/dist/chunk-MQUVYMNQ.mjs +0 -0
  106. package/dist/chunk-NHWWORCH.mjs +0 -72
  107. package/dist/chunk-NI6XBZLI.mjs +0 -83
  108. package/dist/chunk-OGKZ575S.mjs +0 -136
  109. package/dist/chunk-QQ57OGQ2.mjs +0 -0
  110. package/dist/chunk-SSE4MFNN.mjs +0 -278
  111. package/dist/chunk-TET3DJEO.mjs +0 -199
  112. package/dist/chunk-TVLV7C74.mjs +0 -207
  113. package/dist/chunk-UEZNZBJO.mjs +0 -104
  114. package/dist/chunk-WJTVYLKW.mjs +0 -782
  115. package/dist/chunk-WTICJPDB.mjs +0 -0
  116. package/dist/chunk-XNEIWM4O.mjs +0 -0
  117. package/dist/chunk-XRLHLAOW.mjs +0 -51
  118. package/dist/chunk-Z4FZ7W6L.mjs +0 -39
  119. package/dist/chunk-Z7RFCADT.mjs +0 -0
  120. package/dist/chunk-ZHXCVRZX.mjs +0 -0
  121. package/dist/core/operations/generated/types.d.ts +0 -908
  122. package/dist/core/operations/generated/types.mjs +0 -63
  123. package/dist/core/types/encodings.d.ts +0 -11
  124. package/dist/core/types/encodings.mjs +0 -0
@@ -1,782 +0,0 @@
1
- import {
2
- defaultErasureCodingConfig
3
- } from "./chunk-XRLHLAOW.mjs";
4
- import {
5
- getShelbyIndexerClient
6
- } from "./chunk-KYQRWJ3U.mjs";
7
- import {
8
- getAptosConfig
9
- } from "./chunk-IKRWBDG5.mjs";
10
- import {
11
- createBlobKey
12
- } from "./chunk-3NRBHSMQ.mjs";
13
- import {
14
- expectedTotalChunksets
15
- } from "./chunk-BXEVML7N.mjs";
16
- import {
17
- getBlobNameSuffix,
18
- normalizeAddress
19
- } from "./chunk-UEZNZBJO.mjs";
20
- import {
21
- ERASURE_CODE_AND_CHUNK_MAPPING
22
- } from "./chunk-JTXYKO3U.mjs";
23
- import {
24
- ERASURE_CODE_PARAMS
25
- } from "./chunk-AUQDI5BS.mjs";
26
- import {
27
- SHELBY_DEPLOYER
28
- } from "./chunk-7OV5ZYW6.mjs";
29
-
30
- // src/core/clients/ShelbyBlobClient.ts
31
- import {
32
- AccountAddress,
33
- Aptos,
34
- Hex,
35
- U32
36
- } from "@aptos-labs/ts-sdk";
37
- var MissingTransactionSubmitterError = class extends Error {
38
- constructor() {
39
- super(
40
- "USD sponsorship (usdSponsor) requires a transaction submitter to be configured. The sponsored transaction is a multi-agent transaction that must be co-signed by the sponsor. Configure a transaction submitter via aptos.pluginSettings.TRANSACTION_SUBMITTER that routes transactions to a service (like a gas station) capable of providing the sponsor's signature."
41
- );
42
- this.name = "MissingTransactionSubmitterError";
43
- }
44
- };
45
- var ShelbyBlobClient = class _ShelbyBlobClient {
46
- aptos;
47
- deployer;
48
- indexer;
49
- defaultOptions;
50
- /**
51
- * The ShelbyBlobClient is used to interact with the Shelby contract on the Aptos blockchain. This
52
- * includes functions for registering blob commitments and retrieving blob metadata.
53
- *
54
- * @param config - The client configuration object.
55
- * @param config.network - The Shelby network to use.
56
- * @param defaultOptions - Optional default options for blob operations.
57
- *
58
- * @example
59
- * ```typescript
60
- * const blobClient = new ShelbyBlobClient({
61
- * aptos: {
62
- * network: Network.SHELBYNET,
63
- * clientConfig: {
64
- * API_KEY: "AG-***",
65
- * },
66
- * },
67
- * });
68
- * ```
69
- *
70
- * @example
71
- * ```typescript
72
- * // With default options for USD sponsorship
73
- * const blobClient = new ShelbyBlobClient(
74
- * {
75
- * network: Network.SHELBYNET,
76
- * aptos: {
77
- * pluginSettings: {
78
- * TRANSACTION_SUBMITTER: myGasStationSubmitter,
79
- * },
80
- * },
81
- * },
82
- * {
83
- * usdSponsor: { feePayerAddress: sponsorAddress },
84
- * }
85
- * );
86
- * ```
87
- */
88
- constructor(config, defaultOptions) {
89
- this.aptos = new Aptos(getAptosConfig(config));
90
- this.deployer = config.deployer ?? AccountAddress.fromString(SHELBY_DEPLOYER);
91
- this.indexer = getShelbyIndexerClient(config);
92
- this.defaultOptions = defaultOptions ?? {};
93
- }
94
- /**
95
- * Merges method-level options with default options, giving precedence to method-level values.
96
- */
97
- mergeOptions(options) {
98
- return {
99
- build: options?.build ?? this.defaultOptions.build,
100
- submit: options?.submit ?? this.defaultOptions.submit,
101
- usdSponsor: options?.usdSponsor ?? this.defaultOptions.usdSponsor,
102
- chunksetSizeBytes: options?.chunksetSizeBytes ?? this.defaultOptions.chunksetSizeBytes
103
- };
104
- }
105
- /**
106
- * Validates that if USD sponsorship is requested, a transaction submitter is
107
- * configured as well. Checks both the client-level and method-level transaction
108
- * submitter configurations. Throws MissingTransactionSubmitterError if usdSponsor is
109
- * provided without a transaction submitter.
110
- */
111
- validateUsdSponsorConfig(usdSponsor, submit) {
112
- if (!usdSponsor) {
113
- return;
114
- }
115
- const hasClientSubmitter = this.aptos.config.getTransactionSubmitter();
116
- const hasMethodSubmitter = submit?.transactionSubmitter !== void 0 && submit?.transactionSubmitter !== null;
117
- if (!hasClientSubmitter && !hasMethodSubmitter) {
118
- throw new MissingTransactionSubmitterError();
119
- }
120
- }
121
- /**
122
- * Retrieves the blob metadata from the blockchain. If it does not exist,
123
- * returns `undefined`.
124
- *
125
- * @param params.account - The account namespace the blob is stored in (e.g. "0x1")
126
- * @param params.name - The name of the blob (e.g. "foo/bar")
127
- * @returns The blob metadata.
128
- *
129
- * @example
130
- * ```typescript
131
- * const metadata = await client.getBlobMetadata({
132
- * account: AccountAddress.fromString("0x1"),
133
- * name: "foo/bar.txt",
134
- * });
135
- * ```
136
- */
137
- async getBlobMetadata(params) {
138
- try {
139
- const rawMetadata = await this.aptos.view({
140
- payload: {
141
- function: `${this.deployer.toString()}::blob_metadata::get_blob_metadata`,
142
- functionArguments: [
143
- createBlobKey({
144
- account: params.account,
145
- blobName: params.name
146
- })
147
- ]
148
- }
149
- });
150
- if (!rawMetadata?.[0]?.vec?.[0]) {
151
- return void 0;
152
- }
153
- const metadata = rawMetadata[0].vec[0];
154
- let encoding;
155
- if (metadata.encoding.__variant__ === "ClayCode_16Total_10Data_13Helper") {
156
- encoding = {
157
- variant: "clay",
158
- ...ERASURE_CODE_PARAMS[metadata.encoding.__variant__],
159
- ...ERASURE_CODE_AND_CHUNK_MAPPING[metadata.encoding.__variant__]
160
- };
161
- } else if (metadata.encoding.__variant__ === "ClayCode_4Total_2Data_3Helper") {
162
- encoding = {
163
- variant: "clay",
164
- ...ERASURE_CODE_PARAMS[metadata.encoding.__variant__],
165
- ...ERASURE_CODE_AND_CHUNK_MAPPING[metadata.encoding.__variant__]
166
- };
167
- } else {
168
- throw new Error(
169
- "Could not parse encoding from Shelby Smart Contract, this SDK is out of date."
170
- );
171
- }
172
- return {
173
- blobMerkleRoot: Hex.fromHexInput(
174
- metadata.blob_commitment
175
- ).toUint8Array(),
176
- owner: normalizeAddress(metadata.owner),
177
- name: params.name,
178
- blobNameSuffix: getBlobNameSuffix(params.name),
179
- size: Number(metadata.blob_size),
180
- encoding,
181
- expirationMicros: Number(metadata.expiration_micros),
182
- creationMicros: Number(metadata.creation_micros),
183
- sliceAddress: normalizeAddress(metadata.slice.inner),
184
- isWritten: metadata.is_written
185
- };
186
- } catch (error) {
187
- if (error instanceof Error && // Depending on the network, the error message may show up differently.
188
- (error.message?.includes("sub_status: Some(404)") || error.message?.includes("EBLOB_NOT_FOUND"))) {
189
- return void 0;
190
- }
191
- throw error;
192
- }
193
- }
194
- /**
195
- * Retrieves all the blobs and their metadata for an account from the
196
- * blockchain.
197
- *
198
- * @param params.account - The account namespace the blobs are stored in (e.g. "0x1")
199
- * @param params.pagination (optional) - The pagination options.
200
- * @param params.orderBy (optional) - The order by clause to sort the blobs by.
201
- * @returns The blob metadata for all the blobs for the account.
202
- *
203
- * @example
204
- * ```typescript
205
- * // BlobMetadata[]
206
- * const blobs = await client.getAccountBlobs({
207
- * account: AccountAddress.fromString("0x1"),
208
- * });
209
- * ```
210
- */
211
- getAccountBlobs(params) {
212
- const { where, ...rest } = params;
213
- return this.getBlobs({
214
- where: {
215
- ...where,
216
- owner: { _eq: normalizeAddress(params.account).toString() }
217
- },
218
- pagination: rest.pagination,
219
- orderBy: rest.orderBy
220
- });
221
- }
222
- /**
223
- * Retrieves blobs and their metadata from the blockchain.
224
- *
225
- * @param params.where (optional) - The where clause to filter the blobs by.
226
- * @param params.pagination (optional) - The pagination options.
227
- * @param params.orderBy (optional) - The order by clause to sort the blobs by.
228
- * @returns The blob metadata for all the blobs that match the where clause.
229
- *
230
- * @example
231
- * ```typescript
232
- * // BlobMetadata[]
233
- * const blobs = await client.getBlobs({
234
- * where: { owner: { _eq: AccountAddress.fromString("0x1").toString() } },
235
- * });
236
- * ```
237
- */
238
- async getBlobs(params = {}) {
239
- const { limit, offset } = params.pagination ?? {};
240
- const { orderBy, where } = params;
241
- const currentMicros = String(Date.now() * 1e3);
242
- const defaultActiveFilter = {
243
- expires_at: { _gte: currentMicros },
244
- is_deleted: { _eq: "0" }
245
- };
246
- const finalWhere = where !== void 0 ? { ...defaultActiveFilter, ...where } : defaultActiveFilter;
247
- const { blobs } = await this.indexer.getBlobs({
248
- where: finalWhere,
249
- limit,
250
- offset,
251
- orderBy
252
- });
253
- return blobs.map(
254
- (blob) => ({
255
- owner: normalizeAddress(blob.owner),
256
- name: blob.blob_name,
257
- blobNameSuffix: getBlobNameSuffix(blob.blob_name),
258
- blobMerkleRoot: Hex.fromHexInput(blob.blob_commitment).toUint8Array(),
259
- size: Number(blob.size),
260
- // TODO: Add encoding when supported in NCI
261
- encoding: {
262
- variant: "clay",
263
- ...ERASURE_CODE_PARAMS.ClayCode_16Total_10Data_13Helper,
264
- ...ERASURE_CODE_AND_CHUNK_MAPPING.ClayCode_16Total_10Data_13Helper
265
- },
266
- expirationMicros: Number(blob.expires_at),
267
- creationMicros: Number(blob.created_at),
268
- sliceAddress: normalizeAddress(blob.slice_address),
269
- isWritten: Boolean(Number(blob.is_written)),
270
- isDeleted: Boolean(Number(blob.is_deleted))
271
- })
272
- );
273
- }
274
- async getBlobActivities(params) {
275
- const { limit, offset } = params.pagination ?? {};
276
- const { orderBy, where } = params;
277
- const { blob_activities } = await this.indexer.getBlobActivities({
278
- where,
279
- limit,
280
- offset,
281
- orderBy
282
- });
283
- const activityTypeMapping = {
284
- [`${this.deployer.toStringLong()}::blob_metadata::BlobRegisteredEvent`]: "register_blob",
285
- [`${this.deployer.toStringLong()}::blob_metadata::BlobDeletedEvent`]: "delete_blob",
286
- [`${this.deployer.toStringLong()}::blob_metadata::BlobExpirationExtendedEvent`]: "extend_blob_expiration",
287
- [`${this.deployer.toStringLong()}::blob_metadata::BlobWrittenEvent`]: "write_blob"
288
- };
289
- return blob_activities.map(
290
- (activity) => ({
291
- blobName: activity.blob_name,
292
- accountAddress: normalizeAddress(activity.blob_name.substring(1, 65)),
293
- type: activityTypeMapping[activity.event_type] ?? "unknown",
294
- eventType: activity.event_type,
295
- eventIndex: Number(activity.event_index),
296
- transactionHash: activity.transaction_hash,
297
- // Using Number here in JS is technically not okay because txn version can be
298
- // as large as u64::MAX, but it should be fine for a long while.
299
- transactionVersion: Number(activity.transaction_version),
300
- timestamp: `${activity.timestamp}Z`
301
- })
302
- );
303
- }
304
- /**
305
- * Retrieves the total number of blobs from the blockchain.
306
- *
307
- * @param params.where (optional) - The where clause to filter the blobs by.
308
- * @returns The total number of blobs.
309
- *
310
- * @example
311
- * ```typescript
312
- * const count = await client.getBlobsCount();
313
- * ```
314
- */
315
- async getBlobsCount(params) {
316
- const { where } = params;
317
- const { blobs_aggregate } = await this.indexer.getBlobsCount({ where });
318
- return blobs_aggregate?.aggregate?.count ?? 0;
319
- }
320
- /**
321
- * Retrieves the total size of blobs from the blockchain.
322
- *
323
- * @param params.where (optional) - The where clause to filter the blobs by.
324
- * @returns The total size of blobs in bytes.
325
- *
326
- * @example
327
- * ```typescript
328
- * const size = await client.getTotalBlobsSize();
329
- * ```
330
- */
331
- async getTotalBlobsSize(params = {}) {
332
- const { where } = params;
333
- const { blobs_aggregate } = await this.indexer.getTotalBlobsSize({ where });
334
- return Number(blobs_aggregate?.aggregate?.sum?.size ?? 0);
335
- }
336
- /**
337
- * Retrieves the total number of blob activities from the blockchain.
338
- *
339
- * @param params.where (optional) - The where clause to filter the blob activities by.
340
- * @returns The total number of blob activities.
341
- *
342
- * @example
343
- * ```typescript
344
- * const count = await client.getBlobActivitiesCount();
345
- * ```
346
- */
347
- async getBlobActivitiesCount(params) {
348
- const { where } = params;
349
- const { blob_activities_aggregate } = await this.indexer.getBlobActivitiesCount({ where });
350
- return blob_activities_aggregate?.aggregate?.count ?? 0;
351
- }
352
- /**
353
- * Registers a blob on the blockchain by writing its merkle root and metadata.
354
- *
355
- * @param params.account - The account that is signing and paying for the transaction.
356
- * @param params.blobName - The name/path of the blob (e.g. "foo/bar.txt").
357
- * @param params.blobMerkleRoot - The merkle root of the blob commitments.
358
- * @param params.size - The size of the blob in bytes.
359
- * @param params.expirationMicros - The expiration time of the blob in microseconds.
360
- * @param params.options - Optional transaction building options.
361
- * @param params.options.chunksetSizeBytes - Custom chunkset size (defaults to DEFAULT_CHUNKSET_SIZE_BYTES).
362
- * @param params.options.build - Additional Aptos transaction building options.
363
- *
364
- * @returns An object containing the pending transaction.
365
- *
366
- * @example
367
- * ```typescript
368
- * const provider = await ClayErasureCodingProvider.create();
369
- * const blobCommitments = await generateCommitments(provider, data);
370
- *
371
- * const { transaction } = await client.registerBlob({
372
- * account: signer,
373
- * blobName: "foo/bar.txt",
374
- * blobMerkleRoot: blobCommitments.blob_merkle_root,
375
- * size: data.length,
376
- * expirationMicros: Date.now() * 1000 + 3600_000_000, // 1 hour from now in microseconds
377
- * });
378
- * ```
379
- */
380
- async registerBlob(params) {
381
- const options = this.mergeOptions(params.options);
382
- this.validateUsdSponsorConfig(options.usdSponsor, options.submit);
383
- const config = params.config ?? defaultErasureCodingConfig();
384
- const chunksetSize = config.chunkSizeBytes * config.erasure_k;
385
- const buildArgs = {
386
- ...options.build,
387
- data: _ShelbyBlobClient.createRegisterBlobPayload({
388
- deployer: this.deployer,
389
- account: params.account.accountAddress,
390
- blobName: params.blobName,
391
- blobSize: params.size,
392
- blobMerkleRoot: params.blobMerkleRoot,
393
- numChunksets: expectedTotalChunksets(params.size, chunksetSize),
394
- expirationMicros: params.expirationMicros,
395
- useSponsoredUsdVariant: options.usdSponsor !== void 0,
396
- encoding: config.enumIndex
397
- }),
398
- sender: params.account.accountAddress
399
- };
400
- const transaction = options.usdSponsor ? await this.aptos.transaction.build.multiAgent({
401
- ...buildArgs,
402
- secondarySignerAddresses: [options.usdSponsor.feePayerAddress]
403
- }) : await this.aptos.transaction.build.simple(buildArgs);
404
- return {
405
- transaction: await this.aptos.signAndSubmitTransaction({
406
- signer: params.account,
407
- transaction,
408
- ...options.submit
409
- })
410
- };
411
- }
412
- /**
413
- * Deletes a blob on the blockchain.
414
- *
415
- * @param params.account - The account that is signing and paying for the transaction.
416
- * @param params.blobName - The name/path of the blob (e.g. "foo/bar.txt").
417
- * @param params.options - Optional transaction building options.
418
- *
419
- * @returns An object containing the pending transaction.
420
- *
421
- * @example
422
- * ```typescript
423
- *
424
- * const { transaction } = await client.deleteBlob({
425
- * account: signer,
426
- * blobName: "foo/bar.txt",
427
- * });
428
- * ```
429
- */
430
- async deleteBlob(params) {
431
- const transaction = await this.aptos.transaction.build.simple({
432
- ...params.options,
433
- data: _ShelbyBlobClient.createDeleteBlobPayload({
434
- deployer: this.deployer,
435
- blobName: params.blobName
436
- }),
437
- sender: params.account.accountAddress
438
- });
439
- return {
440
- transaction: await this.aptos.signAndSubmitTransaction({
441
- signer: params.account,
442
- transaction
443
- })
444
- };
445
- }
446
- /**
447
- * Deletes multiple blobs on the blockchain in a single atomic transaction.
448
- *
449
- * **Note:** This function requires the `delete_multiple_blobs` entry function
450
- * which will be deployed to the smart contract on 2026-02-04. Using this
451
- * function before that date will result in a transaction failure.
452
- *
453
- * This operation is atomic: if any blob deletion fails (e.g., blob not found),
454
- * the entire transaction fails and no blobs are deleted.
455
- *
456
- * @param params.account - The account that is signing and paying for the transaction.
457
- * @param params.blobNames - Array of blob name suffixes without the account address prefix
458
- * (e.g. ["foo/bar.txt", "baz.txt"], NOT ["0x1/foo/bar.txt"]). The account address
459
- * prefix is automatically derived from the signer.
460
- * @param params.options - Optional transaction building options.
461
- *
462
- * @returns An object containing the pending transaction.
463
- *
464
- * @example
465
- * ```typescript
466
- *
467
- * const { transaction } = await client.deleteMultipleBlobs({
468
- * account: signer,
469
- * blobNames: ["foo/bar.txt", "baz.txt"],
470
- * });
471
- * ```
472
- */
473
- async deleteMultipleBlobs(params) {
474
- const transaction = await this.aptos.transaction.build.simple({
475
- ...params.options,
476
- data: _ShelbyBlobClient.createDeleteMultipleBlobsPayload({
477
- deployer: this.deployer,
478
- blobNames: params.blobNames
479
- }),
480
- sender: params.account.accountAddress
481
- });
482
- return {
483
- transaction: await this.aptos.signAndSubmitTransaction({
484
- signer: params.account,
485
- transaction
486
- })
487
- };
488
- }
489
- /**
490
- * Acks the blob chunksets on-chain. If each chunkset meets the necessary threshold, the entire blob will be marked as written.
491
- *
492
- * @param params.account - The account that is signing the transaction.
493
- * @param params.blobOwner - The account that owns the blob.
494
- * @param params.blobName - The name of the blob (e.g. "foo/bar")
495
- * @param params.creationMicros - The creation time of the blob in microseconds.
496
- * @param params.chunksetIdx - The index of the chunkset being acknowledged.
497
- * @param params.storageProviderChunksetAcks - The signatures
498
- * @param params.options - Additional options for transaction building and encoding.
499
- *
500
- * @returns The blob commitments and the pending transaction.
501
- *
502
- * @example
503
- * ```typescript
504
- * const { transaction } = await client.addChunksetAcknowledgements({
505
- * account: signer,
506
- * blobOwner: owner,
507
- * blobName: "foo/bar.txt",
508
- * creationMicros, // Taken from the blob metadata at registration time.
509
- * chunksetIdx,
510
- * storageProviderAcks: An array of StorageProviderAck types, each having the slot index and signature from the SP.
511
- * });
512
- * ```
513
- */
514
- async addChunksetAcknowledgements(params) {
515
- const transaction = await this.aptos.transaction.build.simple({
516
- ...params.options?.build,
517
- data: _ShelbyBlobClient.createChunksetAcknowledgementsPayload({
518
- blobOwner: params.blobOwner,
519
- blobName: params.blobName,
520
- creationMicros: params.creationMicros,
521
- chunksetIdx: params.chunksetIdx,
522
- storageProviderAcks: params.storageProviderAcks
523
- }),
524
- sender: params.account.accountAddress
525
- });
526
- return {
527
- transaction: await this.aptos.signAndSubmitTransaction({
528
- signer: params.account,
529
- transaction
530
- })
531
- };
532
- }
533
- /**
534
- * Registers multiple blobs on the blockchain by writing their merkle roots and metadata.
535
- *
536
- * @param params.account - The account that is signing and paying for the transaction.
537
- * @param params.expirationMicros - The expiration time of the blobs in microseconds.
538
- * @param params.blobs - The blobs to register.
539
- * @param params.blobs.blobName - The name/path of the blob (e.g. "foo/bar.txt").
540
- * @param params.blobs.blobSize - The size of the blob in bytes.
541
- * @param params.blobs.blobMerkleRoot - The merkle root of the blob commitments as a hex string.
542
- * @param params.options - Optional transaction building options.
543
- * @param params.options.chunksetSizeBytes - Custom chunkset size (defaults to DEFAULT_CHUNKSET_SIZE_BYTES).
544
- * @param params.options.build - Additional Aptos transaction building options.
545
- *
546
- * @returns An object containing the pending transaction.
547
- *
548
- * @example
549
- * ```typescript
550
- * const provider = await ClayErasureCodingProvider.create();
551
- * const blobCommitments = await generateCommitments(provider, data);
552
- *
553
- * const { transaction } = await client.batchRegisterBlobs({
554
- * account: signer,
555
- * expirationMicros: Date.now() * 1000 + 3600_000_000, // 1 hour from now in microseconds
556
- * blobs: [
557
- * {
558
- * blobName: "foo/bar.txt",
559
- * blobSize: data.length,
560
- * blobMerkleRoot: blobCommitments.blob_merkle_root,
561
- * },
562
- * ],
563
- * });
564
- * ```
565
- */
566
- async batchRegisterBlobs(params) {
567
- const options = this.mergeOptions(params.options);
568
- this.validateUsdSponsorConfig(options.usdSponsor, options.submit);
569
- const config = params.config ?? defaultErasureCodingConfig();
570
- const chunksetSize = config.chunkSizeBytes * config.erasure_k;
571
- const buildArgs = {
572
- ...options.build,
573
- sender: params.account.accountAddress,
574
- data: _ShelbyBlobClient.createBatchRegisterBlobsPayload({
575
- deployer: this.deployer,
576
- account: params.account.accountAddress,
577
- expirationMicros: params.expirationMicros,
578
- blobs: params.blobs.map((blob) => ({
579
- blobName: blob.blobName,
580
- blobSize: blob.blobSize,
581
- blobMerkleRoot: blob.blobMerkleRoot,
582
- numChunksets: expectedTotalChunksets(blob.blobSize, chunksetSize)
583
- })),
584
- useSponsoredUsdVariant: options.usdSponsor !== void 0,
585
- encoding: config.enumIndex
586
- })
587
- };
588
- const transaction = options.usdSponsor ? await this.aptos.transaction.build.multiAgent({
589
- ...buildArgs,
590
- secondarySignerAddresses: [options.usdSponsor.feePayerAddress]
591
- }) : await this.aptos.transaction.build.simple(buildArgs);
592
- return {
593
- transaction: await this.aptos.signAndSubmitTransaction({
594
- signer: params.account,
595
- transaction,
596
- ...options.submit
597
- })
598
- };
599
- }
600
- /**
601
- * Creates a transaction payload to register a blob on the blockchain.
602
- * This is a static helper method for constructing the Move function call payload.
603
- *
604
- * @param params.deployer - Optional deployer account address. Defaults to SHELBY_DEPLOYER.
605
- * @param params.account - The account that will own the blob.
606
- * @param params.blobName - The name/path of the blob (e.g. "foo/bar.txt").
607
- * @param params.blobSize - The size of the blob in bytes.
608
- * @param params.blobMerkleRoot - The merkle root of the blob commitments as a hex string.
609
- * @param params.expirationMicros - The expiration time of the blob in microseconds.
610
- * @param params.numChunksets - The total number of chunksets in the blob.
611
- *
612
- * @returns An Aptos transaction payload data object for the register_blob Move function.
613
- *
614
- * @see https://github.com/shelby/shelby/blob/e08e84742cf2b80ad8bb7227deb3013398076d53/move/shelby_contract/sources/global_metadata.move#L357
615
- */
616
- static createRegisterBlobPayload(params) {
617
- const functionName = params.useSponsoredUsdVariant ? "register_blob_with_sponsor" : "register_blob";
618
- return {
619
- function: `${(params.deployer ?? SHELBY_DEPLOYER).toString()}::blob_metadata::${functionName}`,
620
- functionArguments: [
621
- params.blobName,
622
- params.expirationMicros,
623
- Hex.fromHexString(params.blobMerkleRoot).toUint8Array(),
624
- params.numChunksets,
625
- params.blobSize,
626
- // TODO
627
- 0,
628
- // payment tier
629
- params.encoding
630
- ]
631
- };
632
- }
633
- /**
634
- * Creates a transaction payload to register multiple blobs on the blockchain.
635
- * This is a static helper method for constructing the Move function call payload.
636
- *
637
- * @param params.deployer - Optional deployer account address. Defaults to SHELBY_DEPLOYER.
638
- * @param params.account - The account that will own the blobs.
639
- * @param params.expirationMicros - The expiration time of the blobs in microseconds.
640
- * @param params.blobs - The blobs to register.
641
- * @param params.blobs.blobName - The name/path of the blob (e.g. "foo/bar.txt").
642
- * @param params.blobs.blobSize - The size of the blob in bytes.
643
- * @param params.blobs.blobMerkleRoot - The merkle root of the blob commitments as a hex string.
644
- * @param params.blobs.numChunksets - The total number of chunksets in the blob.
645
- *
646
- * @returns An Aptos transaction payload data object for the register_multiple_blobs Move function.
647
- *
648
- * @see https://github.com/shelby/shelby/blob/e08e84742cf2b80ad8bb7227deb3013398076d53/move/shelby_contract/sources/global_metadata.move#L357
649
- */
650
- static createBatchRegisterBlobsPayload(params) {
651
- const functionName = params.useSponsoredUsdVariant ? "register_multiple_blobs_with_sponsor" : "register_multiple_blobs";
652
- const blobNames = [];
653
- const blobMerkleRoots = [];
654
- const blobNumChunksets = [];
655
- const blobSizes = [];
656
- params.blobs.forEach((blob) => {
657
- blobNames.push(blob.blobName);
658
- blobMerkleRoots.push(
659
- Hex.fromHexString(blob.blobMerkleRoot).toUint8Array()
660
- );
661
- blobNumChunksets.push(blob.numChunksets);
662
- blobSizes.push(blob.blobSize);
663
- });
664
- return {
665
- function: `${(params.deployer ?? SHELBY_DEPLOYER).toString()}::blob_metadata::${functionName}`,
666
- functionArguments: [
667
- blobNames,
668
- params.expirationMicros,
669
- blobMerkleRoots,
670
- blobNumChunksets,
671
- blobSizes,
672
- // TODO
673
- 0,
674
- // payment tier
675
- params.encoding
676
- ]
677
- };
678
- }
679
- /**
680
- * Creates a transaction payload to delete a blob on the blockchain.
681
- * This is a static helper method for constructing the Move function call payload.
682
- *
683
- * @param params.deployer - Optional deployer account address. Defaults to SHELBY_DEPLOYER.
684
- * @param params.blobName - The blob name (e.g. "bar.txt", without the account address prefix).
685
- *
686
- * @returns An Aptos transaction payload data object for the delete_blob Move function.
687
- *
688
- * @see https://github.com/shelby/shelby/blob/64e9d7b4f0005e586faeb1e4085c79159234b6b6/move/shelby_contract/sources/global_metadata.move#L616
689
- */
690
- static createDeleteBlobPayload(params) {
691
- return {
692
- function: `${(params.deployer ?? SHELBY_DEPLOYER).toString()}::blob_metadata::delete_blob`,
693
- functionArguments: [params.blobName]
694
- };
695
- }
696
- /**
697
- * Creates a transaction payload to delete multiple blobs on the blockchain.
698
- * This is a static helper method for constructing the Move function call payload.
699
- *
700
- * **Note:** This function requires the `delete_multiple_blobs` entry function
701
- * which will be deployed to the smart contract on 2026-02-04. Using this
702
- * function before that date will result in a transaction failure.
703
- *
704
- * This operation is atomic: if any blob deletion fails (e.g., blob not found),
705
- * the entire transaction fails and no blobs are deleted.
706
- *
707
- * @param params.deployer - Optional deployer account address. Defaults to SHELBY_DEPLOYER.
708
- * @param params.blobNames - Array of blob name suffixes without the account address prefix
709
- * (e.g. ["foo/bar.txt", "baz.txt"], NOT ["0x1/foo/bar.txt"]). The account address
710
- * prefix is automatically derived from the transaction sender.
711
- *
712
- * @returns An Aptos transaction payload data object for the delete_multiple_blobs Move function.
713
- *
714
- * @see https://github.com/shelby/shelby/blob/main/move/shelby_contract/sources/blob_metadata.move
715
- */
716
- static createDeleteMultipleBlobsPayload(params) {
717
- return {
718
- function: `${(params.deployer ?? SHELBY_DEPLOYER).toString()}::blob_metadata::delete_multiple_blobs`,
719
- functionArguments: [params.blobNames]
720
- };
721
- }
722
- static createChunksetAcknowledgementsPayload(params) {
723
- const ackBitMask = params.storageProviderAcks.reduce(
724
- (acc, ack) => acc | 1 << ack.slot,
725
- 0
726
- );
727
- return {
728
- function: `${SHELBY_DEPLOYER}::blob_metadata::add_chunkset_acknowledgements`,
729
- functionArguments: [
730
- createBlobKey({
731
- account: params.blobOwner,
732
- blobName: params.blobName
733
- }),
734
- params.chunksetIdx,
735
- params.creationMicros,
736
- new U32(Number(ackBitMask)),
737
- params.storageProviderAcks.map((ack) => ack.signature)
738
- ]
739
- };
740
- }
741
- static createBlobAcknowledgementsPayload(params) {
742
- const ackBitMask = params.storageProviderAcks.reduce(
743
- (acc, ack) => acc | 1 << ack.slot,
744
- 0
745
- );
746
- return {
747
- function: `${SHELBY_DEPLOYER}::blob_metadata::add_blob_acknowledgements`,
748
- functionArguments: [
749
- createBlobKey({
750
- account: params.blobOwner,
751
- blobName: params.blobName
752
- }),
753
- params.creationMicros,
754
- new U32(Number(ackBitMask)),
755
- params.storageProviderAcks.map((ack) => ack.signature)
756
- ]
757
- };
758
- }
759
- async addBlobAcknowledgements(params) {
760
- const transaction = await this.aptos.transaction.build.simple({
761
- ...params.options?.build,
762
- data: _ShelbyBlobClient.createBlobAcknowledgementsPayload({
763
- blobOwner: params.blobOwner,
764
- blobName: params.blobName,
765
- creationMicros: params.creationMicros,
766
- storageProviderAcks: params.storageProviderAcks
767
- }),
768
- sender: params.account.accountAddress
769
- });
770
- return {
771
- transaction: await this.aptos.signAndSubmitTransaction({
772
- signer: params.account,
773
- transaction
774
- })
775
- };
776
- }
777
- };
778
-
779
- export {
780
- MissingTransactionSubmitterError,
781
- ShelbyBlobClient
782
- };