Skip to content

Commit

Permalink
Merge branch 'master' into fix_update_com_method
Browse files Browse the repository at this point in the history
  • Loading branch information
jovfer authored Mar 18, 2019
2 parents 2cde89b + af056f1 commit 7755bce
Show file tree
Hide file tree
Showing 31 changed files with 5,663 additions and 4,645 deletions.
4 changes: 2 additions & 2 deletions ci/indy-pool.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ RUN echo "deb https://repo.sovrin.org/deb xenial $indy_stream" >> /etc/apt/sourc

RUN useradd -ms /bin/bash -u $uid indy

ARG indy_plenum_ver=1.6.705
ARG indy_plenum_ver=1.6.726
ARG indy_anoncreds_ver=1.0.32
ARG indy_node_ver=1.6.839
ARG indy_node_ver=1.6.862
ARG python3_indy_crypto_ver=0.4.5
ARG indy_crypto_ver=0.4.5

Expand Down
137 changes: 137 additions & 0 deletions cli/src/commands/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,81 @@ pub mod auth_rule_command {
}
}

pub mod get_auth_rule_command {
use super::*;

command!(CommandMetadata::build("get-auth-rule", r#"Send GET_AUTH_RULE request to get authentication rules for ledger transactions.
Note: Either none or all parameters must be specified (`old_value` can be skipped for `ADD` action)."#)
.add_required_param("txn_type", "Ledger transaction alias or associated value.")
.add_required_param("action", "Type of action for. One of: ADD, EDIT")
.add_required_param("field", "Transaction field")
.add_optional_param("old_value", "Old value of field, which can be changed to a new_value (mandatory for EDIT action)")
.add_required_param("new_value", "New value that can be used to fill the field")
.add_example(r#"ledger get-auth-rule txn_type=NYM action=ADD field=role new_value=101"#)
.add_example(r#"ledger get-auth-rule txn_type=NYM action=EDIT field=role old_value=101 new_value=0"#)
.add_example(r#"ledger get-auth-rule"#)
.finalize()
);

fn execute(ctx: &CommandContext, params: &CommandParams) -> Result<(), ()> {
trace!("execute >> ctx {:?} params {:?}", ctx, params);

let (pool_handle, pool_name) = ensure_connected_pool(&ctx)?;
let (_, wallet_name) = ensure_opened_wallet(&ctx)?;
let submitter_did = get_active_did(&ctx);

let auth_type = get_opt_str_param("txn_type", params).map_err(error_err!())?;
let auth_action = get_opt_str_param("action", params).map_err(error_err!())?;
let field = get_opt_str_param("field", params).map_err(error_err!())?;
let old_value = get_opt_str_param("old_value", params).map_err(error_err!())?;
let new_value = get_opt_str_param("new_value", params).map_err(error_err!())?;

let request = Ledger::build_get_auth_rule_request(submitter_did.as_ref().map(String::as_str), auth_type, auth_action, field, old_value, new_value)
.map_err(|err| handle_indy_error(err, None, None, None))?;

let response_json = Ledger::submit_request(pool_handle, &request)
.map_err(|err| handle_indy_error(err, submitter_did.as_ref().map(String::as_str), Some(&pool_name), Some(&wallet_name)))?;

let response: Response<serde_json::Value> = serde_json::from_str::<Response<serde_json::Value>>(&response_json)
.map_err(|err| println_err!("Invalid data has been received: {:?}", err))?;

let result = handle_transaction_response(response)?;

let rules = match result["data"].as_object() {
Some(r) => r,
None => return Err(println_err!("Invalid data has been received"))
};

let constraints = rules
.iter()
.map(|(constraint_id, constraint)| {
let parts: Vec<&str> = constraint_id.split("--").collect();

json!({
"auth_type": get_txn_title(&serde_json::Value::String(parts.get(1).cloned().unwrap_or("-").to_string())),
"auth_action": parts.get(0),
"field": parts.get(2),
"old_value": parts.get(3),
"new_value": parts.get(4),
"constraint": ::serde_json::to_string_pretty(&constraint).unwrap(),
})
})
.collect::<Vec<serde_json::Value>>();

let res = print_list_table(&constraints,
&vec![("auth_type", "Type"),
("auth_action", "Action"),
("field", "Field"),
("old_value", "Old Value"),
("new_value", "New Value"),
("constraint", "Constraint")],
"There are no rules set");

trace!("execute << {:?}", res);
Ok(res)
}
}

pub fn set_request_fees(request: &mut String, wallet_handle: i32, submitter_did: Option<&str>, fees_inputs: &Option<Vec<&str>>, fees_outputs: &Option<Vec<String>>, extra: Option<&str>) -> Result<Option<String>, ()> {
let mut payment_method: Option<String> = None;
if let &Some(ref inputs) = fees_inputs {
Expand Down Expand Up @@ -3575,6 +3650,10 @@ pub mod tests {
mod auth_rule {
use super::*;

const AUTH_TYPE: &str = "NYM";
const AUTH_ACTION: &str = "ADD";
const FIELD: &str = "role";
const NEW_VALUE: &str = "101";
const ROLE_CONSTRAINT: &str = r#"{
"sig_count": 1,
"metadata": {},
Expand All @@ -3599,6 +3678,64 @@ pub mod tests {
}
tear_down_with_wallet_and_pool(&ctx);
}

#[test]
pub fn get_auth_rule_works_for_one_constraint() {
let ctx = setup_with_wallet_and_pool();
use_trustee(&ctx);
{
let cmd = auth_rule_command::new();
let mut params = CommandParams::new();
params.insert("txn_type", AUTH_TYPE.to_string());
params.insert("action", AUTH_ACTION.to_string());
params.insert("field", FIELD.to_string());
params.insert("new_value", NEW_VALUE.to_string());
params.insert("constraint", ROLE_CONSTRAINT.to_string());
cmd.execute(&ctx, &params).unwrap();
}

{
let cmd = get_auth_rule_command::new();
let mut params = CommandParams::new();
params.insert("txn_type", AUTH_TYPE.to_string());
params.insert("action", AUTH_ACTION.to_string());
params.insert("field", FIELD.to_string());
params.insert("new_value", NEW_VALUE.to_string());
cmd.execute(&ctx, &params).unwrap();
}

tear_down_with_wallet_and_pool(&ctx);
}

#[test]
pub fn get_auth_rule_works_for_get_all() {
let ctx = setup_with_wallet_and_pool();

{
let cmd = get_auth_rule_command::new();
let params = CommandParams::new();
cmd.execute(&ctx, &params).unwrap();
}

tear_down_with_wallet_and_pool(&ctx);
}

#[test]
pub fn get_auth_rule_works_for_no_constraint() {
let ctx = setup_with_wallet_and_pool();
use_trustee(&ctx);
{
let cmd = get_auth_rule_command::new();
let mut params = CommandParams::new();
params.insert("txn_type", AUTH_TYPE.to_string());
params.insert("action", AUTH_ACTION.to_string());
params.insert("field", "WRONG_FIELD".to_string());
params.insert("new_value", "WRONG_VALUE".to_string());
cmd.execute(&ctx, &params).unwrap_err();
}

tear_down_with_wallet_and_pool(&ctx);
}
}

fn create_new_did(ctx: &CommandContext) -> (String, String) {
Expand Down
10 changes: 10 additions & 0 deletions cli/src/libindy/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,14 @@ impl Ledger {
ledger::build_auth_rule_request(submitter_did, txn_type, action, field,
old_value, new_value, constraint).wait()
}

pub fn build_get_auth_rule_request(submitter_did: Option<&str>,
auth_type: Option<&str>,
auth_action: Option<&str>,
field: Option<&str>,
old_value: Option<&str>,
new_value: Option<&str>, ) -> Result<String, IndyError> {
ledger::build_get_auth_rule_request(submitter_did, auth_type, auth_action, field,
old_value, new_value).wait()
}
}
1 change: 1 addition & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ fn build_executor() -> CommandExecutor {
.add_command(ledger::verify_payment_receipt_command::new())
.add_command(ledger::sign_multi_command::new())
.add_command(ledger::auth_rule_command::new())
.add_command(ledger::get_auth_rule_command::new())
.finalize_group()
.add_group(payment_address::group::new())
.add_command(payment_address::create_command::new())
Expand Down
6 changes: 6 additions & 0 deletions docs/design/001-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ Send AUTH_RULE transaction
ledger auth-rule txn_type=<txn type> action=<add or edit> field=<txn field> [old_value=<value>] new_value=<new_value> constraint=<{constraint json}>
```

#### GET_AUTH_RULE transaction
Send GET_AUTH_RULE transaction
```
ledger get-auth-rule [txn_type=<txn type>] [action=<ADD or EDIT>] [field=<txn field>] [old_value=<value>] [new_value=<new_value>]
```

#### GET_PAYMENT_SOURCES transaction
Send GET_PAYMENT_SOURCES transaction
```
Expand Down
35 changes: 35 additions & 0 deletions libindy/include/indy_ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,41 @@ extern "C" {
const char* request_json)
);

/// Builds a GET_AUTH_RULE request. Request to get authentication rules for a ledger transaction.
///
/// NOTE: Either none or all transaction related parameters must be specified (`old_value` can be skipped for `ADD` action).
/// * none - to get all authentication rules for all ledger transactions
/// * all - to get authentication rules for specific action (`old_value` can be skipped for `ADD` action)
///
/// #Params
/// command_handle: command handle to map callback to caller context.
/// submitter_did: (Optional) DID of the read request sender.
/// txn_type: (Optional) target ledger transaction alias or associated value.
/// action: (Optional) target action type. Can be either "ADD" or "EDIT".
/// field: (Optional) target transaction field.
/// old_value: (Optional) old value of field, which can be changed to a new_value (must be specified for EDIT action).
/// new_value: (Optional) new value that can be used to fill the field.
///
/// cb: Callback that takes command result as parameter.
///
/// #Returns
/// Request result as json.
///
/// #Errors
/// Common*
extern indy_error_t indy_build_get_auth_rule_request(indy_handle_t command_handle,
const char * submitter_did,
const char * txn_type,
const char * action,
const char * field,
const char * old_value,
const char * new_value,

void (*cb)(indy_handle_t command_handle_,
indy_error_t err,
const char* request_json)
);

#ifdef __cplusplus
}
#endif
Expand Down
72 changes: 72 additions & 0 deletions libindy/src/api/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1923,3 +1923,75 @@ pub extern fn indy_build_auth_rule_request(command_handle: CommandHandle,

res
}

/// Builds a GET_AUTH_RULE request. Request to get authentication rules for ledger transactions.
///
/// NOTE: Either none or all transaction related parameters must be specified (`old_value` can be skipped for `ADD` action).
/// * none - to get all authentication rules for all ledger transactions
/// * all - to get authentication rules for specific action (`old_value` can be skipped for `ADD` action)
///
/// #Params
/// command_handle: command handle to map callback to caller context.
/// submitter_did: (Optional) DID of the read request sender.
/// txn_type: (Optional) target ledger transaction alias or associated value.
/// action: (Optional) target action type. Can be either "ADD" or "EDIT".
/// field: (Optional) target transaction field.
/// old_value: (Optional) old value of field, which can be changed to a new_value (must be specified for EDIT action).
/// new_value: (Optional) new value that can be used to fill the field.
///
/// cb: Callback that takes command result as parameter.
///
/// #Returns
/// Request result as json.
///
/// #Errors
/// Common*
#[no_mangle]
pub extern fn indy_build_get_auth_rule_request(command_handle: CommandHandle,
submitter_did: *const c_char,
txn_type: *const c_char,
action: *const c_char,
field: *const c_char,
old_value: *const c_char,
new_value: *const c_char,
cb: Option<extern fn(command_handle_: CommandHandle,
err: ErrorCode,
request_json: *const c_char)>) -> ErrorCode {
trace!("indy_build_get_auth_rule_request: >>> submitter_did: {:?}, txn_type: {:?}, action: {:?}, field: {:?}, \
old_value: {:?}, new_value: {:?}",
submitter_did, txn_type, action, field, old_value, new_value);

check_useful_opt_c_str!(submitter_did, ErrorCode::CommonInvalidParam2);
check_useful_opt_c_str!(txn_type, ErrorCode::CommonInvalidParam3);
check_useful_opt_c_str!(action, ErrorCode::CommonInvalidParam4);
check_useful_opt_c_str!(field, ErrorCode::CommonInvalidParam5);
check_useful_opt_c_str!(old_value, ErrorCode::CommonInvalidParam6);
check_useful_opt_c_str!(new_value, ErrorCode::CommonInvalidParam7);
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam8);

trace!("indy_build_get_auth_rule_request: entities >>> submitter_did: {:?}, txn_type: {:?}, action: {:?}, field: {:?}, \
old_value: {:?}, new_value: {:?}",
submitter_did, txn_type, action, field, old_value, new_value);

let result = CommandExecutor::instance()
.send(Command::Ledger(LedgerCommand::BuildGetAuthRuleRequest(
submitter_did,
txn_type,
action,
field,
old_value,
new_value,
Box::new(move |result| {
let (err, request_json) = prepare_result_1!(result, String::new());
trace!("indy_build_get_auth_rule_request: request_json: {:?}", request_json);
let request_json = ctypes::string_to_cstring(request_json);
cb(command_handle, err, request_json.as_ptr())
})
)));

let res = prepare_result!(result);

trace!("indy_build_get_auth_rule_request: <<< res: {:?}", res);

res
}
36 changes: 36 additions & 0 deletions libindy/src/commands/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ pub enum LedgerCommand {
String, // new value
String, // constraint
Box<Fn(IndyResult<String>) + Send>),
BuildGetAuthRuleRequest(
Option<String>, // submitter did
Option<String>, // auth type
Option<String>, // auth action
Option<String>, // field
Option<String>, // old value
Option<String>, // new value
Box<Fn(IndyResult<String>) + Send>),
}

pub struct LedgerCommandExecutor {
Expand Down Expand Up @@ -375,6 +383,15 @@ impl LedgerCommandExecutor {
info!(target: "ledger_command_executor", "BuildAuthRuleRequest command received");
cb(self.build_auth_rule_request(&submitter_did, &txn_type, &action, &field, old_value.as_ref().map(String::as_str), &new_value, &constraint));
}
LedgerCommand::BuildGetAuthRuleRequest(submitter_did, txn_type, action, field, old_value, new_value, cb) => {
info!(target: "ledger_command_executor", "BuildGetAuthRuleRequest command received");
cb(self.build_get_auth_rule_request(submitter_did.as_ref().map(String::as_str),
txn_type.as_ref().map(String::as_str),
action.as_ref().map(String::as_str),
field.as_ref().map(String::as_str),
old_value.as_ref().map(String::as_str),
new_value.as_ref().map(String::as_str)));
}
};
}

Expand Down Expand Up @@ -924,6 +941,25 @@ impl LedgerCommandExecutor {
Ok(res)
}

fn build_get_auth_rule_request(&self,
submitter_did: Option<&str>,
txn_type: Option<&str>,
action: Option<&str>,
field: Option<&str>,
old_value: Option<&str>,
new_value: Option<&str>) -> IndyResult<String> {
debug!("build_get_auth_rule_request >>> submitter_did: {:?}, auth_type: {:?}, auth_action: {:?}, field: {:?}, \
old_value: {:?}, new_value: {:?}", submitter_did, txn_type, action, field, old_value, new_value);

self.validate_opt_did(submitter_did)?;

let res = self.ledger_service.build_get_auth_rule_request(submitter_did, txn_type, action, field, old_value, new_value)?;

debug!("build_get_auth_rule_request <<< res: {:?}", res);

Ok(res)
}

fn validate_opt_did(&self, did: Option<&str>) -> IndyResult<()> {
match did {
Some(did) => Ok(self.crypto_service.validate_did(did)?),
Expand Down
Loading

0 comments on commit 7755bce

Please sign in to comment.