@ar.io/sdk 3.11.0-alpha.7 → 3.11.0-alpha.9
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/README.md +52 -0
- package/bundles/web.bundle.min.js +119 -116
- package/lib/cjs/cli/cli.js +137 -122
- package/lib/cjs/cli/commands/readCommands.js +6 -0
- package/lib/cjs/common/ant.js +5 -5
- package/lib/cjs/common/io.js +37 -0
- package/lib/cjs/common/wayfinder/gateways/trusted-gateways.js +106 -0
- package/lib/cjs/common/wayfinder/index.js +6 -0
- 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 +407 -18
- package/lib/cjs/common/wayfinder/wayfinder.test.js +262 -3
- package/lib/cjs/types/wayfinder.js +1 -0
- package/lib/cjs/utils/hash.js +56 -0
- package/lib/cjs/version.js +1 -1
- package/lib/esm/cli/cli.js +138 -123
- package/lib/esm/cli/commands/readCommands.js +5 -0
- package/lib/esm/common/ant.js +5 -5
- package/lib/esm/common/io.js +37 -0
- package/lib/esm/common/wayfinder/gateways/trusted-gateways.js +102 -0
- package/lib/esm/common/wayfinder/index.js +6 -0
- 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 +401 -18
- package/lib/esm/common/wayfinder/wayfinder.test.js +263 -4
- package/lib/esm/types/wayfinder.js +1 -0
- package/lib/esm/utils/hash.js +50 -0
- package/lib/esm/version.js +1 -1
- package/lib/types/cli/commands/readCommands.d.ts +1 -0
- package/lib/types/common/io.d.ts +5 -2
- package/lib/types/common/wayfinder/gateways/trusted-gateways.d.ts +51 -0
- package/lib/types/common/wayfinder/index.d.ts +3 -0
- 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/io.d.ts +16 -1
- 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
|
@@ -0,0 +1,139 @@
|
|
|
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.DataRootVerifier = exports.convertReadableToDataRoot = void 0;
|
|
7
|
+
exports.convertBufferToDataRoot = convertBufferToDataRoot;
|
|
8
|
+
/**
|
|
9
|
+
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
|
|
10
|
+
*
|
|
11
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
12
|
+
* you may not use this file except in compliance with the License.
|
|
13
|
+
* You may obtain a copy of the License at
|
|
14
|
+
*
|
|
15
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
*
|
|
17
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
18
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
19
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
20
|
+
* See the License for the specific language governing permissions and
|
|
21
|
+
* limitations under the License.
|
|
22
|
+
*/
|
|
23
|
+
const arweave_1 = __importDefault(require("arweave"));
|
|
24
|
+
const merkle_js_1 = require("arweave/node/lib/merkle.js");
|
|
25
|
+
const node_stream_1 = require("node:stream");
|
|
26
|
+
const base64_js_1 = require("../../../utils/base64.js");
|
|
27
|
+
async function convertBufferToDataRoot({ buffer, }) {
|
|
28
|
+
const chunks = [];
|
|
29
|
+
let cursor = 0;
|
|
30
|
+
let offset = 0;
|
|
31
|
+
while (offset < buffer.byteLength) {
|
|
32
|
+
let chunkSize = Math.min(merkle_js_1.MAX_CHUNK_SIZE, buffer.byteLength - offset);
|
|
33
|
+
const remainder = buffer.byteLength - offset - chunkSize;
|
|
34
|
+
if (remainder > 0 && remainder < merkle_js_1.MIN_CHUNK_SIZE) {
|
|
35
|
+
chunkSize = Math.ceil((buffer.byteLength - offset) / 2);
|
|
36
|
+
}
|
|
37
|
+
// subarray does not exist on web Buffer type
|
|
38
|
+
const slice = buffer.subarray(offset, offset + chunkSize);
|
|
39
|
+
const hash = await crypto.subtle.digest('SHA-256', slice);
|
|
40
|
+
const hashArray = new Uint8Array(hash);
|
|
41
|
+
chunks.push({
|
|
42
|
+
dataHash: hashArray,
|
|
43
|
+
minByteRange: cursor,
|
|
44
|
+
maxByteRange: cursor + chunkSize,
|
|
45
|
+
});
|
|
46
|
+
cursor += chunkSize;
|
|
47
|
+
offset += chunkSize;
|
|
48
|
+
}
|
|
49
|
+
const leaves = await (0, merkle_js_1.generateLeaves)(chunks);
|
|
50
|
+
const result = await (0, merkle_js_1.buildLayers)(leaves);
|
|
51
|
+
return Buffer.from(result.id).toString('base64url');
|
|
52
|
+
}
|
|
53
|
+
const convertReadableToDataRoot = async ({ iterable, }) => {
|
|
54
|
+
const chunks = [];
|
|
55
|
+
let leftover = new Uint8Array(0);
|
|
56
|
+
let cursor = 0;
|
|
57
|
+
for await (const data of iterable) {
|
|
58
|
+
const inputChunk = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
59
|
+
const combined = new Uint8Array(leftover.length + inputChunk.length);
|
|
60
|
+
combined.set(leftover, 0);
|
|
61
|
+
combined.set(inputChunk, leftover.length);
|
|
62
|
+
let startIndex = 0;
|
|
63
|
+
while (combined.length - startIndex >= merkle_js_1.MAX_CHUNK_SIZE) {
|
|
64
|
+
let chunkSize = merkle_js_1.MAX_CHUNK_SIZE;
|
|
65
|
+
const remainderAfterThis = combined.length - startIndex - merkle_js_1.MAX_CHUNK_SIZE;
|
|
66
|
+
if (remainderAfterThis > 0 && remainderAfterThis < merkle_js_1.MIN_CHUNK_SIZE) {
|
|
67
|
+
chunkSize = Math.ceil((combined.length - startIndex) / 2);
|
|
68
|
+
}
|
|
69
|
+
const chunkData = combined.slice(startIndex, startIndex + chunkSize);
|
|
70
|
+
const dataHash = await arweave_1.default.crypto.hash(chunkData);
|
|
71
|
+
chunks.push({
|
|
72
|
+
dataHash,
|
|
73
|
+
minByteRange: cursor,
|
|
74
|
+
maxByteRange: cursor + chunkSize,
|
|
75
|
+
});
|
|
76
|
+
cursor += chunkSize;
|
|
77
|
+
startIndex += chunkSize;
|
|
78
|
+
}
|
|
79
|
+
leftover = combined.slice(startIndex);
|
|
80
|
+
}
|
|
81
|
+
if (leftover.length > 0) {
|
|
82
|
+
const dataHash = await arweave_1.default.crypto.hash(leftover);
|
|
83
|
+
chunks.push({
|
|
84
|
+
dataHash,
|
|
85
|
+
minByteRange: cursor,
|
|
86
|
+
maxByteRange: cursor + leftover.length,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
const leaves = await (0, merkle_js_1.generateLeaves)(chunks);
|
|
90
|
+
const root = await (0, merkle_js_1.buildLayers)(leaves);
|
|
91
|
+
return (0, base64_js_1.toB64Url)(Buffer.from(root.id));
|
|
92
|
+
};
|
|
93
|
+
exports.convertReadableToDataRoot = convertReadableToDataRoot;
|
|
94
|
+
class DataRootVerifier {
|
|
95
|
+
trustedDataRootProvider;
|
|
96
|
+
constructor({ trustedDataRootProvider, }) {
|
|
97
|
+
this.trustedDataRootProvider = trustedDataRootProvider;
|
|
98
|
+
}
|
|
99
|
+
async verifyData({ data, txId, }) {
|
|
100
|
+
const trustedDataRootPromise = this.trustedDataRootProvider.getDataRoot({
|
|
101
|
+
txId,
|
|
102
|
+
});
|
|
103
|
+
let computedDataRoot;
|
|
104
|
+
if (Buffer.isBuffer(data)) {
|
|
105
|
+
computedDataRoot = await convertBufferToDataRoot({ buffer: data });
|
|
106
|
+
}
|
|
107
|
+
else if (data instanceof node_stream_1.Readable || data instanceof ReadableStream) {
|
|
108
|
+
computedDataRoot = await (0, exports.convertReadableToDataRoot)({ iterable: data });
|
|
109
|
+
}
|
|
110
|
+
if (computedDataRoot === undefined) {
|
|
111
|
+
throw new Error('Data root could not be computed');
|
|
112
|
+
}
|
|
113
|
+
const trustedDataRoot = await trustedDataRootPromise;
|
|
114
|
+
if (computedDataRoot !== trustedDataRoot) {
|
|
115
|
+
throw new Error('Data root does not match', {
|
|
116
|
+
cause: { computedDataRoot, trustedDataRoot },
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
exports.DataRootVerifier = DataRootVerifier;
|
|
122
|
+
// some data item options
|
|
123
|
+
// compute and verify data root, use offsets from server and verify the signature that the data item at the offset matches the signature
|
|
124
|
+
// does not give you assurance of valid bundle, but gives verification that the data item itself is valid
|
|
125
|
+
// reading from offsets is the only way for the client to compute and verify the signature
|
|
126
|
+
/**
|
|
127
|
+
* - when you get a signature of a data item, you can only verify the owner
|
|
128
|
+
* - you still need to verify it's going back to the bundle, unpack it, and verify the data item exists at the offset
|
|
129
|
+
* - 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
|
|
130
|
+
* - 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
|
|
131
|
+
*
|
|
132
|
+
* Composite verifier - you'll want to be very efficient with streams
|
|
133
|
+
* - hash verifier
|
|
134
|
+
* - parent chunks verifier --> for any range of data within a single transaction, tell me that it's correct
|
|
135
|
+
* - signature verifier
|
|
136
|
+
* - offset verifier
|
|
137
|
+
* - data item verifier
|
|
138
|
+
*/
|
|
139
|
+
// introduce a composite verifier that determines where/how to lookup the hash
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HashVerifier = 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 node_stream_1 = require("node:stream");
|
|
20
|
+
const hash_js_1 = require("../../../utils/hash.js");
|
|
21
|
+
class HashVerifier {
|
|
22
|
+
trustedHashProvider;
|
|
23
|
+
constructor({ trustedHashProvider, }) {
|
|
24
|
+
this.trustedHashProvider = trustedHashProvider;
|
|
25
|
+
}
|
|
26
|
+
async verifyData({ data, txId, }) {
|
|
27
|
+
const hashPromise = this.trustedHashProvider.getHash({ txId });
|
|
28
|
+
let computedHash;
|
|
29
|
+
if (Buffer.isBuffer(data)) {
|
|
30
|
+
computedHash = (0, hash_js_1.hashBufferToB64Url)(data);
|
|
31
|
+
}
|
|
32
|
+
else if (data instanceof node_stream_1.Readable) {
|
|
33
|
+
computedHash = await (0, hash_js_1.hashReadableToB64Url)(data);
|
|
34
|
+
}
|
|
35
|
+
else if (data instanceof ReadableStream) {
|
|
36
|
+
computedHash = await (0, hash_js_1.hashReadableStreamToB64Url)(data);
|
|
37
|
+
}
|
|
38
|
+
// await on the hash promise and compare to get a little concurrency when computing hashes over larger data
|
|
39
|
+
const { hash } = await hashPromise;
|
|
40
|
+
if (computedHash === undefined) {
|
|
41
|
+
throw new Error('Hash could not be computed');
|
|
42
|
+
}
|
|
43
|
+
if (computedHash !== hash) {
|
|
44
|
+
throw new Error('Hash does not match', {
|
|
45
|
+
cause: { computedHash, trustedHash: hash },
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.HashVerifier = HashVerifier;
|