@ar.io/sdk 3.10.2 → 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 (114) hide show
  1. package/README.md +52 -0
  2. package/bundles/web.bundle.min.js +140 -132
  3. package/lib/cjs/cli/cli.js +137 -122
  4. package/lib/cjs/cli/commands/readCommands.js +6 -0
  5. package/lib/cjs/cli/options.js +12 -0
  6. package/lib/cjs/cli/utils.js +40 -11
  7. package/lib/cjs/common/ant-versions.js +5 -5
  8. package/lib/cjs/common/ant.js +5 -5
  9. package/lib/cjs/common/index.js +2 -0
  10. package/lib/cjs/common/io.js +106 -1
  11. package/lib/cjs/common/turbo.js +208 -0
  12. package/lib/cjs/common/wayfinder/gateways/trusted-gateways.js +106 -0
  13. package/lib/cjs/common/wayfinder/gateways.js +86 -0
  14. package/lib/cjs/common/wayfinder/index.js +44 -0
  15. package/lib/cjs/common/wayfinder/routers/priority.js +29 -0
  16. package/lib/cjs/common/wayfinder/routers/priority.test.js +155 -0
  17. package/lib/cjs/common/wayfinder/routers/random.js +23 -0
  18. package/lib/cjs/common/wayfinder/routers/random.test.js +25 -0
  19. package/lib/cjs/common/wayfinder/routers/simple-cache.js +25 -0
  20. package/lib/cjs/common/wayfinder/routers/simple-cache.test.js +41 -0
  21. package/lib/cjs/common/wayfinder/routers/static.js +14 -0
  22. package/lib/cjs/common/wayfinder/routers/static.test.js +14 -0
  23. package/lib/cjs/common/wayfinder/verification/data-root-verifier.js +139 -0
  24. package/lib/cjs/common/wayfinder/verification/hash-verifier.js +50 -0
  25. package/lib/cjs/common/wayfinder/wayfinder.js +596 -0
  26. package/lib/cjs/common/wayfinder/wayfinder.test.js +500 -0
  27. package/lib/cjs/types/ant.js +12 -1
  28. package/lib/cjs/types/index.js +1 -0
  29. package/lib/cjs/types/io.js +1 -1
  30. package/lib/cjs/types/wayfinder.js +3 -0
  31. package/lib/cjs/utils/ao.js +42 -0
  32. package/lib/cjs/utils/hash.js +56 -0
  33. package/lib/cjs/utils/random.js +30 -0
  34. package/lib/cjs/utils/url.js +28 -0
  35. package/lib/cjs/utils/url.test.js +24 -0
  36. package/lib/cjs/utils/utils.test.js +8 -8
  37. package/lib/cjs/version.js +1 -1
  38. package/lib/cjs/web/index.js +3 -1
  39. package/lib/esm/cli/cli.js +138 -123
  40. package/lib/esm/cli/commands/readCommands.js +5 -0
  41. package/lib/esm/cli/options.js +12 -0
  42. package/lib/esm/cli/utils.js +39 -11
  43. package/lib/esm/common/ant-versions.js +5 -5
  44. package/lib/esm/common/ant.js +5 -5
  45. package/lib/esm/common/index.js +2 -0
  46. package/lib/esm/common/io.js +106 -1
  47. package/lib/esm/common/turbo.js +200 -0
  48. package/lib/esm/common/wayfinder/gateways/trusted-gateways.js +102 -0
  49. package/lib/esm/common/wayfinder/gateways.js +80 -0
  50. package/lib/esm/common/wayfinder/index.js +28 -0
  51. package/lib/esm/common/wayfinder/routers/priority.js +25 -0
  52. package/lib/esm/common/wayfinder/routers/priority.test.js +153 -0
  53. package/lib/esm/common/wayfinder/routers/random.js +19 -0
  54. package/lib/esm/common/wayfinder/routers/random.test.js +23 -0
  55. package/lib/esm/common/wayfinder/routers/simple-cache.js +21 -0
  56. package/lib/esm/common/wayfinder/routers/simple-cache.test.js +39 -0
  57. package/lib/esm/common/wayfinder/routers/static.js +10 -0
  58. package/lib/esm/common/wayfinder/routers/static.test.js +12 -0
  59. package/lib/esm/common/wayfinder/verification/data-root-verifier.js +130 -0
  60. package/lib/esm/common/wayfinder/verification/hash-verifier.js +46 -0
  61. package/lib/esm/common/wayfinder/wayfinder.js +584 -0
  62. package/lib/esm/common/wayfinder/wayfinder.test.js +495 -0
  63. package/lib/esm/types/ant.js +11 -0
  64. package/lib/esm/types/index.js +1 -0
  65. package/lib/esm/types/io.js +1 -1
  66. package/lib/esm/types/wayfinder.js +2 -0
  67. package/lib/esm/utils/ao.js +42 -0
  68. package/lib/esm/utils/hash.js +50 -0
  69. package/lib/esm/utils/random.js +26 -0
  70. package/lib/esm/utils/url.js +24 -0
  71. package/lib/esm/utils/url.test.js +19 -0
  72. package/lib/esm/utils/utils.test.js +1 -1
  73. package/lib/esm/version.js +1 -1
  74. package/lib/esm/web/index.js +1 -1
  75. package/lib/types/cli/commands/antCommands.d.ts +3 -3
  76. package/lib/types/cli/commands/arnsPurchaseCommands.d.ts +1 -1
  77. package/lib/types/cli/commands/gatewayWriteCommands.d.ts +9 -9
  78. package/lib/types/cli/commands/readCommands.d.ts +2 -0
  79. package/lib/types/cli/commands/transfer.d.ts +3 -3
  80. package/lib/types/cli/options.d.ts +9 -0
  81. package/lib/types/cli/types.d.ts +3 -0
  82. package/lib/types/cli/utils.d.ts +4 -0
  83. package/lib/types/common/ant-versions.d.ts +3 -6
  84. package/lib/types/common/index.d.ts +1 -0
  85. package/lib/types/common/io.d.ts +13 -8
  86. package/lib/types/common/turbo.d.ts +62 -0
  87. package/lib/types/common/wayfinder/gateways/trusted-gateways.d.ts +51 -0
  88. package/lib/types/common/wayfinder/gateways.d.ts +53 -0
  89. package/lib/types/common/wayfinder/index.d.ts +23 -0
  90. package/lib/types/common/wayfinder/routers/priority.d.ts +29 -0
  91. package/lib/types/common/wayfinder/routers/priority.test.d.ts +1 -0
  92. package/lib/types/common/wayfinder/routers/random.d.ts +27 -0
  93. package/lib/types/common/wayfinder/routers/random.test.d.ts +1 -0
  94. package/lib/types/common/wayfinder/routers/simple-cache.d.ts +28 -0
  95. package/lib/types/common/wayfinder/routers/simple-cache.test.d.ts +1 -0
  96. package/lib/types/common/wayfinder/routers/static.d.ts +24 -0
  97. package/lib/types/common/wayfinder/routers/static.test.d.ts +1 -0
  98. package/lib/types/common/wayfinder/verification/data-root-verifier.d.ts +31 -0
  99. package/lib/types/common/wayfinder/verification/hash-verifier.d.ts +27 -0
  100. package/lib/types/common/wayfinder/wayfinder.d.ts +265 -0
  101. package/lib/types/common/wayfinder/wayfinder.test.d.ts +1 -0
  102. package/lib/types/types/ant.d.ts +53 -1
  103. package/lib/types/types/common.d.ts +7 -2
  104. package/lib/types/types/index.d.ts +1 -0
  105. package/lib/types/types/io.d.ts +23 -6
  106. package/lib/types/types/wayfinder.d.ts +62 -0
  107. package/lib/types/utils/ao.d.ts +1 -12
  108. package/lib/types/utils/hash.d.ts +4 -0
  109. package/lib/types/utils/random.d.ts +7 -0
  110. package/lib/types/utils/url.d.ts +19 -0
  111. package/lib/types/utils/url.test.d.ts +1 -0
  112. package/lib/types/version.d.ts +1 -1
  113. package/lib/types/web/index.d.ts +1 -1
  114. package/package.json +5 -3
