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(trezor): add segwit support for withdraw with trezor #1984

Merged
merged 17 commits into from
Dec 21, 2023
Merged

Conversation

dimxy
Copy link
Collaborator

@dimxy dimxy commented Oct 4, 2023

Add support for witness inputs and outputs for withdraw tx to sign it with trezor device
Add test tool to sign withdraw tx with witness inputs/outputs with trezor and emulator
Closes #1685

@dimxy dimxy added the in progress Changes will be made from the author label Oct 4, 2023
@dimxy dimxy self-assigned this Oct 4, 2023
@shamardy shamardy self-requested a review October 4, 2023 16:17
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.

Great work! Only minor comments!
I reviewed the main code changes, will check the ported emulator code and the tests in the next and last iteration :)

mm2src/coins/utxo/utxo_withdraw.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo_signer/src/sign_params.rs Outdated Show resolved Hide resolved
mm2src/trezor/src/utxo/unsigned_tx.rs Show resolved Hide resolved
mm2src/coins/utxo/utxo_tests.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs Outdated Show resolved Hide resolved
@@ -0,0 +1,160 @@
/// This source file was borrowed from Trezor repo and modified to integrate into this project
/// Adds udp transport to interact with trezor emulator
/// To build emulator use this repo: https://github.com/trezor/trezor-firmware, build with build-docker.sh for the desired branch (tag)
Copy link
Collaborator

Choose a reason for hiding this comment

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

It might be a good idea in the future to build and run the trezor emulator in CI to have trezor dockerized tests, please add this to the checklist here #964 (comment) or create a new checklist in a new comment on the issue.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe just create an issue?
(I am just not sure how to find that checklist as it appears buried inside another issue)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe just create an issue?

Sure, please create an issue.

@@ -0,0 +1,160 @@
/// This source file was borrowed from Trezor repo and modified to integrate into this project
Copy link
Collaborator

Choose a reason for hiding this comment

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

For ported code, this guidelines #1861 (review) #1861 (comment) are generally a good idea so we can track the changes in the commit history. If it's not easy to do that now, no problem but please remember this for the future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried to change commits to add the origin ported file to the history

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks, before merging you should squash other commits except the ported code commit, then I should merge to dev without squashing so we would have 2 commits 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.

Could you please clarify, 2 or 3 commits?
in the guidelines above it is suggested to have 3 commits: initial ported code, changes to the ported code and the rest of the work

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, 3 commits is the best way :)
I thought 2 is not a big difference since the other commit will have the changes too, but 3 is better to have a seperate commit related to changes to ported the code only.

@shamardy shamardy added under review and removed in progress Changes will be made from the author labels Oct 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.

Thanks you for the fixes! Next review iteration where it's all also minor stuff to make the code better :)

mm2src/crypto/src/hw_client.rs Outdated Show resolved Hide resolved
mm2src/trezor/src/transport/udp.rs Outdated Show resolved Hide resolved
mm2src/trezor/src/transport/udp.rs Outdated Show resolved Hide resolved
mm2src/trezor/src/transport/udp.rs Outdated Show resolved Hide resolved
mm2src/trezor/src/transport/udp.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/tests/integration_tests_common/mod.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs Outdated Show resolved Hide resolved
shamardy
shamardy previously approved these changes Oct 18, 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.

LGTM!
All unresolved comments are non-blockers, so you can work on them while testing for this feature is underway.

@shamardy
Copy link
Collaborator

shamardy commented Oct 18, 2023

@dimxy I don't think we need to document anything for @KomodoPlatform/qa since this works the same as it does for non-segwit UTXO coins. The only thing needs mentioning is that we need to change the segwit coins derivation path, e.g. here https://github.com/KomodoPlatform/coins/blob/1194a05e76c416437c33658b8eacdf235216d23a/coins#L2074 since the purpose for segwit is 84, so this should be m/84'/0'. I think this applies to all coins not only btc but need your confirmation @dimxy.

@dimxy
Copy link
Collaborator Author

