@lidofinance/lsv-cli 1.8.0 → 1.10.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.
- package/README.md +9 -1
- package/dist/abi/defi-wrapper/MellowStrategy.d.ts +429 -0
- package/dist/abi/defi-wrapper/MellowStrategy.js +892 -0
- package/dist/abi/defi-wrapper/MellowStrategy.js.map +1 -0
- package/dist/abi/defi-wrapper/index.d.ts +1 -0
- package/dist/abi/defi-wrapper/index.js +1 -0
- package/dist/abi/defi-wrapper/index.js.map +1 -1
- package/dist/configs/constants.d.ts +7 -1
- package/dist/features/consolidation.d.ts +2 -3
- package/dist/features/consolidation.js +60 -32
- package/dist/features/consolidation.js.map +1 -1
- package/dist/features/defi-wrapper/timelock.d.ts +8928 -2
- package/dist/features/defi-wrapper/timelock.js +82 -8
- package/dist/features/defi-wrapper/timelock.js.map +1 -1
- package/dist/programs/defi-wrapper/use-cases/timelock-governance/common/read.js +22 -4
- package/dist/programs/defi-wrapper/use-cases/timelock-governance/common/read.js.map +1 -1
- package/dist/programs/defi-wrapper/use-cases/timelock-governance/common/write.js +194 -16
- package/dist/programs/defi-wrapper/use-cases/timelock-governance/common/write.js.map +1 -1
- package/dist/programs/use-cases/consolidation/write.js +4 -4
- package/dist/programs/use-cases/consolidation/write.js.map +1 -1
- package/dist/programs/use-cases/metrics/read.js +4 -4
- package/dist/programs/use-cases/metrics/read.js.map +1 -1
- package/dist/programs/use-cases/report/read.js +4 -4
- package/dist/programs/use-cases/report/read.js.map +1 -1
- package/dist/providers/wallet.d.ts +2905 -8441
- package/dist/tests/fixtures/consolidation-fixtures.js +1 -0
- package/dist/tests/fixtures/consolidation-fixtures.js.map +1 -1
- package/dist/tests/integration/helpers/test-client.d.ts +2071 -5761
- package/dist/tests/utils/consolidation-fee-exemption-calc.test.d.ts +1 -0
- package/dist/tests/utils/consolidation-fee-exemption-calc.test.js +145 -0
- package/dist/tests/utils/consolidation-fee-exemption-calc.test.js.map +1 -0
- package/dist/tests/utils/consolidation-fee-exemption-confirm.test.d.ts +1 -0
- package/dist/tests/utils/consolidation-fee-exemption-confirm.test.js +118 -0
- package/dist/tests/utils/consolidation-fee-exemption-confirm.test.js.map +1 -0
- package/dist/tests/utils/consolidation-fee-exemption.test.d.ts +1 -0
- package/dist/tests/utils/consolidation-fee-exemption.test.js +168 -0
- package/dist/tests/utils/consolidation-fee-exemption.test.js.map +1 -0
- package/dist/tests/utils/first-validator-gindex.test.d.ts +1 -0
- package/dist/tests/utils/first-validator-gindex.test.js +48 -0
- package/dist/tests/utils/first-validator-gindex.test.js.map +1 -0
- package/dist/tests/utils/ipfs-security.test.js +130 -3
- package/dist/tests/utils/ipfs-security.test.js.map +1 -1
- package/dist/tests/utils/ipfs.test.js +40 -12
- package/dist/tests/utils/ipfs.test.js.map +1 -1
- package/dist/tests/utils/parse-abi-arguments.test.d.ts +1 -0
- package/dist/tests/utils/parse-abi-arguments.test.js +399 -0
- package/dist/tests/utils/parse-abi-arguments.test.js.map +1 -0
- package/dist/tests/utils/stream-helpers.d.ts +1 -0
- package/dist/tests/utils/stream-helpers.js +11 -0
- package/dist/tests/utils/stream-helpers.js.map +1 -0
- package/dist/utils/consolidation/confirms.js +19 -8
- package/dist/utils/consolidation/confirms.js.map +1 -1
- package/dist/utils/consolidation/fee-exemption.d.ts +19 -0
- package/dist/utils/consolidation/fee-exemption.js +70 -0
- package/dist/utils/consolidation/fee-exemption.js.map +1 -0
- package/dist/utils/consolidation/index.d.ts +1 -0
- package/dist/utils/consolidation/index.js +1 -0
- package/dist/utils/consolidation/index.js.map +1 -1
- package/dist/utils/consolidation/pubkeys.js +2 -0
- package/dist/utils/consolidation/pubkeys.js.map +1 -1
- package/dist/utils/consolidation/types.d.ts +1 -0
- package/dist/utils/consolidation/validator-info.d.ts +2 -1
- package/dist/utils/consolidation/validator-info.js +6 -0
- package/dist/utils/consolidation/validator-info.js.map +1 -1
- package/dist/utils/env.d.ts +1 -0
- package/dist/utils/env.js +8 -0
- package/dist/utils/env.js.map +1 -0
- package/dist/utils/ipfs.d.ts +10 -3
- package/dist/utils/ipfs.js +109 -27
- package/dist/utils/ipfs.js.map +1 -1
- package/dist/utils/parse-abi-arguments.d.ts +24 -0
- package/dist/utils/parse-abi-arguments.js +159 -0
- package/dist/utils/parse-abi-arguments.js.map +1 -0
- package/dist/utils/proof/constants.d.ts +1 -0
- package/dist/utils/proof/constants.js +1 -0
- package/dist/utils/proof/constants.js.map +1 -1
- package/dist/utils/proof/first-validator-gindex.js +27 -13
- package/dist/utils/proof/first-validator-gindex.js.map +1 -1
- package/dist/utils/proof/proofs.d.ts +1 -1
- package/dist/utils/proof/proofs.js +1 -1
- package/dist/utils/proof/proofs.js.map +1 -1
- package/dist/utils/rate-limit.d.ts +9 -0
- package/dist/utils/rate-limit.js +1 -6
- package/dist/utils/rate-limit.js.map +1 -1
- package/dist/version/index.js +1 -1
- package/package.json +20 -16
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pubkeys.js","sourceRoot":"","sources":["../../../utils/consolidation/pubkeys.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAO,UAAU,EAAE,MAAM,MAAM,CAAC;AAInD,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAClC,yBAAoD,EACnC,EAAE;IACnB,MAAM,aAAa,GAAG,CAAC,GAAG,yBAAyB,CAAC,IAAI,EAAE,CAAC,CAAC;IAC5D,OAAO,aAAa,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE;QAClC,MAAM,SAAS,GAAG,yBAAyB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACxD,IAAI,CAAC,SAAS,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CAAC,oBAAoB,MAAM,mBAAmB,CAAC,CAAC;QACjE,CAAC;QAED,MAAM,MAAM,GAAG,CAAC,GAAG,SAAS,CAAC,gBAAgB,CAAC,IAAI,EAAE,CAAC;aAClD,GAAG,CA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gBACpG;oBACE,MAAM,EAAE,gBAAgB;oBACxB,OAAO,EAAE,YAAY;oBACrB,KAAK,EAAE,GAAG;oBACV,gBAAgB,EAAE,YAAY;oBAC9B,OAAO,EAAE,KAAK;iBACf;aACF;SACF,CAAC;KACH,CACF,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Hex } from 'viem';
|
|
2
2
|
import { ValidatorsInfo } from '../fetch-cl.js';
|
|
3
|
-
import { TargetAndSourceValidators } from './types.js';
|
|
3
|
+
import { TargetAndSourceValidators, ValidatorInfo } from './types.js';
|
|
4
|
+
export declare const consolidatedBalance: (info: ValidatorInfo) => bigint;
|
|
4
5
|
export declare const getTargetAndSourceValidatorsInfo: (targetPubkeys: Hex[], targetValidatorsInfo: ValidatorsInfo, sourcePubkeys: Hex[][], sourceValidatorsInfo: ValidatorsInfo) => TargetAndSourceValidators;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { parseGwei } from 'viem';
|
|
2
|
+
import { bigIntMin } from '../big-int.js';
|
|
3
|
+
// what process_pending_consolidations moves to the target:
|
|
4
|
+
// min(balance, effective_balance), and nothing at all for a slashed source
|
|
5
|
+
export const consolidatedBalance = (info) => info.slashed ? 0n : bigIntMin(info.balance, info.effectiveBalance);
|
|
2
6
|
export const getTargetAndSourceValidatorsInfo = (targetPubkeys, targetValidatorsInfo, sourcePubkeys, sourceValidatorsInfo) => {
|
|
3
7
|
const targetAndSourceValidatorsInfo = new Map();
|
|
4
8
|
for (const [i, targetPubkey] of targetPubkeys.entries()) {
|
|
@@ -12,6 +16,7 @@ export const getTargetAndSourceValidatorsInfo = (targetPubkeys, targetValidators
|
|
|
12
16
|
balance: parseGwei(targetValidatorInfo.balance),
|
|
13
17
|
index: targetValidatorInfo.index,
|
|
14
18
|
effectiveBalance: parseGwei(targetValidatorInfo.validator.effective_balance),
|
|
19
|
+
slashed: targetValidatorInfo.validator.slashed,
|
|
15
20
|
},
|
|
16
21
|
sourceValidators: new Map(),
|
|
17
22
|
});
|
|
@@ -28,6 +33,7 @@ export const getTargetAndSourceValidatorsInfo = (targetPubkeys, targetValidators
|
|
|
28
33
|
balance: parseGwei(sourceValidatorInfo.balance),
|
|
29
34
|
index: sourceValidatorInfo.index,
|
|
30
35
|
effectiveBalance: parseGwei(sourceValidatorInfo.validator.effective_balance),
|
|
36
|
+
slashed: sourceValidatorInfo.validator.slashed,
|
|
31
37
|
});
|
|
32
38
|
}
|
|
33
39
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator-info.js","sourceRoot":"","sources":["../../../utils/consolidation/validator-info.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,SAAS,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"validator-info.js","sourceRoot":"","sources":["../../../utils/consolidation/validator-info.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,SAAS,EAAE,MAAM,MAAM,CAAC;AAGtC,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAI1C,2DAA2D;AAC3D,2EAA2E;AAC3E,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,IAAmB,EAAU,EAAE,CACjE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,gBAAgB,CAAC,CAAC;AAErE,MAAM,CAAC,MAAM,gCAAgC,GAAG,CAC9C,aAAoB,EACpB,oBAAoC,EACpC,aAAsB,EACtB,oBAAoC,EACT,EAAE;IAC7B,MAAM,6BAA6B,GAA8B,IAAI,GAAG,EAAE,CAAC;IAC3E,KAAK,MAAM,CAAC,CAAC,EAAE,YAAY,CAAC,IAAI,aAAa,CAAC,OAAO,EAAE,EAAE,CAAC;QACxD,MAAM,mBAAmB,GAAG,oBAAoB,CAAC,IAAI,CAAC,IAAI,CACxD,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS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BAAiB,CAChD;gBACD,OAAO,EAAE,mBAAmB,CAAC,SAAS,CAAC,OAAO;aAC/C,CAAC,CAAC;QACP,CAAC;IACH,CAAC;IACD,OAAO,6BAA6B,CAAC;AACvC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const parseEnvInt: (raw: string | undefined, fallback: number) => number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.js","sourceRoot":"","sources":["../../utils/env.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,GAAuB,EACvB,QAAgB,EACR,EAAE;IACV,IAAI,CAAC,GAAG;QAAE,OAAO,QAAQ,CAAC;IAC1B,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;IACtB,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;AAC3C,CAAC,CAAC"}
|
package/dist/utils/ipfs.d.ts
CHANGED
|
@@ -1,20 +1,27 @@
|
|
|
1
1
|
import { CID, Version } from 'multiformats/cid';
|
|
2
|
-
export declare const
|
|
2
|
+
export declare const IPFS_GATEWAYS: {
|
|
3
|
+
readonly ipfsIo: "https://ipfs.io/ipfs";
|
|
4
|
+
readonly filebase: "https://ipfs.filebase.io/ipfs";
|
|
5
|
+
readonly pinata: "https://gateway.pinata.cloud/ipfs";
|
|
6
|
+
};
|
|
7
|
+
export declare const IPFS_GATEWAY: string;
|
|
8
|
+
export declare const DEFAULT_IPFS_MAX_CONTENT_BYTES: number;
|
|
3
9
|
export type BigNumberType = 'bigint' | 'string';
|
|
4
10
|
export type ReportFetchArgs = {
|
|
5
11
|
cid: string;
|
|
6
12
|
gateway?: string;
|
|
7
13
|
bigNumberType?: BigNumberType;
|
|
14
|
+
maxBytes?: number;
|
|
8
15
|
};
|
|
9
16
|
export declare const fetchIPFS: <T>(args: ReportFetchArgs, cache?: boolean) => Promise<T>;
|
|
10
17
|
export declare const fetchIPFSDirect: <T>(args: ReportFetchArgs) => Promise<T>;
|
|
11
18
|
export declare const fetchIPFSBuffer: (args: ReportFetchArgs) => Promise<Uint8Array>;
|
|
12
19
|
export declare const calculateIPFSAddCID: (fileContent: Uint8Array, version?: Version) => Promise<CID>;
|
|
13
|
-
export declare const fetchIPFSDirectAndVerify: <T>(cid: string, gateway?: string) => Promise<{
|
|
20
|
+
export declare const fetchIPFSDirectAndVerify: <T>(cid: string, gateway?: string, maxBytes?: number) => Promise<{
|
|
14
21
|
json: T;
|
|
15
22
|
fileContent: Uint8Array;
|
|
16
23
|
}>;
|
|
17
|
-
export declare const fetchIPFSWithCacheAndVerify: <T>(cid: string, gateway?: string) => Promise<T>;
|
|
24
|
+
export declare const fetchIPFSWithCacheAndVerify: <T>(cid: string, gateway?: string, maxBytes?: number) => Promise<T>;
|
|
18
25
|
type PinToIPFSArgs = {
|
|
19
26
|
uploadUrl: string;
|
|
20
27
|
uploadAuthorization?: string;
|
package/dist/utils/ipfs.js
CHANGED
|
@@ -6,26 +6,114 @@ import fs from 'node:fs/promises';
|
|
|
6
6
|
import path from 'node:path';
|
|
7
7
|
import { logInfo, logTable } from './logging/console.js';
|
|
8
8
|
import { assertSafeUrl } from './data-validators.js';
|
|
9
|
-
|
|
9
|
+
import { parseEnvInt } from './env.js';
|
|
10
|
+
export const IPFS_GATEWAYS = {
|
|
11
|
+
ipfsIo: 'https://ipfs.io/ipfs',
|
|
12
|
+
filebase: 'https://ipfs.filebase.io/ipfs',
|
|
13
|
+
pinata: 'https://gateway.pinata.cloud/ipfs',
|
|
14
|
+
};
|
|
15
|
+
export const IPFS_GATEWAY = IPFS_GATEWAYS.ipfsIo;
|
|
16
|
+
// Max IPFS content size — guards against OOM from an oversized CID.
|
|
17
|
+
// Override: maxBytes arg > IPFS_MAX_CONTENT_BYTES env > this default.
|
|
18
|
+
export const DEFAULT_IPFS_MAX_CONTENT_BYTES = 20 * 1024 * 1024;
|
|
10
19
|
const IPFS_CACHE_DIR = path.resolve('ipfs-cache');
|
|
20
|
+
// arg > env > default
|
|
21
|
+
const resolveMaxBytes = (override) => {
|
|
22
|
+
if (typeof override === 'number' &&
|
|
23
|
+
Number.isFinite(override) &&
|
|
24
|
+
override > 0) {
|
|
25
|
+
return override;
|
|
26
|
+
}
|
|
27
|
+
return Math.max(1, parseEnvInt(process.env.IPFS_MAX_CONTENT_BYTES, DEFAULT_IPFS_MAX_CONTENT_BYTES));
|
|
28
|
+
};
|
|
29
|
+
// Cheap pre-check. Header may be absent or lie — streaming is the real limit.
|
|
30
|
+
const assertContentLengthWithinLimit = (response, maxBytes) => {
|
|
31
|
+
const declared = Number(response.headers?.get('content-length'));
|
|
32
|
+
if (Number.isFinite(declared) && declared > maxBytes) {
|
|
33
|
+
throw new Error(`IPFS content too large: ${declared} bytes exceeds limit of ${maxBytes} bytes`);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
// Join collected byte chunks into a single buffer.
|
|
37
|
+
const concatChunks = (chunks, total) => {
|
|
38
|
+
const out = new Uint8Array(total);
|
|
39
|
+
let offset = 0;
|
|
40
|
+
for (const chunk of chunks) {
|
|
41
|
+
out.set(chunk, offset);
|
|
42
|
+
offset += chunk.byteLength;
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
};
|
|
46
|
+
// Read in chunks, abort once total exceeds maxBytes (before full buffering).
|
|
47
|
+
const streamBodyWithLimit = async (body, maxBytes) => {
|
|
48
|
+
const reader = body.getReader();
|
|
49
|
+
const chunks = [];
|
|
50
|
+
let total = 0;
|
|
51
|
+
try {
|
|
52
|
+
while (true) {
|
|
53
|
+
const { done, value } = await reader.read();
|
|
54
|
+
if (done)
|
|
55
|
+
break;
|
|
56
|
+
if (!value)
|
|
57
|
+
continue;
|
|
58
|
+
total += value.byteLength;
|
|
59
|
+
if (total > maxBytes) {
|
|
60
|
+
await reader.cancel();
|
|
61
|
+
throw new Error(`IPFS content exceeds size limit of ${maxBytes} bytes`);
|
|
62
|
+
}
|
|
63
|
+
chunks.push(value);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
reader.releaseLock?.();
|
|
68
|
+
}
|
|
69
|
+
return concatChunks(chunks, total);
|
|
70
|
+
};
|
|
71
|
+
// Streaming is the real limit; Content-Length is just an early reject.
|
|
72
|
+
// Fail closed if there's no body — a real fetch always has one for content.
|
|
73
|
+
const readBodyWithLimit = async (response, maxBytes) => {
|
|
74
|
+
assertContentLengthWithinLimit(response, maxBytes);
|
|
75
|
+
if (!response.body) {
|
|
76
|
+
throw new Error('IPFS response has no readable body');
|
|
77
|
+
}
|
|
78
|
+
return streamBodyWithLimit(response.body, maxBytes);
|
|
79
|
+
};
|
|
80
|
+
const fetchFromIPFSGateways = async (cid, gateway) => {
|
|
81
|
+
const gateways = [
|
|
82
|
+
...new Set([gateway, ...Object.values(IPFS_GATEWAYS)].filter(Boolean)),
|
|
83
|
+
];
|
|
84
|
+
for (const currentGateway of gateways) {
|
|
85
|
+
assertSafeUrl(currentGateway, 'IPFS gateway');
|
|
86
|
+
}
|
|
87
|
+
const errors = [];
|
|
88
|
+
for (const currentGateway of gateways) {
|
|
89
|
+
const ipfsUrl = `${currentGateway}/${cid}`;
|
|
90
|
+
logInfo('Fetching content from', ipfsUrl);
|
|
91
|
+
try {
|
|
92
|
+
const response = await fetch(ipfsUrl);
|
|
93
|
+
if (response.ok)
|
|
94
|
+
return response;
|
|
95
|
+
errors.push(`${ipfsUrl}: ${response.status} ${response.statusText}`);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
99
|
+
errors.push(`${ipfsUrl}: ${message}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
throw new Error(`Failed to fetch IPFS content from all gateways: ${errors.join('; ')}`);
|
|
103
|
+
};
|
|
11
104
|
export const fetchIPFS = async (args, cache = true) => {
|
|
12
|
-
const { cid, gateway = IPFS_GATEWAY } = args;
|
|
105
|
+
const { cid, gateway = IPFS_GATEWAY, maxBytes } = args;
|
|
13
106
|
if (cache)
|
|
14
|
-
return fetchIPFSWithCacheAndVerify(cid, gateway);
|
|
15
|
-
const { json } = await fetchIPFSDirectAndVerify(cid, gateway);
|
|
107
|
+
return fetchIPFSWithCacheAndVerify(cid, gateway, maxBytes);
|
|
108
|
+
const { json } = await fetchIPFSDirectAndVerify(cid, gateway, maxBytes);
|
|
16
109
|
return json;
|
|
17
110
|
};
|
|
18
111
|
// Fetching content by CID through IPFS gateway
|
|
19
112
|
export const fetchIPFSDirect = async (args) => {
|
|
20
|
-
const { cid, gateway
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
const response = await fetch(ipfsUrl);
|
|
25
|
-
if (!response.ok) {
|
|
26
|
-
throw new Error(`Failed to fetch IPFS content: ${response.statusText}`);
|
|
27
|
-
}
|
|
28
|
-
const raw = await response.text();
|
|
113
|
+
const { cid, gateway, bigNumberType = 'string' } = args;
|
|
114
|
+
const maxBytes = resolveMaxBytes(args.maxBytes);
|
|
115
|
+
const response = await fetchFromIPFSGateways(cid, gateway);
|
|
116
|
+
const raw = new TextDecoder().decode(await readBodyWithLimit(response, maxBytes));
|
|
29
117
|
const params = bigNumberType === 'bigint'
|
|
30
118
|
? { useNativeBigInt: true }
|
|
31
119
|
: { storeAsString: true };
|
|
@@ -34,16 +122,10 @@ export const fetchIPFSDirect = async (args) => {
|
|
|
34
122
|
};
|
|
35
123
|
// Fetching buffer content by CID through IPFS gateway
|
|
36
124
|
export const fetchIPFSBuffer = async (args) => {
|
|
37
|
-
const { cid, gateway
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
const response = await fetch(ipfsUrl);
|
|
42
|
-
if (!response.ok) {
|
|
43
|
-
throw new Error(`Failed to fetch IPFS content: ${response.statusText}`);
|
|
44
|
-
}
|
|
45
|
-
const buffer = await response.arrayBuffer();
|
|
46
|
-
return new Uint8Array(buffer);
|
|
125
|
+
const { cid, gateway } = args;
|
|
126
|
+
const maxBytes = resolveMaxBytes(args.maxBytes);
|
|
127
|
+
const response = await fetchFromIPFSGateways(cid, gateway);
|
|
128
|
+
return readBodyWithLimit(response, maxBytes);
|
|
47
129
|
};
|
|
48
130
|
// Recalculate CID using full UnixFS logic (like `ipfs add`)
|
|
49
131
|
export const calculateIPFSAddCID = async (fileContent, version = 0) => {
|
|
@@ -63,7 +145,7 @@ export const calculateIPFSAddCID = async (fileContent, version = 0) => {
|
|
|
63
145
|
return lastCid;
|
|
64
146
|
};
|
|
65
147
|
// Downloading file from IPFS and checking its integrity
|
|
66
|
-
export const fetchIPFSDirectAndVerify = async (cid, gateway = IPFS_GATEWAY) => {
|
|
148
|
+
export const fetchIPFSDirectAndVerify = async (cid, gateway = IPFS_GATEWAY, maxBytes) => {
|
|
67
149
|
let originalCID;
|
|
68
150
|
try {
|
|
69
151
|
originalCID = CID.parse(cid);
|
|
@@ -71,7 +153,7 @@ export const fetchIPFSDirectAndVerify = async (cid, gateway = IPFS_GATEWAY) => {
|
|
|
71
153
|
catch {
|
|
72
154
|
throw new Error(`Invalid IPFS CID: ${cid}`);
|
|
73
155
|
}
|
|
74
|
-
const fileContent = await fetchIPFSBuffer({ cid, gateway });
|
|
156
|
+
const fileContent = await fetchIPFSBuffer({ cid, gateway, maxBytes });
|
|
75
157
|
const calculatedCID = await calculateIPFSAddCID(fileContent, originalCID.version);
|
|
76
158
|
if (!calculatedCID.equals(originalCID)) {
|
|
77
159
|
throw new Error(`❌ File hash mismatch! Expected ${originalCID}, but got ${calculatedCID}`);
|
|
@@ -92,7 +174,7 @@ export const fetchIPFSDirectAndVerify = async (cid, gateway = IPFS_GATEWAY) => {
|
|
|
92
174
|
json,
|
|
93
175
|
};
|
|
94
176
|
};
|
|
95
|
-
export const fetchIPFSWithCacheAndVerify = async (cid, gateway = IPFS_GATEWAY) => {
|
|
177
|
+
export const fetchIPFSWithCacheAndVerify = async (cid, gateway = IPFS_GATEWAY, maxBytes) => {
|
|
96
178
|
assertSafeUrl(gateway, 'IPFS gateway');
|
|
97
179
|
try {
|
|
98
180
|
CID.parse(cid);
|
|
@@ -109,7 +191,7 @@ export const fetchIPFSWithCacheAndVerify = async (cid, gateway = IPFS_GATEWAY) =
|
|
|
109
191
|
}
|
|
110
192
|
catch {
|
|
111
193
|
// Not in cache, fetch from IPFS
|
|
112
|
-
const { json } = await fetchIPFSDirectAndVerify(cid, gateway);
|
|
194
|
+
const { json } = await fetchIPFSDirectAndVerify(cid, gateway, maxBytes);
|
|
113
195
|
await fs.writeFile(cacheFile, JSON.stringify(json), 'utf8');
|
|
114
196
|
return json;
|
|
115
197
|
}
|
package/dist/utils/ipfs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipfs.js","sourceRoot":"","sources":["../../utils/ipfs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAW,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"ipfs.js","sourceRoot":"","sources":["../../utils/ipfs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAW,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,UAAU,MAAM,aA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IAAI,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC;IACrC,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE;QACtC,IAAI,EAAE,kBAAkB;KACzB,CAAC,CAAC;IACH,MAAM,QAAQ,GAAG,IAAI,QAAQ,EAAE,CAAC;IAChC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAE9B,MAAM,aAAa,GAAG,MAAM,KAAK,CAAC,SAAS,EAAE;QAC3C,MAAM,EAAE,MAAM;QACd,OAAO,EAAE;YACP,GAAG,CAAC,mBAAmB;gBACrB,CAAC,CAAC,EAAE,aAAa,EAAE,UAAU,mBAAmB,EAAE,EAAE;gBACpD,CAAC,CAAC,EAAE,CAAC;SACR;QAED,IAAI,EAAE,QAAQ;KACf,CAAC,CAAC;IAEH,IAAI,CAAC,aAAa,CAAC,EAAE,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CACb,4DAA4D,SAAS,aAAa,aAAa,CAAC,MAAM,IAAI,aAAa,CAAC,UAAU,EAAE,CACrI,CAAC;IACJ,CAAC;IACD,MAAM,YAAY,GAAG,MAAM,aAAa,CAAC,IAAI,EAAE,CAAC;IAChD,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { AbiFunction } from 'viem';
|
|
2
|
+
/**
|
|
3
|
+
* Parses an array of raw string arguments into typed values
|
|
4
|
+
* according to the given ABI function definition.
|
|
5
|
+
*
|
|
6
|
+
* @param methodAbi - The viem `AbiFunction` describing the target method.
|
|
7
|
+
* @param args - Raw string arguments in the same order as `methodAbi.inputs`.
|
|
8
|
+
* @returns Typed argument array ready to pass to viem's `encodeFunctionData`.
|
|
9
|
+
* @throws If the argument count doesn't match or any value fails to parse.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { parseAbi } from "viem";
|
|
14
|
+
* import { parseAbiArguments } from "./parseAbiArguments";
|
|
15
|
+
*
|
|
16
|
+
* const [method] = parseAbi(["function transfer(address to, uint256 amount)"]);
|
|
17
|
+
* const args = parseAbiArguments(method, [
|
|
18
|
+
* "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
|
|
19
|
+
* "1000000000000000000",
|
|
20
|
+
* ]);
|
|
21
|
+
* // => ["0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", 1000000000000000000n]
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare const parseAbiArguments: (methodAbi: AbiFunction, args: string[]) => unknown[];
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { isAddress, isHex, size as hexSize } from 'viem';
|
|
2
|
+
/**
|
|
3
|
+
* Parses a single string value into a typed value based on its Solidity ABI type.
|
|
4
|
+
* Uses viem's `isAddress` and `isHex` for validation.
|
|
5
|
+
* Throws a descriptive error if parsing fails.
|
|
6
|
+
*/
|
|
7
|
+
const toArgString = (v) => typeof v === 'string' ? v : JSON.stringify(v);
|
|
8
|
+
const parseAbiArgument = (param, value) => {
|
|
9
|
+
const { type } = param;
|
|
10
|
+
try {
|
|
11
|
+
// --- address ---
|
|
12
|
+
if (type === 'address') {
|
|
13
|
+
if (!isAddress(value)) {
|
|
14
|
+
throw new Error(`Invalid address: "${value}"`);
|
|
15
|
+
}
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
// --- bool ---
|
|
19
|
+
if (type === 'bool') {
|
|
20
|
+
if (value === 'true')
|
|
21
|
+
return true;
|
|
22
|
+
if (value === 'false')
|
|
23
|
+
return false;
|
|
24
|
+
throw new Error(`Invalid bool value: "${value}". Expected "true" or "false"`);
|
|
25
|
+
}
|
|
26
|
+
// --- uint / int ---
|
|
27
|
+
if (/^(uint|int)\d*$/.test(type)) {
|
|
28
|
+
const n = BigInt(value);
|
|
29
|
+
if (type.startsWith('uint') && n < 0n) {
|
|
30
|
+
throw new Error(`Unsigned integer cannot be negative: "${value}"`);
|
|
31
|
+
}
|
|
32
|
+
return n;
|
|
33
|
+
}
|
|
34
|
+
// --- bytes1 … bytes32 (fixed-size) ---
|
|
35
|
+
const fixedBytesMatch = type.match(/^bytes(\d+)$/);
|
|
36
|
+
if (fixedBytesMatch) {
|
|
37
|
+
const expectedSize = Number.parseInt(fixedBytesMatch[1], 10);
|
|
38
|
+
if (!isHex(value)) {
|
|
39
|
+
throw new Error(`Invalid hex for ${type}: "${value}"`);
|
|
40
|
+
}
|
|
41
|
+
const actualSize = hexSize(value);
|
|
42
|
+
if (actualSize !== expectedSize) {
|
|
43
|
+
throw new Error(`Expected ${expectedSize} bytes for ${type}, got ${actualSize}`);
|
|
44
|
+
}
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
// --- bytes (dynamic) ---
|
|
48
|
+
if (type === 'bytes') {
|
|
49
|
+
if (!isHex(value)) {
|
|
50
|
+
throw new Error(`Invalid hex for bytes: "${value}"`);
|
|
51
|
+
}
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
// --- string ---
|
|
55
|
+
if (type === 'string') {
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
// --- fixed-size array, e.g. uint256[3] ---
|
|
59
|
+
const fixedArrayMatch = type.match(/^(.+)\[(\d+)\]$/);
|
|
60
|
+
if (fixedArrayMatch) {
|
|
61
|
+
const innerType = fixedArrayMatch[1];
|
|
62
|
+
const expectedLength = Number.parseInt(fixedArrayMatch[2], 10);
|
|
63
|
+
const parsed = JSON.parse(value);
|
|
64
|
+
if (!Array.isArray(parsed)) {
|
|
65
|
+
throw new TypeError(`Expected a JSON array for ${type}, got: "${value}"`);
|
|
66
|
+
}
|
|
67
|
+
if (parsed.length !== expectedLength) {
|
|
68
|
+
throw new Error(`Expected array of length ${expectedLength} for ${type}, got length ${parsed.length}`);
|
|
69
|
+
}
|
|
70
|
+
return parsed.map((item, i) => parseAbiArgument({ ...param, type: innerType, name: `${param.name}[${i}]` }, toArgString(item)));
|
|
71
|
+
}
|
|
72
|
+
// --- dynamic array, e.g. uint256[] ---
|
|
73
|
+
const dynamicArrayMatch = type.match(/^(.+)\[\]$/);
|
|
74
|
+
if (dynamicArrayMatch) {
|
|
75
|
+
const innerType = dynamicArrayMatch[1];
|
|
76
|
+
const parsed = JSON.parse(value);
|
|
77
|
+
if (!Array.isArray(parsed)) {
|
|
78
|
+
throw new TypeError(`Expected a JSON array for ${type}, got: "${value}"`);
|
|
79
|
+
}
|
|
80
|
+
return parsed.map((item, i) => parseAbiArgument({ ...param, type: innerType, name: `${param.name}[${i}]` }, toArgString(item)));
|
|
81
|
+
}
|
|
82
|
+
// --- tuple (struct) ---
|
|
83
|
+
if (type === 'tuple') {
|
|
84
|
+
const tupleParam = param;
|
|
85
|
+
if (!tupleParam.components || tupleParam.components.length === 0) {
|
|
86
|
+
throw new Error(`Tuple parameter "${param.name}" has no components defined in ABI`);
|
|
87
|
+
}
|
|
88
|
+
let parsed;
|
|
89
|
+
try {
|
|
90
|
+
parsed = JSON.parse(value);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
throw new Error(`Invalid JSON for tuple "${param.name}": "${value}"`);
|
|
94
|
+
}
|
|
95
|
+
// Accept array representation
|
|
96
|
+
if (Array.isArray(parsed)) {
|
|
97
|
+
if (parsed.length !== tupleParam.components.length) {
|
|
98
|
+
throw new Error(`Tuple "${param.name}" expects ${tupleParam.components.length} fields, got ${parsed.length}`);
|
|
99
|
+
}
|
|
100
|
+
return tupleParam.components.reduce((acc, comp, i) => {
|
|
101
|
+
acc[comp.name ?? i] = parseAbiArgument(comp, toArgString(parsed[i]));
|
|
102
|
+
return acc;
|
|
103
|
+
}, {});
|
|
104
|
+
}
|
|
105
|
+
// Accept object representation
|
|
106
|
+
if (typeof parsed === 'object' && parsed !== null) {
|
|
107
|
+
return tupleParam.components.reduce((acc, comp) => {
|
|
108
|
+
const key = comp.name;
|
|
109
|
+
if (!key) {
|
|
110
|
+
throw new Error(`Tuple component has no name`);
|
|
111
|
+
}
|
|
112
|
+
if (!(key in parsed)) {
|
|
113
|
+
throw new Error(`Missing field "${key}" in tuple "${param.name}"`);
|
|
114
|
+
}
|
|
115
|
+
acc[key] = parseAbiArgument(comp, toArgString(parsed[key]));
|
|
116
|
+
return acc;
|
|
117
|
+
}, {});
|
|
118
|
+
}
|
|
119
|
+
throw new Error(`Invalid value for tuple "${param.name}": expected JSON object or array`);
|
|
120
|
+
}
|
|
121
|
+
throw new Error(`Unsupported ABI type: "${type}"`);
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
if (err instanceof Error && !err.message.startsWith(`[param:`)) {
|
|
125
|
+
throw new Error(`[param: "${param.name ?? '<unnamed>'}", type: "${type}"] ${err.message}`);
|
|
126
|
+
}
|
|
127
|
+
throw err;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Parses an array of raw string arguments into typed values
|
|
132
|
+
* according to the given ABI function definition.
|
|
133
|
+
*
|
|
134
|
+
* @param methodAbi - The viem `AbiFunction` describing the target method.
|
|
135
|
+
* @param args - Raw string arguments in the same order as `methodAbi.inputs`.
|
|
136
|
+
* @returns Typed argument array ready to pass to viem's `encodeFunctionData`.
|
|
137
|
+
* @throws If the argument count doesn't match or any value fails to parse.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* import { parseAbi } from "viem";
|
|
142
|
+
* import { parseAbiArguments } from "./parseAbiArguments";
|
|
143
|
+
*
|
|
144
|
+
* const [method] = parseAbi(["function transfer(address to, uint256 amount)"]);
|
|
145
|
+
* const args = parseAbiArguments(method, [
|
|
146
|
+
* "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
|
|
147
|
+
* "1000000000000000000",
|
|
148
|
+
* ]);
|
|
149
|
+
* // => ["0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", 1000000000000000000n]
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export const parseAbiArguments = (methodAbi, args) => {
|
|
153
|
+
const { inputs } = methodAbi;
|
|
154
|
+
if (args.length !== inputs.length) {
|
|
155
|
+
throw new Error(`Argument count mismatch for "${methodAbi.name}": expected ${inputs.length}, got ${args.length}`);
|
|
156
|
+
}
|
|
157
|
+
return inputs.map((input, i) => parseAbiArgument(input, args[i]));
|
|
158
|
+
};
|
|
159
|
+
//# sourceMappingURL=parse-abi-arguments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-abi-arguments.js","sourceRoot":"","sources":["../../utils/parse-abi-arguments.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,IAAI,IAAI,OAAO,EAAE,MAAM,MAAM,CAAC;AAEzD;;;;GAIG;AACH,MAAM,WAAW,GAAG,CAAC,CAAU,EAAU,EAAE,CACzC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAE,IAAI,CAAC,SAAS,CAAC,CAAC,CAAY,CAAC;AAE5D,MAAM,gBAAgB,GAAG,CAAC,KAAmB,EAAE,KAAa,EAAW,EAAE;IACvE,MAAM,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;IAEvB,IAAI,CAAC;QACH,kBAAkB;QAClB,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACvB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;gBACtB,MAAM,IAAI,KAAK,CAAC,qBAAqB,KAAK,GAAG,CAAC,CAAC;YACjD,CAAC;YACD,OAAO,KAAsB,CAAC;QAChC,CAAC;QAED,eAAe;QACf,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;YACpB,IAAI,KAAK,KAAK,MAAM;gBAAE,OAAO,IAAI,CAAC;YAClC,IAAI,KAAK,KAAK,OAAO;gBAAE,OAAO,KAAK,CAAC;YACpC,MAAM,IAAI,KAAK,CACb,wBAAwB,KA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CACb,YAAY,KAAK,CAAC,IAAI,IAAI,WAAW,aAAa,IAAI,MAAM,GAAG,CAAC,OAAO,EAAE,CAC1E,CAAC;QACJ,CAAC;QACD,MAAM,GAAG,CAAC;IACZ,CAAC;AACH,CAAC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,SAAsB,EACtB,IAAc,EACH,EAAE;IACb,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC;IAE7B,IAAI,IAAI,CAAC,MAAM,KAAK,MAAM,CAAC,MAAM,EAAE,CAAC;QAClC,MAAM,IAAI,KAAK,CACb,gCAAgC,SAAS,CAAC,IAAI,eAAe,MAAM,CAAC,MAAM,SAAS,IAAI,CAAC,MAAM,EAAE,CACjG,CAAC;IACJ,CAAC;IAED,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC,gBAAgB,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAW,CAAC,CAAC,CAAC;AAC9E,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../utils/proof/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,OAAO,EAAE,SAAS;IAClB,KAAK,EAAE,OAAO;IACd,OAAO,EAAE,SAAS;IAClB,IAAI,EAAE,MAAM;
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../utils/proof/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,OAAO,EAAE,SAAS;IAClB,KAAK,EAAE,OAAO;IACd,OAAO,EAAE,SAAS;IAClB,IAAI,EAAE,MAAM;IACZ,KAAK,EAAE,OAAO;CACf,CAAC"}
|
|
@@ -1,25 +1,39 @@
|
|
|
1
1
|
import { ssz } from '@lodestar/types';
|
|
2
|
+
import { ListCompositeType, ProgressiveListCompositeType, } from '@chainsafe/ssz';
|
|
2
3
|
import { logResult } from '../index.js';
|
|
4
|
+
// Verifier constructor arg each gindex feeds, see CLProofVerifier.sol
|
|
5
|
+
const PRE_GLOAS_PARAM = 'gIFirstValidatorPreGloas';
|
|
6
|
+
const GLOAS_PARAM = 'gIValidators';
|
|
3
7
|
export const getFirstValidatorGIndex = (forks) => {
|
|
4
|
-
const
|
|
5
|
-
for (const fork of forks) {
|
|
8
|
+
const rows = forks.map((fork) => {
|
|
6
9
|
const Fork = ssz[fork];
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
//
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
if (!Fork)
|
|
11
|
+
throw new Error(`Fork name [${fork}] is not supported`);
|
|
12
|
+
const validators = Fork.BeaconState.getPathInfo(['validators']);
|
|
13
|
+
// Gloas: the list is progressive, so it has no fixed depth to pack. The
|
|
14
|
+
// verifier takes the validators field itself and derives each node.
|
|
15
|
+
if (validators.type instanceof ProgressiveListCompositeType) {
|
|
16
|
+
return [fork, toBytes32String(pack(validators.gindex, 0n)), GLOAS_PARAM];
|
|
17
|
+
}
|
|
18
|
+
if (!(validators.type instanceof ListCompositeType)) {
|
|
19
|
+
throw new TypeError(`Unexpected validators type for fork [${fork}]`);
|
|
20
|
+
}
|
|
21
|
+
const gI = Fork.BeaconState.getPathInfo(['validators', 0]).gindex;
|
|
22
|
+
return [
|
|
23
|
+
fork,
|
|
24
|
+
toBytes32String(pack(gI, widthOf(validators.type.limit))),
|
|
25
|
+
PRE_GLOAS_PARAM,
|
|
26
|
+
];
|
|
27
|
+
});
|
|
14
28
|
logResult({
|
|
15
|
-
data:
|
|
29
|
+
data: rows,
|
|
16
30
|
params: {
|
|
17
|
-
head: ['Fork', 'GIndex'],
|
|
31
|
+
head: ['Fork', 'GIndex', 'Deploy param'],
|
|
18
32
|
},
|
|
19
33
|
});
|
|
20
34
|
};
|
|
21
|
-
const
|
|
22
|
-
|
|
35
|
+
const widthOf = (limit) => (limit ? BigInt(Math.log2(limit)) : 0n);
|
|
36
|
+
const pack = (gI, width) => {
|
|
23
37
|
return (gI << 8n) | width;
|
|
24
38
|
};
|
|
25
39
|
const toBytes32String = (gI) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"first-validator-gindex.js","sourceRoot":"","sources":["../../../utils/proof/first-validator-gindex.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"first-validator-gindex.js","sourceRoot":"","sources":["../../../utils/proof/first-validator-gindex.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AACtC,OAAO,EACL,iBAAiB,EACjB,4BAA4B,GAC7B,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAIlC,sEAAsE;AACtE,MAAM,eAAe,GAAG,0BAA0B,CAAC;AACnD,MAAM,WAAW,GAAG,cAAc,CAAC;AAEnC,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,KAAe,EAAE,EAAE;IACzD,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;QAC9B,MAAM,IAAI,GAAG,GAAG,CAAC,IAAkC,CAAC,CAAC;QACrD,IAAI,CAAC,IAAI;YAAE,MAAM,IAAI,KAAK,CAAC,cAAc,IAAI,oBAAoB,CAAC,CAAC;QAEnE,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC;QAEhE,wEAAwE;QACxE,oEAAoE;QACpE,IAAI,UAAU,CAAC,IAAI,YAAY,4BAA4B,EAAE,CAAC;YAC5D,OAAO,CAAC,IAAI,EAAE,eAAe,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,EA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typeof SupportedFork]: ContainerTreeViewType<(typeof ssz)[K]['BeaconState']['fields']>;
|
|
8
8
|
}[keyof typeof SupportedFork];
|
|
9
|
-
type Validator = ReturnType<SupportedStateView['validators']['
|
|
9
|
+
type Validator = ReturnType<SupportedStateView['validators']['get']>;
|
|
10
10
|
export type BeaconHeaderResponse = {
|
|
11
11
|
slot: number;
|
|
12
12
|
proposer_index: number;
|