@ar.io/sdk 3.11.0-alpha.1 → 3.11.0-alpha.10

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 (64) hide show
  1. package/README.md +52 -0
  2. package/bundles/web.bundle.min.js +119 -116
  3. package/lib/cjs/cli/cli.js +137 -122
  4. package/lib/cjs/cli/commands/readCommands.js +6 -0
  5. package/lib/cjs/cli/utils.js +8 -1
  6. package/lib/cjs/common/ant.js +5 -5
  7. package/lib/cjs/common/io.js +49 -3
  8. package/lib/cjs/common/wayfinder/gateways/trusted-gateways.js +106 -0
  9. package/lib/cjs/common/wayfinder/gateways.js +19 -8
  10. package/lib/cjs/common/wayfinder/index.js +9 -1
  11. package/lib/cjs/common/wayfinder/routers/priority.js +11 -20
  12. package/lib/cjs/common/wayfinder/routers/priority.test.js +5 -5
  13. package/lib/cjs/common/wayfinder/routers/random.js +2 -2
  14. package/lib/cjs/common/wayfinder/routers/random.test.js +5 -84
  15. package/lib/cjs/common/wayfinder/routers/{fixed.js → static.js} +5 -5
  16. package/lib/cjs/common/wayfinder/routers/{fixed.test.js → static.test.js} +4 -4
  17. package/lib/cjs/common/wayfinder/verification/data-root-verifier.js +139 -0
  18. package/lib/cjs/common/wayfinder/verification/hash-verifier.js +50 -0
  19. package/lib/cjs/common/wayfinder/wayfinder.js +459 -30
  20. package/lib/cjs/common/wayfinder/wayfinder.test.js +296 -49
  21. package/lib/cjs/types/wayfinder.js +1 -0
  22. package/lib/cjs/utils/ao.js +37 -0
  23. package/lib/cjs/utils/arweave.js +1 -1
  24. package/lib/cjs/utils/hash.js +56 -0
  25. package/lib/cjs/version.js +1 -1
  26. package/lib/esm/cli/cli.js +138 -123
  27. package/lib/esm/cli/commands/readCommands.js +5 -0
  28. package/lib/esm/cli/utils.js +8 -1
  29. package/lib/esm/common/ant.js +5 -5
  30. package/lib/esm/common/io.js +49 -3
  31. package/lib/esm/common/wayfinder/gateways/trusted-gateways.js +102 -0
  32. package/lib/esm/common/wayfinder/gateways.js +17 -6
  33. package/lib/esm/common/wayfinder/index.js +9 -1
  34. package/lib/esm/common/wayfinder/routers/priority.js +11 -20
  35. package/lib/esm/common/wayfinder/routers/priority.test.js +5 -5
  36. package/lib/esm/common/wayfinder/routers/random.js +2 -2
  37. package/lib/esm/common/wayfinder/routers/random.test.js +5 -84
  38. package/lib/esm/common/wayfinder/routers/{fixed.js → static.js} +3 -3
  39. package/lib/esm/common/wayfinder/routers/{fixed.test.js → static.test.js} +4 -4
  40. package/lib/esm/common/wayfinder/verification/data-root-verifier.js +130 -0
  41. package/lib/esm/common/wayfinder/verification/hash-verifier.js +46 -0
  42. package/lib/esm/common/wayfinder/wayfinder.js +453 -30
  43. package/lib/esm/common/wayfinder/wayfinder.test.js +297 -50
  44. package/lib/esm/types/wayfinder.js +1 -0
  45. package/lib/esm/utils/ao.js +37 -0
  46. package/lib/esm/utils/arweave.js +1 -1
  47. package/lib/esm/utils/hash.js +50 -0
  48. package/lib/esm/version.js +1 -1
  49. package/lib/types/cli/commands/readCommands.d.ts +1 -0
  50. package/lib/types/common/io.d.ts +7 -8
  51. package/lib/types/common/wayfinder/gateways/trusted-gateways.d.ts +51 -0
  52. package/lib/types/common/wayfinder/gateways.d.ts +16 -7
  53. package/lib/types/common/wayfinder/index.d.ts +5 -1
  54. package/lib/types/common/wayfinder/routers/priority.d.ts +8 -12
  55. package/lib/types/common/wayfinder/routers/{fixed.d.ts → static.d.ts} +3 -3
  56. package/lib/types/common/wayfinder/verification/data-root-verifier.d.ts +31 -0
  57. package/lib/types/common/wayfinder/verification/hash-verifier.d.ts +27 -0
  58. package/lib/types/common/wayfinder/wayfinder.d.ts +169 -17
  59. package/lib/types/types/io.d.ts +16 -1
  60. package/lib/types/types/wayfinder.d.ts +43 -0
  61. package/lib/types/utils/hash.d.ts +4 -0
  62. package/lib/types/version.d.ts +1 -1
  63. package/package.json +1 -1
  64. /package/lib/types/common/wayfinder/routers/{fixed.test.d.ts → static.test.d.ts} +0 -0
@@ -1,27 +1,22 @@
1
1
  import axios from 'axios';
2
2
  import got from 'got';
3
3
  import assert from 'node:assert';
4
+ import { EventEmitter } from 'node:events';
5
+ import { Readable } from 'node:stream';
4
6
  import { buffer } from 'node:stream/consumers';
5
7
  import { before, describe, it } from 'node:test';
8
+ import { Logger } from '../../common/logger.js';
6
9
  import { RandomGatewayRouter } from './routers/random.js';
7
- import { Wayfinder } from './wayfinder.js';
8
- const stubbedGateway = {
9
- status: 'joined',
10
- gatewayAddress: 'arweave',
11
- operatorStake: 1000,
12
- totalDelegatedStake: 1000,
13
- startTimestamp: 1000,
14
- settings: {
15
- protocol: 'https',
16
- fqdn: 'arweave.net',
17
- port: 443,
18
- },
19
- };
10
+ import { StaticGatewayRouter } from './routers/static.js';
11
+ import { Wayfinder, tapAndVerifyStream } from './wayfinder.js';
12
+ // TODO: replace with locally running gateway
13
+ const gatewayUrl = 'permagate.io';
20
14
  const stubbedGatewaysProvider = {
21
- getGateways: async () => [stubbedGateway],
15
+ getGateways: async () => [new URL(`http://${gatewayUrl}`)],
22
16
  };
