Ask your WordPress questions! Pay money and get answers fast! (more info)

Gravity forms & bitcoin address Validation

  • SOLVED

Im trying to use this php bitcoin validation script in conjunction with gravity forms using the gform validation hook. if the user enters an invalid bitcoin address he is prompted to enter a correct address

[[LINK href="http://rosettacode.org/wiki/Bitcoin/address_validation#PHP"]]http://rosettacode.org/wiki/Bitcoin/address_validation#PHP[[/LINK]]

https://www.gravityhelp.com/documentation/article/using-the-gravity-forms-gform-validation-hook/

Answers (1)

2015-07-09

Bob answers:

Add this code in functions.php file of your theme.

It is not tested so not sure if it will work or not.

<strong>Note:</strong> In below code there is line <em>add_filter( 'gform_field_validation_6_1', 'validate_bitcoin_address', 10, 4 );</em>
you need to change 6 with your form id and 1 with your field id. Here 6 and 1 are place holder only.



function decodeBase58($input) {
$alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

$out = array_fill(0, 25, 0);
for($i=0;$i<strlen($input);$i++){
if(($p=strpos($alphabet, $input[$i]))===false){
throw new \Exception("invalid character found");
}
$c = $p;
for ($j = 25; $j--; ) {
$c += (int)(58 * $out[$j]);
$out[$j] = (int)($c % 256);
$c /= 256;
$c = (int)$c;
}
if($c != 0){
throw new \Exception("address too long");
}
}

$result = "";
foreach($out as $val){
$result .= chr($val);
}

return $result;
}
function validate_bitcoin($address){
$decoded = decodeBase58($address);

$d1 = hash("sha256", substr($decoded,0,21), true);
$d2 = hash("sha256", $d1, true);

if(substr_compare($decoded, $d2, 21, 4)){
throw new \Exception("bad digest");
}
return true;
}

//The following declaration targets field 1 in form 6
add_filter( 'gform_field_validation_6_1', 'validate_bitcoin_address', 10, 4 );
function validate_bitcoin_address( $result, $value, $form, $field ){

if ( $result['is_valid'] ) {
$isvalid_bitcoin = validate_bitcoin( $value );
if($isvalid_bitcoin != true){
$result['is_valid'] = false;
$result['message'] = 'Please enter valid bitcoin no.';
}
}
return $result;
}


pete70 comments:

Fatal error: Call to undefined function address() when i am using preview mode on gravity forms.

When i add the modual live it just spins


Bob comments:

Replace function
<strong>Old code</strong>

function validate_bitcoin_address( $result, $value, $form, $field ){
if ( $result['is_valid'] ) {
$isvalid_bitcoin = validate_bitcoin( $value );
if($isvalid_bitcoin != true){
$result['is_valid'] = false;
$result['message'] = 'Please enter valid bitcoin no.';

}
}
return $result;
}

<strong>New Code</strong>

function validate_bitcoin_address( $result, $value, $form, $field ){
if ( $result['is_valid'] ) {
try{
validate_bitcoin( $value );
}catch(\Exception $e){
$message = $e->getMessage();
$result['is_valid'] = false;
$result['message'] = 'Please enter valid bitcoin address.';
}
}

return $result;

}


pete70 comments:

Perfect. Code works perfectly. Thank you for your concise and fast response BOB!......