Skip to content

Commit

Permalink
Merge pull request #8 from THCLab/python
Browse files Browse the repository at this point in the history
Add python bindings
  • Loading branch information
chunningham authored Jan 25, 2021
2 parents 5430d12 + 89eb4f1 commit 3090be1
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 0 deletions.
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"

0 comments on commit 3090be1

Please sign in to comment.