Skip to content

Commit

Permalink
make sure of the pallas_crypto::Hash type
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDP committed Jan 18, 2022
1 parent 08b92bd commit 424aeb1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
24 changes: 11 additions & 13 deletions pallas-alonzo/src/model.rs
Expand Up @@ -5,6 +5,7 @@
use log::warn;
use minicbor::{bytes::ByteVec, data::Tag};
use minicbor_derive::{Decode, Encode};
use pallas_crypto::hash::Hash;
use std::{collections::BTreeMap, ops::Deref};

use crate::utils::{KeyValuePairs, MaybeIndefArray};
Expand Down Expand Up @@ -46,7 +47,7 @@ pub struct HeaderBody {
pub slot: u64,

#[n(2)]
pub prev_hash: ByteVec,
pub prev_hash: Hash<32>,

#[n(3)]
pub issuer_vkey: ByteVec,
Expand All @@ -64,7 +65,7 @@ pub struct HeaderBody {
pub block_body_size: u64,

#[n(8)]
pub block_body_hash: ByteVec,
pub block_body_hash: Hash<32>,

#[n(9)]
pub operational_cert: ByteVec,
Expand Down Expand Up @@ -100,7 +101,7 @@ pub struct Header {
#[derive(Encode, Decode, Debug, PartialEq)]
pub struct TransactionInput {
#[n(0)]
pub transaction_id: ByteVec,
pub transaction_id: Hash<32>,

#[n(1)]
pub index: u64,
Expand All @@ -124,7 +125,7 @@ pub struct Nonce {
pub variant: NonceVariant,

#[n(1)]
pub hash: Hash32,
pub hash: Hash<32>,
}

pub type ScriptHash = ByteVec;
Expand Down Expand Up @@ -196,14 +197,11 @@ pub struct TransactionOutput {
pub datum_hash: Option<ByteVec>,
}

pub type Hash28 = ByteVec;
pub type Hash32 = ByteVec;

pub type PoolKeyhash = Hash28;
pub type PoolKeyhash = Hash<28>;
pub type Epoch = u64;
pub type Genesishash = ByteVec;
pub type GenesisDelegateHash = ByteVec;
pub type VrfKeyhash = Hash32;
pub type VrfKeyhash = Hash<32>;

/* move_instantaneous_reward = [ 0 / 1, { * stake_credential => delta_coin } / coin ]
; The first field determines where the funds are drawn from.
Expand Down Expand Up @@ -361,7 +359,7 @@ impl minicbor::encode::Encode for Relay {
}
}

pub type PoolMetadataHash = Hash32;
pub type PoolMetadataHash = Hash<32>;

#[derive(Encode, Decode, Debug, PartialEq)]
pub struct PoolMetadata {
Expand All @@ -372,8 +370,8 @@ pub struct PoolMetadata {
pub hash: PoolMetadataHash,
}

pub type AddrKeyhash = Hash28;
pub type Scripthash = Hash28;
pub type AddrKeyhash = Hash<28>;
pub type Scripthash = Hash<28>;

#[derive(Debug, PartialEq)]
pub struct RationalNumber {
Expand Down Expand Up @@ -720,7 +718,7 @@ pub enum TransactionBodyComponent {
AuxiliaryDataHash(ByteVec),
ValidityIntervalStart(u64),
Mint(Multiasset<i64>),
ScriptDataHash(Hash32),
ScriptDataHash(Hash<32>),
Collateral(MaybeIndefArray<TransactionInput>),
RequiredSigners(MaybeIndefArray<AddrKeyhash>),
NetworkId(NetworkId),
Expand Down
25 changes: 25 additions & 0 deletions pallas-crypto/src/hash/hash.rs
@@ -1,3 +1,4 @@
use minicbor::{Decode, Encode};
use std::{fmt, ops::Deref, str::FromStr};

/// data that is a cryptographic [`struct@Hash`] of `BYTES` long.
Expand Down Expand Up @@ -67,6 +68,30 @@ impl<const BYTES: usize> FromStr for Hash<BYTES> {
}
}

impl<const BYTES: usize> Encode for Hash<BYTES> {
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
) -> Result<(), minicbor::encode::Error<W::Error>> {
e.bytes(&self.0)?.ok()
}
}

impl<'a, const BYTES: usize> Decode<'a> for Hash<BYTES> {
fn decode(d: &mut minicbor::Decoder<'a>) -> Result<Self, minicbor::decode::Error> {
let bytes = d.bytes()?;
if bytes.len() == BYTES {
let mut hash = [0; BYTES];
hash.copy_from_slice(bytes);
Ok(Self::new(hash))
} else {
// TODO: minicbor does not allow for expecting a specific size byte array
// (in fact cbor is not good at it at all anyway)
Err(minicbor::decode::Error::Message("Invalid hash size"))
}
}
}

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

0 comments on commit 424aeb1

Please sign in to comment.