Emurgo / cardano-serialization-lib

This is a library, written in Rust, for serialization & deserialization of data structures used in Cardano's Haskell implementation of Alonzo along with useful utility functions.
Other
235 stars 125 forks source link

Native usage library #267

Open Milerius opened 2 years ago

Milerius commented 2 years ago

Hello, I was curious if this library could be used in a native rust target that is not wasm or web.

For example to add light wallet functionality to a native rust target (amd64)

I was looking to potentially add Cardano support to https://github.com/KomodoPlatform/atomicDEX-API

The project is written in rust and supports all existing platforms: amd64, arm & wasm. But if I take a good look at this project, I feel like I can only use the wasm version currently, the documentation is not very clear on this.

rooooooooob commented 2 years ago

You can use it directly from rust as well. The documentation was written more for web users as those comprise most of the people using the library but it's totally possible to use it from rust. Some of the APIs are a bit clunky from rust due to having to conform to wasm-bindgen though. We have a crate on cargo: https://crates.io/crates/cardano-serialization-lib/

Milerius commented 2 years ago

You can use it directly from rust as well. The documentation was written more for web users as those comprise most of the people using the library but it's totally possible to use it from rust. Some of the APIs are a bit clunky from rust due to having to conform to wasm-bindgen though. We have a crate on cargo: https://crates.io/crates/cardano-serialization-lib/

I wonder how it's will look like on native target to deal with JsError from what I understand this type exist only if the target platform is wasm no ?

edit: I see that JsError is a string on native platform - all good.

I still think that few example in pure Rust would have been nice

rooooooooob commented 2 years ago

Yeah, as you saw I guess with your edit we have

#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
pub type JsError = JsValue;

#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
#[derive(Debug, Clone)]
pub struct JsError {
    msg: String,
}

which isn't ideal but it at least won't panic in rust builds like JsValue/JsError from js_sys would. Hopefully one day wasm-bindgen will allow directly exposing rust error types or some kind of automatic conversion while preserving the rust error types for non-wasm builds. There's an open issue for that so maybe one day rustwasm/wasm-bindgen#1004 and some other related ones. Fingers crossed.

The JS examples should work almost the same you'll just need to replace things like Foo.bar() with Foo::bar() and handle Result/Option return values and reference parameters explicitly. We don't heavily use the more transformative wasm-bindgen renaming/changing things (e.g. to use new in JS) so it's pretty much 1:1.

Milerius commented 2 years ago

Thanks for all those precious explanation, I may have some more questions later, is there any discord or telegram for dev?

rooooooooob commented 2 years ago

Thanks for all those precious explanation, I may have some more questions later, is there any discord or telegram for dev?

@SebastienGllmt @vsubhuman

Milerius commented 2 years ago

Thanks for all those precious explanations. I may have some more questions later. Is there any discord or telegram for dev?

@SebastienGllmt @vsubhuman

I joined the following Cardano Community Discord https://discord.gg/VQRHtDpJ would be nice if Emurgo team would have a channel to discuss possible light wallet integration, I'm curious and may have some questions regarding light node communication (How to fetch balance, transaction history, broadcasting a transaction)

I understand that the scope of cardano-serialization-lib is to create transactions, keypair and everything crypto-related. Still, I'm also curious to know more about the other parts for a full wallet integration.

rooooooooob commented 2 years ago

I'm going to link to #276 here so that people inquiring about rust native usage can help tell us what they would like to see potentially.