17
+ Logger.default.setLogLevel('none');
23
18
  describe('Wayfinder', () => {
24
- describe('http wrapper', () => {
19
+ describe.skip('http wrapper', () => {
25
20
  describe('fetch', () => {
26
21
  let wayfinder;
27
22
  before(() => {
@@ -33,7 +28,7 @@ describe('Wayfinder', () => {
33
28
  });
34
29
  });
35
30
  it('should fetch the data using the selected gateway', async () => {
36
- const nativeFetch = await fetch('https://ao.arweave.net');
31
+ const nativeFetch = await fetch(`https://ao.${gatewayUrl}`);
37
32
  const response = await wayfinder.request('ar://ao');
38
33
  assert.strictEqual(response.status, 200);
39
34
  assert.strictEqual(response.status, nativeFetch.status);
@@ -41,44 +36,48 @@ describe('Wayfinder', () => {
41
36
  const arnsHeaders = Array.from(response.headers.entries()).filter(([key]) => key.startsWith('x-arns-'));
42
37
  const nativeFetchHeaders = Array.from(nativeFetch.headers.entries()).filter(([key]) => key.startsWith('x-arns-'));
43
38
  assert.deepStrictEqual(arnsHeaders, nativeFetchHeaders);
44
- assert.deepStrictEqual(await response.text(), await nativeFetch.text());
39
+ });
40
+ it('should fetch a tx id using the selected gateway', async () => {
41
+ const nativeFetch = await fetch(`https://${gatewayUrl}/KKmRbIfrc7wiLcG0zvY1etlO0NBx1926dSCksxCIN3A`,
42
+ // follow redirects
43
+ { redirect: 'follow' });
44
+ const response = await wayfinder.request('ar://KKmRbIfrc7wiLcG0zvY1etlO0NBx1926dSCksxCIN3A', { redirect: 'follow' });
45
+ assert.strictEqual(response.status, 200);
46
+ assert.strictEqual(response.status, nativeFetch.status);
45
47
  });
46
48
  it('should route a non-ar:// url as a normal fetch', async () => {
47
49
  const [nativeFetch, response] = await Promise.all([
48
- fetch('https://arweave.net/', {
50
+ fetch(`https://${gatewayUrl}/`, {
49
51
  method: 'HEAD',
50
52
  }),
51
- wayfinder.request('https://arweave.net/', {
53
+ wayfinder.request(`https://${gatewayUrl}/`, {
52
54
  method: 'HEAD',
53
55
  }),
54
56
  ]);
55
57
  assert.strictEqual(response.status, 200);
56
58
  assert.strictEqual(response.status, nativeFetch.status);
57
59
  // TODO: ensure the headers are the same excluding unique headers
58
- assert.deepStrictEqual(await response.text(), await nativeFetch.text());
59
60
  });
60
- for (const api of ['/info', '/metrics', '/block/current']) {
61
+ for (const api of ['/info', '/block/current']) {
61
62
  it(`supports native arweave node apis ${api}`, async () => {
62
63
  const [nativeFetch, response] = await Promise.all([
63
- fetch(`https://arweave.net${api}`),
64
- wayfinder.request(`ar:///${api}`),
64
+ fetch(`https://${gatewayUrl}${api}`),
65
+ wayfinder.request(`ar://${api}`),
65
66
  ]);
66
67
  assert.strictEqual(response.status, 200);
67
68
  assert.strictEqual(response.status, nativeFetch.status);
68
69
  // TODO: ensure the headers are the same excluding unique headers
69
- assert.deepStrictEqual(await response.text(), await nativeFetch.text());
70
70
  });
71
71
  }
72
- for (const api of ['/ar-io/info', '/ar-io/__gateway_metrics']) {
72
+ for (const api of ['/ar-io/info']) {
73
73
  it(`supports native ario node gateway apis ${api}`, async () => {
74
74
  const [nativeFetch, response] = await Promise.all([
75
- fetch(`https://arweave.net${api}`),
75
+ fetch(`https://${gatewayUrl}${api}`),
76
76
  wayfinder.request(`ar:///${api}`),
77
77
  ]);
78
78
  assert.strictEqual(response.status, 200);
79
79
  assert.strictEqual(response.status, nativeFetch.status);
80
80
  // TODO: ensure the headers are the same excluding unique headers
81
- assert.deepStrictEqual(await response.text(), await nativeFetch.text());
82
81
  });
83
82
  }
84
83
  it('supports a post request to graphql', async () => {
@@ -117,14 +116,13 @@ describe('Wayfinder', () => {
117
116
  });
118
117
  assert.strictEqual(response.status, 200);
119
118
  });
120
- it('returns the error from the target gateway if the route is not found', async () => {
119
+ it.skip('returns the error from the target gateway if the route is not found', async () => {
121
120
  const [nativeFetch, response] = await Promise.all([
122
- fetch('https://arweave.net/not-found'),
123
- wayfinder.request('https://arweave.net/not-found'),
121
+ fetch(`https://${gatewayUrl}/ar-io/not-found`),
122
+ wayfinder.request('ar:///not-found'),
124
123
  ]);
125
124
  assert.strictEqual(response.status, nativeFetch.status);
126
125
  assert.strictEqual(response.statusText, nativeFetch.statusText);
127
- assert.deepStrictEqual(await response.text(), await nativeFetch.text());
128
126
  });
129
127
  });
130
128
  describe('axios', () => {
@@ -139,7 +137,7 @@ describe('Wayfinder', () => {
139
137
  });
140
138
  it('should fetch the data using axios default function against the target gateway', async () => {
141
139
  const [nativeAxios, response] = await Promise.all([
142
- axios('https://ao.arweave.net'),
140
+ axios(`https://ao.${gatewayUrl}`),
143
141
  wayfinder.request('ar://ao'),
144
142
  ]);
145
143
  assert.strictEqual(response.status, 200);
@@ -150,11 +148,10 @@ describe('Wayfinder', () => {
150
148
  .filter(([key]) => key.startsWith('x-arns-'));
151
149
  const nativeAxiosHeaders = Object.entries(nativeAxios.headers).filter(([key]) => key.startsWith('x-arns-'));
152
150
  assert.deepStrictEqual(arnsHeaders.sort(), nativeAxiosHeaders.sort());
153
- assert.deepStrictEqual(response.data, nativeAxios.data);
154
151
  });
155
152
  it('should fetch the data using the axios.get method against the target gateway', async () => {
156
153
  const [nativeAxios, response] = await Promise.all([
157
- axios.get('https://ao.arweave.net'),
154
+ axios.get(`https://ao.${gatewayUrl}`),
158
155
  wayfinder.request.get('ar://ao'),
159
156
  ]);
160
157
  assert.strictEqual(response.status, 200);
@@ -165,43 +162,39 @@ describe('Wayfinder', () => {
165
162
  .filter(([key]) => key.startsWith('x-arns-'));
166
163
  const nativeAxiosHeaders = Object.entries(nativeAxios.headers).filter(([key]) => key.startsWith('x-arns-'));
167
164
  assert.deepStrictEqual(arnsHeaders.sort(), nativeAxiosHeaders.sort());
168
- assert.deepStrictEqual(response.data, nativeAxios.data);
169
165
  });
170
166
  it('should route a non-ar:// url as a normal axios request', async () => {
171
167
  const [nativeAxios, response] = await Promise.all([
172
- axios('https://arweave.net/'),
173
- wayfinder.request('https://arweave.net/'),
168
+ axios(`https://${gatewayUrl}/`),
169
+ wayfinder.request(`https://${gatewayUrl}/`),
174
170
  ]);
175
171
  assert.strictEqual(response.status, 200);
176
172
  assert.strictEqual(response.status, nativeAxios.status);
177
- assert.deepStrictEqual(response.data, nativeAxios.data);
178
173
  // TODO: ensure the headers are the same excluding unique headers
179
174
  });
180
- for (const api of ['/info', '/metrics', '/block/current']) {
175
+ for (const api of ['/info', '/block/current']) {
181
176
  it(`supports native arweave node apis ${api}`, async () => {
182
177
  const [nativeAxios, response] = await Promise.all([
183
- axios(`https://arweave.net${api}`),
184
- wayfinder.request(`ar:///${api}`),
178
+ axios(`https://${gatewayUrl}${api}`),
179
+ wayfinder.request(`ar://${api}`),
185
180
  ]);
186
181
  assert.strictEqual(response.status, 200);
187
182
  assert.strictEqual(response.status, nativeAxios.status);
188
183
  // TODO: ensure the headers are the same excluding unique headers
189
- assert.deepStrictEqual(response.data, nativeAxios.data);
190
184
  });
191
185
  }
192
186
  for (const api of ['/ar-io/info', '/ar-io/__gateway_metrics']) {
193
187
  it(`supports native ario node gateway apis ${api}`, async () => {
194
188
  const [nativeAxios, response] = await Promise.all([
195
- axios(`https://arweave.net${api}`),
189
+ axios(`https://${gatewayUrl}${api}`),
196
190
  wayfinder.request(`ar:///${api}`),
197
191
  ]);
198
192
  assert.strictEqual(response.status, 200);
199
193
  assert.strictEqual(response.status, nativeAxios.status);
200
194
  // TODO: ensure the headers are the same excluding unique headers
201
- assert.deepStrictEqual(response.data, nativeAxios.data);
202
195
  });
203
196
  }
204
- it('should return the error from the target gateway if the route is not found', async () => {
197
+ it.skip('should return the error from the target gateway if the route is not found', async () => {
205
198
  const axiosInstance = axios.create({
206
199
  validateStatus: () => true, // don't throw so we can compare axios result with wrapped axios result
207
200
  });
@@ -212,11 +205,10 @@ describe('Wayfinder', () => {
212
205
  }),
213
206
  });
214
207
  const [nativeAxios, response] = await Promise.all([
215
- axiosInstance('https://arweave.net/not-found'),
216
- wayfinder.request('https://arweave.net/not-found'),
208
+ axiosInstance(`https://${gatewayUrl}/ar-io/not-found`),
209
+ wayfinder.request('ar:///not-found'),
217
210
  ]);
218
211
  assert.strictEqual(response.status, nativeAxios.status);
219
- assert.strictEqual(response.data, nativeAxios.data);
220
212
  });
221
213
  });
222
214
  describe('got', () => {
@@ -231,7 +223,7 @@ describe('Wayfinder', () => {
231
223
  });
232
224
  it('should fetch the data using the got default function against the target gateway', async () => {
233
225
  const [nativeGot, response] = await Promise.all([
234
- got('https://ao.arweave.net'),
226
+ got(`https://ao.${gatewayUrl}`),
235
227
  wayfinder.request('ar://ao'),
236
228
  ]);
237
229
  assert.strictEqual(response.statusCode, 200);
@@ -239,10 +231,265 @@ describe('Wayfinder', () => {
239
231
  assert.deepStrictEqual(response.body, nativeGot.body);
240
232
  });
241
233
  it('should stream the data using got.stream against the selected target gateway', async () => {
242
- const nativeBuffer = await buffer(await got.stream('https://ao.arweave.net', { decompress: false }));
234
+ const nativeBuffer = await buffer(await got.stream(`https://ao.${gatewayUrl}`, { decompress: false }));
243
235
  const wayfinderBuffer = await buffer(await wayfinder.request.stream('ar://ao', { decompress: false }));
244
236
  assert.deepStrictEqual(wayfinderBuffer, nativeBuffer);
245
237
  });
246
238
  });
247
239
  });
240
+ describe('events', () => {
241
+ it('should emit events on the wayfinder event emitter', async () => {
242
+ const wayfinder = new Wayfinder({
243
+ httpClient: fetch,
244
+ router: new StaticGatewayRouter({
245
+ gateway: `http://${gatewayUrl}`,
246
+ }),
247
+ verifier: {
248
+ // @ts-expect-error
249
+ verifyData: async (params) => {
250
+ return;
251
+ },
252
+ },
253
+ });
254
+ const events = [];
255
+ wayfinder.emitter.on('verification-failed', (event) => {
256
+ events.push({ type: 'verification-failed', ...event });
257
+ });
258
+ wayfinder.emitter.on('verification-progress', (event) => {
259
+ events.push({ type: 'verification-progress', ...event });
260
+ });
261
+ wayfinder.emitter.on('verification-passed', (event) => {
262
+ events.push({ type: 'verification-passed', ...event });
263
+ });
264
+ // request data and assert the event is emitted
265
+ const response = await wayfinder.request('ar://c7wkwt6TKgcWJUfgvpJ5q5qi4DIZyJ1_TqhjXgURh0U', {
266
+ redirect: 'follow',
267
+ });
268
+ // read the full response body to ensure the stream is fully consumed
269
+ await response.text();
270
+ assert.strictEqual(response.status, 200);
271
+ assert.ok(events.find((e) => e.type === 'verification-passed'), 'Should emit at least one verification-passed');
272
+ });
273
+ it('should execute callbacks provided to the wayfinder constructor', async () => {
274
+ let verificationFailed = false;
275
+ let verificationProgress = false;
276
+ let verificationPassed = false;
277
+ const wayfinder = new Wayfinder({
278
+ httpClient: fetch,
279
+ router: new StaticGatewayRouter({
280
+ gateway: `http://${gatewayUrl}`,
281
+ }),
282
+ events: {
283
+ onVerificationFailed: () => {
284
+ verificationFailed = true;
285
+ },
286
+ onVerificationProgress: () => {
287
+ verificationProgress = true;
288
+ },
289
+ onVerificationPassed: () => {
290
+ verificationPassed = true;
291
+ },
292
+ },
293
+ });
294
+ const response = await wayfinder.request('ar://c7wkwt6TKgcWJUfgvpJ5q5qi4DIZyJ1_TqhjXgURh0U', {
295
+ redirect: 'follow',
296
+ });
297
+ // read the full response body to ensure the stream is fully consumed
298
+ await response.text();
299
+ assert.strictEqual(response.status, 200);
300
+ assert.ok(verificationFailed === false, 'Should not emit verification-failed');
301
+ assert.ok(verificationProgress, 'Should emit verification-progress');
302
+ assert.ok(verificationPassed, 'Should emit verification-passed');
303
+ });
304
+ });
305
+ describe.skip('tapAndVerifyRequest', () => {
306
+ describe('Readable', () => {
307
+ it('should duplicate the stream, verify the first and return the second if verification passes', async () => {
308
+ // create a simple readable
309
+ const chunks = [
310
+ Buffer.from('foo'),
311
+ Buffer.from('bar'),
312
+ Buffer.from('baz'),
313
+ ];
314
+ const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
315
+ // a stream that will emit chunks
316
+ const originalStream = Readable.from(chunks);
317
+ let seen = Buffer.alloc(0);
318
+ const verifyData = async ({ data,
319
+ // @ts-expect-error
320
+ txId, }) => {
321
+ return new Promise((resolve, reject) => {
322
+ data.on('data', (chunk) => {
323
+ seen = Buffer.concat([seen, chunk]);
324
+ });
325
+ data.on('end', () => {
326
+ // Should have seen exactly the full payload
327
+ assert.strictEqual(seen.length, contentLength);
328
+ resolve();
329
+ });
330
+ data.on('error', reject);
331
+ });
332
+ };
333
+ const txId = 'test-tx-1';
334
+ const emitter = new EventEmitter();
335
+ const events = [];
336
+ emitter.on('verification-progress', (e) => {
337
+ console.log('verification-progress', e);
338
+ events.push({ type: 'verification-progress', ...e });
339
+ });
340
+ emitter.on('verification-passed', (e) => events.push({ type: 'verification-passed', ...e }));
341
+ // tap with verification
342
+ const tapped = tapAndVerifyStream({
343
+ originalStream,
344
+ contentLength,
345
+ verifyData,
346
+ txId,
347
+ emitter,
348
+ });
349
+ // read the stream
350
+ const out = [];
351
+ for await (const chunk of tapped) {
352
+ out.push(chunk);
353
+ }
354
+ // assert the stream is the same
355
+ assert.strictEqual(Buffer.concat(out).toString(), Buffer.concat(chunks).toString(), 'The tapped stream should emit exactly the original data');
356
+ assert.ok(events.find((e) => e.type === 'verification-progress'), 'Should emit at least one verification-progress');
357
+ assert.ok(events.find((e) => e.type === 'verification-passed' && e.txId === txId), 'Should emit at least one verification-passed');
358
+ });
359
+ it('should throw an error on the client stream if verification fails', async () => {
360
+ const chunks = [
361
+ Buffer.from('foo'),
362
+ Buffer.from('bar'),
363
+ Buffer.from('baz'),
364
+ ];
365
+ const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
366
+ // a stream that will emit chunks
367
+ const originalStream = Readable.from(chunks);
368
+ const verifyData = async ({
369
+ // @ts-expect-error
370
+ data, txId, }) => {
371
+ throw new Error('Verification failed for txId: ' + txId);
372
+ };
373
+ const txId = 'test-tx-1';
374
+ const emitter = new EventEmitter();
375
+ const events = [];
376
+ emitter.on('verification-progress', (e) => events.push({ type: 'verification-progress', ...e }));
377
+ emitter.on('verification-failed', (e) => events.push({ type: 'verification-failed', ...e }));
378
+ // tap with verification
379
+ const tapped = tapAndVerifyStream({
380
+ originalStream,
381
+ contentLength,
382
+ verifyData,
383
+ txId,
384
+ emitter,
385
+ });
386
+ // read the stream
387
+ try {
388
+ const out = [];
389
+ for await (const chunk of tapped) {
390
+ out.push(chunk);
391
+ }
392
+ }
393
+ catch (error) {
394
+ assert.ok(events.find((e) => e.type === 'verification-failed' && e.txId === txId), 'Should emit at least one verification-failed');
395
+ // stream should be closed
396
+ assert.ok(tapped.closed);
397
+ }
398
+ });
399
+ });
400
+ describe('ReadableStream', () => {
401
+ it('should duplicate the ReadableStream, verify the first and return the second if verification passes', async () => {
402
+ // create a simple readable
403
+ const chunks = [
404
+ Buffer.from('foo'),
405
+ Buffer.from('bar'),
406
+ Buffer.from('baz'),
407
+ ];
408
+ const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
409
+ // a stream that will emit chunks
410
+ const originalStream = ReadableStream.from(chunks);
411
+ let seen = Buffer.alloc(0);
412
+ const verifyData = async ({ data,
413
+ // @ts-expect-error
414
+ txId, }) => {
415
+ return new Promise(async (resolve, reject) => {
416
+ const reader = data.getReader();
417
+ while (true) {
418
+ try {
419
+ const { done, value } = await reader.read();
420
+ if (done) {
421
+ resolve();
422
+ break;
423
+ }
424
+ seen = Buffer.concat([seen, value]);
425
+ }
426
+ catch (error) {
427
+ reject(error);
428
+ }
429
+ }
430
+ });
431
+ };
432
+ const txId = 'test-tx-1';
433
+ const emitter = new EventEmitter();
434
+ const events = [];
435
+ emitter.on('verification-progress', (e) => events.push({ type: 'verification-progress', ...e }));
436
+ emitter.on('verification-passed', (e) => events.push({ type: 'verification-passed', ...e }));
437
+ // tap with verification
438
+ const tapped = tapAndVerifyStream({
439
+ originalStream,
440
+ contentLength,
441
+ verifyData,
442
+ txId,
443
+ emitter,
444
+ });
445
+ // read the stream
446
+ const out = [];
447
+ for await (const chunk of tapped) {
448
+ out.push(chunk);
449
+ }
450
+ // assert the stream is the same
451
+ assert.strictEqual(Buffer.concat(out).toString(), Buffer.concat(chunks).toString(), 'The tapped stream should emit exactly the original data');
452
+ assert.ok(events.find((e) => e.type === 'verification-progress'), 'Should emit at least one verification-progress');
453
+ assert.ok(events.find((e) => e.type === 'verification-passed' && e.txId === txId), 'Should emit at least one verification-passed');
454
+ });
455
+ it('should throw an error on the client stream if verification fails', async () => {
456
+ const chunks = [
457
+ Buffer.from('foo'),
458
+ Buffer.from('bar'),
459
+ Buffer.from('baz'),
460
+ ];
461
+ const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
462
+ // a stream that will emit chunks
463
+ const originalStream = ReadableStream.from(chunks);
464
+ const verifyData = async ({
465
+ // @ts-expect-error
466
+ data, txId, }) => {
467
+ throw new Error('Verification failed for txId: ' + txId);
468
+ };
469
+ const txId = 'test-tx-1';
470
+ const emitter = new EventEmitter();
471
+ const events = [];
472
+ emitter.on('verification-progress', (e) => events.push({ type: 'verification-progress', ...e }));
473
+ emitter.on('verification-failed', (e) => events.push({ type: 'verification-failed', ...e }));
474
+ // tap with verification
475
+ const tapped = tapAndVerifyStream({
476
+ originalStream,
477
+ contentLength,
478
+ verifyData,
479
+ txId,
480
+ emitter,
481
+ });
482
+ // read the stream
483
+ try {
484
+ const out = [];
485
+ for await (const chunk of tapped) {
486
+ out.push(chunk);
487
+ }
488
+ }
489
+ catch (error) {
490
+ assert.ok(events.find((e) => e.type === 'verification-failed' && e.txId === txId), 'Should emit at least one verification-failed');
491
+ }
492
+ });
493
+ });
494
+ });
248
495
  });
@@ -1 +1,2 @@
1
1
  export {};
2
+ // 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
@@ -43,6 +43,43 @@ export async function spawnANT({ signer, module = AOS_MODULE_ID, ao = connect(),
43
43
  },
44
44
  ],
45
45
  });
46
+ let bootRes;
47
+ let attempts = 0;
48
+ while (attempts < 5 && bootRes === undefined) {
49
+ try {
50
+ bootRes = await ao.result({
51
+ process: processId,
52
+ message: processId,
53
+ });
54
+ break;
55
+ }
56
+ catch (error) {
57
+ logger.debug('Retrying ANT boot result fetch', {
58
+ processId,
59
+ module,
60
+ scheduler,
61
+ attempts,
62
+ error,
63
+ });
64
+ attempts++;
65
+ await new Promise((resolve) => setTimeout(resolve, 1000 * attempts ** 2));
66
+ }
67
+ }
68
+ if (bootRes === undefined ||
69
+ bootRes.Messages.find((m) => m.Tags.find((t) => t.value === 'Invalid-Boot-Notice'))) {
70
+ if (bootRes === undefined) {
71
+ throw new Error('Failed to get boot result');
72
+ }
73
+ const bootError = errorMessageFromOutput(bootRes);
74
+ logger.error('ANT failed to boot correctly', {
75
+ processId,
76
+ module,
77
+ scheduler,
78
+ bootRes,
79
+ bootError,
80
+ });
81
+ throw new Error(`ANT failed to boot correctly: ${bootError}`);
82
+ }
46
83
  logger.debug(`Spawned ANT`, {
47
84
  processId,
48
85
  module,
@@ -107,7 +107,7 @@ export const getEpochDataFromGqlFallback = async ({ ao, epochIndex, processId =
107
107
  if (!messageResult) {
108
108
  continue;
109
109
  }
110
- for (const message of messageResult.Messages) {
110
+ for (const message of messageResult?.Messages ?? []) {
111
111
  const data = JSON.parse(message.Data);
112
112
  const tags = message.Tags;
113
113
  // check if the message results include epoch-distribution-notice for the requested epoch index
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { createHash } from 'crypto';
17
+ import { toB64Url } from './base64.js';
18
+ export const hashReadableToB64Url = (stream, algorithm = 'sha256') => {
19
+ return new Promise((resolve, reject) => {
20
+ const hash = createHash(algorithm);
21
+ stream.on('data', (chunk) => hash.update(chunk));
22
+ stream.on('end', () => resolve(toB64Url(hash.digest())));
23
+ stream.on('error', (err) => reject(err));
24
+ });
25
+ };
26
+ export const hashReadableStreamToB64Url = (stream, algorithm = 'sha256') => {
27
+ return new Promise((resolve, reject) => {
28
+ const hash = createHash(algorithm);
29
+ const reader = stream.getReader();
30
+ const read = async () => {
31
+ try {
32
+ const { done, value } = await reader.read();
33
+ if (done) {
34
+ resolve(toB64Url(hash.digest()));
35
+ }
36
+ else {
37
+ hash.update(value);
38
+ read();
39
+ }
40
+ }
41
+ catch (err) {
42
+ reject(err);
43
+ }
44
+ };
45
+ read().catch(reject);
46
+ });
47
+ };
48
+ export const hashBufferToB64Url = (buffer, algorithm = 'sha256') => {
49
+ return toB64Url(createHash(algorithm).update(buffer).digest());
50
+ };
@@ -14,4 +14,4 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  // AUTOMATICALLY GENERATED FILE - DO NOT TOUCH
17
- export const version = '3.11.0-alpha.1';
17
+ export const version = '3.11.0-alpha.10';
@@ -68,3 +68,4 @@ export declare function getAllGatewayVaults(o: PaginationCLIOptions): Promise<im
68
68
  message: string;
69
69
  }>;
70
70
  export declare function getVault(o: AddressAndVaultIdCLIOptions): Promise<import("../../types/io.js").AoVaultData>;
71
+ export declare function resolveArNSName(o: NameCLIOptions): Promise<import("../../types/io.js").ArNSNameResolutionData>;
@@ -1,5 +1,5 @@
1
1
  import Arweave from 'arweave';
2
- import { ARIOWithFaucet, AoARIORead, AoARIOWrite, AoAllDelegates, AoAllGatewayVaults, AoArNSNameData, AoArNSNameDataWithName, AoArNSPurchaseParams, AoArNSReservedNameData, AoArNSReservedNameDataWithName, AoBalanceWithAddress, AoBuyRecordParams, AoCreateVaultParams, AoDelegation, AoEligibleDistribution, AoEpochData, AoEpochDistributed, AoEpochDistributionData, AoEpochDistributionTotalsData, AoEpochObservationData, AoEpochSettings, AoExtendLeaseParams, AoExtendVaultParams, AoGateway, AoGatewayDelegateWithAddress, AoGatewayRegistrySettings, AoGatewayVault, AoGatewayWithAddress, AoGetCostDetailsParams, AoIncreaseUndernameLimitParams, AoIncreaseVaultParams, AoJoinNetworkParams, AoMessageResult, AoPaginatedAddressParams, AoPrimaryName, AoPrimaryNameRequest, AoRedelegationFeeInfo, AoRegistrationFees, AoReturnedName, AoRevokeVaultParams, AoTokenSupplyData, AoUpdateGatewaySettingsParams, AoVaultData, AoVaultedTransferParams, AoWalletVault, AoWeightedObserver, CostDetailsResult, DemandFactorSettings, EpochInput, OptionalArweave, OptionalPaymentUrl, PaginationParams, PaginationResult, ProcessConfiguration, TransactionId, WalletAddress, WithSigner, WriteOptions, mARIOToken } from '../types/index.js';
2
+ import { ARIOWithFaucet, AoARIORead, AoARIOWrite, AoAllDelegates, AoAllGatewayVaults, AoArNSNameData, AoArNSNameDataWithName, AoArNSPurchaseParams, AoArNSReservedNameData, AoArNSReservedNameDataWithName, AoBalanceWithAddress, AoBuyRecordParams, AoCreateVaultParams, AoDelegation, AoEligibleDistribution, AoEpochData, AoEpochDistributed, AoEpochDistributionData, AoEpochDistributionTotalsData, AoEpochObservationData, AoEpochSettings, AoExtendLeaseParams, AoExtendVaultParams, AoGateway, AoGatewayDelegateWithAddress, AoGatewayRegistrySettings, AoGatewayVault, AoGatewayWithAddress, AoGetCostDetailsParams, AoIncreaseUndernameLimitParams, AoIncreaseVaultParams, AoJoinNetworkParams, AoMessageResult, AoPaginatedAddressParams, AoPrimaryName, AoPrimaryNameRequest, AoRedelegationFeeInfo, AoRegistrationFees, AoReturnedName, AoRevokeVaultParams, AoTokenSupplyData, AoUpdateGatewaySettingsParams, AoVaultData, AoVaultedTransferParams, AoWalletVault, AoWeightedObserver, ArNSNameResolutionData, ArNSNameResolver, CostDetailsResult, DemandFactorSettings, EpochInput, OptionalArweave, OptionalPaymentUrl, PaginationParams, PaginationResult, ProcessConfiguration, TransactionId, WalletAddress, WithSigner, WriteOptions, mARIOToken } from '../types/index.js';
3
3
  import { AOProcess } from './contracts/ao-process.js';
4
4
  import { TurboArNSPaymentProviderAuthenticated, TurboArNSPaymentProviderUnauthenticated } from './turbo.js';
5
5
  type ARIOConfigNoSigner = OptionalPaymentUrl<OptionalArweave<ProcessConfiguration>>;
@@ -9,12 +9,8 @@ export declare class ARIO {
9
9
  static init(config: ARIOConfigWithSigner): AoARIOWrite;
10
10
  static init(config: ARIOConfigNoSigner): AoARIORead;
11
11
  static mainnet(): AoARIORead;
12
- static mainnet(config: ARIOConfigNoSigner & {
13
- faucetUrl?: string;
14
- }): AoARIORead;
15
- static mainnet(config: ARIOConfigWithSigner & {
16
- faucetUrl?: string;
17
- }): AoARIOWrite;
12
+ static mainnet(config: ARIOConfigNoSigner): AoARIORead;
13
+ static mainnet(config: ARIOConfigWithSigner): AoARIOWrite;
18
14
  static testnet(): ARIOWithFaucet<AoARIORead>;
19
15
  static testnet(config: ARIOConfigNoSigner & {
20
16
  faucetUrl?: string;
@@ -23,7 +19,7 @@ export declare class ARIO {
23
19
  faucetUrl?: string;
24
20
  }): ARIOWithFaucet<AoARIOWrite>;
25
21
  }
26
- export declare class ARIOReadable implements AoARIORead {
22
+ export declare class ARIOReadable implements AoARIORead, ArNSNameResolver {
27
23
  readonly process: AOProcess;
28
24
  protected epochSettings: AoEpochSettings | undefined;
29
25
  protected arweave: Arweave;
@@ -139,6 +135,9 @@ export declare class ARIOReadable implements AoARIORead {
139
135
  getGatewayRegistrySettings(): Promise<AoGatewayRegistrySettings>;
140
136
  getAllDelegates(params?: PaginationParams<AoAllDelegates>): Promise<PaginationResult<AoAllDelegates>>;
141
137
  getAllGatewayVaults(params?: PaginationParams<AoAllGatewayVaults>): Promise<PaginationResult<AoAllGatewayVaults>>;
138
+ resolveArNSName({ name, }: {
139
+ name: string;
140
+ }): Promise<ArNSNameResolutionData>;
142
141
  }
143
142
  export declare class ARIOWriteable extends ARIOReadable implements AoARIOWrite {
144
143
  private signer;