-
Notifications
You must be signed in to change notification settings - Fork 22
/
AddressValidation.class.php
53 lines (48 loc) · 1.48 KB
/
AddressValidation.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/*
* Crypto Currency Address Validation Library
* For use with Bitcoin and Zetacoin compatable crypto currency using the secp256k1 ECC curve
*
* @author Daniel Morante
* Some parts may contain work based on Jan Moritz Lindemann, Matyas Danter, and Joey Hewitt
*/
class AddressValidation {
/***
* Tests if the address is valid or not.
*
* @param String Base58 $address
* @return bool
*/
public static function validateAddress($address)
{
$address = hex2bin(Base58::Decode($address));
if(strlen($address) != 25)
return false;
$checksum = substr($address, 21, 4);
$rawAddress = substr($address, 0, 21);
$sha256 = hash('sha256', $rawAddress);
$sha256 = hash('sha256', hex2bin($sha256));
if(substr(hex2bin($sha256), 0, 4) == $checksum)
return true;
else
return false;
}
/***
* Tests if the Wif key (Wallet Import Format) is valid or not.
*
* @param String Base58 $wif
* @return bool
*/
public static function validateWifKey($wif)
{
$key = Base58::Decode($wif, false);
$length = strlen($key);
$firstSha256 = hash('sha256', hex2bin(substr($key, 0, $length - 8)));
$secondSha256 = hash('sha256', hex2bin($firstSha256));
if(substr($secondSha256, 0, 8) == substr($key, $length - 8, 8))
return true;
else
return false;
}
}
?>