forked from PascalCoin/FreePasa.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pascal.php
195 lines (165 loc) · 5.08 KB
/
pascal.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
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
189
190
191
192
193
194
195
<?php
namespace Pascal;
function rpc(string $method, array $params = [])
{
static $id = 0;
$rpc = [
'id' => $id++,
'jsonrpc' => '2.0',
'method' => $method,
'params' => $params,
];
if(!isset($rpc['params']['fee'])) {
$rpc['params']['fee'] = '0.0000';
} else {
$rpc['params']['fee'] = '0.0001';
}
$ch = curl_init(NODE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($rpc));
$response = curl_exec($ch);
\curl_close($ch);
if ($response === false) {
throw new \Exception('Unable to connect to node ' . NODE, 100);
}
$result = json_decode($response, true);
if(isset($result['result'])) {
return $result['result'];
}
if(isset($result['error']))
{
// if free didn't work out, try with fee
if($rpc['params']['fee'] === '0.0000') {
return rpc($method, $rpc['params']);
}
throw new \Exception($result['error']['message'], $result['error']['code']);
}
die('Invalid result: ' . print_r($result, true));
}
function hasPasa($publicKey)
{
return count(rpc('findaccounts', ['b58_pubkey' => $publicKey]));
}
function getDisbursableAccount()
{
$availableAccounts = rpc('getwalletaccounts', [
'b58_pubkey' => WALLET_PUBKEY,
'start' => 0,
'max' => 10
]);
foreach($availableAccounts as $account) {
$pubKey = rpc('decodepubkey', ['enc_pubkey' => $account['enc_pubkey']]);
if($pubKey['b58_pubkey'] !== WALLET_PUBKEY) {
continue;
}
if(\Database\Verifications\isDisbursed($account['account']) === false &&
$account['account'] != ACCOUNT_SIGNER &&
$account['account'] != ACCOUNT_FAUCET &&
$account['account'] != ACCOUNT_AFFILIATE)
{
return $account;
}
}
return null;
}
function sendPasa($b58Pubkey, $last4)
{
$account = getDisbursableAccount();
$result = rpc('changekey', [
'account' => $account['account'],
'account_signer' => ACCOUNT_SIGNER,
'new_b58_pubkey' => $b58Pubkey,
'payload' => encodePayload('freepasa.org'),
'payload_method' => 'none'
]);
addPascalToAccount($account['account']);
return [
'ophash' => $result['ophash'],
'account' => $account['account']
];
}
function lock() {
rpc('lock', []);
}
function unlock() {
rpc('unlock', ['pwd' => WALLET_PASSWORD]);
}
function addPascalToAccount($account)
{
// fetch account balance
$accountData = getAccount($account);
$balance = (float)$accountData['balance'];
// if the account has a balance..
if($balance > 0)
{
// and the amount is bigger than the faucet amount
if($balance * 10000 > FAUCET_AMOUNT) {
// ..send the diff to the faucet
rpc('sendto', [
'target' => ACCOUNT_FAUCET,
'sender' => $account,
'amount' => (($balance * 10000) - (FAUCET_AMOUNT)) / 10000,
'payload' => encodePayload('freepasa.org / getting started with 0.0010'),
'payload_method' => 'none'
]);
} else {
// just send the delat
$result = rpc('sendto', [
'target' => $account,
'sender' => ACCOUNT_FAUCET,
'amount' => (FAUCET_AMOUNT / 10000) - $balance,
'payload' => encodePayload('freepasa.org / getting started with 0.0010'),
'payload_method' => 'none'
]);
}
} else {
$result = rpc('sendto', [
'target' => $account,
'sender' => ACCOUNT_FAUCET,
'amount' => FAUCET_AMOUNT / 10000,
'payload' => encodePayload('freepasa.org / getting started with 0.0010'),
'payload_method' => 'none'
]);
}
return $result['ophash'];
}
function sendAffiliate($affiliateAccount, $disbursedAccount)
{
$result = rpc('sendto', [
'target' => $affiliateAccount,
'sender' => ACCOUNT_AFFILIATE,
'amount' => AFFILIATE_AMOUNT / 10000,
'payload' => encodePayload('affiliate for ' . $disbursedAccount),
'payload_method' => 'none'
]);
return $result['ophash'];
}
function encodePayload($payload) {
$hex = '';
for ($i = 0; $i < strlen($payload); $i++)
{
$ord = ord($payload[$i]);
$hexCode = dechex($ord);
$hex .= substr('0'.$hexCode, -2);
}
return strtoupper($hex);
}
function nodeStatus() {
return rpc('nodestatus');
}
function decodePublicKey($b58PubKey) {
rpc('decodepubkey', ['b58_pubkey' => $b58PubKey]);
}
function withChecksum($account) {
return $account . '-' . ((((int)$account * 101) % 89)+10);
}
function findOperation($opHash) {
return rpc('findoperation', ['ophash' => $opHash]);
}
function getAccount(int $account) {
return rpc('getaccount', ['account' => $account]);
}
function getWalletAccountsCount() {
return rpc('getwalletaccountscount', ['b58_pubkey' => WALLET_PUBKEY]);
}