dimxy commented Oct 19, 2023

@dimxy I don't think we need to document anything for @KomodoPlatform/qa since this works the same as it does for non-segwit UTXO coins. The only thing needs mentioning is that we need to change the segwit coins derivation path, e.g. here https://github.com/KomodoPlatform/coins/blob/1194a05e76c416437c33658b8eacdf235216d23a/coins#L2074 since the purpose for segwit is 84, so this should be m/84'/0'. I think this applies to all coins not only btc but need your confirmation @dimxy.

I agree that we should fix the purpose field for segwit coins to make it correct according to the BIP84 standard.
We should also validate it to match another field in the coin config: #1995.
By fixing the purpose we won't harm user funds as the GUI currently uses the HD API only for the trezor wallet, for which segwit coins are not enabled yet

@dimxy
Copy link
Collaborator Author

dimxy commented Nov 2, 2023

rebased on the latest dev

@smk762
Copy link

smk762 commented Nov 3, 2023

Can you please include some working method examples? I'm only getting errors here at the moment.

./version.sh

{
    "result": "1.1.0-beta_3909721",
    "datetime": "2023-11-02T15:43:41+05:00"
}

./init_trezor_withdraw_A.sh DGB-segwit dgb1q6drqvl3u8sukfsu4lm3qsk28jr3fahja302wyy 4.321 1

{
    "mmrpc": "2.0",
    "result": {
        "task_id": 4
    },
    "id": null
}

./init_trezor_withdraw_status.sh 4

{
    "mmrpc": "2.0",
    "result": {
        "status": "Error",
        "details": {
            "error": "Unexpected 'from' address: Unknown BIP32 error: decoding error",
            "error_path": "utxo_common",
            "error_trace": "utxo_common:3068] utxo_common:3067]",
            "error_type": "UnexpectedFromAddress",
            "error_data": "Unknown BIP32 error: decoding error"
        }
    },
    "id": null
}

./task_withdraw_init_A.sh DGB-segwit dgb1q6drqvl3u8sukfsu4lm3qsk28jr3fahja302wyy 4.321 "m/84'/20'/0'/0/1"

{
    "mmrpc": "2.0",
    "result": {
        "task_id": 5
    },
    "id": null
}

./task_withdraw_status.sh 5

{
    "mmrpc": "2.0",
    "result": {
        "status": "Error",
        "details": {
            "error": "Internal error: Unexpected interaction request: PinMatrix3x3",
            "error_path": "utxo_withdraw.with_trezor.response",
            "error_trace": "utxo_withdraw:346] with_trezor:31] response:66]",
            "error_type": "InternalError",
            "error_data": "Unexpected interaction request: PinMatrix3x3"
        }
    },
    "id": null
}

@dimxy
Copy link
Collaborator Author

dimxy commented Nov 3, 2023

I have a couple of samples in the code. They are marked 'ignored' as require trezor connected or emulator running:

cargo test -p mm2_main  -- --nocapture --ignored test_withdraw_from_trezor_p2pkh_rpc
cargo test -p mm2_main  -- --nocapture --ignored test_withdraw_from_trezor_segwit_rpc

they both create and print a signed transaction on BTC testnet. the path used is m/44'/1'/0'/0/0 and m/84'/1'/0'/0/0, you'll need some value on that account.
(you could use the emulator, then add --features trezor-udp option)

@dimxy
Copy link
Collaborator Author

dimxy commented Nov 3, 2023

"Internal error: Unexpected interaction request: PinMatrix3x3",

I think pin is not supported yet (I tried my samples with no pin set on trezor)
Update: seems pin is supported, I received a pin request during task::init_trezor::init rpc call. I used your init_trezor_useraction_pin.sh to respond and it worked fine. After that I managed to create a withdraw successfully.
But after some time another withdraw ended with the same error "Unexpected interaction request: PinMatrix3x3". So it looks like pin is supported at the first init call only...

"error": "Unexpected 'from' address: Unknown BIP32 error: decoding error"

