Skip to content

Commit

Permalink
feature propagation and parent macros (#2455)
Browse files Browse the repository at this point in the history
  • Loading branch information
kziemianek committed Jan 31, 2024
1 parent 7b43936 commit 2f77bf8
Show file tree
Hide file tree
Showing 24 changed files with 122 additions and 150 deletions.
53 changes: 34 additions & 19 deletions bitacross-worker/Cargo.lock

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

1 change: 0 additions & 1 deletion bitacross-worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ members = [
"sidechain/state",
"sidechain/validateer-fetch",
"litentry/primitives",
"litentry/macros",
]

[patch."https://github.com/apache/teaclave-sgx-sdk.git"]
Expand Down
1 change: 1 addition & 0 deletions bitacross-worker/app-libs/parentchain-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ sp-runtime = { default-features = false, git = "https://github.com/paritytech/su

# litentry
lc-scheduled-enclave = { path = "../../litentry/core/scheduled-enclave", default-features = false, optional = true }
litentry-hex-utils = { path = "../../../primitives/hex", default-features = false }
litentry-primitives = { path = "../../litentry/primitives", default-features = false }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use ita_stf::{Getter, TrustedCall, TrustedCallSigned};
use itc_parentchain_indirect_calls_executor::error::Error;
use itp_stf_primitives::{traits::IndirectExecutor, types::TrustedOperation};
use itp_types::parentchain::{AccountId, FilterEvents, HandleParentchainEvents, ParentchainError};
use itp_utils::hex::hex_encode;
use litentry_hex_utils::hex_encode;
use log::*;

pub struct ParentchainEventHandler {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use ita_stf::{Getter, TrustedCall, TrustedCallSigned};
use itc_parentchain_indirect_calls_executor::error::Error;
use itp_stf_primitives::{traits::IndirectExecutor, types::TrustedOperation};
use itp_types::parentchain::{AccountId, FilterEvents, HandleParentchainEvents, ParentchainError};
use itp_utils::hex::hex_encode;
use litentry_hex_utils::hex_encode;
use log::*;

pub struct ParentchainEventHandler {}
Expand Down
4 changes: 4 additions & 0 deletions bitacross-worker/app-libs/stf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ sp-std = { default-features = false, git = "https://github.com/paritytech/substr

# litentry
itp-node-api-metadata-provider = { path = "../../core-primitives/node-api/metadata-provider", default-features = false }
litentry-macros = { path = "../../../primitives/core/macros", default-features = false }
litentry-primitives = { path = "../../litentry/primitives", default-features = false }
pallet-parentchain = { path = "../../../pallets/parentchain", default-features = false }

Expand Down Expand Up @@ -87,3 +88,6 @@ std = [
"itp-node-api-metadata-provider/std",
]
test = []
production = [
"litentry-macros/production",
]
3 changes: 2 additions & 1 deletion bitacross-worker/app-libs/stf/src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use codec::{Decode, Encode};
use ita_sgx_runtime::System;
use itp_stf_interface::ExecuteGetter;
use itp_stf_primitives::{traits::GetterAuthorization, types::KeyPair};
use itp_utils::{if_production_or, stringify::account_id_to_string};
use itp_utils::stringify::account_id_to_string;
use litentry_macros::if_production_or;
use litentry_primitives::{Identity, LitentryMultiSignature};
use log::*;
use sp_std::vec;
Expand Down
34 changes: 20 additions & 14 deletions bitacross-worker/app-libs/stf/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@
use crate::ENCLAVE_ACCOUNT_KEY;
use codec::{Decode, Encode};
use frame_support::ensure;
use hex_literal::hex;
use itp_stf_primitives::error::{StfError, StfResult};
use itp_storage::{storage_double_map_key, storage_map_key, storage_value_key, StorageHasher};
use itp_types::Index;
use itp_utils::stringify::account_id_to_string;
use litentry_primitives::{ErrorDetail, Identity, Web3ValidationData};
use log::*;
use sp_core::blake2_256;
use sp_runtime::AccountId32;
use std::prelude::v1::*;

pub const ALICE_ACCOUNTID32: AccountId32 =
AccountId32::new(hex!["d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"]);
#[cfg(not(feature = "production"))]
pub use non_prod::*;

pub fn get_storage_value<V: Decode>(
storage_prefix: &'static str,
Expand Down Expand Up @@ -128,16 +126,6 @@ pub fn ensure_enclave_signer_or_self<AccountId: Encode + Decode + PartialEq>(
}
}

#[cfg(not(feature = "production"))]
pub fn ensure_alice(signer: &AccountId32) -> bool {
signer == &ALICE_ACCOUNTID32
}

#[cfg(not(feature = "production"))]
pub fn ensure_enclave_signer_or_alice(signer: &AccountId32) -> bool {
signer == &enclave_signer_account::<AccountId32>() || ensure_alice(signer)
}

// verification message format:
// ```
// blake2_256(<sidechain nonce> + <primary account> + <identity-to-be-linked>)
Expand Down Expand Up @@ -173,3 +161,21 @@ pub fn verify_web3_identity(

Ok(())
}

#[cfg(not(feature = "production"))]
mod non_prod {
use super::*;
use hex_literal::hex;
use sp_runtime::AccountId32;

pub const ALICE_ACCOUNTID32: AccountId32 =
AccountId32::new(hex!["d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"]);

pub fn ensure_alice(signer: &AccountId32) -> bool {
signer == &ALICE_ACCOUNTID32
}

pub fn ensure_enclave_signer_or_alice(signer: &AccountId32) -> bool {
signer == &enclave_signer_account::<AccountId32>() || ensure_alice(signer)
}
}
1 change: 1 addition & 0 deletions bitacross-worker/core-primitives/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition = "2021"
[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
litentry-hex-utils = { path = "../../../primitives/hex", default-features = false }

[features]
default = ["std"]
Expand Down
46 changes: 3 additions & 43 deletions bitacross-worker/core-primitives/utils/src/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
// Todo: merge with hex_display

use crate::error::{Error, Result};
use alloc::{string::String, vec::Vec};
use alloc::string::String;
use codec::{Decode, Encode};
use litentry_hex_utils::{decode_hex, hex_encode};

/// Trait to encode a given value to a hex string, prefixed with "0x".
pub trait ToHexPrefixed {
Expand All @@ -45,57 +46,16 @@ impl<T: Decode> FromHexPrefixed for T {
type Output = T;

fn from_hex(msg: &str) -> Result<Self::Output> {
let byte_array = decode_hex(msg)?;
let byte_array = decode_hex(msg).map_err(Error::Hex)?;
Decode::decode(&mut byte_array.as_slice()).map_err(Error::Codec)
}
}

/// Hex encodes given data and preappends a "0x".
pub fn hex_encode(data: &[u8]) -> String {
let mut hex_str = hex::encode(data);
hex_str.insert_str(0, "0x");
hex_str
}

/// Helper method for decoding hex.
pub fn decode_hex<T: AsRef<[u8]>>(message: T) -> Result<Vec<u8>> {
let message = message.as_ref();
let message = match message {
[b'0', b'x', hex_value @ ..] => hex_value,
_ => message,
};

let decoded_message = hex::decode(message).map_err(Error::Hex)?;
Ok(decoded_message)
}

#[cfg(test)]
mod tests {
use super::*;
use alloc::string::ToString;

#[test]
fn hex_encode_decode_works() {
let data = "Hello World!".to_string();

let hex_encoded_data = hex_encode(&data.encode());
let decoded_data =
String::decode(&mut decode_hex(hex_encoded_data).unwrap().as_slice()).unwrap();

assert_eq!(data, decoded_data);
}

#[test]
fn hex_encode_decode_works_empty_input() {
let data = String::new();

let hex_encoded_data = hex_encode(&data.encode());
let decoded_data =
String::decode(&mut decode_hex(hex_encoded_data).unwrap().as_slice()).unwrap();

assert_eq!(data, decoded_data);
}

#[test]
fn hex_encode_decode_works_empty_input_for_decode() {
let data = String::new();
Expand Down
1 change: 0 additions & 1 deletion bitacross-worker/core-primitives/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub mod buffer;
pub mod error;
pub mod hex;
pub mod hex_display;
pub mod macros;
pub mod stringify;

// Public re-exports.
Expand Down

0 comments on commit 2f77bf8

Please sign in to comment.