diff --git a/CMakeLists.txt b/CMakeLists.txt index c14e851..19ab3d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,7 @@ endif() find_package(Threads) -add_library(abieos STATIC src/abi.cpp src/crypto.cpp include/eosio/fpconv.c) +add_library(abieos STATIC src/abi.cpp src/crypto.cpp include/eosio/fpconv.c src/abieos.cpp) target_include_directories(abieos PUBLIC include external/rapidjson/include) add_library(abieos_module MODULE src/abieos.cpp src/abi.cpp src/crypto.cpp include/eosio/fpconv.c) @@ -79,3 +79,5 @@ endif() if (NOT ABIEOS_ONLY_LIBRARY) add_subdirectory(tools) endif() + +install(TARGETS abieos DESTINATION .) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index cf87dd8..4baf89e 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -7,3 +7,9 @@ target_link_libraries(name abieos ${CMAKE_THREAD_LIBS_INIT}) add_custom_command( TARGET name POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $ ${CMAKE_CURRENT_BINARY_DIR}/name2num ) add_custom_command( TARGET name POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $ ${CMAKE_CURRENT_BINARY_DIR}/num2name ) + +add_executable(hex_to_json hex_to_json.c) +target_link_libraries(hex_to_json abieos ${CMAKE_THREAD_LIBS_INIT}) + +add_custom_command( TARGET hex_to_json POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $ ${CMAKE_CURRENT_BINARY_DIR}/json2hex ) +add_custom_command( TARGET hex_to_json POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $ ${CMAKE_CURRENT_BINARY_DIR}/hex2json ) diff --git a/tools/hex_to_json.c b/tools/hex_to_json.c new file mode 100644 index 0000000..46ff533 --- /dev/null +++ b/tools/hex_to_json.c @@ -0,0 +1,115 @@ +#include "../src/abieos.h" +#include +#include +#include +#include +#include + +void usage(char* name, bool reverse) { + if (reverse) { + fprintf(stderr, "Usage: %s abi_file.json type json\n", name); + fputs("This example takes a json value, and returns the corresponding abieos encoded hex string\n", stderr); + fputs("Example:\n", stderr); + fprintf(stderr, "$ ./%s transaction.abi.json transaction '{your json here}'\n", name); + } else { + fprintf(stderr, "Usage: %s abi_file.json hex-type hex-string\n", name); + fputs("This example takes a abieos encoded hex string, and returns the corresponding json\n", stderr); + fputs("Example:\n", stderr); + fprintf(stderr, + "$ ./%s transaction.abi.json transaction " + "AE0D635CDCAC90A6DCFA000000000100A6823403EA3055000000572D3CCDCD0100AEAA4AC15CFD4500000000A8ED32323B00AE" + "AA4AC15CFD4500000060D234CD3DA06806000000000004454F53000000001A746865206772617373686F70706572206C696573" + "20686561767900\n", + name); + } +} + +int main(int argc, char* argv[]) { + const char* contract_str = "eosio"; + bool reverse = false; + char* program_name = basename(argv[0]); + + if (strcasecmp(program_name, "json2hex") == 0) { + reverse = true; + } + + if (argc != 4) { + usage(program_name, reverse); + return 1; + } + char* abi_filename = argv[1]; + char* hex_type = argv[2]; + char* hex_json_string = argv[3]; + + char* source = NULL; + FILE* abi_file = fopen(abi_filename, "r"); + if (abi_file == NULL) { + fprintf(stderr, "unable to open abi_file.json %s\n", abi_filename); + return -1; + } + /* Go to the end of the file. */ + if (fseek(abi_file, 0L, SEEK_END) == 0) { + /* Get the size of the file. */ + long bufsize = ftell(abi_file); + if (bufsize == -1) { + /* Error */ + fprintf(stderr, "ftell error\n"); + return -1; + } + + /* Allocate our buffer to that size. */ + source = malloc(sizeof(char) * (bufsize + 1)); + + /* Go back to the start of the file. */ + if (fseek(abi_file, 0L, SEEK_SET) != 0) { /* Error */ + } + + /* Read the entire file into memory. */ + size_t newLen = fread(source, sizeof(char), bufsize, abi_file); + if (ferror(abi_file) != 0) { + fputs("Error reading file\n", stderr); + } else { + source[newLen++] = '\0'; /* Just to be safe. */ + } + } + fclose(abi_file); + + abieos_context* context = abieos_create(); + if (context == NULL) { + fprintf(stderr, "Unable to create context\n"); + free(source); + return -1; + } + + uint64_t contract_name = abieos_string_to_name(context, contract_str); + if (contract_name == 0) { + fprintf(stderr, "Error: abieos_string_to_name %s\n", abieos_get_error(context)); + } else { + abieos_bool result = abieos_set_abi(context, contract_name, source); + if (!result) { + fprintf(stderr, "Error: abieos_set_abi %s\n", abieos_get_error(context)); + } else { + if (reverse) { + abieos_bool j2b_result = abieos_json_to_bin(context, contract_name, hex_type, hex_json_string); + if (j2b_result) { + const char* hex = abieos_get_bin_hex(context); + fprintf(stdout, "%s\n", hex); + } else { + fprintf(stderr, "Error: %s %s\n", program_name, abieos_get_error(context)); + } + + } else { + const char* json = abieos_hex_to_json(context, contract_name, hex_type, hex_json_string); + if (!json) { + fprintf(stderr, "Error: %s %s\n", program_name, abieos_get_error(context)); + } else { + fprintf(stdout, "%s\n", json); + } + } + } + } + abieos_destroy(context); + free(source); + + return 0; +}