@ar.io/sdk 3.13.0-beta.1 → 3.14.0-alpha.1

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 +1 -1
  2. package/bundles/web.bundle.min.js +133 -136
  3. package/lib/cjs/cli/cli.js +8 -0
  4. package/lib/cjs/common/ant.js +102 -1
  5. package/lib/cjs/common/index.js +0 -2
  6. package/lib/cjs/utils/ant.js +32 -1
  7. package/lib/cjs/utils/hash.js +31 -83
  8. package/lib/cjs/version.js +1 -1
  9. package/lib/esm/cli/cli.js +8 -0
  10. package/lib/esm/common/ant.js +103 -2
  11. package/lib/esm/common/index.js +0 -2
  12. package/lib/esm/utils/ant.js +30 -0
  13. package/lib/esm/utils/hash.js +27 -74
  14. package/lib/esm/version.js +1 -1
  15. package/lib/types/common/ant.d.ts +9 -1
  16. package/lib/types/common/index.d.ts +0 -1
  17. package/lib/types/types/ant.d.ts +17 -0
  18. package/lib/types/types/io.d.ts +0 -3
  19. package/lib/types/utils/ant.d.ts +7 -1
  20. package/lib/types/utils/hash.d.ts +4 -8
  21. package/lib/types/version.d.ts +1 -1
  22. package/package.json +1 -2
  23. package/lib/cjs/common/wayfinder/gateways/network.js +0 -48
  24. package/lib/cjs/common/wayfinder/gateways/simple-cache.js +0 -35
  25. package/lib/cjs/common/wayfinder/gateways/static.js +0 -13
  26. package/lib/cjs/common/wayfinder/index.js +0 -48
  27. package/lib/cjs/common/wayfinder/routing/strategies/ping.js +0 -72
  28. package/lib/cjs/common/wayfinder/routing/strategies/preferred-with-fallback.js +0 -50
  29. package/lib/cjs/common/wayfinder/routing/strategies/random.js +0 -13
  30. package/lib/cjs/common/wayfinder/routing/strategies/round-robin.js +0 -42
  31. package/lib/cjs/common/wayfinder/routing/strategies/static.js +0 -29
  32. package/lib/cjs/common/wayfinder/verification/strategies/data-root-verifier.js +0 -110
  33. package/lib/cjs/common/wayfinder/verification/strategies/hash-verifier.js +0 -27
  34. package/lib/cjs/common/wayfinder/verification/trusted.js +0 -125
  35. package/lib/cjs/common/wayfinder/wayfinder.js +0 -508
  36. package/lib/cjs/types/wayfinder.js +0 -3
  37. package/lib/esm/common/wayfinder/gateways/network.js +0 -44
  38. package/lib/esm/common/wayfinder/gateways/simple-cache.js +0 -31
  39. package/lib/esm/common/wayfinder/gateways/static.js +0 -9
  40. package/lib/esm/common/wayfinder/index.js +0 -32
  41. package/lib/esm/common/wayfinder/routing/strategies/ping.js +0 -68
  42. package/lib/esm/common/wayfinder/routing/strategies/preferred-with-fallback.js +0 -46
  43. package/lib/esm/common/wayfinder/routing/strategies/random.js +0 -9
  44. package/lib/esm/common/wayfinder/routing/strategies/round-robin.js +0 -38
  45. package/lib/esm/common/wayfinder/routing/strategies/static.js +0 -25
  46. package/lib/esm/common/wayfinder/verification/strategies/data-root-verifier.js +0 -102
  47. package/lib/esm/common/wayfinder/verification/strategies/hash-verifier.js +0 -23
  48. package/lib/esm/common/wayfinder/verification/trusted.js +0 -121
  49. package/lib/esm/common/wayfinder/wayfinder.js +0 -499
  50. package/lib/esm/types/wayfinder.js +0 -2
  51. package/lib/types/common/wayfinder/gateways/network.d.ts +0 -33
  52. package/lib/types/common/wayfinder/gateways/simple-cache.d.ts +0 -31
  53. package/lib/types/common/wayfinder/gateways/static.d.ts +0 -23
  54. package/lib/types/common/wayfinder/index.d.ts +0 -27
  55. package/lib/types/common/wayfinder/routing/strategies/ping.d.ts +0 -27
  56. package/lib/types/common/wayfinder/routing/strategies/preferred-with-fallback.d.ts +0 -31
  57. package/lib/types/common/wayfinder/routing/strategies/random.d.ts +0 -21
  58. package/lib/types/common/wayfinder/routing/strategies/round-robin.d.ts +0 -29
  59. package/lib/types/common/wayfinder/routing/strategies/static.d.ts +0 -29
  60. package/lib/types/common/wayfinder/verification/strategies/data-root-verifier.d.ts +0 -27
  61. package/lib/types/common/wayfinder/verification/strategies/hash-verifier.d.ts +0 -26
  62. package/lib/types/common/wayfinder/verification/trusted.d.ts +0 -51
  63. package/lib/types/common/wayfinder/wayfinder.d.ts +0 -257
  64. package/lib/types/types/wayfinder.d.ts +0 -66