@@ -0,0 +1,495 @@
1
+ import axios from 'axios';
2
+ import got from 'got';
3
+ import assert from 'node:assert';
4
+ import { EventEmitter } from 'node:events';
5
+ import { Readable } from 'node:stream';
6
+ import { buffer } from 'node:stream/consumers';
7
+ import { before, describe, it } from 'node:test';
8
+ import { Logger } from '../../common/logger.js';
9
+ import { RandomGatewayRouter } from './routers/random.js';
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';
14
+ const stubbedGatewaysProvider = {
15
+ getGateways: async () => [new URL(`http://${gatewayUrl}`)],
16
+ };
17
+ Logger.default.setLogLevel('none');
18
+ describe('Wayfinder', () => {
19
+ describe.skip('http wrapper', () => {
20
+ describe('fetch', () => {
21
+ let wayfinder;
22
+ before(() => {
23
+ wayfinder = new Wayfinder({
24
+ httpClient: fetch,
25
+ router: new RandomGatewayRouter({
26
+ gatewaysProvider: stubbedGatewaysProvider,
27
+ }),
28
+ });
29
+ });
30
+ it('should fetch the data using the selected gateway', async () => {
31
+ const nativeFetch = await fetch(`https://ao.${gatewayUrl}`);
32
+ const response = await wayfinder.request('ar://ao');
33
+ assert.strictEqual(response.status, 200);
34
+ assert.strictEqual(response.status, nativeFetch.status);
35
+ // assert the arns headers are the same
36
+ const arnsHeaders = Array.from(response.headers.entries()).filter(([key]) => key.startsWith('x-arns-'));
37
+ const nativeFetchHeaders = Array.from(nativeFetch.headers.entries()).filter(([key]) => key.startsWith('x-arns-'));
38
+ assert.deepStrictEqual(arnsHeaders, nativeFetchHeaders);
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);
47
+ });
48
+ it('should route a non-ar:// url as a normal fetch', async () => {
49
+ const [nativeFetch, response] = await Promise.all([
50
+ fetch(`https://${gatewayUrl}/`, {
51
+ method: 'HEAD',
52
+ }),
53
+ wayfinder.request(`https://${gatewayUrl}/`, {
54
+ method: 'HEAD',
55
+ }),
56
+ ]);
57
+ assert.strictEqual(response.status, 200);
58
+ assert.strictEqual(response.status, nativeFetch.status);
59
+ // TODO: ensure the headers are the same excluding unique headers
60
+ });
61
+ for (const api of ['/info', '/block/current']) {
62
+ it(`supports native arweave node apis ${api}`, async () => {
63
+ const [nativeFetch, response] = await Promise.all([
64
+ fetch(`https://${gatewayUrl}${api}`),
65
+ wayfinder.request(`ar://${api}`),
66
+ ]);
67
+ assert.strictEqual(response.status, 200);
68
+ assert.strictEqual(response.status, nativeFetch.status);
69
+ // TODO: ensure the headers are the same excluding unique headers
70
+ });
71
+ }
72
+ for (const api of ['/ar-io/info']) {
73
+ it(`supports native ario node gateway apis ${api}`, async () => {
74
+ const [nativeFetch, response] = await Promise.all([
75
+ fetch(`https://${gatewayUrl}${api}`),
76
+ wayfinder.request(`ar:///${api}`),
77
+ ]);
78
+ assert.strictEqual(response.status, 200);
79
+ assert.strictEqual(response.status, nativeFetch.status);
80
+ // TODO: ensure the headers are the same excluding unique headers
81
+ });
82
+ }
83
+ it('supports a post request to graphql', async () => {
84
+ const response = await wayfinder.request('ar:///graphql', {
85
+ method: 'POST',
86
+ headers: {
87
+ 'Content-Type': 'application/json',
88
+ },
89
+ body: JSON.stringify({
90
+ query: `
91
+ query {
92
+ transactions(
93
+ ids: ["xf958qhCNGfDme1FtoiD6DtMfDENDbtxZpjOM_1tsMM"]
94
+ ) {
95
+ edges {
96
+ cursor
97
+ node {
98
+ id
99
+ tags {
100
+ name
101
+ value
102
+ }
103
+ block {
104
+ height
105
+ timestamp
106
+ }
107
+ }
108
+ }
109
+ pageInfo {
110
+ hasNextPage
111
+ }
112
+ }
113
+ }
114
+ `,
115
+ }),
116
+ });
117
+ assert.strictEqual(response.status, 200);
118
+ });
119
+ it.skip('returns the error from the target gateway if the route is not found', async () => {
120
+ const [nativeFetch, response] = await Promise.all([
121
+ fetch(`https://${gatewayUrl}/ar-io/not-found`),
122
+ wayfinder.request('ar:///not-found'),
123
+ ]);
124
+ assert.strictEqual(response.status, nativeFetch.status);
125
+ assert.strictEqual(response.statusText, nativeFetch.statusText);
126
+ });
127
+ });
128
+ describe('axios', () => {
129
+ let wayfinder;
130
+ before(() => {
131
+ wayfinder = new Wayfinder({
132
+ httpClient: axios,
133
+ router: new RandomGatewayRouter({
134
+ gatewaysProvider: stubbedGatewaysProvider,
135
+ }),
136
+ });
137
+ });
138
+ it('should fetch the data using axios default function against the target gateway', async () => {
139
+ const [nativeAxios, response] = await Promise.all([
140
+ axios(`https://ao.${gatewayUrl}`),
141
+ wayfinder.request('ar://ao'),
142
+ ]);
143
+ assert.strictEqual(response.status, 200);
144
+ assert.strictEqual(response.status, nativeAxios.status);
145
+ // assert the arns headers are the same
146
+ const arnsHeaders = Object.entries(response.headers)
147
+ .sort()
148
+ .filter(([key]) => key.startsWith('x-arns-'));
149
+ const nativeAxiosHeaders = Object.entries(nativeAxios.headers).filter(([key]) => key.startsWith('x-arns-'));
150
+ assert.deepStrictEqual(arnsHeaders.sort(), nativeAxiosHeaders.sort());
151
+ });
152
+ it('should fetch the data using the axios.get method against the target gateway', async () => {
153
+ const [nativeAxios, response] = await Promise.all([
154
+ axios.get(`https://ao.${gatewayUrl}`),
155
+ wayfinder.request.get('ar://ao'),
156
+ ]);
157
+ assert.strictEqual(response.status, 200);
158
+ assert.strictEqual(response.status, nativeAxios.status);
159
+ // assert the arns headers are the same
160
+ const arnsHeaders = Object.entries(response.headers)
161
+ .sort()
162
+ .filter(([key]) => key.startsWith('x-arns-'));
163
+ const nativeAxiosHeaders = Object.entries(nativeAxios.headers).filter(([key]) => key.startsWith('x-arns-'));
164
+ assert.deepStrictEqual(arnsHeaders.sort(), nativeAxiosHeaders.sort());
165
+ });
166
+ it('should route a non-ar:// url as a normal axios request', async () => {
167
+ const [nativeAxios, response] = await Promise.all([
168
+ axios(`https://${gatewayUrl}/`),
169
+ wayfinder.request(`https://${gatewayUrl}/`),
170
+ ]);
171
+ assert.strictEqual(response.status, 200);
172
+ assert.strictEqual(response.status, nativeAxios.status);
173
+ // TODO: ensure the headers are the same excluding unique headers
174
+ });
175
+ for (const api of ['/info', '/block/current']) {
176
+ it(`supports native arweave node apis ${api}`, async () => {
177
+ const [nativeAxios, response] = await Promise.all([
178
+ axios(`https://${gatewayUrl}${api}`),
179
+ wayfinder.request(`ar://${api}`),
180
+ ]);
181
+ assert.strictEqual(response.status, 200);
182
+ assert.strictEqual(response.status, nativeAxios.status);
183
+ // TODO: ensure the headers are the same excluding unique headers
184
+ });
185
+ }
186
+ for (const api of ['/ar-io/info', '/ar-io/__gateway_metrics']) {
187
+ it(`supports native ario node gateway apis ${api}`, async () => {
188
+ const [nativeAxios, response] = await Promise.all([
189
+ axios(`https://${gatewayUrl}${api}`),
190
+ wayfinder.request(`ar:///${api}`),
191
+ ]);
192
+ assert.strictEqual(response.status, 200);
193
+ assert.strictEqual(response.status, nativeAxios.status);
194
+ // TODO: ensure the headers are the same excluding unique headers
195
+ });
196
+ }
197
+ it.skip('should return the error from the target gateway if the route is not found', async () => {
198
+ const axiosInstance = axios.create({
199
+ validateStatus: () => true, // don't throw so we can compare axios result with wrapped axios result
200
+ });
201
+ const wayfinder = new Wayfinder({
202
+ httpClient: axiosInstance,
203
+ router: new RandomGatewayRouter({
204
+ gatewaysProvider: stubbedGatewaysProvider,
205
+ }),
206
+ });
207
+ const [nativeAxios, response] = await Promise.all([
208
+ axiosInstance(`https://${gatewayUrl}/ar-io/not-found`),
209
+ wayfinder.request('ar:///not-found'),
210
+ ]);
211
+ assert.strictEqual(response.status, nativeAxios.status);
212
+ });
213
+ });
214
+ describe('got', () => {
215
+ let wayfinder;
216
+ before(() => {
217
+ wayfinder = new Wayfinder({
218
+ httpClient: got,
219
+ router: new RandomGatewayRouter({
220
+ gatewaysProvider: stubbedGatewaysProvider,
221
+ }),
222
+ });
223
+ });
224
+ it('should fetch the data using the got default function against the target gateway', async () => {
225
+ const [nativeGot, response] = await Promise.all([
226
+ got(`https://ao.${gatewayUrl}`),
227
+ wayfinder.request('ar://ao'),
228
+ ]);
229
+ assert.strictEqual(response.statusCode, 200);
230
+ assert.strictEqual(response.statusCode, nativeGot.statusCode);
231
+ assert.deepStrictEqual(response.body, nativeGot.body);
232
+ });
233
+ it('should stream the data using got.stream against the selected target gateway', async () => {
234
+ const nativeBuffer = await buffer(await got.stream(`https://ao.${gatewayUrl}`, { decompress: false }));
235
+ const wayfinderBuffer = await buffer(await wayfinder.request.stream('ar://ao', { decompress: false }));
236
+ assert.deepStrictEqual(wayfinderBuffer, nativeBuffer);
237
+ });
238
+ });
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
+ });
495
+ });
@@ -77,6 +77,17 @@ export const AntStateSchema = z.object({
77
77
  .boolean()
78
78
  .describe('Flag indicating whether the ANT has been initialized.'),
79
79
  });
