-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Sui]: Support Sui sign personal message #4223
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
Merged
satoshiotomakan
merged 4 commits into
trustwallet:master
from
10gic:support-sui-sign-personal-message
Jan 20, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| // Copyright © 2017 Trust Wallet. | ||
|
|
||
| use serde::Serialize; | ||
| use serde_repr::Serialize_repr; | ||
|
|
||
| // Code snippets from: | ||
| // https://github.com/MystenLabs/sui/blob/a16c942b72c13f42846b3c543b6622af85a5f634/crates/shared-crypto/src/intent.rs | ||
|
|
||
| /// This enums specifies the intent scope. | ||
| #[derive(Serialize_repr)] | ||
| #[repr(u8)] | ||
| pub enum IntentScope { | ||
| /// Used for a user signature on a transaction data. | ||
| TransactionData = 0, | ||
| /// Used for a user signature on a personal message. | ||
| PersonalMessage = 3, | ||
| } | ||
|
|
||
| /// The version here is to distinguish between signing different versions of the struct | ||
| /// or enum. Serialized output between two different versions of the same struct/enum | ||
| /// might accidentally (or maliciously on purpose) match. | ||
| #[derive(Serialize_repr)] | ||
| #[repr(u8)] | ||
| pub enum IntentVersion { | ||
| V0 = 0, | ||
| } | ||
|
|
||
| /// This enums specifies the application ID. Two intents in two different applications | ||
| /// (i.e., Narwhal, Sui, Ethereum etc) should never collide, so that even when a signing | ||
| /// key is reused, nobody can take a signature designated for app_1 and present it as a | ||
| /// valid signature for an (any) intent in app_2. | ||
| #[derive(Serialize_repr)] | ||
| #[repr(u8)] | ||
| pub enum AppId { | ||
| Sui = 0, | ||
| } | ||
|
|
||
| /// An intent is a compact struct serves as the domain separator for a message that a signature commits to. | ||
| /// It consists of three parts: [enum IntentScope] (what the type of the message is), | ||
| /// [enum IntentVersion], [enum AppId] (what application that the signature refers to). | ||
| /// It is used to construct [struct IntentMessage] that what a signature commits to. | ||
| /// | ||
| /// The serialization of an Intent is a 3-byte array where each field is represented by a byte. | ||
| #[derive(Serialize)] | ||
| pub struct Intent { | ||
| pub scope: IntentScope, | ||
| pub version: IntentVersion, | ||
| pub app_id: AppId, | ||
| } | ||
|
|
||
| impl Intent { | ||
| pub fn sui_transaction() -> Self { | ||
| Self { | ||
| scope: IntentScope::TransactionData, | ||
| version: IntentVersion::V0, | ||
| app_id: AppId::Sui, | ||
| } | ||
| } | ||
|
|
||
| pub fn personal_message() -> Self { | ||
| Self { | ||
| scope: IntentScope::PersonalMessage, | ||
| version: IntentVersion::V0, | ||
| app_id: AppId::Sui, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Intent Message is a wrapper around a message with its intent. The message can | ||
| /// be any type that implements [trait Serialize]. *ALL* signatures in Sui must commits | ||
| /// to the intent message, not the message itself. This guarantees any intent | ||
| /// message signed in the system cannot collide with another since they are domain | ||
| /// separated by intent. | ||
| /// | ||
| /// The serialization of an IntentMessage is compact: it only appends three bytes | ||
| /// to the message itself. | ||
| #[derive(Serialize)] | ||
| pub struct IntentMessage<T> { | ||
| pub intent: Intent, | ||
| pub value: T, | ||
| } | ||
|
|
||
| impl<T> IntentMessage<T> { | ||
| pub fn new(intent: Intent, value: T) -> Self { | ||
| Self { intent, value } | ||
| } | ||
| } | ||
|
|
||
| /// A person message that wraps around a byte array. | ||
| #[derive(Serialize)] | ||
| pub struct PersonalMessage { | ||
| pub message: Vec<u8>, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| // Copyright © 2017 Trust Wallet. | ||
|
|
||
| use crate::modules::intent::{Intent, IntentMessage, PersonalMessage}; | ||
| use crate::signature::SuiSignatureInfo; | ||
| use tw_coin_entry::coin_context::CoinContext; | ||
| use tw_coin_entry::error::prelude::*; | ||
| use tw_coin_entry::modules::message_signer::MessageSigner; | ||
| use tw_coin_entry::signing_output_error; | ||
| use tw_encoding::bcs; | ||
| use tw_hash::blake2::blake2_b; | ||
| use tw_hash::H256; | ||
| use tw_keypair::ed25519; | ||
| use tw_keypair::traits::{KeyPairTrait, SigningKeyTrait, VerifyingKeyTrait}; | ||
| use tw_memory::Data; | ||
| use tw_misc::traits::ToBytesVec; | ||
| use tw_misc::try_or_false; | ||
| use tw_proto::Sui::Proto; | ||
| use tw_proto::TxCompiler::Proto as CompilerProto; | ||
|
|
||
| pub struct SuiMessageSigner; | ||
|
|
||
| /// Sui personal message signer. | ||
| /// Here is an example of how to sign a message: | ||
| /// https://github.com/MystenLabs/sui/blob/a16c942b72c13f42846b3c543b6622af85a5f634/crates/sui-types/src/unit_tests/utils.rs#L201 | ||
| impl SuiMessageSigner { | ||
| pub fn sign_message_impl( | ||
| _coin: &dyn CoinContext, | ||
| input: Proto::MessageSigningInput, | ||
| ) -> SigningResult<Proto::MessageSigningOutput<'static>> { | ||
| let key_pair = ed25519::sha512::KeyPair::try_from(input.private_key.as_ref())?; | ||
|
|
||
| let hash = Self::message_preimage_hashes_impl(input.message.as_bytes().into())?; | ||
|
|
||
| let signature = key_pair.sign(hash.to_vec())?; | ||
| let signature_info = SuiSignatureInfo::ed25519(&signature, key_pair.public()); | ||
|
|
||
| Ok(Proto::MessageSigningOutput { | ||
| signature: signature_info.to_base64().into(), | ||
| ..Proto::MessageSigningOutput::default() | ||
| }) | ||
| } | ||
|
|
||
| pub fn message_preimage_hashes_impl(message: Data) -> SigningResult<H256> { | ||
| let data = PersonalMessage { message }; | ||
| let intent_msg = IntentMessage::new(Intent::personal_message(), data); | ||
|
|
||
| let data_to_sign = bcs::encode(&intent_msg).tw_err(|_| SigningErrorType::Error_internal)?; | ||
|
|
||
| let data_to_sign = blake2_b(&data_to_sign, H256::LEN) | ||
| .and_then(|hash| H256::try_from(hash.as_slice())) | ||
| .tw_err(|_| SigningErrorType::Error_internal)?; | ||
|
|
||
| Ok(data_to_sign) | ||
| } | ||
| } | ||
|
|
||
| impl MessageSigner for SuiMessageSigner { | ||
| type MessageSigningInput<'a> = Proto::MessageSigningInput<'a>; | ||
| type MessagePreSigningOutput = CompilerProto::PreSigningOutput<'static>; | ||
| type MessageSigningOutput = Proto::MessageSigningOutput<'static>; | ||
| type MessageVerifyingInput<'a> = Proto::MessageVerifyingInput<'a>; | ||
|
|
||
| fn message_preimage_hashes( | ||
| &self, | ||
| _coin: &dyn CoinContext, | ||
| input: Self::MessageSigningInput<'_>, | ||
| ) -> Self::MessagePreSigningOutput { | ||
| let hash = match Self::message_preimage_hashes_impl(input.message.as_bytes().into()) { | ||
| Ok(hash) => hash, | ||
| Err(e) => return signing_output_error!(CompilerProto::PreSigningOutput, e), | ||
| }; | ||
|
|
||
| CompilerProto::PreSigningOutput { | ||
| data: hash.to_vec().into(), | ||
| data_hash: hash.to_vec().into(), | ||
| ..CompilerProto::PreSigningOutput::default() | ||
| } | ||
| } | ||
|
|
||
| fn sign_message( | ||
| &self, | ||
| coin: &dyn CoinContext, | ||
| input: Self::MessageSigningInput<'_>, | ||
| ) -> Self::MessageSigningOutput { | ||
| Self::sign_message_impl(coin, input) | ||
| .unwrap_or_else(|e| signing_output_error!(Proto::MessageSigningOutput, e)) | ||
| } | ||
|
|
||
| fn verify_message( | ||
| &self, | ||
| _coin: &dyn CoinContext, | ||
| input: Self::MessageVerifyingInput<'_>, | ||
| ) -> bool { | ||
| let signature_info = try_or_false!(SuiSignatureInfo::from_base64(input.signature.as_ref())); | ||
| let public_key = try_or_false!(ed25519::sha512::PublicKey::try_from( | ||
| input.public_key.as_ref() | ||
| )); | ||
|
|
||
| // Check if the public key in the signature matches the public key in the input. | ||
| if signature_info.public_key.ne(&public_key.to_bytes()) { | ||
| return false; | ||
| } | ||
| let signature = try_or_false!(ed25519::Signature::try_from( | ||
| signature_info.signature.as_slice() | ||
| )); | ||
| let hash = try_or_false!(Self::message_preimage_hashes_impl( | ||
| input.message.as_bytes().to_vec() | ||
| )); | ||
|
|
||
| // Verify the signature. | ||
| public_key.verify(signature, hash.to_vec()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.