Skip to content
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

feat(tx): add new sign_raw_transaction rpc for UTXO and EVM coins #1930

Merged
merged 23 commits into from
Dec 19, 2023

Conversation

dimxy
Copy link
Collaborator

@dimxy dimxy commented Aug 3, 2023

Adds new sign_raw_transaction rpc method for UTXO coins.
Currently supports P2PKH and P2WPKH spent outputs.
Closes #1407

TODO:
support more previous output types
support several signing keys
update docs to add rpc desc
signing with trezor

@dimxy dimxy changed the title [R2R] feat(rpc): add new sign_raw_transaction rpc for UTXO coins feat(rpc): add new sign_raw_transaction rpc for UTXO coins Aug 3, 2023
Copy link
Member

@borngraced borngraced left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! some suggestions from my side

mm2src/coins/lp_coins.rs Outdated Show resolved Hide resolved
Comment on lines 2426 to 2428
hex::decode(args.tx_hex.as_bytes()).map_to_mm(|e| RawTransactionError::InvalidParam(e.to_string()))?;
let tx: UtxoTx =
deserialize(tx_bytes.as_slice()).map_to_mm(|e| RawTransactionError::InvalidParam(e.to_string()))?;
Copy link
Member

@borngraced borngraced Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these should be TxByteParseError or DecodeError

Comment on lines 2439 to 2444
.map_to_mm(|e| RawTransactionError::InvalidParam(e.to_string()))?,
);
}

let prev_hash = hex::decode(prev_utxo.tx_hash.as_bytes())
.map_err(|e| RawTransactionError::InvalidParam(e.to_string()))?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Comment on lines 2461 to 2462
.filter(|input| !unspents.iter().any(|u| u.outpoint.hash == input.previous_output.hash))
.map(|input| input.previous_output.hash.into())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about using filter_map here once?

signature_version,
coin.as_ref().conf.fork_id,
)
.map_err(|err| RawTransactionError::SigningError(err.get_inner().to_string()))?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we just call err.to_string() so we get the error trace too

