@ardrive/turbo-sdk 1.42.0-alpha.9 → 1.43.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 (65) hide show
  1. package/lib/cjs/cli/cli.js +31 -0
  2. package/lib/cjs/cli/commands/arns.js +126 -3
  3. package/lib/cjs/cli/options.js +78 -1
  4. package/lib/cjs/common/arnsActions.js +23 -0
  5. package/lib/cjs/common/chunked.js +51 -1
  6. package/lib/cjs/common/folderIndex.js +394 -0
  7. package/lib/cjs/common/http.js +12 -1
  8. package/lib/cjs/common/index.js +4 -0
  9. package/lib/cjs/common/payment.js +73 -4
  10. package/lib/cjs/common/turbo.js +36 -5
  11. package/lib/cjs/common/upload.js +311 -29
  12. package/lib/cjs/node/folderIndex.js +132 -0
  13. package/lib/cjs/node/index.js +1 -0
  14. package/lib/cjs/node/upload.js +7 -0
  15. package/lib/cjs/types.js +13 -4
  16. package/lib/cjs/utils/folderIndex.js +88 -0
  17. package/lib/esm/cli/cli.js +33 -2
  18. package/lib/esm/cli/commands/arns.js +119 -3
  19. package/lib/esm/cli/options.js +77 -0
  20. package/lib/esm/common/arnsActions.js +21 -0
  21. package/lib/esm/common/chunked.js +51 -1
  22. package/lib/esm/common/folderIndex.js +389 -0
  23. package/lib/esm/common/http.js +12 -1
  24. package/lib/esm/common/index.js +4 -0
  25. package/lib/esm/common/payment.js +74 -5
  26. package/lib/esm/common/turbo.js +36 -5
  27. package/lib/esm/common/upload.js +312 -30
  28. package/lib/esm/node/folderIndex.js +129 -0
  29. package/lib/esm/node/index.js +1 -0
  30. package/lib/esm/node/upload.js +7 -0
  31. package/lib/esm/types.js +13 -4
  32. package/lib/esm/utils/folderIndex.js +81 -0
  33. package/lib/types/cli/commands/arns.d.ts +49 -2
  34. package/lib/types/cli/commands/arns.d.ts.map +1 -1
  35. package/lib/types/cli/options.d.ts +172 -0
  36. package/lib/types/cli/options.d.ts.map +1 -1
  37. package/lib/types/cli/types.d.ts +30 -0
  38. package/lib/types/cli/types.d.ts.map +1 -1
  39. package/lib/types/common/arnsActions.d.ts +16 -1
  40. package/lib/types/common/arnsActions.d.ts.map +1 -1
  41. package/lib/types/common/chunked.d.ts +47 -2
  42. package/lib/types/common/chunked.d.ts.map +1 -1
  43. package/lib/types/common/folderIndex.d.ts +54 -0
  44. package/lib/types/common/folderIndex.d.ts.map +1 -0
  45. package/lib/types/common/http.d.ts +11 -1
  46. package/lib/types/common/http.d.ts.map +1 -1
  47. package/lib/types/common/index.d.ts +2 -0
  48. package/lib/types/common/index.d.ts.map +1 -1
  49. package/lib/types/common/payment.d.ts +67 -5
  50. package/lib/types/common/payment.d.ts.map +1 -1
  51. package/lib/types/common/turbo.d.ts +49 -6
  52. package/lib/types/common/turbo.d.ts.map +1 -1
  53. package/lib/types/common/upload.d.ts +41 -0
  54. package/lib/types/common/upload.d.ts.map +1 -1
  55. package/lib/types/node/folderIndex.d.ts +28 -0
  56. package/lib/types/node/folderIndex.d.ts.map +1 -0
  57. package/lib/types/node/index.d.ts +1 -0
  58. package/lib/types/node/index.d.ts.map +1 -1
  59. package/lib/types/node/upload.d.ts +1 -0
  60. package/lib/types/node/upload.d.ts.map +1 -1
  61. package/lib/types/types.d.ts +216 -5
  62. package/lib/types/types.d.ts.map +1 -1
  63. package/lib/types/utils/folderIndex.d.ts +47 -0
  64. package/lib/types/utils/folderIndex.d.ts.map +1 -0
  65. package/package.json +1 -1
