-
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
refactor(utxo): refactor utxo output script creation #1960
Conversation
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 for the PR! I have few changes requests, all related to signing.
Added missed fix for segwit signing code (thanks @artemii235 for noticing that). (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. |
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 suggestions, please consider them as non-blocking.
mm2src/coins/qrc20.rs
Outdated
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, |
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.
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)
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.
yes, I also thought about a struct like that or array. I will do this, thank you
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.
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
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.
Added NetworkAddressPrefixes and AddressPrefixes structs to encapsulate prefixes
mm2src/coins/qrc20/qrc20_tests.rs
Outdated
@@ -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() |
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.
I think you can use coin.as_ref().conf.pub_addr_prefix/...
here and other similar lines. Also, QTUM regtest P2SH prefix is 110.
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.
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
impl From<&'static str> for LegacyAddress { | ||
fn from(s: &'static str) -> Self { s.parse().unwrap() } // TODO: dangerous unwrap? | ||
} |
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 can likely add #cfg[test]
to make it test-only and avoid occasional usage and panic in production code.
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.
I could not use cfg[test] as this code used in tests in other modules but I added Default instead
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(), |
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.
It is preferred to use constants like const KMD_P2PKH: u8 = 60;
and so on.
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.
added contants (see address_prefixes.rs)
e1e72df
to
20570bc
Compare
I hope, I have fixed the requests above |
@dimxy Hi, this PR started to have conflicts. |
…s deserializaton)
fix output_script fn to use script_type add LegacyAddress obj allowing from_str deser use parametrised Address::from_legacyaddress instead of default from_str use LegacyAddress in convertaddress fix some clippy errors
…ddress (different error processing)
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) |
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 you for the fixes! Next review iteration!
@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. |
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.
Next review iteration! Probably the last review iteration unless there are some minor things found during the last code check :)
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!
I only have a couple of nits here.
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.
🔥
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!
This is just a refactor PR and it's needed for other PRs, so sec-review can be done in release PR. |
* 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) ...
* 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)
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.