librustzcash
librustzcash copied to clipboard
Allow reading address kind and network
Allow reading the type of address and the network that it can be used on. Useful for tests.
Codecov Report
Merging #586 (013eb25) into main (37fc286) will increase coverage by
0.05%
. The diff coverage isn/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.
I'm probably misunderstanding this API, but basically, I want to:
- 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 theTest
network. Please type in amainnet
address"
- 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).
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 inzcash_client_backend
but will likely refactor them soon). This in combination withZcashAddress::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 withpk_d
parsing etc. Technically the checker would not detect all possible invalid address encodings, but it would (in combination withZcashAddress::convert_if_network
) be sufficient to cover the requested functionality.
We have added introspection methods to the ZcashAddress
type to reveal the properties that this PR was intended to expose.