librustzcash icon indicating copy to clipboard operation
librustzcash copied to clipboard

Allow reading address kind and network

Open adityapk00 opened this issue 2 years ago • 4 comments

Allow reading the type of address and the network that it can be used on. Useful for tests.

adityapk00 avatar Jul 12 '22 15:07 adityapk00

Codecov Report

Merging #586 (013eb25) into main (37fc286) will increase coverage by 0.05%. The diff coverage is n/a.

@@            Coverage Diff             @@
##             main     #586      +/-   ##
==========================================
+ Coverage   51.16%   51.22%   +0.05%     
==========================================
  Files          94       94              
  Lines        8847     8847              
==========================================
+ Hits         4527     4532       +5     
+ Misses       4320     4315       -5     
Impacted Files Coverage Δ
components/zcash_address/src/lib.rs 31.81% <ø> (+4.54%) :arrow_up:
zcash_primitives/src/transaction/mod.rs 35.97% <0.00%> (ø)
zcash_primitives/src/transaction/tests.rs 94.11% <0.00%> (ø)
zcash_primitives/src/sapling/note_encryption.rs 63.43% <0.00%> (ø)
zcash_proofs/src/circuit/sprout/mod.rs 0.87% <0.00%> (+0.87%) :arrow_up:
zcash_proofs/src/circuit/sprout/input.rs 1.35% <0.00%> (+1.35%) :arrow_up:
zcash_primitives/src/block.rs 39.13% <0.00%> (+2.17%) :arrow_up:
...imitives/src/transaction/components/transparent.rs 43.47% <0.00%> (+2.17%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 37fc286...013eb25. Read the comment docs.

codecov[bot] avatar Jul 12 '22 15:07 codecov[bot]

I'm probably misunderstanding this API, but basically, I want to:

  1. Be able to parse a user string from an "address" field, and identify what type of address it is. Like if the user types in "ztestsapling....." into an address field, I want to show "You typed a sapling address, but it is for the Test network. Please type in a mainnet address"

adityapk00 avatar Jul 12 '22 15:07 adityapk00

  1. Be able to parse a user string from an "address" field, and identify what type of address it is. Like if the user types in "ztestsapling....." into an address field, I want to show "You typed a sapling address, but it is for the Test network. Please type in a mainnet address"

For this specific example, it sounds like what you want is ZcashAddress::convert_if_network, which returns ConversionError::IncorrectNetwork if the networks don't match. This error does not expose the kind of address though (both because as mentioned above AddressKind is an implementation detail, and because I'm not currently convinced that the kind matters to a user in this context).

More generally, the ZcashAddress type is not intended for this kind of introspection, as it is only partially parsed (e.g. it might look like a Sapling address as far as the outer encoding goes, but the pk_d value inside it is invalid). Full introspection is what the user-defined type is for; we intend to provide our own type for this in zcash_primitives or somewhere similar that people could use for this kind of custom input validation, but haven't yet due to time constraints (and because it's somewhat impacted by other ongoing refactors).

str4d avatar Jul 27 '22 23:07 str4d

Thinking about this again. I think I'd prefer one of the following approaches:

  • We get address types into zcash_primitives (we currently have been developing types in zcash_client_backend but will likely refactor them soon). This in combination with ZcashAddress::convert_if_network provides all the requested functionality.
  • We add a dedicated "address checker" struct that acts as a conversion target which drops the address data. That is equivalent to exposing AddressKind but does not commit us to supporting a particular internal structure for it, and it avoids the issue of dealing with pk_d parsing etc. Technically the checker would not detect all possible invalid address encodings, but it would (in combination with ZcashAddress::convert_if_network) be sufficient to cover the requested functionality.

str4d avatar Oct 12 '22 23:10 str4d

We have added introspection methods to the ZcashAddress type to reveal the properties that this PR was intended to expose.

nuttycom avatar Jun 17 '24 19:06 nuttycom