Skip to content

Commit

Permalink
refactor ffi, implement method wrapping for FFI client
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <marin.versic101@gmail.com>
  • Loading branch information
mversic committed Jul 28, 2022
1 parent cd78eb5 commit f9cf8a0
Show file tree
Hide file tree
Showing 36 changed files with 1,682 additions and 1,266 deletions.
9 changes: 6 additions & 3 deletions crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ default = ["std"]
std = ["ursa"]
# Force static linking
vendored = ["openssl-sys"]
# Generate extern functions callable via FFI
ffi = ["iroha_ffi"]
## Replace structures and methods with FFI equivalents. Facilitates dynamic linkage
#ffi = ["iroha_ffi/client"]

# Expose FFI API for dynamic linking (Internal use only)
ffi_api = ["std"]

[dependencies]
iroha_primitives = { path = "../primitives", version = "=2.0.0-pre-rc.5", default-features = false }
iroha_ffi = { path = "../ffi", version = "=2.0.0-pre-rc.5", optional = true }
iroha_ffi = { path = "../ffi", version = "=2.0.0-pre-rc.5" }
iroha_schema = { path = "../schema" }

derive_more = { version = "0.99.16", default-features = false, features = ["deref", "deref_mut", "display"] }
Expand Down
105 changes: 52 additions & 53 deletions crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ mod signature;
mod varint;

#[cfg(not(feature = "std"))]
use alloc::{format, string::String, vec::Vec};
use alloc::{boxed::Box, format, string::String, vec::Vec};
use core::{fmt, str::FromStr};

#[cfg(feature = "base64")]
pub use base64;
use derive_more::{DebugCustom, Display};
use getset::Getters;
pub use hash::*;
#[cfg(feature = "ffi")]
use iroha_ffi::{ffi_export, IntoFfi, TryFromFfi};
#[cfg(any(feature = "ffi_api", feature = "ffi"))]
use iroha_ffi::ffi_export;
use iroha_ffi::{ffi, IntoFfi, TryFromReprC};
use iroha_primitives::conststr::ConstString;
use iroha_schema::IntoSchema;
pub use merkle::MerkleTree;
Expand Down Expand Up @@ -64,8 +65,7 @@ pub struct NoSuchAlgorithm;
impl std::error::Error for NoSuchAlgorithm {}

/// Algorithm for hashing
#[cfg_attr(feature = "ffi", derive(IntoFfi, TryFromFfi))]
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, IntoFfi, TryFromReprC)]
#[repr(u8)]
pub enum Algorithm {
/// Ed25519
Expand Down Expand Up @@ -164,15 +164,16 @@ impl KeyGenConfiguration {
}
}

/// Pair of Public and Private keys.
#[cfg_attr(feature = "ffi", derive(IntoFfi, TryFromFfi))]
#[derive(Debug, Clone, PartialEq, Eq, Getters, Serialize)]
#[getset(get = "pub")]
pub struct KeyPair {
/// Public Key.
public_key: PublicKey,
/// Private Key.
private_key: PrivateKey,
ffi! {
/// Pair of Public and Private keys.
#[derive(Debug, Clone, PartialEq, Eq, Getters, Serialize, IntoFfi, TryFromReprC)]
#[getset(get = "pub")]
pub struct KeyPair {
/// Public Key.
public_key: PublicKey,
/// Private Key.
private_key: PrivateKey,
}
}

/// Error when dealing with cryptographic functions
Expand Down Expand Up @@ -363,21 +364,22 @@ impl From<multihash::ConvertError> for KeyParseError {
#[cfg(feature = "std")]
impl std::error::Error for KeyParseError {}

/// Public Key used in signatures.
#[derive(DebugCustom, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, IntoSchema)]
#[cfg_attr(feature = "ffi", derive(IntoFfi, TryFromFfi))]
#[debug(
fmt = "{{ digest: {digest_function}, payload: {} }}",
"hex::encode_upper(payload.as_slice())"
)]
pub struct PublicKey {
/// Digest function
digest_function: ConstString,
/// payload of key
payload: Vec<u8>,
ffi! {
/// Public Key used in signatures.
#[derive(DebugCustom, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, IntoFfi, TryFromReprC, IntoSchema)]
#[debug(
fmt = "{{ digest: {digest_function}, payload: {} }}",
"hex::encode_upper(payload.as_slice())"
)]
pub struct PublicKey {
/// Digest function
digest_function: ConstString,
/// payload of key
payload: Vec<u8>,
}
}

