@ar.io/sdk 3.11.0-alpha.6 → 3.11.0-alpha.8

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 (50) hide show
  1. package/bundles/web.bundle.min.js +119 -116
  2. package/lib/cjs/common/io.js +8 -3
  3. package/lib/cjs/common/wayfinder/gateways/trusted-gateways.js +106 -0
  4. package/lib/cjs/common/wayfinder/gateways.js +19 -8
  5. package/lib/cjs/common/wayfinder/index.js +7 -1
  6. package/lib/cjs/common/wayfinder/routers/priority.js +11 -20
  7. package/lib/cjs/common/wayfinder/routers/priority.test.js +5 -5
  8. package/lib/cjs/common/wayfinder/routers/random.js +2 -2
  9. package/lib/cjs/common/wayfinder/routers/random.test.js +5 -84
  10. package/lib/cjs/common/wayfinder/routers/{fixed.js → static.js} +5 -5
  11. package/lib/cjs/common/wayfinder/routers/{fixed.test.js → static.test.js} +4 -4
  12. package/lib/cjs/common/wayfinder/verification/data-root-verifier.js +139 -0
  13. package/lib/cjs/common/wayfinder/verification/hash-verifier.js +50 -0
  14. package/lib/cjs/common/wayfinder/wayfinder.js +408 -19
  15. package/lib/cjs/common/wayfinder/wayfinder.test.js +296 -49
  16. package/lib/cjs/types/wayfinder.js +1 -0
  17. package/lib/cjs/utils/arweave.js +1 -1
  18. package/lib/cjs/utils/hash.js +56 -0
  19. package/lib/cjs/version.js +1 -1
  20. package/lib/esm/common/io.js +8 -3
  21. package/lib/esm/common/wayfinder/gateways/trusted-gateways.js +102 -0
  22. package/lib/esm/common/wayfinder/gateways.js +17 -6
  23. package/lib/esm/common/wayfinder/index.js +7 -1
  24. package/lib/esm/common/wayfinder/routers/priority.js +11 -20
  25. package/lib/esm/common/wayfinder/routers/priority.test.js +5 -5
  26. package/lib/esm/common/wayfinder/routers/random.js +2 -2
  27. package/lib/esm/common/wayfinder/routers/random.test.js +5 -84
  28. package/lib/esm/common/wayfinder/routers/{fixed.js → static.js} +3 -3
  29. package/lib/esm/common/wayfinder/routers/{fixed.test.js → static.test.js} +4 -4
  30. package/lib/esm/common/wayfinder/verification/data-root-verifier.js +130 -0
  31. package/lib/esm/common/wayfinder/verification/hash-verifier.js +46 -0
  32. package/lib/esm/common/wayfinder/wayfinder.js +402 -19
  33. package/lib/esm/common/wayfinder/wayfinder.test.js +297 -50
  34. package/lib/esm/types/wayfinder.js +1 -0
  35. package/lib/esm/utils/arweave.js +1 -1
  36. package/lib/esm/utils/hash.js +50 -0
  37. package/lib/esm/version.js +1 -1
  38. package/lib/types/common/wayfinder/gateways/trusted-gateways.d.ts +51 -0
  39. package/lib/types/common/wayfinder/gateways.d.ts +16 -7
  40. package/lib/types/common/wayfinder/index.d.ts +4 -1
  41. package/lib/types/common/wayfinder/routers/priority.d.ts +8 -12
  42. package/lib/types/common/wayfinder/routers/{fixed.d.ts → static.d.ts} +3 -3
  43. package/lib/types/common/wayfinder/verification/data-root-verifier.d.ts +31 -0
  44. package/lib/types/common/wayfinder/verification/hash-verifier.d.ts +27 -0
  45. package/lib/types/common/wayfinder/wayfinder.d.ts +148 -10
  46. package/lib/types/types/wayfinder.d.ts +43 -0
  47. package/lib/types/utils/hash.d.ts +4 -0
  48. package/lib/types/version.d.ts +1 -1
  49. package/package.json +1 -1
  50. /package/lib/types/common/wayfinder/routers/{fixed.test.d.ts → static.test.d.ts} +0 -0
