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

HV-1973 - Bitcoin address validator #1343

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

jyoshiriro
Copy link

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.

Copy link
Member

@marko-bekhta marko-bekhta left a 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.

*/
package org.hibernate.validator.constraints;

public enum BitcoinAddressType {
Copy link
Member

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:

Copy link
Author

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 :)

Comment on lines 10 to 37
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;
}
Copy link
Member

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.

Copy link
Author

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() );
Copy link
Member

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.

Copy link
Author

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 :)

Comment on lines 37 to 53
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( "; " ) );
Copy link
Member

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;
}

Copy link
Author

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 :)

Comment on lines 11 to 16
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}$");
Copy link
Member

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.

Copy link
Author

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>
@hibernate-github-bot
Copy link

hibernate-github-bot bot commented Apr 15, 2024

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 HV-\d+
    ↳ Offending commits: [404f529]

› This message was automatically generated.

@jyoshiriro
Copy link
Author

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.

I believe I fixed everything that was missing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants