2024-09-19 10:59:07 +02:00
|
|
|
import { moneroChecksum } from "./checksum_monero.ts";
|
2024-12-01 15:02:20 +01:00
|
|
|
import { readCString } from "./src/utils.ts";
|
|
|
|
|
import { fns, loadMoneroDylib } from "./src/bindings.ts";
|
2024-09-19 10:59:07 +02:00
|
|
|
|
2024-10-16 07:55:11 +02:00
|
|
|
loadMoneroDylib();
|
2024-09-19 10:59:07 +02:00
|
|
|
|
|
|
|
|
export class ChecksumError extends Error {
|
|
|
|
|
readonly code: number;
|
|
|
|
|
readonly errors: string[];
|
|
|
|
|
|
|
|
|
|
constructor(code: number, errors: string[]) {
|
|
|
|
|
super("MoneroC binding checksum failed:\n" + errors.join("\n"));
|
|
|
|
|
this.code = code;
|
|
|
|
|
this.errors = errors;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validates MoneroC checksums
|
|
|
|
|
* @returns {null} if checksums are correct
|
|
|
|
|
* @returns {ChecksumError} which contains information about why checksum failed
|
|
|
|
|
*/
|
|
|
|
|
export async function validateChecksum(): Promise<ChecksumError | null> {
|
2024-12-01 15:02:20 +01:00
|
|
|
const cppHeaderHash = await readCString(await fns.checksum_wallet2_api_c_h!(), false);
|
2024-09-19 10:59:07 +02:00
|
|
|
const tsHeaderHash = moneroChecksum.wallet2_api_c_h_sha256;
|
|
|
|
|
|
|
|
|
|
const errors: string[] = [];
|
|
|
|
|
|
|
|
|
|
let errorCode = 0;
|
|
|
|
|
if (cppHeaderHash !== tsHeaderHash) {
|
|
|
|
|
errors.push("ERR: Header file check mismatch");
|
|
|
|
|
errorCode++;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-01 15:02:20 +01:00
|
|
|
const cppSourceHash = await readCString(await fns.checksum_wallet2_api_c_cpp!(), false);
|
2024-09-19 10:59:07 +02:00
|
|
|
const tsSourceHash = moneroChecksum.wallet2_api_c_cpp_sha256;
|
|
|
|
|
if (cppSourceHash !== tsSourceHash) {
|
|
|
|
|
errors.push(`ERR: CPP source file check mismatch ${cppSourceHash} == ${tsSourceHash}`);
|
|
|
|
|
errorCode++;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-01 15:02:20 +01:00
|
|
|
const cppExportHash = await readCString(await fns.checksum_wallet2_api_c_exp!(), false);
|
2024-09-19 10:59:07 +02:00
|
|
|
const tsExportHash = moneroChecksum.wallet2_api_c_exp_sha256;
|
|
|
|
|
if (cppExportHash !== tsExportHash) {
|
|
|
|
|
if (Deno.build.os !== "darwin") {
|
|
|
|
|
errors.push("WARN: EXP source file check mismatch");
|
|
|
|
|
} else {
|
|
|
|
|
errors.push(`ERR: EXP source file check mismatch ${cppExportHash} == ${tsExportHash}`);
|
|
|
|
|
}
|
|
|
|
|
errorCode++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (errorCode) {
|
|
|
|
|
return new ChecksumError(errorCode, errors);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (import.meta.main) {
|
|
|
|
|
const maybeError = await validateChecksum();
|
|
|
|
|
if (maybeError) {
|
|
|
|
|
throw maybeError;
|
|
|
|
|
}
|
|
|
|
|
}
|