summaryrefslogtreecommitdiff
path: root/impls/monero.ts/src/utils.ts
diff options
context:
space:
mode:
authorKonstantin Ullrich <konstantinullrich12@gmail.com>2024-10-07 12:37:30 +0200
committerKonstantin Ullrich <konstantinullrich12@gmail.com>2024-10-07 12:37:30 +0200
commit98272ee381bd07081502dd426226f58c879300a6 (patch)
tree672f6f06727dbc1c84270973ce13e9403913e481 /impls/monero.ts/src/utils.ts
parent04b29d84a2c368c677cf5ec946269203622ca170 (diff)
parent67f4baa015a4407d096e35b6e5a81d72932fb55f (diff)
Merge branch 'master' into ledger
# Conflicts: # .github/workflows/full_check.yaml # impls/monero.dart/lib/monero.dart # impls/monero.dart/pubspec.yaml # patches/monero/0016-add-dummy-device-for-ledger.patch
Diffstat (limited to 'impls/monero.ts/src/utils.ts')
-rw-r--r--impls/monero.ts/src/utils.ts25
1 files changed, 25 insertions, 0 deletions
diff --git a/impls/monero.ts/src/utils.ts b/impls/monero.ts/src/utils.ts
new file mode 100644
index 0000000..6fa640f
--- /dev/null
+++ b/impls/monero.ts/src/utils.ts
@@ -0,0 +1,25 @@
+import { dylib } from "../mod.ts";
+
+export type Sanitizer = () => void | PromiseLike<void>;
+
+const textEncoder = new TextEncoder();
+export function CString(string: string): Deno.PointerValue<string> {
+ return Deno.UnsafePointer.of(textEncoder.encode(`${string}\x00`));
+}
+
+/**
+ * This method reads string from the given pointer and frees the string.
+ *
+ * SAFETY: Do not use readCString twice on the same pointer as it will cause double free\
+ * If you want to read CString without freeing it set the {@linkcode free} parameter to false
+ */
+export async function readCString(pointer: Deno.PointerObject, free?: boolean): Promise<string>;
+export async function readCString(pointer: Deno.PointerValue, free?: boolean): Promise<string | null>;
+export async function readCString(pointer: Deno.PointerValue, free = true): Promise<string | null> {
+ if (!pointer) return null;
+ const string = new Deno.UnsafePointerView(pointer).getCString();
+ if (free) {
+ await dylib.symbols.MONERO_free(pointer);
+ }
+ return string;
+}