Skip to content

Commit

Permalink
feat: P-503 added identity Solana, and web3Network Solana (#2539)
Browse files Browse the repository at this point in the history
* feat: P-503 added identity Solana, and web3Network Solana

* fix clippy

* fix test

---------

Co-authored-by: higherordertech <higherordertech>
  • Loading branch information
higherordertech committed Mar 4, 2024
1 parent f6a6c1e commit f1715e0
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ incremental = false
async-trait = { version = "0.1.76" }
assert_matches = { version = "1.3.0" }
blake2-rfc = { version = "0.2.18", default-features = false }
base58 = { version = "0.2", default-features = false }
base64 = { version = "0.13", default-features = false, features = ["alloc"] }
chrono = { version = "0.4", default-features = false, features = ["serde"] }
clap = { version = "4.3", features = ["derive"] }
Expand Down
4 changes: 4 additions & 0 deletions bitacross-worker/litentry/primitives/src/validation_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub enum Web3ValidationData {
Evm(Web3CommonValidationData),
#[codec(index = 2)]
Bitcoin(Web3CommonValidationData),
#[codec(index = 3)]
Solana(Web3CommonValidationData),
}

impl Web3ValidationData {
Expand All @@ -74,6 +76,7 @@ impl Web3ValidationData {
Self::Substrate(data) => &data.message,
Self::Evm(data) => &data.message,
Self::Bitcoin(data) => &data.message,
Self::Solana(data) => &data.message,
}
}

Expand All @@ -82,6 +85,7 @@ impl Web3ValidationData {
Self::Substrate(data) => &data.signature,
Self::Evm(data) => &data.signature,
Self::Bitcoin(data) => &data.signature,
Self::Solana(data) => &data.signature,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions primitives/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ name = 'core-primitives'
version = '0.9.12'

[dependencies]
base58 = { workspace = true }
parity-scale-codec = { workspace = true }
serde = { workspace = true }
strum = { workspace = true }
Expand Down
72 changes: 67 additions & 5 deletions primitives/core/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
pub extern crate alloc;

use crate::{
all_bitcoin_web3networks, all_evm_web3networks, all_substrate_web3networks, AccountId,
Web3Network,
all_bitcoin_web3networks, all_evm_web3networks, all_solana_web3networks,
all_substrate_web3networks, AccountId, Web3Network,
};
use alloc::{format, str, string::String};
use base58::{FromBase58, ToBase58};
use core::fmt::{Debug, Formatter};
use litentry_hex_utils::{decode_hex, hex_encode};
use litentry_macros::if_production_or;
Expand Down Expand Up @@ -288,6 +289,9 @@ pub enum Identity {
// e.g. unisat-wallet: https://docs.unisat.io/dev/unisat-developer-service/unisat-wallet#getpublickey
#[codec(index = 5)]
Bitcoin(Address33),

#[codec(index = 6)]
Solana(Address32),
}

impl Identity {
Expand All @@ -296,7 +300,7 @@ impl Identity {
}

pub fn is_web3(&self) -> bool {
matches!(self, Self::Substrate(..) | Self::Evm(..) | Self::Bitcoin(..))
matches!(self, Self::Substrate(..) | Self::Evm(..) | Self::Bitcoin(..) | Self::Solana(..))
}

pub fn is_substrate(&self) -> bool {
Expand All @@ -311,11 +315,16 @@ impl Identity {
matches!(self, Self::Bitcoin(..))
}

pub fn is_solana(&self) -> bool {
matches!(self, Self::Solana(..))
}

pub fn default_web3networks(&self) -> Vec<Web3Network> {
match self {
Identity::Substrate(_) => all_substrate_web3networks(),
Identity::Evm(_) => all_evm_web3networks(),
Identity::Bitcoin(_) => all_bitcoin_web3networks(),
Identity::Solana(_) => all_solana_web3networks(),
Identity::Twitter(_) | Identity::Discord(_) | Identity::Github(_) => Vec::new(),
}
}
Expand All @@ -327,6 +336,7 @@ impl Identity {
!networks.is_empty() && networks.iter().all(|n| n.is_substrate()),
Identity::Evm(_) => !networks.is_empty() && networks.iter().all(|n| n.is_evm()),
Identity::Bitcoin(_) => !networks.is_empty() && networks.iter().all(|n| n.is_bitcoin()),
Identity::Solana(_) => !networks.is_empty() && networks.iter().all(|n| n.is_solana()),
Identity::Twitter(_) | Identity::Discord(_) | Identity::Github(_) =>
networks.is_empty(),
}
Expand All @@ -335,7 +345,7 @@ impl Identity {
/// Currently we only support mapping from Address32/Address20 to AccountId, not opposite.
pub fn to_account_id(&self) -> Option<AccountId> {
match self {
Identity::Substrate(address) => Some(address.into()),
Identity::Substrate(address) | Identity::Solana(address) => Some(address.into()),
Identity::Evm(address) =>
Some(HashedAddressMapping::into_account_id(H160::from_slice(address.as_ref()))),
Identity::Bitcoin(address) => Some(blake2_256(address.as_ref()).into()),
Expand Down Expand Up @@ -370,6 +380,14 @@ impl Identity {
.try_into()
.map_err(|_| "Address33 conversion error")?;
return Ok(Identity::Bitcoin(handle))
} else if v[0] == "solana" {
let handle = v[1]
.from_base58()
.unwrap()
.as_slice()
.try_into()
.map_err(|_| "Address32 conversion error")?;
return Ok(Identity::Solana(handle))
} else if v[0] == "github" {
return Ok(Identity::Github(IdentityString::new(v[1].as_bytes().to_vec())))
} else if v[0] == "discord" {
Expand All @@ -395,6 +413,7 @@ impl Identity {
Identity::Substrate(address) =>
format!("substrate:{}", &hex_encode(address.as_ref())),
Identity::Bitcoin(address) => format!("bitcoin:{}", &hex_encode(address.as_ref())),
Identity::Solana(address) => format!("solana:{}", address.as_ref().to_base58()),
Identity::Twitter(handle) => format!(
"twitter:{}",
str::from_utf8(handle.inner_ref())
Expand Down Expand Up @@ -488,6 +507,7 @@ mod tests {
Identity::Substrate(..) => false,
Identity::Evm(..) => false,
Identity::Bitcoin(..) => false,
Identity::Solana(..) => false,
}
)
})
Expand All @@ -505,6 +525,7 @@ mod tests {
Identity::Substrate(..) => true,
Identity::Evm(..) => true,
Identity::Bitcoin(..) => true,
Identity::Solana(..) => true,
}
)
})
Expand All @@ -522,6 +543,7 @@ mod tests {
Identity::Substrate(..) => true,
Identity::Evm(..) => false,
Identity::Bitcoin(..) => false,
Identity::Solana(..) => false,
}
)
})
Expand All @@ -539,6 +561,7 @@ mod tests {
Identity::Substrate(..) => false,
Identity::Evm(..) => true,
Identity::Bitcoin(..) => false,
Identity::Solana(..) => false,
}
)
})
Expand All @@ -556,6 +579,25 @@ mod tests {
Identity::Substrate(..) => false,
Identity::Evm(..) => false,
Identity::Bitcoin(..) => true,
Identity::Solana(..) => false,
}
)
})
}

