Skip to content

feat(monero-sys): Monero bindings #303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 50 commits into
base: master
Choose a base branch
from
Draft

Conversation

binarybaron
Copy link

@binarybaron binarybaron commented Apr 26, 2025

This PR aims to bringt support for native Monero bindings to our codebase. We'll be migrating away from monero-wallet-rpc and towards cxx bindings on top of wallet2_api.h.

Closes #114

This PR imports @Einliterflasche work on the monero-wallet-sys to this mono-repo.

Non-exhaustive task list:

  • Use data types from monero crate for monero-sys (e.g monero::Address)
  • Error handling using statusWithErrorString to return an error if an FFI call like create_wallet fails
  • Async wrapper functions around syncing. Call async_refresh and return once refresh is finished
  • Refactor core/swap/src/monero/wallet.rs to use monero-sys
  • Think about migrating monero-harness towards using monero-sys under the hood. If this is possible remove monero-rpc. Decide on: Do we want to keep the monero-wallet-rpc wrapper functions around? If so, why?
  • I believe the monero-wallet crate is not used at all. Remove it after confirming.
  • Ensure we can safely handle wallets concurrently
  • Migrate our integration tests to using monero-sys
  • Modify CI to have the appropriate tooling installed to compile Monero from scratch.

…round basic Wallet functions, depends on monero#9464
@binarybaron binarybaron changed the title feat(monero-sys): Initial commit. Regtest integration test. Wrapper around basic Wallet functions, depends on monero#9464 feat(monero-sys): Native Monero bindings Apr 26, 2025
@binarybaron binarybaron changed the title feat(monero-sys): Native Monero bindings feat(monero-sys): Monero bindings Apr 26, 2025
binarybaron and others added 4 commits April 26, 2025 17:56
Removed the monero-wallet crate which was not being used anywhere in the codebase,
simplifying the project structure.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@Einliterflasche
Copy link

Einliterflasche commented Apr 28, 2025

We will first replicate the monero wallet rpc interface, to keep our changes in the codebase minimal. These are the functions we currently use:

  • get_address
  • get_balance
  • create_account we don't use this
  • get_accounts unused, too
  • open_wallet
  • close_wallet
  • create_wallet
  • transfer
  • get_height
  • check_tx_key
  • generate_from_keys
  • refresh
  • sweep_all
  • get_version unused, too

@binarybaron
Copy link
Author

Check out this code from the monero_c PR (MrCyjaneK/monero_c#103): integration_tests.rs

We can most likely easily copy most of the integration tests from there. I'd try to keep it as simplistic as possible though. We only need to test the cxx bridge. We are not trying to test the Monero codebase itself.

I'd also recommend you check out lib.rs from the same PR. You might pick up on some useful patterns. sneurlax and MrCyjaneK have a ton of experience working with wallet2.h so should be worth skimming over it once.

@Einliterflasche Einliterflasche added the P1 Priority 1 label Apr 29, 2025
@binarybaron
Copy link
Author

binarybaron commented May 8, 2025

I got this compiling and tested out a swap! Awesome

Some thoughts:

  • If the GUI has no Monero node saved we will silently fail because the init context invoke fails. We should keep the behaviour from before or allow a list of nodes to be passed into the wallet and let the wallet dynamically switch between nodes at runtime. This is now possible and wasn't before because monero-wallet-rpc did not allow us to change the node at runtime.
  • Get Dockerfile working
  • Get CI working
  • Write some instructions for migrating the asb
  • Send logs from monero-sys to the GUI through a Tauri channels. It might be possible to hook into the logging library used by Monero itself.
  • Closing the GUI sometimes start to hang? (similarly to monero-wallet-gui). We might need some cleanup logic
  • I started a swap on your branch and tried to resume on master. The BlockHeight struct seems to break serialization: {"timestamp":"2025-05-09T00:41:15.022446Z","level":"ERROR","fields":{"message":"Failed to deserialize state","swap_id":"****","error":"Error(\"invalid type: integer `3407467`, expected struct BlockHeight\", line: 1, column: 2999)"},"target":"swap::database::sqlite","span":{"method":"get_swap_infos_all","name":"get_swap_infos_all"},"spans":[{"method":"get_swap_infos_all","name":"get_swap_infos_all"}]})

@Einliterflasche
Copy link

Einliterflasche commented May 9, 2025

I think some change to the caching made the build process loop indefinitely

