import 'dart:ffi'; import 'package:ffi/ffi.dart'; import 'dart:convert'; class PayloadResult { final String payload; final String err; final int errCode; const PayloadResult(this.payload, this.err, this.errCode); } // Executes the provided fn and converts the string response to a PayloadReult. // Returns payload, error code, and error. PayloadResult executePayloadFn({ required Pointer fn(), required List ptrsToFree, bool skipErrorCheck = false, }) { final jsonStr = fn().toDartString(); freePointers(ptrsToFree); if (jsonStr == null) throw Exception("no json return from wallet library"); final decoded = json.decode(jsonStr); final err = decoded["error"] ?? ""; if (!skipErrorCheck) { checkErr(err); } final payload = decoded["payload"] ?? ""; final errCode = decoded["errorcode"] ?? -1; return new PayloadResult(payload, err, errCode); } void freePointers(List ptrsToFree) { for (final ptr in ptrsToFree) { malloc.free(ptr); } } void checkErr(String err) { if (err == "") return; throw Exception(err); } extension StringUtil on String { Pointer toCString() => toNativeUtf8().cast(); } extension CStringUtil on Pointer { bool get isNull => address == nullptr.address; free() { malloc.free(this); } String? toDartString() { if (isNull) return null; final str = cast().toDartString(); free(); return str; } }