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
10 function decodeHex($hex)
12 $hex=strtoupper($hex);
13 $chars="0123456789ABCDEF";
15 for($i=0;$i<strlen($hex);$i++)
17 $current=(string)strpos($chars,$hex[$i]);
18 $return=(string)bcmul($return,"16",0);
19 $return=(string)bcadd($return,$current,0);
24 function encodeHex($dec)
26 $chars="0123456789ABCDEF";
28 while (bccomp($dec,0)==1)
30 $dv=(string)bcdiv($dec,"16",0);
31 $rem=(integer)bcmod($dec,"16");
33 $return=$return.$chars[$rem];
35 return strrev($return);
38 function decodeBase58($base58)
42 $chars="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
44 for($i=0;$i<strlen($base58);$i++)
46 $current=(string)strpos($chars,$base58[$i]);
47 $return=(string)bcmul($return,"58",0);
48 $return=(string)bcadd($return,$current,0);
51 $return=encodeHex($return);
54 for($i=0;$i<strlen($origbase58)&&$origbase58[$i]=="1";$i++)
59 if(strlen($return)%2!=0)
67 function encodeBase58($hex)
71 die("encodeBase58: uneven number of hex characters");
75 $chars="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
78 while (bccomp($hex,0)==1)
80 $dv=(string)bcdiv($hex,"58",0);
81 $rem=(integer)bcmod($hex,"58");
83 $return=$return.$chars[$rem];
85 $return=strrev($return);
88 for($i=0;$i<strlen($orighex)&&substr($orighex,$i,2)=="00";$i+=2)
96 function hash160ToAddress($hash160,$addressversion=ADDRESSVERSION)
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);
106 function addressToHash160($addr)
108 $addr=decodeBase58($addr);
109 $addr=substr($addr,2,strlen($addr)-10);
113 function checkAddressBTC($addr,$addressversion=ADDRESSVERSION)
115 $addr=decodeBase58($addr);
116 if(strlen($addr)!=50)
120 $version=substr($addr,0,2);
121 if(hexdec($version)>hexdec($addressversion))
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);
132 function checkAddressLTC($addr,$addressversion=ADDRESSVERSION)
134 $addr=decodeBase58($addr);
135 if(strlen($addr)!=50)
139 $version=substr($addr,0,2);
140 if(hexdec($version)!=hexdec($addressversion)) //Changed from ">" to "!=" for LTC
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);
151 function hash160($data)
153 $data=pack("H*" , $data);
154 return strtoupper(hash("ripemd160",hash("sha256",$data,true)));
157 function pubKeyToAddress($pubkey)
159 return hash160ToAddress(hash160($pubkey));
162 function remove0x($string)
164 if(substr($string,0,2)=="0x"||substr($string,0,2)=="0X")
166 $string=substr($string,2);
171 //start of BTC LTC switch
172 function determineValidity($address, $addressType)
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
183 $error_Address_type = "Address type not correctly specified";
184 return $error_Address_type;