@ar.io/sdk 3.10.2 → 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 +140 -132
- package/lib/cjs/cli/cli.js +137 -122
- package/lib/cjs/cli/commands/readCommands.js +6 -0
- package/lib/cjs/cli/options.js +12 -0
- package/lib/cjs/cli/utils.js +40 -11
- package/lib/cjs/common/ant-versions.js +5 -5
- package/lib/cjs/common/ant.js +5 -5
- package/lib/cjs/common/index.js +2 -0
- package/lib/cjs/common/io.js +106 -1
- package/lib/cjs/common/turbo.js +208 -0
- package/lib/cjs/common/wayfinder/gateways/trusted-gateways.js +106 -0
- package/lib/cjs/common/wayfinder/gateways.js +86 -0
- package/lib/cjs/common/wayfinder/index.js +44 -0
- package/lib/cjs/common/wayfinder/routers/priority.js +29 -0
- package/lib/cjs/common/wayfinder/routers/priority.test.js +155 -0
- package/lib/cjs/common/wayfinder/routers/random.js +23 -0
- package/lib/cjs/common/wayfinder/routers/random.test.js +25 -0
- package/lib/cjs/common/wayfinder/routers/simple-cache.js +25 -0
- package/lib/cjs/common/wayfinder/routers/simple-cache.test.js +41 -0
- package/lib/cjs/common/wayfinder/routers/static.js +14 -0
- package/lib/cjs/common/wayfinder/routers/static.test.js +14 -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 +596 -0
- package/lib/cjs/common/wayfinder/wayfinder.test.js +500 -0
- package/lib/cjs/types/ant.js +12 -1
- package/lib/cjs/types/index.js +1 -0
- package/lib/cjs/types/io.js +1 -1
- package/lib/cjs/types/wayfinder.js +3 -0
- package/lib/cjs/utils/ao.js +42 -0
- package/lib/cjs/utils/hash.js +56 -0
- package/lib/cjs/utils/random.js +30 -0
- package/lib/cjs/utils/url.js +28 -0
- package/lib/cjs/utils/url.test.js +24 -0
- package/lib/cjs/utils/utils.test.js +8 -8
- package/lib/cjs/version.js +1 -1
- package/lib/cjs/web/index.js +3 -1
- package/lib/esm/cli/cli.js +138 -123
- package/lib/esm/cli/commands/readCommands.js +5 -0
- package/lib/esm/cli/options.js +12 -0
- package/lib/esm/cli/utils.js +39 -11
- package/lib/esm/common/ant-versions.js +5 -5
- package/lib/esm/common/ant.js +5 -5
- package/lib/esm/common/index.js +2 -0
- package/lib/esm/common/io.js +106 -1
- package/lib/esm/common/turbo.js +200 -0
- package/lib/esm/common/wayfinder/gateways/trusted-gateways.js +102 -0
- package/lib/esm/common/wayfinder/gateways.js +80 -0
- package/lib/esm/common/wayfinder/index.js +28 -0
- package/lib/esm/common/wayfinder/routers/priority.js +25 -0
- package/lib/esm/common/wayfinder/routers/priority.test.js +153 -0
- package/lib/esm/common/wayfinder/routers/random.js +19 -0
- package/lib/esm/common/wayfinder/routers/random.test.js +23 -0
- package/lib/esm/common/wayfinder/routers/simple-cache.js +21 -0
- package/lib/esm/common/wayfinder/routers/simple-cache.test.js +39 -0
- package/lib/esm/common/wayfinder/routers/static.js +10 -0
- package/lib/esm/common/wayfinder/routers/static.test.js +12 -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 +584 -0
- package/lib/esm/common/wayfinder/wayfinder.test.js +495 -0
- package/lib/esm/types/ant.js +11 -0
- package/lib/esm/types/index.js +1 -0
- package/lib/esm/types/io.js +1 -1
- package/lib/esm/types/wayfinder.js +2 -0
- package/lib/esm/utils/ao.js +42 -0
- package/lib/esm/utils/hash.js +50 -0
- package/lib/esm/utils/random.js +26 -0
- package/lib/esm/utils/url.js +24 -0
- package/lib/esm/utils/url.test.js +19 -0
- package/lib/esm/utils/utils.test.js +1 -1
- package/lib/esm/version.js +1 -1
- package/lib/esm/web/index.js +1 -1
- package/lib/types/cli/commands/antCommands.d.ts +3 -3
- package/lib/types/cli/commands/arnsPurchaseCommands.d.ts +1 -1
- package/lib/types/cli/commands/gatewayWriteCommands.d.ts +9 -9
- package/lib/types/cli/commands/readCommands.d.ts +2 -0
- package/lib/types/cli/commands/transfer.d.ts +3 -3
- package/lib/types/cli/options.d.ts +9 -0
- package/lib/types/cli/types.d.ts +3 -0
- package/lib/types/cli/utils.d.ts +4 -0
- package/lib/types/common/ant-versions.d.ts +3 -6
- package/lib/types/common/index.d.ts +1 -0
- package/lib/types/common/io.d.ts +13 -8
- package/lib/types/common/turbo.d.ts +62 -0
- package/lib/types/common/wayfinder/gateways/trusted-gateways.d.ts +51 -0
- package/lib/types/common/wayfinder/gateways.d.ts +53 -0
- package/lib/types/common/wayfinder/index.d.ts +23 -0
- package/lib/types/common/wayfinder/routers/priority.d.ts +29 -0
- package/lib/types/common/wayfinder/routers/priority.test.d.ts +1 -0
- package/lib/types/common/wayfinder/routers/random.d.ts +27 -0
- package/lib/types/common/wayfinder/routers/random.test.d.ts +1 -0
- package/lib/types/common/wayfinder/routers/simple-cache.d.ts +28 -0
- package/lib/types/common/wayfinder/routers/simple-cache.test.d.ts +1 -0
- package/lib/types/common/wayfinder/routers/static.d.ts +24 -0
- package/lib/types/common/wayfinder/routers/static.test.d.ts +1 -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 +265 -0
- package/lib/types/common/wayfinder/wayfinder.test.d.ts +1 -0
- package/lib/types/types/ant.d.ts +53 -1
- package/lib/types/types/common.d.ts +7 -2
- package/lib/types/types/index.d.ts +1 -0
- package/lib/types/types/io.d.ts +23 -6
- package/lib/types/types/wayfinder.d.ts +62 -0
- package/lib/types/utils/ao.d.ts +1 -12
- package/lib/types/utils/hash.d.ts +4 -0
- package/lib/types/utils/random.d.ts +7 -0
- package/lib/types/utils/url.d.ts +19 -0
- package/lib/types/utils/url.test.d.ts +1 -0
- package/lib/types/version.d.ts +1 -1
- package/lib/types/web/index.d.ts +1 -1
- package/package.json +5 -3
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const node_assert_1 = require("node:assert");
|
|
4
|
+
const node_test_1 = require("node:test");
|
|
5
|
+
const priority_js_1 = require("./priority.js");
|
|
6
|
+
(0, node_test_1.describe)('PriorityRouter', () => {
|
|
7
|
+
const mockGateways = [
|
|
8
|
+
{
|
|
9
|
+
settings: {
|
|
10
|
+
fqdn: 'gateway1.net',
|
|
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: 'gateway1.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: 'addr1',
|
|
64
|
+
observerAddress: 'addr1',
|
|
65
|
+
totalDelegatedStake: 2000,
|
|
66
|
+
startTimestamp: 0,
|
|
67
|
+
endTimestamp: 0,
|
|
68
|
+
operatorStake: 2000,
|
|
69
|
+
status: 'joined',
|
|
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
|
+
},
|
|
90
|
+
{
|
|
91
|
+
settings: {
|
|
92
|
+
fqdn: 'gateway2.net',
|
|
93
|
+
port: 443,
|
|
94
|
+
protocol: 'https',
|
|
95
|
+
allowDelegatedStaking: false,
|
|
96
|
+
delegateRewardShareRatio: 0.5,
|
|
97
|
+
allowedDelegates: [],
|
|
98
|
+
minDelegatedStake: 0,
|
|
99
|
+
autoStake: false,
|
|
100
|
+
properties: '',
|
|
101
|
+
label: '',
|
|
102
|
+
note: '',
|
|
103
|
+
},
|
|
104
|
+
gatewayAddress: 'addr2',
|
|
105
|
+
observerAddress: 'addr2',
|
|
106
|
+
totalDelegatedStake: 0,
|
|
107
|
+
startTimestamp: 0,
|
|
108
|
+
endTimestamp: 0,
|
|
109
|
+
operatorStake: 0,
|
|
110
|
+
status: 'leaving',
|
|
111
|
+
weights: {
|
|
112
|
+
normalizedCompositeWeight: 0.5,
|
|
113
|
+
stakeWeight: 0.5,
|
|
114
|
+
tenureWeight: 0.5,
|
|
115
|
+
gatewayPerformanceRatio: 0.5,
|
|
116
|
+
observerPerformanceRatio: 0.5,
|
|
117
|
+
compositeWeight: 0.5,
|
|
118
|
+
gatewayRewardRatioWeight: 0.5,
|
|
119
|
+
observerRewardRatioWeight: 0.5,
|
|
120
|
+
},
|
|
121
|
+
stats: {
|
|
122
|
+
passedConsecutiveEpochs: 10,
|
|
123
|
+
failedConsecutiveEpochs: 5,
|
|
124
|
+
totalEpochCount: 15,
|
|
125
|
+
passedEpochCount: 10,
|
|
126
|
+
failedEpochCount: 5,
|
|
127
|
+
observedEpochCount: 15,
|
|
128
|
+
prescribedEpochCount: 20,
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
];
|
|
132
|
+
const mockArIOClient = {
|
|
133
|
+
getGateways: async () => ({ items: mockGateways }),
|
|
134
|
+
};
|
|
135
|
+
(0, node_test_1.it)('should prioritize gateway with highest success rate when using successRate weight', async () => {
|
|
136
|
+
const router = new priority_js_1.PriorityGatewayRouter({
|
|
137
|
+
ario: mockArIOClient,
|
|
138
|
+
sortBy: 'operatorStake',
|
|
139
|
+
sortOrder: 'desc',
|
|
140
|
+
limit: 1,
|
|
141
|
+
});
|
|
142
|
+
const result = await router.getTargetGateway();
|
|
143
|
+
node_assert_1.strict.deepStrictEqual(result, new URL('http://gateway1.net'));
|
|
144
|
+
});
|
|
145
|
+
(0, node_test_1.it)('should prioritize gateway with lowest latency when using latency weight', async () => {
|
|
146
|
+
const router = new priority_js_1.PriorityGatewayRouter({
|
|
147
|
+
ario: mockArIOClient,
|
|
148
|
+
sortBy: 'operatorStake',
|
|
149
|
+
sortOrder: 'desc',
|
|
150
|
+
limit: 1,
|
|
151
|
+
});
|
|
152
|
+
const result = await router.getTargetGateway();
|
|
153
|
+
node_assert_1.strict.deepStrictEqual(result, new URL('http://gateway2.net'));
|
|
154
|
+
});
|
|
155
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RandomGatewayRouter = void 0;
|
|
4
|
+
const random_js_1 = require("../../../utils/random.js");
|
|
5
|
+
class RandomGatewayRouter {
|
|
6
|
+
name = 'random';
|
|
7
|
+
gatewaysProvider;
|
|
8
|
+
blocklist;
|
|
9
|
+
constructor({ gatewaysProvider, blocklist = [], }) {
|
|
10
|
+
this.gatewaysProvider = gatewaysProvider;
|
|
11
|
+
this.blocklist = blocklist;
|
|
12
|
+
}
|
|
13
|
+
async getTargetGateway() {
|
|
14
|
+
const allGateways = await this.gatewaysProvider.getGateways();
|
|
15
|
+
const gateways = allGateways.filter((g) => !this.blocklist.includes(g.hostname));
|
|
16
|
+
const targetGateway = gateways[(0, random_js_1.randomInt)(0, gateways.length)];
|
|
17
|
+
if (targetGateway === undefined) {
|
|
18
|
+
throw new Error('No target gateway found');
|
|
19
|
+
}
|
|
20
|
+
return targetGateway;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.RandomGatewayRouter = RandomGatewayRouter;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const node_assert_1 = require("node:assert");
|
|
4
|
+
const node_test_1 = require("node:test");
|
|
5
|
+
const random_js_1 = require("./random.js");
|
|
6
|
+
(0, node_test_1.describe)('RandomRouter', () => {
|
|
7
|
+
const mockGateways = [
|
|
8
|
+
new URL('https://gateway1.net'),
|
|
9
|
+
new URL('https://gateway2.net'),
|
|
10
|
+
new URL('https://gateway3.net'),
|
|
11
|
+
];
|
|
12
|
+
const mockGatewaysProvider = {
|
|
13
|
+
getGateways: async () => mockGateways,
|
|
14
|
+
};
|
|
15
|
+
(0, node_test_1.it)('should only return joined gateways', async () => {
|
|
16
|
+
const router = new random_js_1.RandomGatewayRouter({
|
|
17
|
+
gatewaysProvider: mockGatewaysProvider,
|
|
18
|
+
});
|
|
19
|
+
// random gateway should be one of the mock gateways
|
|
20
|
+
for (let i = 0; i < 10; i++) {
|
|
21
|
+
const result = await router.getTargetGateway();
|
|
22
|
+
node_assert_1.strict.ok(mockGateways.includes(result));
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SimpleCacheRouter = void 0;
|
|
4
|
+
class SimpleCacheRouter {
|
|
5
|
+
name;
|
|
6
|
+
lastUpdatedTimestamp;
|
|
7
|
+
ttlSeconds;
|
|
8
|
+
targetGateway;
|
|
9
|
+
router;
|
|
10
|
+
constructor({ router, ttlSeconds = 5 * 60, // 5 minutes
|
|
11
|
+
}) {
|
|
12
|
+
this.router = router;
|
|
13
|
+
this.ttlSeconds = ttlSeconds;
|
|
14
|
+
}
|
|
15
|
+
async getTargetGateway() {
|
|
16
|
+
if (this.targetGateway === undefined ||
|
|
17
|
+
this.lastUpdatedTimestamp + this.ttlSeconds * 1000 < Date.now()) {
|
|
18
|
+
this.targetGateway = await this.router.getTargetGateway();
|
|
19
|
+
this.lastUpdatedTimestamp = Date.now();
|
|
20
|
+
}
|
|
21
|
+
return this.targetGateway;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.SimpleCacheRouter = SimpleCacheRouter;
|
|
25
|
+
// TODO: a router that accepts ario and a router, and adds read through promise cache to ario.getGateways to avoid calling the ARIO contract on every request
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const node_assert_1 = require("node:assert");
|
|
4
|
+
const node_test_1 = require("node:test");
|
|
5
|
+
const simple_cache_js_1 = require("./simple-cache.js");
|
|
6
|
+
(0, node_test_1.describe)('SimpleCacheRouter', () => {
|
|
7
|
+
(0, node_test_1.it)('should cache gateway for TTL period even if underlying gateway changes', async () => {
|
|
8
|
+
const gateway1 = new URL('https://gateway1.net');
|
|
9
|
+
const gateway2 = new URL('https://gateway2.net');
|
|
10
|
+
let currentGateway = gateway1;
|
|
11
|
+
const mockRouter = {
|
|
12
|
+
name: 'mock',
|
|
13
|
+
getTargetGateway: async () => currentGateway,
|
|
14
|
+
};
|
|
15
|
+
const router = new simple_cache_js_1.SimpleCacheRouter({
|
|
16
|
+
router: mockRouter,
|
|
17
|
+
ttlSeconds: 300, // 5 minutes
|
|
18
|
+
});
|
|
19
|
+
// Get initial gateway which should be cached
|
|
20
|
+
const initial = await router.getTargetGateway();
|
|
21
|
+
node_assert_1.strict.deepStrictEqual(initial, gateway1);
|
|
22
|
+
// Change the underlying gateway
|
|
23
|
+
currentGateway = gateway2;
|
|
24
|
+
// Should still return cached gateway1 for multiple calls
|
|
25
|
+
for (let i = 0; i < 5; i++) {
|
|
26
|
+
const result = await router.getTargetGateway();
|
|
27
|
+
node_assert_1.strict.deepStrictEqual(result, gateway1);
|
|
28
|
+
}
|
|
29
|
+
// Advance time past TTL
|
|
30
|
+
const originalNow = Date.now;
|
|
31
|
+
try {
|
|
32
|
+
Date.now = () => originalNow() + 300 * 1000 + 1;
|
|
33
|
+
// Should now return the new gateway2
|
|
34
|
+
const result = await router.getTargetGateway();
|
|
35
|
+
node_assert_1.strict.deepStrictEqual(result, gateway2);
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
Date.now = originalNow;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StaticGatewayRouter = void 0;
|
|
4
|
+
class StaticGatewayRouter {
|
|
5
|
+
name = 'static';
|
|
6
|
+
gateway;
|
|
7
|
+
constructor({ gateway }) {
|
|
8
|
+
this.gateway = new URL(gateway);
|
|
9
|
+
}
|
|
10
|
+
async getTargetGateway() {
|
|
11
|
+
return this.gateway;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.StaticGatewayRouter = StaticGatewayRouter;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const node_assert_1 = require("node:assert");
|
|
4
|
+
const node_test_1 = require("node:test");
|
|
5
|
+
const static_js_1 = require("./static.js");
|
|
6
|
+
(0, node_test_1.describe)('StaticGatewayRouter', () => {
|
|
7
|
+
(0, node_test_1.it)('should return the provided gateway', async () => {
|
|
8
|
+
const router = new static_js_1.StaticGatewayRouter({
|
|
9
|
+
gateway: 'http://test-gateway.net',
|
|
10
|
+
});
|
|
11
|
+
const result = await router.getTargetGateway();
|
|
12
|
+
node_assert_1.strict.deepStrictEqual(result, new URL('http://test-gateway.net'));
|
|
13
|
+
});
|
|
14
|
+
});
|
|
@@ -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;
|