-
Notifications
You must be signed in to change notification settings - Fork 38
Add TON WalletConnect support #826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ pub enum WalletConnectCAIP2 { | |
| Algorand, | ||
| #[serde(rename = "sui")] | ||
| Sui, | ||
| #[serde(rename = "ton")] | ||
| Ton, | ||
| } | ||
|
|
||
| impl WalletConnectCAIP2 { | ||
|
|
@@ -27,8 +29,8 @@ impl WalletConnectCAIP2 { | |
| ChainType::Cosmos => Some(format!("{}:{}", WalletConnectCAIP2::Cosmos.as_ref(), chain.network_id())), | ||
| ChainType::Algorand => Some(WalletConnectCAIP2::Algorand.as_ref().to_string()), | ||
| ChainType::Sui => Some(WalletConnectCAIP2::Sui.as_ref().to_string()), | ||
| ChainType::Ton => Some(WalletConnectCAIP2::Ton.as_ref().to_string()), | ||
| ChainType::Bitcoin | ||
| | ChainType::Ton | ||
| | ChainType::Tron | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the definition of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ton mainnet chain id https://docs.reown.com/advanced/multichain/rpc-reference/ton-rpc |
||
| | ChainType::Aptos | ||
| | ChainType::Xrp | ||
|
|
@@ -47,6 +49,7 @@ impl WalletConnectCAIP2 { | |
| WalletConnectCAIP2::Cosmos => Some(ChainType::Cosmos), | ||
| WalletConnectCAIP2::Algorand => Some(ChainType::Algorand), | ||
| WalletConnectCAIP2::Sui => Some(ChainType::Sui), | ||
| WalletConnectCAIP2::Ton => Some(ChainType::Ton), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -65,6 +68,7 @@ impl WalletConnectCAIP2 { | |
| WalletConnectCAIP2::Solana => Some(Chain::Solana), | ||
| WalletConnectCAIP2::Algorand => Some(Chain::Algorand), | ||
| WalletConnectCAIP2::Sui => Some(Chain::Sui), | ||
| WalletConnectCAIP2::Ton => Some(Chain::Ton), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -75,8 +79,8 @@ impl WalletConnectCAIP2 { | |
| ChainType::Cosmos => Self::get_namespace(chain).map(|namespace| format!("{}:{}", namespace, chain.network_id())), | ||
| ChainType::Algorand => Some("wGHE2Pwdvd7S12BL5FaOP20EGYesN73k".to_string()), | ||
| ChainType::Sui => Some("mainnet".to_string()), | ||
| ChainType::Ton => Some("-239".to_string()), | ||
| ChainType::Bitcoin | ||
| | ChainType::Ton | ||
| | ChainType::Tron | ||
| | ChainType::Aptos | ||
| | ChainType::Xrp | ||
|
|
@@ -114,6 +118,7 @@ mod tests { | |
| assert_eq!(WalletConnectCAIP2::get_chain_type("cosmos".to_string()), Some(ChainType::Cosmos)); | ||
| assert_eq!(WalletConnectCAIP2::get_chain_type("algorand".to_string()), Some(ChainType::Algorand)); | ||
| assert_eq!(WalletConnectCAIP2::get_chain_type("sui".to_string()), Some(ChainType::Sui)); | ||
| assert_eq!(WalletConnectCAIP2::get_chain_type("ton".to_string()), Some(ChainType::Ton)); | ||
| assert_eq!(WalletConnectCAIP2::get_chain_type("unknown".to_string()), None); | ||
| } | ||
|
|
||
|
|
@@ -123,6 +128,7 @@ mod tests { | |
| assert_eq!(WalletConnectCAIP2::get_chain("eip155".to_string(), "56".to_string()), Some(Chain::SmartChain)); | ||
| assert_eq!(WalletConnectCAIP2::get_chain("solana".to_string(), "ignored".to_string()), Some(Chain::Solana)); | ||
| assert_eq!(WalletConnectCAIP2::get_chain("sui".to_string(), "mainnet".to_string()), Some(Chain::Sui)); | ||
| assert_eq!(WalletConnectCAIP2::get_chain("ton".to_string(), "-239".to_string()), Some(Chain::Ton)); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
@@ -133,9 +139,20 @@ mod tests { | |
| Ok(Chain::Solana) | ||
| ); | ||
| assert_eq!(WalletConnectCAIP2::resolve_chain(Some("sui:mainnet".to_string())), Ok(Chain::Sui)); | ||
| assert_eq!(WalletConnectCAIP2::resolve_chain(Some("ton:-239".to_string())), Ok(Chain::Ton)); | ||
| assert!(WalletConnectCAIP2::resolve_chain(Some("invalid".to_string())).is_err()); | ||
| assert!(WalletConnectCAIP2::resolve_chain(Some("eip155:1:extra".to_string())).is_err()); | ||
| assert!(WalletConnectCAIP2::resolve_chain(None).is_err()); | ||
| assert!(WalletConnectCAIP2::resolve_chain(Some("unknown:chain".to_string())).is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_namespace_ton() { | ||
| assert_eq!(WalletConnectCAIP2::get_namespace(Chain::Ton), Some("ton".to_string())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_reference_ton() { | ||
| assert_eq!(WalletConnectCAIP2::get_reference(Chain::Ton), Some("-239".to_string())); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| use std::str::FromStr; | ||
|
|
||
| use serde::{Deserialize, Serialize}; | ||
| use strum::{AsRefStr, EnumString}; | ||
|
|
||
| use crate::error::SignerError; | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, AsRefStr, EnumString)] | ||
| #[strum(serialize_all = "lowercase")] | ||
| pub enum TonSignDataType { | ||
| Text, | ||
| Binary, | ||
| Cell, | ||
| } | ||
|
|
||
| impl TonSignDataType { | ||
DRadmir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pub fn data_field(&self) -> &'static str { | ||
| match self { | ||
| TonSignDataType::Text => "text", | ||
| TonSignDataType::Binary => "bytes", | ||
| TonSignDataType::Cell => "cell", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
| struct TonSignDataPayloadRaw { | ||
| #[serde(rename = "type")] | ||
| payload_type: String, | ||
| text: Option<String>, | ||
| bytes: Option<String>, | ||
| cell: Option<String>, | ||
| } | ||
|
|
||
| pub struct TonSignDataPayload { | ||
| pub payload_type: TonSignDataType, | ||
| pub data: String, | ||
| } | ||
|
|
||
| impl TonSignDataPayload { | ||
| pub fn parse(json: &str) -> Result<Self, SignerError> { | ||
| let raw: TonSignDataPayloadRaw = serde_json::from_str(json)?; | ||
|
|
||
| let payload_type = TonSignDataType::from_str(&raw.payload_type).map_err(|_| SignerError::new(format!("Unknown payload type: {}", raw.payload_type)))?; | ||
|
|
||
| let data = match payload_type { | ||
| TonSignDataType::Text => raw.text.ok_or("Missing text field")?, | ||
| TonSignDataType::Binary => raw.bytes.ok_or("Missing bytes field")?, | ||
| TonSignDataType::Cell => raw.cell.ok_or("Missing cell field")?, | ||
| }; | ||
|
|
||
| Ok(Self { payload_type, data }) | ||
| } | ||
|
|
||
| pub fn hash(&self) -> Vec<u8> { | ||
| self.data.as_bytes().to_vec() | ||
| } | ||
|
|
||
| pub fn to_json(&self) -> serde_json::Value { | ||
| serde_json::json!({ | ||
| "type": self.payload_type.as_ref(), | ||
| self.payload_type.data_field(): self.data, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Serialize)] | ||
| pub struct TonSignDataResponse { | ||
| signature: String, | ||
| #[serde(rename = "publicKey")] | ||
| public_key: String, | ||
| timestamp: u64, | ||
| domain: String, | ||
| payload: serde_json::Value, | ||
| } | ||
|
|
||
| impl TonSignDataResponse { | ||
| pub fn new(signature: String, public_key: String, timestamp: u64, domain: String, payload: serde_json::Value) -> Self { | ||
| Self { | ||
| signature, | ||
| public_key, | ||
| timestamp, | ||
| domain, | ||
| payload, | ||
| } | ||
| } | ||
|
|
||
| pub fn to_json(&self) -> Result<String, SignerError> { | ||
| serde_json::to_string(self).map_err(SignerError::from) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_parse_payload_text() { | ||
| let json = r#"{"type":"text","text":"Hello TON"}"#; | ||
| let parsed = TonSignDataPayload::parse(json).unwrap(); | ||
|
|
||
| assert_eq!(parsed.payload_type, TonSignDataType::Text); | ||
| assert_eq!(parsed.data, "Hello TON"); | ||
| assert_eq!(parsed.hash(), b"Hello TON".to_vec()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_parse_payload_binary() { | ||
| let json = r#"{"type":"binary","bytes":"SGVsbG8="}"#; | ||
| let parsed = TonSignDataPayload::parse(json).unwrap(); | ||
|
|
||
| assert_eq!(parsed.payload_type, TonSignDataType::Binary); | ||
| assert_eq!(parsed.data, "SGVsbG8="); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_parse_payload_cell() { | ||
| let json = r#"{"type":"cell","cell":"te6c"}"#; | ||
| let parsed = TonSignDataPayload::parse(json).unwrap(); | ||
|
|
||
| assert_eq!(parsed.payload_type, TonSignDataType::Cell); | ||
| assert_eq!(parsed.data, "te6c"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_payload_to_json() { | ||
| let payload = TonSignDataPayload { | ||
| payload_type: TonSignDataType::Text, | ||
| data: "Hello TON".to_string(), | ||
| }; | ||
|
|
||
| let json = payload.to_json(); | ||
| assert_eq!(json["type"], "text"); | ||
| assert_eq!(json["text"], "Hello TON"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_response_to_json() { | ||
| let payload = TonSignDataPayload { | ||
| payload_type: TonSignDataType::Text, | ||
| data: "Hello TON".to_string(), | ||
| }; | ||
|
|
||
| let response = TonSignDataResponse::new( | ||
| "c2lnbmF0dXJl".to_string(), | ||
| "cHVibGljS2V5".to_string(), | ||
| 1234567890, | ||
| "example.com".to_string(), | ||
| payload.to_json(), | ||
| ); | ||
|
|
||
| let json = response.to_json().unwrap(); | ||
| let parsed: serde_json::Value = serde_json::from_str(&json).unwrap(); | ||
|
|
||
| assert_eq!(parsed["signature"], "c2lnbmF0dXJl"); | ||
| assert_eq!(parsed["publicKey"], "cHVibGljS2V5"); | ||
| assert_eq!(parsed["timestamp"], 1234567890); | ||
| assert_eq!(parsed["domain"], "example.com"); | ||
| assert_eq!(parsed["payload"]["type"], "text"); | ||
| assert_eq!(parsed["payload"]["text"], "Hello TON"); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better usability and consistency with other data structures in the crate, consider deriving
CloneandPartialEqforWCTonMessage. This will make it easier to use in tests and other logic where copying or comparison is needed.