[...]
   Compiling objc2-app-kit v0.3.1
   Compiling monero-sys v0.1.0 (/Users/me/code/xmr-btc-swap/monero-sys)
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages.pb.h changed. Rebuilding application...                            
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages-monero.pb.h changed. Rebuilding application...
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages-monero.pb.cc changed. Rebuilding application...
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages-management.pb.h changed. Rebuilding application...
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages.pb.cc changed. Rebuilding application...
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages-common.pb.h changed. Rebuilding application...
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages-management.pb.cc changed. Rebuilding application...
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages-common.pb.cc changed. Rebuilding application...
   Compiling objc2-app-kit v0.3.1
   Compiling monero-sys v0.1.0 (/Users/me/code/xmr-btc-swap/monero-sys)
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages.pb.h changed. Rebuilding application...                            
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages-management.pb.h changed. Rebuilding application...
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages-common.pb.h changed. Rebuilding application...
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages-management.pb.cc changed. Rebuilding application...
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages.pb.cc changed. Rebuilding application...
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages-common.pb.cc changed. Rebuilding application...
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages-monero.pb.h changed. Rebuilding application...
    Info File monero-sys/monero/src/device_trezor/trezor/messages/messages-monero.pb.cc changed. Rebuilding application...
   Compiling objc2-app-kit v0.3.1
   Compiling monero-sys v0.1.0 (/Users/me/code/xmr-btc-swap/monero-sys)
^C  Building [=====================> ] 1089/1113: monero-sys(build), objc2-app-kit

@binarybaron
Copy link
Author

Let's try to get this PR and #180 merged by the end of this week. #180 is close to completion and I'll be able to chime in here once that's done.

@binarybaron
Copy link
Author

CI is failing because of: actions/runner-images#6680

Using larger runners or creating a swap file should do the trick.

@binarybaron
Copy link
Author

2025-05-13T22:43:38.112019Z  INFO monero_harness: Funded alice wallet with 1000000000000
2025-05-13T22:43:38.761790Z  INFO monero_harness: Funded alice wallet with 1000000000000
2025-05-13T22:43:39.626001Z ERROR monero_cpp: Failed to connect to daemon function="boost::optional<std::string> tools::NodeRPCProxy::get_info()"
2025-05-13T22:43:39.626358Z ERROR monero_cpp: Failed to connect to daemon function="boost::optional<std::string> tools::NodeRPCProxy::get_info()"
2025-05-13T22:43:39.706602Z ERROR monero_cpp: failed to generate wallet keys file /var/folders/q3/96gvm8255230tvzw3szbj0yr0000gn/T/.tmpcIRh0X/monero/monero-data/alice.keys.new function="bool tools::wallet2::store_keys_file_data(const std::string &, wallet2::keys_file_data &, bool)"
2025-05-13T22:43:39.706618Z ERROR monero_cpp: !r. THROW EXCEPTION: error::file_save_error function="void tools::wallet2::create_keys_file(const std::string &, bool, const epee::wipeable_string &, bool)"
2025-05-13T22:43:39.706672Z  WARN monero_cpp: /Users/****/Development/UnstoppableSwap/core/monero-sys/monero/src/wallet/wallet2.cpp:5460:N5tools5error15file_error_baseILi3EEE: failed to save file "/var/folders/q3/96gvm8255230tvzw3szbj0yr0000gn/T/.tmpcIRh0X/monero/monero-data/alice.keys" function="void tools::error::throw_wallet_ex(std::string &&, const TArgs &...) [TException = tools::error::file_error_base<3>, TArgs = <std::string>]"
2025-05-13T22:43:39.706895Z ERROR monero_cpp: Error creating wallet: failed to save file "/var/folders/q3/96gvm8255230tvzw3szbj0yr0000gn/T/.tmpcIRh0X/monero/monero-data/alice.keys" function="bool Monero::WalletImpl::create(const std::string &, const std::string &, const std::string &)"
thread 'given_bob_restarts_after_xmr_is_locked_resume_swap' panicked at swap/tests/harness/mod.rs:318:6:
called `Result::unwrap()` on an `Err` value: Failed to open or create wallet `alice`

Caused by:
    0: Failed to initialize wallet `/var/folders/q3/96gvm8255230tvzw3szbj0yr0000gn/T/.tmpcIRh0X/monero/monero-data/alice`
    1: Experienced wallet error (critical): `unknown error, error not set`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    given_bob_restarts_after_xmr_is_locked_resume_swap

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 31.52s

Recovery from keys seem to fail. I have also experienced this while testing manually.

@binarybaron
Copy link
Author

binarybaron commented May 15, 2025

How Feather wallet does node management: https://deepwiki.com/feather-wallet/feather/4.1-node-management

Feather maintained list of default nodes: https://github.com/feather-wallet/feather-nodes/blob/master/nodes.yaml

@Einliterflasche Einliterflasche linked an issue May 16, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P1 Priority 1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug] Unable to Create Wallet, Wallet Exists Replace monero-wallet-rpc with monero bindings
2 participants