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

refactor(utxo): refactor utxo output script creation #1960

Merged
merged 34 commits into from
Feb 6, 2024

Conversation

dimxy
Copy link
Collaborator

@dimxy dimxy commented Sep 6, 2023

Closes #1270
As suggested in the issue, script_type field is added to Address structure so we do not need the extra script type param in output_script() any more. Now the script type is determined right at parsing input address strings. For that we always need coin prefixes when we parse addresses as strings, so the standard Address::from_str() was replaced on Address::from_legacyaddress() function, with coin prefixes as params.
The output_script() fn is now universally used everywhere when output script should be created from an address

There are still few cases when we need the standard from_str fn (w/o prefixes) to parse addresses as strings, f.e. the convertaddress rpc. For this a new LegacyAddress type is added which supports the from_str() method.

@dimxy dimxy changed the title feat(utxo) Refactor utxo output script creation feat(utxo): Refactor utxo output script creation Sep 6, 2023
@dimxy dimxy changed the title feat(utxo): Refactor utxo output script creation refactor(utxo): Refactor utxo output script creation Sep 6, 2023
@dimxy dimxy changed the title refactor(utxo): Refactor utxo output script creation refactor(utxo): refactor utxo output script creation Sep 6, 2023
@dimxy dimxy added the in progress Changes will be made from the author label Sep 6, 2023
@artemii235 artemii235 self-requested a review September 7, 2023 08:00
@dimxy dimxy requested a review from shamardy September 8, 2023 10:15
@dimxy dimxy added under review and removed in progress Changes will be made from the author labels Sep 8, 2023
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.

Thanks for the PR! I have few changes requests, all related to signing.

mm2src/coins/lightning/ln_events.rs Outdated Show resolved Hide resolved
mm2src/coins/qrc20.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/qtum_delegation.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/slp.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_tests.rs Outdated Show resolved Hide resolved
@dimxy
Copy link
Collaborator Author

dimxy commented Sep 8, 2023

Added missed fix for segwit signing code (thanks @artemii235 for noticing that).
The explanation is: as now a unified output_script fn is used for all cases when prev_script is created, for segwit we pass scriptPubKey (not scriptCode as before) to the signing code. The segwit signing code creates scriptCode itself. I believe this is more similar to how the original signing code in daemons does this.

(Seems tests are better now. There are still few tests failing but I believe because of this PR)

@shamardy
Copy link
Collaborator

shamardy commented Sep 8, 2023

(Seems tests are better now. There are still few tests failing but I believe because of this PR)

Yeah, failing tests now are not due to changes in this PR.

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 suggestions, please consider them as non-blocking.

mm2src/mm2_bitcoin/rpc/src/v1/types/mod.rs Outdated Show resolved Hide resolved
Comment on lines 1497 to 1500
coin.as_ref().conf.pub_addr_prefix,
coin.as_ref().conf.pub_t_addr_prefix,
coin.as_ref().conf.p2sh_addr_prefix,
coin.as_ref().conf.p2sh_t_addr_prefix,
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about creating the following struct and using it instead to make code a bit shorted/avoid duplication?

struct AddressPrefixes {
    pub_addr_prefix: u8,
    pub_t_addr_prefix: u8,
    p2sh_addr_prefix: u8,
    p2sh_t_addr_prefix: u8,
}

So this and other from_legacyaddress calls will look like

UtxoAddress::from_legacyaddress(&req.to, coin.as_ref().conf.address_prefixes)

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 also thought about a struct like that or array. I will do this, thank you

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What would you say about using Vec for prefixes, like it is in daemons codebases. So such a vec would look like [t_addr_prefix, p2pkh_prefix] with t_addr_prefix optional.

pub struct Address {
    /// The prefixes of the p2pkh address of size 1 or 2
    pub p2pkh_prefix: Vec<u8>,
    /// The prefixes of the p2sh address of size 1 or 2
    pub p2sh_prefix: Vec<u8>,
    ...
}

Functions would accept slices in their params.
I think it would be easier to deal with such types (less variables or members)
Also we do not have t_addr_prefix in most cases, so we do not process it with this approach.
Plus this is consistent with other prefixes representations like xpub

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added NetworkAddressPrefixes and AddressPrefixes structs to encapsulate prefixes

