-
Notifications
You must be signed in to change notification settings - Fork 42
Support nested Tron transactions from WalletConnect #958
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
Open
0xh3rman
wants to merge
4
commits into
main
Choose a base branch
from
tron-wc-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+162
−90
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,69 +1,81 @@ | ||
| use super::transaction::{TronPayload, TronTransaction}; | ||
| use gem_hash::sha2::sha256; | ||
| use primitives::{ChainSigner, SignerError, TransactionLoadInput, TransferDataOutputAction, TransferDataOutputType, hex::decode_hex}; | ||
| use primitives::{ChainSigner, SignerError, TransactionLoadInput, TransferDataOutputType, hex::decode_hex}; | ||
| use serde_json::Value; | ||
| use signer::{SignatureScheme, Signer}; | ||
|
|
||
| struct PayloadMetadata { | ||
| enum PayloadFormat { | ||
| V1, | ||
| Legacy, | ||
| } | ||
|
|
||
| struct TronPayload { | ||
| payload: Value, | ||
| format: PayloadFormat, | ||
| output_type: TransferDataOutputType, | ||
| output_action: TransferDataOutputAction, | ||
| } | ||
|
|
||
| pub struct TronChainSigner; | ||
| impl TronPayload { | ||
| fn parse(input: &TransactionLoadInput) -> Result<Self, SignerError> { | ||
| let extra = input.get_data_extra().map_err(SignerError::invalid_input)?; | ||
| let data = extra.data.as_ref().ok_or_else(|| SignerError::invalid_input("Missing transaction data"))?; | ||
| let payload: Value = serde_json::from_slice(data)?; | ||
|
|
||
| impl ChainSigner for TronChainSigner { | ||
| fn sign_data(&self, input: &TransactionLoadInput, private_key: &[u8]) -> Result<String, SignerError> { | ||
| sign_data(input, private_key) | ||
| } | ||
| } | ||
| let transaction = payload | ||
| .get("transaction") | ||
| .ok_or_else(|| SignerError::invalid_input("Missing transaction in Tron payload"))?; | ||
|
|
||
| fn sign_data(input: &TransactionLoadInput, private_key: &[u8]) -> Result<String, SignerError> { | ||
| let (transaction, metadata) = get_transaction(input)?; | ||
| let raw_data_hex = transaction | ||
| .raw_data_hex | ||
| .as_deref() | ||
| .ok_or_else(|| SignerError::invalid_input("Missing raw_data_hex in Tron transaction payload"))?; | ||
| let raw_bytes = decode_hex(raw_data_hex)?; | ||
| let digest = sha256(&raw_bytes); | ||
| let signature = Signer::sign_digest(SignatureScheme::Secp256k1, digest.to_vec(), private_key.to_vec()).map_err(|e| SignerError::signing_error(e.to_string()))?; | ||
| let signature_hex = hex::encode(signature); | ||
| let format = if transaction.get("raw_data_hex").is_some() { | ||
| PayloadFormat::V1 | ||
| } else if transaction.get("transaction").and_then(|t| t.get("raw_data_hex")).is_some() { | ||
| PayloadFormat::Legacy | ||
| } else { | ||
| return Err(SignerError::invalid_input("Missing raw_data_hex in Tron transaction payload")); | ||
| }; | ||
|
|
||
| match metadata.output_type { | ||
| TransferDataOutputType::Signature => Ok(signature_hex), | ||
| TransferDataOutputType::EncodedTransaction => { | ||
| let payload = apply_signature(metadata.payload, &signature_hex)?; | ||
| let result_payload = match metadata.output_action { | ||
| TransferDataOutputAction::Send => payload | ||
| .get("transaction") | ||
| .cloned() | ||
| .ok_or_else(|| SignerError::invalid_input("Missing transaction object for Tron broadcast"))?, | ||
| TransferDataOutputAction::Sign => payload, | ||
| }; | ||
| Ok(Self { | ||
| payload, | ||
| format, | ||
| output_type: extra.output_type.clone(), | ||
| }) | ||
| } | ||
|
|
||
| Ok(serde_json::to_string(&result_payload)?) | ||
| fn transaction(&self) -> &Value { | ||
| let transaction = &self.payload["transaction"]; | ||
| match self.format { | ||
| PayloadFormat::V1 => transaction, | ||
| PayloadFormat::Legacy => &transaction["transaction"], | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn get_transaction(input: &TransactionLoadInput) -> Result<(TronTransaction, PayloadMetadata), SignerError> { | ||
| let extra = input.get_data_extra().map_err(SignerError::invalid_input)?; | ||
| let data = extra.data.as_ref().ok_or_else(|| SignerError::invalid_input("Missing transaction data"))?; | ||
| fn raw_data_hex(&self) -> Result<&str, SignerError> { | ||
| self.transaction()["raw_data_hex"] | ||
| .as_str() | ||
| .ok_or_else(|| SignerError::invalid_input("raw_data_hex must be a string")) | ||
| } | ||
|
|
||
| let payload: TronPayload = serde_json::from_slice(data).map_err(|_| SignerError::invalid_input("Invalid Tron transaction payload"))?; | ||
| let transaction = payload.transaction.clone(); | ||
| let payload_value = serde_json::to_value(&payload).map_err(|_| SignerError::invalid_input("Invalid Tron transaction payload"))?; | ||
| let metadata = PayloadMetadata { | ||
| payload: payload_value, | ||
| output_type: extra.output_type.clone(), | ||
| output_action: extra.output_action.clone(), | ||
| }; | ||
| Ok((transaction, metadata)) | ||
| fn into_signed(self, signature_hex: &str) -> Result<String, SignerError> { | ||
| let mut transaction = self.transaction().clone(); | ||
| transaction | ||
| .as_object_mut() | ||
| .ok_or_else(|| SignerError::invalid_input("Transaction is not an object"))? | ||
| .insert("signature".to_string(), serde_json::json!([signature_hex])); | ||
| serde_json::to_string(&transaction).map_err(Into::into) | ||
| } | ||
| } | ||
|
|
||
| fn apply_signature(payload: Value, signature_hex: &str) -> Result<Value, SignerError> { | ||
| let mut payload: TronPayload = serde_json::from_value(payload).map_err(|_| SignerError::invalid_input("Invalid Tron transaction payload"))?; | ||
| payload.transaction.signature = Some(vec![signature_hex.to_string()]); | ||
| payload.signature = Some(signature_hex.to_string()); | ||
| Ok(serde_json::to_value(payload)?) | ||
| pub struct TronChainSigner; | ||
|
|
||
| impl ChainSigner for TronChainSigner { | ||
| fn sign_data(&self, input: &TransactionLoadInput, private_key: &[u8]) -> Result<String, SignerError> { | ||
| let payload = TronPayload::parse(input)?; | ||
| let raw_bytes = decode_hex(payload.raw_data_hex()?)?; | ||
| let digest = sha256(&raw_bytes); | ||
| let signature = Signer::sign_digest(SignatureScheme::Secp256k1, digest.to_vec(), private_key.to_vec()).map_err(|e| SignerError::signing_error(e.to_string()))?; | ||
| let signature_hex = hex::encode(signature); | ||
|
|
||
| match payload.output_type { | ||
| TransferDataOutputType::Signature => Ok(signature_hex), | ||
| TransferDataOutputType::EncodedTransaction => payload.into_signed(&signature_hex), | ||
| } | ||
| } | ||
| } |
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
30 changes: 30 additions & 0 deletions
30
gemstone/src/wallet_connect/test/tron_sign_transaction_nested.json
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,30 @@ | ||
| { | ||
| "address": "TJoSEwEqt7cT3TUwmEoUYnYs5cZR3xSukM", | ||
| "transaction": { | ||
| "transaction": { | ||
| "raw_data": { | ||
| "contract": [ | ||
| { | ||
| "parameter": { | ||
| "type_url": "type.googleapis.com/protocol.TriggerSmartContract", | ||
| "value": { | ||
| "contract_address": "41a614f803b6fd780986a42c78ec9c7f77e6ded13c", | ||
| "data": "095ea7b300000000000000000000000060e00625a95cbc180f290e2611c826f90eeba56f0000000000000000000000000000000000000000000000000000000000000000", | ||
| "owner_address": "4160e00625a95cbc180f290e2611c826f90eeba56f" | ||
| } | ||
| }, | ||
| "type": "TriggerSmartContract" | ||
| } | ||
| ], | ||
| "expiration": 1770267837000, | ||
| "fee_limit": 200000000, | ||
| "ref_block_bytes": "af5b", | ||
| "ref_block_hash": "64a0e8e5926b22fc", | ||
| "timestamp": 1770267778282 | ||
| }, | ||
| "raw_data_hex": "0a02af5b220864a0e8e5926b22fc40c884bee1c2335aae01081f12a9010a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412740a154160e00625a95cbc180f290e2611c826f90eeba56f121541a614f803b6fd780986a42c78ec9c7f77e6ded13c2244095ea7b300000000000000000000000060e00625a95cbc180f290e2611c826f90eeba56f000000000000000000000000000000000000000000000000000000000000000070eab9bae1c23390018084af5f", | ||
| "txID": "fb21f360363fcf23f80bbf33f28c1d9972f0bf5e13f20e48430000c88ce88205", | ||
| "visible": false | ||
| } | ||
| } | ||
| } |
52 changes: 24 additions & 28 deletions
52
gemstone/src/wallet_connect/test/tron_sign_transaction_response.json
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,32 +1,28 @@ | ||
| { | ||
| "address": "TJoSEwEqt7cT3TUwmEoUYnYs5cZR3xSukM", | ||
| "transaction": { | ||
| "raw_data": { | ||
| "contract": [ | ||
| { | ||
| "parameter": { | ||
| "type_url": "type.googleapis.com/protocol.TriggerSmartContract", | ||
| "value": { | ||
| "contract_address": "41a614f803b6fd780986a42c78ec9c7f77e6ded13c", | ||
| "data": "095ea7b300000000000000000000000060e00625a95cbc180f290e2611c826f90eeba56f0000000000000000000000000000000000000000000000000000000000000000", | ||
| "owner_address": "4160e00625a95cbc180f290e2611c826f90eeba56f" | ||
| } | ||
| }, | ||
| "type": "TriggerSmartContract" | ||
| } | ||
| ], | ||
| "expiration": 1770267837000, | ||
| "fee_limit": 200000000, | ||
| "ref_block_bytes": "af5b", | ||
| "ref_block_hash": "64a0e8e5926b22fc", | ||
| "timestamp": 1770267778282 | ||
| }, | ||
| "raw_data_hex": "0a02af5b220864a0e8e5926b22fc40c884bee1c2335aae01081f12a9010a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412740a154160e00625a95cbc180f290e2611c826f90eeba56f121541a614f803b6fd780986a42c78ec9c7f77e6ded13c2244095ea7b300000000000000000000000060e00625a95cbc180f290e2611c826f90eeba56f000000000000000000000000000000000000000000000000000000000000000070eab9bae1c23390018084af5f", | ||
| "signature": [ | ||
| "943d286dfd1fb6a2cd31c9af7a6cfd23ee062ec2e0abcf82c7daa0c7bb43ab04458e0e88ebe3a94060122cccc8fb4395e5eb922720327df04ae840139c729a1f00" | ||
| "raw_data": { | ||
| "contract": [ | ||
| { | ||
| "parameter": { | ||
| "type_url": "type.googleapis.com/protocol.TriggerSmartContract", | ||
| "value": { | ||
| "contract_address": "41a614f803b6fd780986a42c78ec9c7f77e6ded13c", | ||
| "data": "095ea7b300000000000000000000000060e00625a95cbc180f290e2611c826f90eeba56f0000000000000000000000000000000000000000000000000000000000000000", | ||
| "owner_address": "4160e00625a95cbc180f290e2611c826f90eeba56f" | ||
| } | ||
| }, | ||
| "type": "TriggerSmartContract" | ||
| } | ||
| ], | ||
| "txID": "fb21f360363fcf23f80bbf33f28c1d9972f0bf5e13f20e48430000c88ce88205", | ||
| "visible": false | ||
| "expiration": 1770267837000, | ||
| "fee_limit": 200000000, | ||
| "ref_block_bytes": "af5b", | ||
| "ref_block_hash": "64a0e8e5926b22fc", | ||
| "timestamp": 1770267778282 | ||
| }, | ||
| "signature": "943d286dfd1fb6a2cd31c9af7a6cfd23ee062ec2e0abcf82c7daa0c7bb43ab04458e0e88ebe3a94060122cccc8fb4395e5eb922720327df04ae840139c729a1f00" | ||
| "raw_data_hex": "0a02af5b220864a0e8e5926b22fc40c884bee1c2335aae01081f12a9010a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412740a154160e00625a95cbc180f290e2611c826f90eeba56f121541a614f803b6fd780986a42c78ec9c7f77e6ded13c2244095ea7b300000000000000000000000060e00625a95cbc180f290e2611c826f90eeba56f000000000000000000000000000000000000000000000000000000000000000070eab9bae1c23390018084af5f", | ||
| "signature": [ | ||
| "943d286dfd1fb6a2cd31c9af7a6cfd23ee062ec2e0abcf82c7daa0c7bb43ab04458e0e88ebe3a94060122cccc8fb4395e5eb922720327df04ae840139c729a1f00" | ||
| ], | ||
| "txID": "fb21f360363fcf23f80bbf33f28c1d9972f0bf5e13f20e48430000c88ce88205", | ||
| "visible": false | ||
| } |
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.