Files
hash-wallet/cw_mweb/lib/mweb_ffi.dart
T
309ad1836e use ffi for mweb (#2456)
* chore: add ffi and ffigen dependencies to pubspec.yaml

* refactor: replace method channel with FFI implementation for MWeb module

* fix: correct `cd` command in build_mwebd.sh script

* chore: add go.sum file with dependency checksums

* fix: ensure ANDROID_NDK_VERSION is set before building and dynamically update NDK_BIN path

* fix: update NDK_BIN path to dynamically detect platform in build_mwebd.sh script

* refactor: remove unused mwebd dependencies and clean up CwMwebPlugin code

* Update scripts/android/build_mwebd.sh

Co-authored-by: cyan <cyjan@mrcyjanek.net>

* refactor: replace iOS MoneroWallet framework with Mwebd.xcframework and clean up plugin implementation [skip-ci]

* fix: mwebd ffi ios

* refactor: update MWebFfi class to use non-nullable `lib` and improve address debug logging

* chore: remove unused iOS/Android build scripts for mwebd

* refactor: replace mweb.so with libmweb.so, use sync methods in MWebFfi, and update iOS/Android frameworks/scripts accordingly

* chore: unify mwebd build process in workflows, remove unused gomobile init command

---------

Co-authored-by: cyan <cyjan@mrcyjanek.net>
2025-09-12 16:13:00 +03:00

69 lines
1.7 KiB
Dart

import 'dart:ffi';
import 'dart:io';
import 'dart:typed_data';
import 'package:cw_mweb/generated_bindings.g.dart';
import 'package:ffi/ffi.dart';
String libPath = (() {
if (Platform.isWindows) return 'mweb.dll';
if (Platform.isMacOS) return 'mweb.dylib';
if (Platform.isIOS) return 'Mwebd.framework/Mwebd';
if (Platform.isAndroid) return 'libmweb.so';
return 'libmweb.so';
})();
class MWebFfi {
late final MWebFlutter lib;
MWebFfi() : lib = MWebFlutter(DynamicLibrary.open(libPath));
static MWebFfi instance = MWebFfi();
int start(String dataDir, String nodeUri) {
final chain = "".toNativeUtf8().cast<Char>();
final dataDir_ = dataDir.toNativeUtf8().cast<Char>();
final nodeUri_ = nodeUri.toNativeUtf8().cast<Char>();
final port = lib.StartServer(chain, dataDir_, nodeUri_);
calloc.free(chain);
calloc.free(dataDir_);
calloc.free(nodeUri_);
return port;
}
void stop() => lib.StopServer();
String addresses(
Uint8List scanSecret, Uint8List spendPub, int fromIndex, int toIndex) {
final scanSecretPtr = malloc.allocate<Uint8>(scanSecret.length);
for (int k = 0; k < scanSecret.length; k++) {
scanSecretPtr[k] = scanSecret[k];
}
final spendPubKeyPtr = malloc.allocate<Uint8>(spendPub.length);
for (int k = 0; k < spendPub.length; k++) {
spendPubKeyPtr[k] = spendPub[k];
}
final Pointer<Char> resultPtr = lib.Addresses(
scanSecretPtr.cast(),
scanSecret.length,
spendPubKeyPtr.cast(),
spendPub.length,
fromIndex,
toIndex,
);
final result = resultPtr.cast<Utf8>().toDartString();
malloc.free(scanSecretPtr);
malloc.free(spendPubKeyPtr);
malloc.free(resultPtr);
return result;
}
}