#[cfg_attr(feature = "ffi", ffi_export)]
#[cfg_attr(any(feature = "ffi_api", feature = "ffi"), ffi_export)]
impl PublicKey {
/// Key payload
pub fn payload(&self) -> &[u8] {
Expand Down Expand Up @@ -494,21 +496,22 @@ impl Decode for PublicKey {
}
}

/// Private Key used in signatures.
#[derive(DebugCustom, Display, Clone, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "ffi", derive(IntoFfi, TryFromFfi))]
#[debug(fmt = "{{ digest: {digest_function}, payload: {:X?}}}", payload)]
#[display(fmt = "{}", "hex::encode(payload)")]
#[allow(clippy::multiple_inherent_impl)]
pub struct PrivateKey {
/// Digest function
digest_function: ConstString,
/// key payload. WARNING! Do not use `"string".as_bytes()` to obtain the key.
#[serde(with = "hex::serde")]
payload: Vec<u8>,
ffi! {
/// Private Key used in signatures.
#[derive(DebugCustom, Clone, PartialEq, Eq, Display, Serialize, IntoFfi, TryFromReprC)]
#[debug(fmt = "{{ digest: {digest_function}, payload: {:X?}}}", payload)]
#[display(fmt = "{}", "hex::encode(payload)")]
#[allow(clippy::multiple_inherent_impl)]
pub struct PrivateKey {
/// Digest function
digest_function: ConstString,
/// key payload. WARNING! Do not use `"string".as_bytes()` to obtain the key.
#[serde(with = "hex::serde")]
payload: Vec<u8>,
}
}

#[cfg_attr(feature = "ffi", ffi_export)]
#[cfg_attr(any(feature = "ffi_api", feature = "ffi"), ffi_export)]
impl PrivateKey {
/// Key payload
pub fn payload(&self) -> &[u8] {
Expand Down Expand Up @@ -574,19 +577,15 @@ impl<'de> Deserialize<'de> for PrivateKey {
}
}

#[cfg(feature = "ffi")]
mod ffi {
use iroha_ffi::{gen_ffi_impl, handles};

use super::{KeyPair, PrivateKey, PublicKey};

handles! {0, KeyPair, PublicKey, PrivateKey}

gen_ffi_impl! { Clone: KeyPair, PublicKey, PrivateKey}
gen_ffi_impl! { Eq: KeyPair, PublicKey, PrivateKey}
gen_ffi_impl! { Ord: PublicKey }
gen_ffi_impl! { Drop: KeyPair, PublicKey, PrivateKey}
}
iroha_ffi::handles! {0, KeyPair, PublicKey, PrivateKey}
#[cfg(any(feature = "ffi_api", feature = "ffi"))]
iroha_ffi::gen_ffi_impl! { Clone: KeyPair, PublicKey, PrivateKey}
#[cfg(any(feature = "ffi_api", feature = "ffi"))]
iroha_ffi::gen_ffi_impl! { Eq: KeyPair, PublicKey, PrivateKey}
#[cfg(any(feature = "ffi_api", feature = "ffi"))]
iroha_ffi::gen_ffi_impl! { Ord: PublicKey }
#[cfg(any(feature = "ffi_api", feature = "ffi"))]
iroha_ffi::gen_ffi_impl! { Drop: KeyPair, PublicKey, PrivateKey}

/// The prelude re-exports most commonly used traits, structs and macros from this crate.
pub mod prelude {
Expand Down
8 changes: 5 additions & 3 deletions data_model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ default = ["std"]
# Disabled for WASM interoperability, to reduce the binary size.
# Please refer to https://docs.rust-embedded.org/book/intro/no-std.html
std = ["iroha_macro/std", "iroha_version/std", "iroha_version/warp", "iroha_crypto/std", "iroha_primitives/std", "thiserror", "strum/std", "dashmap", "tokio"]
# Generate extern functions callable via FFI
ffi = ["iroha_crypto/ffi", "iroha_ffi"]
## Replace structures and methods with FFI equivalents. Facilitates dynamic linkage
#ffi = ["iroha_ffi/client"]

# Expose FFI API for dynamic linking (Internal use only)
ffi_api = ["std"]
# Expose API for mutating structures (Internal use only)
mutable_api = []

Expand All @@ -35,7 +37,7 @@ iroha_crypto = { path = "../crypto", version = "=2.0.0-pre-rc.5", default-featur
iroha_macro = { path = "../macro", version = "=2.0.0-pre-rc.5", default-features = false }
iroha_version = { path = "../version", version = "=2.0.0-pre-rc.5", default-features = false, features = ["derive", "json", "scale"] }
iroha_schema = { path = "../schema", version = "=2.0.0-pre-rc.5" }
iroha_ffi = { path = "../ffi", version = "=2.0.0-pre-rc.5", optional = true }
iroha_ffi = { path = "../ffi", version = "=2.0.0-pre-rc.5" }

dashmap = { version = "4.0", optional = true}
tokio = { version = "1.6.0", features = ["sync", "rt-multi-thread"], optional = true}
Expand Down
38 changes: 26 additions & 12 deletions data_model/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#[cfg(not(feature = "std"))]
use alloc::{
boxed::Box,
collections::{btree_map, btree_set},
format,
string::String,
Expand All @@ -14,8 +15,9 @@ use std::collections::{btree_map, btree_set};
use derive_more::Display;
use getset::{Getters, MutGetters, Setters};
use iroha_data_model_derive::IdOrdEqHash;
#[cfg(feature = "ffi")]
use iroha_ffi::{ffi_export, IntoFfi, TryFromFfi};
#[cfg(any(feature = "ffi_api", feature = "ffi"))]
use iroha_ffi::ffi_export;
use iroha_ffi::{IntoFfi, TryFromReprC};
use iroha_schema::IntoSchema;
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -89,9 +91,10 @@ impl From<GenesisAccount> for Account {
Encode,
Deserialize,
Serialize,
IntoFfi,
TryFromReprC,
IntoSchema,
)]
#[cfg_attr(feature = "ffi", derive(IntoFfi, TryFromFfi))]
pub struct SignatureCheckCondition(pub EvaluatesTo<bool>);

impl SignatureCheckCondition {
Expand Down Expand Up @@ -126,13 +129,22 @@ impl Default for SignatureCheckCondition {
}

/// Builder which should be submitted in a transaction to create a new [`Account`]
#[allow(clippy::multiple_inherent_impl)]
#[derive(
Debug, Display, Clone, IdOrdEqHash, Decode, Encode, Deserialize, Serialize, IntoSchema,
Debug,
Display,
Clone,
IdOrdEqHash,
Decode,
Encode,
Deserialize,
Serialize,
IntoFfi,
TryFromReprC,
IntoSchema,
)]
#[cfg_attr(feature = "ffi", derive(IntoFfi, TryFromFfi))]
#[display(fmt = "[{id}]")]
#[id(type = "<Account as Identifiable>::Id")]
#[allow(clippy::multiple_inherent_impl)]
#[display(fmt = "[{id}]")]
pub struct NewAccount {
/// Identification
id: <Account as Identifiable>::Id,
Expand Down Expand Up @@ -185,7 +197,7 @@ impl NewAccount {
}
}

#[cfg_attr(feature = "ffi", ffi_export)]
#[cfg_attr(any(feature = "ffi_api", feature = "ffi"), ffi_export)]
impl NewAccount {
/// Add [`Metadata`] to the account replacing previously defined
#[must_use]
Expand All @@ -208,12 +220,13 @@ impl NewAccount {
Encode,
Deserialize,
Serialize,
IntoFfi,
TryFromReprC,
IntoSchema,
)]
#[cfg_attr(feature = "ffi", derive(IntoFfi, TryFromFfi))]
#[allow(clippy::multiple_inherent_impl)]
#[display(fmt = "({id})")] // TODO: Add more?
#[id(type = "Id")]
#[allow(clippy::multiple_inherent_impl)]
pub struct Account {
/// An Identification of the [`Account`].
id: <Self as Identifiable>::Id,
Expand Down Expand Up @@ -244,7 +257,7 @@ impl Registered for Account {
type With = NewAccount;
}

#[cfg_attr(feature = "ffi", ffi_export)]
#[cfg_attr(any(feature = "ffi_api", feature = "ffi"), ffi_export)]
impl Account {
/// Construct builder for [`Account`] identifiable by [`Id`] containing the given signatories.
#[must_use]
Expand Down Expand Up @@ -401,9 +414,10 @@ impl FromIterator<Account> for crate::Value {
Encode,
Deserialize,
Serialize,
IntoFfi,
TryFromReprC,
IntoSchema,
)]
#[cfg_attr(feature = "ffi", derive(IntoFfi, TryFromFfi))]
#[display(fmt = "{name}@{domain_id}")]
pub struct Id {
/// [`Account`]'s name.
Expand Down
Loading

0 comments on commit f9cf8a0

Please sign in to comment.