#[test]
fn is_solana_works() {
Identity::iter().for_each(|identity| {
assert_eq!(
identity.is_solana(),
match identity {
Identity::Twitter(..) => false,
Identity::Discord(..) => false,
Identity::Github(..) => false,
Identity::Substrate(..) => false,
Identity::Evm(..) => false,
Identity::Bitcoin(..) => false,
Identity::Solana(..) => true,
}
)
})
Expand Down Expand Up @@ -587,6 +629,17 @@ mod tests {
assert!(!id.matches_web3networks(&networks));
networks = vec![Web3Network::Bsc, Web3Network::Ethereum];
assert!(id.matches_web3networks(&networks));

// solana identity
id = Identity::Solana(Default::default());
networks = vec![];
assert!(!id.matches_web3networks(&networks));
networks = vec![Web3Network::Bsc, Web3Network::Litentry];
assert!(!id.matches_web3networks(&networks));
networks = vec![Web3Network::Bsc, Web3Network::Ethereum];
assert!(!id.matches_web3networks(&networks));
networks = vec![Web3Network::Solana];
assert!(id.matches_web3networks(&networks));
}

#[test]
Expand Down Expand Up @@ -646,11 +699,20 @@ mod tests {
}

#[test]

fn test_github_did() {
let identity = Identity::Github(IdentityString::new("github_handle".as_bytes().to_vec()));
let did_str = "did:litentry:github:github_handle";
assert_eq!(identity.to_did().unwrap(), did_str);
assert_eq!(Identity::from_did(did_str).unwrap(), identity);
}

#[test]
fn test_solana_did() {
let address = "4fuUiYxTQ6QCrdSq9ouBYcTM7bqSwYTSyLueGZLTy4T4";
let identity =
Identity::Solana(address.from_base58().unwrap().as_slice().try_into().unwrap());
let did = format!("did:litentry:solana:{}", address);
assert_eq!(identity.to_did().unwrap(), did.as_str());
assert_eq!(Identity::from_did(did.as_str()).unwrap(), identity);
}
}
42 changes: 42 additions & 0 deletions primitives/core/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ pub enum Web3Network {
Polygon,
#[codec(index = 15)]
Arbitrum,
// solana
#[codec(index = 16)]
Solana,
}

