@ar.io/sdk 3.12.2 → 3.13.0-beta.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 (68) hide show
  1. package/README.md +28 -1
  2. package/bundles/web.bundle.min.js +142 -147
  3. package/lib/cjs/cli/commands/arnsPurchaseCommands.js +10 -0
  4. package/lib/cjs/cli/options.js +5 -0
  5. package/lib/cjs/cli/utils.js +4 -0
  6. package/lib/cjs/common/index.js +2 -0
  7. package/lib/cjs/common/io.js +4 -0
  8. package/lib/cjs/common/turbo.js +2 -1
  9. package/lib/cjs/common/wayfinder/gateways/network.js +48 -0
  10. package/lib/cjs/common/wayfinder/gateways/simple-cache.js +35 -0
  11. package/lib/cjs/common/wayfinder/gateways/static.js +13 -0
  12. package/lib/cjs/common/wayfinder/index.js +48 -0
  13. package/lib/cjs/common/wayfinder/routing/strategies/ping.js +72 -0
  14. package/lib/cjs/common/wayfinder/routing/strategies/preferred-with-fallback.js +50 -0
  15. package/lib/cjs/common/wayfinder/routing/strategies/random.js +13 -0
  16. package/lib/cjs/common/wayfinder/routing/strategies/round-robin.js +42 -0
  17. package/lib/cjs/common/wayfinder/routing/strategies/static.js +29 -0
  18. package/lib/cjs/common/wayfinder/verification/strategies/data-root-verifier.js +110 -0
  19. package/lib/cjs/common/wayfinder/verification/strategies/hash-verifier.js +27 -0
  20. package/lib/cjs/common/wayfinder/verification/trusted.js +125 -0
  21. package/lib/cjs/common/wayfinder/wayfinder.js +508 -0
  22. package/lib/cjs/types/wayfinder.js +3 -0
  23. package/lib/cjs/utils/hash.js +83 -31
  24. package/lib/cjs/version.js +1 -1
  25. package/lib/esm/cli/commands/arnsPurchaseCommands.js +11 -1
  26. package/lib/esm/cli/options.js +5 -0
  27. package/lib/esm/cli/utils.js +3 -0
  28. package/lib/esm/common/index.js +2 -0
  29. package/lib/esm/common/io.js +4 -0
  30. package/lib/esm/common/turbo.js +2 -1
  31. package/lib/esm/common/wayfinder/gateways/network.js +44 -0
  32. package/lib/esm/common/wayfinder/gateways/simple-cache.js +31 -0
  33. package/lib/esm/common/wayfinder/gateways/static.js +9 -0
  34. package/lib/esm/common/wayfinder/index.js +32 -0
  35. package/lib/esm/common/wayfinder/routing/strategies/ping.js +68 -0
  36. package/lib/esm/common/wayfinder/routing/strategies/preferred-with-fallback.js +46 -0
  37. package/lib/esm/common/wayfinder/routing/strategies/random.js +9 -0
  38. package/lib/esm/common/wayfinder/routing/strategies/round-robin.js +38 -0
  39. package/lib/esm/common/wayfinder/routing/strategies/static.js +25 -0
  40. package/lib/esm/common/wayfinder/verification/strategies/data-root-verifier.js +102 -0
  41. package/lib/esm/common/wayfinder/verification/strategies/hash-verifier.js +23 -0
  42. package/lib/esm/common/wayfinder/verification/trusted.js +121 -0
  43. package/lib/esm/common/wayfinder/wayfinder.js +499 -0
  44. package/lib/esm/types/wayfinder.js +2 -0
  45. package/lib/esm/utils/hash.js +74 -27
  46. package/lib/esm/version.js +1 -1
  47. package/lib/types/cli/options.d.ts +4 -0
  48. package/lib/types/cli/utils.d.ts +3 -0
  49. package/lib/types/common/index.d.ts +1 -0
  50. package/lib/types/common/turbo.d.ts +2 -1
  51. package/lib/types/common/wayfinder/gateways/network.d.ts +33 -0
  52. package/lib/types/common/wayfinder/gateways/simple-cache.d.ts +31 -0
  53. package/lib/types/common/wayfinder/gateways/static.d.ts +23 -0
  54. package/lib/types/common/wayfinder/index.d.ts +27 -0
  55. package/lib/types/common/wayfinder/routing/strategies/ping.d.ts +27 -0
  56. package/lib/types/common/wayfinder/routing/strategies/preferred-with-fallback.d.ts +31 -0
  57. package/lib/types/common/wayfinder/routing/strategies/random.d.ts +21 -0
  58. package/lib/types/common/wayfinder/routing/strategies/round-robin.d.ts +29 -0
  59. package/lib/types/common/wayfinder/routing/strategies/static.d.ts +29 -0
  60. package/lib/types/common/wayfinder/verification/strategies/data-root-verifier.d.ts +27 -0
  61. package/lib/types/common/wayfinder/verification/strategies/hash-verifier.d.ts +26 -0
  62. package/lib/types/common/wayfinder/verification/trusted.d.ts +51 -0
  63. package/lib/types/common/wayfinder/wayfinder.d.ts +257 -0
  64. package/lib/types/types/io.d.ts +4 -0
  65. package/lib/types/types/wayfinder.d.ts +66 -0
  66. package/lib/types/utils/hash.d.ts +8 -4
  67. package/lib/types/version.d.ts +1 -1
  68. package/package.json +2 -1
