@ar.io/sdk 3.11.0-alpha.6 → 3.11.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundles/web.bundle.min.js +119 -116
- package/lib/cjs/common/io.js +8 -3
- package/lib/cjs/common/wayfinder/gateways/trusted-gateways.js +106 -0
- package/lib/cjs/common/wayfinder/gateways.js +19 -8
- package/lib/cjs/common/wayfinder/index.js +7 -1
- package/lib/cjs/common/wayfinder/routers/priority.js +11 -20
- package/lib/cjs/common/wayfinder/routers/priority.test.js +5 -5
- package/lib/cjs/common/wayfinder/routers/random.js +2 -2
- package/lib/cjs/common/wayfinder/routers/random.test.js +5 -84
- package/lib/cjs/common/wayfinder/routers/{fixed.js → static.js} +5 -5
- package/lib/cjs/common/wayfinder/routers/{fixed.test.js → static.test.js} +4 -4
- package/lib/cjs/common/wayfinder/verification/data-root-verifier.js +139 -0
- package/lib/cjs/common/wayfinder/verification/hash-verifier.js +50 -0
- package/lib/cjs/common/wayfinder/wayfinder.js +408 -19
- package/lib/cjs/common/wayfinder/wayfinder.test.js +296 -49
- package/lib/cjs/types/wayfinder.js +1 -0
- package/lib/cjs/utils/arweave.js +1 -1
- package/lib/cjs/utils/hash.js +56 -0
- package/lib/cjs/version.js +1 -1
- package/lib/esm/common/io.js +8 -3
- package/lib/esm/common/wayfinder/gateways/trusted-gateways.js +102 -0
- package/lib/esm/common/wayfinder/gateways.js +17 -6
- package/lib/esm/common/wayfinder/index.js +7 -1
- package/lib/esm/common/wayfinder/routers/priority.js +11 -20
- package/lib/esm/common/wayfinder/routers/priority.test.js +5 -5
- package/lib/esm/common/wayfinder/routers/random.js +2 -2
- package/lib/esm/common/wayfinder/routers/random.test.js +5 -84
- package/lib/esm/common/wayfinder/routers/{fixed.js → static.js} +3 -3
- package/lib/esm/common/wayfinder/routers/{fixed.test.js → static.test.js} +4 -4
- package/lib/esm/common/wayfinder/verification/data-root-verifier.js +130 -0
- package/lib/esm/common/wayfinder/verification/hash-verifier.js +46 -0
- package/lib/esm/common/wayfinder/wayfinder.js +402 -19
- package/lib/esm/common/wayfinder/wayfinder.test.js +297 -50
- package/lib/esm/types/wayfinder.js +1 -0
- package/lib/esm/utils/arweave.js +1 -1
- package/lib/esm/utils/hash.js +50 -0
- package/lib/esm/version.js +1 -1
- package/lib/types/common/wayfinder/gateways/trusted-gateways.d.ts +51 -0
- package/lib/types/common/wayfinder/gateways.d.ts +16 -7
- package/lib/types/common/wayfinder/index.d.ts +4 -1
- package/lib/types/common/wayfinder/routers/priority.d.ts +8 -12
- package/lib/types/common/wayfinder/routers/{fixed.d.ts → static.d.ts} +3 -3
- package/lib/types/common/wayfinder/verification/data-root-verifier.d.ts +31 -0
- package/lib/types/common/wayfinder/verification/hash-verifier.d.ts +27 -0
- package/lib/types/common/wayfinder/wayfinder.d.ts +148 -10
- package/lib/types/types/wayfinder.d.ts +43 -0
- package/lib/types/utils/hash.d.ts +4 -0
- package/lib/types/version.d.ts +1 -1
- package/package.json +1 -1
- /package/lib/types/common/wayfinder/routers/{fixed.test.d.ts → static.test.d.ts} +0 -0
|
@@ -6,27 +6,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const axios_1 = __importDefault(require("axios"));
|
|
7
7
|
const got_1 = __importDefault(require("got"));
|
|
8
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");
|
|
9
11
|
const consumers_1 = require("node:stream/consumers");
|
|
10
12
|
const node_test_1 = require("node:test");
|
|
13
|
+
const logger_js_1 = require("../../common/logger.js");
|
|
11
14
|
const random_js_1 = require("./routers/random.js");
|
|
15
|
+
const static_js_1 = require("./routers/static.js");
|
|
12
16
|
const wayfinder_js_1 = require("./wayfinder.js");
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
gatewayAddress: 'arweave',
|
|
16
|
-
operatorStake: 1000,
|
|
17
|
-
totalDelegatedStake: 1000,
|
|
18
|
-
startTimestamp: 1000,
|
|
19
|
-
settings: {
|
|
20
|
-
protocol: 'https',
|
|
21
|
-
fqdn: 'arweave.net',
|
|
22
|
-
port: 443,
|
|
23
|
-
},
|
|
24
|
-
};
|
|
17
|
+
// TODO: replace with locally running gateway
|
|
18
|
+
const gatewayUrl = 'permagate.io';
|
|
25
19
|
const stubbedGatewaysProvider = {
|
|
26
|
-
getGateways: async () => [
|
|
20
|
+
getGateways: async () => [new URL(`http://${gatewayUrl}`)],
|
|
27
21
|
};
|
|
22
|
+
logger_js_1.Logger.default.setLogLevel('none');
|
|
28
23
|
(0, node_test_1.describe)('Wayfinder', () => {
|
|
29
|
-
|
|
24
|
+
node_test_1.describe.skip('http wrapper', () => {
|
|
30
25
|
(0, node_test_1.describe)('fetch', () => {
|
|
31
26
|
let wayfinder;
|
|
32
27
|
(0, node_test_1.before)(() => {
|
|
@@ -38,7 +33,7 @@ const stubbedGatewaysProvider = {
|
|
|
38
33
|
});
|
|
39
34
|
});
|
|
40
35
|
(0, node_test_1.it)('should fetch the data using the selected gateway', async () => {
|
|
41
|
-
const nativeFetch = await fetch(
|
|
36
|
+
const nativeFetch = await fetch(`https://ao.${gatewayUrl}`);
|
|
42
37
|
const response = await wayfinder.request('ar://ao');
|
|
43
38
|
node_assert_1.default.strictEqual(response.status, 200);
|
|
44
39
|
node_assert_1.default.strictEqual(response.status, nativeFetch.status);
|
|
@@ -46,44 +41,48 @@ const stubbedGatewaysProvider = {
|
|
|
46
41
|
const arnsHeaders = Array.from(response.headers.entries()).filter(([key]) => key.startsWith('x-arns-'));
|
|
47
42
|
const nativeFetchHeaders = Array.from(nativeFetch.headers.entries()).filter(([key]) => key.startsWith('x-arns-'));
|
|
48
43
|
node_assert_1.default.deepStrictEqual(arnsHeaders, nativeFetchHeaders);
|
|
49
|
-
|
|
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);
|
|
50
52
|
});
|
|
51
53
|
(0, node_test_1.it)('should route a non-ar:// url as a normal fetch', async () => {
|
|
52
54
|
const [nativeFetch, response] = await Promise.all([
|
|
53
|
-
fetch(
|
|
55
|
+
fetch(`https://${gatewayUrl}/`, {
|
|
54
56
|
method: 'HEAD',
|
|
55
57
|
}),
|
|
56
|
-
wayfinder.request(
|
|
58
|
+
wayfinder.request(`https://${gatewayUrl}/`, {
|
|
57
59
|
method: 'HEAD',
|
|
58
60
|
}),
|
|
59
61
|
]);
|
|
60
62
|
node_assert_1.default.strictEqual(response.status, 200);
|
|
61
63
|
node_assert_1.default.strictEqual(response.status, nativeFetch.status);
|
|
62
64
|
// TODO: ensure the headers are the same excluding unique headers
|
|
63
|
-
node_assert_1.default.deepStrictEqual(await response.text(), await nativeFetch.text());
|
|
64
65
|
});
|
|
65
|
-
for (const api of ['/info', '/
|
|
66
|
+
for (const api of ['/info', '/block/current']) {
|
|
66
67
|
(0, node_test_1.it)(`supports native arweave node apis ${api}`, async () => {
|
|
67
68
|
const [nativeFetch, response] = await Promise.all([
|
|
68
|
-
fetch(`https
|
|
69
|
-
wayfinder.request(`ar
|
|
69
|
+
fetch(`https://${gatewayUrl}${api}`),
|
|
70
|
+
wayfinder.request(`ar://${api}`),
|
|
70
71
|
]);
|
|
71
72
|
node_assert_1.default.strictEqual(response.status, 200);
|
|
72
73
|
node_assert_1.default.strictEqual(response.status, nativeFetch.status);
|
|
73
74
|
// TODO: ensure the headers are the same excluding unique headers
|
|
74
|
-
node_assert_1.default.deepStrictEqual(await response.text(), await nativeFetch.text());
|
|
75
75
|
});
|
|
76
76
|
}
|
|
77
|
-
for (const api of ['/ar-io/info'
|
|
77
|
+
for (const api of ['/ar-io/info']) {
|
|
78
78
|
(0, node_test_1.it)(`supports native ario node gateway apis ${api}`, async () => {
|
|
79
79
|
const [nativeFetch, response] = await Promise.all([
|
|
80
|
-
fetch(`https
|
|
80
|
+
fetch(`https://${gatewayUrl}${api}`),
|
|
81
81
|
wayfinder.request(`ar:///${api}`),
|
|
82
82
|
]);
|
|
83
83
|
node_assert_1.default.strictEqual(response.status, 200);
|
|
84
84
|
node_assert_1.default.strictEqual(response.status, nativeFetch.status);
|
|
85
85
|
// TODO: ensure the headers are the same excluding unique headers
|
|
86
|
-
node_assert_1.default.deepStrictEqual(await response.text(), await nativeFetch.text());
|
|
87
86
|
});
|
|
88
87
|
}
|
|
89
88
|
(0, node_test_1.it)('supports a post request to graphql', async () => {
|
|
@@ -122,14 +121,13 @@ const stubbedGatewaysProvider = {
|
|
|
122
121
|
});
|
|
123
122
|
node_assert_1.default.strictEqual(response.status, 200);
|
|
124
123
|
});
|
|
125
|
-
|
|
124
|
+
node_test_1.it.skip('returns the error from the target gateway if the route is not found', async () => {
|
|
126
125
|
const [nativeFetch, response] = await Promise.all([
|
|
127
|
-
fetch(
|
|
128
|
-
wayfinder.request('
|
|
126
|
+
fetch(`https://${gatewayUrl}/ar-io/not-found`),
|
|
127
|
+
wayfinder.request('ar:///not-found'),
|
|
129
128
|
]);
|
|
130
129
|
node_assert_1.default.strictEqual(response.status, nativeFetch.status);
|
|
131
130
|
node_assert_1.default.strictEqual(response.statusText, nativeFetch.statusText);
|
|
132
|
-
node_assert_1.default.deepStrictEqual(await response.text(), await nativeFetch.text());
|
|
133
131
|
});
|
|
134
132
|
});
|
|
135
133
|
(0, node_test_1.describe)('axios', () => {
|
|
@@ -144,7 +142,7 @@ const stubbedGatewaysProvider = {
|
|
|
144
142
|
});
|
|
145
143
|
(0, node_test_1.it)('should fetch the data using axios default function against the target gateway', async () => {
|
|
146
144
|
const [nativeAxios, response] = await Promise.all([
|
|
147
|
-
(0, axios_1.default)(
|
|
145
|
+
(0, axios_1.default)(`https://ao.${gatewayUrl}`),
|
|
148
146
|
wayfinder.request('ar://ao'),
|
|
149
147
|
]);
|
|
150
148
|
node_assert_1.default.strictEqual(response.status, 200);
|
|
@@ -155,11 +153,10 @@ const stubbedGatewaysProvider = {
|
|
|
155
153
|
.filter(([key]) => key.startsWith('x-arns-'));
|
|
156
154
|
const nativeAxiosHeaders = Object.entries(nativeAxios.headers).filter(([key]) => key.startsWith('x-arns-'));
|
|
157
155
|
node_assert_1.default.deepStrictEqual(arnsHeaders.sort(), nativeAxiosHeaders.sort());
|
|
158
|
-
node_assert_1.default.deepStrictEqual(response.data, nativeAxios.data);
|
|
159
156
|
});
|
|
160
157
|
(0, node_test_1.it)('should fetch the data using the axios.get method against the target gateway', async () => {
|
|
161
158
|
const [nativeAxios, response] = await Promise.all([
|
|
162
|
-
axios_1.default.get(
|
|
159
|
+
axios_1.default.get(`https://ao.${gatewayUrl}`),
|
|
163
160
|
wayfinder.request.get('ar://ao'),
|
|
164
161
|
]);
|
|
165
162
|
node_assert_1.default.strictEqual(response.status, 200);
|
|
@@ -170,43 +167,39 @@ const stubbedGatewaysProvider = {
|
|
|
170
167
|
.filter(([key]) => key.startsWith('x-arns-'));
|
|
171
168
|
const nativeAxiosHeaders = Object.entries(nativeAxios.headers).filter(([key]) => key.startsWith('x-arns-'));
|
|
172
169
|
node_assert_1.default.deepStrictEqual(arnsHeaders.sort(), nativeAxiosHeaders.sort());
|
|
173
|
-
node_assert_1.default.deepStrictEqual(response.data, nativeAxios.data);
|
|
174
170
|
});
|
|
175
171
|
(0, node_test_1.it)('should route a non-ar:// url as a normal axios request', async () => {
|
|
176
172
|
const [nativeAxios, response] = await Promise.all([
|
|
177
|
-
(0, axios_1.default)(
|
|
178
|
-
wayfinder.request(
|
|
173
|
+
(0, axios_1.default)(`https://${gatewayUrl}/`),
|
|
174
|
+
wayfinder.request(`https://${gatewayUrl}/`),
|
|
179
175
|
]);
|
|
180
176
|
node_assert_1.default.strictEqual(response.status, 200);
|
|
181
177
|
node_assert_1.default.strictEqual(response.status, nativeAxios.status);
|
|
182
|
-
node_assert_1.default.deepStrictEqual(response.data, nativeAxios.data);
|
|
183
178
|
// TODO: ensure the headers are the same excluding unique headers
|
|
184
179
|
});
|
|
185
|
-
for (const api of ['/info', '/
|
|
180
|
+
for (const api of ['/info', '/block/current']) {
|
|
186
181
|
(0, node_test_1.it)(`supports native arweave node apis ${api}`, async () => {
|
|
187
182
|
const [nativeAxios, response] = await Promise.all([
|
|
188
|
-
(0, axios_1.default)(`https
|
|
189
|
-
wayfinder.request(`ar
|
|
183
|
+
(0, axios_1.default)(`https://${gatewayUrl}${api}`),
|
|
184
|
+
wayfinder.request(`ar://${api}`),
|
|
190
185
|
]);
|
|
191
186
|
node_assert_1.default.strictEqual(response.status, 200);
|
|
192
187
|
node_assert_1.default.strictEqual(response.status, nativeAxios.status);
|
|
193
188
|
// TODO: ensure the headers are the same excluding unique headers
|
|
194
|
-
node_assert_1.default.deepStrictEqual(response.data, nativeAxios.data);
|
|
195
189
|
});
|
|
196
190
|
}
|
|
197
191
|
for (const api of ['/ar-io/info', '/ar-io/__gateway_metrics']) {
|
|
198
192
|
(0, node_test_1.it)(`supports native ario node gateway apis ${api}`, async () => {
|
|
199
193
|
const [nativeAxios, response] = await Promise.all([
|
|
200
|
-
(0, axios_1.default)(`https
|
|
194
|
+
(0, axios_1.default)(`https://${gatewayUrl}${api}`),
|
|
201
195
|
wayfinder.request(`ar:///${api}`),
|
|
202
196
|
]);
|
|
203
197
|
node_assert_1.default.strictEqual(response.status, 200);
|
|
204
198
|
node_assert_1.default.strictEqual(response.status, nativeAxios.status);
|
|
205
199
|
// TODO: ensure the headers are the same excluding unique headers
|
|
206
|
-
node_assert_1.default.deepStrictEqual(response.data, nativeAxios.data);
|
|
207
200
|
});
|
|
208
201
|
}
|
|
209
|
-
|
|
202
|
+
node_test_1.it.skip('should return the error from the target gateway if the route is not found', async () => {
|
|
210
203
|
const axiosInstance = axios_1.default.create({
|
|
211
204
|
validateStatus: () => true, // don't throw so we can compare axios result with wrapped axios result
|
|
212
205
|
});
|
|
@@ -217,11 +210,10 @@ const stubbedGatewaysProvider = {
|
|
|
217
210
|
}),
|
|
218
211
|
});
|
|
219
212
|
const [nativeAxios, response] = await Promise.all([
|
|
220
|
-
axiosInstance(
|
|
221
|
-
wayfinder.request('
|
|
213
|
+
axiosInstance(`https://${gatewayUrl}/ar-io/not-found`),
|
|
214
|
+
wayfinder.request('ar:///not-found'),
|
|
222
215
|
]);
|
|
223
216
|
node_assert_1.default.strictEqual(response.status, nativeAxios.status);
|
|
224
|
-
node_assert_1.default.strictEqual(response.data, nativeAxios.data);
|
|
225
217
|
});
|
|
226
218
|
});
|
|
227
219
|
(0, node_test_1.describe)('got', () => {
|
|
@@ -236,7 +228,7 @@ const stubbedGatewaysProvider = {
|
|
|
236
228
|
});
|
|
237
229
|
(0, node_test_1.it)('should fetch the data using the got default function against the target gateway', async () => {
|
|
238
230
|
const [nativeGot, response] = await Promise.all([
|
|
239
|
-
(0, got_1.default)(
|
|
231
|
+
(0, got_1.default)(`https://ao.${gatewayUrl}`),
|
|
240
232
|
wayfinder.request('ar://ao'),
|
|
241
233
|
]);
|
|
242
234
|
node_assert_1.default.strictEqual(response.statusCode, 200);
|
|
@@ -244,10 +236,265 @@ const stubbedGatewaysProvider = {
|
|
|
244
236
|
node_assert_1.default.deepStrictEqual(response.body, nativeGot.body);
|
|
245
237
|
});
|
|
246
238
|
(0, node_test_1.it)('should stream the data using got.stream against the selected target gateway', async () => {
|
|
247
|
-
const nativeBuffer = await (0, consumers_1.buffer)(await got_1.default.stream(
|
|
239
|
+
const nativeBuffer = await (0, consumers_1.buffer)(await got_1.default.stream(`https://ao.${gatewayUrl}`, { decompress: false }));
|
|
248
240
|
const wayfinderBuffer = await (0, consumers_1.buffer)(await wayfinder.request.stream('ar://ao', { decompress: false }));
|
|
249
241
|
node_assert_1.default.deepStrictEqual(wayfinderBuffer, nativeBuffer);
|
|
250
242
|
});
|
|
251
243
|
});
|
|
252
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
|
+
});
|
|
253
500
|
});
|
package/lib/cjs/utils/arweave.js
CHANGED
|
@@ -118,7 +118,7 @@ const getEpochDataFromGqlFallback = async ({ ao, epochIndex, processId = constan
|
|
|
118
118
|
if (!messageResult) {
|
|
119
119
|
continue;
|
|
120
120
|
}
|
|
121
|
-
for (const message of messageResult
|
|
121
|
+
for (const message of messageResult?.Messages ?? []) {
|
|
122
122
|
const data = JSON.parse(message.Data);
|
|
123
123
|
const tags = message.Tags;
|
|
124
124
|
// check if the message results include epoch-distribution-notice for the requested epoch index
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hashBufferToB64Url = exports.hashReadableStreamToB64Url = exports.hashReadableToB64Url = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
|
|
6
|
+
*
|
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
*
|
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
const crypto_1 = require("crypto");
|
|
20
|
+
const base64_js_1 = require("./base64.js");
|
|
21
|
+
const hashReadableToB64Url = (stream, algorithm = 'sha256') => {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
const hash = (0, crypto_1.createHash)(algorithm);
|
|
24
|
+
stream.on('data', (chunk) => hash.update(chunk));
|
|
25
|
+
stream.on('end', () => resolve((0, base64_js_1.toB64Url)(hash.digest())));
|
|
26
|
+
stream.on('error', (err) => reject(err));
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
exports.hashReadableToB64Url = hashReadableToB64Url;
|
|
30
|
+
const hashReadableStreamToB64Url = (stream, algorithm = 'sha256') => {
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
const hash = (0, crypto_1.createHash)(algorithm);
|
|
33
|
+
const reader = stream.getReader();
|
|
34
|
+
const read = async () => {
|
|
35
|
+
try {
|
|
36
|
+
const { done, value } = await reader.read();
|
|
37
|
+
if (done) {
|
|
38
|
+
resolve((0, base64_js_1.toB64Url)(hash.digest()));
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
hash.update(value);
|
|
42
|
+
read();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
reject(err);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
read().catch(reject);
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
exports.hashReadableStreamToB64Url = hashReadableStreamToB64Url;
|
|
53
|
+
const hashBufferToB64Url = (buffer, algorithm = 'sha256') => {
|
|
54
|
+
return (0, base64_js_1.toB64Url)((0, crypto_1.createHash)(algorithm).update(buffer).digest());
|
|
55
|
+
};
|
|
56
|
+
exports.hashBufferToB64Url = hashBufferToB64Url;
|
package/lib/cjs/version.js
CHANGED
package/lib/esm/common/io.js
CHANGED
|
@@ -920,7 +920,10 @@ export class ARIOWriteable extends ARIOReadable {
|
|
|
920
920
|
}
|
|
921
921
|
return this.paymentProvider.initiateArNSPurchase({
|
|
922
922
|
intent: 'Buy-Name',
|
|
923
|
-
|
|
923
|
+
name: params.name,
|
|
924
|
+
years: params.years,
|
|
925
|
+
type: params.type,
|
|
926
|
+
processId: params.processId,
|
|
924
927
|
});
|
|
925
928
|
}
|
|
926
929
|
const { tags = [] } = options || {};
|
|
@@ -984,7 +987,8 @@ export class ARIOWriteable extends ARIOReadable {
|
|
|
984
987
|
}
|
|
985
988
|
return this.paymentProvider.initiateArNSPurchase({
|
|
986
989
|
intent: 'Extend-Lease',
|
|
987
|
-
|
|
990
|
+
name: params.name,
|
|
991
|
+
years: params.years,
|
|
988
992
|
});
|
|
989
993
|
}
|
|
990
994
|
const { tags = [] } = options || {};
|
|
@@ -1007,7 +1011,8 @@ export class ARIOWriteable extends ARIOReadable {
|
|
|
1007
1011
|
}
|
|
1008
1012
|
return this.paymentProvider.initiateArNSPurchase({
|
|
1009
1013
|
intent: 'Increase-Undername-Limit',
|
|
1010
|
-
|
|
1014
|
+
quantity: params.increaseCount,
|
|
1015
|
+
name: params.name,
|
|
1011
1016
|
});
|
|
1012
1017
|
}
|
|
1013
1018
|
const { tags = [] } = options || {};
|