How to use safe_client_libs in python?


#1

I’m trying to login to the safe network using python.

I’ve compiled safe_authenticator using cargo build --release

Using the ctypes python module I can import the .so file into python:

authenticator = cdll.LoadLibrary("/path/to/libsafe_authenticator.so")

But I can’t call authenticator.login without the app crashing (segfault).

When I look at the input arguments for login I start having trouble.

The first two arguments are simple enough: secret_1 and secret_2 (presumably corresponding to ‘secret’ and ‘password’ in the UI?) which are just normal strings.

The third fourth and fifth arguments seem impossible to satisfy.

Third arg is type tokio_core::reactor::Handle which doesn’t seem to be exported

Fourth arg is type event_loop::CoreMsgTx also not exported

Fifth arg is type event::NetworkTx also not exported

I feel I must be lacking some crucial understanding, but searching the safe_browser javascript didn’t reveal anything useful to me.

Are these arguments possible to supply in python? Is it possible to login to the network using python? Am I even approaching this the right way?


#2

You can imagine our API and libraries as a stack or an “onion” with several layers: the underlying library, SAFE Core, doesn’t have any notion of apps, Authenticator, or a browser. It’s just an abstract way to talk to the network, putting and getting structures like Immutable Data or Mutable Data. On top of that library we have a higher-level interfaces: SAFE App, the API to build apps, and SAFE Authenticator, the API for the “app centre”, for dealing with the users’ accounts, etc. And on the higher level we have the FFI interface: the functions that we expose to the world from Rust, and these functions can be used from any language, including Python.

So the problem on your side is that you’re looking at the lower-level Rust API; you’d want to use this API if you want to write your own libraries and layers like safe_authenticator or safe_app on top of safe_core. Instead, if you want to just write apps in Python, you’ll want to use the safe_app FFI layer, which is currently documented here: https://docs.rs/safe_app/0.4.0/safe_app/ffi/index.html

Unfortunately though, we don’t provide any official Python bindings yet, so for now you’ll have to write your own or use an automatic generator. I’d suggest trying to look into the CFFI library in Python: this library allows you to import function and type definitions from C headers, which we automatically generate and provide in safe_client_libs (just look inside the auto-gen/c-include directory after you manually build the libs).

Please let me know if this explanation doesn’t make much sense to you or if you have further questions :slight_smile:


#3

Thanks very much for this explanation, it helps a lot.

To add some context to my initial question (ie not looking for a response):

My main motivation is to be able to use safe via a scripting / non-gui interface with login and permissions etc all managed automatically. This goes against the canonical ‘safe app’ way of doing things (ie everything approved by the user via the browser / authenticator and developers using apps as the gateway to the network - from the getting started doc “When the user approves the request…” is the part I want to automate).

I can understand the desire to wrap everything around the app concept, but I find the way this a pretty unempowering interface. For now it’s fine, I’ll just write my code in rust so I can still do things at the very lowest level, but I think maybe in the future cleanly exposing the lowest level network interfaces will be useful (despite the security implications for end-users).

I prefer to be able to set permissions and authentication in $HOME/.safe_config file and let my safe apps run in the background with no gui or input needed.

But all the same I’m having a blast working with alpha2 so thanks for all the work so far :mage:


#4

I too would like to be able to run scripts against an account. IoT stuff has so many methods, procedures etc that having something that I can script will make an easy path to integration in some cases.


#5

Nothing prevents you from doing that now, actually. You can create your own minimal headless Authenticator on top of the safe_authenticator lib: the SAFE Browser and its Authenticator plugin aren’t magical, they’re too just apps that use the SAFE Client Libs underneath. And the Authenticator API is exposed and documented too, so you might want to experiment with it and build a terminal/non-GUI Authenticator that approves every request that it gets :slight_smile:

So what I want to say is that it’s more a question of security/user interface rather than the technical ability. But yes, for now we don’t provide a native headless mode in the SAFE Browser, as @joshuef answered here.


#6