this apparently relates to improper derivation path in the withdraw params

@dimxy
Copy link
Collaborator Author

dimxy commented Nov 6, 2023

added a small fix for the trezor withdraw test (removed an unused object)

Copy link
Member

@onur-ozkan onur-ozkan left a comment

Choose a reason for hiding this comment

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

Nice work!

Some change notes from my side:

mm2src/coins/utxo_signer/src/sign_params.rs Show resolved Hide resolved
mm2src/coins/utxo_signer/src/sign_params.rs Show resolved Hide resolved
mm2src/coins_activation/src/lib.rs Outdated Show resolved Hide resolved
mm2src/coins_activation/src/utxo_activation/mod.rs Outdated Show resolved Hide resolved
@@ -20,6 +20,7 @@ run-docker-tests = []
# TODO
enable-solana = []
default = []
trezor-udp = ["crypto/trezor-udp"] # use for tests to connect to trezor emulator over udp
Copy link
Member

Choose a reason for hiding this comment

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

use for tests to connect to trezor emulator over udp

If we enable this by default here, wouldn't it be enabled for release binaries as well?

Copy link
Collaborator

@shamardy shamardy Nov 29, 2023

Choose a reason for hiding this comment

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

@dimxy Is this fixed? I see that it's not enabled by default now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry, I missed this.
I tried to mark the code as #[cfg(all(test, feature = "trezor-udp"))] but in this case the udp support is not included when I run tests using this feature. I guess, this is because the tests are in a different crate.

Comment on lines +152 to +162
#[cfg(feature = "trezor-udp")]
// try also to connect to emulator over UDP
if let Some(trezor) = try_to_connect::<trezor::transport::udp::UdpAvailableDevice>().await? {
return Ok(trezor);
}
Copy link
Member

Choose a reason for hiding this comment

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

I guess it should be enough to check whether if mm2 running on debug mode or not? Adding a feature for just this piece of code seems an overkill to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

My idea for adding udp as a feature was that normally we would use those tests for trezor with a physical device (that is why I marked them 'ignored' so you need to have a device to run them) and only who wishes to build and use emulator would enable this feature.
(Later we will add ci tests which include emulator auto build and start)

Copy link
Member

Choose a reason for hiding this comment

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

/// Tool to run withdraw directly with trezor device or emulator (no rpc version, added for easier debugging)
/// run cargo test with '--ignored' option

But we have many other tests marked as ignored too, so this test will end up with lots of other tests running and most of them probably will fail. Here we should actually use the feature flag e.g., cargo test --features enable-device-tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay, thank you for this idea, I'll add this feature for device test

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see that run-device-tests feature is added, please confirm that this is fixed @dimxy

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 I added a feature "run-device-tests" (to invoke trezor tests, if needed)

@@ -22,10 +22,14 @@ serde_derive = "1.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
bip32 = { version = "0.2.2", default-features = false, features = ["alloc", "secp256k1-ffi"] }
async-std = { version = "1.5" }
Copy link

Choose a reason for hiding this comment

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

secure code reviewed async-std

ca333
ca333 previously approved these changes Nov 10, 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.

🔥

@shamardy shamardy merged commit c83778c into dev Dec 21, 2023
25 of 32 checks passed
@shamardy shamardy deleted the trezor-segwit branch December 21, 2023 15:03
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)
  ...
dimxy added a commit to dimxy/komodo-defi-framework that referenced this pull request Feb 25, 2024
* dev:
  feat(zcoin): ARRR WASM implementation (KomodoPlatform#1957)
  feat(trading-proto-upgrade): locked amounts, kmd burn and other impl (KomodoPlatform#2046)
  fix(indexeddb): set stop on success cursor condition (KomodoPlatform#2067)
  feat(config): add `max_concurrent_connections` to mm2 config (KomodoPlatform#2063)
  feat(stats_swaps): add gui/mm_version in stats db (KomodoPlatform#2061)
  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)
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.

5 participants