How to get started using the Rust API


#1

Hello!

I’m trying to get started writing applications for the SAFE Network in Rust.

I’ve tried finding a minimal standalone example or tutorial somewhere to get me started, but I haven’t been able to find anything. It seems like pretty much all information about using the APIs are about how to create a web browser application in one way or another.

After playing around and trying to get things to work for a while I found the client_stress_test.rs file: https://github.com/maidsafe/safe_client_libs/blob/master/safe_app/examples/client_stress_test.rs

That file seems to have everything I need, but I’ve run into an issue with it.

The issue is that when I try to use the Authenticator::login() function, I get the following errors:

error: cannot find macro `wait_for_response!` in this scope
   --> C:\Users\Lukas\.cargo\registry\src\github.com-1ecc6299db9ec823\safe_authenticator-0.8.0\src\client.rs:188:27
    |
188 |             .and_then(|_| wait_for_response!(routing_rx, Response::PutMData, msg_id))
    |                           ^^^^^^^^^^^^^^^^^

error: cannot find macro `wait_for_response!` in this scope
   --> C:\Users\Lukas\.cargo\registry\src\github.com-1ecc6299db9ec823\safe_authenticator-0.8.0\src\client.rs:310:31
    |
310 |                 .and_then(|_| wait_for_response!(routing_rx, Response::GetMDataValue, msg_id))
    |                               ^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

error: Could not compile `safe_authenticator`.

Here is the minimal main.rs that produces the error:

extern crate safe_authenticator;
use safe_authenticator::Authenticator;

fn main() {
    let secret = "2hGBu03klEttptnzTvFYm";
    let password = "e5f0FT0Zu12f0azeu60n8";

    Authenticator::login(secret, password, || ());
}

And are the dependencies:

[dependencies]
safe_authenticator = "0.8.0"

Does anyone have any idea how I can solve this issue?

Thanks!


#2

Hi @lukas! Thanks for your interest in building Rust apps for SAFE :slight_smile:

The API package you need to use is safe_app: https://docs.rs/safe_app/0.8.0/safe_app/

Unfortunately there’s no extensive tutorials on Rust API usage just yet, but actually the most complicated part about it is the initialisation and the entry point. You need to send a request to the Authenticator and obtain app keys to be able to communicate with the SAFE Network (even with its mock version). After that, the API stays similar to what you can find in NodeJS: it’s all about Mutable Data, Immutable Data, and so forth (just with a more idiomatic feeling).

Give us some time and in the following post I or @marcin will show you a minimal Rust example that establishes a connection with the Network.


#3

Thank you for the overview! I’ll be looking forward to the minimal Rust example! :smiley:


#4

Been trying to get something to work the past couple of weeks.

This is what I have so far:

extern crate safe_app;

use safe_app::{
    App,
    ipc::{
        self,
        IpcMsg,
        req::{
            AppExchangeInfo,
            AuthReq,
            IpcReq,
            Permission,
        },
    },
};

use std::collections::{
    BTreeSet,
    HashMap,
};

fn main() {
    let app = AppExchangeInfo {
        id: "com.company.test".to_string(),
        scope: None,
        name: "safe_test".to_string(),
        vendor: "My Company".to_string(),
    };

    let mut permissions: BTreeSet<Permission> = BTreeSet::new();
    permissions.insert(Permission::Insert);

    let mut containers: HashMap<String, BTreeSet<Permission>> = HashMap::new();
    containers.insert(
        "my_company_data".to_string(),
        permissions,
    );

    let auth_req = AuthReq {
        app,
        app_container: false,
        containers,
    };

    let ipc_req = IpcReq::Auth(auth_req);

    let ipc_msg = IpcMsg::Req {
        req_id: ipc::gen_req_id(),
        req: ipc_req,
    };
}

At this point I think I need to get access to the authenticator somehow so I can send this message I’ve created to it.

I’ve been unable to find somewhere in the API where I can send this message. Does anyone know where I can send it?


#5

Me too! I would love to see a Rust getting started tutorial that authenticates then creates and updates mutable data.