I’d like to be able to do this too. Much as I’m enjoying (re) learning JavaScript, interacting with SN using scripts and cli utilities opens up a whole new world. @cretz wrote a simple set of cli programs that talked directly to the old SAFE Launcher API, and they were a really helpful option. Similarly, when we had the cli for SAFE-VFS it was mind-blowing!


#7

Where are these located?


#8

Not sure, try user cretz on github, but they are for the old API, so not much use IMO.


#9

Thanks for the tip. I found these on github:


I know they’re outdated, but I’m just trying to find some examples on how to work directly with safe_client_libs via native rust in order to make some simple/basic command line programs. It seemed like safe_core/examples/self_authentication.rs is was going to be a good start but it gives me build errors.

I’m looking forward to the ~30 page guide from @polpolrene that was slated to be released soon…


#10

And @JPL :wink: I’m also looking forward to this.


#11

Examples are there mainly for this purpose: to help you understand the native Rust API better, and they should work without requiring any changes. However, if you want to run them on the real network (i.e. not mock-routing), you’ll have to provide your credentials through environment variables.

Could you please provide more details about the build errors you’ve encountered?


#12

My off-hand comment above was not a criticism, but rather a cry for help. Also, it wasn’t fair because the code example in the repo has been commented out and I was playing around trying to resurrect it in order to see if I could directly authenticate with alpha 2 using my credentials from the command line. (https://github.com/maidsafe/safe_client_libs/blob/master/safe_core/examples/self_authentication.rs)

The build errors look to be from api changes related to error messages being moved to private implementations and also from changes w.r.t. tokio connections. Example:

error[E0432]: unresolved import tokio_core::channel
–> src/main.rs:51:5
|
51 | use tokio_core::channel;
| ^^^^^^^^^^^^^^^^^^^ no channel in the root

There are plenty of other examples for me to play with, and I’ve moved on. In the future I’ll try to be much more specific if I run into any issues. I’m trying to conceptualize how all the pieces fit together and get some basic things working via text input rather than going the browser/javascript route. I’m not exactly sure where my entry point to the api should be, whether or not I should be dealing directly with safe_core, or sticking to safe_app. I’m very eager to learn more and will take all the advice you can offer. Any suggestions that you could give will help me get up to speed faster when trying to implement some terminal/CLI apps, but I also understand how busy you folks at MaidSafe are so don’t feel like you need to offer anything.


#13

@jlpell saw this and thought of you, for anyone who fancies writing command line apps using Rust:


#14

Thanks. I’ll consider this along with getopts and docopt.


#15

If it helps I have found clap (this lib uses that) is much simpler and more powerful than getopts or docopts.


#16

Ah sure, that’s why I am asking for details, wanting to help :slight_smile:

You’re right, some of the examples haven’t yet been ported from the previous version of the SAFE Client Libs API (the pre-authenticator one). Others can be a bit complicated but you can check e.g. client_stress_test and gen_invites for working examples.

You should also bear in mind that the Rust Client Libs API is built upon the foundation of futures (which quickly became idiomatic in Rust). So you might want to read more on the futures API and examples too.

Most of the operations go through the Client interface which is defined and implemented in safe_core. That way you can talk to the network directly; but it can be rather overwhelming if you want to go to the territory of SAFE Authenticator and apps. That’s why we have a multitude of helper functions in safe_app and safe_authenticator.

If you just want to create an app, you’ll need to go through the App interface, providing your credentials. It will connect to the network, create the event loop, etc., and then you can use the App.send method to obtain a Client instance and perform network operations. You can also find multiple usage examples in safe_app tests.

It’s unfortunate that currently Rust examples are scattered around numerous places. This is caused by the API itself being very unstable at the time. Now that it’s been stabilised, we intend to improve the documentation in the foreseeable future.

If you have any other questions or if my description wasn’t clear enough, please don’t hesitate to ask!


#17

I’m looking forward to the java bindings for the same reason. I moth balled my java fuse project as the rest API was being removed. I haven’t had much spare time to pursue this anyway, but I don’t think the java client libs are at a point where I could easily use them yet.

I am looking forward to working on this again though, especially when I may be out of contract in the summer.