Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Feature/ledger commands #15

Merged
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ local_nodes_pool = []
[dependencies]
int_traits = { version = "0.1.1", optional = true }
env_logger = "0.4.2"
hex = "0.2.0"
libc = "0.2.21"
log = "0.3.7"
openssl = { version = "0.9.11", optional = true }
Expand Down
149 changes: 81 additions & 68 deletions src/api/ledger.rs

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/api/signus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ use self::libc::c_char;
/// command_handle: command handle to map callback to user context.
/// did_json: Identity information as json. Example:
/// {
/// "did": string, (optional; if not provided then the first 16 bit of the verkey will be used
/// as a new DID; if provided, then keys will be replaced - key rotation use case)
/// "did": string, (optional;
/// if not provided and cid param is false then the first 16 bit of the verkey will be used as a new DID;
/// if not provided and cid is true then the full verkey will be used as a new DID;
/// if provided, then keys will be replaced - key rotation use case)
/// "seed": string, (optional; if not provide then a random one will be created)
/// "crypto_type": string, (optional; if not set then ed25519 curve is used;
/// currently only 'ed25519' value is supported for this field)
/// "cid": string, (optional; if not set then false is used;)
/// }
/// cb: Callback that takes command result as parameter.
///
Expand Down
142 changes: 98 additions & 44 deletions src/commands/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use services::pool::PoolService;
use services::signus::SignusService;
use services::signus::types::MyDid;
use services::wallet::WalletService;
use services::ledger::LedgerService;

use utils::json::JsonDecodable;