@@ -925,7 +925,10 @@ class ARIOWriteable extends ARIOReadable {
925
925
  }
926
926
  return this.paymentProvider.initiateArNSPurchase({
927
927
  intent: 'Buy-Name',
928
- ...params,
928
+ name: params.name,
929
+ years: params.years,
930
+ type: params.type,
931
+ processId: params.processId,
929
932
  });
930
933
  }
931
934
  const { tags = [] } = options || {};
@@ -989,7 +992,8 @@ class ARIOWriteable extends ARIOReadable {
989
992
  }
990
993
  return this.paymentProvider.initiateArNSPurchase({
991
994
  intent: 'Extend-Lease',
992
- ...params,
995
+ name: params.name,
996
+ years: params.years,
993
997
  });
994
998
  }
995
999
  const { tags = [] } = options || {};
@@ -1012,7 +1016,8 @@ class ARIOWriteable extends ARIOReadable {
1012
1016
  }
1013
1017
  return this.paymentProvider.initiateArNSPurchase({
1014
1018
  intent: 'Increase-Undername-Limit',
1015
- ...params,
1019
+ quantity: params.increaseCount,
1020
+ name: params.name,
1016
1021
  });
1017
1022
  }
1018
1023
  const { tags = [] } = options || {};
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TrustedGatewaysHashProvider = void 0;
4
+ const arioGatewayHeaders = {
5
+ digest: 'x-ar-io-digest',
6
+ verified: 'x-ar-io-verified',
7
+ txId: 'x-arns-resolved-tx-id',
8
+ processId: 'x-arns-resolved-process-id',
9
+ };
10
+ class TrustedGatewaysHashProvider {
11
+ gatewaysProvider;
12
+ constructor({ gatewaysProvider,
13
+ // TODO: add threshold for allowed hash difference (i.e. by count or ratio of total gateways checked)
14
+ }) {
15
+ this.gatewaysProvider = gatewaysProvider;
16
+ }
17
+ /**
18
+ * Gets the digest for a given txId from all trusted gateways and ensures they all match.
19
+ * @param txId - The txId to get the digest for.
20
+ * @returns The digest for the given txId.
21
+ */
22
+ async getHash({ txId, }) {
23
+ // get the hash from every gateway, and ensure they all match
24
+ const hashSet = new Set();
25
+ const hashResults = [];
26
+ const gateways = await this.gatewaysProvider.getGateways();
27
+ const hashes = await Promise.all(gateways.map(async (gateway) => {
28
+ const response = await fetch(`${gateway.toString()}${txId}`, {
29
+ method: 'HEAD',
30
+ redirect: 'follow',
31
+ });
32
+ if (!response.ok) {
33
+ // skip this gateway
34
+ return undefined;
35
+ }
36
+ const txIdHash = response.headers.get(arioGatewayHeaders.digest);
37
+ if (txIdHash === null || txIdHash === undefined) {
38
+ // skip this gateway
39
+ return undefined;
40
+ }
41
+ hashResults.push({
42
+ gateway: gateway.hostname,
43
+ txIdHash,
44
+ });
45
+ return txIdHash;
46
+ }));
47
+ for (const hash of hashes) {
48
+ if (hash !== undefined) {
49
+ hashSet.add(hash);
50
+ }
51
+ }
52
+ if (hashSet.size === 0) {
53
+ throw new Error(`No trusted gateways found for txId ${txId}`);
54
+ }
55
+ if (hashSet.size > 1) {
56
+ throw new Error(`Failed to get consistent hash from all trusted gateways. ${JSON.stringify(hashResults)}`);
57
+ }
58
+ return { hash: hashResults[0].txIdHash, algorithm: 'sha256' };
59
+ }
60
+ /**
61
+ * Get the data root for a given txId from all trusted gateways and ensure they all match.
62
+ * @param txId - The txId to get the data root for.
63
+ * @returns The data root for the given txId.
64
+ */
65
+ async getDataRoot({ txId }) {
66
+ const dataRootSet = new Set();
67
+ const dataRootResults = [];
68
+ const gateways = await this.gatewaysProvider.getGateways();
69
+ const dataRoots = await Promise.all(gateways.map(async (gateway) => {
70
+ const response = await fetch(`${gateway.toString()}tx/${txId}/data_root`);
71
+ if (!response.ok) {
72
+ // skip this gateway
73
+ return undefined;
74
+ }
75
+ const dataRoot = await response.text();
76
+ dataRootResults.push({
77
+ gateway: gateway.hostname,
78
+ dataRoot,
79
+ });
80
+ return dataRoot;
81
+ }));
82
+ for (const dataRoot of dataRoots) {
83
+ if (dataRoot !== undefined) {
84
+ dataRootSet.add(dataRoot);
85
+ }
86
+ }
87
+ if (dataRootSet.size > 1) {
88
+ throw new Error(`Failed to get consistent data root from all trusted gateways. ${JSON.stringify(dataRootResults)}`);
89
+ }
90
+ return dataRootSet.values().next().value;
91
+ }
92
+ }
93
+ exports.TrustedGatewaysHashProvider = TrustedGatewaysHashProvider;
94
+ // client could check hashes of data items, match expected hash
95
+ // if the gateway has the hash and they've verified it, you can trust the data item and offset
96
+ // you would be only trusting the gateway that it is a valid bundle
97
+ // you can request the offset from the gateway to verify the id
98
+ /**
99
+ * Note from @djwhitt
100
+ *
101
+ * Calculating data roots this way is fine, but it may not reproduce the original data root.
102
+ * We could also implement a data root verifier that pulls all the chunks, checks that they
103
+ * reproduce the expected data root, and then compares the concatenated chunk data to the
104
+ * original data retrieved. That would take a while, but it should be able to verify any L1
105
+ * data where we can find the chunks.
106
+ */
@@ -1,10 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SimpleCacheGatewaysProvider = exports.StaticGatewaysProvider = exports.ARIOGatewaysProvider = void 0;
4
- class ARIOGatewaysProvider {
3
+ exports.SimpleCacheGatewaysProvider = exports.StaticGatewaysProvider = exports.NetworkGatewaysProvider = void 0;
4
+ class NetworkGatewaysProvider {
5
5
  ario;
6
- constructor({ ario }) {
6
+ sortBy;
7
+ sortOrder;
8
+ limit;
9
+ filter;
10
+ constructor({ ario, sortBy = 'operatorStake', sortOrder = 'desc', limit = 1000, filter = (g) => g.status === 'joined', }) {
7
11
  this.ario = ario;
12
+ this.sortBy = sortBy;
13
+ this.sortOrder = sortOrder;
14
+ this.limit = limit;
15
+ this.filter = filter;
8
16
  }
9
17
  async getGateways() {
10
18
  let cursor;
@@ -12,9 +20,11 @@ class ARIOGatewaysProvider {
12
20
  const gateways = [];
13
21
  do {
14
22
  try {
15
- const { items: newGateways, nextCursor } = await this.ario.getGateways({
23
+ const { items: newGateways = [], nextCursor } = await this.ario.getGateways({
16
24
  limit: 1000,
17
25
  cursor,
26
+ sortBy: this.sortBy,
27
+ sortOrder: this.sortOrder,
18
28
  });
19
29
  gateways.push(...newGateways);
20
30
  cursor = nextCursor;
@@ -30,14 +40,15 @@ class ARIOGatewaysProvider {
30
40
  }
31
41
  } while (cursor !== undefined && attempts < 3);
32
42
  // filter out any gateways that are not joined
33
- return gateways.filter((g) => g.status === 'joined');
43
+ const filteredGateways = gateways.filter(this.filter).slice(0, this.limit);
44
+ return filteredGateways.map((g) => new URL(`${g.settings.protocol}://${g.settings.fqdn}:${g.settings.port}`));
34
45
  }
35
46
  }
36
- exports.ARIOGatewaysProvider = ARIOGatewaysProvider;
47
+ exports.NetworkGatewaysProvider = NetworkGatewaysProvider;
37
48
  class StaticGatewaysProvider {
38
49
  gateways;
39
50
  constructor({ gateways }) {
40
- this.gateways = gateways;
51
+ this.gateways = gateways.map((g) => new URL(g));
41
52
  }
42
53
  async getGateways() {
43
54
  return this.gateways;
@@ -62,7 +73,7 @@ class SimpleCacheGatewaysProvider {
62
73
  try {
63
74
  // preserve the cache if the fetch fails
64
75
  const allGateways = await this.gatewaysProvider.getGateways();
65
- this.gatewaysCache = allGateways.filter((g) => g.status === 'joined');
76
+ this.gatewaysCache = allGateways;
66
77
  this.lastUpdated = now;
67
78
  }
68
79
  catch (error) {
@@ -33,6 +33,12 @@ __exportStar(require("./wayfinder.js"), exports);
33
33
  // routers
34
34
  __exportStar(require("./routers/random.js"), exports);
35
35
  __exportStar(require("./routers/priority.js"), exports);
36
- __exportStar(require("./routers/fixed.js"), exports);
36
+ __exportStar(require("./routers/static.js"), exports);
37
37
  // gateways providers
38
38
  __exportStar(require("./gateways.js"), exports);
39
+ // trusted gateways
40
+ __exportStar(require("./gateways/trusted-gateways.js"), exports);
41
+ // hash providers
42
+ __exportStar(require("./verification/data-root-verifier.js"), exports);
43
+ __exportStar(require("./verification/hash-verifier.js"), exports);
44
+ // TODO: signature verification
@@ -2,37 +2,28 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PriorityGatewayRouter = void 0;
4
4
  const random_js_1 = require("../../../utils/random.js");
5
+ const gateways_js_1 = require("../gateways.js");
5
6
  // TODO: one of N where N are in the last time window have met certain performance thresholds
6
7
  // TODO: look at bitorrent routing protocols for inspiration
7
8
  // TODO: router that looks at local stats/metrics and adjusts based on those
8
9
  class PriorityGatewayRouter {
9
10
  name = 'priority';
10
11
  gatewaysProvider;
11
- limit;
12
- sortBy;
13
- sortOrder;
14
- blocklist;
15
- constructor({ gatewaysProvider, limit = 1, sortBy = 'operatorStake', sortOrder = 'desc', blocklist = [], }) {
16
- this.gatewaysProvider = gatewaysProvider;
17
- this.limit = limit;
18
- this.sortBy = sortBy;
19
- this.sortOrder = sortOrder;
20
- this.blocklist = blocklist;
12
+ constructor({ ario, sortBy, sortOrder, limit, }) {
13
+ this.gatewaysProvider = new gateways_js_1.NetworkGatewaysProvider({
14
+ ario,
15
+ sortBy,
16
+ sortOrder,
17
+ limit,
18
+ });
21
19
  }
22
20
  async getTargetGateway() {
23
- const allGateways = await this.gatewaysProvider.getGateways();
24
- const gateways = allGateways.filter((gateway) => gateway.status === 'joined' &&
25
- !this.blocklist.includes(gateway.settings.fqdn));
26
- const sortedGateways = gateways
27
- .sort(this.sortOrder === 'asc'
28
- ? (a, b) => a[this.sortBy] - b[this.sortBy]
29
- : (a, b) => b[this.sortBy] - a[this.sortBy])
30
- .slice(0, this.limit);
31
- const targetGateway = sortedGateways[(0, random_js_1.randomInt)(0, sortedGateways.length)];
21
+ const gateways = await this.gatewaysProvider.getGateways();
22
+ const targetGateway = gateways[(0, random_js_1.randomInt)(0, gateways.length)];
32
23
  if (targetGateway === undefined) {
33
24
  throw new Error('No target gateway found');
34
25
  }
35
- return new URL(`${targetGateway.settings.protocol}://${targetGateway.settings.fqdn}:${targetGateway.settings.port}`);
26
+ return targetGateway;
36
27
  }
37
28
  }
38
29
  exports.PriorityGatewayRouter = PriorityGatewayRouter;
@@ -129,13 +129,13 @@ const priority_js_1 = require("./priority.js");
129
129
  },
130
130
  },
131
131
  ];
132
- const mockGatewaysProvider = {
133
- getGateways: async () => mockGateways,
132
+ const mockArIOClient = {
133
+ getGateways: async () => ({ items: mockGateways }),
134
134
  };
135
135
  (0, node_test_1.it)('should prioritize gateway with highest success rate when using successRate weight', async () => {
136
136
  const router = new priority_js_1.PriorityGatewayRouter({
137
- gatewaysProvider: mockGatewaysProvider,
138
- sortBy: 'totalDelegatedStake',
137
+ ario: mockArIOClient,
138
+ sortBy: 'operatorStake',
139
139
  sortOrder: 'desc',
140
140
  limit: 1,
141
141
  });
@@ -144,7 +144,7 @@ const priority_js_1 = require("./priority.js");
144
144
  });
145
145
  (0, node_test_1.it)('should prioritize gateway with lowest latency when using latency weight', async () => {
146
146
  const router = new priority_js_1.PriorityGatewayRouter({
147
- gatewaysProvider: mockGatewaysProvider,
147
+ ario: mockArIOClient,
148
148
  sortBy: 'operatorStake',
149
149
  sortOrder: 'desc',
150
150
  limit: 1,
@@ -12,12 +12,12 @@ class RandomGatewayRouter {
12
12
  }
13
13
  async getTargetGateway() {
14
14
  const allGateways = await this.gatewaysProvider.getGateways();
15
- const gateways = allGateways.filter((g) => g.status === 'joined' && !this.blocklist.includes(g.settings.fqdn));
15
+ const gateways = allGateways.filter((g) => !this.blocklist.includes(g.hostname));
16
16
  const targetGateway = gateways[(0, random_js_1.randomInt)(0, gateways.length)];
17
17
  if (targetGateway === undefined) {
18
18
  throw new Error('No target gateway found');
19
19
  }
20
- return new URL(`${targetGateway.settings.protocol}://${targetGateway.settings.fqdn}:${targetGateway.settings.port}`);
20
+ return targetGateway;
21
21
  }
22
22
  }
23
23
  exports.RandomGatewayRouter = RandomGatewayRouter;
@@ -5,88 +5,9 @@ const node_test_1 = require("node:test");
5
5
  const random_js_1 = require("./random.js");
6
6
  (0, node_test_1.describe)('RandomRouter', () => {
7
7
  const mockGateways = [
8
- {
9
- settings: {
10
- fqdn: 'gateway1.net',
11
- port: 443,
12
- protocol: 'https',
13
- allowDelegatedStaking: false,
14
- delegateRewardShareRatio: 0.5,
15
- allowedDelegates: [],
16
- minDelegatedStake: 0,
17
- autoStake: false,
18
- properties: '',
19
- label: '',
20
- note: '',
21
- },
22
- gatewayAddress: 'addr1',
23
- observerAddress: 'addr1',
24
- totalDelegatedStake: 1000,
25
- startTimestamp: 0,
26
- endTimestamp: 0,
27
- operatorStake: 100,
28
- status: 'joined',
29
- weights: {
30
- normalizedCompositeWeight: 0.5,
31
- stakeWeight: 0.5,
32
- tenureWeight: 0.5,
33
- gatewayPerformanceRatio: 0.5,
34
- observerPerformanceRatio: 0.5,
35
- compositeWeight: 0.5,
36
- gatewayRewardRatioWeight: 0.5,
37
- observerRewardRatioWeight: 0.5,
38
- },
39
- stats: {
40
- passedConsecutiveEpochs: 10,
41
- failedConsecutiveEpochs: 5,
42
- totalEpochCount: 15,
43
- passedEpochCount: 10,
44
- failedEpochCount: 5,
45
- observedEpochCount: 15,
46
- prescribedEpochCount: 20,
47
- },
48
- },
49
- {
50
- settings: {
51
- fqdn: 'gateway2.net',
52
- port: 443,
53
- protocol: 'https',
54
- allowDelegatedStaking: false,
55
- delegateRewardShareRatio: 0.5,
56
- allowedDelegates: [],
57
- minDelegatedStake: 0,
58
- autoStake: false,
59
- properties: '',
60
- label: '',
61
- note: '',
62
- },
63
- gatewayAddress: 'addr2',
64
- observerAddress: 'addr2',
65
- totalDelegatedStake: 0,
66
- startTimestamp: 0,
67
- endTimestamp: 0,
68
- operatorStake: 0,
69
- status: 'leaving',
70
- weights: {
71
- normalizedCompositeWeight: 0.5,
72
- stakeWeight: 0.5,
73
- tenureWeight: 0.5,
74
- gatewayPerformanceRatio: 0.5,
75
- observerPerformanceRatio: 0.5,
76
- compositeWeight: 0.5,
77
- gatewayRewardRatioWeight: 0.5,
78
- observerRewardRatioWeight: 0.5,
79
- },
80
- stats: {
81
- passedConsecutiveEpochs: 10,
82
- failedConsecutiveEpochs: 5,
83
- totalEpochCount: 15,
84
- passedEpochCount: 10,
85
- failedEpochCount: 5,
86
- observedEpochCount: 15,
87
- prescribedEpochCount: 20,
88
- },
89
- },
8
+ new URL('https://gateway1.net'),
9
+ new URL('https://gateway2.net'),
10
+ new URL('https://gateway3.net'),
90
11
  ];
91
12
  const mockGatewaysProvider = {
92
13
  getGateways: async () => mockGateways,
@@ -95,10 +16,10 @@ const random_js_1 = require("./random.js");
95
16
  const router = new random_js_1.RandomGatewayRouter({
96
17
  gatewaysProvider: mockGatewaysProvider,
97
18
  });
98
- // Run multiple times to ensure we only get joined gateways
19
+ // random gateway should be one of the mock gateways
99
20
  for (let i = 0; i < 10; i++) {
100
21
  const result = await router.getTargetGateway();
101
- node_assert_1.strict.deepStrictEqual(result, new URL('https://gateway1.net'));
22
+ node_assert_1.strict.ok(mockGateways.includes(result));
102
23
  }
103
24
  });
104
25
  });
@@ -1,14 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FixedGatewayRouter = void 0;
4
- class FixedGatewayRouter {
5
- name = 'fixed';
3
+ exports.StaticGatewayRouter = void 0;
4
+ class StaticGatewayRouter {
5
+ name = 'static';
6
6
  gateway;
7
7
  constructor({ gateway }) {
8
- this.gateway = gateway;
8
+ this.gateway = new URL(gateway);
9
9
  }
10
10
  async getTargetGateway() {
11
11
  return this.gateway;
12
12
  }
13
13
  }
14
- exports.FixedGatewayRouter = FixedGatewayRouter;
14
+ exports.StaticGatewayRouter = StaticGatewayRouter;
@@ -2,11 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const node_assert_1 = require("node:assert");
4
4
  const node_test_1 = require("node:test");
5
- const fixed_js_1 = require("./fixed.js");
6
- (0, node_test_1.describe)('FixedRouter', () => {
5
+ const static_js_1 = require("./static.js");
6
+ (0, node_test_1.describe)('StaticGatewayRouter', () => {
7
7
  (0, node_test_1.it)('should return the provided gateway', async () => {
8
- const router = new fixed_js_1.FixedGatewayRouter({
9
- gateway: new URL('http://test-gateway.net'),
8
+ const router = new static_js_1.StaticGatewayRouter({
9
+ gateway: 'http://test-gateway.net',
10
10
  });
11
11
  const result = await router.getTargetGateway();
12
12
  node_assert_1.strict.deepStrictEqual(result, new URL('http://test-gateway.net'));
@@ -0,0 +1,139 @@
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.DataRootVerifier = exports.convertReadableToDataRoot = void 0;
7
+ exports.convertBufferToDataRoot = convertBufferToDataRoot;
8
+ /**
9
+ * Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
10
+ *
11
+ * Licensed under the Apache License, Version 2.0 (the "License");
12
+ * you may not use this file except in compliance with the License.
13
+ * You may obtain a copy of the License at
14
+ *
15
+ * http://www.apache.org/licenses/LICENSE-2.0
16
+ *
17
+ * Unless required by applicable law or agreed to in writing, software
18
+ * distributed under the License is distributed on an "AS IS" BASIS,
19
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
+ * See the License for the specific language governing permissions and
21
+ * limitations under the License.
22
+ */
23
+ const arweave_1 = __importDefault(require("arweave"));
24
+ const merkle_js_1 = require("arweave/node/lib/merkle.js");
25
+ const node_stream_1 = require("node:stream");
26
+ const base64_js_1 = require("../../../utils/base64.js");
27
+ async function convertBufferToDataRoot({ buffer, }) {
28
+ const chunks = [];
29
+ let cursor = 0;
30
+ let offset = 0;
31
+ while (offset < buffer.byteLength) {
32
+ let chunkSize = Math.min(merkle_js_1.MAX_CHUNK_SIZE, buffer.byteLength - offset);
33
+ const remainder = buffer.byteLength - offset - chunkSize;
34
+ if (remainder > 0 && remainder < merkle_js_1.MIN_CHUNK_SIZE) {
35
+ chunkSize = Math.ceil((buffer.byteLength - offset) / 2);
36
+ }
37
+ // subarray does not exist on web Buffer type
38
+ const slice = buffer.subarray(offset, offset + chunkSize);
39
+ const hash = await crypto.subtle.digest('SHA-256', slice);
40
+ const hashArray = new Uint8Array(hash);
41
+ chunks.push({
42
+ dataHash: hashArray,
43
+ minByteRange: cursor,
44
+ maxByteRange: cursor + chunkSize,
45
+ });
46
+ cursor += chunkSize;
47
+ offset += chunkSize;
48
+ }
49
+ const leaves = await (0, merkle_js_1.generateLeaves)(chunks);
50
+ const result = await (0, merkle_js_1.buildLayers)(leaves);
51
+ return Buffer.from(result.id).toString('base64url');
52
+ }
53
+ const convertReadableToDataRoot = async ({ iterable, }) => {
54
+ const chunks = [];
55
+ let leftover = new Uint8Array(0);
56
+ let cursor = 0;
57
+ for await (const data of iterable) {
58
+ const inputChunk = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
59
+ const combined = new Uint8Array(leftover.length + inputChunk.length);
60
+ combined.set(leftover, 0);
61
+ combined.set(inputChunk, leftover.length);
62
+ let startIndex = 0;
63
+ while (combined.length - startIndex >= merkle_js_1.MAX_CHUNK_SIZE) {
64
+ let chunkSize = merkle_js_1.MAX_CHUNK_SIZE;
65
+ const remainderAfterThis = combined.length - startIndex - merkle_js_1.MAX_CHUNK_SIZE;
66
+ if (remainderAfterThis > 0 && remainderAfterThis < merkle_js_1.MIN_CHUNK_SIZE) {
67
+ chunkSize = Math.ceil((combined.length - startIndex) / 2);
68
+ }
69
+ const chunkData = combined.slice(startIndex, startIndex + chunkSize);
70
+ const dataHash = await arweave_1.default.crypto.hash(chunkData);
71
+ chunks.push({
72
+ dataHash,
73
+ minByteRange: cursor,
74
+ maxByteRange: cursor + chunkSize,
75
+ });
76
+ cursor += chunkSize;
77
+ startIndex += chunkSize;
78
+ }
79
+ leftover = combined.slice(startIndex);
80
+ }
81
+ if (leftover.length > 0) {
82
+ const dataHash = await arweave_1.default.crypto.hash(leftover);
83
+ chunks.push({
84
+ dataHash,
85
+ minByteRange: cursor,
86
+ maxByteRange: cursor + leftover.length,
87
+ });
88
+ }
89
+ const leaves = await (0, merkle_js_1.generateLeaves)(chunks);
90
+ const root = await (0, merkle_js_1.buildLayers)(leaves);
91
+ return (0, base64_js_1.toB64Url)(Buffer.from(root.id));
92
+ };
93
+ exports.convertReadableToDataRoot = convertReadableToDataRoot;
94
+ class DataRootVerifier {
95
+ trustedDataRootProvider;
96
+ constructor({ trustedDataRootProvider, }) {
97
+ this.trustedDataRootProvider = trustedDataRootProvider;
98
+ }
99
+ async verifyData({ data, txId, }) {
100
+ const trustedDataRootPromise = this.trustedDataRootProvider.getDataRoot({
101
+ txId,
102
+ });
103
+ let computedDataRoot;
104
+ if (Buffer.isBuffer(data)) {
105
+ computedDataRoot = await convertBufferToDataRoot({ buffer: data });
106
+ }
107
+ else if (data instanceof node_stream_1.Readable || data instanceof ReadableStream) {
108
+ computedDataRoot = await (0, exports.convertReadableToDataRoot)({ iterable: data });
109
+ }
110
+ if (computedDataRoot === undefined) {
111
+ throw new Error('Data root could not be computed');
112
+ }
113
+ const trustedDataRoot = await trustedDataRootPromise;
114
+ if (computedDataRoot !== trustedDataRoot) {
115
+ throw new Error('Data root does not match', {
116
+ cause: { computedDataRoot, trustedDataRoot },
117
+ });
118
+ }
119
+ }
120
+ }
121
+ exports.DataRootVerifier = DataRootVerifier;
122
+ // some data item options
123
+ // compute and verify data root, use offsets from server and verify the signature that the data item at the offset matches the signature
124
+ // does not give you assurance of valid bundle, but gives verification that the data item itself is valid
125
+ // reading from offsets is the only way for the client to compute and verify the signature
126
+ /**
127
+ * - when you get a signature of a data item, you can only verify the owner
128
+ * - you still need to verify it's going back to the bundle, unpack it, and verify the data item exists at the offset
129
+ * - 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
130
+ * - 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
131
+ *
132
+ * Composite verifier - you'll want to be very efficient with streams
133
+ * - hash verifier
134
+ * - parent chunks verifier --> for any range of data within a single transaction, tell me that it's correct
135
+ * - signature verifier
136
+ * - offset verifier
137
+ * - data item verifier
138
+ */
139
+ // introduce a composite verifier that determines where/how to lookup the hash
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HashVerifier = 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 node_stream_1 = require("node:stream");
20
+ const hash_js_1 = require("../../../utils/hash.js");
21
+ class HashVerifier {
22
+ trustedHashProvider;
23
+ constructor({ trustedHashProvider, }) {
24
+ this.trustedHashProvider = trustedHashProvider;
25
+ }
26
+ async verifyData({ data, txId, }) {
27
+ const hashPromise = this.trustedHashProvider.getHash({ txId });
28
+ let computedHash;
29
+ if (Buffer.isBuffer(data)) {
30
+ computedHash = (0, hash_js_1.hashBufferToB64Url)(data);
31
+ }
32
+ else if (data instanceof node_stream_1.Readable) {
33
+ computedHash = await (0, hash_js_1.hashReadableToB64Url)(data);
34
+ }
35
+ else if (data instanceof ReadableStream) {
36
+ computedHash = await (0, hash_js_1.hashReadableStreamToB64Url)(data);
37
+ }
38
+ // await on the hash promise and compare to get a little concurrency when computing hashes over larger data
39
+ const { hash } = await hashPromise;
40
+ if (computedHash === undefined) {
41
+ throw new Error('Hash could not be computed');
42
+ }
43
+ if (computedHash !== hash) {
44
+ throw new Error('Hash does not match', {
45
+ cause: { computedHash, trustedHash: hash },
46
+ });
47
+ }
48
+ }
49
+ }
50
+ exports.HashVerifier = HashVerifier;