Skip to content

Commit

Permalink
Merge pull request #145 from manuelmauro/fix-indexer-parsing
Browse files Browse the repository at this point in the history
Fix deserialization bugs
  • Loading branch information
ivnsch authored Mar 17, 2022
2 parents a35c351 + f45da3e commit 0d93ce1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
23 changes: 21 additions & 2 deletions algonaut_crypto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::fmt::{self, Formatter};

use algonaut_encoding::{deserialize_bytes32, SignatureVisitor, U8_32Visitor};
use data_encoding::{BASE32_NOPAD, BASE64};
use fmt::Debug;
use ring::signature::UnparsedPublicKey;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;

/// Support for turning 32 byte keys into human-readable mnemonics and back
pub mod mnemonic;
Expand All @@ -16,6 +16,25 @@ pub mod error;
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct HashDigest(pub [u8; 32]);

impl FromStr for HashDigest {
type Err = String;
fn from_str(string: &str) -> Result<Self, Self::Err> {
let mut decoded = [0; 32];
decoded.copy_from_slice(
&BASE64
.decode(string.as_bytes())
.map_err(|e| e.to_string())?,
);
Ok(HashDigest(decoded))
}
}

impl Display for HashDigest {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", BASE64.encode(&self.0))
}
}

#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Ed25519PublicKey(pub [u8; 32]);

Expand Down
16 changes: 12 additions & 4 deletions algonaut_model/src/indexer/v2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use algonaut_core::{Address, MicroAlgos, Round};
use algonaut_crypto::{deserialize_hash, HashDigest};
use algonaut_crypto::HashDigest;
use algonaut_encoding::deserialize_bytes;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
Expand Down Expand Up @@ -927,12 +927,14 @@ pub struct AssetParams {
}

/// Block information.
#[serde_as]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Block {
/// `gh` hash to which this block belongs.
///
/// Pattern : "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==\|[A-Za-z0-9+/]{3}=)?$"
#[serde(rename = "genesis-hash", deserialize_with = "deserialize_hash")]
#[serde_as(as = "DisplayFromStr")]
#[serde(rename = "genesis-hash")]
pub genesis_hash: HashDigest,

/// `gen` ID to which this block belongs.
Expand Down Expand Up @@ -1208,6 +1210,7 @@ pub struct TealValue {

/// Contains all fields common to all transactions and serves as an envelope to all transactions
/// type..
#[serde_as]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Transaction {
/// Application transaction.
Expand Down Expand Up @@ -1261,7 +1264,8 @@ pub struct Transaction {
/// `gh` Hash of genesis block.
///
/// Pattern : "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==\|[A-Za-z0-9+/]{3}=)?$"
#[serde(rename = "genesis-hash", deserialize_with = "deserialize_hash")]
#[serde_as(as = "DisplayFromStr")]
#[serde(rename = "genesis-hash")]
pub genesis_hash: HashDigest,

/// `gen` genesis block ID.
Expand Down Expand Up @@ -1301,6 +1305,8 @@ pub struct Transaction {
/// be confirmed.
///
/// Pattern : "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==\|[A-Za-z0-9+/]{3}=)?$"
#[serde(default)]
#[serde_as(as = "Option<DisplayFromStr>")]
pub lease: Option<HashDigest>,

/// `ld` Local state key/value changes for the application being executed by this transaction.
Expand Down Expand Up @@ -1372,13 +1378,15 @@ pub enum TransactionType {
ApplicationTransaction,
}

#[serde_as]
/// Fields for application transactions.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TransactionApplication {
/// `apat` List of accounts in addition to the sender that may be accessed from the application's
/// approval-program and clear-state-program.
#[serde_as(as = "Vec<DisplayFromStr>")]
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub accounts: Vec<Account>,
pub accounts: Vec<Address>,

/// `apaa` transaction specific arguments accessed from the application's approval-program and
/// clear-state-program.
Expand Down

0 comments on commit 0d93ce1

Please sign in to comment.