Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python bindings #8

Merged
merged 3 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ buck-out/

#rust
rust/signer/target

# flapigen
ffi/python/libs
21 changes: 21 additions & 0 deletions ffi/python/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# #!/usr/bin/python3

from libs.libkeriox_wrapper import Wallet, Error as WalletError

wallet = Wallet.new_wallet("did", "pass")
error_ocuured = False
try :
Wallet.keri_incept_wallet(wallet, "did", "pass")
except WalletError:
error_ocuured = True

assert not error_ocuured

wallet = Wallet.change_pass(wallet, "did", "pass", "new_pass")

try :
Wallet.add_content(wallet, "did", "pass", "content")
except WalletError:
error_ocuured = True

assert error_ocuured
15 changes: 15 additions & 0 deletions ffi/rust/keriox_wrapper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Generated by Cargo
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk


#Added by cargo

/target
18 changes: 18 additions & 0 deletions ffi/rust/keriox_wrapper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "keriox_wrapper"
version = "0.1.0"
authors = ["Decentralized Identity Foundation"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
name = "keriox_wrapper"
crate-type = ["cdylib", "staticlib"]

[dependencies]
jolocom_native_utils = { path = "../../../rust/jolocom_native_utils" }
cpython = { version = "0.3", features = ["extension-module"] }

[build-dependencies]
flapigen = "0.6.0-pre7"
19 changes: 19 additions & 0 deletions ffi/rust/keriox_wrapper/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::{env, path::Path};

use flapigen::{LanguageConfig, PythonConfig};

fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
flapigen_expand(
Path::new("src/glue.rs.in"),
&Path::new(&out_dir).join("glue.rs"),
);
}

fn flapigen_expand(from: &Path, out: &Path) {
println!("Run flapigen_expand");
let python_cfg = PythonConfig::new("libkeriox_wrapper".to_owned());
let flapigen =
flapigen::Generator::new(LanguageConfig::PythonConfig(python_cfg)).rustfmt_bindings(true);
flapigen.expand("libkeriox_wrapper", from, out);
}
109 changes: 109 additions & 0 deletions ffi/rust/keriox_wrapper/src/glue.rs.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use std::fmt;
use jolocom_native_utils::{wallet};
use std::convert::TryInto;

#[derive(Debug, Clone, Copy)]
pub struct WalletError {}

impl WalletError {
pub fn new() -> WalletError {
WalletError {}
}
}

impl fmt::Display for WalletError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Wallet Error")
}
}

impl std::error::Error for WalletError {}

