diff --git a/src/Makefile.am b/src/Makefile.am index f9dd8ba3..8ea8885d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,5 +5,5 @@ libckpool_la_SOURCES = libckpool.c libckpool.h libckpool_la_LIBADD = @PTHREAD_LIBS@ @MATH_LIBS@ @RT_LIBS@ @JANSSON_LIBS@ bin_PROGRAMS = ckpool -ckpool_SOURCES = ckpool.c ckpool.h +ckpool_SOURCES = ckpool.c ckpool.h gbt.c gbt.h ckpool_LDADD = libckpool.la diff --git a/src/gbt.c b/src/gbt.c new file mode 100644 index 00000000..7183221d --- /dev/null +++ b/src/gbt.c @@ -0,0 +1,77 @@ +/* + * Copyright 2011-2014 Con Kolivas + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) + * any later version. See COPYING for more details. + */ + +#include "config.h" + +#include + +#include "ckpool.h" +#include "libckpool.h" + +static const char *b58chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; + +bool validate_address(connsock_t *cs, const char *address) +{ + json_t *val, *res_val, *valid_val; + char rpc_req[256]; + bool ret = false; + int len, i, j; + + if (unlikely(!address)) { + LOGWARNING("Null address passed to validate_address"); + return ret; + } + len = strlen(address); + if (len < 27 || len > 34) { + LOGWARNING("Invalid address length %d passed to validate_address", len); + return ret; + } + for (i = 0; i < len; i++) { + char c = address[i]; + bool found = false; + + for (j = 0; j < 58; j++) { + if (c == b58chars[j]) { + found = true; + break; + } + } + if (!found) { + LOGWARNING("Invalid char %.1s passed to validate_address", &c); + return ret; + } + } + + snprintf(rpc_req, 256, "{\"method\": \"validateaddress\", \"params\": [\"%s\"]}\n", address); + val = json_rpc_call(cs, rpc_req); + if (!val) { + LOGERR("Failed to get valid json response to validate_address"); + return ret; + } + res_val = json_object_get(val, "result"); + if (!res_val) { + LOGERR("Failed to get result json response to validate_address"); + goto out; + } + valid_val = json_object_get(res_val, "isvalid"); + if (!valid_val) { + LOGERR("Failed to get isvalid json response to validate_address"); + goto out; + } + if (!json_is_true(valid_val)) + LOGWARNING("Bitcoin address %s is NOT valid", address); + else { + LOGDEBUG("Bitcoin address %s IS valid", address); + ret = true; + } +out: + if (val) + json_decref(val); + return ret; +} diff --git a/src/gbt.h b/src/gbt.h new file mode 100644 index 00000000..f4a73e67 --- /dev/null +++ b/src/gbt.h @@ -0,0 +1,10 @@ +/* + * Copyright 2011-2014 Con Kolivas + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) + * any later version. See COPYING for more details. + */ + +bool validate_address(connsock_t *cs, const char *address); diff --git a/src/libckpool.c b/src/libckpool.c index 265656ea..92519408 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -1119,7 +1119,7 @@ void target_from_diff(uchar *target, double diff) if (unlikely(diff == 0.0)) { /* This shouldn't happen but best we check to prevent a crash */ - memset(target, 0, 32); + memset(target, 0xff, 32); return; }