80
+ export const SpawnANTStateSchema = z.object({
81
+ name: z.string().describe('The name of the ANT.'),
82
+ ticker: z.string().describe('The ticker symbol for the ANT.'),
83
+ description: z.string().describe('The description for the ANT.'),
84
+ keywords: AntKeywordsSchema.describe('The keywords for the ANT.'),
85
+ owner: AOAddressSchema.describe('The Owners address.'),
86
+ controllers: AntControllersSchema.describe('Controllers of the ANT who have administrative privileges.'),
87
+ records: AntRecordsSchema.describe('Records associated with the ANT.'),
88
+ balances: AntBalancesSchema.describe('Balance details for each address holding the ANT.'),
89
+ logo: ArweaveTxIdSchema.describe('Transaction ID of the ANT logo.'),
90
+ });
80
91
  export const AntReadHandlers = [
81
92
  'balance',
82
93
  'balances',
@@ -19,3 +19,4 @@ export * from './common.js';
19
19
  export * from './faucet.js';
20
20
  export * from './io.js';
21
21
  export * from './token.js';
22
+ export * from './wayfinder.js';
@@ -22,7 +22,7 @@ export const intentsUsingYears = [
22
22
  export const isValidIntent = (intent) => {
23
23
  return validIntents.indexOf(intent) !== -1;
24
24
  };
25
- export const fundFromOptions = ['balance', 'stakes', 'any'];
25
+ export const fundFromOptions = ['balance', 'stakes', 'any', 'turbo'];
26
26
  export const isValidFundFrom = (fundFrom) => {
27
27
  return fundFromOptions.indexOf(fundFrom) !== -1;
28
28
  };
@@ -0,0 +1,2 @@
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
@@ -19,8 +19,13 @@ import { z } from 'zod';
19
19
  import { defaultArweave } from '../common/arweave.js';
20
20
  import { AOProcess, Logger } from '../common/index.js';
21
21
  import { ANT_LUA_ID, ANT_REGISTRY_ID, AOS_MODULE_ID, AO_AUTHORITY, DEFAULT_SCHEDULER_ID, } from '../constants.js';
22
+ import { SpawnANTStateSchema } from '../types/ant.js';
23
+ import { parseSchemaResult } from './schema.js';
22
24
  export async function spawnANT({ signer, module = AOS_MODULE_ID, ao = connect(), scheduler = DEFAULT_SCHEDULER_ID, state, antRegistryId = ANT_REGISTRY_ID, logger = Logger.default, authority = AO_AUTHORITY, }) {
23
25
  // TODO: use On-Boot data handler for bootstrapping state instead of initialize-state
26
+ if (state) {
27
+ parseSchemaResult(SpawnANTStateSchema, state);
28
+ }
24
29
  const processId = await ao.spawn({
25
30
  module,
26
31
  scheduler,
@@ -38,6 +43,43 @@ export async function spawnANT({ signer, module = AOS_MODULE_ID, ao = connect(),
38
43
  },
39
44
  ],
40
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
+ }
41
83
  logger.debug(`Spawned ANT`, {
42
84
  processId,
43
85
  module,
@@ -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
+ };
@@ -0,0 +1,26 @@
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 { webcrypto } from 'crypto';
17
+ /**
18
+ * Cryptographically secure helper for randomness, does not support seeding
19
+ * @param min - the minimum value
20
+ * @param max - the maximum value
21
+ * @returns a random integer between min and max
22
+ */
23
+ export const randomInt = (min, max) => {
24
+ const [rand] = webcrypto.getRandomValues(new Uint32Array(1));
25
+ return min + (rand % (max - min));
26
+ };