-
Notifications
You must be signed in to change notification settings - Fork 94
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
[r2r] Global HD account #1512
Conversation
* 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
* Use `NegotiationDataMsg::V3` even if maker and taker pubkeys are the same but differ from persistent pub * Log Swap error event in WASM
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(); |
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.
@shamardy could you please confirm that we can use the platform's key-pair to initialize the KeysManager
?
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.
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.
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.
@shamardy Will this effect from which address the funding transaction will be sent and to which address closed channel funds will be claimed?
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.
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)
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.
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.
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.
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.
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.
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 :)
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.
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.
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.
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:
- Global HD account:
one coin -> one address
. - 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?
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.
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.
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.
Huge work! Only one question for now since I am still trying to wrap my head around how all this supposed to work :)
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.
A couple of questions. I will likely have more notes.
* 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`
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.
Few more notes.
# 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
* Remove unnecessary methods from `GlobalHDAccountCtx` * Rename `CryptoCtx::mm2_internal_privkey_bytes` to `mm2_internal_privkey_secret`
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.
Few more comments.
* Remove `mm2_internal_key_pair` from `GlobalHDAccountCtx`
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!
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.
Thanks, LGTM!
* Allo the user to specify `32`, `44`, `49`, `84` purposes in `derivation_path` * Rename `Bip44DerivationPath` to `StandardHDPath`
@artemii235 PR is ready for the next review round |
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.
Huge work, thanks a lot!
No description provided.