@@ -12,6 +12,7 @@ async function buyRecordCLICommand(o) {
12
12
  const type = (0, utils_js_1.recordTypeFromOptions)(o);
13
13
  const years = (0, utils_js_1.positiveIntegerFromOptions)(o, 'years');
14
14
  const fundFrom = (0, utils_js_1.fundFromFromOptions)(o);
15
+ const referrer = (0, utils_js_1.referrerFromOptions)(o);
15
16
  const processId = o.processId;
16
17
  if (processId === undefined) {
17
18
  // TODO: Spawn ANT process, register it to ANT registry, get process ID
@@ -45,12 +46,14 @@ async function buyRecordCLICommand(o) {
45
46
  years,
46
47
  fundFrom: (0, utils_js_1.fundFromFromOptions)(o),
47
48
  paidBy: (0, utils_js_1.stringArrayFromOptions)(o, 'paidBy'),
49
+ referrer,
48
50
  }, (0, utils_js_1.customTagsFromOptions)(o));
49
51
  }
50
52
  async function upgradeRecordCLICommand(o) {
51
53
  const name = (0, utils_js_1.requiredStringFromOptions)(o, 'name');
52
54
  const { ario, signerAddress } = (0, utils_js_1.writeARIOFromOptions)(o);
53
55
  const fundFrom = (0, utils_js_1.fundFromFromOptions)(o);
56
+ const referrer = (0, utils_js_1.referrerFromOptions)(o);
54
57
  if (!o.skipConfirmation) {
55
58
  const existingRecord = await ario.getArNSRecord({
56
59
  name,
@@ -77,12 +80,14 @@ async function upgradeRecordCLICommand(o) {
77
80
  name,
78
81
  fundFrom,
79
82
  paidBy: (0, utils_js_1.stringArrayFromOptions)(o, 'paidBy'),
83
+ referrer,
80
84
  });
81
85
  }
82
86
  async function extendLeaseCLICommand(o) {
83
87
  const name = (0, utils_js_1.requiredStringFromOptions)(o, 'name');
84
88
  const years = (0, utils_js_1.requiredPositiveIntegerFromOptions)(o, 'years');
85
89
  const fundFrom = (0, utils_js_1.fundFromFromOptions)(o);
90
+ const referrer = (0, utils_js_1.referrerFromOptions)(o);
86
91
  const { ario, signerAddress } = (0, utils_js_1.writeARIOFromOptions)(o);
87
92
  if (!o.skipConfirmation) {
88
93
  const existingRecord = await ario.getArNSRecord({
@@ -111,12 +116,14 @@ async function extendLeaseCLICommand(o) {
111
116
  name,
112
117
  years,
113
118
  paidBy: (0, utils_js_1.stringArrayFromOptions)(o, 'paidBy'),
119
+ referrer,
114
120
  }, (0, utils_js_1.customTagsFromOptions)(o));
115
121
  }
116
122
  async function increaseUndernameLimitCLICommand(o) {
117
123
  const name = (0, utils_js_1.requiredStringFromOptions)(o, 'name');
118
124
  const increaseCount = (0, utils_js_1.requiredPositiveIntegerFromOptions)(o, 'increaseCount');
119
125
  const fundFrom = (0, utils_js_1.fundFromFromOptions)(o);
126
+ const referrer = (0, utils_js_1.referrerFromOptions)(o);
120
127
  const { ario, signerAddress } = (0, utils_js_1.writeARIOFromOptions)(o);
121
128
  if (!o.skipConfirmation) {
122
129
  const existingRecord = await ario.getArNSRecord({
@@ -142,11 +149,13 @@ async function increaseUndernameLimitCLICommand(o) {
142
149
  name,
143
150
  increaseCount,
144
151
  paidBy: (0, utils_js_1.stringArrayFromOptions)(o, 'paidBy'),
152
+ referrer,
145
153
  }, (0, utils_js_1.customTagsFromOptions)(o));
146
154
  }
147
155
  async function requestPrimaryNameCLICommand(o) {
148
156
  const { ario, signerAddress } = (0, utils_js_1.writeARIOFromOptions)(o);
149
157
  const fundFrom = (0, utils_js_1.fundFromFromOptions)(o);
158
+ const referrer = (0, utils_js_1.referrerFromOptions)(o);
150
159
  const name = (0, utils_js_1.requiredStringFromOptions)(o, 'name');
151
160
  if (!o.skipConfirmation) {
152
161
  // TODO: Assert name requested is not already owned?
@@ -167,5 +176,6 @@ async function requestPrimaryNameCLICommand(o) {
167
176
  name,
168
177
  fundFrom,
169
178
  paidBy: (0, utils_js_1.stringArrayFromOptions)(o, 'paidBy'),
179
+ referrer,
170
180
  }, (0, utils_js_1.customTagsFromOptions)(o));
171
181
  }
@@ -286,6 +286,10 @@ exports.optionMap = {
286
286
  description: 'Addresses to pay for the interaction',
287
287
  type: 'array',
288
288
  },
289
+ referrer: {
290
+ alias: '--referrer <referrer>',
291
+ description: 'The referrer for ArNS purchase tracking',
292
+ },
289
293
  };
290
294
  exports.walletOptions = [
291
295
  exports.optionMap.walletFile,
@@ -309,6 +313,7 @@ exports.arnsPurchaseOptions = [
309
313
  exports.optionMap.fundFrom,
310
314
  exports.optionMap.paidBy,
311
315
  exports.optionMap.paymentUrl,
316
+ exports.optionMap.referrer,
312
317
  ];
313
318
  exports.epochOptions = [exports.optionMap.epochIndex, exports.optionMap.timestamp];
314
319
  exports.addressAndVaultIdOptions = [exports.optionMap.address, exports.optionMap.vaultId];
@@ -46,6 +46,7 @@ exports.requiredPositiveIntegerFromOptions = requiredPositiveIntegerFromOptions;
46
46
  exports.getANTStateFromOptions = getANTStateFromOptions;
47
47
  exports.getTokenCostParamsFromOptions = getTokenCostParamsFromOptions;
48
48
  exports.fundFromFromOptions = fundFromFromOptions;
49
+ exports.referrerFromOptions = referrerFromOptions;
49
50
  exports.assertLockLengthInRange = assertLockLengthInRange;
50
51
  /**
51
52
  * Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
@@ -507,6 +508,9 @@ function fundFromFromOptions(o) {
507
508
  }
508
509
  return o.fundFrom ?? 'balance';
509
510
  }
511
+ function referrerFromOptions(o) {
512
+ return o.referrer;
513
+ }
510
514
  function assertLockLengthInRange(lockLengthMs, assertMin = true) {
511
515
  const minLockLengthMs = 1209600000; // 14 days
512
516
  const maxLockLengthMs = 378432000000; // ~12 years
@@ -38,3 +38,5 @@ __exportStar(require("./faucet.js"), exports);
38
38
  // ao
39
39
  __exportStar(require("./io.js"), exports);
40
40
  __exportStar(require("./contracts/ao-process.js"), exports);
41
+ // wayfinder
42
+ __exportStar(require("./wayfinder/index.js"), exports);
@@ -986,6 +986,7 @@ class ARIOWriteable extends ARIOReadable {
986
986
  { name: 'Process-Id', value: params.processId },
987
987
  { name: 'Purchase-Type', value: params.type || 'lease' },
988
988
  { name: 'Fund-From', value: params.fundFrom },
989
+ { name: 'Referrer', value: params.referrer },
989
990
  ];
990
991
  return this.process.send({
991
992
  signer: this.signer,
@@ -1016,6 +1017,7 @@ class ARIOWriteable extends ARIOReadable {
1016
1017
  { name: 'Action', value: 'Upgrade-Name' },
1017
1018
  { name: 'Name', value: params.name },
1018
1019
  { name: 'Fund-From', value: params.fundFrom },
1020
+ { name: 'Referrer', value: params.referrer },
1019
1021
  ];
1020
1022
  return this.process.send({
1021
1023
  signer: this.signer,
@@ -1049,6 +1051,7 @@ class ARIOWriteable extends ARIOReadable {
1049
1051
  { name: 'Name', value: params.name },
1050
1052
  { name: 'Years', value: params.years.toString() },
1051
1053
  { name: 'Fund-From', value: params.fundFrom },
1054
+ { name: 'Referrer', value: params.referrer },
1052
1055
  ];
1053
1056
  return this.process.send({
1054
1057
  signer: this.signer,
@@ -1073,6 +1076,7 @@ class ARIOWriteable extends ARIOReadable {
1073
1076
  { name: 'Name', value: params.name },
1074
1077
  { name: 'Quantity', value: params.increaseCount.toString() },
1075
1078
  { name: 'Fund-From', value: params.fundFrom },
1079
+ { name: 'Referrer', value: params.referrer },
1076
1080
  ];
1077
1081
  return this.process.send({
1078
1082
  signer: this.signer,
@@ -155,7 +155,7 @@ class TurboArNSPaymentProviderAuthenticated extends TurboArNSPaymentProviderUnau
155
155
  }
156
156
  this.signer = signer;
157
157
  }
158
- async initiateArNSPurchase({ intent, name, quantity, type, processId, years, paidBy = [], }) {
158
+ async initiateArNSPurchase({ intent, name, quantity, type, processId, years, paidBy = [], referrer, }) {
159
159
  // Signer check is implicitly handled by requiring it in the constructor
160
160
  const url = (0, url_js_1.urlWithSearchParams)({
161
161
  baseUrl: `${this.paymentUrl}/v1/arns/purchase/${intent}/${name}`,
@@ -165,6 +165,7 @@ class TurboArNSPaymentProviderAuthenticated extends TurboArNSPaymentProviderUnau
165
165
  type,
166
166
  years,
167
167
  paidBy,
168
+ referrer,
168
169
  },
169
170
  });
170
171
  const headers = await signedRequestHeadersFromSigner({
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NetworkGatewaysProvider = void 0;
4
+ const io_js_1 = require("../../io.js");
5
+ class NetworkGatewaysProvider {
6
+ ario;
7
+ sortBy;
8
+ sortOrder;
9
+ limit;
10
+ filter;
11
+ constructor({ ario = io_js_1.ARIO.mainnet(), sortBy = 'operatorStake', sortOrder = 'desc', limit = 1000, filter = (g) => g.status === 'joined', }) {
12
+ this.ario = ario;
13
+ this.sortBy = sortBy;
14
+ this.sortOrder = sortOrder;
15
+ this.limit = limit;
16
+ this.filter = filter;
17
+ }
18
+ async getGateways() {
19
+ let cursor;
20
+ let attempts = 0;
21
+ const gateways = [];
22
+ do {
23
+ try {
24
+ const { items: newGateways = [], nextCursor } = await this.ario.getGateways({
25
+ limit: 1000,
26
+ cursor,
27
+ sortBy: this.sortBy,
28
+ sortOrder: this.sortOrder,
29
+ });
30
+ gateways.push(...newGateways);
31
+ cursor = nextCursor;
32
+ attempts = 0; // reset attempts if we get a new cursor
33
+ }
34
+ catch (error) {
35
+ console.error('Error fetching gateways', {
36
+ cursor,
37
+ attempts,
38
+ error,
39
+ });
40
+ attempts++;
41
+ }
42
+ } while (cursor !== undefined && attempts < 3);
43
+ // filter out any gateways that are not joined
44
+ const filteredGateways = gateways.filter(this.filter).slice(0, this.limit);
45
+ return filteredGateways.map((g) => new URL(`${g.settings.protocol}://${g.settings.fqdn}:${g.settings.port}`));
46
+ }
47
+ }
48
+ exports.NetworkGatewaysProvider = NetworkGatewaysProvider;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SimpleCacheGatewaysProvider = void 0;
4
+ const index_js_1 = require("../../../web/index.js");
5
+ class SimpleCacheGatewaysProvider {
6
+ gatewaysProvider;
7
+ ttlSeconds;
8
+ lastUpdated;
9
+ gatewaysCache;
10
+ logger;
11
+ constructor({ gatewaysProvider, ttlSeconds = 60 * 60, // 1 hour
12
+ logger = index_js_1.Logger.default, }) {
13
+ this.gatewaysCache = [];
14
+ this.gatewaysProvider = gatewaysProvider;
15
+ this.ttlSeconds = ttlSeconds;
16
+ this.logger = logger;
17
+ }
18
+ async getGateways() {
19
+ const now = Date.now();
20
+ if (this.gatewaysCache.length === 0 ||
21
+ now - this.lastUpdated > this.ttlSeconds * 1000) {
22
+ try {
23
+ // preserve the cache if the fetch fails
24
+ const allGateways = await this.gatewaysProvider.getGateways();
25
+ this.gatewaysCache = allGateways;
26
+ this.lastUpdated = now;
27
+ }
28
+ catch (error) {
29
+ this.logger.error('Error fetching gateways', error);
30
+ }
31
+ }
32
+ return this.gatewaysCache;
33
+ }
34
+ }
35
+ exports.SimpleCacheGatewaysProvider = SimpleCacheGatewaysProvider;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StaticGatewaysProvider = void 0;
4
+ class StaticGatewaysProvider {
5
+ gateways;
6
+ constructor({ gateways }) {
7
+ this.gateways = gateways.map((g) => new URL(g));
8
+ }
9
+ async getGateways() {
10
+ return this.gateways;
11
+ }
12
+ }
13
+ exports.StaticGatewaysProvider = StaticGatewaysProvider;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ /**
18
+ * Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
19
+ *
20
+ * Licensed under the Apache License, Version 2.0 (the "License");
21
+ * you may not use this file except in compliance with the License.
22
+ * You may obtain a copy of the License at
23
+ *
24
+ * http://www.apache.org/licenses/LICENSE-2.0
25
+ *
26
+ * Unless required by applicable law or agreed to in writing, software
27
+ * distributed under the License is distributed on an "AS IS" BASIS,
28
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29
+ * See the License for the specific language governing permissions and
30
+ * limitations under the License.
31
+ */
32
+ __exportStar(require("./wayfinder.js"), exports);
33
+ // routing strategies
34
+ __exportStar(require("./routing/strategies/random.js"), exports);
35
+ __exportStar(require("./routing/strategies/static.js"), exports);
36
+ __exportStar(require("./routing/strategies/ping.js"), exports);
37
+ __exportStar(require("./routing/strategies/round-robin.js"), exports);
38
+ __exportStar(require("./routing/strategies/preferred-with-fallback.js"), exports);
39
+ // gateways providers
40
+ __exportStar(require("./gateways/network.js"), exports);
41
+ __exportStar(require("./gateways/simple-cache.js"), exports);
42
+ __exportStar(require("./gateways/static.js"), exports);
43
+ // trusted gateways
44
+ __exportStar(require("./verification/trusted.js"), exports);
45
+ // hash providers
46
+ __exportStar(require("./verification/strategies/data-root-verifier.js"), exports);
47
+ __exportStar(require("./verification/strategies/hash-verifier.js"), exports);
48
+ // TODO: signature verification
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FastestPingRoutingStrategy = void 0;
4
+ class FastestPingRoutingStrategy {
5
+ timeoutMs;
6
+ probePath;
7
+ constructor({ timeoutMs = 500, probePath = '/ar-io/info', // TODO: limit to allowed /ar-io and arweave node endpoints
8
+ } = {}) {
9
+ this.timeoutMs = timeoutMs;
10
+ this.probePath = probePath;
11
+ }
12
+ async selectGateway({ gateways }) {
13
+ if (gateways.length === 0) {
14
+ throw new Error('No gateways provided');
15
+ }
16
+ try {
17
+ const results = await Promise.allSettled(gateways.map(async (gateway) => {
18
+ try {
19
+ const startTime = Date.now();
20
+ const response = await fetch(`${gateway.toString().replace(/\/$/, '')}${this.probePath}`, {
21
+ method: 'HEAD',
22
+ signal: AbortSignal.timeout(this.timeoutMs),
23
+ });
24
+ const endTime = Date.now();
25
+ const durationMs = endTime - startTime;
26
+ return {
27
+ gateway,
28
+ status: response.status,
29
+ durationMs,
30
+ error: null,
31
+ };
32
+ }
33
+ catch (error) {
34
+ // Handle network errors
35
+ return {
36
+ gateway,
37
+ status: 'rejected',
38
+ durationMs: Infinity,
39
+ error,
40
+ };
41
+ }
42
+ }));
43
+ // Process results
44
+ const processedResults = results.map((result, index) => {
45
+ if (result.status === 'fulfilled') {
46
+ return result.value;
47
+ }
48
+ else {
49
+ return {
50
+ gateway: gateways[index],
51
+ status: 'rejected',
52
+ durationMs: Infinity,
53
+ error: result.reason,
54
+ };
55
+ }
56
+ });
57
+ // Filter healthy gateways and sort by latency
58
+ const healthyGateways = processedResults
59
+ .filter((result) => result.status === 200)
60
+ .sort((a, b) => a.durationMs - b.durationMs);
61
+ if (healthyGateways.length > 0) {
62
+ return healthyGateways[0].gateway;
63
+ }
64
+ throw new Error('No healthy gateways found');
65
+ }
66
+ catch (error) {
67
+ throw new Error('Failed to ping gateways: ' +
68
+ (error instanceof Error ? error.message : String(error)));
69
+ }
70
+ }
71
+ }
72
+ exports.FastestPingRoutingStrategy = FastestPingRoutingStrategy;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PreferredWithFallbackRoutingStrategy = void 0;
4
+ const logger_js_1 = require("../../../logger.js");
5
+ const ping_js_1 = require("./ping.js");
6
+ class PreferredWithFallbackRoutingStrategy {
7
+ name = 'preferred-with-fallback';
8
+ preferredGateway;
9
+ fallbackStrategy;
10
+ logger;
11
+ constructor({ preferredGateway, fallbackStrategy = new ping_js_1.FastestPingRoutingStrategy(), logger = logger_js_1.Logger.default, }) {
12
+ try {
13
+ this.preferredGateway = new URL(preferredGateway);
14
+ }
15
+ catch (error) {
16
+ throw new Error(`Invalid URL provided for preferred gateway: ${preferredGateway}`);
17
+ }
18
+ this.fallbackStrategy = fallbackStrategy;
19
+ this.logger = logger;
20
+ }
21
+ async selectGateway({ gateways = [] }) {
22
+ this.logger.debug('Attempting to connect to preferred gateway', {
23
+ preferredGateway: this.preferredGateway.toString(),
24
+ });
25
+ try {
26
+ // Check if the preferred gateway is responsive
27
+ const response = await fetch(this.preferredGateway.toString(), {
28
+ method: 'HEAD',
29
+ signal: AbortSignal.timeout(1000),
30
+ });
31
+ if (response.ok) {
32
+ this.logger.debug('Successfully connected to preferred gateway', {
33
+ preferredGateway: this.preferredGateway.toString(),
34
+ });
35
+ return this.preferredGateway;
36
+ }
37
+ throw new Error(`Preferred gateway responded with status: ${response.status}`);
38
+ }
39
+ catch (error) {
40
+ this.logger.warn('Failed to connect to preferred gateway, falling back to alternative strategy', {
41
+ preferredGateway: this.preferredGateway.toString(),
42
+ error: error instanceof Error ? error.message : String(error),
43
+ fallbackStrategy: this.fallbackStrategy.constructor.name,
44
+ });
45
+ // Fall back to the provided routing strategy
46
+ return this.fallbackStrategy.selectGateway({ gateways });
47
+ }
48
+ }
49
+ }
50
+ exports.PreferredWithFallbackRoutingStrategy = PreferredWithFallbackRoutingStrategy;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RandomRoutingStrategy = void 0;
4
+ const random_js_1 = require("../../../../utils/random.js");
5
+ class RandomRoutingStrategy {
6
+ async selectGateway({ gateways }) {
7
+ if (gateways.length === 0) {
8
+ throw new Error('No gateways available');
9
+ }
10
+ return gateways[(0, random_js_1.randomInt)(0, gateways.length)];
11
+ }
12
+ }
13
+ exports.RandomRoutingStrategy = RandomRoutingStrategy;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RoundRobinRoutingStrategy = void 0;
4
+ /**
5
+ * Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ const logger_js_1 = require("../../../../common/logger.js");
20
+ class RoundRobinRoutingStrategy {
21
+ gateways;
22
+ currentIndex;
23
+ logger;
24
+ constructor({ gateways, logger = logger_js_1.Logger.default, }) {
25
+ this.gateways = gateways;
26
+ this.currentIndex = 0;
27
+ this.logger = logger;
28
+ }
29
+ // provided gateways are ignored
30
+ async selectGateway({ gateways = [], } = {}) {
31
+ if (gateways.length > 0) {
32
+ this.logger.warn('RoundRobinRoutingStrategy does not accept provided gateways. Ignoring provided gateways...', {
33
+ providedGateways: gateways.length,
34
+ internalGateways: this.gateways,
35
+ });
36
+ }
37
+ const gateway = this.gateways[this.currentIndex];
38
+ this.currentIndex = (this.currentIndex + 1) % this.gateways.length;
39
+ return gateway;
40
+ }
41
+ }
42
+ exports.RoundRobinRoutingStrategy = RoundRobinRoutingStrategy;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StaticRoutingStrategy = void 0;
4
+ const logger_js_1 = require("../../../logger.js");
5
+ class StaticRoutingStrategy {
6
+ name = 'static';
7
+ gateway;
8
+ logger;
9
+ constructor({ gateway, logger = logger_js_1.Logger.default, }) {
10
+ try {
11
+ this.gateway = new URL(gateway);
12
+ }
13
+ catch (error) {
14
+ throw new Error(`Invalid URL provided for static gateway: ${gateway}`);
15
+ }
16
+ this.logger = logger;
17
+ }
18
+ // provided gateways are ignored
19
+ async selectGateway({ gateways = [], } = {}) {
20
+ if (gateways.length > 0) {
21
+ this.logger.warn('StaticRoutingStrategy does not accept provided gateways. Ignoring provided gateways...', {
22
+ providedGateways: gateways.length,
23
+ internalGateway: this.gateway,
24
+ });
25
+ }
26
+ return this.gateway;
27
+ }
28
+ }
29
+ exports.StaticRoutingStrategy = StaticRoutingStrategy;
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DataRootVerificationStrategy = exports.convertDataStreamToDataRoot = void 0;
7
+ /**
8
+ * Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
9
+ *
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ const arweave_1 = __importDefault(require("arweave"));
23
+ const merkle_js_1 = require("arweave/node/lib/merkle.js");
24
+ const base64_js_1 = require("../../../../utils/base64.js");
25
+ const hash_js_1 = require("../../../../utils/hash.js");
26
+ const convertDataStreamToDataRoot = async ({ dataStream, }) => {
27
+ const chunks = [];
28
+ let leftover = new Uint8Array(0);
29
+ let cursor = 0;
30
+ const asyncIterable = (0, hash_js_1.isAsyncIterable)(dataStream)
31
+ ? dataStream
32
+ : (0, hash_js_1.readableStreamToAsyncIterable)(dataStream);
33
+ for await (const data of asyncIterable) {
34
+ const inputChunk = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
35
+ const combined = new Uint8Array(leftover.length + inputChunk.length);
36
+ combined.set(leftover, 0);
37
+ combined.set(inputChunk, leftover.length);
38
+ let startIndex = 0;
39
+ while (combined.length - startIndex >= merkle_js_1.MAX_CHUNK_SIZE) {
40
+ let chunkSize = merkle_js_1.MAX_CHUNK_SIZE;
41
+ const remainderAfterThis = combined.length - startIndex - merkle_js_1.MAX_CHUNK_SIZE;
42
+ if (remainderAfterThis > 0 && remainderAfterThis < merkle_js_1.MIN_CHUNK_SIZE) {
43
+ chunkSize = Math.ceil((combined.length - startIndex) / 2);
44
+ }
45
+ const chunkData = combined.slice(startIndex, startIndex + chunkSize);
46
+ const dataHash = await arweave_1.default.crypto.hash(chunkData);
47
+ chunks.push({
48
+ dataHash,
49
+ minByteRange: cursor,
50
+ maxByteRange: cursor + chunkSize,
51
+ });
52
+ cursor += chunkSize;
53
+ startIndex += chunkSize;
54
+ }
55
+ leftover = combined.slice(startIndex);
56
+ }
57
+ if (leftover.length > 0) {
58
+ // TODO: ensure a web friendly crypto hash function is used in web
59
+ const dataHash = await arweave_1.default.crypto.hash(leftover);
60
+ chunks.push({
61
+ dataHash,
62
+ minByteRange: cursor,
63
+ maxByteRange: cursor + leftover.length,
64
+ });
65
+ }
66
+ const leaves = await (0, merkle_js_1.generateLeaves)(chunks);
67
+ const root = await (0, merkle_js_1.buildLayers)(leaves);
68
+ return (0, base64_js_1.toB64Url)(Buffer.from(root.id));
69
+ };
70
+ exports.convertDataStreamToDataRoot = convertDataStreamToDataRoot;
71
+ class DataRootVerificationStrategy {
72
+ trustedDataRootProvider;
73
+ constructor({ trustedDataRootProvider, }) {
74
+ this.trustedDataRootProvider = trustedDataRootProvider;
75
+ }
76
+ async verifyData({ data, txId, }) {
77
+ const [computedDataRoot, trustedDataRoot] = await Promise.all([
78
+ (0, exports.convertDataStreamToDataRoot)({
79
+ dataStream: data,
80
+ }),
81
+ this.trustedDataRootProvider.getDataRoot({
82
+ txId,
83
+ }),
84
+ ]);
85
+ if (computedDataRoot !== trustedDataRoot) {
86
+ throw new Error('Data root does not match', {
87
+ cause: { computedDataRoot, trustedDataRoot },
88
+ });
89
+ }
90
+ }
91
+ }
92
+ exports.DataRootVerificationStrategy = DataRootVerificationStrategy;
93
+ // some data item options
94
+ // compute and verify data root, use offsets from server and verify the signature that the data item at the offset matches the signature
95
+ // does not give you assurance of valid bundle, but gives verification that the data item itself is valid
96
+ // reading from offsets is the only way for the client to compute and verify the signature
97
+ /**
98
+ * - when you get a signature of a data item, you can only verify the owner
99
+ * - you still need to verify it's going back to the bundle, unpack it, and verify the data item exists at the offset
100
+ * - you need to the location of the chunks for the data item, and prove it's in the chunk and then prove the data root of the bundle, then you have fully verified the data verifier
101
+ * - how to prove the data item is on arweave - verify the merkle hash that the chunks for the data item, fit within the expected tree of the parent bundle
102
+ *
103
+ * Composite verifier - you'll want to be very efficient with streams
104
+ * - hash verifier
105
+ * - parent chunks verifier --> for any range of data within a single transaction, tell me that it's correct
106
+ * - signature verifier
107
+ * - offset verifier
108
+ * - data item verifier
109
+ */
110
+ // introduce a composite verifier that determines where/how to lookup the hash
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HashVerificationStrategy = void 0;
4
+ const hash_js_1 = require("../../../../utils/hash.js");
5
+ class HashVerificationStrategy {
6
+ trustedHashProvider;
7
+ constructor({ trustedHashProvider, }) {
8
+ this.trustedHashProvider = trustedHashProvider;
9
+ }
10
+ async verifyData({ data, txId, }) {
11
+ // kick off the hash computation, but don't wait for it until we compute our own hash
12
+ const [computedHash, fetchedHash] = await Promise.all([
13
+ (0, hash_js_1.hashDataStreamToB64Url)(data),
14
+ this.trustedHashProvider.getHash({ txId }),
15
+ ]);
16
+ // await on the hash promise and compare to get a little concurrency when computing hashes over larger data
17
+ if (computedHash === undefined) {
18
+ throw new Error('Hash could not be computed');
19
+ }
20
+ if (computedHash !== fetchedHash.hash) {
21
+ throw new Error('Hash does not match', {
22
+ cause: { computedHash, trustedHash: fetchedHash },
23
+ });
24
+ }
25
+ }
26
+ }
27
+ exports.HashVerificationStrategy = HashVerificationStrategy;