@ar.io/sdk 3.11.0-beta.1 → 3.11.0

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 (74) hide show
  1. package/bundles/web.bundle.min.js +111 -114
  2. package/lib/cjs/common/ant-versions.js +3 -2
  3. package/lib/cjs/common/index.js +0 -2
  4. package/lib/cjs/common/io.js +13 -9
  5. package/lib/cjs/common/turbo.js +1 -1
  6. package/lib/cjs/types/index.js +0 -1
  7. package/lib/cjs/utils/ao.js +2 -1
  8. package/lib/cjs/version.js +1 -1
  9. package/lib/esm/common/ant-versions.js +3 -2
  10. package/lib/esm/common/index.js +0 -2
  11. package/lib/esm/common/io.js +13 -9
  12. package/lib/esm/common/turbo.js +1 -1
  13. package/lib/esm/types/index.js +0 -1
  14. package/lib/esm/utils/ao.js +2 -1
  15. package/lib/esm/version.js +1 -1
  16. package/lib/types/common/ant-versions.d.ts +3 -5
  17. package/lib/types/common/index.d.ts +0 -1
  18. package/lib/types/types/index.d.ts +0 -1
  19. package/lib/types/version.d.ts +1 -1
  20. package/package.json +1 -1
  21. package/lib/cjs/common/wayfinder/gateways/network.js +0 -48
  22. package/lib/cjs/common/wayfinder/gateways/simple-cache.js +0 -35
  23. package/lib/cjs/common/wayfinder/gateways/static.js +0 -13
  24. package/lib/cjs/common/wayfinder/index.js +0 -47
  25. package/lib/cjs/common/wayfinder/routing/strategies/ping.js +0 -72
  26. package/lib/cjs/common/wayfinder/routing/strategies/ping.test.js +0 -156
  27. package/lib/cjs/common/wayfinder/routing/strategies/random.js +0 -13
  28. package/lib/cjs/common/wayfinder/routing/strategies/random.test.js +0 -68
  29. package/lib/cjs/common/wayfinder/routing/strategies/round-robin.js +0 -42
  30. package/lib/cjs/common/wayfinder/routing/strategies/round-robin.test.js +0 -78
  31. package/lib/cjs/common/wayfinder/routing/strategies/static.js +0 -29
  32. package/lib/cjs/common/wayfinder/routing/strategies/static.test.js +0 -40
  33. package/lib/cjs/common/wayfinder/verification/strategies/data-root-verifier.js +0 -139
  34. package/lib/cjs/common/wayfinder/verification/strategies/hash-verifier.js +0 -50
  35. package/lib/cjs/common/wayfinder/verification/trusted.js +0 -106
  36. package/lib/cjs/common/wayfinder/wayfinder.js +0 -736
  37. package/lib/cjs/common/wayfinder/wayfinder.test.js +0 -519
  38. package/lib/cjs/types/wayfinder.js +0 -3
  39. package/lib/esm/common/wayfinder/gateways/network.js +0 -44
  40. package/lib/esm/common/wayfinder/gateways/simple-cache.js +0 -31
  41. package/lib/esm/common/wayfinder/gateways/static.js +0 -9
  42. package/lib/esm/common/wayfinder/index.js +0 -31
  43. package/lib/esm/common/wayfinder/routing/strategies/ping.js +0 -68
  44. package/lib/esm/common/wayfinder/routing/strategies/ping.test.js +0 -151
  45. package/lib/esm/common/wayfinder/routing/strategies/random.js +0 -9
  46. package/lib/esm/common/wayfinder/routing/strategies/random.test.js +0 -63
  47. package/lib/esm/common/wayfinder/routing/strategies/round-robin.js +0 -38
  48. package/lib/esm/common/wayfinder/routing/strategies/round-robin.test.js +0 -73
  49. package/lib/esm/common/wayfinder/routing/strategies/static.js +0 -25
  50. package/lib/esm/common/wayfinder/routing/strategies/static.test.js +0 -35
  51. package/lib/esm/common/wayfinder/verification/strategies/data-root-verifier.js +0 -130
  52. package/lib/esm/common/wayfinder/verification/strategies/hash-verifier.js +0 -46
  53. package/lib/esm/common/wayfinder/verification/trusted.js +0 -102
  54. package/lib/esm/common/wayfinder/wayfinder.js +0 -724
  55. package/lib/esm/common/wayfinder/wayfinder.test.js +0 -514
  56. package/lib/esm/types/wayfinder.js +0 -2
  57. package/lib/types/common/wayfinder/gateways/network.d.ts +0 -33
  58. package/lib/types/common/wayfinder/gateways/simple-cache.d.ts +0 -31
  59. package/lib/types/common/wayfinder/gateways/static.d.ts +0 -23
  60. package/lib/types/common/wayfinder/index.d.ts +0 -26
  61. package/lib/types/common/wayfinder/routing/strategies/ping.d.ts +0 -27
  62. package/lib/types/common/wayfinder/routing/strategies/ping.test.d.ts +0 -1
  63. package/lib/types/common/wayfinder/routing/strategies/random.d.ts +0 -21
  64. package/lib/types/common/wayfinder/routing/strategies/random.test.d.ts +0 -1
  65. package/lib/types/common/wayfinder/routing/strategies/round-robin.d.ts +0 -29
  66. package/lib/types/common/wayfinder/routing/strategies/round-robin.test.d.ts +0 -1
  67. package/lib/types/common/wayfinder/routing/strategies/static.d.ts +0 -29
  68. package/lib/types/common/wayfinder/routing/strategies/static.test.d.ts +0 -1
  69. package/lib/types/common/wayfinder/verification/strategies/data-root-verifier.d.ts +0 -31
  70. package/lib/types/common/wayfinder/verification/strategies/hash-verifier.d.ts +0 -27
  71. package/lib/types/common/wayfinder/verification/trusted.d.ts +0 -51
  72. package/lib/types/common/wayfinder/wayfinder.d.ts +0 -299
  73. package/lib/types/common/wayfinder/wayfinder.test.d.ts +0 -1
  74. package/lib/types/types/wayfinder.d.ts +0 -66
@@ -1,13 +0,0 @@
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;
@@ -1,68 +0,0 @@
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
- /**
7
- * Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
8
- *
9
- * Licensed under the Apache License, Version 2.0 (the "License");
10
- * you may not use this file except in compliance with the License.
11
- * You may obtain a copy of the License at
12
- *
13
- * http://www.apache.org/licenses/LICENSE-2.0
14
- *
15
- * Unless required by applicable law or agreed to in writing, software
16
- * distributed under the License is distributed on an "AS IS" BASIS,
17
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
- * See the License for the specific language governing permissions and
19
- * limitations under the License.
20
- */
21
- const strict_1 = __importDefault(require("node:assert/strict"));
22
- const node_test_1 = require("node:test");
23
- const random_js_1 = require("./random.js");
24
- (0, node_test_1.describe)('RandomRoutingStrategy', () => {
25
- (0, node_test_1.it)('selects a gateway from the provided list', async () => {
26
- // Arrange
27
- const gateways = [
28
- new URL('https://example1.com'),
29
- new URL('https://example2.com'),
30
- new URL('https://example3.com'),
31
- ];
32
- const strategy = new random_js_1.RandomRoutingStrategy();
33
- const selectedGateway = await strategy.selectGateway({ gateways });
34
- strict_1.default.ok(gateways.includes(selectedGateway), 'The selected gateway should be one of the gateways provided');
35
- });
36
- (0, node_test_1.it)('throws error when no gateways are provided', async () => {
37
- const gateways = [];
38
- const strategy = new random_js_1.RandomRoutingStrategy();
39
- await strict_1.default.rejects(async () => await strategy.selectGateway({ gateways }), /No gateways available/, 'Should throw an error when no gateways are provided');
40
- });
41
- (0, node_test_1.it)('should distribute gateway selection somewhat randomly', async () => {
42
- const gateways = [
43
- new URL('https://example1.com'),
44
- new URL('https://example2.com'),
45
- new URL('https://example3.com'),
46
- new URL('https://example4.com'),
47
- new URL('https://example5.com'),
48
- ];
49
- const strategy = new random_js_1.RandomRoutingStrategy();
50
- const selections = new Map();
51
- // select gateways multiple times
52
- const iterations = 100;
53
- for (let i = 0; i < iterations; i++) {
54
- const gateway = await strategy.selectGateway({ gateways });
55
- const key = gateway.toString();
56
- selections.set(key, (selections.get(key) || 0) + 1);
57
- }
58
- // each gateway should be selected at least once
59
- for (const gateway of gateways) {
60
- const key = gateway.toString();
61
- strict_1.default.ok(selections.has(key), `Gateway ${key} should be selected at least once`);
62
- }
63
- // no gateway should be selected more than 50% of the time
64
- for (const [key, count] of selections.entries()) {
65
- strict_1.default.ok(count < iterations * 0.5, `Gateway ${key} was selected ${count} times, which is more than 50% of iterations`);
66
- }
67
- });
68
- });
@@ -1,42 +0,0 @@
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;
@@ -1,78 +0,0 @@
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
- /**
7
- * Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
8
- *
9
- * Licensed under the Apache License, Version 2.0 (the "License");
10
- * you may not use this file except in compliance with the License.
11
- * You may obtain a copy of the License at
12
- *
13
- * http://www.apache.org/licenses/LICENSE-2.0
14
- *
15
- * Unless required by applicable law or agreed to in writing, software
16
- * distributed under the License is distributed on an "AS IS" BASIS,
17
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
- * See the License for the specific language governing permissions and
19
- * limitations under the License.
20
- */
21
- const strict_1 = __importDefault(require("node:assert/strict"));
22
- const node_test_1 = require("node:test");
23
- const logger_js_1 = require("../../../../common/logger.js");
24
- const round_robin_js_1 = require("./round-robin.js");
25
- logger_js_1.Logger.default.setLogLevel('none');
26
- (0, node_test_1.describe)('RoundRobinRoutingStrategy', () => {
27
- (0, node_test_1.it)('selects gateways in order and cycles back to the beginning', async () => {
28
- const gateways = [
29
- new URL('https://example1.com'),
30
- new URL('https://example2.com'),
31
- new URL('https://example3.com'),
32
- ];
33
- const strategy = new round_robin_js_1.RoundRobinRoutingStrategy({ gateways });
34
- const selection1 = await strategy.selectGateway();
35
- strict_1.default.equal(selection1.toString(), gateways[0].toString(), 'Should select the first gateway first');
36
- const selection2 = await strategy.selectGateway();
37
- strict_1.default.equal(selection2.toString(), gateways[1].toString(), 'Should select the second gateway second');
38
- const selection3 = await strategy.selectGateway();
39
- strict_1.default.equal(selection3.toString(), gateways[2].toString(), 'Should select the third gateway third');
40
- // should cycle back to the first gateway
41
- const selection4 = await strategy.selectGateway();
42
- strict_1.default.equal(selection4.toString(), gateways[0].toString(), 'Should cycle back to the first gateway');
43
- });
44
- (0, node_test_1.it)('uses the internal list even when a different list is provided', async () => {
45
- const initialGateways = [
46
- new URL('https://example1.com'),
47
- new URL('https://example2.com'),
48
- ];
49
- const newGateways = [
50
- new URL('https://example3.com'),
51
- new URL('https://example4.com'),
52
- ];
53
- const strategy = new round_robin_js_1.RoundRobinRoutingStrategy({
54
- gateways: initialGateways,
55
- });
56
- const selection1 = await strategy.selectGateway({
57
- gateways: newGateways,
58
- });
59
- strict_1.default.equal(selection1.toString(), initialGateways[0].toString(), 'Should use the internal list even when a different list is provided');
60
- const selection2 = await strategy.selectGateway({
61
- // @ts-ignore
62
- gateways: newGateways,
63
- });
64
- strict_1.default.equal(selection2.toString(), initialGateways[1].toString(), 'Should use the internal list even when a different list is provided');
65
- });
66
- (0, node_test_1.it)('handles a single gateway by returning it repeatedly', async () => {
67
- const gateways = [new URL('https://example1.com')];
68
- const strategy = new round_robin_js_1.RoundRobinRoutingStrategy({ gateways });
69
- const selection1 = await strategy.selectGateway({
70
- gateways: [new URL('https://example2.com')],
71
- });
72
- strict_1.default.equal(selection1.toString(), gateways[0].toString(), 'Should return the single gateway');
73
- const selection2 = await strategy.selectGateway({
74
- gateways: [new URL('https://example2.com')],
75
- });
76
- strict_1.default.equal(selection2.toString(), gateways[0].toString(), 'Should return the single gateway again');
77
- });
78
- });
@@ -1,29 +0,0 @@
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;
@@ -1,40 +0,0 @@
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
- /**
7
- * Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
8
- *
9
- * Licensed under the Apache License, Version 2.0 (the "License");
10
- * you may not use this file except in compliance with the License.
11
- * You may obtain a copy of the License at
12
- *
13
- * http://www.apache.org/licenses/LICENSE-2.0
14
- *
15
- * Unless required by applicable law or agreed to in writing, software
16
- * distributed under the License is distributed on an "AS IS" BASIS,
17
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
- * See the License for the specific language governing permissions and
19
- * limitations under the License.
20
- */
21
- const strict_1 = __importDefault(require("node:assert/strict"));
22
- const node_test_1 = require("node:test");
23
- const static_js_1 = require("./static.js");
24
- (0, node_test_1.describe)('StaticRoutingStrategy', () => {
25
- (0, node_test_1.it)('returns the configured gateway regardless of the gateways parameter', async () => {
26
- const staticGateway = 'https://static-example.com/';
27
- const strategy = new static_js_1.StaticRoutingStrategy({
28
- gateway: staticGateway,
29
- });
30
- const result1 = await strategy.selectGateway();
31
- const result2 = await strategy.selectGateway();
32
- const result3 = await strategy.selectGateway();
33
- strict_1.default.equal(result1.toString(), staticGateway, 'Should return the static gateway');
34
- strict_1.default.equal(result2.toString(), staticGateway, 'Should return the static gateway');
35
- strict_1.default.equal(result3.toString(), staticGateway, 'Should return the static gateway even when no gateways are provided');
36
- });
37
- (0, node_test_1.it)('throws an error when an invalid URL is provided', () => {
38
- strict_1.default.throws(() => new static_js_1.StaticRoutingStrategy({ gateway: 'not-a-valid-url' }), /Invalid URL provided for static gateway/, 'Should throw an error when an invalid URL is provided');
39
- });
40
- });
@@ -1,139 +0,0 @@
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.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 DataRootVerificationStrategy {
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.DataRootVerificationStrategy = DataRootVerificationStrategy;
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
@@ -1,50 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HashVerificationStrategy = 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 HashVerificationStrategy {
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.HashVerificationStrategy = HashVerificationStrategy;
@@ -1,106 +0,0 @@
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 returned a hash 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
- */