pub struct Wallet {}
impl Wallet {
fn new_wallet(id: String, pass: String) -> Result<String, WalletError> {
wallet::new_wallet(&id, &pass).map_err(|_| WalletError::new())
}

fn keri_incept_wallet(ew: String, id: String, pass: String) -> Result<String, WalletError> {
wallet::incept_wallet(&ew, &id, &pass).map_err(|_| WalletError::new())
}

fn keri_incept_wallet_from_keys(live_keys: String, pre_rotated_keys: String, pass: String) -> Result<String, WalletError> {
wallet::incept_populated_wallet(&live_keys, &pre_rotated_keys, &pass).map_err(|_| WalletError::new())
}

fn change_pass(ew: String, id: String, old_pass: String, new_pass: String) -> Result<String, WalletError> {
wallet::change_pass(&ew, &id, &old_pass, &new_pass).map_err(|_| WalletError::new())
}

fn change_id(ew: String, id: String, new_id: String, pass: String) -> Result<String, WalletError> {
wallet::change_id(&ew, &id, &new_id, &pass).map_err(|_| WalletError::new())
}

fn new_key(ew: String, id: String, pass: String, key_type: String, controller: String) -> Result<String, WalletError> {
wallet::new_key(&ew, &id, &pass, &key_type, if controller.len() > 0 {Some(vec![controller])} else { None }).map_err(|_| WalletError::new())
}

fn add_content(ew: String, id: String, pass: String, content: String) -> Result<String, WalletError> {
wallet::add_content(&ew, &id, &pass, &content).map_err(|_| WalletError::new())
}

fn set_key_controller(ew: String, id: String, pass: String, key_ref: String, controller: String) -> Result<String, WalletError> {
wallet::set_key_controller(&ew, &id, &pass, &key_ref, &controller).map_err(|_| WalletError::new())
}

fn get_key(ew: String, id: String, pass: String, key_ref: String) -> Result<String, WalletError> {
wallet::get_key(&ew, &id, &pass, &key_ref).map_err(|_| WalletError::new())
}

fn get_key_by_controller(ew: String, id: String, pass: String, controller: String) -> Result<String, WalletError> {
wallet::get_key_by_controller(&ew, &id, &pass, &controller).map_err(|_| WalletError::new())
}

fn get_keys(ew: String, id: String, pass: String) -> Result<String, WalletError> {
wallet::get_keys(&ew, &id, &pass).map_err(|_| WalletError::new())
}

fn sign_by_controller(ew: String, id: String, pass: String, controller: String, data: String) -> Result<String, WalletError> {
wallet::sign_by_controller(&ew, &id, &pass, &controller, &data).map_err(|_| WalletError::new())
}

fn jc_verify(key: String, key_type: String, data: String, signature: String) -> Result<bool, WalletError> {
wallet::verify(&key, &key_type, &data, &signature).map_err(|_| WalletError::new())
}

fn jc_encrypt(key: String, key_type: String, data: String, aad: String) -> Result<String, WalletError> {
wallet::encrypt(&key, &key_type, &data, &aad).map_err(|_| WalletError::new())
}

fn jc_decrypt(ew: String, id: String, pass: String, key_ref: String, data: String, aad: String) -> Result<String, WalletError> {
wallet::decrypt_by_controller(&ew, &id, &pass, &key_ref, &data, &aad).map_err(|_| WalletError::new())
}

fn get_random(len: u32) -> Result<String, WalletError> {
wallet::get_random_b64(len.try_into().unwrap()).map_err(|_| WalletError::new())
}

}

foreign_class!(
class Wallet {
fn Wallet::new_wallet(id: String, pass: String) -> Result<String, WalletError>;
fn Wallet::keri_incept_wallet(ew: String, id: String, pass: String) -> Result<String, WalletError>;
fn Wallet::keri_incept_wallet_from_keys(live_keys: String, pre_rotated_keys: String, pass: String) -> Result<String, WalletError>;
fn Wallet::change_pass(ew: String, id: String, old_pass: String, new_pass: String) -> Result<String, WalletError>;
fn Wallet::change_id(ew: String, id: String, new_id: String, pass: String) -> Result<String, WalletError>;
fn Wallet::new_key(ew: String, id: String, pass: String, key_type: String, controller: String) -> Result<String, WalletError>;
fn Wallet::add_content(ew: String, id: String, pass: String, content: String) -> Result<String, WalletError>;
fn Wallet::set_key_controller(ew: String, id: String, pass: String, key_ref: String, controller: String) -> Result<String, WalletError>;
fn Wallet::get_key(ew: String, id: String, pass: String, key_ref: String) -> Result<String, WalletError>;
fn Wallet::get_key_by_controller(ew: String, id: String, pass: String, controller: String) -> Result<String, WalletError>;
fn Wallet::get_keys(ew: String, id: String, pass: String) -> Result<String, WalletError>;
fn Wallet::sign_by_controller(ew: String, id: String, pass: String, controller: String, data: String) -> Result<String, WalletError>;
fn Wallet::jc_verify(key: String, key_type: String, data: String, signature: String) -> Result<bool, WalletError>;
fn Wallet::jc_encrypt(key: String, key_type: String, data: String, aad: String) -> Result<String, WalletError>;
fn Wallet::jc_decrypt(ew: String, id: String, pass: String, key_ref: String, data: String, aad: String) -> Result<String, WalletError>;
fn Wallet::get_random(len: u32) -> Result<String, WalletError>;
}
);
3 changes: 3 additions & 0 deletions ffi/rust/keriox_wrapper/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![allow(unused)]

include!(concat!(env!("OUT_DIR"), "/glue.rs"));
9 changes: 9 additions & 0 deletions scripts/build-python.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -ex
source ./variables.sh

cd $BASE_DIR/ffi/rust/keriox_wrapper/
cargo build --release

cp "./target/release/lib${LIB_NAME}.so" "../../python/libs/lib${LIB_NAME}.so"