Validate altcoins
[elbandi:minifaucet.git] / validator.php
1 <?php
2 //hex input must be in uppercase, with no leading 0x
3 //This Script will test for both BTC and LTC depending on Input. There is no error checking. 
4 //Included at the bottom is a short test of the code, feel free to reuse or eliminate.
5 //This Script has been Modified by CrazyRabbit with help from Pooler to Validate LTC address
6 //Original Version by theymos
7 //Works without being connected to Litecoin Server
8 //To ask questions follow in the litecoin Forums Thread: //http://forum.litecoin.net/index.php/topic,521.0.html
9
10 function decodeHex($hex)
11 {
12         $hex=strtoupper($hex);
13         $chars="0123456789ABCDEF";
14         $return="0";
15         for($i=0;$i<strlen($hex);$i++)
16         {
17                 $current=(string)strpos($chars,$hex[$i]);
18                 $return=(string)bcmul($return,"16",0);
19                 $return=(string)bcadd($return,$current,0);
20         }
21         return $return;
22 }
23
24 function encodeHex($dec)
25 {
26         $chars="0123456789ABCDEF";
27         $return="";
28         while (bccomp($dec,0)==1)
29         {
30                 $dv=(string)bcdiv($dec,"16",0);
31                 $rem=(integer)bcmod($dec,"16");
32                 $dec=$dv;
33                 $return=$return.$chars[$rem];
34         }
35         return strrev($return);
36 }
37
38 function decodeBase58($base58)
39 {
40         $origbase58=$base58;
41         
42         $chars="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
43         $return="0";
44         for($i=0;$i<strlen($base58);$i++)
45         {
46                 $current=(string)strpos($chars,$base58[$i]);
47                 $return=(string)bcmul($return,"58",0);
48                 $return=(string)bcadd($return,$current,0);
49         }
50         
51         $return=encodeHex($return);
52         
53         //leading zeros
54         for($i=0;$i<strlen($origbase58)&&$origbase58[$i]=="1";$i++)
55         {
56                 $return="00".$return;
57         }
58         
59         if(strlen($return)%2!=0)
60         {
61                 $return="0".$return;
62         }
63         
64         return $return;
65 }
66
67 function encodeBase58($hex)
68 {
69         if(strlen($hex)%2!=0)
70         {
71                 die("encodeBase58: uneven number of hex characters");
72         }
73         $orighex=$hex;
74         
75         $chars="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
76         $hex=decodeHex($hex);
77         $return="";
78         while (bccomp($hex,0)==1)
79         {
80                 $dv=(string)bcdiv($hex,"58",0);
81                 $rem=(integer)bcmod($hex,"58");
82                 $hex=$dv;
83                 $return=$return.$chars[$rem];
84         }
85         $return=strrev($return);
86         
87         //leading zeros
88         for($i=0;$i<strlen($orighex)&&substr($orighex,$i,2)=="00";$i+=2)
89         {
90                 $return="1".$return;
91         }
92         
93         return $return;
94 }
95
96 function hash160ToAddress($hash160,$addressversion=ADDRESSVERSION)
97 {
98         $hash160=$addressversion.$hash160;
99         $check=pack("H*" , $hash160);
100         $check=hash("sha256",hash("sha256",$check,true));
101         $check=substr($check,0,8);
102         $hash160=strtoupper($hash160.$check);
103         return encodeBase58($hash160);
104 }
105
106 function addressToHash160($addr)
107 {
108         $addr=decodeBase58($addr);
109         $addr=substr($addr,2,strlen($addr)-10);
110         return $addr;
111 }
112
113 function checkAddressBTC($addr,$addressversion=ADDRESSVERSION)
114 {
115         $addr=decodeBase58($addr);
116         if(strlen($addr)!=50)
117         {
118                 return false;
119         }
120         $version=substr($addr,0,2);
121         if(hexdec($version)>hexdec($addressversion)) 
122         {
123                 return false;
124         }
125         $check=substr($addr,0,strlen($addr)-8);
126         $check=pack("H*" , $check);
127         $check=strtoupper(hash("sha256",hash("sha256",$check,true)));
128         $check=substr($check,0,8);
129         return $check==substr($addr,strlen($addr)-8);
130 }
131
132 function checkAddressLTC($addr,$addressversion=ADDRESSVERSION)
133 {
134         $addr=decodeBase58($addr);
135         if(strlen($addr)!=50)
136         {
137                 return false;
138         }
139         $version=substr($addr,0,2);
140         if(hexdec($version)!=hexdec($addressversion)) //Changed from ">" to "!=" for LTC
141         {
142                 return false;
143         }
144         $check=substr($addr,0,strlen($addr)-8);
145         $check=pack("H*" , $check);
146         $check=strtoupper(hash("sha256",hash("sha256",$check,true)));
147         $check=substr($check,0,8);
148         return $check==substr($addr,strlen($addr)-8);
149 }
150
151 function hash160($data)
152 {
153         $data=pack("H*" , $data);
154         return strtoupper(hash("ripemd160",hash("sha256",$data,true)));
155 }
156
157 function pubKeyToAddress($pubkey)
158 {
159         return hash160ToAddress(hash160($pubkey));
160 }
161
162 function remove0x($string)
163 {
164         if(substr($string,0,2)=="0x"||substr($string,0,2)=="0X")
165         {
166                 $string=substr($string,2);
167         }
168         return $string;
169 }
170
171 //start of BTC LTC switch
172 function determineValidity($address, $addressType)
173 {
174         // https://en.bitcoin.it/wiki/List_of_address_prefixes
175         switch ($addressType) {
176                 case "BTC": return checkAddressBTC($address,"00");
177                 case "LTC": return checkAddressLTC($address,"30");
178                 case "NMC": return checkAddressLTC($address,"34"); // https://github.com/namecoin/namecoin/blob/master/src/namecoin.cpp#L2485
179                 case "BTCTEST": return checkAddressLTC($address,"6F");
180                 case "NVC": return checkAddressLTC($address,"08"); // https://github.com/CryptoManiac/novacoin/blob/master/src/base58.h#L279
181                 case "PPC": return checkAddressLTC($address,"37"); // https://github.com/ppcoin/ppcoin/blob/master/src/base58.h#L267
182                 default:
183                         $error_Address_type = "Address type not correctly specified";
184                         return $error_Address_type;
185         }
186 }