-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Support calculating the TX hash of a transaction #4008
Support calculating the TX hash of a transaction #4008
Conversation
Hi @10gic, I checked the PR. Do you plan to use this |
We already have Please also note that there is one exception - |
Hi @satoshiotomakan, as I mentioned in the issue #4001 : There are cases where the TX is not constructed by the wallet core; for example, the transaction might be constructed by a DApp. The DApp only requests the wallet to perform the signing, constructs the transaction itself, and then asks the wallet to broadcast it. The usage scenario is limited if it is only provided in the SigningOutput. |
@10gic most of the blockchain RPC nodes provide hash of the broadcasted transaction. Is it possible to leverage this in your case? I'm a little concerned that the number of such utilities in CoinEntry will grow |
Hi @satoshiotomakan, If we want to monitor the status of TX more thoroughly, relying on the RPC return result may be too late. This is because RPC return timeouts can occur, and in such cases, the TX might have already been submitted, but the client is unable to obtain the TX Hash. |
Hi @satoshiotomakan, providing a generic function to calculate a transaction hash is a common feature. Many blockchain SDKs offer this capability. For example, okx js-wallet-sdk. I was wondering if you might have plans to merge this pull request, or if there's anything else you'd like me to adjust. Thank you for your time! |
fn calc_tx_hash_impl(_coin: &dyn CoinContext, encoded_tx: &str) -> SigningResult<String> { | ||
let tx = base64::decode(encoded_tx, STANDARD)?; | ||
|
||
let tx_hash = sha256(&tx); |
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.
tw_cosmos_sdk
is used by many Cosmos-based chains. There is Greenfield
that uses keccak256
hash instead of sha256
. The type of hash is provided in Cosmos::Proto::SigningInput::tx_hasher
.
Most likely we should allow an external user to specify some parameters for the transaction to be hashed correctly.
I'll think about how to better implement it. Brb in one/two days once I have something in mind
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.
We can choose not to regard Greenfield as a Cosmos chain.
Line 4184 in af67cae
"blockchain": "Greenfield", |
And implement a GreenfieldTransactionUtil for Greenfield
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.
I think it's fine to use CosmosContext::default_hasher()
, so please refactor CosmosTransactionUtil
to have a generic type parameter like:
struct CosmosTransactionUtil<Context: CosmosContext> { todo };
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.
Thank you for your advice. I didn't realize that we already have the StandardCosmosContext. I've adjusted the code according to your suggestion.
I was planning to make calcTxHash support Greenfield as well, but I discovered that there might be some changes in Greenfield since the wallet core started supporting it. I noticed this because the links in the test cases are now returning 404 errors.
// Successfully broadcasted https://greenfieldscan.com/tx/0x9f895cf2dd64fb1f428cefcf2a6585a813c3540fc9fe1ef42db1da2cb1df55ab |
// Successfully broadcasted: https://greenfieldscan.com/tx/ED8508F3C174C4430B8EE718A6D6F0B02A8C516357BE72B1336CF74356529D19 |
Therefore, I won't be supporting it in this pull request.
Could you please review the code for me again?
src/interface/TWTranscationUtil.cpp
Outdated
|
||
const Rust::TWStringWrapper outputDataPtr = Rust::tw_transaction_util_calc_tx_hash(static_cast<uint32_t>(coinType), encodedTxStr.get()); | ||
|
||
return TWStringCreateWithUTF8Bytes(outputDataPtr.toStringOrDefault().c_str()); |
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.
Better to return nullptr
in case of an error
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.
I think it's not bad to return an empty string in calcTxHash in case of an error.
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.
It's a unified behaviour we try to follow in WalletCore. We already have some FFI functions that return an empty string or data in case of errors, but they're exceptions we plan to refactor.
For example, if a function returns TWString* _Nullable
pointer, the wrapper function in Swift we generate will return String?
that can be checked by try
keyword
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.
Thank you for providing the background. I will adjust the code accordingly.
6dde9d8
to
1c96dd4
Compare
Hi @satoshiotomakan, please let me know if there's anything else you would like me to adjust in the code. Thank you! |
Hi @10gic, could you please add unit tests in
We need to keep code coverage high level |
Please also note that any change in wallet-core/codegen-v2/src/codegen/rust/templates/blockchain_crate/entry.rs Lines 25 to 93 in 406abe4
|
Thank you for the reminder, I have added it. |
Sure, I've copied the C++ test cases into Rust. |
e092104
to
0a96b87
Compare
Hi @satoshiotomakan, I've addressed the comments on the PR. Could you please let me know if there are any other comments or suggestions? |
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.
LGTM, thank you for the quick fixes!
Description
Support calculating the TX hash of a transaction. Fix issue #4001
How to test
Run Rust, C++, Kotlin, Swift tests
Types of changes
Checklist
If you're adding a new blockchain