@@ -27,7 +27,7 @@ impl Private {
pub fn sign(&self, message: &Message) -> Result<Signature, Error> {
let secret = SecretKey::from_slice(&*self.secret)?;
let message = SecpMessage::from_slice(&**message)?;
let signature = SECP_SIGN.sign(&message, &secret);
let signature = SECP_SIGN.sign_low_r(&message, &secret); // use low R signing from bitcoin which reduces signature malleability
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// use low R signing from bitcoin which reduces signature malleability
let signature = SECP_SIGN.sign_low_r(&message, &secret); 

@shamardy
Copy link
Collaborator

shamardy commented Aug 10, 2023

@dimxy please fix clippy and fmt warnings, you should use cargo clippy --all-targets --all-features -- --D warnings to check clippy warnings and cargo +nightly fmt for formatting the code.

Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the PR! First review iteration!

mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs Outdated Show resolved Hide resolved
mm2src/coins/lp_coins.rs Outdated Show resolved Hide resolved
mm2src/coins/lp_coins.rs Outdated Show resolved Hide resolved
mm2src/coins/lp_coins.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_common.rs Outdated Show resolved Hide resolved
@dimxy
Copy link
Collaborator Author

dimxy commented Aug 11, 2023

@dimxy please fix clippy and fmt warnings, you should use cargo clippy --all-targets --all-features -- --D warnings to check clippy warnings and cargo +nightly fmt for formatting the code.

okay thank you, I used clippy but apparently with different params that I found in the contrib guide

@shamardy
Copy link
Collaborator

I used clippy but apparently with different params that I found in the contrib guide

Contribution guide needs lots of updates apparently.

@dimxy
Copy link
Collaborator Author

dimxy commented Aug 14, 2023

thank you all for your reviews, added new commits following them: used async_trait, fixed formatting, clippy errs, refactored sign_raw_tx (advised function added), improved RawTransactionError

@dimxy dimxy assigned dimxy and unassigned shamardy Aug 16, 2023
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the fixes! Next review iteration!

mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/lp_coins.rs Outdated Show resolved Hide resolved
mm2src/coins/lp_coins.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_common.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_common.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo_signer/src/with_key_pair.rs Outdated Show resolved Hide resolved
@shamardy shamardy added in progress Changes will be made from the author and removed under review labels Aug 23, 2023
@dimxy
Copy link
Collaborator Author

dimxy commented Sep 6, 2023

I rebased on the latest dev. I believe I have made all fixes requested by the reviewers

@dimxy dimxy added under review and removed in progress Changes will be made from the author labels Sep 8, 2023
@shamardy shamardy changed the title feat(rpc): add new sign_raw_transaction rpc for UTXO coins feat(rpc): add new sign_raw_transaction rpc for UTXO and EVM coins Sep 13, 2023
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next review iteration! Will do one final review round once these comments are fixed.

mm2src/coins/lp_coins.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/lp_coins.rs Show resolved Hide resolved
mm2src/coins/utxo/utxo_common.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_common.rs Outdated Show resolved Hide resolved
@laruh
Copy link
Member

laruh commented Sep 14, 2023

@dimxy please note, that this PR started to have conflicts

@dimxy
Copy link
Collaborator Author

dimxy commented Sep 14, 2023

@dimxy please note, that this PR started to have conflicts

Oh, thank you. I have already fixed several conflicts recently but more of them came

@dimxy
Copy link
Collaborator Author

dimxy commented Nov 28, 2023

just rebased on the latest dev

@smk762
Copy link

smk762 commented Nov 28, 2023

just rebased on the latest dev

Thanks! I'm, still a little confused about the ETH/EVM units. Seems the 0x prefix from examples is optional, which is nice. Units appear to be in wei? I created and broadcast a few transactions on polygon, and I'm not sure how the transaction value is being determined. Ideally this would be in ETH rather than gwei or wei (and consistent with other withdraw methods).

            "value": "0x1",
            "gas_limit": "210000"
            https://polygonscan.com/tx/0x6f7e1573b490d1a2ac197b5cf4cb85b98a4a6c5794cbe8d2d53da9717860e70c            
            Value: 1 wei (< $0.000001)
            Max Txn Cost/Fee: 0.273444490862789 MATIC ($0.21)
            Gas Price: 126.437327466 Gwei (0.000000126437327466 MATIC)

            "value": "1",
            "gas_limit": "210000"
            https://polygonscan.com/tx/0x6c266a3cc299ed091e3d5e11d75b10e9921c4a5ef8b21b0173371a3651866e11
            Value: 1 wei
            Transaction Fee: 0.002522350168632 MATIC ($0.00)
            Gas Price: 120.111912792 Gwei (0.000000120111912792 MATIC)

            "value": "1000000000",
            "gas_limit": "21000"
            https://polygonscan.com/tx/0x383b3c343bae089cad35d037d84ac2aed69fca26e1676f2835a62bffea595f53
            Value: 0.000000068719476736 MATIC (< $0.000001)
            Transaction Fee: 0.002931836276166 MATIC ($0.00)
            Gas Price: 139.611251246 Gwei (0.000000139611251246 MATIC)
          
            "value": "1000000000000000",
            "gas_limit": "21000"
            https://polygonscan.com/tx/0xe52cf506aa0f7932158388fad80af058cb242a50494896c8ab8baba7dd453852
            Value: 1.152921504606846976 MATIC ($0.87)
            Transaction Fee: 0.003759189279783 MATIC ($0.00)
            Gas Price: 179.009013323 Gwei (0.000000179009013323 MATIC)

            "value": "0x1000000000000000",
            "gas_limit": "21000"
            https://polygonscan.com/tx/0x137175ea2ccf4d4819669ae59a53ecc782ebc1dcfa0b05763d5016c607ca2ee5
            Value: 1.152921504606846976 MATIC ($0.87)
            Transaction Fee: 0.002742404163051 MATIC ($0.00)
            Gas Price: 130.590674431 Gwei (0.000000130590674431 MATIC)

@smk762
Copy link

smk762 commented Nov 28, 2023

Docs draft at https://edbbd891.komodo-docs.pages.dev/en/docs/atomicdex/api/v20/sign_raw_transaction/
Everything outside of ETH units confusion working as expected

@dimxy
Copy link
Collaborator Author

dimxy commented Nov 29, 2023

Units appear to be in wei?

Yes it is in wei and to be directly converted into U256 'value' (as it was supposed to be the 'raw transaction').
let's make it 'Eth' for consistency with other rpcs

smk762
smk762 previously approved these changes Dec 2, 2023
Copy link

@smk762 smk762 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

shamardy
shamardy previously approved these changes Dec 12, 2023
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest changes approved! Will merge this after 2.0.0-beta is released.

@dimxy dimxy dismissed stale reviews from shamardy and smk762 via 078385f December 19, 2023 19:32
@shamardy shamardy changed the title feat(rpc): add new sign_raw_transaction rpc for UTXO and EVM coins feat(tx): add new sign_raw_transaction rpc for UTXO and EVM coins Dec 19, 2023
@shamardy shamardy merged commit 7382588 into dev Dec 19, 2023
25 of 33 checks passed
@shamardy shamardy deleted the feature-sign-raw-tx branch December 19, 2023 20:02
@@ -216,7 +216,7 @@ impl FromStr for Address {
}

impl From<&'static str> for Address {
fn from(s: &'static str) -> Self { s.parse().unwrap() }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this should be conditionally compiled for tests only.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, here is a PR for refactoring this struct and a discussion on this unwrap in it: #1960 (comment)

@Alrighttt Alrighttt self-assigned this Dec 20, 2023
dimxy added a commit to dimxy/komodo-defi-framework that referenced this pull request Dec 21, 2023
* dev: (22 commits)
  chore(config): remove vscode launchjson (KomodoPlatform#2040)
  feat(trading-proto-upgrade): wasm DB, kickstart, refund states, v2 RPCs (KomodoPlatform#2015)
  feat(UTXO): balance event streaming for Electrum clients (KomodoPlatform#2013)
  feat(tx): add new sign_raw_transaction rpc for UTXO and EVM coins (KomodoPlatform#1930)
  fix(p2p): handle encode_and_sign errors (KomodoPlatform#2038)
  chore(release): add changelog entries for v2.0.0-beta (KomodoPlatform#2037)
  chore(network): write network information to stdout (KomodoPlatform#2034)
  fix(price_endpoints): add cached url (KomodoPlatform#2032)
  deps(network): sync with upstream yamux (KomodoPlatform#2030)
  fix(config): accept a string as rpcport value (KomodoPlatform#2026)
  feat(nft): move db lock, add tx fee and confirmations (KomodoPlatform#1989)
  chore(network): update seednodes for netid 8762 (KomodoPlatform#2024)
  chore(network): add todo on peer storage behaviour (KomodoPlatform#2025)
  chore(network): exclude `168.119.236.249` from the seednode list (KomodoPlatform#2021)
  feat(network): deprecate 7777 network (KomodoPlatform#2020)
  chore(release): bump mm2 version to 2.0.0-beta (KomodoPlatform#2018)
  feat(UTXO swaps): kmd burn plan impl (KomodoPlatform#2006)
  chore(docs): fix the link to simple market maker in README.md (KomodoPlatform#2011)
  refactor(cli): cli dependency updates and warn on bad config perm (KomodoPlatform#1956)
  chore(containers and docs): update docs and container images (KomodoPlatform#2003)
  ...

# Conflicts:
#	mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs
#	mm2src/mm2_test_helpers/src/for_tests.rs
dimxy added a commit to dimxy/komodo-defi-framework that referenced this pull request Jan 23, 2024
* dev: (24 commits)
  chore(release): bump mm2 version to 2.1.0-beta (KomodoPlatform#2044)
  feat(trezor): add segwit support for withdraw with trezor (KomodoPlatform#1984)
  chore(config): remove vscode launchjson (KomodoPlatform#2040)
  feat(trading-proto-upgrade): wasm DB, kickstart, refund states, v2 RPCs (KomodoPlatform#2015)
  feat(UTXO): balance event streaming for Electrum clients (KomodoPlatform#2013)
  feat(tx): add new sign_raw_transaction rpc for UTXO and EVM coins (KomodoPlatform#1930)
  fix(p2p): handle encode_and_sign errors (KomodoPlatform#2038)
  chore(release): add changelog entries for v2.0.0-beta (KomodoPlatform#2037)
  chore(network): write network information to stdout (KomodoPlatform#2034)
  fix(price_endpoints): add cached url (KomodoPlatform#2032)
  deps(network): sync with upstream yamux (KomodoPlatform#2030)
  fix(config): accept a string as rpcport value (KomodoPlatform#2026)
  feat(nft): move db lock, add tx fee and confirmations (KomodoPlatform#1989)
  chore(network): update seednodes for netid 8762 (KomodoPlatform#2024)
  chore(network): add todo on peer storage behaviour (KomodoPlatform#2025)
  chore(network): exclude `168.119.236.249` from the seednode list (KomodoPlatform#2021)
  feat(network): deprecate 7777 network (KomodoPlatform#2020)
  chore(release): bump mm2 version to 2.0.0-beta (KomodoPlatform#2018)
  feat(UTXO swaps): kmd burn plan impl (KomodoPlatform#2006)
  chore(docs): fix the link to simple market maker in README.md (KomodoPlatform#2011)
  ...
dimxy added a commit to dimxy/komodo-defi-framework that referenced this pull request Feb 18, 2024
* evm-hd-wallet: (27 commits)
  Fix todo comments
  Fix HDAddressOps::Address trait bounds
  fix(indexeddb): fix IDB cursor.continue_() call after drop (KomodoPlatform#2028)
  security bump for `h2` (KomodoPlatform#2062)
  fix(makerbot): allow more than one prices url in makerbot (KomodoPlatform#2027)
  fix(wasm worker env): refactor direct usage of `window` (KomodoPlatform#1953)
  feat(nft): nft abi in withdraw_nft RPC, clear_nft_db RPC (KomodoPlatform#2039)
  refactor(utxo): refactor utxo output script creation (KomodoPlatform#1960)
  feat(ETH): balance event streaming for ETH (KomodoPlatform#2041)
  chore(release): bump mm2 version to 2.1.0-beta (KomodoPlatform#2044)
  feat(trezor): add segwit support for withdraw with trezor (KomodoPlatform#1984)
  chore(config): remove vscode launchjson (KomodoPlatform#2040)
  feat(trading-proto-upgrade): wasm DB, kickstart, refund states, v2 RPCs (KomodoPlatform#2015)
  feat(UTXO): balance event streaming for Electrum clients (KomodoPlatform#2013)
  feat(tx): add new sign_raw_transaction rpc for UTXO and EVM coins (KomodoPlatform#1930)
  fix(p2p): handle encode_and_sign errors (KomodoPlatform#2038)
  chore(release): add changelog entries for v2.0.0-beta (KomodoPlatform#2037)
  chore(network): write network information to stdout (KomodoPlatform#2034)
  fix(price_endpoints): add cached url (KomodoPlatform#2032)
  deps(network): sync with upstream yamux (KomodoPlatform#2030)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants