Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug tool hex_to_json #60

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pattern seems to be to only include src/abieos.cpp in the executables that link to abieos and not in abieos.

There is also abieos_module which may be the more appropriate target_link_library for hex_to_json, was that attempted and failed for some reason?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having in tools/CMakeLists
target_link_libraries(hex_to_json abieos_module ${CMAKE_THREAD_LIBS_INIT})
resulted in

CMake Error at tools/CMakeLists.txt:12 (target_link_libraries):
  Target "abieos_module" of type MODULE_LIBRARY may not be linked into
  another target.  One may link only to INTERFACE, OBJECT, STATIC or SHARED
  libraries, or to executables with the ENABLE_EXPORTS property set.

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)
Expand Down Expand Up @@ -79,3 +79,5 @@ endif()
if (NOT ABIEOS_ONLY_LIBRARY)
add_subdirectory(tools)
endif()

install(TARGETS abieos DESTINATION .)
6 changes: 6 additions & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<TARGET_FILE:name> ${CMAKE_CURRENT_BINARY_DIR}/name2num )
add_custom_command( TARGET name POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $<TARGET_FILE:name> ${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 $<TARGET_FILE:hex_to_json> ${CMAKE_CURRENT_BINARY_DIR}/json2hex )
add_custom_command( TARGET hex_to_json POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $<TARGET_FILE:hex_to_json> ${CMAKE_CURRENT_BINARY_DIR}/hex2json )
115 changes: 115 additions & 0 deletions tools/hex_to_json.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "../src/abieos.h"
#include <libgen.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

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;
}