Skip to content

Commit

Permalink
jade-rpc
Browse files Browse the repository at this point in the history
add function to parse generic jade-rpc response into json, lossy
conversion is not allowed
  • Loading branch information
gministeri committed Dec 18, 2023
1 parent 4e9678a commit 95d1026
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/urc/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
#define URC_EINVALIDARG 11
#define URC_EWALLYINTERNALERROR 12
#define URC_ENOMEM 13
#define URC_EINTERNALERROR 14
8 changes: 8 additions & 0 deletions include/urc/jade_rpc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <stddef.h>
#include <stdint.h>

#include "urc/error.h"

int urc_jade_rpc_parse(const uint8_t *cbor, size_t cbor_len, char **out);
1 change: 1 addition & 0 deletions include/urc/urc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
#include "urc/crypto_seed.h"
#include "urc/error.h"
#include "urc/jade_bip8539.h"
#include "urc/jade_rpc.h"
#include "urc/tags.h"
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_library(
account.c
bip8539.c
jadeaccount.c
jade_rpc.c
eckey.c
hdkey.c
output.c
Expand Down
45 changes: 45 additions & 0 deletions src/jade_rpc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

#include <stdio.h>

#include "cborjson.h"
#include "wally_core.h"

#include "urc/jade_rpc.h"

#include "macros.h"
#include "utils.h"

int urc_jade_rpc_parse(const uint8_t *cbor, size_t cbor_len, char **out) {
CborParser parser;
CborValue value;
CborError err = cbor_parser_init(cbor, cbor_len, 0, &parser, &value);
if (err != CborNoError) {
return URC_ECBORINTERNALERROR;
}
FILE *stream = NULL;
*out = NULL;
size_t buffer_len = cbor_len;
do {
wally_free(*out);
*out = wally_malloc(buffer_len);
if (*out == NULL) {
return URC_EWALLYINTERNALERROR;
}
stream = fmemopen(*out, buffer_len, "w");
if (stream == NULL) {
wally_free(*out);
*out = NULL;
return URC_EINTERNALERROR;
}
setbuf(stream, NULL);
err = cbor_value_to_json(stream, &value, CborConvertIgnoreTags | CborConvertRequireMapStringKeys);
buffer_len *= 2;
} while (err == CborErrorIO);
fclose(stream);
if (err != CborNoError) {
wally_free(*out);
*out = NULL;
return URC_ECBORINTERNALERROR;
}
return URC_OK;
}
10 changes: 9 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ else()
find_package(unity REQUIRED)
endif()

add_executable(units helpers.h helpers.c formatter.c parser.c runner.c)
add_executable(
units
helpers.h
helpers.c
formatter.c
parser.c
runner.c
jade_rpc.c
)
target_link_libraries(units PRIVATE urc unity)
target_include_directories(units PRIVATE ${CMAKE_SOURCE_DIR}/src)
add_test(NAME units COMMAND units)
Expand Down
8 changes: 8 additions & 0 deletions tests/fuzzy/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,13 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t len) {
if (result != URC_OK) {
return -1;
}

char *out = NULL;
result = urc_jade_rpc_parse(data, len, &out);
if (result != URC_OK) {
return -1;
}
urc_string_free(out);

return 0;
}
35 changes: 35 additions & 0 deletions tests/jade_rpc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

#include "unity_fixture.h"

#include "urc/core.h"
#include "urc/jade_rpc.h"

#include "helpers.h"

#define BUFLEN 1000

TEST_GROUP(jade_rpc);
TEST_SETUP(jade_rpc) {}
TEST_TEAR_DOWN(jade_rpc) {}

TEST(jade_rpc, parse_jade_pin) {
const char *hex =
"a26269646671726175746866726573756c74a16c687474705f72657175657374a266706172616d73a46475726c7382782f68747470733a2f2f6a6164"
"6570696e2e626c6f636b73747265616d2e636f6d2f73746172745f68616e647368616b657855687474703a2f2f6d727278747136746a70626e626d37"
"7668356a74366d706a63746e3767677966793577656776626566663378376a727a6e7161776c6d69642e6f6e696f6e2f73746172745f68616e647368"
"616b65666d6574686f6464504f535466616363657074646a736f6e646461746160686f6e2d7265706c796e68616e647368616b655f696e6974";
uint8_t raw[BUFLEN];
size_t len = h2b(hex, BUFLEN, (uint8_t *)&raw);
TEST_ASSERT_GREATER_THAN_INT(0, len);

char *out = NULL;
int result = urc_jade_rpc_parse(raw, len, &out);
TEST_ASSERT_EQUAL_INT(URC_OK, result);

const char *expected =
"{\"id\":\"qrauth\",\"result\":{\"http_request\":{\"params\":{\"urls\":[\"https://jadepin.blockstream.com/"
"start_handshake\",\"http://mrrxtq6tjpbnbm7vh5jt6mpjctn7ggyfy5wegvbeff3x7jrznqawlmid.onion/"
"start_handshake\"],\"method\":\"POST\",\"accept\":\"json\",\"data\":\"\"},\"on-reply\":\"handshake_init\"}}}";
TEST_ASSERT_EQUAL_STRING(expected, out);
urc_string_free(out);
}
5 changes: 5 additions & 0 deletions tests/runner.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ TEST_GROUP_RUNNER(formatter) {
RUN_TEST_CASE(formatter, jaderequest_format);
}

TEST_GROUP_RUNNER(jade_rpc) {
RUN_TEST_CASE(jade_rpc, parse_jade_pin);
}

static void RunAllTests(void) {
RUN_TEST_GROUP(parser);
RUN_TEST_GROUP(formatter);
RUN_TEST_GROUP(jade_rpc);
}

int main(int argc, const char *argv[]) { return UnityMain(argc, argv, RunAllTests); }

0 comments on commit 95d1026

Please sign in to comment.