Skip to content

Commit cbe911e

Browse files
authored
Merge branch 'main' into hu55a1n1/v0_2-improvements
2 parents 1e80233 + cf373dc commit cbe911e

File tree

14 files changed

+90
-87
lines changed

14 files changed

+90
-87
lines changed

Cargo.lock

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/contracts/core/src/handler/execute/session_set_pub_key.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl Handler for SessionSetPubKey {
1313
let (nonce, pub_key) = self.into_tuple();
1414

1515
let session = session
16-
.with_pub_key(nonce, pub_key)
16+
.with_pub_key(nonce, pub_key.clone())
1717
.ok_or(Error::BadSessionTransition)?;
1818
SESSION.save(deps.storage, &session).map_err(Error::Std)?;
1919

@@ -24,9 +24,6 @@ impl Handler for SessionSetPubKey {
2424

2525
Ok(Response::new()
2626
.add_attribute("action", "session_set_pub_key")
27-
.add_attribute(
28-
"pub_key",
29-
HexBinary::from(pub_key.to_sec1_bytes().into_vec()).to_hex(),
30-
))
27+
.add_attribute("pub_key", HexBinary::from(pub_key).to_hex()))
3128
}
3229
}

crates/contracts/core/src/msg/execute/session_set_pub_key.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
use cosmwasm_schema::cw_serde;
22
use cosmwasm_std::{HexBinary, StdError};
3-
use k256::ecdsa::VerifyingKey;
43
use sha2::{Digest, Sha256};
54

65
use crate::{
7-
error::Error,
86
msg::{execute::attested::HasUserData, HasDomainType},
97
state::{Nonce, UserData},
108
};
119

1210
#[derive(Clone, Debug, PartialEq)]
1311
pub struct SessionSetPubKey {
1412
nonce: Nonce,
15-
pub_key: VerifyingKey,
13+
pub_key: Vec<u8>,
1614
}
1715

1816
impl SessionSetPubKey {
19-
pub fn new(nonce: Nonce, pub_key: VerifyingKey) -> Self {
17+
pub fn new(nonce: Nonce, pub_key: Vec<u8>) -> Self {
2018
Self { nonce, pub_key }
2119
}
2220

23-
pub fn into_tuple(self) -> (Nonce, VerifyingKey) {
21+
pub fn into_tuple(self) -> (Nonce, Vec<u8>) {
2422
(self.nonce, self.pub_key)
2523
}
2624
}
@@ -42,18 +40,18 @@ impl TryFrom<RawSessionSetPubKey> for SessionSetPubKey {
4240

4341
fn try_from(value: RawSessionSetPubKey) -> Result<Self, Self::Error> {
4442
let nonce = value.nonce.to_array()?;
45-
let pub_key = VerifyingKey::from_sec1_bytes(&value.pub_key)
46-
.map_err(Error::from)
47-
.map_err(|e| StdError::generic_err(e.to_string()))?;
48-
Ok(Self { nonce, pub_key })
43+
Ok(Self {
44+
nonce,
45+
pub_key: value.pub_key.into(),
46+
})
4947
}
5048
}
5149

5250
impl From<SessionSetPubKey> for RawSessionSetPubKey {
5351
fn from(value: SessionSetPubKey) -> Self {
5452
Self {
5553
nonce: value.nonce.into(),
56-
pub_key: value.pub_key.to_sec1_bytes().into_vec().into(),
54+
pub_key: value.pub_key.into(),
5755
}
5856
}
5957
}

crates/contracts/core/src/state.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use cosmwasm_schema::cw_serde;
22
use cosmwasm_std::{HexBinary, StdError, Uint64};
33
use cw_storage_plus::Item;
4-
use k256::ecdsa::VerifyingKey;
54

65
pub type MrEnclave = [u8; 32];
76
pub type Nonce = [u8; 32];
@@ -233,9 +232,9 @@ impl Session {
233232
}
234233
}
235234

236-
pub fn with_pub_key(mut self, nonce: Nonce, pub_key: VerifyingKey) -> Option<Self> {
235+
pub fn with_pub_key(mut self, nonce: Nonce, pub_key: Vec<u8>) -> Option<Self> {
237236
if self.nonce == nonce && self.pub_key.is_none() {
238-
self.pub_key = Some(pub_key.to_sec1_bytes().into_vec().into());
237+
self.pub_key = Some(pub_key.into());
239238
Some(self)
240239
} else {
241240
None

crates/enclave/core/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async-trait.workspace = true
2424
displaydoc.workspace = true
2525
futures-util.workspace = true
2626
hex.workspace = true
27-
k256.workspace = true
27+
k256 = { workspace = true, features = ["pem", "serde"] }
2828
log.workspace = true
2929
rand.workspace = true
3030
reqwest = { workspace = true, features = ["blocking"] }

crates/enclave/core/src/handler/session_set_pubkey.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use cosmrs::AccountId;
2-
use k256::ecdsa::VerifyingKey;
32
use quartz_contract_core::{
43
msg::execute::{attested::Attested, session_set_pub_key::SessionSetPubKey},
54
state::{Session, SESSION_KEY},
@@ -24,7 +23,7 @@ use crate::{
2423
impl<E> Handler<E> for RawSessionSetPubKeyRequest
2524
where
2625
E: Enclave,
27-
E::KeyManager: KeyManager<PubKey = VerifyingKey>,
26+
E::KeyManager: KeyManager,
2827
E::Store: Store<Contract = AccountId>,
2928
{
3029
type Error = Status;
@@ -71,13 +70,10 @@ where
7170
}
7271

7372
// generate enclave key
74-
ctx.key_manager().await.keygen().await;
75-
let pk = ctx
76-
.key_manager()
77-
.await
78-
.pub_key()
79-
.await
80-
.ok_or_else(|| Status::internal("failed to get public key"))?;
73+
let pk = {
74+
let pk = ctx.key_manager().await.pub_key().await;
75+
serde_json::to_vec(&pk).expect("pubkey serialization failure")
76+
};
8177

8278
// create `SessionSetPubKey` msg and attest to it
8379
let msg = SessionSetPubKey::new(nonce, pk);
+4-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
use serde::Serialize;
2+
13
pub mod default;
24
pub mod shared;
35

46
#[async_trait::async_trait]
57
pub trait KeyManager: Send + Sync + 'static {
6-
type PubKey;
7-
type PrivKey;
8+
type PubKey: Serialize;
89

9-
async fn keygen(&mut self);
10-
async fn pub_key(&self) -> Option<Self::PubKey>;
11-
async fn priv_key(&self) -> Option<Self::PrivKey>;
10+
async fn pub_key(&self) -> Self::PubKey;
1211
}

crates/enclave/core/src/key_manager/default.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@ use k256::ecdsa::{SigningKey, VerifyingKey};
22

33
use crate::key_manager::KeyManager;
44

5-
#[derive(Clone, Default)]
5+
#[derive(Clone)]
66
pub struct DefaultKeyManager {
7-
sk: Option<SigningKey>,
7+
pub sk: SigningKey,
8+
}
9+
10+
impl Default for DefaultKeyManager {
11+
fn default() -> Self {
12+
Self {
13+
sk: SigningKey::random(&mut rand::thread_rng()),
14+
}
15+
}
816
}
917

1018
#[async_trait::async_trait]
1119
impl KeyManager for DefaultKeyManager {
1220
type PubKey = VerifyingKey;
13-
type PrivKey = SigningKey;
14-
15-
async fn keygen(&mut self) {
16-
self.sk = Some(SigningKey::random(&mut rand::thread_rng()));
17-
}
18-
19-
async fn pub_key(&self) -> Option<Self::PubKey> {
20-
self.sk.clone().map(|sk| VerifyingKey::from(&sk))
21-
}
2221

23-
async fn priv_key(&self) -> Option<Self::PrivKey> {
24-
self.sk.clone()
22+
async fn pub_key(&self) -> Self::PubKey {
23+
self.sk.clone().into()
2524
}
2625
}
+7-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::sync::Arc;
22

3-
use tokio::sync::RwLock;
3+
use tokio::sync::{RwLock, RwLockReadGuard};
44

55
use crate::key_manager::KeyManager;
66

77
#[derive(Clone, Debug)]
88
pub struct SharedKeyManager<K> {
9-
inner: Arc<RwLock<K>>,
9+
pub inner: Arc<RwLock<K>>,
1010
}
1111

1212
impl<K> SharedKeyManager<K> {
@@ -15,22 +15,17 @@ impl<K> SharedKeyManager<K> {
1515
inner: Arc::new(RwLock::new(key_manager)),
1616
}
1717
}
18+
19+
pub async fn read_lock(&self) -> RwLockReadGuard<'_, K> {
20+
self.inner.read().await
21+
}
1822
}
1923

2024
#[async_trait::async_trait]
2125
impl<K: KeyManager> KeyManager for SharedKeyManager<K> {
2226
type PubKey = K::PubKey;
23-
type PrivKey = K::PrivKey;
24-
25-
async fn keygen(&mut self) {
26-
self.inner.write().await.keygen().await
27-
}
2827

29-
async fn pub_key(&self) -> Option<Self::PubKey> {
28+
async fn pub_key(&self) -> Self::PubKey {
3029
self.inner.read().await.pub_key().await
3130
}
32-
33-
async fn priv_key(&self) -> Option<Self::PrivKey> {
34-
self.inner.read().await.priv_key().await
35-
}
3631
}

crates/enclave/core/src/lib.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub struct DefaultEnclave<C, A = DefaultAttestor, K = DefaultKeyManager, S = Def
5454
pub ctx: C,
5555
}
5656

57-
impl<C> DefaultSharedEnclave<C> {
57+
impl<C: Send + Sync + 'static> DefaultSharedEnclave<C> {
5858
pub fn shared(attestor: DefaultAttestor, config: Config, ctx: C) -> DefaultSharedEnclave<C> {
5959
DefaultSharedEnclave {
6060
attestor,
@@ -63,6 +63,18 @@ impl<C> DefaultSharedEnclave<C> {
6363
ctx,
6464
}
6565
}
66+
67+
pub fn with_key_manager<K: KeyManager>(
68+
self,
69+
key_manager: K,
70+
) -> DefaultEnclave<C, <Self as Enclave>::Attestor, K, <Self as Enclave>::Store> {
71+
DefaultEnclave {
72+
attestor: self.attestor,
73+
key_manager,
74+
store: self.store,
75+
ctx: self.ctx,
76+
}
77+
}
6678
}
6779

6880
#[async_trait::async_trait]

examples/transfers/enclave/Cargo.lock

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/transfers/enclave/src/request/query.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use cosmwasm_std::{Addr, HexBinary, Uint128};
22
use k256::ecdsa::VerifyingKey;
3-
use quartz_common::enclave::{
4-
handler::Handler, key_manager::KeyManager, DefaultSharedEnclave, Enclave,
5-
};
3+
use quartz_common::enclave::{handler::Handler, DefaultSharedEnclave};
64
use serde::{Deserialize, Serialize};
75
use tonic::Status;
86
use transfers_contract::msg::execute;
@@ -35,12 +33,7 @@ impl Handler<DefaultSharedEnclave<()>> for QueryRequest {
3533
let state = match &message.state.to_vec()[..] {
3634
&[0] => State::default(),
3735
state_bytes => {
38-
let sk = ctx
39-
.key_manager()
40-
.await
41-
.priv_key()
42-
.await
43-
.ok_or_else(|| Status::internal("failed to get private key"))?;
36+
let sk = ctx.key_manager.read_lock().await.sk.clone();
4437
decrypt_state(&sk, state_bytes)?
4538
}
4639
};

0 commit comments

Comments
 (0)