Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the cryptographic hash to newly created pallas-crypto
- Loading branch information
Showing
15 changed files
with
309 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,4 @@ mod utils; | |
pub use framework::*; | ||
pub use model::*; | ||
|
||
#[cfg(feature = "crypto")] | ||
pub mod crypto; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "pallas-crypto" | ||
description = "Cryptographic primitives for Cardano" | ||
version = "0.3.0" | ||
edition = "2021" | ||
repository = "https://github.com/txpipe/pallas" | ||
homepage = "https://github.com/txpipe/pallas" | ||
documentation = "https://docs.rs/pallas-crypto" | ||
license = "Apache-2.0" | ||
readme = "README.md" | ||
authors = [ | ||
"Nicolas Di Prima <nicolas@primetype.co.uk>" | ||
] | ||
|
||
[dependencies] | ||
minicbor = { version = "0.12" } | ||
hex = "0.4" | ||
cryptoxide = { version = "0.3.6" } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Pallas Crypto | ||
|
||
Crate with all the cryptographic material to support Cardano protocol: | ||
|
||
- [x] Blake2b 256 | ||
- [x] Blake2b 224 | ||
- [ ] Ed25519 asymmetric key pair and ECDSA | ||
- [ ] Ed25519 Extended asymmetric key pair | ||
- [ ] Bip32-Ed25519 key derivation | ||
- [ ] BIP39 mnemonics | ||
- [ ] VRF | ||
- [ ] KES | ||
- [ ] SECP256k1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use std::{fmt, ops::Deref, str::FromStr}; | ||
|
||
/// data that is a cryptographic [`struct@Hash`] of `BYTES` long. | ||
/// | ||
/// Possible values with Cardano are 32 bytes long (block hash or transaction | ||
/// hash). Or 28 bytes long (as used in addresses) | ||
/// | ||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub struct Hash<const BYTES: usize>([u8; BYTES]); | ||
|
||
impl<const BYTES: usize> Hash<BYTES> { | ||
#[inline] | ||
pub const fn new(bytes: [u8; BYTES]) -> Self { | ||
Self(bytes) | ||
} | ||
} | ||
|
||
impl<const BYTES: usize> From<[u8; BYTES]> for Hash<BYTES> { | ||
#[inline] | ||
fn from(bytes: [u8; BYTES]) -> Self { | ||
Self::new(bytes) | ||
} | ||
} | ||
|
||
impl<const BYTES: usize> AsRef<[u8]> for Hash<BYTES> { | ||
#[inline] | ||
fn as_ref(&self) -> &[u8] { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<const BYTES: usize> Deref for Hash<BYTES> { | ||
type Target = [u8; BYTES]; | ||
|
||
#[inline] | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<const BYTES: usize> PartialEq<[u8]> for Hash<BYTES> { | ||
fn eq(&self, other: &[u8]) -> bool { | ||
self.0.eq(other) | ||
} | ||
} | ||
|
||
impl<const BYTES: usize> fmt::Debug for Hash<BYTES> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_tuple(&format!("Hash<{size}>", size = BYTES)) | ||
.field(&hex::encode(self)) | ||
.finish() | ||
} | ||
} | ||
|
||
impl<const BYTES: usize> fmt::Display for Hash<BYTES> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.write_str(&hex::encode(self)) | ||
} | ||
} | ||
|
||
impl<const BYTES: usize> FromStr for Hash<BYTES> { | ||
type Err = hex::FromHexError; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let mut bytes = [0; BYTES]; | ||
hex::decode_to_slice(s, &mut bytes)?; | ||
Ok(Self::new(bytes)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn from_str() { | ||
let _digest: Hash<28> = "276fd18711931e2c0e21430192dbeac0e458093cd9d1fcd7210f64b3" | ||
.parse() | ||
.unwrap(); | ||
|
||
let _digest: Hash<32> = "0d8d00cdd4657ac84d82f0a56067634a7adfdf43da41cb534bcaa45060973d21" | ||
.parse() | ||
.unwrap(); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn from_str_fail_1() { | ||
let _digest: Hash<28> = "27".parse().unwrap(); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn from_str_fail_2() { | ||
let _digest: Hash<32> = "0d8d00cdd465".parse().unwrap(); | ||
} | ||
} |
Oops, something went wrong.