summaryrefslogtreecommitdiff
path: root/impls/monero.ts/checksum.ts
blob: 22d30383db2110aa6d654514bc1a1a9f63e60709 (plain)
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
import { moneroChecksum } from "./checksum_monero.ts";
import { readCString } from "./src/utils.ts";
import { dylib, loadDylib } from "./src/bindings.ts";

loadDylib();

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> {
  const cppHeaderHash = await readCString(await dylib.symbols.MONERO_checksum_wallet2_api_c_h(), false);
  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++;
  }

  const cppSourceHash = await readCString(await dylib.symbols.MONERO_checksum_wallet2_api_c_cpp(), false);
  const tsSourceHash = moneroChecksum.wallet2_api_c_cpp_sha256;
  if (cppSourceHash !== tsSourceHash) {
    errors.push(`ERR: CPP source file check mismatch ${cppSourceHash} == ${tsSourceHash}`);
    errorCode++;
  }

  const cppExportHash = await readCString(await dylib.symbols.MONERO_checksum_wallet2_api_c_exp(), false);
  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;
  }
}