-
Notifications
You must be signed in to change notification settings - Fork 565
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
HV-1973 - Bitcoin address validator #1343
base: main
Are you sure you want to change the base?
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.
Hey,
thanks for submitting the pull request with the new constraint. This looks interesting.
I've added some suggestions inline. Please also take a look at this post https://in.relation.to/2018/01/04/adding-new-constraint-to-engine/ to see about the additional parts that should be added for a constraint.
engine/src/main/java/org/hibernate/validator/constraints/BitcoinAddress.java
Outdated
Show resolved
Hide resolved
*/ | ||
package org.hibernate.validator.constraints; | ||
|
||
public enum BitcoinAddressType { |
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'd suggest moving this enum into constraint itself, see how it is done for ISBN:
hibernate-validator/engine/src/main/java/org/hibernate/validator/constraints/ISBN.java
Lines 74 to 77 in 729cd01
enum Type { | |
ISBN_10, | |
ISBN_13, | |
ANY |
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 did almost that... When you have a chance to take a look please :)
ANY("Bitcoin", null), | ||
P2PKH("Legacy (P2PKH)", "^(1)[a-zA-HJ-NP-Z0-9]{25,61}$"), | ||
P2SH("Nested SegWit (P2SH)", "^(3)[a-zA-HJ-NP-Z0-9]{33}$"), | ||
BECH32("Native SegWit (Bech32)", "^(bc1)[a-zA-HJ-NP-Z0-9]{39,59}$"), | ||
P2WSH("SegWit variant of P2SH (P2WSH)", "^(bc1q)[a-zA-HJ-NP-Z0-9]{58}$"), | ||
P2WPKH("SegWit variant of P2PKH (P2WPKH)", "^(bc1q)[a-zA-HJ-NP-Z0-9]{38}$"), | ||
P2TR("Taproot (P2TR)", "^(bc1p)[a-zA-HJ-NP-Z0-9]{58}$"); | ||
|
||
private final String description; | ||
private final String regex; | ||
|
||
BitcoinAddressType(String description, String regex) { | ||
this.description = description; | ||
this.regex = regex; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public String getRegex() { | ||
return regex; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.description; | ||
} |
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.
and move all the implementation details into the ConstraintValidator implementation itself.
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 did almost that... When you have a chance to take a look please :)
} | ||
|
||
for ( BitcoinAddressType type : this.addressType ) { | ||
Pattern pattern = Pattern.compile( type.getRegex() ); |
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.
Let's have the patterns pre-compiled.
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.
Done! When you have a chance to take a look please :)
BitcoinAddressType[] types = bitcoinAddress.value(); | ||
|
||
this.singleType = ( types.length == 1 ); | ||
|
||
if ( Arrays.stream( types ).anyMatch( type -> type == BitcoinAddressType.ANY ) ) { | ||
this.typesDescription = BitcoinAddressType.ANY.getDescription(); | ||
this.addressType.addAll( | ||
Arrays.stream( BitcoinAddressType.values() ) | ||
.filter( type -> type != BitcoinAddressType.ANY ) | ||
.collect( Collectors.toList() ) ); | ||
return; | ||
} | ||
|
||
Collections.addAll( this.addressType, types ); | ||
this.typesDescription = this.addressType.stream() | ||
.map( BitcoinAddressType::getDescription ) | ||
.collect( Collectors.joining( "; " ) ); |
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.
once the impl details are moved out of the enum here you could still keep them in the enum and maybe also have this prebuilt? e.g.:
public enum BitcoinAddressConstraint {
P2PKH("Legacy (P2PKH)", "^(1)[a-zA-HJ-NP-Z0-9]{25,61}$"),
P2SH("Nested SegWit (P2SH)", "^(3)[a-zA-HJ-NP-Z0-9]{33}$"),
BECH32("Native SegWit (Bech32)", "^(bc1)[a-zA-HJ-NP-Z0-9]{39,59}$"),
P2WSH("SegWit variant of P2SH (P2WSH)", "^(bc1q)[a-zA-HJ-NP-Z0-9]{58}$"),
P2WPKH("SegWit variant of P2PKH (P2WPKH)", "^(bc1q)[a-zA-HJ-NP-Z0-9]{38}$"),
P2TR("Taproot (P2TR)", "^(bc1p)[a-zA-HJ-NP-Z0-9]{58}$");
BitcoinAddressConstraint(String description, String pattern) {
this.pattern = Pattern.compile( pattern );
}
private static final EnumMap<BitcoinAddress.Type, Collection<BitcoinAddressConstraint>> constraints;
static {
constraints = new EnumMap<>( BitcoinAddress.Type.class );
constraints.put( BitcoinAddress.Type.ANY, Arrays.asList( BitcoinAddressConstraint.values() ) );
constraints.put( BitcoinAddress.Type.P2PKH, Collections.singleton( P2PKH ) );
constraints.put( BitcoinAddress.Type.P2SH, Collections.singleton( P2SH ) );
constraints.put( BitcoinAddress.Type.BECH32, Collections.singleton( BECH32 ) );
constraints.put( BitcoinAddress.Type.P2WSH, Collections.singleton( P2WSH ) );
constraints.put( BitcoinAddress.Type.P2WPKH, Collections.singleton( P2WPKH ) );
constraints.put( BitcoinAddress.Type.P2TR, Collections.singleton( P2TR ) );
}
...
}
and then in the initialize:
public void initialize(BitcoinAddress bitcoinAddress) {
Set<BitcoinAddressConstraint> constraints = new HashSet<>();
for ( BitcoinAddress.Type type : bitcoinAddress.value() ) {
constraints.addAll( BitcoinAddressConstraint.constraints.get( type ) );
}
this.singleType = constraints.size() == 1;
}
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 did almost that... When you have a chance to take a look please :)
P2PKH("Legacy (P2PKH)", "^(1)[a-zA-HJ-NP-Z0-9]{25,61}$"), | ||
P2SH("Nested SegWit (P2SH)", "^(3)[a-zA-HJ-NP-Z0-9]{33}$"), | ||
BECH32("Native SegWit (Bech32)", "^(bc1)[a-zA-HJ-NP-Z0-9]{39,59}$"), | ||
P2WSH("SegWit variant of P2SH (P2WSH)", "^(bc1q)[a-zA-HJ-NP-Z0-9]{58}$"), | ||
P2WPKH("SegWit variant of P2PKH (P2WPKH)", "^(bc1q)[a-zA-HJ-NP-Z0-9]{38}$"), | ||
P2TR("Taproot (P2TR)", "^(bc1p)[a-zA-HJ-NP-Z0-9]{58}$"); |
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.
BTW I see that this description is used in the validation message. But as they use regular words they may need to be translated as well... So I'd suggest exploring an option of placing the description in the translation file as well and then let EL do the string interpolation.
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.
Done. When you have a chance to take a look please :)
Co-authored-by: Marko Bekhta <marko-bekhta@users.noreply.github.com>
Thanks for your pull request! This pull request does not follow the contribution rules. Could you have a look? ❌ All commit messages should start with a JIRA issue key matching pattern › This message was automatically generated. |
I believe I fixed everything that was missing :) |
Bitcoin address validator.
More translations are still missing. Only english and portuguese have been provided so far.
See https://hibernate.atlassian.net/browse/HV-1973.