Merge branch 'master' of https://repo.or.cz/libcoin
[libcoin.git] / utils / ethc.c
bloba4280e9f8222ee10cbfe2011f64f06dcc1677f07
1 #include <libex/array.h>
2 #include <libex/file.h>
3 #include "ether.h"
4 #include "linenoise.h"
5 #if 0
6 int mint (const char *to, const char *amount_str) {
7 if (!to) {
8 dprintf(STDERR_FILENO, "destination address expected\n");
9 return 1;
11 if (!amount_str) {
12 dprintf(STDERR_FILENO, "amount expected\n");
13 return 1;
16 str_t *contract_def = load_all_file(contract_file, 64, 65535);
17 if (contract_def) {
18 int rc = -1;
19 eth_contract_t *contract = eth_load_contract(contract_addr, contract_def->ptr, contract_def->len);
20 eth_ctx_t ctx;
21 int unlocked = eth_unlock_account(auth_addr, pass);
22 switch (unlocked) {
23 case -1:
24 dprintf(STDERR_FILENO, "%d: %s\n", eth_errcode, eth_errmsg);
25 goto done;
26 case 0:
27 dprintf(STDERR_FILENO, "illegal password\n");
28 goto done;
29 default: break;
31 mpz_t amount;
32 mpz_init_set_str(amount, amount_str, 10);
33 memset(&ctx, 0, sizeof(eth_ctx_t));
34 if (-1 == eth_prepare_exec(contract, from_addr, "mint(address,uint256)", &ctx))
35 goto done;
36 eth_param_address(&ctx, to);
37 eth_param_int(&ctx, amount);
38 if (-1 == (rc = eth_exec(&ctx, ETH_NULL, NULL, ETH_NULL)))
39 dprintf(STDERR_FILENO, "%d: %s\n", eth_errcode, eth_errmsg);
40 else
41 dprintf(STDOUT_FILENO, "%s\n", ctx.result->ptr);
42 done:
43 eth_ctx_clear(&ctx);
44 eth_free_contract(contract);
45 free(contract_def);
46 return -rc;
48 return 1;
50 #endif
51 static void linenoise_ac (char *buf, int *posp, int len, size_t cols) {
52 static const char *cstr = "quit";
53 if (len > 0 && len < 4 && *posp > 0) {
54 for (int i = 0; i < *posp; ++i)
55 if (buf[i] != cstr[i]) return;
56 strcpy(buf, "quit");
57 *posp = strlen(buf);
61 int main (int argc, const char *argv[]) {
62 char *line;
63 curl_global_init(CURL_GLOBAL_ALL);
64 linenoiseHistoryLoadFile("history.txt");
65 linenoiseACHook = linenoise_ac;
66 while ((line = linenoise("> "))) {
67 if (*line) {
68 char *ptr = line;
69 strptr_t tok;
70 size_t len;
71 if (!strcmp(line, "quit"))
72 break;
73 len = strlen(line);
74 if (0 == strntok(&ptr, &len, CONST_STR_LEN(STR_SPACES), &tok)) {
75 if (0 == cmpstr(tok.ptr, tok.len, CONST_STR_LEN("new_account"))) {
76 if (0 == strntok(&ptr, &len, CONST_STR_LEN(STR_SPACES), &tok)) {
77 char address [65], *pass = strndup(tok.ptr, tok.len);
78 if (eth_new_account(pass, address, sizeof(address)))
79 printf("%s\n", address);
80 else
81 printf("%d: %s\n", coin_errcode, coin_errmsg);
82 free(pass);
86 linenoiseHistoryAdd(line);
89 linenoiseHistorySaveFile("history.txt");
90 curl_global_cleanup();
91 return 0;