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

[r2r] Global HD account #1512

Merged
merged 32 commits into from
Nov 17, 2022
Merged

[r2r] Global HD account #1512

merged 32 commits into from
Nov 17, 2022

Conversation

sergeyboyko0791
Copy link

No description provided.

* TODO: remove `MmCtx::secp256k1_key_pair_as_option()` and ``MmCtx::secp256k1_key_pair`
* Add `Secp256k1Secret` and `IguanaPrivKey` aliases, use it everywhere instead of the `[u8]` slice
* Add common `InternalError`
* Add `DiscardMmResult`, `SplitMmResult` traits to `mm2_error_handle`
* Move `MmCtx::public_id()` to `CryptoCtx::mm2_internal_public_id()`
* Add `hd_account_id` conf param
* Cover swaps in tests
* Rename `DerivationMethod::Iguana` to `DerivationMethod::SingleAddress`
* Rename `PrivKeyNotAllowed` to `PrivKeyPolicyNotAllowed`
* Use `NegotiationDataMsg::V3` even if maker and taker pubkeys are the same but differ from persistent pub
* Log Swap error event in WASM
@sergeyboyko0791 sergeyboyko0791 changed the title [wip] Global HD account [r2r] Global HD account Nov 1, 2022
Comment on lines +82 to +92
pub fn init_keys_manager(platform: &Platform) -> EnableLightningResult<Arc<KeysManager>> {
// The current time is used to derive random numbers from the seed where required, to ensure all random generation is unique across restarts.
let seed: [u8; 32] = ctx.secp256k1_key_pair().private().secret.into();
// TODO validate that this is right
let seed: [u8; 32] = platform
.coin
.as_ref()
.priv_key_policy
.key_pair_or_err()?
.private()
.secret
.into();
Copy link
Author

Choose a reason for hiding this comment

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

@shamardy could you please confirm that we can use the platform's key-pair to initialize the KeysManager?

Copy link
Collaborator

Choose a reason for hiding this comment

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

My understanding is that if mm2 is started with a global hd account the key pair here will be the one corresponding to a valid child address so I don't think this will be a problem. Now, this private key will be used as seed in KeysManager to generate a new master key that should generate other multiple child keys (a total of six) for node public/private key, closing channels address, and some of these child keys (e.g. channel_master_key) are used as master keys again :) You can check the KeysManager::new function internals for the exact details https://github.com/KomodoPlatform/atomicDEX-API/blob/5627cded1616e34c67b795023514a2a86b4c3bba/mm2src/coins/lightning/ln_utils.rs#L95
We have two choices here, either have one lightning node per user that uses the below master seed https://github.com/KomodoPlatform/atomicDEX-API/blob/5627cded1616e34c67b795023514a2a86b4c3bba/mm2src/crypto/src/privkey.rs#L121-L122 as it's KeysManager seed, or to give the user the ability to have a lightning wallet/node for every child address using the current implementation in this PR. I think we would need @artemii235 input on which of these options is prefered.

Copy link
Member

Choose a reason for hiding this comment

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

@shamardy Will this effect from which address the funding transaction will be sent and to which address closed channel funds will be claimed?

Copy link
Collaborator

@shamardy shamardy Nov 4, 2022

Choose a reason for hiding this comment

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

You are right. For every key pair there will be a different funding/claiming address (funding and claiming addresses are one and the same address of the platform coin), but currently lightning can't be enabled in HDWallet mode, I even added a todo for this in the past, I thought then that HDWallet meant hardware :) https://github.com/KomodoPlatform/atomicDEX-API/blob/5627cded1616e34c67b795023514a2a86b4c3bba/mm2src/coins/lightning.rs#L1178-L1184
https://github.com/KomodoPlatform/atomicDEX-API/blob/5627cded1616e34c67b795023514a2a86b4c3bba/mm2src/coins/lightning/ln_events.rs#L528-L535
https://github.com/KomodoPlatform/atomicDEX-API/blob/5627cded1616e34c67b795023514a2a86b4c3bba/mm2src/coins/rpc_command/lightning/open_channel.rs#L148
If we agreed to have a lightning address/node for every child address, I should add HDWallet implementation for lightning to this checklist #1045 (comment)

Copy link
Collaborator

Choose a reason for hiding this comment

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

One more note, we can always implement our own lightning KeysManager if we needed that. The only requirement for KeysManager is to implement the KeysInterface trait. But I think the one implemented in rust-lightning is very good for preserving privacy, we break this privacy in our lightning implementation when claiming the closing funds back to the same address the channel was opened from. I guess to continue the preserving of privacy with HDWallets we can let the user decide which child address to generate funding from and which address to claim the closing funds to for every channel, if we choose to use only one lightning node for all platform coin addresses then we would need to implement our own lightning KeysManager where there is one node address using the master seed and change the destination script, the shutdown pubkey when switching addresses.

Copy link
Author

Choose a reason for hiding this comment

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

HDWallet is related to Hardware wallet only.
Global HD account is implemented the way that if the user wants to switch its Global HD account, he needs to restart mm2 instance with another conf.hd_account_id index.
So for a running mm2 instance with an conf.hd_account_id value there is only one address (therefore one key-pair) for any coin.

Copy link
Collaborator

Choose a reason for hiding this comment

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

mmm, I understand now but I think we should change DerivationMethod::HDWallet naming to convey that it's for hardware wallets only to avoid confusion.
So, in light of this info, for every conf.hd_account_id there will be a different lightning address/node with different channels backups etc..., I still see no problem caused in lightning by the current implementation :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

For the privacy issues I detailed here #1512 (comment), there will be a different destination script and shutdown pubkey also for every hd_account_id, I guess we can add an option for the user to claim the closing transaction of a channel to a different hd_account_id or a different address of his choosing making lightning channels opening/closing completely private. Of Course this is not a priority now, but just adding my thoughts about HD accounts implications for lightning.

Copy link
Author

Choose a reason for hiding this comment

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

Ok thank you! DerivationMethod::HDWallet is really a confusing name, because it overlaps with Global HD account. But they are both related to HD wallet:

  1. Global HD account: one coin -> one address.
  2. DerivationMethod::HDWallet: one coin -> multiple accounts, account -> multiple addresses (internals and/or externals).

I explained it in more details here: https://github.com/KomodoPlatform/air_dex/issues/502#issuecomment-1193167304

DerivationMethod::HDWallet is currently used with Trezor only, but it's designed the way that it's independent from the private key policy. Thus later we can add new PrivKeyPolicy::CoinHDAccount, for example. The only difference from PrivKeyPolicy::CoinHDAccount will be how transactions are signed.

If we're sure that we don't plan to support such derivation in the future, I can rename DerivationMethod::HDWallet to DerivationMethod::HardwareWallet.
@artemii235 could you please advice?

Copy link
Member

Choose a reason for hiding this comment

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

If we're sure that we don't plan to support such derivation in the future

We can't 100% sure, I guess 🙂 Though, I would prefer the naming to reflect the current meaning of the code. We can figure out one more renaming later if required.

mm2src/mm2_main/src/lp_ordermatch.rs Outdated Show resolved Hide resolved
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.

Huge work! Only one question for now since I am still trying to wrap my head around how all this supposed to work :)

mm2src/crypto/src/global_hd_ctx.rs Show resolved Hide resolved
Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

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

A couple of questions. I will likely have more notes.

mm2src/coins/eth/v2_activation.rs Show resolved Hide resolved
mm2src/mm2_core/src/mm_ctx.rs Show resolved Hide resolved
@sergeyboyko0791 sergeyboyko0791 changed the title [r2r] Global HD account [wip] Global HD account Nov 6, 2022
* TODO fix mm2_main tests

# Conflicts:
#	mm2src/coins/eth.rs
#	mm2src/coins/qrc20.rs
#	mm2src/coins/solana.rs
#	mm2src/coins/tendermint/tendermint_coin.rs
#	mm2src/coins/utxo/qtum.rs
#	mm2src/coins/z_coin.rs
#	mm2src/mm2_main/src/docker_tests.rs
#	mm2src/mm2_main/src/mm2_tests/lp_bot_tests.rs
#	mm2src/mm2_main/tests/docker_tests/docker_tests_common.rs
#	mm2src/mm2_main/tests/mm2_tests/bch_and_slp_tests.rs
#	mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs
#	mm2src/mm2_test_helpers/src/for_tests.rs
* Rename `PrivKeyBuildPolicy::IguanaPrivKey` to `Secp256k1Secret`
Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

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

Few more notes.

mm2src/crypto/src/crypto_ctx.rs Show resolved Hide resolved
mm2src/crypto/src/crypto_ctx.rs Outdated Show resolved Hide resolved
mm2src/crypto/src/crypto_ctx.rs Outdated Show resolved Hide resolved
mm2src/crypto/src/global_hd_ctx.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/src/lp_swap/maker_swap.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/src/lp_swap/taker_swap.rs Outdated Show resolved Hide resolved
# Conflicts:
#	mm2src/coins/eth.rs
#	mm2src/coins/lightning.rs
#	mm2src/coins/lightning/ln_errors.rs
#	mm2src/coins/lp_coins.rs
#	mm2src/coins/qrc20.rs
#	mm2src/coins/solana.rs
#	mm2src/coins/tendermint/tendermint_coin.rs
#	mm2src/coins/utxo/qtum.rs
#	mm2src/coins/utxo/slp.rs
#	mm2src/coins/utxo/utxo_standard.rs
#	mm2src/coins/z_coin.rs
#	mm2src/mm2_core/src/mm_ctx.rs
#	mm2src/mm2_main/tests/docker_tests/qrc20_tests.rs
* Fix `CryptoCtx` comments
* Simplify `if` conditions for Negotiation message
@sergeyboyko0791 sergeyboyko0791 changed the title [wip] Global HD account [r2r] Global HD account Nov 10, 2022
* Remove unnecessary methods from `GlobalHDAccountCtx`
* Rename `CryptoCtx::mm2_internal_privkey_bytes` to `mm2_internal_privkey_secret`
Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

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

Few more comments.

mm2src/mm2_main/src/lp_swap/maker_swap.rs Show resolved Hide resolved
mm2src/mm2_err_handle/src/discard_mm.rs Outdated Show resolved Hide resolved
mm2src/coins/z_coin.rs Outdated Show resolved Hide resolved
* Remove `mm2_internal_key_pair` from `GlobalHDAccountCtx`
@shamardy shamardy mentioned this pull request Nov 11, 2022
24 tasks
shamardy
shamardy previously approved these changes Nov 11, 2022
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!

laruh
laruh previously approved these changes Nov 13, 2022
Copy link
Member

@laruh laruh left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM!

@sergeyboyko0791 sergeyboyko0791 changed the title [r2r] Global HD account [wip] Global HD account Nov 14, 2022
@sergeyboyko0791 sergeyboyko0791 dismissed stale reviews from laruh and shamardy via 95a266e November 15, 2022 12:24
* Allo the user to specify `32`, `44`, `49`, `84` purposes in `derivation_path`
* Rename `Bip44DerivationPath` to `StandardHDPath`
* Rename `DiscardMmResult::discard_mm` to `DiscardMmTrace::discard_mm_trace`
@sergeyboyko0791 sergeyboyko0791 changed the title [wip] Global HD account [r2r] Global HD account Nov 15, 2022
@sergeyboyko0791
Copy link
Author

@artemii235 PR is ready for the next review round

Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

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

Huge work, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants