summaryrefslogtreecommitdiff
path: root/impls/monero.ts/checksum.ts
diff options
context:
space:
mode:
authorcyan <cyjan@mrcyjanek.net>2024-09-19 10:59:07 +0200
committerGitHub <noreply@github.com>2024-09-19 10:59:07 +0200
commitd1940944943cd79ce1f2b2080456dd28bec5c36f (patch)
tree8a2a29c5de6cec47ccefff3472f38a8ecb9fb8af /impls/monero.ts/checksum.ts
parent0b91e91d19d4a11d753f9bdda56398bcfcdb4260 (diff)
feat: deno ffi bindings (#40)
* initial ts commit * monero.ts improvements * test on latest, build on debian:bookworm * feat: upstream changes to bindings * chore: update checksums * feat: allow manually loading dylib * chore: add readme * fix: free strings after being read to prevent potential memory leaks * fix: load dylib * fix: checksum checks segfaulting because of freeing const variables --------- Co-authored-by: Mateusz Franik <47059999+Im-Beast@users.noreply.github.com> Co-authored-by: Im-Beast <franik.mateusz@gmail.com>
Diffstat (limited to 'impls/monero.ts/checksum.ts')
-rw-r--r--impls/monero.ts/checksum.ts65
1 files changed, 65 insertions, 0 deletions
diff --git a/impls/monero.ts/checksum.ts b/impls/monero.ts/checksum.ts
new file mode 100644
index 0000000..22d3038
--- /dev/null
+++ b/impls/monero.ts/checksum.ts
@@ -0,0 +1,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;
+ }
+}