ProvableHQ / sdk

A Software Development Kit (SDK) for Zero-Knowledge Transactions
https://provable.tools
GNU General Public License v3.0
584 stars 468 forks source link

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

Closed gtmickey closed 4 months ago

gtmickey commented 5 months ago

🚀 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

Pauan commented 4 months ago

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.

gtmickey commented 4 months ago

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?

Pauan commented 4 months ago

@gtmickey Have you tried enabling appcache?

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

gtmickey commented 4 months ago

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.

Pauan commented 4 months ago

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