forked from CycleOperators/BalanceCheckerVerification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify.ts
More file actions
76 lines (71 loc) · 3.17 KB
/
Copy pathverify.ts
File metadata and controls
76 lines (71 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import fs from "fs-extra";
import { execSync } from "child_process";
import { verifyOS } from './os';
/**
* Retrieve black hole canister ID on mainnet.
*/
export async function blackholeMainnetCanisterId(): Promise<string> {
const canisterIds = JSON.parse(
await fs.readFile("canister_ids.json", "utf-8")
);
return canisterIds.blackhole.ic;
}
/**
* Retrieve black hole canister ID on local replica.
*/
async function blackholeCanisterIdLocal(): Promise<string> {
const canisterIds = JSON.parse(
await fs.readFile("./.dfx/local/canister_ids.json", "utf-8")
);
return canisterIds.blackhole.local;
}
// Retrieve the controllers and module hash of a canister.
function retrieveControllersAndModuleHash(
canister: string,
local: boolean = false
): { controllers: string[]; moduleHash: string } {
const network = local ? "" : "--network=ic";
const infoString = execSync(`dfx canister ${network} info ${canister}`).toString();
const [controllerText, moduleHashText] = infoString.split("\n");
const controllers = controllerText
.split(":")[1]
.trim().split(" ")
// filter out empty strings (that aren't controllers)
.filter(controller => controller !== "");
const moduleHash = moduleHashText.split(":")[1].trim();
return { controllers, moduleHash };
};
/**
* Verifies the status of the mainnet black hole canister in this repository.
*/
async function run() {
verifyOS();
const canisterLocal = await blackholeCanisterIdLocal();
const canisterMainnet = await blackholeMainnetCanisterId();
console.log(`Verifiying canister ${await blackholeMainnetCanisterId()}...`);
const localCanister = retrieveControllersAndModuleHash(canisterLocal, true);
const mainnetCanister = retrieveControllersAndModuleHash(canisterMainnet, false);
//const { controllers, mainnetModuleHash } = retrieveControllersAndModuleHash(canister, local);
console.log("\nmainnet canister controllers =", mainnetCanister.controllers);
const hasNoControllers = mainnetCanister.controllers.length == 0;
if (hasNoControllers === true) {
console.log(`\n✅ canister ${canisterMainnet} zero controllers verified.`);
} else {
console.log(`\n❌ canister ${canisterMainnet} has ${mainnetCanister.controllers.length} controllers (${mainnetCanister.controllers}) and is not a valid black hole!`);
}
// Determine if the hash of the local source code matches the hash of the source code running on mainnet.
const isSourceCodeVerified = localCanister.moduleHash == mainnetCanister.moduleHash;
console.log("\nlocal canister module hash ", localCanister.moduleHash);
console.log("mainnet canister module hash", mainnetCanister.moduleHash)
if (isSourceCodeVerified === true) {
console.log(`️\n✅ canister ${canisterMainnet} source code verified.`);
} else {
console.log(`\n❌ canister ${canisterMainnet} source code is not verified!`);
}
if (hasNoControllers && isSourceCodeVerified) {
console.log(`\n✅ verification and blackhole check of canister ${canisterMainnet} successful.\n`);
} else {
throw new Error(`\n❌ verification and blackhole check of canister ${canisterMainnet} failed. Do NOT add this canister as a controller\n`);
}
}
if (require.main === module) run();