@@ -147,6 +147,37 @@ const utils_js_1 = require("./utils.js");
147
147
  .description('Remove a resolution record (undername) from a Turbo-custodied ANT'), options_js_1.removeArNSRecordOptions).action(async (_commandOptions, command) => {
148
148
  await (0, utils_js_1.runCommand)(command, arns_js_1.removeArNSRecord);
149
149
  });
150
+ (0, utils_js_1.applyOptions)(commander_1.program
151
+ .command('add-arns-controller')
152
+ .description('Grant controller rights on a Turbo-custodied ANT'), options_js_1.addArNSControllerOptions).action(async (_commandOptions, command) => {
153
+ await (0, utils_js_1.runCommand)(command, arns_js_1.addArNSController);
154
+ });
155
+ (0, utils_js_1.applyOptions)(commander_1.program
156
+ .command('remove-arns-controller')
157
+ .description('Revoke controller rights on a Turbo-custodied ANT'), options_js_1.removeArNSControllerOptions).action(async (_commandOptions, command) => {
158
+ await (0, utils_js_1.runCommand)(command, arns_js_1.removeArNSController);
159
+ });
160
+ (0, utils_js_1.applyOptions)(commander_1.program
161
+ .command('transfer-arns-record')
162
+ .description('Transfer one record (undername) on a Turbo-custodied ANT to another address'), options_js_1.transferArNSRecordOptions).action(async (_commandOptions, command) => {
163
+ await (0, utils_js_1.runCommand)(command, arns_js_1.transferArNSRecord);
164
+ });
165
+ (0, utils_js_1.applyOptions)(commander_1.program
166
+ .command('set-arns-record-metadata')
167
+ .description("Set a record's display name, logo, description, or keywords on a Turbo-custodied ANT"), options_js_1.setArNSRecordMetadataOptions).action(async (_commandOptions, command) => {
168
+ await (0, utils_js_1.runCommand)(command, arns_js_1.setArNSRecordMetadata);
169
+ });
170
+ (0, utils_js_1.applyOptions)(commander_1.program
171
+ .command('remove-arns-record-metadata')
172
+ .description("Clear a record's metadata on a Turbo-custodied ANT"), options_js_1.removeArNSRecordMetadataOptions).action(async (_commandOptions, command) => {
173
+ await (0, utils_js_1.runCommand)(command, arns_js_1.removeArNSRecordMetadata);
174
+ });
175
+ (0, utils_js_1.applyOptions)(commander_1.program
176
+ .command('arns-action-price')
177
+ .description('Preview the Turbo Credit price of a non-purchase ArNS action (set-record, remove-record, ' +
178
+ 'set-record-metadata, remove-record-metadata, transfer-record, add-controller, remove-controller, transfer)'), options_js_1.arnsActionPriceOptions).action(async (_commandOptions, command) => {
179
+ await (0, utils_js_1.runCommand)(command, arns_js_1.arnsActionPrice);
180
+ });
150
181
  (0, utils_js_1.applyOptions)(commander_1.program
151
182
  .command('inspect-data-items')
152
183
  .description('Lists all given or received Turbo credit share approvals for specified address or connected wallet'), [options_js_1.optionMap.folderPath]).action(async (_commandOptions, command) => {
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.requiredNameFromOptions = requiredNameFromOptions;
4
4
  exports.positiveIntFromOption = positiveIntFromOption;
5
5
  exports.typeFromOptions = typeFromOptions;
6
+ exports.actionFromOptions = actionFromOptions;
6
7
  exports.paidByFromArNSOptions = paidByFromArNSOptions;
7
8
  exports.arnsPriceParamsFromOptions = arnsPriceParamsFromOptions;
8
9
  exports.arnsPrice = arnsPrice;
@@ -15,6 +16,12 @@ exports.arnsPurchaseStatus = arnsPurchaseStatus;
15
16
  exports.transferArNSAnt = transferArNSAnt;
16
17
  exports.setArNSRecord = setArNSRecord;
17
18
  exports.removeArNSRecord = removeArNSRecord;
19
+ exports.addArNSController = addArNSController;
20
+ exports.removeArNSController = removeArNSController;
21
+ exports.transferArNSRecord = transferArNSRecord;
22
+ exports.setArNSRecordMetadata = setArNSRecordMetadata;
23
+ exports.removeArNSRecordMetadata = removeArNSRecordMetadata;
24
+ exports.arnsActionPrice = arnsActionPrice;
18
25
  /**
19
26
  * Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
20
27
  *
@@ -33,6 +40,7 @@ exports.removeArNSRecord = removeArNSRecord;
33
40
  const bignumber_js_1 = require("bignumber.js");
34
41
  const arnsActions_js_1 = require("../../common/arnsActions.js");
35
42
  const factory_js_1 = require("../../node/factory.js");
43
+ const types_js_1 = require("../../types.js");
36
44
  const errors_js_1 = require("../../utils/errors.js");
37
45
  const constants_js_1 = require("../constants.js");
38
46
  const utils_js_1 = require("../utils.js");
@@ -51,6 +59,13 @@ function ownerFromOptions(options) {
51
59
  }
52
60
  return (0, arnsActions_js_1.solanaOwnerSigner)(options.ownerKey);
53
61
  }
62
+ /** The four ARIO-purchase actions are priced via `arnsPrice`, not this route. */
63
+ const ARNS_PURCHASE_ACTIONS = new Set([
64
+ 'buy-name',
65
+ 'extend-lease',
66
+ 'upgrade-name',
67
+ 'increase-undername-limit',
68
+ ]);
54
69
  function requiredNameFromOptions(options) {
55
70
  if (options.name === undefined || options.name.length === 0) {
56
71
  throw new Error('Must provide an ArNS --name');
@@ -73,6 +88,25 @@ function typeFromOptions(value) {
73
88
  }
74
89
  return value;
75
90
  }
91
+ /** Parses `--action` for `arns-action-price`, rejecting the four purchase actions. */
92
+ function actionFromOptions(options) {
93
+ const { action } = options;
94
+ if (action === undefined || action.length === 0) {
95
+ throw new Error(`Must provide --action. One of: ${types_js_1.arNSActions.join(', ')}`);
96
+ }
97
+ if (!types_js_1.arNSActions.includes(action)) {
98
+ throw new Error(`Invalid --action '${action}'. One of: ${types_js_1.arNSActions.join(', ')}`);
99
+ }
100
+ if (ARNS_PURCHASE_ACTIONS.has(action)) {
101
+ throw new Error(`'${action}' is priced via \`turbo arns-price\`, not \`arns-action-price\` — ` +
102
+ 'it spends ARIO, not just the flat credits margin this route quotes.');
103
+ }
104
+ return action;
105
+ }
106
+ /** `null` clears the field (Set-Record-Metadata's tri-state), `undefined` leaves it unchanged. */
107
+ function metadataFieldFromOptions(value, clear) {
108
+ return clear ? null : value;
109
+ }
76
110
  function paidByFromArNSOptions(paidBy) {
77
111
  return paidBy !== undefined && paidBy.length > 0 ? paidBy : undefined;
78
112
  }
@@ -156,12 +190,17 @@ function logPurchaseResult(action, result) {
156
190
  async function arnsPrice(options, turbo) {
157
191
  const params = arnsPriceParamsFromOptions(options);
158
192
  const client = turbo ?? factory_js_1.TurboFactory.unauthenticated((0, utils_js_1.configFromOptions)(options));
159
- const { winc, mARIO } = await client.getArNSPriceForName(params);
193
+ const { winc, wincTotal, antSpawnSurchargeWinc, mARIO } = await client.getArNSPriceForName(params);
194
+ // `wincTotal` is the figure to pay. `winc` is the name only and excludes the
195
+ // ANT spawn surcharge, which for a Buy-Name can exceed the name's own price,
196
+ // so printing it as the price under-quotes what `buy-arns-name` then debits.
160
197
  console.log(JSON.stringify({
161
198
  name: params.name,
162
199
  intent: params.intent,
163
- winc,
164
- credits: creditsFromWinc(winc),
200
+ wincTotal,
201
+ credits: creditsFromWinc(wincTotal),
202
+ nameOnlyWinc: winc,
203
+ antSpawnSurchargeWinc,
165
204
  mARIO,
166
205
  }, null, 2));
167
206
  }
@@ -315,3 +354,87 @@ async function removeArNSRecord(options, turbo) {
315
354
  });
316
355
  console.log(JSON.stringify({ message: 'ArNS record removed!', ...result }, null, 2));
317
356
  }
357
+ async function addArNSController(options, turbo) {
358
+ if (options.antId === undefined) {
359
+ throw new Error('Must provide an --ant-id to add a controller to');
360
+ }
361
+ const client = turbo ?? (await (0, utils_js_1.turboFromOptions)(options));
362
+ const result = await client.addArNSController({
363
+ antId: options.antId,
364
+ owner: ownerFromOptions(options),
365
+ target: options.target,
366
+ });
367
+ console.log(JSON.stringify({ message: 'ArNS controller added!', ...result }, null, 2));
368
+ }
369
+ async function removeArNSController(options, turbo) {
370
+ if (options.antId === undefined) {
371
+ throw new Error('Must provide an --ant-id to remove a controller from');
372
+ }
373
+ const client = turbo ?? (await (0, utils_js_1.turboFromOptions)(options));
374
+ const result = await client.removeArNSController({
375
+ antId: options.antId,
376
+ owner: ownerFromOptions(options),
377
+ target: options.target,
378
+ });
379
+ console.log(JSON.stringify({ message: 'ArNS controller removed!', ...result }, null, 2));
380
+ }
381
+ async function transferArNSRecord(options, turbo) {
382
+ if (options.antId === undefined) {
383
+ throw new Error('Must provide an --ant-id whose record to transfer');
384
+ }
385
+ if (options.undername === undefined || options.undername.length === 0) {
386
+ throw new Error('Must provide an --undername to transfer');
387
+ }
388
+ if (options.target === undefined) {
389
+ throw new Error('Must provide a --target address to transfer the record to');
390
+ }
391
+ const client = turbo ?? (await (0, utils_js_1.turboFromOptions)(options));
392
+ const result = await client.transferArNSRecord({
393
+ antId: options.antId,
394
+ owner: ownerFromOptions(options),
395
+ undername: options.undername,
396
+ target: options.target,
397
+ });
398
+ console.log(JSON.stringify({ message: 'ArNS record transferred!', ...result }, null, 2));
399
+ }
400
+ async function setArNSRecordMetadata(options, turbo) {
401
+ if (options.antId === undefined) {
402
+ throw new Error('Must provide an --ant-id to set record metadata on');
403
+ }
404
+ const client = turbo ?? (await (0, utils_js_1.turboFromOptions)(options));
405
+ const result = await client.setArNSRecordMetadata({
406
+ antId: options.antId,
407
+ owner: ownerFromOptions(options),
408
+ undername: options.undername ?? '@',
409
+ displayName: metadataFieldFromOptions(options.displayName, options.clearDisplayName),
410
+ recordLogo: metadataFieldFromOptions(options.recordLogo, options.clearRecordLogo),
411
+ recordDescription: metadataFieldFromOptions(options.recordDescription, options.clearRecordDescription),
412
+ recordKeywords: metadataFieldFromOptions(options.recordKeywords, options.clearRecordKeywords),
413
+ });
414
+ console.log(JSON.stringify({ message: 'ArNS record metadata set!', ...result }, null, 2));
415
+ }
416
+ async function removeArNSRecordMetadata(options, turbo) {
417
+ if (options.antId === undefined) {
418
+ throw new Error('Must provide an --ant-id to remove record metadata from');
419
+ }
420
+ if (options.undername === undefined || options.undername.length === 0) {
421
+ throw new Error('Must provide an --undername');
422
+ }
423
+ const client = turbo ?? (await (0, utils_js_1.turboFromOptions)(options));
424
+ const result = await client.removeArNSRecordMetadata({
425
+ antId: options.antId,
426
+ owner: ownerFromOptions(options),
427
+ undername: options.undername,
428
+ });
429
+ console.log(JSON.stringify({ message: 'ArNS record metadata removed!', ...result }, null, 2));
430
+ }
431
+ /**
432
+ * Preview what one of the eight non-purchase actions will debit, without
433
+ * creating it.
434
+ */
435
+ async function arnsActionPrice(options, turbo) {
436
+ const action = actionFromOptions(options);
437
+ const client = turbo ?? factory_js_1.TurboFactory.unauthenticated((0, utils_js_1.configFromOptions)(options));
438
+ const { wincQty } = await client.getArNSActionPrice(action);
439
+ console.log(JSON.stringify({ action, wincQty, credits: creditsFromWinc(wincQty) }, null, 2));
440
+ }
@@ -15,7 +15,7 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.removeArNSRecordOptions = exports.setArNSRecordOptions = exports.transferArNSAntOptions = exports.arnsPurchaseStatusOptions = exports.upgradeArNSNameOptions = exports.increaseArNSUndernamesOptions = exports.extendArNSLeaseOptions = exports.buyArNSNameOptions = exports.arnsFiatQuoteOptions = exports.arnsPriceOptions = exports.listSharesOptions = exports.revokeCreditsOptions = exports.shareCreditsOptions = exports.uploadFileOptions = exports.uploadFolderOptions = exports.uploadOptions = exports.globalOptions = exports.walletOptions = exports.optionMap = void 0;
18
+ exports.arnsActionPriceOptions = exports.removeArNSRecordMetadataOptions = exports.setArNSRecordMetadataOptions = exports.transferArNSRecordOptions = exports.removeArNSControllerOptions = exports.addArNSControllerOptions = exports.removeArNSRecordOptions = exports.setArNSRecordOptions = exports.transferArNSAntOptions = exports.arnsPurchaseStatusOptions = exports.upgradeArNSNameOptions = exports.increaseArNSUndernamesOptions = exports.extendArNSLeaseOptions = exports.buyArNSNameOptions = exports.arnsFiatQuoteOptions = exports.arnsPriceOptions = exports.listSharesOptions = exports.revokeCreditsOptions = exports.shareCreditsOptions = exports.uploadFileOptions = exports.uploadFolderOptions = exports.uploadOptions = exports.globalOptions = exports.walletOptions = exports.optionMap = void 0;
19
19
  exports.optionMap = {
20
20
  token: {
21
21
  alias: '-t, --token <type>',
@@ -234,6 +234,48 @@ exports.optionMap = {
234
234
  alias: '--ttl-seconds <ttlSeconds>',
235
235
  description: 'TTL in seconds for the ArNS record',
236
236
  },
237
+ arnsDisplayName: {
238
+ alias: '--display-name <displayName>',
239
+ description: 'Display name for the record (Set-Record-Metadata)',
240
+ },
241
+ arnsClearDisplayName: {
242
+ alias: '--clear-display-name',
243
+ description: 'Clear the record display name (Set-Record-Metadata)',
244
+ default: false,
245
+ },
246
+ arnsRecordLogo: {
247
+ alias: '--record-logo <transactionId>',
248
+ description: 'Arweave transaction ID for the record logo (Set-Record-Metadata)',
249
+ },
250
+ arnsClearRecordLogo: {
251
+ alias: '--clear-record-logo',
252
+ description: 'Clear the record logo (Set-Record-Metadata)',
253
+ default: false,
254
+ },
255
+ arnsRecordDescription: {
256
+ alias: '--record-description <description>',
257
+ description: 'Description for the record (Set-Record-Metadata)',
258
+ },
259
+ arnsClearRecordDescription: {
260
+ alias: '--clear-record-description',
261
+ description: 'Clear the record description (Set-Record-Metadata)',
262
+ default: false,
263
+ },
264
+ arnsRecordKeywords: {
265
+ alias: '--record-keywords <keywords...>',
266
+ description: 'Keywords for the record (Set-Record-Metadata)',
267
+ type: 'array',
268
+ },
269
+ arnsClearRecordKeywords: {
270
+ alias: '--clear-record-keywords',
271
+ description: 'Clear the record keywords (Set-Record-Metadata)',
272
+ default: false,
273
+ },
274
+ arnsAction: {
275
+ alias: '--action <action>',
276
+ description: 'ArNS action to price: set-record, remove-record, set-record-metadata, ' +
277
+ 'remove-record-metadata, transfer-record, add-controller, remove-controller, or transfer',
278
+ },
237
279
  // ---- Payment history ----
238
280
  limit: {
239
281
  alias: '--limit <limit>',
@@ -365,3 +407,38 @@ exports.removeArNSRecordOptions = [
365
407
  exports.optionMap.arnsAntId,
366
408
  exports.optionMap.arnsUndername,
367
409
  ];
410
+ exports.addArNSControllerOptions = [
411
+ ...exports.walletOptions,
412
+ exports.optionMap.arnsOwnerKey,
413
+ exports.optionMap.arnsAntId,
414
+ exports.optionMap.arnsTarget,
415
+ ];
416
+ exports.removeArNSControllerOptions = exports.addArNSControllerOptions;
417
+ exports.transferArNSRecordOptions = [
418
+ ...exports.walletOptions,
419
+ exports.optionMap.arnsOwnerKey,
420
+ exports.optionMap.arnsAntId,
421
+ exports.optionMap.arnsUndername,
422
+ exports.optionMap.arnsTarget,
423
+ ];
424
+ exports.setArNSRecordMetadataOptions = [
425
+ ...exports.walletOptions,
426
+ exports.optionMap.arnsOwnerKey,
427
+ exports.optionMap.arnsAntId,
428
+ exports.optionMap.arnsUndername,
429
+ exports.optionMap.arnsDisplayName,
430
+ exports.optionMap.arnsClearDisplayName,
431
+ exports.optionMap.arnsRecordLogo,
432
+ exports.optionMap.arnsClearRecordLogo,
433
+ exports.optionMap.arnsRecordDescription,
434
+ exports.optionMap.arnsClearRecordDescription,
435
+ exports.optionMap.arnsRecordKeywords,
436
+ exports.optionMap.arnsClearRecordKeywords,
437
+ ];
438
+ exports.removeArNSRecordMetadataOptions = [
439
+ ...exports.walletOptions,
440
+ exports.optionMap.arnsOwnerKey,
441
+ exports.optionMap.arnsAntId,
442
+ exports.optionMap.arnsUndername,
443
+ ];
444
+ exports.arnsActionPriceOptions = [exports.optionMap.arnsAction];
@@ -4,6 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.buildArNSCustodyMessage = buildArNSCustodyMessage;
7
+ exports.arNSMetadataField = arNSMetadataField;
8
+ exports.arNSKeywordsField = arNSKeywordsField;
7
9
  exports.arNSOwnerProofHeaders = arNSOwnerProofHeaders;
8
10
  exports.solanaOwnerSigner = solanaOwnerSigner;
9
11
  exports.emptySignatureSlots = emptySignatureSlots;
@@ -39,6 +41,27 @@ const base64_js_1 = require("../utils/base64.js");
39
41
  function buildArNSCustodyMessage(action, fields) {
40
42
  return ['arns', action, ...fields].join('\n');
41
43
  }
44
+ /**
45
+ * A metadata field's ABSENT/EMPTY distinction, as the bundler binds it.
46
+ *
47
+ * `null` (clear the field) must not sign the same message as `''` (set it to
48
+ * empty), or a signature authorizing one would authorize the other. NUL can
49
+ * never appear in a value that reached here — the route rejects control
50
+ * characters — so it is a safe sentinel.
51
+ */
52
+ function arNSMetadataField(value) {
53
+ return value === null || value === undefined ? '\u0000' : value;
54
+ }
55
+ /**
56
+ * Keywords joined on a separator the route rejects INSIDE a keyword, so
57
+ * `['a,b']` and `['a','b']` cannot be re-partitioned into one another with the
58
+ * same signature.
59
+ */
60
+ function arNSKeywordsField(keywords) {
61
+ return keywords === null || keywords === undefined
62
+ ? '\u0000'
63
+ : keywords.join('\u0001');
64
+ }
42
65
  /** Solana's signature-type discriminator in Turbo's signed-request scheme. */
43
66
  const SOLANA_SIGNATURE_TYPE = 4;
44
67
  /**
@@ -41,7 +41,7 @@ const chunkingHeader = { 'x-chunking-version': '2' };
41
41
  * uploading them in parallel, and emitting progress/error events.
42
42
  */
43
43
  class ChunkedUploader {
44
- constructor({ http, token, maxChunkConcurrency = exports.defaultMaxChunkConcurrency, maxFinalizeMs, chunkByteCount = exports.defaultChunkByteCount, logger = logger_js_1.Logger.default, chunkingMode = 'auto', dataItemByteCount, }) {
44
+ constructor({ http, token, maxChunkConcurrency = exports.defaultMaxChunkConcurrency, maxFinalizeMs, chunkByteCount = exports.defaultChunkByteCount, logger = logger_js_1.Logger.default, chunkingMode = 'auto', dataItemByteCount, x402, x402RefundIdentity, }) {
45
45
  this.assertChunkParams({
46
46
  chunkByteCount,
47
47
  chunkingMode,
@@ -60,6 +60,9 @@ class ChunkedUploader {
60
60
  dataItemByteCount,
61
61
  });
62
62
  this.maxBacklogQueue = this.maxChunkConcurrency * backlogQueueFactor;
63
+ this.x402 = x402;
64
+ this.x402RefundIdentity = x402RefundIdentity;
65
+ this.dataItemByteCount = dataItemByteCount;
63
66
  }
64
67
  shouldChunkUpload({ chunkByteCount, chunkingMode, dataItemByteCount, }) {
65
68
  if (chunkingMode === 'disabled') {
@@ -98,7 +101,54 @@ class ChunkedUploader {
98
101
  /**
99
102
  * Initialize or resume an upload session, returning the upload ID.
100
103
  */
104
+ /**
105
+ * Open a multipart upload paid for with x402.
106
+ *
107
+ * The bundler settles the payment at CREATE, before accepting a single chunk
108
+ * — so an unpaid upload never consumes storage. That is why the size is
109
+ * declared here and the money moves here, not at finalize.
110
+ *
111
+ * The 402 handshake is delegated to `wrapFetchWithPayment`, the same
112
+ * mechanism the single-shot x402 POST already uses, so the signer and the
113
+ * spend cap behave identically on both paths.
114
+ */
115
+ async initPaidUpload(x402) {
116
+ if (this.paidUploadId !== undefined) {
117
+ this.logger.debug('Resuming the already-paid chunked upload', {
118
+ uploadId: this.paidUploadId,
119
+ });
120
+ return this.paidUploadId;
121
+ }
122
+ if (this.x402RefundIdentity === undefined) {
123
+ throw new Error('An x402-paid chunked upload needs a refund identity — it is the Turbo wallet credited if the upload delivers fewer bytes than were paid for.');
124
+ }
125
+ const { address, signatureType } = this.x402RefundIdentity;
126
+ const endpoint = `/chunks/${this.token}/-1/-1?chunkSize=${this.chunkByteCount}` +
127
+ `&totalBytes=${this.dataItemByteCount}` +
128
+ `&address=${encodeURIComponent(address)}` +
129
+ `&signatureType=${signatureType}`;
130
+ this.logger.debug('Opening an x402-paid chunked upload', {
131
+ totalBytes: this.dataItemByteCount,
132
+ });
133
+ const res = await this.http.get({
134
+ endpoint: endpoint,
135
+ headers: chunkingHeader,
136
+ x402Options: x402,
137
+ });
138
+ if (res.chunkSize !== undefined && res.chunkSize !== this.chunkByteCount) {
139
+ this.logger.warn('Chunk size mismatch! Overriding with server value.', {
140
+ clientExpected: this.chunkByteCount,
141
+ serverReturned: res.chunkSize,
142
+ });
143
+ this.chunkByteCount = res.chunkSize;
144
+ }
145
+ this.paidUploadId = res.id;
146
+ return res.id;
147
+ }
101
148
  async initUpload() {
149
+ if (this.x402 !== undefined) {
150
+ return this.initPaidUpload(this.x402);
151
+ }
102
152
  const res = await this.http.get({
103
153
  endpoint: `/chunks/${this.token}/-1/-1?chunkSize=${this.chunkByteCount}`,
104
154
  headers: chunkingHeader,