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