// mainly used in CLI
Expand Down Expand Up @@ -133,6 +136,10 @@ impl Web3Network {
Self::BitcoinP2wsh
)
}

pub fn is_solana(&self) -> bool {
matches!(self, Self::Solana)
}
}

pub fn all_web3networks() -> Vec<Web3Network> {
Expand All @@ -151,6 +158,10 @@ pub fn all_bitcoin_web3networks() -> Vec<Web3Network> {
Web3Network::iter().filter(|n| n.is_bitcoin()).collect()
}

pub fn all_solana_web3networks() -> Vec<Web3Network> {
Web3Network::iter().filter(|n| n.is_solana()).collect()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -183,6 +194,7 @@ mod tests {
Web3Network::BitcoinP2wsh => false,
Web3Network::Polygon => true,
Web3Network::Arbitrum => true,
Web3Network::Solana => false,
}
)
})
Expand Down Expand Up @@ -210,6 +222,7 @@ mod tests {
Web3Network::BitcoinP2wsh => false,
Web3Network::Polygon => false,
Web3Network::Arbitrum => false,
Web3Network::Solana => false,
}
)
})
Expand Down Expand Up @@ -237,6 +250,35 @@ mod tests {
Web3Network::BitcoinP2wsh => true,
Web3Network::Polygon => false,
Web3Network::Arbitrum => false,
Web3Network::Solana => false,
}
)
})
}

#[test]
fn is_solana_works() {
Web3Network::iter().for_each(|network| {
assert_eq!(
network.is_solana(),
match network {
Web3Network::Polkadot => false,
Web3Network::Kusama => false,
Web3Network::Litentry => false,
Web3Network::Litmus => false,
Web3Network::LitentryRococo => false,
Web3Network::Khala => false,
Web3Network::SubstrateTestnet => false,
Web3Network::Ethereum => false,
Web3Network::Bsc => false,
Web3Network::BitcoinP2tr => false,
Web3Network::BitcoinP2pkh => false,
Web3Network::BitcoinP2sh => false,
Web3Network::BitcoinP2wpkh => false,
Web3Network::BitcoinP2wsh => false,
Web3Network::Polygon => false,
Web3Network::Arbitrum => false,
Web3Network::Solana => true,
}
)
})
Expand Down
1 change: 1 addition & 0 deletions tee-worker/Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default {
Substrate: "Address32",
Evm: "Address20",
Bitcoin: "Address33",
Solana: "Address32",
},
},
Address32: "[u8;32]",
Expand All @@ -51,6 +52,7 @@ export default {
"BitcoinP2wsh",
"Polygon",
"Arbitrum",
"Solana",
],
},
LitentryValidationData: {
Expand Down Expand Up @@ -78,6 +80,7 @@ export default {
Substrate: "Web3CommonValidationData",
Evm: "Web3CommonValidationData",
Bitcoin: "Web3CommonValidationData",
Solana: "Web3CommonValidationData",
},
},
Web3CommonValidationData: {
Expand Down
7 changes: 7 additions & 0 deletions tee-worker/enclave-runtime/Cargo.lock

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

3 changes: 2 additions & 1 deletion tee-worker/litentry/core/assertion-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ fn pubkey_to_address(network: &Web3Network, pubkey: &str) -> String {
| Web3Network::Ethereum
| Web3Network::Bsc
| Web3Network::Polygon
| Web3Network::Arbitrum => "".to_string(),
| Web3Network::Arbitrum
| Web3Network::Solana => "".to_string(),
}
}

Expand Down

0 comments on commit f1715e0

Please sign in to comment.