@ar.io/sdk 3.11.0-alpha.12 → 3.11.0-alpha.14

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/bundles/web.bundle.min.js +120 -123
  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/trusted-gateways.js +0 -106
  22. package/lib/cjs/common/wayfinder/gateways.js +0 -86
  23. package/lib/cjs/common/wayfinder/index.js +0 -44
  24. package/lib/cjs/common/wayfinder/routers/priority.js +0 -29
  25. package/lib/cjs/common/wayfinder/routers/priority.test.js +0 -155
  26. package/lib/cjs/common/wayfinder/routers/random.js +0 -23
  27. package/lib/cjs/common/wayfinder/routers/random.test.js +0 -25
  28. package/lib/cjs/common/wayfinder/routers/simple-cache.js +0 -25
  29. package/lib/cjs/common/wayfinder/routers/simple-cache.test.js +0 -41
  30. package/lib/cjs/common/wayfinder/routers/static.js +0 -14
  31. package/lib/cjs/common/wayfinder/routers/static.test.js +0 -14
  32. package/lib/cjs/common/wayfinder/verification/data-root-verifier.js +0 -139
  33. package/lib/cjs/common/wayfinder/verification/hash-verifier.js +0 -50
  34. package/lib/cjs/common/wayfinder/wayfinder.js +0 -596
  35. package/lib/cjs/common/wayfinder/wayfinder.test.js +0 -500
  36. package/lib/cjs/types/wayfinder.js +0 -3
  37. package/lib/esm/common/wayfinder/gateways/trusted-gateways.js +0 -102
  38. package/lib/esm/common/wayfinder/gateways.js +0 -80
  39. package/lib/esm/common/wayfinder/index.js +0 -28
  40. package/lib/esm/common/wayfinder/routers/priority.js +0 -25
  41. package/lib/esm/common/wayfinder/routers/priority.test.js +0 -153
  42. package/lib/esm/common/wayfinder/routers/random.js +0 -19
  43. package/lib/esm/common/wayfinder/routers/random.test.js +0 -23
  44. package/lib/esm/common/wayfinder/routers/simple-cache.js +0 -21
  45. package/lib/esm/common/wayfinder/routers/simple-cache.test.js +0 -39
  46. package/lib/esm/common/wayfinder/routers/static.js +0 -10
  47. package/lib/esm/common/wayfinder/routers/static.test.js +0 -12
  48. package/lib/esm/common/wayfinder/verification/data-root-verifier.js +0 -130
  49. package/lib/esm/common/wayfinder/verification/hash-verifier.js +0 -46
  50. package/lib/esm/common/wayfinder/wayfinder.js +0 -584
  51. package/lib/esm/common/wayfinder/wayfinder.test.js +0 -495
  52. package/lib/esm/types/wayfinder.js +0 -2
  53. package/lib/types/common/wayfinder/gateways/trusted-gateways.d.ts +0 -51
  54. package/lib/types/common/wayfinder/gateways.d.ts +0 -53
  55. package/lib/types/common/wayfinder/index.d.ts +0 -23
  56. package/lib/types/common/wayfinder/routers/priority.d.ts +0 -29
  57. package/lib/types/common/wayfinder/routers/priority.test.d.ts +0 -1
  58. package/lib/types/common/wayfinder/routers/random.d.ts +0 -27
  59. package/lib/types/common/wayfinder/routers/random.test.d.ts +0 -1
  60. package/lib/types/common/wayfinder/routers/simple-cache.d.ts +0 -28
  61. package/lib/types/common/wayfinder/routers/simple-cache.test.d.ts +0 -1
  62. package/lib/types/common/wayfinder/routers/static.d.ts +0 -24
  63. package/lib/types/common/wayfinder/routers/static.test.d.ts +0 -1
  64. package/lib/types/common/wayfinder/verification/data-root-verifier.d.ts +0 -31
  65. package/lib/types/common/wayfinder/verification/hash-verifier.d.ts +0 -27
  66. package/lib/types/common/wayfinder/wayfinder.d.ts +0 -265
  67. package/lib/types/common/wayfinder/wayfinder.test.d.ts +0 -1
  68. package/lib/types/types/wayfinder.d.ts +0 -62
