@freedomofpress/cometbft 0.1.0

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/src/types.ts ADDED
@@ -0,0 +1,63 @@
1
+ export interface ValidatorJson {
2
+ block_height?: string;
3
+ validators: {
4
+ address: string;
5
+ pub_key: {
6
+ type: string;
7
+ value: string;
8
+ };
9
+ voting_power?: string;
10
+ power?: string;
11
+ proposer_priority?: string;
12
+ }[];
13
+ count: string;
14
+ total: string;
15
+ }
16
+
17
+ export interface CommitJson {
18
+ signed_header: {
19
+ header: {
20
+ version: {
21
+ block: string;
22
+ app: string;
23
+ };
24
+ chain_id: string;
25
+ height: string;
26
+ time: string; // RFC3339 timestamp
27
+ last_block_id: {
28
+ hash: string;
29
+ parts: {
30
+ total: number;
31
+ hash: string;
32
+ };
33
+ };
34
+ last_commit_hash: string;
35
+ data_hash: string;
36
+ validators_hash: string;
37
+ next_validators_hash: string;
38
+ consensus_hash: string;
39
+ app_hash: string;
40
+ last_results_hash: string;
41
+ evidence_hash: string;
42
+ proposer_address: string;
43
+ };
44
+ commit: {
45
+ height: string;
46
+ round: number;
47
+ block_id: {
48
+ hash: string;
49
+ parts: {
50
+ total: number;
51
+ hash: string;
52
+ };
53
+ };
54
+ signatures: {
55
+ block_id_flag: number;
56
+ validator_address: string;
57
+ timestamp: string;
58
+ signature: string; // base64
59
+ }[];
60
+ };
61
+ };
62
+ canonical?: boolean;
63
+ }
@@ -0,0 +1,84 @@
1
+ import { base64ToUint8Array, Uint8ArrayToHex } from "./encoding";
2
+ import { Validator, ValidatorSet } from "./proto/cometbft/types/v1/validator";
3
+ import type { ValidatorJson } from "./types";
4
+
5
+ export async function importValidators(resp: ValidatorJson): Promise<{
6
+ proto: ValidatorSet;
7
+ cryptoIndex: Map<string, CryptoKey>;
8
+ }> {
9
+ const seen: Set<string> = new Set();
10
+ const cryptoIndex = new Map<string, CryptoKey>();
11
+ const protoValidators: Validator[] = [];
12
+
13
+ if (!resp.validators || resp.validators.length < 1) {
14
+ throw new Error("Missing validators");
15
+ }
16
+ let countedPower = 0n;
17
+
18
+ for (const v of resp.validators) {
19
+ if (!v.address || v.address.length !== 40) {
20
+ throw new Error(
21
+ `Validator address must be 40 HEX digits, provided: ${v.address}`,
22
+ );
23
+ }
24
+ if (!v.pub_key?.type || !v.pub_key?.value) {
25
+ throw new Error("Validator key object is invalid");
26
+ }
27
+ if (v.pub_key.type !== "tendermint/PubKeyEd25519") {
28
+ throw new Error(
29
+ `Key of type ${v.pub_key.type} is currently unsupported.`,
30
+ );
31
+ }
32
+
33
+ const rawKey = base64ToUint8Array(v.pub_key.value);
34
+ const key = await crypto.subtle.importKey(
35
+ "raw",
36
+ new Uint8Array(rawKey),
37
+ { name: "Ed25519" },
38
+ false,
39
+ ["verify"],
40
+ );
41
+
42
+ const sha = new Uint8Array(
43
+ await crypto.subtle.digest("SHA-256", new Uint8Array(rawKey)),
44
+ );
45
+ const addrHex = Uint8ArrayToHex(sha.slice(0, 20)).toUpperCase();
46
+
47
+ if (addrHex !== v.address.toUpperCase()) {
48
+ throw new Error(`Address ${v.address} does not match its public key`);
49
+ }
50
+ if (seen.has(addrHex)) {
51
+ throw new Error("Duplicate entry in validators set");
52
+ }
53
+ seen.add(addrHex);
54
+ cryptoIndex.set(addrHex, key);
55
+
56
+ const powerNum = Number(v.voting_power) || Number(v.power);
57
+ if (
58
+ !Number.isFinite(powerNum) ||
59
+ !Number.isInteger(powerNum) ||
60
+ powerNum < 1
61
+ ) {
62
+ throw new Error(`Invalid voting power for ${addrHex}`);
63
+ }
64
+ countedPower += BigInt(powerNum);
65
+
66
+ const protoV: Validator = {
67
+ address: new Uint8Array(sha.slice(0, 20)),
68
+ pubKeyBytes: rawKey,
69
+ pubKeyType: "ed25519",
70
+ votingPower: BigInt(powerNum),
71
+ proposerPriority: 0n, // JSON gives string "0"; use 0n by default
72
+ };
73
+
74
+ protoValidators.push(protoV);
75
+ }
76
+
77
+ const protoSet: ValidatorSet = {
78
+ validators: protoValidators,
79
+ proposer: undefined,
80
+ totalVotingPower: countedPower,
81
+ };
82
+
83
+ return { proto: protoSet, cryptoIndex };
84
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "lib": ["DOM", "ES2022"],
5
+ "module": "ESNext",
6
+ "sourceMap": true,
7
+ "moduleResolution": "node",
8
+ "strict": true,
9
+ "esModuleInterop": true,
10
+ "resolveJsonModule": true,
11
+ "declaration": true,
12
+ "skipLibCheck": true,
13
+ "outDir": "./dist"
14
+ },
15
+ "include": ["./src/**/*.ts"],
16
+ "exclude": ["node_modules"]
17
+ }
@@ -0,0 +1,12 @@
1
+ import { defineConfig } from "vitest/config";
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ coverage: {
6
+ provider: "v8",
7
+ reporter: ["text", "html", "json"],
8
+ include: ["src/**/*.ts"],
9
+ exclude: ["node_modules/**", "dist/**", "src/proto/**"],
10
+ },
11
+ },
12
+ });