@@ -1,110 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.DataRootVerificationStrategy = exports.convertDataStreamToDataRoot = void 0;
7
- /**
8
- * Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
9
- *
10
- * Licensed under the Apache License, Version 2.0 (the "License");
11
- * you may not use this file except in compliance with the License.
12
- * You may obtain a copy of the License at
13
- *
14
- * http://www.apache.org/licenses/LICENSE-2.0
15
- *
16
- * Unless required by applicable law or agreed to in writing, software
17
- * distributed under the License is distributed on an "AS IS" BASIS,
18
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
- * See the License for the specific language governing permissions and
20
- * limitations under the License.
21
- */
22
- const arweave_1 = __importDefault(require("arweave"));
23
- const merkle_js_1 = require("arweave/node/lib/merkle.js");
24
- const base64_js_1 = require("../../../../utils/base64.js");
25
- const hash_js_1 = require("../../../../utils/hash.js");
26
- const convertDataStreamToDataRoot = async ({ dataStream, }) => {
27
- const chunks = [];
28
- let leftover = new Uint8Array(0);
29
- let cursor = 0;
30
- const asyncIterable = (0, hash_js_1.isAsyncIterable)(dataStream)
31
- ? dataStream
32
- : (0, hash_js_1.readableStreamToAsyncIterable)(dataStream);
33
- for await (const data of asyncIterable) {
34
- const inputChunk = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
35
- const combined = new Uint8Array(leftover.length + inputChunk.length);
36
- combined.set(leftover, 0);
37
- combined.set(inputChunk, leftover.length);
38
- let startIndex = 0;
39
- while (combined.length - startIndex >= merkle_js_1.MAX_CHUNK_SIZE) {
40
- let chunkSize = merkle_js_1.MAX_CHUNK_SIZE;
41
- const remainderAfterThis = combined.length - startIndex - merkle_js_1.MAX_CHUNK_SIZE;
42
- if (remainderAfterThis > 0 && remainderAfterThis < merkle_js_1.MIN_CHUNK_SIZE) {
43
- chunkSize = Math.ceil((combined.length - startIndex) / 2);
44
- }
45
- const chunkData = combined.slice(startIndex, startIndex + chunkSize);
46
- const dataHash = await arweave_1.default.crypto.hash(chunkData);
47
- chunks.push({
48
- dataHash,
49
- minByteRange: cursor,
50
- maxByteRange: cursor + chunkSize,
51
- });
52
- cursor += chunkSize;
53
- startIndex += chunkSize;
54
- }
55
- leftover = combined.slice(startIndex);
56
- }
57
- if (leftover.length > 0) {
58
- // TODO: ensure a web friendly crypto hash function is used in web
59
- const dataHash = await arweave_1.default.crypto.hash(leftover);
60
- chunks.push({
61
- dataHash,
62
- minByteRange: cursor,
63
- maxByteRange: cursor + leftover.length,
64
- });
65
- }
66
- const leaves = await (0, merkle_js_1.generateLeaves)(chunks);
67
- const root = await (0, merkle_js_1.buildLayers)(leaves);
68
- return (0, base64_js_1.toB64Url)(Buffer.from(root.id));
69
- };
70
- exports.convertDataStreamToDataRoot = convertDataStreamToDataRoot;
71
- class DataRootVerificationStrategy {
72
- trustedDataRootProvider;
73
- constructor({ trustedDataRootProvider, }) {
74
- this.trustedDataRootProvider = trustedDataRootProvider;
75
- }
76
- async verifyData({ data, txId, }) {
77
- const [computedDataRoot, trustedDataRoot] = await Promise.all([
78
- (0, exports.convertDataStreamToDataRoot)({
79
- dataStream: data,
80
- }),
81
- this.trustedDataRootProvider.getDataRoot({
82
- txId,
83
- }),
84
- ]);
85
- if (computedDataRoot !== trustedDataRoot) {
86
- throw new Error('Data root does not match', {
87
- cause: { computedDataRoot, trustedDataRoot },
88
- });
89
- }
90
- }
91
- }
92
- exports.DataRootVerificationStrategy = DataRootVerificationStrategy;
93
- // some data item options
94
- // compute and verify data root, use offsets from server and verify the signature that the data item at the offset matches the signature
95
- // does not give you assurance of valid bundle, but gives verification that the data item itself is valid
96
- // reading from offsets is the only way for the client to compute and verify the signature
97
- /**
98
- * - when you get a signature of a data item, you can only verify the owner
99
- * - you still need to verify it's going back to the bundle, unpack it, and verify the data item exists at the offset
100
- * - you need to the location of the chunks for the data item, and prove it's in the chunk and then prove the data root of the bundle, then you have fully verified the data verifier
101
- * - how to prove the data item is on arweave - verify the merkle hash that the chunks for the data item, fit within the expected tree of the parent bundle
102
- *
103
- * Composite verifier - you'll want to be very efficient with streams
104
- * - hash verifier
105
- * - parent chunks verifier --> for any range of data within a single transaction, tell me that it's correct
106
- * - signature verifier
107
- * - offset verifier
108
- * - data item verifier
109
- */
110
- // introduce a composite verifier that determines where/how to lookup the hash
@@ -1,27 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HashVerificationStrategy = void 0;
4
- const hash_js_1 = require("../../../../utils/hash.js");
5
- class HashVerificationStrategy {
6
- trustedHashProvider;
7
- constructor({ trustedHashProvider, }) {
8
- this.trustedHashProvider = trustedHashProvider;
9
- }
10
- async verifyData({ data, txId, }) {
11
- // kick off the hash computation, but don't wait for it until we compute our own hash
12
- const [computedHash, fetchedHash] = await Promise.all([
13
- (0, hash_js_1.hashDataStreamToB64Url)(data),
14
- this.trustedHashProvider.getHash({ txId }),
15
- ]);
16
- // await on the hash promise and compare to get a little concurrency when computing hashes over larger data
17
- if (computedHash === undefined) {
18
- throw new Error('Hash could not be computed');
19
- }
20
- if (computedHash !== fetchedHash.hash) {
21
- throw new Error('Hash does not match', {
22
- cause: { computedHash, trustedHash: fetchedHash },
23
- });
24
- }
25
- }
26
- }
27
- exports.HashVerificationStrategy = HashVerificationStrategy;
@@ -1,125 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TrustedGatewaysHashProvider = void 0;
4
- const wayfinder_js_1 = require("../wayfinder.js");
5
- const arioGatewayHeaders = {
6
- digest: 'x-ar-io-digest',
7
- verified: 'x-ar-io-verified',
8
- txId: 'x-arns-resolved-tx-id',
9
- processId: 'x-arns-resolved-process-id',
10
- };
11
- class TrustedGatewaysHashProvider {
12
- gatewaysProvider;
13
- constructor({ gatewaysProvider,
14
- // TODO: add threshold for allowed hash difference (i.e. by count or ratio of total gateways checked)
15
- }) {
16
- this.gatewaysProvider = gatewaysProvider;
17
- }
18
- /**
19
- * Gets the digest for a given txId from all trusted gateways and ensures they all match.
20
- * @param txId - The txId to get the digest for.
21
- * @returns The digest for the given txId.
22
- */
23
- async getHash({ txId, }) {
24
- // get the hash from every gateway, and ensure they all match
25
- const hashSet = new Set();
26
- const hashResults = [];
27
- const gateways = await this.gatewaysProvider.getGateways();
28
- const hashes = await Promise.all(gateways.map(async (gateway) => {
29
- const sandbox = (0, wayfinder_js_1.sandboxFromId)(txId);
30
- const urlWithSandbox = `${gateway.protocol}//${sandbox}.${gateway.hostname}/${txId}`;
31
- let txIdHash;
32
- /**
33
- * This is a problem because we're not able to verify the hash of the data item if the gateway doesn't have the data in its cache.
34
- * We should add the ability to send a HEAD request to trigger a GET request to hydrate the cache on the trusted gateway via a header.
35
- * For now, we'll just do a GET request to hydrate the cache if the HEAD request doesn't contain the digest.
36
- */
37
- for (const method of ['GET', 'GET']) {
38
- const response = await fetch(urlWithSandbox, {
39
- method,
40
- redirect: 'follow',
41
- mode: 'cors',
42
- headers: {
43
- 'Cache-Control': 'no-cache',
44
- },
45
- });
46
- if (!response.ok) {
47
- // skip if the request failed or the digest is not present
48
- return;
49
- }
50
- const fetchedTxIdHash = response.headers.get(arioGatewayHeaders.digest);
51
- if (fetchedTxIdHash !== null && fetchedTxIdHash !== undefined) {
52
- txIdHash = fetchedTxIdHash;
53
- break;
54
- }
55
- }
56
- if (txIdHash === undefined) {
57
- // skip this gateway if we didn't get a hash
58
- return undefined;
59
- }
60
- hashResults.push({
61
- gateway: gateway.hostname,
62
- txIdHash,
63
- });
64
- return txIdHash;
65
- }));
66
- for (const hash of hashes) {
67
- if (hash !== undefined) {
68
- hashSet.add(hash);
69
- }
70
- }
71
- if (hashSet.size === 0) {
72
- throw new Error(`No trusted gateways returned a hash for txId ${txId}`);
73
- }
74
- if (hashSet.size > 1) {
75
- throw new Error(`Failed to get consistent hash from all trusted gateways. ${JSON.stringify(hashResults)}`);
76
- }
77
- return { hash: hashResults[0].txIdHash, algorithm: 'sha256' };
78
- }
79
- /**
80
- * Get the data root for a given txId from all trusted gateways and ensure they all match.
81
- * @param txId - The txId to get the data root for.
82
- * @returns The data root for the given txId.
83
- */
84
- async getDataRoot({ txId }) {
85
- const dataRootSet = new Set();
86
- const dataRootResults = [];
87
- const gateways = await this.gatewaysProvider.getGateways();
88
- const dataRoots = await Promise.all(gateways.map(async (gateway) => {
89
- const response = await fetch(`${gateway.toString()}tx/${txId}/data_root`);
90
- if (!response.ok) {
91
- // skip this gateway
92
- return undefined;
93
- }
94
- const dataRoot = await response.text();
95
- dataRootResults.push({
96
- gateway: gateway.hostname,
97
- dataRoot,
98
- });
99
- return dataRoot;
100
- }));
101
- for (const dataRoot of dataRoots) {
102
- if (dataRoot !== undefined) {
103
- dataRootSet.add(dataRoot);
104
- }
105
- }
106
- if (dataRootSet.size > 1) {
107
- throw new Error(`Failed to get consistent data root from all trusted gateways. ${JSON.stringify(dataRootResults)}`);
108
- }
109
- return dataRootSet.values().next().value;
110
- }
111
- }
112
- exports.TrustedGatewaysHashProvider = TrustedGatewaysHashProvider;
113
- // client could check hashes of data items, match expected hash
114
- // if the gateway has the hash and they've verified it, you can trust the data item and offset
115
- // you would be only trusting the gateway that it is a valid bundle
116
- // you can request the offset from the gateway to verify the id
117
- /**
118
- * Note from @djwhitt
119
- *
120
- * Calculating data roots this way is fine, but it may not reproduce the original data root.
121
- * We could also implement a data root verifier that pulls all the chunks, checks that they
122
- * reproduce the expected data root, and then compares the concatenated chunk data to the
123
- * original data retrieved. That would take a while, but it should be able to verify any L1
124
- * data where we can find the chunks.
125
- */