Skip to content

Commit a29b84b

Browse files
author
Vyacheslav Gudkov
committed
* Remove boilerplate in crypto module
* Hygiene refactoring of wallet code * Avoid of creation wallet configuration files * Wallet API changes to allow wallet access in cluster Signed-off-by: Vyacheslav Gudkov <[email protected]>
1 parent a6e7769 commit a29b84b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1427
-1696
lines changed

libindy/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ path = "src/lib.rs"
2020
crate-type = ["staticlib","rlib", "cdylib"]
2121

2222
[features]
23-
default = ["bn_openssl", "box_sodium", "sealedbox_sodium", "base58_rust_base58", "xsalsa20_sodium", "chacha20poly1305_ietf_sodium", "pair_amcl", "hash_openssl", "local_nodes_pool", "revocation_tests", "pwhash_argon2i13_sodium", "hmacsha256_sodium"]
23+
default = ["bn_openssl", "box_sodium", "sealedbox_sodium", "base58_rust_base58", "base64_rust_base64", "xsalsa20_sodium", "chacha20poly1305_ietf_sodium", "pair_amcl", "hash_openssl", "local_nodes_pool", "revocation_tests", "pwhash_argon2i13_sodium", "hmacsha256_sodium", "memzero_sodium"]
2424
bn_openssl = ["openssl", "int_traits"]
2525
box_sodium = ["sodiumoxide"]
2626
sealedbox_sodium = ["sodiumoxide"]
2727
base58_rust_base58 = ["rust-base58"]
28+
base64_rust_base64 = ["base64"]
2829
xsalsa20_sodium = ["sodiumoxide"]
2930
chacha20poly1305_ietf_sodium = ["sodiumoxide"]
3031
pwhash_argon2i13_sodium = ["sodiumoxide"]
@@ -34,6 +35,7 @@ local_nodes_pool = []
3435
revocation_tests = []
3536
sodium_static = []
3637
hmacsha256_sodium = ["sodiumoxide"]
38+
memzero_sodium = ["sodiumoxide"]
3739

3840
[dependencies]
3941
indy-crypto = { version = "=0.4.1", optional = true }
@@ -52,7 +54,7 @@ rand = "0.3"
5254
rusqlite = "0.13.0"
5355
libsqlite3-sys = "0.9.1"
5456
rust-base58 = {version = "0.0.4", optional = true}
55-
base64 = "0.6.0"
57+
base64 = {version = "0.6.0", optional = true}
5658
serde = "1.0"
5759
serde_json = "1.0"
5860
serde_derive = "1.0"

libindy/src/api/mod.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,45 @@ pub enum ErrorCode
7070
// Caller passed invalid value as param 14 (null, invalid json and etc..)
7171
CommonInvalidParam14 = 116,
7272

73+
// Caller passed invalid value as param 15 (null, invalid json and etc..)
74+
CommonInvalidParam15 = 117,
75+
76+
// Caller passed invalid value as param 16 (null, invalid json and etc..)
77+
CommonInvalidParam16 = 118,
78+
79+
// Caller passed invalid value as param 17 (null, invalid json and etc..)
80+
CommonInvalidParam17 = 119,
81+
82+
// Caller passed invalid value as param 18 (null, invalid json and etc..)
83+
CommonInvalidParam18 = 120,
84+
85+
// Caller passed invalid value as param 19 (null, invalid json and etc..)
86+
CommonInvalidParam19 = 121,
87+
88+
// Caller passed invalid value as param 20 (null, invalid json and etc..)
89+
CommonInvalidParam20 = 122,
90+
91+
// Caller passed invalid value as param 21 (null, invalid json and etc..)
92+
CommonInvalidParam21 = 123,
93+
94+
// Caller passed invalid value as param 22 (null, invalid json and etc..)
95+
CommonInvalidParam22 = 124,
96+
97+
// Caller passed invalid value as param 23 (null, invalid json and etc..)
98+
CommonInvalidParam23 = 125,
99+
100+
// Caller passed invalid value as param 24 (null, invalid json and etc..)
101+
CommonInvalidParam24 = 126,
102+
103+
// Caller passed invalid value as param 25 (null, invalid json and etc..)
104+
CommonInvalidParam25 = 127,
105+
106+
// Caller passed invalid value as param 26 (null, invalid json and etc..)
107+
CommonInvalidParam26 = 128,
108+
109+
// Caller passed invalid value as param 27 (null, invalid json and etc..)
110+
CommonInvalidParam27 = 129,
111+
73112
// Wallet errors
74113
// Caller passed invalid wallet handle
75114
WalletInvalidHandle = 200,

libindy/src/api/wallet.rs

Lines changed: 177 additions & 199 deletions
Large diffs are not rendered by default.

libindy/src/commands/crypto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use errors::wallet::WalletError;
99
use errors::indy::IndyError;
1010
use domain::crypto::key::{KeyInfo, Key};
1111
use domain::crypto::combo_box::ComboBox;
12+
use utils::crypto::base64;
1213
use services::wallet::{WalletService, RecordOptions};
1314
use services::crypto::CryptoService;
1415

@@ -19,7 +20,6 @@ use std::result;
1920

2021
type Result<T> = result::Result<T, IndyError>;
2122

22-
use base64;
2323

2424
pub enum CryptoCommand {
2525
CreateKey(

libindy/src/commands/did.rs

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
extern crate indy_crypto;
2-
extern crate serde_json;
3-
41
use errors::common::CommonError;
52
use errors::did::DidError;
63
use errors::wallet::WalletError;
@@ -10,11 +7,11 @@ use domain::crypto::did::{MyDidInfo, Did, TheirDidInfo, TheirDid, TemporaryDid,
107
use domain::ledger::response::Reply;
118
use domain::ledger::nym::{GetNymReplyResult, GetNymResultDataV0};
129
use domain::ledger::attrib::{GetAttrReplyResult, AttribData, Endpoint};
13-
use services::pool::PoolService;
1410
use services::wallet::{WalletService, RecordOptions, SearchOptions};
1511
use services::crypto::CryptoService;
1612
use services::ledger::LedgerService;
1713

14+
use serde_json;
1815
use std::error::Error;
1916
use std::rc::Rc;
2017
use std::str;
@@ -24,10 +21,7 @@ use commands::ledger::LedgerCommand;
2421
use commands::{Command, CommandExecutor};
2522
use std::collections::HashMap;
2623
use utils::sequence::SequenceUtils;
27-
use utils::crypto::base58::Base58;
28-
use self::indy_crypto::utils::json::{JsonEncodable, JsonDecodable};
29-
30-
use super::utils::check_wallet_and_pool_handles_consistency;
24+
use utils::crypto::base58;
3125

3226
pub enum DidCommand {
3327
CreateAndStoreMyDid(
@@ -105,10 +99,6 @@ macro_rules! ensure_their_did {
10599
($self_:ident, $wallet_handle:ident, $pool_handle:ident, $their_did:ident, $deferred_cmd:expr, $cb:ident) => (match $self_._wallet_get_their_did($wallet_handle, &$their_did) {
106100
Ok(val) => val,
107101
Err(WalletError::ItemNotFound) => {
108-
109-
check_wallet_and_pool_handles_consistency!($self_.wallet_service, $self_.pool_service,
110-
$wallet_handle, $pool_handle, $cb);
111-
112102
// No their their_did present in the wallet. Defer this command until it is fetched from ledger.
113103
return $self_._fetch_their_did_from_ledger($wallet_handle, $pool_handle, &$their_did, $deferred_cmd);
114104
}
@@ -117,20 +107,17 @@ macro_rules! ensure_their_did {
117107
}
118108

119109
pub struct DidCommandExecutor {
120-
pool_service: Rc<PoolService>,
121110
wallet_service: Rc<WalletService>,
122111
crypto_service: Rc<CryptoService>,
123112
ledger_service: Rc<LedgerService>,
124113
deferred_commands: RefCell<HashMap<i32, DidCommand>>,
125114
}
126115

127116
impl DidCommandExecutor {
128-
pub fn new(pool_service: Rc<PoolService>,
129-
wallet_service: Rc<WalletService>,
117+
pub fn new(wallet_service: Rc<WalletService>,
130118
crypto_service: Rc<CryptoService>,
131119
ledger_service: Rc<LedgerService>) -> DidCommandExecutor {
132120
DidCommandExecutor {
133-
pool_service,
134121
wallet_service,
135122
crypto_service,
136123
ledger_service,
@@ -208,7 +195,7 @@ impl DidCommandExecutor {
208195
my_did_info_json: &str) -> Result<(String, String), IndyError> {
209196
debug!("create_and_store_my_did >>> wallet_handle: {:?}, my_did_info_json: {:?}", wallet_handle, my_did_info_json);
210197

211-
let my_did_info = MyDidInfo::from_json(&my_did_info_json)
198+
let my_did_info: MyDidInfo = serde_json::from_str(&my_did_info_json)
212199
.map_err(map_err_trace!())
213200
.map_err(|err|
214201
CommonError::InvalidStructure(
@@ -238,7 +225,7 @@ impl DidCommandExecutor {
238225

239226
self.crypto_service.validate_did(my_did)?;
240227

241-
let key_info: KeyInfo = KeyInfo::from_json(key_info_json)
228+
let key_info: KeyInfo = serde_json::from_str(key_info_json)
242229
.map_err(map_err_trace!())
243230
.map_err(|err|
244231
CommonError::InvalidStructure(format!("Invalid KeyInfo json: {}", err.description())))?;
@@ -284,7 +271,7 @@ impl DidCommandExecutor {
284271
their_did_info_json: &str) -> Result<(), IndyError> {
285272
debug!("store_their_did >>> wallet_handle: {:?}, their_did_info_json: {:?}", wallet_handle, their_did_info_json);
286273

287-
let their_did_info = TheirDidInfo::from_json(their_did_info_json)
274+
let their_did_info: TheirDidInfo = serde_json::from_str(their_did_info_json)
288275
.map_err(map_err_trace!())
289276
.map_err(|err|
290277
CommonError::InvalidStructure(format!("Invalid TheirDidInfo json: {}", err.description())))?;
@@ -305,8 +292,8 @@ impl DidCommandExecutor {
305292

306293
let did_record = self.wallet_service.get_indy_record::<Did>(wallet_handle, &my_did, &RecordOptions::full())?;
307294

308-
let did = did_record.get_value()
309-
.and_then(|tags_json| Did::from_json(&tags_json).ok())
295+
let did: Did = did_record.get_value()
296+
.and_then(|tags_json| serde_json::from_str(&tags_json).ok())
310297
.ok_or(CommonError::InvalidStructure(format!("Cannot deserialize Did: {:?}", my_did)))?;
311298

312299
let meta: Option<String> = did_record.get_tags()
@@ -318,7 +305,7 @@ impl DidCommandExecutor {
318305
metadata: meta
319306
};
320307

321-
let res = did_with_meta.to_json()
308+
let res = serde_json::to_string(&did_with_meta)
322309
.map_err(|err|
323310
IndyError::CommonError(CommonError::InvalidState(format!("Can't serialize DID {}", err))))?;
324311

@@ -338,8 +325,8 @@ impl DidCommandExecutor {
338325
while let Some(did_record) = did_search.fetch_next_record()? {
339326
let did_id = did_record.get_id();
340327

341-
let did = did_record.get_value()
342-
.and_then(|tags_json| Did::from_json(&tags_json).ok())
328+
let did: Did = did_record.get_value()
329+
.and_then(|tags_json| serde_json::from_str(&tags_json).ok())
343330
.ok_or(CommonError::InvalidStructure(format!("Cannot deserialize Did: {:?}", did_id)))?;
344331

345332
let meta: Option<String> = did_record.get_tags()
@@ -434,11 +421,10 @@ impl DidCommandExecutor {
434421

435422
let endpoint = Endpoint::new(address.to_string(), Some(transport_key.to_string()));
436423

437-
let res = self.wallet_service.upsert_indy_object(wallet_handle, &did, &endpoint)?;
424+
self.wallet_service.upsert_indy_object(wallet_handle, &did, &endpoint)?;
438425

439-
debug!("set_endpoint_for_did <<< res: {:?}", res);
440-
441-
Ok(res)
426+
debug!("set_endpoint_for_did <<<");
427+
Ok(())
442428
}
443429

444430
fn get_endpoint_for_did(&self,
@@ -456,9 +442,6 @@ impl DidCommandExecutor {
456442
match endpoint {
457443
Ok(endpoint) => cb(Ok((endpoint.ha, endpoint.verkey))),
458444
Err(WalletError::ItemNotFound) => {
459-
check_wallet_and_pool_handles_consistency!(self.wallet_service, self.pool_service,
460-
wallet_handle, pool_handle, cb);
461-
462445
return self._fetch_attrib_from_ledger(wallet_handle,
463446
pool_handle,
464447
&did,
@@ -517,13 +500,13 @@ impl DidCommandExecutor {
517500
self.crypto_service.validate_did(&did)?;
518501
self.crypto_service.validate_key(&verkey)?;
519502

520-
let did = Base58::decode(&did)?;
521-
let dverkey = Base58::decode(&verkey)?;
503+
let did = base58::decode(&did)?;
504+
let dverkey = base58::decode(&verkey)?;
522505

523506
let (first_part, second_part) = dverkey.split_at(16);
524507

525508
let res = if first_part.eq(did.as_slice()) {
526-
format!("~{}", Base58::encode(second_part))
509+
format!("~{}", base58::encode(second_part))
527510
} else {
528511
verkey
529512
};
@@ -546,13 +529,13 @@ impl DidCommandExecutor {
546529

547530
let get_nym_reply = get_nym_reply_result?;
548531

549-
let get_nym_response: Reply<GetNymReplyResult> = Reply::from_json(&get_nym_reply)
532+
let get_nym_response: Reply<GetNymReplyResult> = serde_json::from_str(&get_nym_reply)
550533
.map_err(map_err_trace!())
551534
.map_err(|err| CommonError::InvalidState(format!("Invalid GetNymReplyResult json: {:?}", err)))?;
552535

553536
let their_did_info = match get_nym_response.result() {
554537
GetNymReplyResult::GetNymReplyResultV0(res) => {
555-
let gen_nym_result_data = GetNymResultDataV0::from_json(&res.data)
538+
let gen_nym_result_data: GetNymResultDataV0 = serde_json::from_str(&res.data)
556539
.map_err(map_err_trace!())
557540
.map_err(|_| CommonError::InvalidState("Invalid GetNymResultData json".to_string()))?;
558541

@@ -583,7 +566,7 @@ impl DidCommandExecutor {
583566

584567
let get_attrib_reply = get_attrib_reply_result?;
585568

586-
let get_attrib_reply: Reply<GetAttrReplyResult> = Reply::from_json(&get_attrib_reply)
569+
let get_attrib_reply: Reply<GetAttrReplyResult> = serde_json::from_str(&get_attrib_reply)
587570
.map_err(map_err_trace!())
588571
.map_err(|err| CommonError::InvalidState(format!("Invalid GetAttrReplyResult json {:?}", err)))?;
589572

@@ -592,7 +575,7 @@ impl DidCommandExecutor {
592575
GetAttrReplyResult::GetAttrReplyResultV1(res) => (res.txn.data.raw, res.txn.data.did)
593576
};
594577

595-
let attrib_data = AttribData::from_json(&raw)
578+
let attrib_data: AttribData = serde_json::from_str(&raw)
596579
.map_err(map_err_trace!())
597580
.map_err(|err| CommonError::InvalidState(format!("Invalid GetAttReply json: {:?}", err)))?;
598581

libindy/src/commands/ledger.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
extern crate serde_json;
2-
extern crate indy_crypto;
3-
4-
use self::serde_json::Value;
5-
61
use api::ledger::{CustomFree, CustomTransactionParser};
72

83
use errors::common::CommonError;
@@ -16,19 +11,16 @@ use domain::crypto::key::Key;
1611
use domain::crypto::did::Did;
1712
use services::wallet::{WalletService, RecordOptions};
1813
use services::ledger::LedgerService;
14+
use utils::crypto::base58;
15+
use utils::crypto::signature_serializer::serialize_signature;
1916

20-
21-
use super::utils::check_wallet_and_pool_handles_consistency;
22-
17+
use serde_json;
18+
use serde_json::Value;
2319
use std::cell::RefCell;
2420
use std::collections::HashMap;
2521
use std::error::Error;
2622
use std::rc::Rc;
2723

28-
use utils::crypto::base58::Base58;
29-
30-
use utils::crypto::signature_serializer::serialize_signature;
31-
3224
pub enum LedgerCommand {
3325
SignAndSubmitRequest(
3426
i32, // pool handle
@@ -365,8 +357,6 @@ impl LedgerCommandExecutor {
365357
debug!("sign_and_submit_request >>> pool_handle: {:?}, wallet_handle: {:?}, submitter_did: {:?}, request_json: {:?}",
366358
pool_handle, wallet_handle, submitter_did, request_json);
367359

368-
check_wallet_and_pool_handles_consistency!(self.wallet_service, self.pool_service,
369-
wallet_handle, pool_handle, cb);
370360
match self._sign_request(wallet_handle, submitter_did, request_json, SignatureType::Single) {
371361
Ok(signed_request) => self.submit_request(pool_handle, signed_request.as_str(), cb),
372362
Err(err) => cb(Err(err))
@@ -406,14 +396,14 @@ impl LedgerCommandExecutor {
406396
let signature = self.crypto_service.sign(&my_key, &serialized_request.as_bytes().to_vec())?;
407397

408398
match signature_type {
409-
SignatureType::Single => { request["signature"] = Value::String(Base58::encode(&signature)); }
399+
SignatureType::Single => { request["signature"] = Value::String(base58::encode(&signature)); }
410400
SignatureType::Multi => {
411401
request.as_object_mut()
412402
.map(|request| {
413403
if !request.contains_key("signatures") {
414404
request.insert("signatures".to_string(), Value::Object(serde_json::Map::new()));
415405
}
416-
request["signatures"].as_object_mut().unwrap().insert(submitter_did.to_string(), Value::String(Base58::encode(&signature)));
406+
request["signatures"].as_object_mut().unwrap().insert(submitter_did.to_string(), Value::String(base58::encode(&signature)));
417407
});
418408
}
419409
}

libindy/src/commands/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#[macro_use]
2-
mod utils;
3-
41
pub mod anoncreds;
52
pub mod blob_storage;
63
pub mod crypto;
@@ -89,7 +86,7 @@ impl CommandExecutor {
8986
let crypto_command_executor = CryptoCommandExecutor::new(wallet_service.clone(), crypto_service.clone());
9087
let ledger_command_executor = LedgerCommandExecutor::new(pool_service.clone(), crypto_service.clone(), wallet_service.clone(), ledger_service.clone());
9188
let pool_command_executor = PoolCommandExecutor::new(pool_service.clone());
92-
let did_command_executor = DidCommandExecutor::new(pool_service.clone(), wallet_service.clone(), crypto_service.clone(), ledger_service.clone());
89+
let did_command_executor = DidCommandExecutor::new(wallet_service.clone(), crypto_service.clone(), ledger_service.clone());
9390
let wallet_command_executor = WalletCommandExecutor::new(wallet_service.clone());
9491
let pairwise_command_executor = PairwiseCommandExecutor::new(wallet_service.clone());
9592
let blob_storage_command_executor = BlobStorageCommandExecutor::new(blob_storage_service.clone());

libindy/src/commands/non_secrets.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::rc::Rc;
1010
use std::collections::HashMap;
1111
use utils::sequence::SequenceUtils;
1212
use std::cell::RefCell;
13-
use self::indy_crypto::utils::json::{JsonEncodable, JsonDecodable};
13+
use self::indy_crypto::utils::json::JsonEncodable;
1414

1515
use std::result;
1616

@@ -237,12 +237,12 @@ impl NonSecretsCommandExecutor {
237237

238238
self._check_type(type_)?;
239239

240-
RecordOptions::from_json(options_json)
240+
serde_json::from_str::<RecordOptions>(options_json)
241241
.map_err(|err| CommonError::InvalidStructure(format!("Cannot deserialize Options Json: {:?}", err)))?;
242242

243243
let record = self.wallet_service.get_record(wallet_handle, type_, id, &options_json)?;
244244

245-
let res = record.to_json()
245+
let res = serde_json::to_string(&record)
246246
.map_err(|err| CommonError::InvalidState(format!("Cannot serialize WalletRecord: {:?}", err)))?;
247247

248248
trace!("get_record <<< res: {:?}", res);
@@ -259,7 +259,7 @@ impl NonSecretsCommandExecutor {
259259

260260
self._check_type(type_)?;
261261

262-
SearchOptions::from_json(options_json)
262+
serde_json::from_str::<SearchOptions>(options_json)
263263
.map_err(|err| CommonError::InvalidStructure(format!("Cannot deserialize Options Json: {:?}", err)))?;
264264

265265
let search = self.wallet_service.search_records(wallet_handle, type_, query_json, &options_json)?;

0 commit comments

Comments
 (0)