Skip to content

Commit 2e273e4

Browse files
authored
OmniAccount auth rework (#3431)
1 parent 9e71edb commit 2e273e4

40 files changed

+658
-227
lines changed

common/primitives/core/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,22 @@ pub use constants::*;
4444
pub use litentry_proc_macros::*;
4545
pub use opaque::*;
4646
pub use sp_runtime::BoundedVec;
47+
pub use traits::*;
4748
pub use types::*;
4849

4950
pub type ParameterString = BoundedVec<u8, ConstU32<64>>;
5051

52+
mod traits {
53+
use crate::types;
54+
55+
pub trait Hashable {
56+
fn hash(&self) -> types::Hash;
57+
}
58+
}
59+
5160
/// Common types of parachains.
5261
mod types {
62+
use parity_scale_codec::Encode;
5363
use sp_core::H256;
5464
use sp_runtime::{
5565
traits::{IdentifyAccount, Verify},
@@ -63,6 +73,12 @@ mod types {
6373
/// to the public key of our transaction signing scheme.
6474
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
6575

76+
impl crate::traits::Hashable for AccountId {
77+
fn hash(&self) -> Hash {
78+
self.using_encoded(sp_core::hashing::blake2_256).into()
79+
}
80+
}
81+
6682
/// Signed version of Balance
6783
pub type Amount = i128;
6884

tee-worker/client-api/src/omni/interfaces/omniExecutor/definitions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ export default {
4242
},
4343
OmniAuth: {
4444
_enum: {
45-
Web3: "(HeimaMultiSignature)",
45+
Web3: "(Identity, HeimaMultiSignature)",
4646
Email: "(Text, Text)",
47-
AuthToken: "(Text)",
48-
OAuth2: "(OAuth2Data)",
47+
AuthToken: "(Text, Text)",
48+
OAuth2: "(Identity, OAuth2Data)",
4949
},
5050
},
5151
OAuth2Data: {

tee-worker/omni-executor/Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tee-worker/omni-executor/executor-crypto/src/jwt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn decode<T: DeserializeOwned>(
2323
if skip_exp_check {
2424
validation.validate_exp = false;
2525
}
26+
validation.set_required_spec_claims(&["sub", "typ"]);
2627
let decoding_key = DecodingKey::from_rsa_der(public_key);
2728
decode_jwt::<T>(token, &decoding_key, &validation).map(|data| data.claims)
2829
}

tee-worker/omni-executor/executor-primitives/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ authors = ['Trust Computing GmbH <info@litentry.com>']
55
edition.workspace = true
66

77
[dependencies]
8+
base58 = { workspace = true }
89
base64 = { workspace = true }
910
bitcoin = { workspace = true, features = ["secp-recovery"] }
1011
hex = { workspace = true }
@@ -15,7 +16,10 @@ tracing = { workspace = true }
1516

1617
executor-crypto = { workspace = true }
1718
heima-primitives = { workspace = true }
18-
sp-core = { workspace = true }
19+
sp-core = { workspace = true, features = ["serde"] }
20+
21+
[dev-dependencies]
22+
serde_json = { workspace = true }
1923

2024
[lints]
2125
workspace = true
Lines changed: 114 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,140 @@
1-
use crate::{signature::HeimaMultiSignature, OmniAccountAuthType};
1+
use crate::{signature::HeimaMultiSignature, utils::hex::decode_hex, OmniAccountAuthType};
2+
use base58::FromBase58;
3+
use heima_primitives::{Address20, Address32, Address33, Identity, IdentityString};
24
use parity_scale_codec::{Decode, Encode};
5+
use serde::{Deserialize, Serialize};
36

47
pub type VerificationCode = String;
8+
type Email = String;
9+
type OmniAccount = String;
10+
type JwtToken = String;
11+
12+
/// A serializable representation of Identity for JSON interchange.
13+
/// ```json
14+
/// {
15+
/// "type": "Email",
16+
/// "data": "test@test.com"
17+
/// }
18+
/// ```
19+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
20+
#[serde(tag = "type", content = "data")]
21+
pub enum IdentitySerde {
22+
Twitter(String),
23+
Discord(String),
24+
Github(String),
25+
Substrate(String), // hex-encoded
26+
Evm(String), // hex-encoded
27+
Bitcoin(String), // hex-encoded
28+
Solana(String), // base58-encoded
29+
Email(String),
30+
Google(String),
31+
Pumpx(String),
32+
}
33+
34+
impl TryFrom<IdentitySerde> for Identity {
35+
type Error = &'static str;
36+
37+
fn try_from(value: IdentitySerde) -> Result<Self, Self::Error> {
38+
match value {
39+
IdentitySerde::Twitter(handle) => {
40+
Ok(Identity::Twitter(IdentityString::new(handle.as_bytes().to_vec())))
41+
},
42+
IdentitySerde::Discord(handle) => {
43+
Ok(Identity::Discord(IdentityString::new(handle.as_bytes().to_vec())))
44+
},
45+
IdentitySerde::Github(handle) => {
46+
Ok(Identity::Github(IdentityString::new(handle.as_bytes().to_vec())))
47+
},
48+
IdentitySerde::Substrate(hex_address) => {
49+
let bytes = decode_hex(&hex_address).map_err(|_| "Invalid hex encoding")?;
50+
let address =
51+
Address32::try_from(bytes.as_slice()).map_err(|_| "Invalid address")?;
52+
Ok(Identity::Substrate(address))
53+
},
54+
IdentitySerde::Evm(hex_address) => {
55+
let bytes = decode_hex(&hex_address).map_err(|_| "Invalid hex encoding")?;
56+
let address =
57+
Address20::try_from(bytes.as_slice()).map_err(|_| "Invalid address")?;
58+
Ok(Identity::Evm(address))
59+
},
60+
IdentitySerde::Bitcoin(hex_address) => {
61+
let bytes = decode_hex(&hex_address).map_err(|_| "Invalid hex encoding")?;
62+
let address =
63+
Address33::try_from(bytes.as_slice()).map_err(|_| "Invalid address")?;
64+
Ok(Identity::Bitcoin(address))
65+
},
66+
IdentitySerde::Solana(base58_address) => {
67+
let address: Address32 = base58_address
68+
.from_base58()
69+
.map_err(|_| "Invalid base58 encoding")?
70+
.as_slice()
71+
.try_into()
72+
.map_err(|_| "Invalid address")?;
73+
Ok(Identity::Solana(address))
74+
},
75+
IdentitySerde::Email(handle) => {
76+
Ok(Identity::Email(IdentityString::new(handle.as_bytes().to_vec())))
77+
},
78+
IdentitySerde::Google(handle) => {
79+
Ok(Identity::Google(IdentityString::new(handle.as_bytes().to_vec())))
80+
},
81+
IdentitySerde::Pumpx(handle) => {
82+
Ok(Identity::Pumpx(IdentityString::new(handle.as_bytes().to_vec())))
83+
},
84+
}
85+
}
86+
}
587

688
#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq)]
789
pub enum OmniAuth {
8-
Web3(HeimaMultiSignature),
9-
Email(String, VerificationCode),
10-
AuthToken(String),
11-
OAuth2(OAuth2Data),
90+
Web3(Identity, HeimaMultiSignature), // (Signer, Signature)
91+
Email(Email, VerificationCode),
92+
AuthToken(OmniAccount, JwtToken),
93+
OAuth2(Identity, OAuth2Data), // (Sender, OAuth2Data)
94+
}
95+
96+
#[derive(Deserialize)]
97+
pub enum OmniAuthSerde {
98+
Web3(IdentitySerde, HeimaMultiSignature),
99+
Email(Email, VerificationCode),
100+
AuthToken(OmniAccount, JwtToken),
101+
OAuth2(IdentitySerde, OAuth2Data),
12102
}
13103

14104
impl From<OmniAuth> for OmniAccountAuthType {
15105
fn from(value: OmniAuth) -> Self {
16106
match value {
17-
OmniAuth::Web3(_) => Self::Web3,
107+
OmniAuth::Web3(..) => Self::Web3,
18108
OmniAuth::Email(..) => Self::Email,
19-
OmniAuth::OAuth2(_) => Self::OAuth2,
20-
OmniAuth::AuthToken(_) => Self::AuthToken,
109+
OmniAuth::OAuth2(..) => Self::OAuth2,
110+
OmniAuth::AuthToken(..) => Self::AuthToken,
21111
}
22112
}
23113
}
24114

25-
#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq)]
115+
#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq, Deserialize)]
26116
pub enum OAuth2Provider {
27117
Google,
28118
}
29119

30-
#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq)]
120+
#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq, Deserialize)]
31121
pub struct OAuth2Data {
32122
pub provider: OAuth2Provider,
33123
pub code: String,
34124
pub state: String,
35125
pub redirect_uri: String,
36126
}
127+
128+
#[cfg(test)]
129+
mod tests {
130+
use super::*;
131+
132+
#[test]
133+
fn test_identity_serde() {
134+
let json = r#"{"type":"Twitter","data":"handle"}"#;
135+
let deserialized: IdentitySerde = serde_json::from_str(json).unwrap();
136+
let identity = Identity::try_from(deserialized).unwrap();
137+
138+
assert_eq!(identity, Identity::Twitter(IdentityString::new(b"handle".to_vec())));
139+
}
140+
}

tee-worker/omni-executor/executor-primitives/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub mod signature;
2626
pub mod utils;
2727
pub use heima_primitives::{
2828
identity::Address32, omni::*, teebag::DcapQuote, AccountId, BlockNumber, ChainAsset, Hash,
29-
Identity, IntentId, MrEnclave, Nonce, ShardIdentifier, Web2IdentityType,
29+
Hashable, Identity, IntentId, MrEnclave, Nonce, ShardIdentifier, Web2IdentityType,
3030
};
3131
use std::fmt::Debug;
3232

tee-worker/omni-executor/executor-primitives/src/signature/heima_multi_signature.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use tracing::log::error;
1818
#[derive(
1919
Encode, Decode, Clone, Debug, PartialEq, Eq, TypeInfo, MaxEncodedLen, Serialize, Deserialize,
2020
)]
21+
#[serde(tag = "type", content = "data")]
2122
pub enum HeimaMultiSignature {
2223
/// An Ed25519 signature.
2324
#[codec(index = 0)]
@@ -174,6 +175,8 @@ fn evm_eip191_wrap(msg: &[u8]) -> Vec<u8> {
174175

175176
#[cfg(test)]
176177
mod tests {
178+
use crate::utils::hex::hex_encode;
179+
177180
use super::*;
178181
use base64::{engine::general_purpose::STANDARD, Engine};
179182

@@ -214,4 +217,23 @@ mod tests {
214217
.verify(b"test message", &signer);
215218
assert!(result);
216219
}
220+
221+
#[test]
222+
fn test_heima_multi_signature_serde() {
223+
let raw_signature: [u8; 64] = [
224+
62, 25, 148, 186, 53, 137, 248, 174, 149, 187, 225, 24, 186, 48, 24, 109, 100, 27, 149,
225+
196, 66, 5, 222, 140, 22, 16, 136, 239, 154, 22, 133, 96, 79, 2, 180, 106, 150, 112,
226+
116, 11, 6, 35, 32, 4, 145, 240, 54, 130, 206, 193, 200, 57, 241, 112, 35, 122, 226,
227+
97, 174, 231, 221, 13, 98, 2,
228+
];
229+
230+
let hex_signature = hex_encode(raw_signature.as_slice());
231+
let json = format!(r#"{{"type":"Ed25519","data":"{}"}}"#, hex_signature);
232+
let deserialized: HeimaMultiSignature = serde_json::from_str(&json).unwrap();
233+
234+
assert_eq!(
235+
deserialized,
236+
HeimaMultiSignature::Ed25519(ed25519::Signature::from_raw(raw_signature))
237+
);
238+
}
217239
}

tee-worker/omni-executor/executor-worker/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ async fn main() -> Result<(), ()> {
292292
})
293293
.expect("Could not serialize shielding public key");
294294

295-
let mrenclave = perform_attestation(
295+
let _ = perform_attestation(
296296
parentchain_rpc_client_factory,
297297
parentchain_signer,
298298
tx_signer.clone(),
@@ -310,7 +310,6 @@ async fn main() -> Result<(), ()> {
310310
Arc::new(native_task_sender),
311311
pumpx_api,
312312
storage_db.clone(),
313-
mrenclave,
314313
jwt_rsa_private_key,
315314
)
316315
.await

tee-worker/omni-executor/heima/authentication/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ executor-crypto = { workspace = true }
1515
executor-primitives = { workspace = true }
1616

1717
[dev-dependencies]
18-
rand = { workspace = true }
1918
chrono = { workspace = true }
19+
rand = { workspace = true }
2020

2121
[lints]
2222
workspace = true

0 commit comments

Comments
 (0)