-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support calculating the TX hash of a transaction (#4008)
* Support calculating the TX hash of a transaction * Implement the calc_tx_hash for TON/Aptos/Sui * Adjust the code according to the comments * Refactor the calcTxHash for Solana * Format rust code using `cargo fmt` * Change UtxoTransactionUtil to BitcoinTransactionUtil * Add some comments * Adjust the code according to the comments * Add more test cases * Fix issue found by `cargo clippy`
- Loading branch information
Showing
58 changed files
with
658 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,24 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Copyright © 2017 Trust Wallet. | ||
|
||
#pragma once | ||
|
||
#include "TWBase.h" | ||
#include "TWCoinType.h" | ||
#include "TWData.h" | ||
|
||
TW_EXTERN_C_BEGIN | ||
|
||
TW_EXPORT_STRUCT | ||
struct TWTransactionUtil; | ||
|
||
/// Calculate the TX hash of a transaction. | ||
/// | ||
/// \param coin coin type. | ||
/// \param encodedTx encoded transaction data. | ||
/// \return The TX hash of a transaction, If the input is invalid or the chain is unsupported, null is returned. | ||
TW_EXPORT_STATIC_METHOD | ||
TWString* _Nullable TWTransactionUtilCalcTxHash(enum TWCoinType coinType, TWString* _Nonnull encodedTx); | ||
|
||
TW_EXTERN_C_END |
This file contains 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 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 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,5 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Copyright © 2017 Trust Wallet. | ||
|
||
pub mod transaction_util; |
This file contains 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,35 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Copyright © 2017 Trust Wallet. | ||
|
||
use tw_coin_entry::coin_context::CoinContext; | ||
use tw_coin_entry::error::prelude::*; | ||
use tw_coin_entry::modules::transaction_util::TransactionUtil; | ||
use tw_encoding::hex; | ||
use tw_hash::sha3::sha3_256; | ||
|
||
pub struct AptosTransactionUtil; | ||
|
||
impl TransactionUtil for AptosTransactionUtil { | ||
fn calc_tx_hash(&self, coin: &dyn CoinContext, encoded_tx: &str) -> SigningResult<String> { | ||
Self::calc_tx_hash_impl(coin, encoded_tx) | ||
} | ||
} | ||
|
||
impl AptosTransactionUtil { | ||
fn calc_tx_hash_impl(_coin: &dyn CoinContext, encoded_tx: &str) -> SigningResult<String> { | ||
let txn_bytes = hex::decode(encoded_tx).map_err(|_| SigningErrorType::Error_input_parse)?; | ||
|
||
// See: https://github.com/aptos-labs/aptos-ts-sdk/blob/f54cac824a41e41dea09c7a6916858a8604dc901/src/api/transaction.ts#L118 | ||
let prefix = sha3_256("APTOS::Transaction".as_bytes()); | ||
|
||
let mut hash_message = Vec::new(); | ||
hash_message.extend_from_slice(&prefix); | ||
// 0 is the index of the enum `Transaction`, see: https://github.com/aptos-labs/aptos-core/blob/6a130c1cca274a5cfdb4a65b441cd5fe61b6c15b/types/src/transaction/mod.rs#L1939 | ||
hash_message.push(0); | ||
hash_message.extend_from_slice(&txn_bytes); | ||
|
||
let tx_hash = sha3_256(&hash_message); | ||
Ok(hex::encode(tx_hash, true)) | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 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 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 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 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 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,37 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Copyright © 2017 Trust Wallet. | ||
|
||
use crate::modules::transaction_decoder::SolanaTransactionDecoder; | ||
use tw_coin_entry::coin_context::CoinContext; | ||
use tw_coin_entry::error::prelude::*; | ||
use tw_coin_entry::modules::transaction_util::TransactionUtil; | ||
use tw_encoding::base64; | ||
use tw_encoding::base64::STANDARD; | ||
|
||
pub struct SolanaTransactionUtil; | ||
|
||
impl TransactionUtil for SolanaTransactionUtil { | ||
fn calc_tx_hash(&self, coin: &dyn CoinContext, encoded_tx: &str) -> SigningResult<String> { | ||
Self::calc_tx_hash_impl(coin, encoded_tx) | ||
} | ||
} | ||
|
||
impl SolanaTransactionUtil { | ||
fn calc_tx_hash_impl(coin: &dyn CoinContext, encoded_tx: &str) -> SigningResult<String> { | ||
// Solana signed transactions can be encoded in either base64 or base58. For more information, see: https://solana.com/docs/rpc/http/sendtransaction | ||
// Currently, this function only accepts base64 encoding. | ||
let tx_bytes = base64::decode(encoded_tx, STANDARD)?; | ||
let decoded_tx_output = SolanaTransactionDecoder::decode_transaction_impl(coin, &tx_bytes)?; | ||
|
||
let first_sig = decoded_tx_output | ||
.transaction | ||
.as_ref() | ||
.and_then(|tx| tx.signatures.first()) | ||
.or_tw_err(SigningErrorType::Error_input_parse) | ||
.context("There is no transaction signatures. Looks like it hasn't been signed yet")?; | ||
|
||
// Tx hash is the first signature | ||
Ok(first_sig.signature.to_string()) | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
// | ||
// Copyright © 2017 Trust Wallet. | ||
|
||
pub mod transaction_util; | ||
pub mod tx_builder; | ||
pub mod tx_signer; |
Oops, something went wrong.