Skip to content
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

Add remote-wallet crate for talking to hardware wallets #4

Merged
merged 44 commits into from
May 25, 2023
Merged

Conversation

lrettig
Copy link
Member

@lrettig lrettig commented May 8, 2023

Adds new remote-wallet crate for talking to ledger devices
Refactor function signature/FFI interface for existing ed25519-bip32 function to receive pointer to byte array
Remove unused/unneeded ledger-udev script (holdover forked solana code)
Move macros to shared crate, for now (we may want to clean this up in future when we improve error handling in #2)
Change FFI header files from C++ to C to more easily use them in CGO
Removes musl toolchain (it causes downstream issues with libs like libudev that aren't musl-friendly, and static linking seems to work fine with glibc)

Create multiple sets of cdylib and staticlib with cheaders
Misc minor cleanup, regenerate C headers
Update CI release flow to work with separate linux headers required by
musl.
Remove unused udev script.
@lrettig lrettig requested a review from poszu May 8, 2023 19:27
@lrettig lrettig marked this pull request as ready for review May 9, 2023 00:12
lrettig added 12 commits May 8, 2023 20:14
Fix branch name
Fill in some basic info
These functions now receive a bytearray ptr to write the result, and
return a u16 status code

Remove the freeptr() method
Makes CGO compatibility much easier
Receive null-terminated CStr instead of byte arrays with len for
strings. Move most code outside unsafe {}, and give variables better
names.
@lrettig lrettig requested a review from fasmat May 23, 2023 18:35
Copy link
Member

@fasmat fasmat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks OK to me, but maybe @poszu has some time to take a look at it as well?

.github/workflows/release.yml Outdated Show resolved Hide resolved
.github/workflows/release.yml Outdated Show resolved Hide resolved
@@ -1,28 +1,21 @@
/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this header file required to be written by hand?

I think https://github.com/spacemeshos/post-rs generates the C headers when it builds the library, maybe @poszu can help here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the question. It's not written by hand. There's a makefile recipe for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah my bad. It seems like https://github.com/spacemeshos/post-rs also generates the headers using a build.rs file, but doesn't commit them to the repository. I misunderstood the files to be manually edited 😞

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why check it in the repo though? Cbindgen can be used with build.rs to automatically generate a header when the rust project is being built. This is the approach taken by post-rs (see https://github.com/spacemeshos/post-rs/blob/755f5c2432dd7493b4c6457dce1556955fe9a94d/ffi/build.rs). It has a few advantages:

  • no need for extra make targets,
  • no risk of forgetting to check the header in,
  • no need to manually install cbindgen (it is declared as a build dependency and fetched automatically by cargo).

The header is later bundled together with the library in release artifacts: https://github.com/spacemeshos/post-rs/blob/fbf1b886253beec659527262499d1392490ea14d/.github/workflows/ci.yml#L155-L162

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no strong opinion. personally I kind of like manually regenerating it, and being alerted when it changes.

.github/workflows/release.yml Outdated Show resolved Hide resolved
.github/workflows/rust.yml Outdated Show resolved Hide resolved
@@ -1,28 +1,21 @@
/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why check it in the repo though? Cbindgen can be used with build.rs to automatically generate a header when the rust project is being built. This is the approach taken by post-rs (see https://github.com/spacemeshos/post-rs/blob/755f5c2432dd7493b4c6457dce1556955fe9a94d/ffi/build.rs). It has a few advantages:

  • no need for extra make targets,
  • no risk of forgetting to check the header in,
  • no need to manually install cbindgen (it is declared as a build dependency and fetched automatically by cargo).

The header is later bundled together with the library in release artifacts: https://github.com/spacemeshos/post-rs/blob/fbf1b886253beec659527262499d1392490ea14d/.github/workflows/ci.yml#L155-L162

ed25519-bip32/src/lib.rs Outdated Show resolved Hide resolved
remote-wallet/src/lib.rs Outdated Show resolved Hide resolved
remote-wallet/src/lib.rs Outdated Show resolved Hide resolved
@@ -21,5 +21,6 @@ qstring = "0.7.2"
solana-sdk = "=1.14.17"
spacemesh-derivation-path = { path = "derivation-path", version = "=0.0.1" }
spacemesh-remote-wallet = { path = "remote-wallet", version = "=0.0.1" }
spacemesh-sdkutils = { path = "sdkutils", version = "=0.0.1" }
thiserror = "1.0.40"
uriparse = "0.6.4"
Copy link
Member

@fasmat fasmat May 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix the rpath issue on macOS this file I think needs the following configuration:

[profile.release-clib]
inherits = "release"
strip = true
lto = true
rpath = true

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to go ahead and merge without this. If it's still causing issues we can open a new PR to fix it (as you did with #5)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if related but I created a new issue: #12

lrettig and others added 2 commits May 25, 2023 10:24
Simplify the code, rename toolchain -> target to be precise
Thanks @poszu
@lrettig lrettig mentioned this pull request May 25, 2023
lrettig and others added 6 commits May 25, 2023 10:45
Avoid using unwrap()s and check_err! macro extensively with a simple change. split the whole function into two. The extern "C" would be a small wrapper around a rust function. It would convert a Result<Pubkey, ...> into C error code (in case of Err) and write result (in case of Ok)

by @poszu

Co-authored-by: Bartosz Różański <[email protected]>
Ignore HW ledger test using feature instead
Thanks @poszu
Fix an import and fix unused import warning in test
to match changed function signature
@lrettig lrettig merged commit 61e8b9f into main May 25, 2023
@lrettig lrettig deleted the cffi branch May 25, 2023 15:25
@lrettig lrettig mentioned this pull request May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants