sdk icon indicating copy to clipboard operation
sdk copied to clipboard

[Feature] Is that there have pure rust code without wasm binding can provides functions in dir sdk/wasm ?

Open gtmickey opened this issue 1 year ago • 3 comments

🚀 Feature

I would like to ask of there are some pure rust codes can provide functions like dir sdk/wasm , so I can use them to build android so and ios .framework, and then call from dart lang

Motivation

So that I can use flutter_rust_bridge to call rust api to do new privatekey->viewKey->address, and sign message, transfer credits

Implementation

now I can do it with some functions, like privatekey's api,


#[flutter_rust_bridge::frb(sync)]
pub fn to_address(private_key: String) -> String {
    return PrivateKey::from_string(&private_key).unwrap().to_address().to_string();
}

#[flutter_rust_bridge::frb(sync)]
pub fn to_view_key(private_key: String) -> String {
    return PrivateKey::from_string(&private_key).unwrap().to_view_key().to_string();
}

#[flutter_rust_bridge::frb(sync)]
pub fn sign(message_bytes: Vec<u8>, private_key: String) -> String {
    let pk = PrivateKey::from_string(&private_key).unwrap();
    return pk.sign(&message_bytes).to_string();
}

but can not transfer, because, inside transfer function, there are wasm_bindgen::JsValue call ex:


 let (transfer_type, inputs) = match transfer_type {
            "private" | "transfer_private" | "transferPrivate" => {
                if amount_record.is_none() {
                    return Err("Amount record must be provided for private transfers".to_string());
                }
                let inputs = Array::new_with_length(3);
                inputs.set(0u32, wasm_bindgen::JsValue::from_str(&amount_record.unwrap().to_string()));
                inputs.set(1u32, wasm_bindgen::JsValue::from_str(recipient));
                inputs.set(2u32, wasm_bindgen::JsValue::from_str(&amount_microcredits.to_string().add("u64")));
                ("transfer_private", inputs)
            }

that make impossible to use with flutter_rust_bridge

gtmickey avatar Apr 08 '24 09:04 gtmickey

Currently the SDK functions rely on wasm-bindgen, so it requires Wasm. You have a couple options:

  1. You can use the snarkvm API, which exists as a pure-Rust crate.

  2. You can run Wasm code in Android and iOS, for example by using WebView.

    Many Android / iOS apps internally use JS / Wasm, this is a common practice.

Pauan avatar Apr 29 '24 00:04 Pauan

I have try use wasm on android, but there are an error: webview cannot load fetch local assets due to secure reason, so .wasm file cannot be load, could you please provide some test code or link to guide me how to do that?

gtmickey avatar Apr 30 '24 02:04 gtmickey

@gtmickey Have you tried enabling appcache?

https://stackoverflow.com/questions/56983215/how-to-access-local-files-of-android-app-inside-a-web-view

Pauan avatar May 01 '24 23:05 Pauan

I had try above solution but still error with URL scheme "file" is not supported., looks like need to find another way to do it.

gtmickey avatar May 10 '24 01:05 gtmickey

Unfortunately I'm not familiar with Android apps. I did find these things, which might help:

https://developer.android.com/develop/ui/views/layout/webapps/load-local-content

https://github.com/react-native-webview/react-native-webview/issues/746#issuecomment-812812275

Pauan avatar May 14 '24 03:05 Pauan