@@ -150,7 +152,7 @@ fn test_validate_maker_payment() {

assert_eq!(
*coin.utxo.derivation_method.unwrap_single_addr(),
"qUX9FGHubczidVjWPCUWuwCUJWpkAtGCgf".into()
Address::from_legacyaddress("qUX9FGHubczidVjWPCUWuwCUJWpkAtGCgf", 120, 0, 50, 0).unwrap()
Copy link
Member

Choose a reason for hiding this comment

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

I think you can use coin.as_ref().conf.pub_addr_prefix/... here and other similar lines. Also, QTUM regtest P2SH prefix is 110.

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 as_ref but I guess deref here is okay, the alternative is to use '&Address::from_legacyaddress(..).
Also instead of using consts I changed it to coin.as_ref().conf.address_prefixes

Comment on lines 100 to 92
impl From<&'static str> for LegacyAddress {
fn from(s: &'static str) -> Self { s.parse().unwrap() } // TODO: dangerous unwrap?
}
Copy link
Member

Choose a reason for hiding this comment

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

You can likely add #cfg[test] to make it test-only and avoid occasional usage and panic in production code.

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 could not use cfg[test] as this code used in tests in other modules but I added Default instead

Comment on lines 203 to 206
Address::from_legacyaddress("RG278CfeNPFtNztFZQir8cgdWexVhViYVy", 60, 0, 85, 0).unwrap(),
Address::from_legacyaddress("RYPz6Lr4muj4gcFzpMdv3ks1NCGn3mkDPN", 60, 0, 85, 0).unwrap(),
Address::from_legacyaddress("RJeDDtDRtKUoL8BCKdH7TNCHqUKr7kQRsi", 60, 0, 85, 0).unwrap(),
Address::from_legacyaddress("RQHn9VPHBqNjYwyKfJbZCiaxVrWPKGQjeF", 60, 0, 85, 0).unwrap(),
Copy link
Member

Choose a reason for hiding this comment

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

It is preferred to use constants like const KMD_P2PKH: u8 = 60; and so on.

Copy link
Collaborator Author

@dimxy dimxy Oct 9, 2023

Choose a reason for hiding this comment

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

added contants (see address_prefixes.rs)

@dimxy dimxy force-pushed the feature-add-script-type-to-address branch 2 times, most recently from e1e72df to 20570bc Compare October 9, 2023 17:12
@dimxy
Copy link
Collaborator Author

dimxy commented Oct 9, 2023

I hope, I have fixed the requests above

@laruh
Copy link
Member

laruh commented Oct 31, 2023

@dimxy Hi, this PR started to have conflicts.

@dimxy
Copy link
Collaborator Author

dimxy commented Jan 29, 2024

fixed review notes, added AddressBuilder and divided the witness script creation fn into two functions separately for wpkh and wsh (for better address hash checking)

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!

mm2src/coins/lightning/ln_events.rs Outdated Show resolved Hide resolved
mm2src/coins/qrc20.rs Show resolved Hide resolved
mm2src/mm2_bitcoin/keys/src/address/address_builder.rs Outdated Show resolved Hide resolved
@shamardy
Copy link
Collaborator

shamardy commented Feb 1, 2024

@mariocynicys you can give this PR a look/review since changes here will affect your PR #2053 a lot and this is to be merged first.

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! Probably the last review iteration unless there are some minor things found during the last code check :)

mm2src/mm2_bitcoin/keys/src/address/address_builder.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/script/src/builder.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/script/src/builder.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_builder/utxo_coin_builder.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_withdraw.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@mariocynicys mariocynicys left a comment

Choose a reason for hiding this comment

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

Thanks!
I only have a couple of nits here.

mm2src/mm2_bitcoin/keys/src/address_prefixes.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/keys/src/address.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/keys/src/address_prefixes.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/keys/src/address.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/keys/src/address.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.

🔥

Copy link
Collaborator

@mariocynicys mariocynicys 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!

@shamardy
Copy link
Collaborator

shamardy commented Feb 6, 2024

This is just a refactor PR and it's needed for other PRs, so sec-review can be done in release PR.

@shamardy shamardy merged commit ed80898 into dev Feb 6, 2024
26 of 33 checks passed
@shamardy shamardy deleted the feature-add-script-type-to-address branch February 6, 2024 17:58
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