-
Notifications
You must be signed in to change notification settings - Fork 39
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
kryton
wants to merge
5
commits into
EOSIO:master
Choose a base branch
from
kryton:hex_to_json_tool
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4fc6903
debug tool, helpful for diagnosing abi issues
kryton fe34053
Update hex_to_json.c
kryton a019ce4
make it do reverse as well.
kryton 564ba06
Merge branch 'hex_to_json_tool' of https://github.com/kryton/abieos i…
kryton 48b8314
make 'abieos' build target include abieos.cpp.
kryton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 toabieos
and not inabieos
.There is also
abieos_module
which may be the more appropriatetarget_link_library
forhex_to_json
, was that attempted and failed for some reason?There was a problem hiding this comment.
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