Skip to content
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

fix: parse sui personal message #143

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions rust/apps/sui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ extern crate core;
#[macro_use]
extern crate std;

use core::str::FromStr;

use crate::{errors::Result, types::intent::IntentScope};
use alloc::{
string::{String, ToString},
Expand Down Expand Up @@ -56,8 +58,21 @@ pub fn parse_intent(intent: &[u8]) -> Result<Intent> {
Ok(Intent::TransactionData(tx))
}
IntentScope::PersonalMessage => {
let msg: IntentMessage<PersonalMessage> =
bcs::from_bytes(&intent).map_err(|err| SuiError::from(err))?;
let msg: IntentMessage<PersonalMessage> = match bcs::from_bytes(&intent) {
Ok(msg) => msg,
Err(_) => {
if intent.len() < 4 {
return Err(SuiError::InvalidData(String::from("message too short")));
}
let intent_bytes = intent[..3].to_vec();
IntentMessage::<PersonalMessage>::new(
types::intent::Intent::from_str(hex::encode(&intent_bytes).as_str())?,
PersonalMessage {
message: intent[3..].to_vec(),
},
)
}
};
let m = match decode_utf8(&msg.value.message) {
Ok(m) => m,
Err(_) => serde_json::to_string(&msg.value.message)?,
Expand Down Expand Up @@ -145,6 +160,14 @@ mod tests {
assert_eq!(json!(intent.unwrap()).to_string(), "{\"PersonalMessage\":{\"intent\":{\"app_id\":\"Sui\",\"scope\":\"PersonalMessage\",\"version\":\"V0\"},\"value\":{\"message\":\"Hello, Sui\"}}}");
}

#[test]
fn test_parse_msg_not_bcs() {
let bytes = hex::decode("030000506C656173652C20766572696679206F776E657273686970206279207369676E696E672074686973206D6573736167652E").unwrap();

let intent = parse_intent(&bytes);
assert_eq!(json!(intent.unwrap()).to_string(), "{\"PersonalMessage\":{\"intent\":{\"app_id\":\"Sui\",\"scope\":\"PersonalMessage\",\"version\":\"V0\"},\"value\":{\"message\":\"Please, verify ownership by signing this message.\"}}}");
}

#[test]
fn test_decode_utf8() {
let bytes = hex::decode("48656c6c6f2c20537569").unwrap();
Expand Down
Loading