@ar.io/sdk 3.12.0-alpha.2 → 3.12.0-beta.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.
- package/bundles/web.bundle.min.js +170 -167
- package/lib/cjs/common/index.js +2 -0
- package/lib/cjs/common/wayfinder/gateways/network.js +48 -0
- package/lib/cjs/common/wayfinder/gateways/simple-cache.js +35 -0
- package/lib/cjs/common/wayfinder/gateways/static.js +13 -0
- package/lib/cjs/common/wayfinder/index.js +47 -0
- package/lib/cjs/common/wayfinder/routing/strategies/ping.js +72 -0
- package/lib/cjs/common/wayfinder/routing/strategies/random.js +13 -0
- package/lib/cjs/common/wayfinder/routing/strategies/round-robin.js +42 -0
- package/lib/cjs/common/wayfinder/routing/strategies/static.js +29 -0
- package/lib/cjs/common/wayfinder/verification/strategies/data-root-verifier.js +139 -0
- package/lib/cjs/common/wayfinder/verification/strategies/hash-verifier.js +50 -0
- package/lib/cjs/common/wayfinder/verification/trusted.js +106 -0
- package/lib/cjs/common/wayfinder/wayfinder.js +735 -0
- package/lib/cjs/types/wayfinder.js +3 -0
- package/lib/cjs/version.js +1 -1
- package/lib/esm/common/index.js +2 -0
- package/lib/esm/common/wayfinder/gateways/network.js +44 -0
- package/lib/esm/common/wayfinder/gateways/simple-cache.js +31 -0
- package/lib/esm/common/wayfinder/gateways/static.js +9 -0
- package/lib/esm/common/wayfinder/index.js +31 -0
- package/lib/esm/common/wayfinder/routing/strategies/ping.js +68 -0
- package/lib/esm/common/wayfinder/routing/strategies/random.js +9 -0
- package/lib/esm/common/wayfinder/routing/strategies/round-robin.js +38 -0
- package/lib/esm/common/wayfinder/routing/strategies/static.js +25 -0
- package/lib/esm/common/wayfinder/verification/strategies/data-root-verifier.js +130 -0
- package/lib/esm/common/wayfinder/verification/strategies/hash-verifier.js +46 -0
- package/lib/esm/common/wayfinder/verification/trusted.js +102 -0
- package/lib/esm/common/wayfinder/wayfinder.js +723 -0
- package/lib/esm/types/wayfinder.js +2 -0
- package/lib/esm/version.js +1 -1
- package/lib/types/common/index.d.ts +1 -0
- package/lib/types/common/wayfinder/gateways/network.d.ts +33 -0
- package/lib/types/common/wayfinder/gateways/simple-cache.d.ts +31 -0
- package/lib/types/common/wayfinder/gateways/static.d.ts +23 -0
- package/lib/types/common/wayfinder/index.d.ts +26 -0
- package/lib/types/common/wayfinder/routing/strategies/ping.d.ts +27 -0
- package/lib/types/common/wayfinder/routing/strategies/random.d.ts +21 -0
- package/lib/types/common/wayfinder/routing/strategies/round-robin.d.ts +29 -0
- package/lib/types/common/wayfinder/routing/strategies/static.d.ts +29 -0
- package/lib/types/common/wayfinder/verification/strategies/data-root-verifier.d.ts +31 -0
- package/lib/types/common/wayfinder/verification/strategies/hash-verifier.d.ts +27 -0
- package/lib/types/common/wayfinder/verification/trusted.d.ts +51 -0
- package/lib/types/common/wayfinder/wayfinder.d.ts +299 -0
- package/lib/types/types/wayfinder.d.ts +66 -0
- package/lib/types/version.d.ts +1 -1
- package/package.json +1 -1
package/lib/cjs/version.js
CHANGED
package/lib/esm/common/index.js
CHANGED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ARIO } from '../../io.js';
|
|
2
|
+
export class NetworkGatewaysProvider {
|
|
3
|
+
ario;
|
|
4
|
+
sortBy;
|
|
5
|
+
sortOrder;
|
|
6
|
+
limit;
|
|
7
|
+
filter;
|
|
8
|
+
constructor({ ario = ARIO.mainnet(), sortBy = 'operatorStake', sortOrder = 'desc', limit = 1000, filter = (g) => g.status === 'joined', }) {
|
|
9
|
+
this.ario = ario;
|
|
10
|
+
this.sortBy = sortBy;
|
|
11
|
+
this.sortOrder = sortOrder;
|
|
12
|
+
this.limit = limit;
|
|
13
|
+
this.filter = filter;
|
|
14
|
+
}
|
|
15
|
+
async getGateways() {
|
|
16
|
+
let cursor;
|
|
17
|
+
let attempts = 0;
|
|
18
|
+
const gateways = [];
|
|
19
|
+
do {
|
|
20
|
+
try {
|
|
21
|
+
const { items: newGateways = [], nextCursor } = await this.ario.getGateways({
|
|
22
|
+
limit: 1000,
|
|
23
|
+
cursor,
|
|
24
|
+
sortBy: this.sortBy,
|
|
25
|
+
sortOrder: this.sortOrder,
|
|
26
|
+
});
|
|
27
|
+
gateways.push(...newGateways);
|
|
28
|
+
cursor = nextCursor;
|
|
29
|
+
attempts = 0; // reset attempts if we get a new cursor
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
console.error('Error fetching gateways', {
|
|
33
|
+
cursor,
|
|
34
|
+
attempts,
|
|
35
|
+
error,
|
|
36
|
+
});
|
|
37
|
+
attempts++;
|
|
38
|
+
}
|
|
39
|
+
} while (cursor !== undefined && attempts < 3);
|
|
40
|
+
// filter out any gateways that are not joined
|
|
41
|
+
const filteredGateways = gateways.filter(this.filter).slice(0, this.limit);
|
|
42
|
+
return filteredGateways.map((g) => new URL(`${g.settings.protocol}://${g.settings.fqdn}:${g.settings.port}`));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Logger } from '../../../web/index.js';
|
|
2
|
+
export class SimpleCacheGatewaysProvider {
|
|
3
|
+
gatewaysProvider;
|
|
4
|
+
ttlSeconds;
|
|
5
|
+
lastUpdated;
|
|
6
|
+
gatewaysCache;
|
|
7
|
+
logger;
|
|
8
|
+
constructor({ gatewaysProvider, ttlSeconds = 60 * 60, // 1 hour
|
|
9
|
+
logger = Logger.default, }) {
|
|
10
|
+
this.gatewaysCache = [];
|
|
11
|
+
this.gatewaysProvider = gatewaysProvider;
|
|
12
|
+
this.ttlSeconds = ttlSeconds;
|
|
13
|
+
this.logger = logger;
|
|
14
|
+
}
|
|
15
|
+
async getGateways() {
|
|
16
|
+
const now = Date.now();
|
|
17
|
+
if (this.gatewaysCache.length === 0 ||
|
|
18
|
+
now - this.lastUpdated > this.ttlSeconds * 1000) {
|
|
19
|
+
try {
|
|
20
|
+
// preserve the cache if the fetch fails
|
|
21
|
+
const allGateways = await this.gatewaysProvider.getGateways();
|
|
22
|
+
this.gatewaysCache = allGateways;
|
|
23
|
+
this.lastUpdated = now;
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
this.logger.error('Error fetching gateways', error);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return this.gatewaysCache;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
export * from './wayfinder.js';
|
|
17
|
+
// routing strategies
|
|
18
|
+
export * from './routing/strategies/random.js';
|
|
19
|
+
export * from './routing/strategies/static.js';
|
|
20
|
+
export * from './routing/strategies/ping.js';
|
|
21
|
+
export * from './routing/strategies/round-robin.js';
|
|
22
|
+
// gateways providers
|
|
23
|
+
export * from './gateways/network.js';
|
|
24
|
+
export * from './gateways/simple-cache.js';
|
|
25
|
+
export * from './gateways/static.js';
|
|
26
|
+
// trusted gateways
|
|
27
|
+
export * from './verification/trusted.js';
|
|
28
|
+
// hash providers
|
|
29
|
+
export * from './verification/strategies/data-root-verifier.js';
|
|
30
|
+
export * from './verification/strategies/hash-verifier.js';
|
|
31
|
+
// TODO: signature verification
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export class FastestPingRoutingStrategy {
|
|
2
|
+
timeoutMs;
|
|
3
|
+
probePath;
|
|
4
|
+
constructor({ timeoutMs = 500, probePath = '/ar-io/info', // TODO: limit to allowed /ar-io and arweave node endpoints
|
|
5
|
+
} = {}) {
|
|
6
|
+
this.timeoutMs = timeoutMs;
|
|
7
|
+
this.probePath = probePath;
|
|
8
|
+
}
|
|
9
|
+
async selectGateway({ gateways }) {
|
|
10
|
+
if (gateways.length === 0) {
|
|
11
|
+
throw new Error('No gateways provided');
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
const results = await Promise.allSettled(gateways.map(async (gateway) => {
|
|
15
|
+
try {
|
|
16
|
+
const startTime = Date.now();
|
|
17
|
+
const response = await fetch(`${gateway.toString().replace(/\/$/, '')}${this.probePath}`, {
|
|
18
|
+
method: 'HEAD',
|
|
19
|
+
signal: AbortSignal.timeout(this.timeoutMs),
|
|
20
|
+
});
|
|
21
|
+
const endTime = Date.now();
|
|
22
|
+
const durationMs = endTime - startTime;
|
|
23
|
+
return {
|
|
24
|
+
gateway,
|
|
25
|
+
status: response.status,
|
|
26
|
+
durationMs,
|
|
27
|
+
error: null,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
// Handle network errors
|
|
32
|
+
return {
|
|
33
|
+
gateway,
|
|
34
|
+
status: 'rejected',
|
|
35
|
+
durationMs: Infinity,
|
|
36
|
+
error,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}));
|
|
40
|
+
// Process results
|
|
41
|
+
const processedResults = results.map((result, index) => {
|
|
42
|
+
if (result.status === 'fulfilled') {
|
|
43
|
+
return result.value;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
return {
|
|
47
|
+
gateway: gateways[index],
|
|
48
|
+
status: 'rejected',
|
|
49
|
+
durationMs: Infinity,
|
|
50
|
+
error: result.reason,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
// Filter healthy gateways and sort by latency
|
|
55
|
+
const healthyGateways = processedResults
|
|
56
|
+
.filter((result) => result.status === 200)
|
|
57
|
+
.sort((a, b) => a.durationMs - b.durationMs);
|
|
58
|
+
if (healthyGateways.length > 0) {
|
|
59
|
+
return healthyGateways[0].gateway;
|
|
60
|
+
}
|
|
61
|
+
throw new Error('No healthy gateways found');
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
throw new Error('Failed to ping gateways: ' +
|
|
65
|
+
(error instanceof Error ? error.message : String(error)));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { randomInt } from '../../../../utils/random.js';
|
|
2
|
+
export class RandomRoutingStrategy {
|
|
3
|
+
async selectGateway({ gateways }) {
|
|
4
|
+
if (gateways.length === 0) {
|
|
5
|
+
throw new Error('No gateways available');
|
|
6
|
+
}
|
|
7
|
+
return gateways[randomInt(0, gateways.length)];
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
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 { Logger } from '../../../../common/logger.js';
|
|
17
|
+
export class RoundRobinRoutingStrategy {
|
|
18
|
+
gateways;
|
|
19
|
+
currentIndex;
|
|
20
|
+
logger;
|
|
21
|
+
constructor({ gateways, logger = Logger.default, }) {
|
|
22
|
+
this.gateways = gateways;
|
|
23
|
+
this.currentIndex = 0;
|
|
24
|
+
this.logger = logger;
|
|
25
|
+
}
|
|
26
|
+
// provided gateways are ignored
|
|
27
|
+
async selectGateway({ gateways = [], } = {}) {
|
|
28
|
+
if (gateways.length > 0) {
|
|
29
|
+
this.logger.warn('RoundRobinRoutingStrategy does not accept provided gateways. Ignoring provided gateways...', {
|
|
30
|
+
providedGateways: gateways.length,
|
|
31
|
+
internalGateways: this.gateways,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
const gateway = this.gateways[this.currentIndex];
|
|
35
|
+
this.currentIndex = (this.currentIndex + 1) % this.gateways.length;
|
|
36
|
+
return gateway;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Logger } from '../../../logger.js';
|
|
2
|
+
export class StaticRoutingStrategy {
|
|
3
|
+
name = 'static';
|
|
4
|
+
gateway;
|
|
5
|
+
logger;
|
|
6
|
+
constructor({ gateway, logger = Logger.default, }) {
|
|
7
|
+
try {
|
|
8
|
+
this.gateway = new URL(gateway);
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
throw new Error(`Invalid URL provided for static gateway: ${gateway}`);
|
|
12
|
+
}
|
|
13
|
+
this.logger = logger;
|
|
14
|
+
}
|
|
15
|
+
// provided gateways are ignored
|
|
16
|
+
async selectGateway({ gateways = [], } = {}) {
|
|
17
|
+
if (gateways.length > 0) {
|
|
18
|
+
this.logger.warn('StaticRoutingStrategy does not accept provided gateways. Ignoring provided gateways...', {
|
|
19
|
+
providedGateways: gateways.length,
|
|
20
|
+
internalGateway: this.gateway,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return this.gateway;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
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 Arweave from 'arweave';
|
|
17
|
+
import { MAX_CHUNK_SIZE, MIN_CHUNK_SIZE, buildLayers, generateLeaves, } from 'arweave/node/lib/merkle.js';
|
|
18
|
+
import { Readable } from 'node:stream';
|
|
19
|
+
import { toB64Url } from '../../../../utils/base64.js';
|
|
20
|
+
export async function convertBufferToDataRoot({ buffer, }) {
|
|
21
|
+
const chunks = [];
|
|
22
|
+
let cursor = 0;
|
|
23
|
+
let offset = 0;
|
|
24
|
+
while (offset < buffer.byteLength) {
|
|
25
|
+
let chunkSize = Math.min(MAX_CHUNK_SIZE, buffer.byteLength - offset);
|
|
26
|
+
const remainder = buffer.byteLength - offset - chunkSize;
|
|
27
|
+
if (remainder > 0 && remainder < MIN_CHUNK_SIZE) {
|
|
28
|
+
chunkSize = Math.ceil((buffer.byteLength - offset) / 2);
|
|
29
|
+
}
|
|
30
|
+
// subarray does not exist on web Buffer type
|
|
31
|
+
const slice = buffer.subarray(offset, offset + chunkSize);
|
|
32
|
+
const hash = await crypto.subtle.digest('SHA-256', slice);
|
|
33
|
+
const hashArray = new Uint8Array(hash);
|
|
34
|
+
chunks.push({
|
|
35
|
+
dataHash: hashArray,
|
|
36
|
+
minByteRange: cursor,
|
|
37
|
+
maxByteRange: cursor + chunkSize,
|
|
38
|
+
});
|
|
39
|
+
cursor += chunkSize;
|
|
40
|
+
offset += chunkSize;
|
|
41
|
+
}
|
|
42
|
+
const leaves = await generateLeaves(chunks);
|
|
43
|
+
const result = await buildLayers(leaves);
|
|
44
|
+
return Buffer.from(result.id).toString('base64url');
|
|
45
|
+
}
|
|
46
|
+
export const convertReadableToDataRoot = async ({ iterable, }) => {
|
|
47
|
+
const chunks = [];
|
|
48
|
+
let leftover = new Uint8Array(0);
|
|
49
|
+
let cursor = 0;
|
|
50
|
+
for await (const data of iterable) {
|
|
51
|
+
const inputChunk = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
52
|
+
const combined = new Uint8Array(leftover.length + inputChunk.length);
|
|
53
|
+
combined.set(leftover, 0);
|
|
54
|
+
combined.set(inputChunk, leftover.length);
|
|
55
|
+
let startIndex = 0;
|
|
56
|
+
while (combined.length - startIndex >= MAX_CHUNK_SIZE) {
|
|
57
|
+
let chunkSize = MAX_CHUNK_SIZE;
|
|
58
|
+
const remainderAfterThis = combined.length - startIndex - MAX_CHUNK_SIZE;
|
|
59
|
+
if (remainderAfterThis > 0 && remainderAfterThis < MIN_CHUNK_SIZE) {
|
|
60
|
+
chunkSize = Math.ceil((combined.length - startIndex) / 2);
|
|
61
|
+
}
|
|
62
|
+
const chunkData = combined.slice(startIndex, startIndex + chunkSize);
|
|
63
|
+
const dataHash = await Arweave.crypto.hash(chunkData);
|
|
64
|
+
chunks.push({
|
|
65
|
+
dataHash,
|
|
66
|
+
minByteRange: cursor,
|
|
67
|
+
maxByteRange: cursor + chunkSize,
|
|
68
|
+
});
|
|
69
|
+
cursor += chunkSize;
|
|
70
|
+
startIndex += chunkSize;
|
|
71
|
+
}
|
|
72
|
+
leftover = combined.slice(startIndex);
|
|
73
|
+
}
|
|
74
|
+
if (leftover.length > 0) {
|
|
75
|
+
const dataHash = await Arweave.crypto.hash(leftover);
|
|
76
|
+
chunks.push({
|
|
77
|
+
dataHash,
|
|
78
|
+
minByteRange: cursor,
|
|
79
|
+
maxByteRange: cursor + leftover.length,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
const leaves = await generateLeaves(chunks);
|
|
83
|
+
const root = await buildLayers(leaves);
|
|
84
|
+
return toB64Url(Buffer.from(root.id));
|
|
85
|
+
};
|
|
86
|
+
export class DataRootVerificationStrategy {
|
|
87
|
+
trustedDataRootProvider;
|
|
88
|
+
constructor({ trustedDataRootProvider, }) {
|
|
89
|
+
this.trustedDataRootProvider = trustedDataRootProvider;
|
|
90
|
+
}
|
|
91
|
+
async verifyData({ data, txId, }) {
|
|
92
|
+
const trustedDataRootPromise = this.trustedDataRootProvider.getDataRoot({
|
|
93
|
+
txId,
|
|
94
|
+
});
|
|
95
|
+
let computedDataRoot;
|
|
96
|
+
if (Buffer.isBuffer(data)) {
|
|
97
|
+
computedDataRoot = await convertBufferToDataRoot({ buffer: data });
|
|
98
|
+
}
|
|
99
|
+
else if (data instanceof Readable || data instanceof ReadableStream) {
|
|
100
|
+
computedDataRoot = await convertReadableToDataRoot({ iterable: data });
|
|
101
|
+
}
|
|
102
|
+
if (computedDataRoot === undefined) {
|
|
103
|
+
throw new Error('Data root could not be computed');
|
|
104
|
+
}
|
|
105
|
+
const trustedDataRoot = await trustedDataRootPromise;
|
|
106
|
+
if (computedDataRoot !== trustedDataRoot) {
|
|
107
|
+
throw new Error('Data root does not match', {
|
|
108
|
+
cause: { computedDataRoot, trustedDataRoot },
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// some data item options
|
|
114
|
+
// compute and verify data root, use offsets from server and verify the signature that the data item at the offset matches the signature
|
|
115
|
+
// does not give you assurance of valid bundle, but gives verification that the data item itself is valid
|
|
116
|
+
// reading from offsets is the only way for the client to compute and verify the signature
|
|
117
|
+
/**
|
|
118
|
+
* - when you get a signature of a data item, you can only verify the owner
|
|
119
|
+
* - you still need to verify it's going back to the bundle, unpack it, and verify the data item exists at the offset
|
|
120
|
+
* - 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
|
|
121
|
+
* - 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
|
|
122
|
+
*
|
|
123
|
+
* Composite verifier - you'll want to be very efficient with streams
|
|
124
|
+
* - hash verifier
|
|
125
|
+
* - parent chunks verifier --> for any range of data within a single transaction, tell me that it's correct
|
|
126
|
+
* - signature verifier
|
|
127
|
+
* - offset verifier
|
|
128
|
+
* - data item verifier
|
|
129
|
+
*/
|
|
130
|
+
// introduce a composite verifier that determines where/how to lookup the hash
|
|
@@ -0,0 +1,46 @@
|
|
|
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 { Readable } from 'node:stream';
|
|
17
|
+
import { hashBufferToB64Url, hashReadableStreamToB64Url, hashReadableToB64Url, } from '../../../../utils/hash.js';
|
|
18
|
+
export class HashVerificationStrategy {
|
|
19
|
+
trustedHashProvider;
|
|
20
|
+
constructor({ trustedHashProvider, }) {
|
|
21
|
+
this.trustedHashProvider = trustedHashProvider;
|
|
22
|
+
}
|
|
23
|
+
async verifyData({ data, txId, }) {
|
|
24
|
+
const hashPromise = this.trustedHashProvider.getHash({ txId });
|
|
25
|
+
let computedHash;
|
|
26
|
+
if (Buffer.isBuffer(data)) {
|
|
27
|
+
computedHash = hashBufferToB64Url(data);
|
|
28
|
+
}
|
|
29
|
+
else if (data instanceof Readable) {
|
|
30
|
+
computedHash = await hashReadableToB64Url(data);
|
|
31
|
+
}
|
|
32
|
+
else if (data instanceof ReadableStream) {
|
|
33
|
+
computedHash = await hashReadableStreamToB64Url(data);
|
|
34
|
+
}
|
|
35
|
+
// await on the hash promise and compare to get a little concurrency when computing hashes over larger data
|
|
36
|
+
const { hash } = await hashPromise;
|
|
37
|
+
if (computedHash === undefined) {
|
|
38
|
+
throw new Error('Hash could not be computed');
|
|
39
|
+
}
|
|
40
|
+
if (computedHash !== hash) {
|
|
41
|
+
throw new Error('Hash does not match', {
|
|
42
|
+
cause: { computedHash, trustedHash: hash },
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
const arioGatewayHeaders = {
|
|
2
|
+
digest: 'x-ar-io-digest',
|
|
3
|
+
verified: 'x-ar-io-verified',
|
|
4
|
+
txId: 'x-arns-resolved-tx-id',
|
|
5
|
+
processId: 'x-arns-resolved-process-id',
|
|
6
|
+
};
|
|
7
|
+
export class TrustedGatewaysHashProvider {
|
|
8
|
+
gatewaysProvider;
|
|
9
|
+
constructor({ gatewaysProvider,
|
|
10
|
+
// TODO: add threshold for allowed hash difference (i.e. by count or ratio of total gateways checked)
|
|
11
|
+
}) {
|
|
12
|
+
this.gatewaysProvider = gatewaysProvider;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Gets the digest for a given txId from all trusted gateways and ensures they all match.
|
|
16
|
+
* @param txId - The txId to get the digest for.
|
|
17
|
+
* @returns The digest for the given txId.
|
|
18
|
+
*/
|
|
19
|
+
async getHash({ txId, }) {
|
|
20
|
+
// get the hash from every gateway, and ensure they all match
|
|
21
|
+
const hashSet = new Set();
|
|
22
|
+
const hashResults = [];
|
|
23
|
+
const gateways = await this.gatewaysProvider.getGateways();
|
|
24
|
+
const hashes = await Promise.all(gateways.map(async (gateway) => {
|
|
25
|
+
const response = await fetch(`${gateway.toString()}${txId}`, {
|
|
26
|
+
method: 'HEAD',
|
|
27
|
+
redirect: 'follow',
|
|
28
|
+
});
|
|
29
|
+
if (!response.ok) {
|
|
30
|
+
// skip this gateway
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
const txIdHash = response.headers.get(arioGatewayHeaders.digest);
|
|
34
|
+
if (txIdHash === null || txIdHash === undefined) {
|
|
35
|
+
// skip this gateway
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
hashResults.push({
|
|
39
|
+
gateway: gateway.hostname,
|
|
40
|
+
txIdHash,
|
|
41
|
+
});
|
|
42
|
+
return txIdHash;
|
|
43
|
+
}));
|
|
44
|
+
for (const hash of hashes) {
|
|
45
|
+
if (hash !== undefined) {
|
|
46
|
+
hashSet.add(hash);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (hashSet.size === 0) {
|
|
50
|
+
throw new Error(`No trusted gateways returned a hash for txId ${txId}`);
|
|
51
|
+
}
|
|
52
|
+
if (hashSet.size > 1) {
|
|
53
|
+
throw new Error(`Failed to get consistent hash from all trusted gateways. ${JSON.stringify(hashResults)}`);
|
|
54
|
+
}
|
|
55
|
+
return { hash: hashResults[0].txIdHash, algorithm: 'sha256' };
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Get the data root for a given txId from all trusted gateways and ensure they all match.
|
|
59
|
+
* @param txId - The txId to get the data root for.
|
|
60
|
+
* @returns The data root for the given txId.
|
|
61
|
+
*/
|
|
62
|
+
async getDataRoot({ txId }) {
|
|
63
|
+
const dataRootSet = new Set();
|
|
64
|
+
const dataRootResults = [];
|
|
65
|
+
const gateways = await this.gatewaysProvider.getGateways();
|
|
66
|
+
const dataRoots = await Promise.all(gateways.map(async (gateway) => {
|
|
67
|
+
const response = await fetch(`${gateway.toString()}tx/${txId}/data_root`);
|
|
68
|
+
if (!response.ok) {
|
|
69
|
+
// skip this gateway
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
const dataRoot = await response.text();
|
|
73
|
+
dataRootResults.push({
|
|
74
|
+
gateway: gateway.hostname,
|
|
75
|
+
dataRoot,
|
|
76
|
+
});
|
|
77
|
+
return dataRoot;
|
|
78
|
+
}));
|
|
79
|
+
for (const dataRoot of dataRoots) {
|
|
80
|
+
if (dataRoot !== undefined) {
|
|
81
|
+
dataRootSet.add(dataRoot);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (dataRootSet.size > 1) {
|
|
85
|
+
throw new Error(`Failed to get consistent data root from all trusted gateways. ${JSON.stringify(dataRootResults)}`);
|
|
86
|
+
}
|
|
87
|
+
return dataRootSet.values().next().value;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// client could check hashes of data items, match expected hash
|
|
91
|
+
// if the gateway has the hash and they've verified it, you can trust the data item and offset
|
|
92
|
+
// you would be only trusting the gateway that it is a valid bundle
|
|
93
|
+
// you can request the offset from the gateway to verify the id
|
|
94
|
+
/**
|
|
95
|
+
* Note from @djwhitt
|
|
96
|
+
*
|
|
97
|
+
* Calculating data roots this way is fine, but it may not reproduce the original data root.
|
|
98
|
+
* We could also implement a data root verifier that pulls all the chunks, checks that they
|
|
99
|
+
* reproduce the expected data root, and then compares the concatenated chunk data to the
|
|
100
|
+
* original data retrieved. That would take a while, but it should be able to verify any L1
|
|
101
|
+
* data where we can find the chunks.
|
|
102
|
+
*/
|