forked from dogecoinfoundation/libdogecoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.c
227 lines (198 loc) · 8.9 KB
/
rest.c
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
The MIT License (MIT)
Copyright (c) 2024 edtubbs, bluezr
Copyright (c) 2024 The Dogecoin Foundation
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include <dogecoin/rest.h>
#include <dogecoin/blockchain.h>
#include <dogecoin/koinu.h>
#include <dogecoin/headersdb_file.h>
#include <dogecoin/spv.h>
#include <dogecoin/wallet.h>
#define TIMESTAMP_MAX_LEN 32
/**
* This function is called when an http request is received
* It handles the request and sends a response
*
* @param req the request
* @param arg the client
*
* @return Nothing.
*/
void dogecoin_http_request_cb(struct evhttp_request *req, void *arg) {
dogecoin_spv_client* client = (dogecoin_spv_client*)arg;
dogecoin_wallet* wallet = (dogecoin_wallet*)client->sync_transaction_ctx;
if (!wallet) {
evhttp_send_error(req, HTTP_INTERNAL, "Internal Server Error");
return;
}
const struct evhttp_uri* uri = evhttp_request_get_evhttp_uri(req);
const char* path = evhttp_uri_get_path(uri);
struct evbuffer *evb = NULL;
evb = evbuffer_new();
if (!evb) {
evhttp_send_error(req, HTTP_INTERNAL, "Internal Server Error");
return;
}
if (strcmp(path, "/getBalance") == 0) {
int64_t balance = dogecoin_wallet_get_balance(wallet);
char balance_str[32] = {0};
koinu_to_coins_str(balance, balance_str);
evbuffer_add_printf(evb, "Wallet balance: %s\n", balance_str);
} else if (strcmp(path, "/getAddresses") == 0) {
vector* addresses = vector_new(10, dogecoin_free);
dogecoin_wallet_get_addresses(wallet, addresses);
for (unsigned int i = 0; i < addresses->len; i++) {
char* address = vector_idx(addresses, i);
evbuffer_add_printf(evb, "address: %s\n", address);
}
vector_free(addresses, true);
} else if (strcmp(path, "/getTransactions") == 0) {
char wallet_total[21];
dogecoin_mem_zero(wallet_total, 21);
uint64_t wallet_total_u64 = 0;
if (HASH_COUNT(utxos) > 0) {
dogecoin_utxo* utxo;
dogecoin_utxo* tmp;
HASH_ITER(hh, utxos, utxo, tmp) {
if (!utxo->spendable) {
// For spent UTXOs
evbuffer_add_printf(evb, "%s\n", "----------------------");
evbuffer_add_printf(evb, "txid: %s\n", utils_uint8_to_hex(utxo->txid, sizeof utxo->txid));
evbuffer_add_printf(evb, "vout: %d\n", utxo->vout);
evbuffer_add_printf(evb, "address: %s\n", utxo->address);
evbuffer_add_printf(evb, "script_pubkey: %s\n", utxo->script_pubkey);
evbuffer_add_printf(evb, "amount: %s\n", utxo->amount);
evbuffer_add_printf(evb, "confirmations: %d\n", utxo->confirmations);
evbuffer_add_printf(evb, "spendable: %d\n", utxo->spendable);
evbuffer_add_printf(evb, "solvable: %d\n", utxo->solvable);
wallet_total_u64 += coins_to_koinu_str(utxo->amount);
}
}
}
// Convert and print totals for spent UTXOs.
koinu_to_coins_str(wallet_total_u64, wallet_total);
evbuffer_add_printf(evb, "Spent Balance: %s\n", wallet_total);
} else if (strcmp(path, "/getUTXOs") == 0) {
char wallet_total[21];
dogecoin_mem_zero(wallet_total, 21);
uint64_t wallet_total_u64_unspent = 0;
dogecoin_utxo* utxo;
dogecoin_utxo* tmp;
HASH_ITER(hh, wallet->utxos, utxo, tmp) {
if (utxo->spendable) {
// For unspent UTXOs
evbuffer_add_printf(evb, "----------------------\n");
evbuffer_add_printf(evb, "Unspent UTXO:\n");
evbuffer_add_printf(evb, "txid: %s\n", utils_uint8_to_hex(utxo->txid, sizeof(utxo->txid)));
evbuffer_add_printf(evb, "vout: %d\n", utxo->vout);
evbuffer_add_printf(evb, "address: %s\n", utxo->address);
evbuffer_add_printf(evb, "script_pubkey: %s\n", utxo->script_pubkey);
evbuffer_add_printf(evb, "amount: %s\n", utxo->amount);
evbuffer_add_printf(evb, "spendable: %d\n", utxo->spendable);
evbuffer_add_printf(evb, "solvable: %d\n", utxo->solvable);
wallet_total_u64_unspent += coins_to_koinu_str(utxo->amount);
}
}
// Convert and print totals for unspent UTXOs.
koinu_to_coins_str(wallet_total_u64_unspent, wallet_total);
evbuffer_add_printf(evb, "Total Unspent: %s\n", wallet_total);
} else if (strcmp(path, "/getWallet") == 0) {
// Get the wallet file
FILE* file = wallet->dbfile;
if (file == NULL) {
evhttp_send_error(req, HTTP_NOTFOUND, "Wallet file not found");
evbuffer_free(evb);
return;
}
// Get the size of the wallet file
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
rewind(file);
// Read the wallet file into a buffer
char* buffer = malloc(file_size);
if (buffer == NULL) {
fclose(file);
evhttp_send_error(req, HTTP_INTERNAL, "Internal Server Error");
evbuffer_free(evb);
return;
}
size_t result = fread(buffer, 1, file_size, file);
if (!result) {
evbuffer_free(evb);
free(buffer);
return;
}
// Add the buffer to the response buffer
evbuffer_add(evb, buffer, file_size);
// Set the Content-Type header to "application/octet-stream" for binary data
evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", "application/octet-stream");
// Clean up
free(buffer);
} else if (strcmp(path, "/getHeaders") == 0) {
// Get the headers file
dogecoin_headers_db* headers_db = (dogecoin_headers_db *)(client->headers_db_ctx);
FILE* file = headers_db->headers_tree_file;
if (file == NULL) {
evhttp_send_error(req, HTTP_NOTFOUND, "Headers file not found");
evbuffer_free(evb);
return;
}
// Get the size of the headers file
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
rewind(file);
// Read the headers file into a buffer
char* buffer = malloc(file_size);
if (buffer == NULL) {
fclose(file);
evhttp_send_error(req, HTTP_INTERNAL, "Internal Server Error");
evbuffer_free(evb);
return;
}
size_t result = fread(buffer, 1, file_size, file);
if (!result) {
evbuffer_free(evb);
free(buffer);
return;
}
// Add the buffer to the response buffer
evbuffer_add(evb, buffer, file_size);
// Set the Content-Type header to "application/octet-stream" for binary data
evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", "application/octet-stream");
// Clean up
free(buffer);
} else if (strcmp(path, "/getChaintip") == 0) {
dogecoin_blockindex* tip = client->headers_db->getchaintip(client->headers_db_ctx);
evbuffer_add_printf(evb, "Chain tip: %d\n", tip->height);
} else if (strcmp(path, "/getTimestamp") == 0) {
dogecoin_blockindex* tip = client->headers_db->getchaintip(client->headers_db_ctx);
char s[TIMESTAMP_MAX_LEN];
time_t t = tip->header.timestamp;
struct tm *p = localtime(&t);
strftime(s, sizeof(s), "%F %T", p);
evbuffer_add_printf(evb, "%s\n", s);
} else {
evhttp_send_error(req, HTTP_NOTFOUND, "Not Found");
evbuffer_free(evb);
return;
}
evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", "text/plain");
evhttp_send_reply(req, HTTP_OK, "OK", evb);
evbuffer_free(evb);
}