GeniusVentures / GeniusWallet

MIT License
9 stars 2 forks source link

Make sure FFI interface from C++ <-> Dart is using static libraries #34

Open Super-Genius opened 11 months ago

Super-Genius commented 11 months ago

The Original code assumed we were using dynamic/shared libraries/DLLs.

We are not, so the FFI code needs to be updated to use static libraries generated in ThirdParty.

https://docs.flutter.dev/platform-integration/ios/c-interop#dynamic-vs-static-linking

henriqueaklein commented 11 months ago

@Super-Genius I did some searching on the repo and found that the "genius_api_ffi.dart" is not referenced anywhere. I've debugged the code and as far as I could test the debugger never landed on this source file:

class NativeLibrary {
  /// Holds the symbol lookup function.
  final ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
      _lookup;

  /// The symbols are looked up in [dynamicLibrary].
  NativeLibrary(ffi.DynamicLibrary dynamicLibrary)
      : _lookup = dynamicLibrary.lookup;

Is this the code you were talking about? Also, what are the main libraries I need to load? I've checked them on the Release folder of the thirdparty, but I couldn't quite track it down what lib (.so or .a) the symbols (TWDataCreateWithData and etc) from wallet-core are.

Do you have any pointers on that?

henriqueaklein commented 11 months ago

In order to test anything I'm trying to compile wallet-core. It caused an error due to rust nightly usage, since some things got "broken" on latest nightly builds. I temporarily changed the "install-rust-dependencies" script to a fixed nightly release:

  rustup toolchain install nightly-2023-05-11
  rustup default nightly-2023-05-11

@Super-Genius Right now I'm having some issues on CMakeLists, since it's requesting the boost directory. I infer that I'm supposed to point to the build/Android (or Linux or iOS)/Release/boost, correct?

Also, after some research I don't think wallet-core was ever linked to GeniusWallet. After I'm able to create the libraries from wallet-core I'm gonna try to embed them on a wrapper on the application (without an extra plugin). The current build.gradle compiles the "native.cpp" source and creates an "libnative.so", which then its linked by the dart:ffi abstraction layer. My end goal is to replace this "native.cpp" test for a "TrustWalletWrapper.cpp" which will have basically "extern "C" and static or singletons for the wallet-core static libraries I'll link once I have them compiled. What do you think?

Super-Genius commented 11 months ago

Yes boost library linking should follow SuperGenius CMakeLists.txt default location for thirdparty and Find_Package() locating boost automatically. CMakeLists.Txt has a function that looks for a define for THIRDPARTY_DIR and if not defined sets to ../../thirdparty relative directory and that is used to define directories for header files and library builds with the target OS defines

The WalletCore and GeniusSDK were never built and linked in using plugins. So, yes. This is the right direction for you to proceed

henriqueaklein commented 10 months ago

I paused this task to compile the wallet-core on Android so I'd have something to test it. So far I've only changed CMakeLists and build.gradle to forward wallet-core library to flutter. I was able to load a function pointer (the wallet-core already exports C like symbols). I'll check if this approach is the best one, since it takes a lot of memory (had to increase emulator's heap to 2GB I think) and also I read that there's another way to link it statically with JNI

vampiebar commented 10 months ago

There is no other easy ( less ram method ) for this FFI, you need heap memory up to 3-4 GB for compilation. Because it needs to see all FFI functions at once to build. Those are the side effect for dart / jni ( like in java JNI )

On Mon, Aug 21, 2023 at 5:30 PM Henrique A Klein @.***> wrote:

I paused this task to compile the wallet-core on Android so I'd have something to test it. So far I've only changed CMakeLists and build.gradle to forward wallet-core library to flutter. I was able to load a function pointer (the wallet-core already exports C like symbols). I'll check if this approach is the best one, since it takes a lot of memory (had to increase emulator's heap to 2GB I think) and also I read that there's another way to link it statically with JNI

— Reply to this email directly, view it on GitHub https://github.com/GeniusVentures/GeniusWallet/issues/34#issuecomment-1686443880, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALLSR2T63XDOEFU2XDCHOTXWNWGXANCNFSM6AAAAAA2R2GBHQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

henriqueaklein commented 7 months ago

Trust wallet core is being loaded as a static library, along with its dependencies (which aren't "whole-archived"). The procedure to insert extra libraries is pretty straightforward from now on, although for it to be added on all OSs one needs to change a couple of different places (cmakelists, runner xcode projects)