-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
70 lines (59 loc) · 1.71 KB
/
api.php
File metadata and controls
70 lines (59 loc) · 1.71 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
<?php
$rpcUser = "test";
$rpcPass = "test";
$rpcUrl = "http://localhost:19918/";
function rpc($method, $params = []) {
global $rpcUser, $rpcPass, $rpcUrl;
$payload = json_encode([
"jsonrpc" => "1.0",
"id" => "web",
"method" => $method,
"params" => $params
]);
$ch = curl_init($rpcUrl);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "$rpcUser:$rpcPass",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => ["Content-Type: application/json"]
]);
$res = curl_exec($ch);
if ($res === false) {
http_response_code(500);
exit("RPC error: " . curl_error($ch));
}
$data = json_decode($res, true);
if ($data["error"]) {
http_response_code(500);
exit($data["error"]["message"]);
}
return $data["result"];
}
$action = $_GET["action"] ?? "";
if ($action === "utxos") {
$address = $_GET["address"];
echo json_encode(
rpc("listunspent", [1, 9999999, [$address]])
);
}
if ($action === "broadcast") {
$raw = json_decode(file_get_contents("php://input"), true)["rawtx"];
echo json_encode([
"txid" => rpc("sendrawtransaction", [$raw])
]);
}
if ($action === "balance") {
$address = $_GET["address"];
rpc("importaddress", [$address, "", false]);
$utxos = rpc("listunspent", [0, 9999999, [$address], true]);
$total = 0;
foreach ($utxos as $utxo) {
$total += $utxo["amount"];
}
$balance = $total;
echo json_encode([
"balance" => $balance
]);
}