@@ -1,500 +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
- const axios_1 = __importDefault(require("axios"));
7
- const got_1 = __importDefault(require("got"));
8
- const node_assert_1 = __importDefault(require("node:assert"));
9
- const node_events_1 = require("node:events");
10
- const node_stream_1 = require("node:stream");
11
- const consumers_1 = require("node:stream/consumers");
12
- const node_test_1 = require("node:test");
13
- const logger_js_1 = require("../../common/logger.js");
14
- const random_js_1 = require("./routers/random.js");
15
- const static_js_1 = require("./routers/static.js");
16
- const wayfinder_js_1 = require("./wayfinder.js");
17
- // TODO: replace with locally running gateway
18
- const gatewayUrl = 'permagate.io';
19
- const stubbedGatewaysProvider = {
20
- getGateways: async () => [new URL(`http://${gatewayUrl}`)],
21
- };
22
- logger_js_1.Logger.default.setLogLevel('none');
23
- (0, node_test_1.describe)('Wayfinder', () => {
24
- node_test_1.describe.skip('http wrapper', () => {
25
- (0, node_test_1.describe)('fetch', () => {
26
- let wayfinder;
27
- (0, node_test_1.before)(() => {
28
- wayfinder = new wayfinder_js_1.Wayfinder({
29
- httpClient: fetch,
30
- router: new random_js_1.RandomGatewayRouter({
31
- gatewaysProvider: stubbedGatewaysProvider,
32
- }),
33
- });
34
- });
35
- (0, node_test_1.it)('should fetch the data using the selected gateway', async () => {
36
- const nativeFetch = await fetch(`https://ao.${gatewayUrl}`);
37
- const response = await wayfinder.request('ar://ao');
38
- node_assert_1.default.strictEqual(response.status, 200);
39
- node_assert_1.default.strictEqual(response.status, nativeFetch.status);
40
- // assert the arns headers are the same
41
- const arnsHeaders = Array.from(response.headers.entries()).filter(([key]) => key.startsWith('x-arns-'));
42
- const nativeFetchHeaders = Array.from(nativeFetch.headers.entries()).filter(([key]) => key.startsWith('x-arns-'));
43
- node_assert_1.default.deepStrictEqual(arnsHeaders, nativeFetchHeaders);
44
- });
45
- (0, node_test_1.it)('should fetch a tx id using the selected gateway', async () => {
46
- const nativeFetch = await fetch(`https://${gatewayUrl}/KKmRbIfrc7wiLcG0zvY1etlO0NBx1926dSCksxCIN3A`,
47
- // follow redirects
48
- { redirect: 'follow' });
49
- const response = await wayfinder.request('ar://KKmRbIfrc7wiLcG0zvY1etlO0NBx1926dSCksxCIN3A', { redirect: 'follow' });
50
- node_assert_1.default.strictEqual(response.status, 200);
51
- node_assert_1.default.strictEqual(response.status, nativeFetch.status);
52
- });
53
- (0, node_test_1.it)('should route a non-ar:// url as a normal fetch', async () => {
54
- const [nativeFetch, response] = await Promise.all([
55
- fetch(`https://${gatewayUrl}/`, {
56
- method: 'HEAD',
57
- }),
58
- wayfinder.request(`https://${gatewayUrl}/`, {
59
- method: 'HEAD',
60
- }),
61
- ]);
62
- node_assert_1.default.strictEqual(response.status, 200);
63
- node_assert_1.default.strictEqual(response.status, nativeFetch.status);
64
- // TODO: ensure the headers are the same excluding unique headers
65
- });
66
- for (const api of ['/info', '/block/current']) {
67
- (0, node_test_1.it)(`supports native arweave node apis ${api}`, async () => {
68
- const [nativeFetch, response] = await Promise.all([
69
- fetch(`https://${gatewayUrl}${api}`),
70
- wayfinder.request(`ar://${api}`),
71
- ]);
72
- node_assert_1.default.strictEqual(response.status, 200);
73
- node_assert_1.default.strictEqual(response.status, nativeFetch.status);
74
- // TODO: ensure the headers are the same excluding unique headers
75
- });
76
- }
77
- for (const api of ['/ar-io/info']) {
78
- (0, node_test_1.it)(`supports native ario node gateway apis ${api}`, async () => {
79
- const [nativeFetch, response] = await Promise.all([
80
- fetch(`https://${gatewayUrl}${api}`),
81
- wayfinder.request(`ar:///${api}`),
82
- ]);
83
- node_assert_1.default.strictEqual(response.status, 200);
84
- node_assert_1.default.strictEqual(response.status, nativeFetch.status);
85
- // TODO: ensure the headers are the same excluding unique headers
86
- });
87
- }
88
- (0, node_test_1.it)('supports a post request to graphql', async () => {
89
- const response = await wayfinder.request('ar:///graphql', {
90
- method: 'POST',
91
- headers: {
92
- 'Content-Type': 'application/json',
93
- },
94
- body: JSON.stringify({
95
- query: `
96
- query {
97
- transactions(
98
- ids: ["xf958qhCNGfDme1FtoiD6DtMfDENDbtxZpjOM_1tsMM"]
99
- ) {
100
- edges {
101
- cursor
102
- node {
103
- id
104
- tags {
105
- name
106
- value
107
- }
108
- block {
109
- height
110
- timestamp
111
- }
112
- }
113
- }
114
- pageInfo {
115
- hasNextPage
116
- }
117
- }
118
- }
119
- `,
120
- }),
121
- });
122
- node_assert_1.default.strictEqual(response.status, 200);
123
- });
124
- node_test_1.it.skip('returns the error from the target gateway if the route is not found', async () => {
125
- const [nativeFetch, response] = await Promise.all([
126
- fetch(`https://${gatewayUrl}/ar-io/not-found`),
127
- wayfinder.request('ar:///not-found'),
128
- ]);
129
- node_assert_1.default.strictEqual(response.status, nativeFetch.status);
130
- node_assert_1.default.strictEqual(response.statusText, nativeFetch.statusText);
131
- });
132
- });
133
- (0, node_test_1.describe)('axios', () => {
134
- let wayfinder;
135
- (0, node_test_1.before)(() => {
136
- wayfinder = new wayfinder_js_1.Wayfinder({
137
- httpClient: axios_1.default,
138
- router: new random_js_1.RandomGatewayRouter({
139
- gatewaysProvider: stubbedGatewaysProvider,
140
- }),
141
- });
142
- });
143
- (0, node_test_1.it)('should fetch the data using axios default function against the target gateway', async () => {
144
- const [nativeAxios, response] = await Promise.all([
145
- (0, axios_1.default)(`https://ao.${gatewayUrl}`),
146
- wayfinder.request('ar://ao'),
147
- ]);
148
- node_assert_1.default.strictEqual(response.status, 200);
149
- node_assert_1.default.strictEqual(response.status, nativeAxios.status);
150
- // assert the arns headers are the same
151
- const arnsHeaders = Object.entries(response.headers)
152
- .sort()
153
- .filter(([key]) => key.startsWith('x-arns-'));
154
- const nativeAxiosHeaders = Object.entries(nativeAxios.headers).filter(([key]) => key.startsWith('x-arns-'));
155
- node_assert_1.default.deepStrictEqual(arnsHeaders.sort(), nativeAxiosHeaders.sort());
156
- });
157
- (0, node_test_1.it)('should fetch the data using the axios.get method against the target gateway', async () => {
158
- const [nativeAxios, response] = await Promise.all([
159
- axios_1.default.get(`https://ao.${gatewayUrl}`),
160
- wayfinder.request.get('ar://ao'),
161
- ]);
162
- node_assert_1.default.strictEqual(response.status, 200);
163
- node_assert_1.default.strictEqual(response.status, nativeAxios.status);
164
- // assert the arns headers are the same
165
- const arnsHeaders = Object.entries(response.headers)
166
- .sort()
167
- .filter(([key]) => key.startsWith('x-arns-'));
168
- const nativeAxiosHeaders = Object.entries(nativeAxios.headers).filter(([key]) => key.startsWith('x-arns-'));
169
- node_assert_1.default.deepStrictEqual(arnsHeaders.sort(), nativeAxiosHeaders.sort());
170
- });
171
- (0, node_test_1.it)('should route a non-ar:// url as a normal axios request', async () => {
172
- const [nativeAxios, response] = await Promise.all([
173
- (0, axios_1.default)(`https://${gatewayUrl}/`),
174
- wayfinder.request(`https://${gatewayUrl}/`),
175
- ]);
176
- node_assert_1.default.strictEqual(response.status, 200);
177
- node_assert_1.default.strictEqual(response.status, nativeAxios.status);
178
- // TODO: ensure the headers are the same excluding unique headers
179
- });
180
- for (const api of ['/info', '/block/current']) {
181
- (0, node_test_1.it)(`supports native arweave node apis ${api}`, async () => {
182
- const [nativeAxios, response] = await Promise.all([
183
- (0, axios_1.default)(`https://${gatewayUrl}${api}`),
184
- wayfinder.request(`ar://${api}`),
185
- ]);
186
- node_assert_1.default.strictEqual(response.status, 200);
187
- node_assert_1.default.strictEqual(response.status, nativeAxios.status);
188
- // TODO: ensure the headers are the same excluding unique headers
189
- });
190
- }
191
- for (const api of ['/ar-io/info', '/ar-io/__gateway_metrics']) {
192
- (0, node_test_1.it)(`supports native ario node gateway apis ${api}`, async () => {
193
- const [nativeAxios, response] = await Promise.all([
194
- (0, axios_1.default)(`https://${gatewayUrl}${api}`),
195
- wayfinder.request(`ar:///${api}`),
196
- ]);
197
- node_assert_1.default.strictEqual(response.status, 200);
198
- node_assert_1.default.strictEqual(response.status, nativeAxios.status);
199
- // TODO: ensure the headers are the same excluding unique headers
200
- });
201
- }
202
- node_test_1.it.skip('should return the error from the target gateway if the route is not found', async () => {
203
- const axiosInstance = axios_1.default.create({
204
- validateStatus: () => true, // don't throw so we can compare axios result with wrapped axios result
205
- });
206
- const wayfinder = new wayfinder_js_1.Wayfinder({
207
- httpClient: axiosInstance,
208
- router: new random_js_1.RandomGatewayRouter({
209
- gatewaysProvider: stubbedGatewaysProvider,
210
- }),
211
- });
212
- const [nativeAxios, response] = await Promise.all([
213
- axiosInstance(`https://${gatewayUrl}/ar-io/not-found`),
214
- wayfinder.request('ar:///not-found'),
215
- ]);
216
- node_assert_1.default.strictEqual(response.status, nativeAxios.status);
217
- });
218
- });
219
- (0, node_test_1.describe)('got', () => {
220
- let wayfinder;
221
- (0, node_test_1.before)(() => {
222
- wayfinder = new wayfinder_js_1.Wayfinder({
223
- httpClient: got_1.default,
224
- router: new random_js_1.RandomGatewayRouter({
225
- gatewaysProvider: stubbedGatewaysProvider,
226
- }),
227
- });
228
- });
229
- (0, node_test_1.it)('should fetch the data using the got default function against the target gateway', async () => {
230
- const [nativeGot, response] = await Promise.all([
231
- (0, got_1.default)(`https://ao.${gatewayUrl}`),
232
- wayfinder.request('ar://ao'),
233
- ]);
234
- node_assert_1.default.strictEqual(response.statusCode, 200);
235
- node_assert_1.default.strictEqual(response.statusCode, nativeGot.statusCode);
236
- node_assert_1.default.deepStrictEqual(response.body, nativeGot.body);
237
- });
238
- (0, node_test_1.it)('should stream the data using got.stream against the selected target gateway', async () => {
239
- const nativeBuffer = await (0, consumers_1.buffer)(await got_1.default.stream(`https://ao.${gatewayUrl}`, { decompress: false }));
240
- const wayfinderBuffer = await (0, consumers_1.buffer)(await wayfinder.request.stream('ar://ao', { decompress: false }));
241
- node_assert_1.default.deepStrictEqual(wayfinderBuffer, nativeBuffer);
242
- });
243
- });
244
- });
245
- (0, node_test_1.describe)('events', () => {
246
- (0, node_test_1.it)('should emit events on the wayfinder event emitter', async () => {
247
- const wayfinder = new wayfinder_js_1.Wayfinder({
248
- httpClient: fetch,
249
- router: new static_js_1.StaticGatewayRouter({
250
- gateway: `http://${gatewayUrl}`,
251
- }),
252
- verifier: {
253
- // @ts-expect-error
254
- verifyData: async (params) => {
255
- return;
256
- },
257
- },
258
- });
259
- const events = [];
260
- wayfinder.emitter.on('verification-failed', (event) => {
261
- events.push({ type: 'verification-failed', ...event });
262
- });
263
- wayfinder.emitter.on('verification-progress', (event) => {
264
- events.push({ type: 'verification-progress', ...event });
265
- });
266
- wayfinder.emitter.on('verification-passed', (event) => {
267
- events.push({ type: 'verification-passed', ...event });
268
- });
269
- // request data and assert the event is emitted
270
- const response = await wayfinder.request('ar://c7wkwt6TKgcWJUfgvpJ5q5qi4DIZyJ1_TqhjXgURh0U', {
271
- redirect: 'follow',
272
- });
273
- // read the full response body to ensure the stream is fully consumed
274
- await response.text();
275
- node_assert_1.default.strictEqual(response.status, 200);
276
- node_assert_1.default.ok(events.find((e) => e.type === 'verification-passed'), 'Should emit at least one verification-passed');
277
- });
278
- (0, node_test_1.it)('should execute callbacks provided to the wayfinder constructor', async () => {
279
- let verificationFailed = false;
280
- let verificationProgress = false;
281
- let verificationPassed = false;
282
- const wayfinder = new wayfinder_js_1.Wayfinder({
283
- httpClient: fetch,
284
- router: new static_js_1.StaticGatewayRouter({
285
- gateway: `http://${gatewayUrl}`,
286
- }),
287
- events: {
288
- onVerificationFailed: () => {
289
- verificationFailed = true;
290
- },
291
- onVerificationProgress: () => {
292
- verificationProgress = true;
293
- },
294
- onVerificationPassed: () => {
295
- verificationPassed = true;
296
- },
297
- },
298
- });
299
- const response = await wayfinder.request('ar://c7wkwt6TKgcWJUfgvpJ5q5qi4DIZyJ1_TqhjXgURh0U', {
300
- redirect: 'follow',
301
- });
302
- // read the full response body to ensure the stream is fully consumed
303
- await response.text();
304
- node_assert_1.default.strictEqual(response.status, 200);
305
- node_assert_1.default.ok(verificationFailed === false, 'Should not emit verification-failed');
306
- node_assert_1.default.ok(verificationProgress, 'Should emit verification-progress');
307
- node_assert_1.default.ok(verificationPassed, 'Should emit verification-passed');
308
- });
309
- });
310
- node_test_1.describe.skip('tapAndVerifyRequest', () => {
311
- (0, node_test_1.describe)('Readable', () => {
312
- (0, node_test_1.it)('should duplicate the stream, verify the first and return the second if verification passes', async () => {
313
- // create a simple readable
314
- const chunks = [
315
- Buffer.from('foo'),
316
- Buffer.from('bar'),
317
- Buffer.from('baz'),
318
- ];
319
- const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
320
- // a stream that will emit chunks
321
- const originalStream = node_stream_1.Readable.from(chunks);
322
- let seen = Buffer.alloc(0);
323
- const verifyData = async ({ data,
324
- // @ts-expect-error
325
- txId, }) => {
326
- return new Promise((resolve, reject) => {
327
- data.on('data', (chunk) => {
328
- seen = Buffer.concat([seen, chunk]);
329
- });
330
- data.on('end', () => {
331
- // Should have seen exactly the full payload
332
- node_assert_1.default.strictEqual(seen.length, contentLength);
333
- resolve();
334
- });
335
- data.on('error', reject);
336
- });
337
- };
338
- const txId = 'test-tx-1';
339
- const emitter = new node_events_1.EventEmitter();
340
- const events = [];
341
- emitter.on('verification-progress', (e) => {
342
- console.log('verification-progress', e);
343
- events.push({ type: 'verification-progress', ...e });
344
- });
345
- emitter.on('verification-passed', (e) => events.push({ type: 'verification-passed', ...e }));
346
- // tap with verification
347
- const tapped = (0, wayfinder_js_1.tapAndVerifyStream)({
348
- originalStream,
349
- contentLength,
350
- verifyData,
351
- txId,
352
- emitter,
353
- });
354
- // read the stream
355
- const out = [];
356
- for await (const chunk of tapped) {
357
- out.push(chunk);
358
- }
359
- // assert the stream is the same
360
- node_assert_1.default.strictEqual(Buffer.concat(out).toString(), Buffer.concat(chunks).toString(), 'The tapped stream should emit exactly the original data');
361
- node_assert_1.default.ok(events.find((e) => e.type === 'verification-progress'), 'Should emit at least one verification-progress');
362
- node_assert_1.default.ok(events.find((e) => e.type === 'verification-passed' && e.txId === txId), 'Should emit at least one verification-passed');
363
- });
364
- (0, node_test_1.it)('should throw an error on the client stream if verification fails', async () => {
365
- const chunks = [
366
- Buffer.from('foo'),
367
- Buffer.from('bar'),
368
- Buffer.from('baz'),
369
- ];
370
- const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
371
- // a stream that will emit chunks
372
- const originalStream = node_stream_1.Readable.from(chunks);
373
- const verifyData = async ({
374
- // @ts-expect-error
375
- data, txId, }) => {
376
- throw new Error('Verification failed for txId: ' + txId);
377
- };
378
- const txId = 'test-tx-1';
379
- const emitter = new node_events_1.EventEmitter();
380
- const events = [];
381
- emitter.on('verification-progress', (e) => events.push({ type: 'verification-progress', ...e }));
382
- emitter.on('verification-failed', (e) => events.push({ type: 'verification-failed', ...e }));
383
- // tap with verification
384
- const tapped = (0, wayfinder_js_1.tapAndVerifyStream)({
385
- originalStream,
386
- contentLength,
387
- verifyData,
388
- txId,
389
- emitter,
390
- });
391
- // read the stream
392
- try {
393
- const out = [];
394
- for await (const chunk of tapped) {
395
- out.push(chunk);
396
- }
397
- }
398
- catch (error) {
399
- node_assert_1.default.ok(events.find((e) => e.type === 'verification-failed' && e.txId === txId), 'Should emit at least one verification-failed');
400
- // stream should be closed
401
- node_assert_1.default.ok(tapped.closed);
402
- }
403
- });
404
- });
405
- (0, node_test_1.describe)('ReadableStream', () => {
406
- (0, node_test_1.it)('should duplicate the ReadableStream, verify the first and return the second if verification passes', async () => {
407
- // create a simple readable
408
- const chunks = [
409
- Buffer.from('foo'),
410
- Buffer.from('bar'),
411
- Buffer.from('baz'),
412
- ];
413
- const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
414
- // a stream that will emit chunks
415
- const originalStream = ReadableStream.from(chunks);
416
- let seen = Buffer.alloc(0);
417
- const verifyData = async ({ data,
418
- // @ts-expect-error
419
- txId, }) => {
420
- return new Promise(async (resolve, reject) => {
421
- const reader = data.getReader();
422
- while (true) {
423
- try {
424
- const { done, value } = await reader.read();
425
- if (done) {
426
- resolve();
427
- break;
428
- }
429
- seen = Buffer.concat([seen, value]);
430
- }
431
- catch (error) {
432
- reject(error);
433
- }
434
- }
435
- });
436
- };
437
- const txId = 'test-tx-1';
438
- const emitter = new node_events_1.EventEmitter();
439
- const events = [];
440
- emitter.on('verification-progress', (e) => events.push({ type: 'verification-progress', ...e }));
441
- emitter.on('verification-passed', (e) => events.push({ type: 'verification-passed', ...e }));
442
- // tap with verification
443
- const tapped = (0, wayfinder_js_1.tapAndVerifyStream)({
444
- originalStream,
445
- contentLength,
446
- verifyData,
447
- txId,
448
- emitter,
449
- });
450
- // read the stream
451
- const out = [];
452
- for await (const chunk of tapped) {
453
- out.push(chunk);
454
- }
455
- // assert the stream is the same
456
- node_assert_1.default.strictEqual(Buffer.concat(out).toString(), Buffer.concat(chunks).toString(), 'The tapped stream should emit exactly the original data');
457
- node_assert_1.default.ok(events.find((e) => e.type === 'verification-progress'), 'Should emit at least one verification-progress');
458
- node_assert_1.default.ok(events.find((e) => e.type === 'verification-passed' && e.txId === txId), 'Should emit at least one verification-passed');
459
- });
460
- (0, node_test_1.it)('should throw an error on the client stream if verification fails', async () => {
461
- const chunks = [
462
- Buffer.from('foo'),
463
- Buffer.from('bar'),
464
- Buffer.from('baz'),
465
- ];
466
- const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
467
- // a stream that will emit chunks
468
- const originalStream = ReadableStream.from(chunks);
469
- const verifyData = async ({
470
- // @ts-expect-error
471
- data, txId, }) => {
472
- throw new Error('Verification failed for txId: ' + txId);
473
- };
474
- const txId = 'test-tx-1';
475
- const emitter = new node_events_1.EventEmitter();
476
- const events = [];
477
- emitter.on('verification-progress', (e) => events.push({ type: 'verification-progress', ...e }));
478
- emitter.on('verification-failed', (e) => events.push({ type: 'verification-failed', ...e }));
479
- // tap with verification
480
- const tapped = (0, wayfinder_js_1.tapAndVerifyStream)({
481
- originalStream,
482
- contentLength,
483
- verifyData,
484
- txId,
485
- emitter,
486
- });
487
- // read the stream
488
- try {
489
- const out = [];
490
- for await (const chunk of tapped) {
491
- out.push(chunk);
492
- }
493
- }
494
- catch (error) {
495
- node_assert_1.default.ok(events.find((e) => e.type === 'verification-failed' && e.txId === txId), 'Should emit at least one verification-failed');
496
- }
497
- });
498
- });
499
- });
500
- });
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- // TODO: add an offset provider that returns offsets for data items so we can use them to verify the signatures of a data item within a bundle
@@ -1,102 +0,0 @@
1
- const arioGatewayHeaders = {
2
- digest: 'x-ar-io-digest',
3
- verified: 'x-ar-io-verified',
4
- txId: 'x-arns-resolved-tx-id',
5
- processId: 'x-arns-resolved-process-id',
6
- };
7
- export class TrustedGatewaysHashProvider {
8
- gatewaysProvider;
9
- constructor({ gatewaysProvider,
10
- // TODO: add threshold for allowed hash difference (i.e. by count or ratio of total gateways checked)
11
- }) {
12
- this.gatewaysProvider = gatewaysProvider;
13
- }
14
- /**
15
- * Gets the digest for a given txId from all trusted gateways and ensures they all match.
16
- * @param txId - The txId to get the digest for.
17
- * @returns The digest for the given txId.
18
- */
19
- async getHash({ txId, }) {
20
- // get the hash from every gateway, and ensure they all match
21
- const hashSet = new Set();
22
- const hashResults = [];
23
- const gateways = await this.gatewaysProvider.getGateways();
24
- const hashes = await Promise.all(gateways.map(async (gateway) => {
25
- const response = await fetch(`${gateway.toString()}${txId}`, {
26
- method: 'HEAD',
27
- redirect: 'follow',
28
- });
29
- if (!response.ok) {
30
- // skip this gateway
31
- return undefined;
32
- }
33
- const txIdHash = response.headers.get(arioGatewayHeaders.digest);
34
- if (txIdHash === null || txIdHash === undefined) {
35
- // skip this gateway
36
- return undefined;
37
- }
38
- hashResults.push({
39
- gateway: gateway.hostname,
40
- txIdHash,
41
- });
42
- return txIdHash;
43
- }));
44
- for (const hash of hashes) {
45
- if (hash !== undefined) {
46
- hashSet.add(hash);
47
- }
48
- }
49
- if (hashSet.size === 0) {
50
- throw new Error(`No trusted gateways returned a hash for txId ${txId}`);
51
- }
52
- if (hashSet.size > 1) {
53
- throw new Error(`Failed to get consistent hash from all trusted gateways. ${JSON.stringify(hashResults)}`);
54
- }
55
- return { hash: hashResults[0].txIdHash, algorithm: 'sha256' };
56
- }
57
- /**
58
- * Get the data root for a given txId from all trusted gateways and ensure they all match.
59
- * @param txId - The txId to get the data root for.
60
- * @returns The data root for the given txId.
61
- */
62
- async getDataRoot({ txId }) {
63
- const dataRootSet = new Set();
64
- const dataRootResults = [];
65
- const gateways = await this.gatewaysProvider.getGateways();
66
- const dataRoots = await Promise.all(gateways.map(async (gateway) => {
67
- const response = await fetch(`${gateway.toString()}tx/${txId}/data_root`);
68
- if (!response.ok) {
69
- // skip this gateway
70
- return undefined;
71
- }
72
- const dataRoot = await response.text();
73
- dataRootResults.push({
74
- gateway: gateway.hostname,
75
- dataRoot,
76
- });
77
- return dataRoot;
78
- }));
79
- for (const dataRoot of dataRoots) {
80
- if (dataRoot !== undefined) {
81
- dataRootSet.add(dataRoot);
82
- }
83
- }
84
- if (dataRootSet.size > 1) {
85
- throw new Error(`Failed to get consistent data root from all trusted gateways. ${JSON.stringify(dataRootResults)}`);
86
- }
87
- return dataRootSet.values().next().value;
88
- }
89
- }
90
- // client could check hashes of data items, match expected hash
91
- // if the gateway has the hash and they've verified it, you can trust the data item and offset
92
- // you would be only trusting the gateway that it is a valid bundle
93
- // you can request the offset from the gateway to verify the id
94
- /**
95
- * Note from @djwhitt
96
- *
97
- * Calculating data roots this way is fine, but it may not reproduce the original data root.
98
- * We could also implement a data root verifier that pulls all the chunks, checks that they
99
- * reproduce the expected data root, and then compares the concatenated chunk data to the
100
- * original data retrieved. That would take a while, but it should be able to verify any L1
101
- * data where we can find the chunks.
102
- */