Skip to content

Commit

Permalink
Upstream fetch from integritee
Browse files Browse the repository at this point in the history
* Add secp256k1 ecdsa recover (integritee-network#42)

* implement secp256k1_ecdsa_recover

* fix sgx compilation error

* reorder libsecp256k1 in .toml

Co-authored-by: suinuj <junius@litentry.com>

Add runtime-api `get_metadata` (integritee-network#47)

Co-authored-by: echevrier <edith.chevrier@scs.ch>

Bump substrate to commit 7c63420  (integritee-network#48)

* update .tomls to match new substrate version

* update rust toolchain

* cargo update

* revert unnecessary cargo update..

* updae sp-io version

* redo toolchain bump - teaclave..

* fix version changes

add missing sp-io functions (integritee-network#49)

* update sp-io trie

* add root version 2

* add storage::root version2

Include connect 4 pallet
  • Loading branch information
haerdib authored and gaudenzkessler committed Feb 15, 2022
1 parent 31c3ec3 commit 5063174
Show file tree
Hide file tree
Showing 8 changed files with 398 additions and 232 deletions.
442 changes: 255 additions & 187 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ pallet-transaction-payment-rpc-runtime-api = { version = "4.0.0-dev", default-fe
sp-api = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-block-builder = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-consensus-aura = { version = "0.10.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-core = { version = "4.1.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-core = { version = "5.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-inherents = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-offchain = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-runtime = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-runtime = { version = "5.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-session = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-std = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-transaction-pool = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
Expand All @@ -48,10 +48,9 @@ sp-version = { version = "4.0.0-dev", default-features = false, git = "https://g
pallet-parentchain = { default-features = false, git = "https://github.com/integritee-network/pallets.git", branch = "master" }

# Ajuna dependencies
pallet-ajuna-connectfour = { default-features = false, git = "https://github.com/ajuna-network/ajuna-node.git", branch = "darkfriend77/integritee" }
pallet-ajuna-connectfour = { default-features = false, git = "https://github.com/ajuna-network/ajuna-node.git", branch = "update-substrate-5" }
pallet-scheduler = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }


[features]
default = ['std']
runtime-benchmarks = [
Expand Down
9 changes: 9 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use frame_system::EnsureRoot;
use pallet_transaction_payment::CurrencyAdapter;
use sp_api::impl_runtime_apis;
use sp_core::OpaqueMetadata;
use sp_runtime::{
create_runtime_str, generic,
traits::{AccountIdLookup, BlakeTwo256, Block as BlockT, IdentifyAccount, Verify},
Expand Down Expand Up @@ -144,6 +145,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
state_version: 0,
};

pub const MILLISECS_PER_BLOCK: u64 = 6000;
Expand Down Expand Up @@ -338,4 +340,11 @@ impl_runtime_apis! {
Executive::initialize_block(header)
}
}

impl sp_api::Metadata<Block> for Runtime {
fn metadata() -> OpaqueMetadata {
OpaqueMetadata::new(Runtime::metadata().into())
}
}

}
4 changes: 2 additions & 2 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "nightly-2021-10-16"
channel = "nightly-2021-11-10"
targets = ["wasm32-unknown-unknown"]
profile = "default" # include rustfmt, clippy
profile = "default" # include rustfmt, clippy
6 changes: 3 additions & 3 deletions substrate-sgx/externalities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ serde = { version = "1.0", default-features = false, features = ["derive", "allo
postcard = { version = "0.7.2", default-features = false, features = ["alloc"] }

# sgx dependencies
sgx_tstd = { branch = "master", git = "https://github.com/apache/teaclave-sgx-sdk.git", features = ["untrusted_fs","net","backtrace"], optional = true }
sgx_types = { branch = "master", git = "https://github.com/apache/teaclave-sgx-sdk.git" }
sgx_tstd = { branch = "master", git = "https://github.com/apache/teaclave-sgx-sdk.git", features = ["untrusted_fs","net","backtrace"], optional = true }
sgx_types = { branch = "master", git = "https://github.com/apache/teaclave-sgx-sdk.git" }

[features]
default = ["std"]
Expand All @@ -28,4 +28,4 @@ std = [
]
sgx = [
"sgx_tstd",
]
]
28 changes: 13 additions & 15 deletions substrate-sgx/sp-io/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
[package]
name = "sp-io"
version = "4.0.0-dev"
version = "5.0.0"
authors = ["Integritee AG <hello@integritee.network> and Parity Technologies <admin@parity.io>"]
edition = "2018"
license = "Apache-2.0"

[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
hash-db = { version = "0.15.2", default-features = false }
libsecp256k1 = { version = "0.6.0", optional = true }
libsecp256k1 = { version = "0.7.0", default-features = false, features = ["static-context"] }
futures = { version = "0.3.1", features = ["thread-pool"], optional = true }
parking_lot = { version = "0.11.1", optional = true }
parking_lot = { version = "0.12.0", optional = true }
tracing = { version = "0.1.25", default-features = false }
tracing-core = { version = "0.1.17", default-features = false}
log = { version = "0.4", default-features = false }

environmental = { version = "1.1.3", default-features = false }
sgx_tstd = { branch = "master", git = "https://github.com/apache/teaclave-sgx-sdk.git", features = ["untrusted_fs","net","backtrace"], optional = true}
sgx_types = { branch = "master", git = "https://github.com/apache/teaclave-sgx-sdk.git", optional = true}
sgx_tstd = { branch = "master", git = "https://github.com/apache/teaclave-sgx-sdk.git", features = ["untrusted_fs","net","backtrace"], optional = true }
sgx_types = { branch = "master", git = "https://github.com/apache/teaclave-sgx-sdk.git", optional = true }
sgx-externalities = { default-features = false, path = "../externalities", optional = true }

# Substrate dependencies
sp-std = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-core = { version = "4.1.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master", features=["full_crypto"] }
sp-state-machine = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "master", optional = true}
sp-runtime-interface = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-wasm-interface = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-core = { version = "5.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master", features=["full_crypto"] }
sp-state-machine = { version = "0.11.0", git = "https://github.com/paritytech/substrate.git", branch = "master", optional = true}
sp-runtime-interface = { version = "5.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-wasm-interface = { version = "5.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-tracing = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-trie = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "master", optional = true }
sp-keystore = { version = "0.10.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master", optional = true }
sp-externalities = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "master", optional = true }
sp-trie = { version = "5.0.0", git = "https://github.com/paritytech/substrate.git", branch = "master", optional = true }
sp-keystore = { version = "0.11.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master", optional = true }
sp-externalities = { version = "0.11.0", git = "https://github.com/paritytech/substrate.git", branch = "master", optional = true }

[dev-dependencies]
hex-literal = { version = "0.3.4" }

[features]
default = ["std"]

std = [
"environmental/std",
"log/std",
Expand All @@ -47,15 +46,14 @@ std = [
"hash-db/std",
"sp-trie",
"sp-state-machine",
"libsecp256k1",
"libsecp256k1/std",
"sp-runtime-interface/std",
"sp-externalities",
"sp-wasm-interface/std",
"futures",
"parking_lot",
"sgx-externalities/std",
]

sgx = [
"sgx_tstd",
"sgx_types",
Expand Down
128 changes: 110 additions & 18 deletions substrate-sgx/sp-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use sp_core::{
HttpError, HttpRequestId, HttpRequestStatus, OpaqueNetworkState, StorageKind, Timestamp,
},
sr25519,
storage::StateVersion,
};
use std::{char, prelude::v1::String, println, vec, vec::Vec};

Expand Down Expand Up @@ -200,7 +201,22 @@ pub mod storage {
warn!("storage::append() unimplemented");
}

pub fn root() -> [u8; 32] {
/// "Commit" all existing operations and compute the resulting storage root.
///
/// The hashing algorithm is defined by the `Block`.
///
/// Returns a `Vec<u8>` that holds the SCALE encoded hash.
pub fn root_version_1() -> [u8; 32] {
warn!("storage::root() unimplemented");
[0u8; 32]
}

/// "Commit" all existing operations and compute the resulting storage root.
///
/// The hashing algorithm is defined by the `Block`.
///
/// Returns a `Vec<u8>` that holds the SCALE encoded hash.
pub fn root(version: StateVersion) -> [u8; 32] {
warn!("storage::root() unimplemented");
[0u8; 32]
}
Expand Down Expand Up @@ -308,7 +324,7 @@ pub mod default_child_storage {
/// Clear child default key by prefix.
///
/// Clear the child storage of each key-value pair where the key starts with the given `prefix`.
fn clear_prefix_version_1(storage_key: &[u8], prefix: &[u8]) {
pub fn clear_prefix_version_1(storage_key: &[u8], prefix: &[u8]) {
warn!("child storage::clear_prefix() unimplemented");
}

Expand All @@ -324,7 +340,12 @@ pub mod default_child_storage {
KillStorageResult::AllRemoved(0)
}

pub fn root(storage_key: &[u8]) -> Vec<u8> {
pub fn root_version_1(storage_key: &[u8]) -> Vec<u8> {
warn!("child storage::root() unimplemented");
vec![0, 1, 2, 3]
}

pub fn root(storage_key: &[u8], version: StateVersion) -> Vec<u8> {
warn!("child storage::root() unimplemented");
vec![0, 1, 2, 3]
}
Expand All @@ -339,35 +360,93 @@ pub mod trie {
use super::*;

/// A trie root formed from the iterated items.
pub fn blake2_256_root(input: Vec<(Vec<u8>, Vec<u8>)>) -> H256 {
pub fn blake2_256_root_version_1(input: Vec<(Vec<u8>, Vec<u8>)>) -> H256 {
warn!("trie::blake2_256_root() unimplemented");
H256::default()
}

/// A trie root formed from the iterated items.
pub fn blake2_256_root(input: Vec<(Vec<u8>, Vec<u8>)>, version: StateVersion) -> H256 {
warn!("trie::blake2_256_root() unimplemented");
H256::default()
}

/// A trie root formed from the enumerated items.
pub fn blake2_256_ordered_root(input: Vec<Vec<u8>>) -> H256 {
pub fn blake2_256_ordered_root_version_1(input: Vec<Vec<u8>>) -> H256 {
warn!("trie::blake2_256_ordered_root() unimplemented");
H256::default()
}

pub fn keccak_256_root(input: Vec<(Vec<u8>, Vec<u8>)>) -> H256 {
/// A trie root formed from the enumerated items.
pub fn blake2_256_ordered_root(input: Vec<Vec<u8>>, version: StateVersion) -> H256 {
warn!("trie::blake2_256_ordered_root() unimplemented");
H256::default()
}

pub fn keccak_256_root_version_1(input: Vec<(Vec<u8>, Vec<u8>)>) -> H256 {
warn!("trie::keccak_256_root_version_1() unimplemented");
H256::default()
}

pub fn keccak_256_root(input: Vec<(Vec<u8>, Vec<u8>)>, version: StateVersion) -> H256 {
warn!("trie::keccak_256_root() unimplemented");
H256::default()
}

/// A trie root formed from the enumerated items.
pub fn keccak_256_ordered_root(input: Vec<Vec<u8>>) -> H256 {
pub fn keccak_256_ordered_root_version_1(input: Vec<Vec<u8>>) -> H256 {
warn!("trie::keccak_256_ordered_root() unimplemented");
H256::default()
}

/// A trie root formed from the enumerated items.
pub fn keccak_256_ordered_root(input: Vec<Vec<u8>>, version: StateVersion) -> H256 {
warn!("trie::keccak_256_ordered_root() unimplemented");
H256::default()
}

/// Verify trie proof
fn blake2_256_verify_proof(root: H256, proof: &[Vec<u8>], key: &[u8], value: &[u8]) -> bool {
fn blake2_256_verify_proof_version_1(
root: H256,
proof: &[Vec<u8>],
key: &[u8],
value: &[u8],
) -> bool {
warn!("trie::blake2_256_verify_proof() unimplemented");
false
}

/// Verify trie proof
fn keccak_256_verify_proof(root: H256, proof: &[Vec<u8>], key: &[u8], value: &[u8]) -> bool {
fn blake2_256_verify_proof(
root: H256,
proof: &[Vec<u8>],
key: &[u8],
value: &[u8],
version: StateVersion,
) -> bool {
warn!("trie::blake2_256_verify_proof() unimplemented");
false
}

/// Verify trie proof
fn keccak_256_verify_proof_version_1(
root: H256,
proof: &[Vec<u8>],
key: &[u8],
value: &[u8],
) -> bool {
warn!("trie::keccak_256_verify_proof() unimplemented");
false
}

/// Verify trie proof
fn keccak_256_verify_proof(
root: H256,
proof: &[Vec<u8>],
key: &[u8],
value: &[u8],
version: StateVersion,
) -> bool {
warn!("trie::keccak_256_verify_proof() unimplemented");
false
}
Expand Down Expand Up @@ -401,14 +480,15 @@ pub mod misc {
/// Interfaces for working with crypto related types from within the runtime.
pub mod crypto {
use super::*;
use sp_core::H512;
pub fn ed25519_public_keys(id: KeyTypeId) -> Vec<ed25519::Public> {
warn!("crypto::ed25519_public_keys unimplemented");
vec![ed25519::Public::default()]
vec![ed25519::Public::from_h256(H256::default())]
}

pub fn ed25519_generate(id: KeyTypeId, seed: Option<Vec<u8>>) -> ed25519::Public {
warn!("crypto::ed25519_generate unimplemented");
ed25519::Public::default()
ed25519::Public::from_h256(H256::default())
}

pub fn ed25519_sign(
Expand All @@ -417,7 +497,8 @@ pub mod crypto {
msg: &[u8],
) -> Option<ed25519::Signature> {
warn!("crypto::ed25519_sign unimplemented");
Some(ed25519::Signature::default())

Some(ed25519::Signature::from_raw(H512::default().into()))
}

pub fn ed25519_verify(sig: &ed25519::Signature, msg: &[u8], pub_key: &ed25519::Public) -> bool {
Expand Down Expand Up @@ -461,12 +542,12 @@ pub mod crypto {

pub fn sr25519_public_keys(id: KeyTypeId) -> Vec<sr25519::Public> {
warn!("crypto::sr25519_public_key unimplemented");
vec![sr25519::Public::default()]
vec![sr25519::Public::from_h256(H256::default())]
}

pub fn sr25519_generate(id: KeyTypeId, seed: Option<Vec<u8>>) -> sr25519::Public {
warn!("crypto::sr25519_generate unimplemented");
sr25519::Public::default()
sr25519::Public::from_h256(H256::default())
}

pub fn sr25519_sign(
Expand All @@ -475,7 +556,7 @@ pub mod crypto {
msg: &[u8],
) -> Option<sr25519::Signature> {
warn!("crypto::sr25519_sign unimplemented");
Some(sr25519::Signature::default())
Some(sr25519::Signature::from_raw(H512::default().into()))
}

/// Verify `sr25519` signature.
Expand All @@ -499,7 +580,8 @@ pub mod crypto {
/// Returns the public key.
pub fn ecdsa_generate(id: KeyTypeId, seed: Option<Vec<u8>>) -> ecdsa::Public {
warn!("crypto::ecdsa_generate unimplemented");
ecdsa::Public::default()
let raw: [u8; 33] = [0; 33];
ecdsa::Public::from_raw(raw)
}

/// Sign the given `msg` with the `ecdsa` key that corresponds to the given public key and
Expand Down Expand Up @@ -539,8 +621,18 @@ pub mod crypto {
sig: &[u8; 65],
msg: &[u8; 32],
) -> Result<[u8; 64], EcdsaVerifyError> {
warn!("crypto::secp256k1_ecdsa_recover unimplemented");
Ok([0; 64])
let rs = libsecp256k1::Signature::parse_standard_slice(&sig[0..64])
.map_err(|_| EcdsaVerifyError::BadRS)?;
let v = libsecp256k1::RecoveryId::parse(
if sig[64] > 26 { sig[64] - 27 } else { sig[64] } as u8
)
.map_err(|_| EcdsaVerifyError::BadV)?;
let pubkey = libsecp256k1::recover(&libsecp256k1::Message::parse(msg), &rs, &v)
.map_err(|_| EcdsaVerifyError::BadSignature)?;
let mut res = [0u8; 64];
res.copy_from_slice(&pubkey.serialize()[1..65]);

Ok(res)
}

pub fn secp256k1_ecdsa_recover_compressed(
Expand Down
6 changes: 3 additions & 3 deletions test-no-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ sgx_tstd = { branch = "master", git = "https://github.com/apache/teaclave-sgx-sd
[dependencies]
libc = { version = "0.2", default-features = false }
sgx-runtime = { path = "../runtime", default-features = false }
sp-io = { version = "4.0.0-dev", path = "../substrate-sgx/sp-io", default-features = false, features = ["disable_oom", "disable_panic_handler", "disable_allocator", "sgx"] }
sp-application-crypto = { git = "https://github.com/paritytech/substrate.git", branch = "master", default-features = false, features = ["full_crypto"] }
sp-core = { version = "4.1.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master", features = ["full_crypto"] }
sp-io = { version = "5.0.0", path = "../substrate-sgx/sp-io", default-features = false, features = ["disable_oom", "disable_panic_handler", "disable_allocator", "sgx"] }
sp-application-crypto = { version = "5.0.0", git = "https://github.com/paritytech/substrate.git", branch = "master", default-features = false, features = ["full_crypto"] }
sp-core = { version = "5.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master", features = ["full_crypto"] }

0 comments on commit 5063174

Please sign in to comment.