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

feat: added feature flag to core crypto to support wasm build #6387

Closed
wants to merge 3 commits into from

Conversation

russellwmy
Copy link

Background:

There is not a good package to support secp256k1 in WASM build. Therefore, we should add a feature flag to disable secp256k1 to allow developer to build without secp256k1.

Changes:

  • added feature flag secp256k1
  • added secp256k1 as default feature

How to test:

  • Run cargo build, it should build successfully
  • Run cargo build --no-default-features --target wasm32-unknown-unknown, it should build successfully

@russellwmy russellwmy closed this Mar 4, 2022
@russellwmy russellwmy reopened this Mar 4, 2022
@russellwmy russellwmy closed this Mar 4, 2022
@matklad
Copy link
Contributor

matklad commented Mar 4, 2022

If we want to do this in the future, one question I would have is why do we want this crate to be compatible with wasm

@russellwmy
Copy link
Author

russellwmy commented Mar 4, 2022

@matklad

My use case:

I am working with yew.rs to build Dapp. However, there is only near-api-js for a web app to interact with the chain. So I have to build a Rust version of api sdk to enable yew.rs to interact with the chain.

For future use case:

It enables developers can build web Dapp in Rust.

My motivation:

I build product in one programming language. So my team can manage the product from frontend to backend. It is important to me to enable scaling team smoothly. Since everyone work in Rust.

I am not sure if it is the way near will do. I will keep the logic in my repository first.

@russellwmy
Copy link
Author

@matklad if you are interested to the SDK, you can check https://github.com/russellwmy/near-api-rs. But it is still WIP.

@austinabell
Copy link
Contributor

austinabell commented Mar 9, 2022

My use case:

I am working with yew.rs to build Dapp. However, there is only near-api-js for a web app to interact with the chain. So I have to build a Rust version of api sdk to enable yew.rs to interact with the chain.

For future use case:

It enables developers can build web Dapp in Rust.

My motivation:

I build product in one programming language. So my team can manage the product from frontend to backend. It is important to me to enable scaling team smoothly. Since everyone work in Rust.

I am not sure if it is the way near will do. I will keep the logic in my repository first.

Can you be more specific about what you are looking for? Is there some functionality that is missing from the secp/ed libraries in Rust, or is it just that you'd like a library that handles the different key types together with a shared API?

if you are interested to the SDK, you can check russellwmy/near-api-rs. But it is still WIP.

Unlikely it would come into the SDK, since it sounds like this would not be used within a contract. Some library could potentially be built out to expose a simple API that handles NEAR supported crypto operations, which could be used in the SDK or any other Rust tooling lib.

Edit: it seems like you just wanted ed key support, but would it be beneficial to have secp also included with a wasm compatible package? If only using ED25519, I'm missing the motivation for using the nearcore internal crate

@russellwmy
Copy link
Author

russellwmy commented Mar 9, 2022

@austinabell

Can you be more specific about what you are looking for? Is there some functionality that is missing from the secp/ed libraries in Rust, or is it just that you'd like a library that handles the different key types together with a shared API?

I am looking for a Rust SDK can run in browser. So my application can interact with Near platform in browser. Which my application frontend is built with Rust framework yew.rs. I only find Rust SDK for interact with smart contract.

Unlikely it would come into the SDK, since it sounds like this would not be used within a contract. Some library could potentially be built out to expose a simple API that handles NEAR supported crypto operations, which could be used in the SDK or any other Rust tooling lib.

That would be good to have Rust SDK to do that. I did research for awhile. I cannot find one.

Edit: it seems like you just wanted ed key support, but would it be beneficial to have secp also included with a wasm compatible package? If only using ED25519, I'm missing the motivation for using the nearcore internal crate

The internal crate using https://crates.io/crates/secp256k1. I try to build it but it seems it cannot compile into wasm. Maybe you have idea to make it works. But I noticed near-api-js also does not implement that key_type. So I add feature flag to disable until there is a method to build or find replacement.

@austinabell
Copy link
Contributor

I am looking for a Rust SDK can run in browser. So my application can interact with Near platform in browser. Which my application frontend is built with Rust framework yew.rs. I only find Rust SDK for interact with smart contract.

You can use https://github.com/near/near-jsonrpc-client-rs (cc @miraclx) or even https://github.com/near/workspaces-rs/ (although this is primarily just used as a testing framework for now)

The Rust SDK is for building smart contracts, not for interacting with them

Unlikely it would come into the SDK, since it sounds like this would not be used within a contract. Some library could potentially be built out to expose a simple API that handles NEAR supported crypto operations, which could be used in the SDK or any other Rust tooling lib.

That would be good to have Rust SDK to do that. I did research for awhile. I cannot find one.

Edit: it seems like you just wanted ed key support, but would it be beneficial to have secp also included with a wasm compatible package? If only using ED25519, I'm missing the motivation for using the nearcore internal crate

The internal crate using crates.io/crates/secp256k1. I try to build it but it seems it cannot compile into wasm. Maybe you have idea to make it works. But I noticed near-api-js also does not implement that key_type. So I add feature flag to disable until there is a method to build or find replacement.

But you could just use a ed25519 library that compiles to wasm? What comes with the nearcore crate that doesn't exist there? A simpler API?

@russellwmy
Copy link
Author

russellwmy commented Mar 9, 2022

You can use https://github.com/near/near-jsonrpc-client-rs (cc @miraclx) or even https://github.com/near/workspaces-rs/ (although this is primarily just used as a testing framework for now)
The Rust SDK is for building smart contracts, not for interacting with them

added near-jsonrpc-client-rs, it seems cannot run in yew.rs app. I am looking for somethings like: https://github.com/tomusdrw/rust-web3
Screenshot 2022-03-10 at 7 52 57 AM

But you could just use a ed25519 library that compiles to wasm? What comes with the nearcore crate that doesn't exist there? A simpler API?

In that case, I can do that, just create another things to the same things that nearcore provides. But I am thinking I am reinventing the wheel. Btw, I also think core/primitives and jsonrpc-primitives should be the package with less dependencies and allow other developer to develop thing base on that, cause they are core types.

@matklad
Copy link
Contributor

matklad commented Mar 19, 2022

Drive by, but I've learned today that https://docs.rs/ed25519-compact/latest/ed25519_compact/ exists, and, from the cursory look, it might be better suited for wasm than ed25519-dalek which we use for nearcore.

@russellwmy
Copy link
Author

russellwmy commented Mar 20, 2022

@matklad Yes, we can replace the library. But we should replace parity-secp256k1 with libsecp256k1 for specp256k1. it is a blocker for compiling the crypto-core in WASM. parity-secp256k1 had not update for 2 years.
And we need to review the version of other libraries. I found number of libraries are out-dated.

BTW, I am successfully compile ed25519-dalek and libsecp256k1 in WASM. You can check https://github.com/russellwmy/web3-rs/tree/near/near if you have time.

@russellwmy
Copy link
Author

russellwmy commented Mar 21, 2022

Drive by, but I've learned today that https://docs.rs/ed25519-compact/latest/ed25519_compact/ exists, and, from the cursory look, it might be better suited for wasm than ed25519-dalek which we use for nearcore.

I just check their code. It is cool, small and less dependencies. We can check what is the security level first if we want to employ into nearcore. It is good for near if near is going to target embedded system in the future.

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