Skip to content

Commit

Permalink
Eliminate dependence of indy_ledger_response_parser on indy-api-types
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <[email protected]>
  • Loading branch information
mirgee committed Aug 11, 2023
1 parent 3491f23 commit 264e3ad
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 49 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion indy_ledger_response_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
time = "=0.3.20"
ursa = { version = "0.3.7" }
indy-api-types = { path = "../libvdrtools/indy-api-types" }
indy-vdr = { git = "https://github.com/Patrik-Stas/indy-vdr.git", rev = "3cd499ad75", default-features = false, features = ["log"] }
thiserror = "1.0.44"
25 changes: 14 additions & 11 deletions indy_ledger_response_parser/src/domain/response.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use indy_api_types::{
errors::{err_msg, IndyErrorKind},
IndyError,
};
use crate::error::LedgerResponseParserError;

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -107,10 +104,16 @@ impl<'a, T> TryFrom<ReplyV0<TypedReply<'a, T>>> for ReplyV0<T>
where
T: ReplyType,
{
type Error = IndyError;
type Error = LedgerResponseParserError;

fn try_from(value: ReplyV0<TypedReply<'a, T>>) -> Result<Self, Self::Error> {
if value.result.type_ != T::get_type() {
Err(err_msg(IndyErrorKind::InvalidTransaction, "Invalid response type"))
let expected_type = T::get_type();
let actual_type = value.result.type_;
if expected_type != actual_type {
Err(LedgerResponseParserError::InvalidTransaction(format!(
"Unexpected response type:\nExpected: {}\nActual: {}",
expected_type, actual_type
)))
} else {
Ok(ReplyV0 {
result: value.result.reply,
Expand All @@ -123,15 +126,15 @@ impl<'a, T> TryFrom<ReplyV1<TypedReply<'a, T>>> for ReplyV1<T>
where
T: ReplyType,
{
type Error = IndyError;
type Error = LedgerResponseParserError;

fn try_from(value: ReplyV1<TypedReply<'a, T>>) -> Result<Self, Self::Error> {
let value = value
.data
.result
.into_iter()
.next()
.ok_or_else(|| err_msg(IndyErrorKind::InvalidTransaction, "Invalid response type"))?;
.ok_or_else(|| LedgerResponseParserError::InvalidTransaction("Result field is empty".to_string()))?;
let data = ReplyDataV1 {
result: [value.try_into()?],
};
Expand All @@ -143,7 +146,7 @@ impl<'a, T> TryFrom<Reply<TypedReply<'a, T>>> for Reply<T>
where
T: ReplyType,
{
type Error = IndyError;
type Error = LedgerResponseParserError;

fn try_from(value: Reply<TypedReply<'a, T>>) -> Result<Self, Self::Error> {
let reply = match value {
Expand All @@ -158,7 +161,7 @@ impl<'a, T> TryFrom<MessageWithTypedReply<'a, T>> for Message<T>
where
T: ReplyType,
{
type Error = IndyError;
type Error = LedgerResponseParserError;

fn try_from(value: MessageWithTypedReply<'a, T>) -> Result<Self, Self::Error> {
match value {
Expand Down
11 changes: 11 additions & 0 deletions indy_ledger_response_parser/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum LedgerResponseParserError {
#[error("JSON error: {0}")]
JsonError(#[from] serde_json::error::Error),
#[error("Ledger item not found: {0}")]
LedgerItemNotFound(&'static str),
#[error("Invalid transaction: {0}")]
InvalidTransaction(String),
}
59 changes: 23 additions & 36 deletions indy_ledger_response_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ mod domain;

pub use domain::author_agreement::GetTxnAuthorAgreementData;
use domain::author_agreement::GetTxnAuthorAgreementResult;
pub use indy_api_types::{errors, ErrorCode};
use indy_api_types::{
errors::{err_msg, IndyErrorKind, IndyResult, IndyResultExt},
IndyError,
};
use error::LedgerResponseParserError;
use indy_vdr::{
ledger::{
identifiers::{CredentialDefinitionId, RevocationRegistryId, SchemaId},
Expand Down Expand Up @@ -56,22 +52,15 @@ impl ResponseParser {
Self {}
}

pub fn parse_get_nym_response(&self, get_nym_response: &str) -> IndyResult<NymData> {
pub fn parse_get_nym_response(&self, get_nym_response: &str) -> Result<NymData, LedgerResponseParserError> {
let reply: Reply<GetNymReplyResult> = Self::parse_response(get_nym_response)?;

let nym_data = match reply.result() {
GetNymReplyResult::GetNymReplyResultV0(res) => {
let data: GetNymResultDataV0 = res
.data
.ok_or_else(|| IndyError::from_msg(IndyErrorKind::LedgerItemNotFound, format!("Nym not found")))
.and_then(|data| {
serde_json::from_str(&data).map_err(|err| {
IndyError::from_msg(
IndyErrorKind::InvalidState,
format!("Cannot parse GET_NYM response: {}", err),
)
})
})?;
.ok_or_else(|| LedgerResponseParserError::LedgerItemNotFound("NYM"))
.and_then(|data| serde_json::from_str(&data).map_err(Into::into))?;

NymData {
did: data.dest,
Expand All @@ -93,7 +82,7 @@ impl ResponseParser {
&self,
get_schema_response: &str,
method_name: Option<&str>,
) -> IndyResult<Schema> {
) -> Result<Schema, LedgerResponseParserError> {
let reply: Reply<GetSchemaReplyResult> = Self::parse_response(get_schema_response)?;

let schema = match reply.result() {
Expand Down Expand Up @@ -128,7 +117,7 @@ impl ResponseParser {
&self,
get_cred_def_response: &str,
method_name: Option<&str>,
) -> IndyResult<CredentialDefinition> {
) -> Result<CredentialDefinition, LedgerResponseParserError> {
let reply: Reply<GetCredDefReplyResult> = Self::parse_response(get_cred_def_response)?;

let cred_def = match reply.result() {
Expand Down Expand Up @@ -159,7 +148,7 @@ impl ResponseParser {
pub fn parse_get_revoc_reg_def_response(
&self,
get_revoc_reg_def_response: &str,
) -> IndyResult<RevocationRegistryDefinition> {
) -> Result<RevocationRegistryDefinition, LedgerResponseParserError> {
let reply: Reply<GetRevocRegDefReplyResult> = Self::parse_response(get_revoc_reg_def_response)?;

let revoc_reg_def = match reply.result() {
Expand All @@ -172,7 +161,10 @@ impl ResponseParser {
))
}

pub fn parse_get_revoc_reg_response(&self, get_revoc_reg_response: &str) -> IndyResult<RevocationRegistryInfo> {
pub fn parse_get_revoc_reg_response(
&self,
get_revoc_reg_response: &str,
) -> Result<RevocationRegistryInfo, LedgerResponseParserError> {
let reply: Reply<GetRevocRegReplyResult> = Self::parse_response(get_revoc_reg_response)?;

let (revoc_reg_def_id, revoc_reg, timestamp) = match reply.result() {
Expand All @@ -191,13 +183,16 @@ impl ResponseParser {
})
}

pub fn parse_get_txn_author_agreement_response(&self, taa_response: &str) -> IndyResult<GetTxnAuthorAgreementData> {
pub fn parse_get_txn_author_agreement_response(
&self,
taa_response: &str,
) -> Result<GetTxnAuthorAgreementData, LedgerResponseParserError> {
let reply: Reply<GetTxnAuthorAgreementResult> = Self::parse_response(taa_response)?;

let data = match reply.result() {
GetTxnAuthorAgreementResult::GetTxnAuthorAgreementResultV1(res) => res
.data
.ok_or_else(|| IndyError::from_msg(IndyErrorKind::LedgerItemNotFound, "TAA not found"))?,
.ok_or_else(|| LedgerResponseParserError::LedgerItemNotFound("TAA"))?,
};

Ok(GetTxnAuthorAgreementData {
Expand All @@ -212,7 +207,7 @@ impl ResponseParser {
pub fn parse_get_revoc_reg_delta_response(
&self,
get_revoc_reg_delta_response: &str,
) -> IndyResult<RevocationRegistryDeltaInfo> {
) -> Result<RevocationRegistryDeltaInfo, LedgerResponseParserError> {
let reply: Reply<GetRevocRegDeltaReplyResult> = Self::parse_response(get_revoc_reg_delta_response)?;

let (revoc_reg_def_id, revoc_reg) = match reply.result() {
Expand All @@ -228,11 +223,7 @@ impl ResponseParser {
&revoc_reg.value.accum_to.value,
&revoc_reg.value.issued,
&revoc_reg.value.revoked,
))
.to_indy(
IndyErrorKind::InvalidStructure,
"Cannot convert RevocationRegistryDelta to Value",
)?,
))?,
};

Ok(RevocationRegistryDeltaInfo {
Expand All @@ -242,20 +233,16 @@ impl ResponseParser {
})
}

pub fn parse_response<T>(response: &str) -> IndyResult<Reply<T>>
pub fn parse_response<T>(response: &str) -> Result<Reply<T>, LedgerResponseParserError>
where
T: DeserializeOwned + ReplyType + ::std::fmt::Debug,
{
let message: Message<T> = serde_json::from_str(response).to_indy(
IndyErrorKind::LedgerItemNotFound,
"Structure doesn't correspond to type. Most probably not found",
)?; // FIXME: Review how we handle not found
let message: Message<T> = serde_json::from_str(response)?;

match message {
Message::Reject(response) | Message::ReqNACK(response) => Err(err_msg(
IndyErrorKind::InvalidTransaction,
format!("Transaction has been failed: {:?}", response.reason),
)),
Message::Reject(response) | Message::ReqNACK(response) => {
Err(LedgerResponseParserError::InvalidTransaction(response.reason))
}
Message::Reply(reply) => Ok(reply),
}
}
Expand Down

0 comments on commit 264e3ad

Please sign in to comment.