@ar.io/sdk 3.11.0-alpha.1 → 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.
- 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/cli/utils.js +8 -1
- package/lib/cjs/common/ant.js +5 -5
- package/lib/cjs/common/io.js +49 -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 +9 -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 +459 -30
- package/lib/cjs/common/wayfinder/wayfinder.test.js +296 -49
- package/lib/cjs/types/wayfinder.js +1 -0
- package/lib/cjs/utils/ao.js +37 -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/cli/cli.js +138 -123
- package/lib/esm/cli/commands/readCommands.js +5 -0
- package/lib/esm/cli/utils.js +8 -1
- package/lib/esm/common/ant.js +5 -5
- package/lib/esm/common/io.js +49 -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 +9 -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 +453 -30
- package/lib/esm/common/wayfinder/wayfinder.test.js +297 -50
- package/lib/esm/types/wayfinder.js +1 -0
- package/lib/esm/utils/ao.js +37 -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/cli/commands/readCommands.d.ts +1 -0
- package/lib/types/common/io.d.ts +7 -8
- 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 +5 -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 +169 -17
- 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
- /package/lib/types/common/wayfinder/routers/{fixed.test.d.ts → static.test.d.ts} +0 -0
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SimpleCacheGatewaysProvider = exports.StaticGatewaysProvider = exports.
|
|
4
|
-
class
|
|
3
|
+
exports.SimpleCacheGatewaysProvider = exports.StaticGatewaysProvider = exports.NetworkGatewaysProvider = void 0;
|
|
4
|
+
class NetworkGatewaysProvider {
|
|
5
5
|
ario;
|
|
6
|
-
|
|
6
|
+
sortBy;
|
|
7
|
+
sortOrder;
|
|
8
|
+
limit;
|
|
9
|
+
filter;
|
|
10
|
+
constructor({ ario, sortBy = 'operatorStake', sortOrder = 'desc', limit = 1000, filter = (g) => g.status === 'joined', }) {
|
|
7
11
|
this.ario = ario;
|
|
12
|
+
this.sortBy = sortBy;
|
|
13
|
+
this.sortOrder = sortOrder;
|
|
14
|
+
this.limit = limit;
|
|
15
|
+
this.filter = filter;
|
|
8
16
|
}
|
|
9
17
|
async getGateways() {
|
|
10
18
|
let cursor;
|
|
@@ -12,9 +20,11 @@ class ARIOGatewaysProvider {
|
|
|
12
20
|
const gateways = [];
|
|
13
21
|
do {
|
|
14
22
|
try {
|
|
15
|
-
const { items: newGateways, nextCursor } = await this.ario.getGateways({
|
|
23
|
+
const { items: newGateways = [], nextCursor } = await this.ario.getGateways({
|
|
16
24
|
limit: 1000,
|
|
17
25
|
cursor,
|
|
26
|
+
sortBy: this.sortBy,
|
|
27
|
+
sortOrder: this.sortOrder,
|
|
18
28
|
});
|
|
19
29
|
gateways.push(...newGateways);
|
|
20
30
|
cursor = nextCursor;
|
|
@@ -30,14 +40,15 @@ class ARIOGatewaysProvider {
|
|
|
30
40
|
}
|
|
31
41
|
} while (cursor !== undefined && attempts < 3);
|
|
32
42
|
// filter out any gateways that are not joined
|
|
33
|
-
|
|
43
|
+
const filteredGateways = gateways.filter(this.filter).slice(0, this.limit);
|
|
44
|
+
return filteredGateways.map((g) => new URL(`${g.settings.protocol}://${g.settings.fqdn}:${g.settings.port}`));
|
|
34
45
|
}
|
|
35
46
|
}
|
|
36
|
-
exports.
|
|
47
|
+
exports.NetworkGatewaysProvider = NetworkGatewaysProvider;
|
|
37
48
|
class StaticGatewaysProvider {
|
|
38
49
|
gateways;
|
|
39
50
|
constructor({ gateways }) {
|
|
40
|
-
this.gateways = gateways;
|
|
51
|
+
this.gateways = gateways.map((g) => new URL(g));
|
|
41
52
|
}
|
|
42
53
|
async getGateways() {
|
|
43
54
|
return this.gateways;
|
|
@@ -62,7 +73,7 @@ class SimpleCacheGatewaysProvider {
|
|
|
62
73
|
try {
|
|
63
74
|
// preserve the cache if the fetch fails
|
|
64
75
|
const allGateways = await this.gatewaysProvider.getGateways();
|
|
65
|
-
this.gatewaysCache = allGateways
|
|
76
|
+
this.gatewaysCache = allGateways;
|
|
66
77
|
this.lastUpdated = now;
|
|
67
78
|
}
|
|
68
79
|
catch (error) {
|
|
@@ -33,4 +33,12 @@ __exportStar(require("./wayfinder.js"), exports);
|
|
|
33
33
|
// routers
|
|
34
34
|
__exportStar(require("./routers/random.js"), exports);
|
|
35
35
|
__exportStar(require("./routers/priority.js"), exports);
|
|
36
|
-
__exportStar(require("./routers/
|
|
36
|
+
__exportStar(require("./routers/static.js"), exports);
|
|
37
|
+
// gateways providers
|
|
38
|
+
__exportStar(require("./gateways.js"), exports);
|
|
39
|
+
// trusted gateways
|
|
40
|
+
__exportStar(require("./gateways/trusted-gateways.js"), exports);
|
|
41
|
+
// hash providers
|
|
42
|
+
__exportStar(require("./verification/data-root-verifier.js"), exports);
|
|
43
|
+
__exportStar(require("./verification/hash-verifier.js"), exports);
|
|
44
|
+
// TODO: signature verification
|
|
@@ -2,37 +2,28 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PriorityGatewayRouter = void 0;
|
|
4
4
|
const random_js_1 = require("../../../utils/random.js");
|
|
5
|
+
const gateways_js_1 = require("../gateways.js");
|
|
5
6
|
// TODO: one of N where N are in the last time window have met certain performance thresholds
|
|
6
7
|
// TODO: look at bitorrent routing protocols for inspiration
|
|
7
8
|
// TODO: router that looks at local stats/metrics and adjusts based on those
|
|
8
9
|
class PriorityGatewayRouter {
|
|
9
10
|
name = 'priority';
|
|
10
11
|
gatewaysProvider;
|
|
11
|
-
limit
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
this.sortBy = sortBy;
|
|
19
|
-
this.sortOrder = sortOrder;
|
|
20
|
-
this.blocklist = blocklist;
|
|
12
|
+
constructor({ ario, sortBy, sortOrder, limit, }) {
|
|
13
|
+
this.gatewaysProvider = new gateways_js_1.NetworkGatewaysProvider({
|
|
14
|
+
ario,
|
|
15
|
+
sortBy,
|
|
16
|
+
sortOrder,
|
|
17
|
+
limit,
|
|
18
|
+
});
|
|
21
19
|
}
|
|
22
20
|
async getTargetGateway() {
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
!this.blocklist.includes(gateway.settings.fqdn));
|
|
26
|
-
const sortedGateways = gateways
|
|
27
|
-
.sort(this.sortOrder === 'asc'
|
|
28
|
-
? (a, b) => a[this.sortBy] - b[this.sortBy]
|
|
29
|
-
: (a, b) => b[this.sortBy] - a[this.sortBy])
|
|
30
|
-
.slice(0, this.limit);
|
|
31
|
-
const targetGateway = sortedGateways[(0, random_js_1.randomInt)(0, sortedGateways.length)];
|
|
21
|
+
const gateways = await this.gatewaysProvider.getGateways();
|
|
22
|
+
const targetGateway = gateways[(0, random_js_1.randomInt)(0, gateways.length)];
|
|
32
23
|
if (targetGateway === undefined) {
|
|
33
24
|
throw new Error('No target gateway found');
|
|
34
25
|
}
|
|
35
|
-
return
|
|
26
|
+
return targetGateway;
|
|
36
27
|
}
|
|
37
28
|
}
|
|
38
29
|
exports.PriorityGatewayRouter = PriorityGatewayRouter;
|
|
@@ -129,13 +129,13 @@ const priority_js_1 = require("./priority.js");
|
|
|
129
129
|
},
|
|
130
130
|
},
|
|
131
131
|
];
|
|
132
|
-
const
|
|
133
|
-
getGateways: async () => mockGateways,
|
|
132
|
+
const mockArIOClient = {
|
|
133
|
+
getGateways: async () => ({ items: mockGateways }),
|
|
134
134
|
};
|
|
135
135
|
(0, node_test_1.it)('should prioritize gateway with highest success rate when using successRate weight', async () => {
|
|
136
136
|
const router = new priority_js_1.PriorityGatewayRouter({
|
|
137
|
-
|
|
138
|
-
sortBy: '
|
|
137
|
+
ario: mockArIOClient,
|
|
138
|
+
sortBy: 'operatorStake',
|
|
139
139
|
sortOrder: 'desc',
|
|
140
140
|
limit: 1,
|
|
141
141
|
});
|
|
@@ -144,7 +144,7 @@ const priority_js_1 = require("./priority.js");
|
|
|
144
144
|
});
|
|
145
145
|
(0, node_test_1.it)('should prioritize gateway with lowest latency when using latency weight', async () => {
|
|
146
146
|
const router = new priority_js_1.PriorityGatewayRouter({
|
|
147
|
-
|
|
147
|
+
ario: mockArIOClient,
|
|
148
148
|
sortBy: 'operatorStake',
|
|
149
149
|
sortOrder: 'desc',
|
|
150
150
|
limit: 1,
|
|
@@ -12,12 +12,12 @@ class RandomGatewayRouter {
|
|
|
12
12
|
}
|
|
13
13
|
async getTargetGateway() {
|
|
14
14
|
const allGateways = await this.gatewaysProvider.getGateways();
|
|
15
|
-
const gateways = allGateways.filter((g) =>
|
|
15
|
+
const gateways = allGateways.filter((g) => !this.blocklist.includes(g.hostname));
|
|
16
16
|
const targetGateway = gateways[(0, random_js_1.randomInt)(0, gateways.length)];
|
|
17
17
|
if (targetGateway === undefined) {
|
|
18
18
|
throw new Error('No target gateway found');
|
|
19
19
|
}
|
|
20
|
-
return
|
|
20
|
+
return targetGateway;
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
exports.RandomGatewayRouter = RandomGatewayRouter;
|
|
@@ -5,88 +5,9 @@ const node_test_1 = require("node:test");
|
|
|
5
5
|
const random_js_1 = require("./random.js");
|
|
6
6
|
(0, node_test_1.describe)('RandomRouter', () => {
|
|
7
7
|
const mockGateways = [
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
port: 443,
|
|
12
|
-
protocol: 'https',
|
|
13
|
-
allowDelegatedStaking: false,
|
|
14
|
-
delegateRewardShareRatio: 0.5,
|
|
15
|
-
allowedDelegates: [],
|
|
16
|
-
minDelegatedStake: 0,
|
|
17
|
-
autoStake: false,
|
|
18
|
-
properties: '',
|
|
19
|
-
label: '',
|
|
20
|
-
note: '',
|
|
21
|
-
},
|
|
22
|
-
gatewayAddress: 'addr1',
|
|
23
|
-
observerAddress: 'addr1',
|
|
24
|
-
totalDelegatedStake: 1000,
|
|
25
|
-
startTimestamp: 0,
|
|
26
|
-
endTimestamp: 0,
|
|
27
|
-
operatorStake: 100,
|
|
28
|
-
status: 'joined',
|
|
29
|
-
weights: {
|
|
30
|
-
normalizedCompositeWeight: 0.5,
|
|
31
|
-
stakeWeight: 0.5,
|
|
32
|
-
tenureWeight: 0.5,
|
|
33
|
-
gatewayPerformanceRatio: 0.5,
|
|
34
|
-
observerPerformanceRatio: 0.5,
|
|
35
|
-
compositeWeight: 0.5,
|
|
36
|
-
gatewayRewardRatioWeight: 0.5,
|
|
37
|
-
observerRewardRatioWeight: 0.5,
|
|
38
|
-
},
|
|
39
|
-
stats: {
|
|
40
|
-
passedConsecutiveEpochs: 10,
|
|
41
|
-
failedConsecutiveEpochs: 5,
|
|
42
|
-
totalEpochCount: 15,
|
|
43
|
-
passedEpochCount: 10,
|
|
44
|
-
failedEpochCount: 5,
|
|
45
|
-
observedEpochCount: 15,
|
|
46
|
-
prescribedEpochCount: 20,
|
|
47
|
-
},
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
settings: {
|
|
51
|
-
fqdn: 'gateway2.net',
|
|
52
|
-
port: 443,
|
|
53
|
-
protocol: 'https',
|
|
54
|
-
allowDelegatedStaking: false,
|
|
55
|
-
delegateRewardShareRatio: 0.5,
|
|
56
|
-
allowedDelegates: [],
|
|
57
|
-
minDelegatedStake: 0,
|
|
58
|
-
autoStake: false,
|
|
59
|
-
properties: '',
|
|
60
|
-
label: '',
|
|
61
|
-
note: '',
|
|
62
|
-
},
|
|
63
|
-
gatewayAddress: 'addr2',
|
|
64
|
-
observerAddress: 'addr2',
|
|
65
|
-
totalDelegatedStake: 0,
|
|
66
|
-
startTimestamp: 0,
|
|
67
|
-
endTimestamp: 0,
|
|
68
|
-
operatorStake: 0,
|
|
69
|
-
status: 'leaving',
|
|
70
|
-
weights: {
|
|
71
|
-
normalizedCompositeWeight: 0.5,
|
|
72
|
-
stakeWeight: 0.5,
|
|
73
|
-
tenureWeight: 0.5,
|
|
74
|
-
gatewayPerformanceRatio: 0.5,
|
|
75
|
-
observerPerformanceRatio: 0.5,
|
|
76
|
-
compositeWeight: 0.5,
|
|
77
|
-
gatewayRewardRatioWeight: 0.5,
|
|
78
|
-
observerRewardRatioWeight: 0.5,
|
|
79
|
-
},
|
|
80
|
-
stats: {
|
|
81
|
-
passedConsecutiveEpochs: 10,
|
|
82
|
-
failedConsecutiveEpochs: 5,
|
|
83
|
-
totalEpochCount: 15,
|
|
84
|
-
passedEpochCount: 10,
|
|
85
|
-
failedEpochCount: 5,
|
|
86
|
-
observedEpochCount: 15,
|
|
87
|
-
prescribedEpochCount: 20,
|
|
88
|
-
},
|
|
89
|
-
},
|
|
8
|
+
new URL('https://gateway1.net'),
|
|
9
|
+
new URL('https://gateway2.net'),
|
|
10
|
+
new URL('https://gateway3.net'),
|
|
90
11
|
];
|
|
91
12
|
const mockGatewaysProvider = {
|
|
92
13
|
getGateways: async () => mockGateways,
|
|
@@ -95,10 +16,10 @@ const random_js_1 = require("./random.js");
|
|
|
95
16
|
const router = new random_js_1.RandomGatewayRouter({
|
|
96
17
|
gatewaysProvider: mockGatewaysProvider,
|
|
97
18
|
});
|
|
98
|
-
//
|
|
19
|
+
// random gateway should be one of the mock gateways
|
|
99
20
|
for (let i = 0; i < 10; i++) {
|
|
100
21
|
const result = await router.getTargetGateway();
|
|
101
|
-
node_assert_1.strict.
|
|
22
|
+
node_assert_1.strict.ok(mockGateways.includes(result));
|
|
102
23
|
}
|
|
103
24
|
});
|
|
104
25
|
});
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
class
|
|
5
|
-
name = '
|
|
3
|
+
exports.StaticGatewayRouter = void 0;
|
|
4
|
+
class StaticGatewayRouter {
|
|
5
|
+
name = 'static';
|
|
6
6
|
gateway;
|
|
7
7
|
constructor({ gateway }) {
|
|
8
|
-
this.gateway = gateway;
|
|
8
|
+
this.gateway = new URL(gateway);
|
|
9
9
|
}
|
|
10
10
|
async getTargetGateway() {
|
|
11
11
|
return this.gateway;
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
-
exports.
|
|
14
|
+
exports.StaticGatewayRouter = StaticGatewayRouter;
|
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const node_assert_1 = require("node:assert");
|
|
4
4
|
const node_test_1 = require("node:test");
|
|
5
|
-
const
|
|
6
|
-
(0, node_test_1.describe)('
|
|
5
|
+
const static_js_1 = require("./static.js");
|
|
6
|
+
(0, node_test_1.describe)('StaticGatewayRouter', () => {
|
|
7
7
|
(0, node_test_1.it)('should return the provided gateway', async () => {
|
|
8
|
-
const router = new
|
|
9
|
-
gateway:
|
|
8
|
+
const router = new static_js_1.StaticGatewayRouter({
|
|
9
|
+
gateway: 'http://test-gateway.net',
|
|
10
10
|
});
|
|
11
11
|
const result = await router.getTargetGateway();
|
|
12
12
|
node_assert_1.strict.deepStrictEqual(result, new URL('http://test-gateway.net'));
|
|
@@ -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;
|