Expand Down Expand Up @@ -38,17 +39,17 @@ pub enum LedgerCommand {
BuildNymRequest(
String, // submitter did
String, // target did
String, // verkey
String, // xref
String, // data
String, // role
Option<String>, // verkey
Option<String>, // xref
Option<String>, // data
Option<String>, // role
Box<Fn(Result<String, SovrinError>) + Send>),
BuildAttribRequest(
String, // submitter did
String, // target did
String, // hash
String, // raw
String, // enc
Option<String>, // hash
Option<String>, // raw
Option<String>, // enc
Box<Fn(Result<String, SovrinError>) + Send>),
BuildGetAttribRequest(
String, // submitter did
Expand All @@ -65,16 +66,20 @@ pub enum LedgerCommand {
Box<Fn(Result<String, SovrinError>) + Send>),
BuildGetSchemaRequest(
String, // submitter did
String, // dest
String, // data
Box<Fn(Result<String, SovrinError>) + Send>),
BuildClaimDefRequest(
String, // submitter did
String, // xref
i32, // xref
String, // signature_type
String, // data
Box<Fn(Result<String, SovrinError>) + Send>),
BuildGetClaimDefRequest(
String, // submitter did
String, // xref
i32, // xref
String, // signature_type
String, // origin
Box<Fn(Result<String, SovrinError>) + Send>),
BuildNodeRequest(
String, // submitter did
Expand All @@ -88,6 +93,7 @@ pub struct LedgerCommandExecutor {
pool_service: Rc<PoolService>,
signus_service: Rc<SignusService>,
wallet_service: Rc<WalletService>,
ledger_service: Rc<LedgerService>,

send_callbacks: RefCell<HashMap<i32, Box<Fn(Result<String, SovrinError>)>>>,
}
Expand All @@ -96,12 +102,14 @@ impl LedgerCommandExecutor {
pub fn new(anoncreds_service: Rc<AnoncredsService>,
pool_service: Rc<PoolService>,
signus_service: Rc<SignusService>,
wallet_service: Rc<WalletService>) -> LedgerCommandExecutor {
wallet_service: Rc<WalletService>,
ledger_service: Rc<LedgerService>) -> LedgerCommandExecutor {
LedgerCommandExecutor {
anoncreds_service: anoncreds_service,
pool_service: pool_service,
signus_service: signus_service,
wallet_service: wallet_service,
ledger_service: ledger_service,
send_callbacks: RefCell::new(HashMap::new()),
}
}
Expand All @@ -128,11 +136,20 @@ impl LedgerCommandExecutor {
}
LedgerCommand::BuildNymRequest(submitter_did, target_did, verkey, xref, data, role, cb) => {
info!(target: "ledger_command_executor", "BuildNymRequest command received");
self.build_nym_request(&submitter_did, &target_did, &verkey, &xref, &data, &role, cb);
self.build_nym_request(&submitter_did, &target_did,
verkey.as_ref().map(String::as_str),
xref.as_ref().map(String::as_str),
data.as_ref().map(String::as_str),
role.as_ref().map(String::as_str),
cb);
}
LedgerCommand::BuildAttribRequest(submitter_did, target_did, hash, raw, enc, cb) => {
info!(target: "ledger_command_executor", "BuildAttribRequest command received");
self.build_attrib_request(&submitter_did, &target_did, &hash, &raw, &enc, cb);
self.build_attrib_request(&submitter_did, &target_did,
hash.as_ref().map(String::as_str),
raw.as_ref().map(String::as_str),
enc.as_ref().map(String::as_str),
cb);
}
LedgerCommand::BuildGetAttribRequest(submitter_did, target_did, data, cb) => {
info!(target: "ledger_command_executor", "BuildGetAttribRequest command received");
Expand All @@ -146,17 +163,17 @@ impl LedgerCommandExecutor {
info!(target: "ledger_command_executor", "BuildSchemaRequest command received");
self.build_schema_request(&submitter_did, &data, cb);
}
LedgerCommand::BuildGetSchemaRequest(submitter_did, data, cb) => {
LedgerCommand::BuildGetSchemaRequest(submitter_did, dest, data, cb) => {
info!(target: "ledger_command_executor", "BuildGetSchemaRequest command received");
self.build_get_schema_request(&submitter_did, &data, cb);
self.build_get_schema_request(&submitter_did, &dest, &data, cb);
}
LedgerCommand::BuildClaimDefRequest(submitter_did, xref, data, cb) => {
LedgerCommand::BuildClaimDefRequest(submitter_did, xref, signature_type, data, cb) => {
info!(target: "ledger_command_executor", "BuildClaimDefRequest command received");
self.build_issuer_key_request(&submitter_did, &xref, &data, cb);
self.build_claim_def_request(&submitter_did, xref, &signature_type, &data, cb);
}
LedgerCommand::BuildGetClaimDefRequest(submitter_did, xref, cb) => {
LedgerCommand::BuildGetClaimDefRequest(submitter_did, xref, signature_type, origin, cb) => {
info!(target: "ledger_command_executor", "BuildGetClaimDefRequest command received");
self.build_get_issuer_key_request(&submitter_did, &xref, cb);
self.build_get_claim_def_request(&submitter_did, xref, &signature_type, &origin, cb);
}
LedgerCommand::BuildNodeRequest(submitter_did, target_did, data, cb) => {
info!(target: "ledger_command_executor", "BuildNodeRequest command received");
Expand Down Expand Up @@ -216,79 +233,116 @@ impl LedgerCommandExecutor {
submitter_did: &str,
target_did: &str,
cb: Box<Fn(Result<String, SovrinError>) + Send>) {
cb(Ok("".to_string()));
cb(self.ledger_service.build_get_ddo_request(submitter_did, target_did)
.map_err(|err| SovrinError::CommonError(err)))
}

fn build_nym_request(&self,
submitter_did: &str,
target_did: &str,
verkey: &str,
xref: &str,
data: &str,
role: &str,
verkey: Option<&str>,
xref: Option<&str>,
data: Option<&str>,
role: Option<&str>,
cb: Box<Fn(Result<String, SovrinError>) + Send>) {
cb(Ok("".to_string()));
cb(self.ledger_service.build_nym_request(submitter_did,
target_did,
verkey,
xref,
data,
role
).map_err(|err| SovrinError::CommonError(err)))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving of сlosing parenthesis of function params list to next line is bad code style. Please fix it.

}

fn build_attrib_request(&self,
submitter_did: &str,
target_did: &str,
hash: &str,
raw: &str,
enc: &str,
hash: Option<&str>,
raw: Option<&str>,
enc: Option<&str>,
cb: Box<Fn(Result<String, SovrinError>) + Send>) {
cb(Ok("".to_string()));
cb(self.ledger_service.build_attrib_request(submitter_did,
target_did,
hash,
raw,
enc
).map_err(|err| SovrinError::CommonError(err)))
}

fn build_get_attrib_request(&self,
submitter_did: &str,
target_did: &str,
data: &str,
cb: Box<Fn(Result<String, SovrinError>) + Send>) {
cb(Ok("".to_string()));
cb(self.ledger_service.build_get_attrib_request(submitter_did,
target_did,
data
).map_err(|err| SovrinError::CommonError(err)))
}

fn build_get_nym_request(&self,
submitter_did: &str,
target_did: &str,
cb: Box<Fn(Result<String, SovrinError>) + Send>) {
cb(Ok("".to_string()));
cb(self.ledger_service.build_get_nym_request(submitter_did,
target_did
).map_err(|err| SovrinError::CommonError(err)))
}

fn build_schema_request(&self,
submitter_did: &str,
data: &str,
cb: Box<Fn(Result<String, SovrinError>) + Send>) {
cb(Ok("".to_string()));
cb(self.ledger_service.build_schema_request(submitter_did,
data
).map_err(|err| SovrinError::CommonError(err)))
}

fn build_get_schema_request(&self,
submitter_did: &str,
dest: &str,
data: &str,
cb: Box<Fn(Result<String, SovrinError>) + Send>) {
cb(Ok("".to_string()));
cb(self.ledger_service.build_get_schema_request(submitter_did,
dest,
data
).map_err(|err| SovrinError::CommonError(err)))
}

fn build_issuer_key_request(&self,
submitter_did: &str,
xref: &str,
data: &str,
cb: Box<Fn(Result<String, SovrinError>) + Send>) {
cb(Ok("".to_string()));
fn build_claim_def_request(&self,
submitter_did: &str,
xref: i32,
signature_type: &str,
data: &str,
cb: Box<Fn(Result<String, SovrinError>) + Send>) {
cb(self.ledger_service.build_claim_def_request(submitter_did,
xref,
signature_type,
data
).map_err(|err| SovrinError::CommonError(err)))
}

fn build_get_issuer_key_request(&self,
submitter_did: &str,
xref: &str,
cb: Box<Fn(Result<String, SovrinError>) + Send>) {
cb(Ok("".to_string()));
fn build_get_claim_def_request(&self,
submitter_did: &str,
xref: i32,
signature_type: &str,
origin: &str,
cb: Box<Fn(Result<String, SovrinError>) + Send>) {
cb(self.ledger_service.build_get_claim_def_request(submitter_did,
xref,
signature_type,
origin
).map_err(|err| SovrinError::CommonError(err)))
}

fn build_node_key_request(&self,
submitter_did: &str,
target_did: &str,
data: &str,
cb: Box<Fn(Result<String, SovrinError>) + Send>) {
cb(Ok("".to_string()));
cb(self.ledger_service.build_node_request(submitter_did,
target_did,
data
).map_err(|err| SovrinError::CommonError(err)))
}
}
4 changes: 3 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use services::anoncreds::AnoncredsService;
use services::pool::PoolService;
use services::wallet::WalletService;
use services::signus::SignusService;
use services::ledger::LedgerService;

use std::error::Error;
use std::sync::mpsc::{Sender, channel};
Expand Down Expand Up @@ -62,9 +63,10 @@ impl CommandExecutor {
let pool_service = Rc::new(PoolService::new());
let wallet_service = Rc::new(WalletService::new());
let signus_service = Rc::new(SignusService::new());
let ledger_service = Rc::new(LedgerService::new());

let anoncreds_command_executor = AnoncredsCommandExecutor::new(anoncreds_service.clone(), pool_service.clone(), wallet_service.clone());
let ledger_command_executor = LedgerCommandExecutor::new(anoncreds_service.clone(), pool_service.clone(), signus_service.clone(), wallet_service.clone());
let ledger_command_executor = LedgerCommandExecutor::new(anoncreds_service.clone(), pool_service.clone(), signus_service.clone(), wallet_service.clone(), ledger_service.clone());
let pool_command_executor = PoolCommandExecutor::new(pool_service.clone());
let signus_command_executor = SignusCommandExecutor::new(anoncreds_service.clone(), pool_service.clone(), wallet_service.clone(), signus_service.clone());
let wallet_command_executor = WalletCommandExecutor::new(wallet_service.clone());
Expand Down
2 changes: 1 addition & 1 deletion src/commands/signus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl SignusCommandExecutor {
let identity_info: MyIdentityInfo = MyIdentityInfo::from_json(identity_json)
.map_err(|_| CommonError::InvalidStructure(format!("Invalid identity json")))?;

let did_info = MyDidInfo::new(Some(did.to_string()), identity_info.seed, identity_info.crypto_type);
let did_info = MyDidInfo::new(Some(did.to_string()), identity_info.seed, identity_info.crypto_type, None);

let my_did = self.signus_service.create_my_did(&did_info)?;
let my_did_json = my_did.to_json()
Expand Down
8 changes: 8 additions & 0 deletions src/errors/ledger.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate serde_json;

use std::error;
use std::io;
use std::fmt;
Expand Down Expand Up @@ -58,6 +60,12 @@ impl ToErrorCode for LedgerError {
}
}

impl From<serde_json::Error> for LedgerError {
fn from(err: serde_json::Error) -> LedgerError {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Common mapping from json error to Ledger error looks wrong. Depend on situation it can be Common::InvalidParam or Common::InvalidState

LedgerError::CryptoError(CryptoError::InvalidStructure(err.to_string()))
}
}

#[cfg(test)]
mod tests {
// use super::*;
Expand Down
Loading