-
Notifications
You must be signed in to change notification settings - Fork 45
TON signing: include address, timestamp & hash #991
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
Merged
Changes from all commits
Commits
Show all changes
3 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| mod chain_signer; | ||
| mod signature; | ||
| #[cfg(test)] | ||
| pub(crate) mod testkit; | ||
| mod types; | ||
|
|
||
| pub use chain_signer::TonChainSigner; | ||
| pub use signature::sign_personal; | ||
| pub use types::{TonSignDataPayload, TonSignDataResponse, TonSignMessageData}; | ||
| pub use types::{TonSignDataPayload, TonSignDataResponse, TonSignMessageData, TonSignResult}; |
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 |
|---|---|---|
| @@ -1,41 +1,48 @@ | ||
| use primitives::SignerError; | ||
| use signer::Signer; | ||
|
|
||
| use super::types::TonSignMessageData; | ||
| use super::types::{TonSignMessageData, TonSignResult}; | ||
|
|
||
| pub fn sign_personal(data: &[u8], private_key: &[u8]) -> Result<(Vec<u8>, Vec<u8>), SignerError> { | ||
| pub fn sign_personal(data: &[u8], private_key: &[u8], timestamp: u64) -> Result<TonSignResult, SignerError> { | ||
| let ton_data = TonSignMessageData::from_bytes(data)?; | ||
| let digest = ton_data.payload.hash(); | ||
| let digest = ton_data.hash(timestamp)?; | ||
|
|
||
| Signer::sign_ed25519_with_public_key(&digest, private_key).map_err(|e| SignerError::InvalidInput(e.to_string())) | ||
| let (signature, public_key) = Signer::sign_ed25519_with_public_key(&digest, private_key).map_err(|e| SignerError::InvalidInput(e.to_string()))?; | ||
| Ok(TonSignResult { signature, public_key, timestamp }) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::signer::TonSignDataPayload; | ||
| use crate::signer::testkit::TEST_ADDRESS; | ||
|
|
||
| #[test] | ||
| fn test_sign_ton_personal() { | ||
| let payload = TonSignDataPayload::Text { text: "Hello TON".to_string() }; | ||
| let ton_data = TonSignMessageData::new(payload, "example.com".to_string()); | ||
| let ton_data = TonSignMessageData::new(payload, "example.com".to_string(), TEST_ADDRESS.to_string()); | ||
| let data = ton_data.to_bytes(); | ||
|
|
||
| let private_key = hex::decode("1e9d38b5274152a78dff1a86fa464ceadc1f4238ca2c17060c3c507349424a34").expect("valid hex"); | ||
| let private_key = hex::decode("1e9d38b5274152a78dff1a86fa464ceadc1f4238ca2c17060c3c507349424a34").unwrap(); | ||
| let timestamp = 1234567890u64; | ||
|
|
||
| let (signature, public_key) = sign_personal(&data, &private_key).expect("signing succeeds"); | ||
| let result = sign_personal(&data, &private_key, timestamp).unwrap(); | ||
|
|
||
| assert_eq!(signature.len(), 64, "Ed25519 signature should be 64 bytes"); | ||
| assert_eq!(public_key.len(), 32, "Ed25519 public key should be 32 bytes"); | ||
| assert_eq!( | ||
| hex::encode(&result.signature), | ||
| "3fe42db1d77534ba52d43240cf6b84b36eb1c53a28e3370c5872f37558cee9b758b9f93a8740c84ee4190b99de83901dcb9d5b42b1c7826b3836236ef5cd3a0f" | ||
| ); | ||
| assert_eq!(hex::encode(&result.public_key), "d369452197c2a56481e5e2d3e8bf03de2349f67a63151956822208c2334adee2"); | ||
| assert_eq!(result.timestamp, timestamp); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_sign_ton_personal_rejects_invalid_key() { | ||
| let payload = TonSignDataPayload::Text { text: "Hello TON".to_string() }; | ||
| let ton_data = TonSignMessageData::new(payload, "example.com".to_string()); | ||
| let ton_data = TonSignMessageData::new(payload, "example.com".to_string(), TEST_ADDRESS.to_string()); | ||
| let data = ton_data.to_bytes(); | ||
|
|
||
| let result = sign_personal(&data, &[0u8; 16]); | ||
| let result = sign_personal(&data, &[0u8; 16], 1234567890); | ||
| assert!(result.is_err()); | ||
| } | ||
| } |
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 @@ | ||
| pub const TEST_ADDRESS: &str = "UQBY1cVPu4SIr36q0M3HWcqPb_efyVVRBsEzmwN-wKQDR6zg"; |
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.
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.
The
Address::from_base64_urlfunction decodes a base64-encoded TON address but critically fails to validate the 2-byte CRC checksum, which is a standard part of the 36-byte TON user-friendly address format. This omission allows the application to accept malformed addresses or addresses with typos, potentially leading to signing messages for unintended accounts or displaying incorrect information. Beyond this, the function's namefrom_base64_urlsuggests it only handles URL-safe base64, but it falls back to decoding standard base64 if URL-safe decoding fails. This behavior, while robust, might obscure issues with data sources expected to provide URL-safe base64. If both formats are intentionally supported, this should be clearly documented; otherwise, the fallback should be removed to enforce the correct format. Remediation: Implement CRC16-CCITT validation to verify that the last 2 bytes of the decoded 36-byte array match the CRC16-CCITT checksum of the first 34 bytes.