Skip to content

Commit 5375f7b

Browse files
author
diogorac
committed
init
0 parents  commit 5375f7b

File tree

15 files changed

+3418
-0
lines changed

15 files changed

+3418
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.vscode/
2+
build/

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "libs/trezor-crypto"]
2+
path = libs/trezor-crypto
3+
url = git@github.com:trezor/trezor-crypto.git

CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
project (cbig)
2+
cmake_minimum_required(VERSION 3.11.4)
3+
4+
set(TARGET_GROUP main CACHE STRING "Group to build")
5+
6+
find_program(
7+
CMAKE_C_CPPCHECK
8+
NAMES "cppcheck"
9+
DOC "Path to cppcheck executable"
10+
)
11+
if(NOT CMAKE_C_CPPCHECK)
12+
message(FATAL_ERROR "cppcheck not found.")
13+
else()
14+
message(STATUS "cppcheck found: ${CMAKE_C_CPPCHECK}")
15+
set(CMAKE_C_CPPCHECK "${CMAKE_C_CPPCHECK}" --enable=warning,performance,portability,style --error-exitcode=1 --force --quiet)
16+
endif()
17+
18+
add_compile_options(-DuECC_VLI_NATIVE_LITTLE_ENDIAN=0 -std=c99 -Wall -Wextra -O0 -g --coverage -fsanitize=address -fno-omit-frame-pointer)
19+
SET(CMAKE_EXE_LINKER_FLAGS "-DuECC_VLI_NATIVE_LITTLE_ENDIAN=0 -std=c99 -Wall -Wextra -O0 -g --coverage -fsanitize=address -fno-omit-frame-pointer ${CMAKE_EXE_LINKER_FLAGS}")
20+
add_subdirectory(src)
21+
include(CTest)
22+
add_subdirectory(tests)

inc/transaction.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef _BIGCHAIN_TX_H_
2+
#define _BIGCHAIN_TX_H_
3+
#include <stddef.h>
4+
#include <stdint.h>
5+
#include <stdio.h>
6+
#include <stdbool.h>
7+
8+
#include "json-maker.h"
9+
#include "sha3.h"
10+
#include "ed25519-donna/ed25519.h"
11+
12+
typedef struct
13+
{
14+
char amount[8];
15+
// FIXED CONDITION FOR EDCURVE
16+
char details_public_key[45];
17+
char public_keys[8][45];
18+
uint8_t num_public_keys;
19+
} BIGCHAIN_OUTPUT;
20+
21+
typedef struct
22+
{
23+
char fulfillment[256];
24+
char fulfills[256];
25+
char owners_before[8][256];
26+
uint8_t num_owners;
27+
} BIGCHAIN_INPUT;
28+
29+
typedef struct
30+
{
31+
char asset[256];
32+
char metadata[256];
33+
char operation[32];
34+
BIGCHAIN_OUTPUT outputs[8];
35+
uint8_t num_outputs;
36+
BIGCHAIN_INPUT inputs[8];
37+
uint8_t num_inputs;
38+
char version[8];
39+
char id[65];
40+
} BIGCHAIN_TX;
41+
42+
void bigchain_sign_transaction(uint8_t *json_tx, uint16_t len, uint8_t *priv_key, uint8_t *pub_key, uint8_t *sig);
43+
char* bigchain_build_json_inputs(BIGCHAIN_INPUT *inputs, uint8_t num_inputs, char *json_obj);
44+
char* bigchain_build_json_outputs(BIGCHAIN_OUTPUT *outputs, uint8_t num_outputs, char *json_obj);
45+
void bigchain_build_json_tx(BIGCHAIN_TX *tx, char *json_tx);
46+
47+
#endif // _BIGCHAIN_TX_H_

libs/json-maker

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 66cb2b7a5155417898463152e247eb8b2ae6fff8

libs/trezor-crypto

Submodule trezor-crypto added at 4211ce3

src/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
file(GLOB SOURCES "*.c")
3+
file(GLOB JSON_SOURCES "../libs/json-maker/*.c")
4+
file(GLOB TREZOR_CRYPTO_ED255 "../libs/trezor-crypto/ed25519-donna/*.c")
5+
file(GLOB TREZOR_CRYPTO "../libs/trezor-crypto/*.c")
6+
7+
add_library(cbig STATIC
8+
${SOURCES}
9+
${TREZOR_CRYPTO}
10+
${TREZOR_CRYPTO_ED255}
11+
${JSON_SOURCES}
12+
)
13+
14+
target_include_directories(cbig PUBLIC
15+
../inc
16+
../libs/trezor-crypto/
17+
../
18+
../libs/json-maker
19+
)

src/transaction.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#include "transaction.h"
2+
3+
/*
4+
* Takes a json string, hashes it sha3_256 and signs it with ed25519.
5+
*/
6+
void bigchain_sign_transaction(uint8_t *json_tx, uint16_t len, uint8_t *priv_key, uint8_t *pub_key, uint8_t *sig)
7+
{
8+
uint8_t hash[32] = {0};
9+
sha3_256((const unsigned char*)json_tx, len, hash);
10+
ed25519_sign(hash, 32, priv_key, pub_key, sig);
11+
}
12+
13+
char* bigchain_build_json_outputs(BIGCHAIN_OUTPUT *outputs, uint8_t num_outputs, char *json_obj)
14+
{
15+
char *p = json_obj;
16+
p = json_arrOpen( p, "outputs" );
17+
for(uint8_t i = 0; i < num_outputs; i++)
18+
{
19+
p = json_objOpen(p, NULL);
20+
p = json_str(p, "amount", outputs[i].amount);
21+
p = json_objOpen(p, "condition");
22+
23+
p = json_objOpen(p, "details");
24+
p = json_str(p, "public_key", outputs[i].details_public_key);
25+
p = json_str(p, "type", "ed25519-sha-256");
26+
p = json_objClose(p);
27+
28+
p = json_str(p, "uri", "ni:///sha-256;Vta3W592Kt_Y2ljfHEDZLd4OCZPHLiHyCgjNNKrrNwo?fpt=ed25519-sha-256&cost=131072");
29+
p = json_objClose(p);
30+
31+
p = json_arrOpen(p, "public_keys");
32+
for(uint8_t j = 0; j < outputs[i].num_public_keys; j++ )
33+
{
34+
p = json_str(p, NULL, outputs[i].public_keys[j]);
35+
}
36+
p = json_arrClose(p);
37+
38+
p = json_objClose(p);
39+
}
40+
p = json_arrClose(p);
41+
return p;
42+
}
43+
44+
char* bigchain_build_json_inputs(BIGCHAIN_INPUT *inputs, uint8_t num_inputs, char *json_obj)
45+
{
46+
char *p = json_obj;
47+
p = json_arrOpen( p, "inputs" );
48+
for(uint8_t i = 0; i < num_inputs; i++)
49+
{
50+
p = json_objOpen(p, NULL);
51+
if(inputs[i].fulfillment[0] != '\0')
52+
{
53+
p = json_str(p, "fulfillment", inputs[0].fulfillment);
54+
}
55+
else
56+
{
57+
p = json_null(p, "fulfillment");
58+
}
59+
60+
if(inputs[i].fulfills[0] != '\0')
61+
{
62+
p = json_str(p, "fulfills", inputs[0].fulfills);
63+
}
64+
else
65+
{
66+
p = json_null(p, "fulfills");
67+
}
68+
69+
p = json_arrOpen(p, "owners_before");
70+
for (uint8_t j = 0; j < inputs[0].num_owners; j++)
71+
{
72+
p = json_str(p, NULL, inputs[0].owners_before[j]);
73+
}
74+
p = json_arrClose(p);
75+
p = json_objClose(p);
76+
}
77+
p = json_arrClose(p);
78+
return p;
79+
}
80+
81+
void bigchain_build_json_tx(BIGCHAIN_TX *tx, char *json_tx)
82+
{
83+
char *p = json_tx;
84+
p = json_objOpen(p, NULL);
85+
86+
// ASSET
87+
p = json_objOpen(p, "asset");
88+
p = atoa(p, tx->asset);
89+
p = json_objClose(p);
90+
91+
// ID
92+
if(tx->id[0] != '\0')
93+
{
94+
p = json_str(p, "id", tx->id);
95+
}
96+
else
97+
{
98+
p = json_null(p, "id");
99+
}
100+
101+
// INPUTS
102+
p = bigchain_build_json_inputs(tx->inputs, tx->num_inputs, p);
103+
104+
// METADA
105+
p = json_objOpen(p, "metadata");
106+
p = atoa(p, tx->metadata);
107+
p = json_objClose(p);
108+
109+
// OPERATION
110+
p = json_str(p, "operation", tx->operation);
111+
112+
// OUTPUTS
113+
p = bigchain_build_json_outputs(tx->outputs, tx->num_outputs, p);
114+
115+
// VERSION
116+
p = json_str(p, "version", tx->version);
117+
p = json_objClose(p);
118+
119+
}
120+

tests/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
file(GLOB TEST_LIB_SOURCES "*.c")
2+
3+
add_subdirectory(Unity)
4+
5+
add_custom_target(check
6+
COMMENT "Creating Coverage HTML and XML summary"
7+
COMMAND ctest -T Test --no-compress-output || true
8+
COMMAND gcovr -k --branches -r ${CMAKE_SOURCE_DIR}
9+
--xml-pretty -o ${CMAKE_BINARY_DIR}/coverage.xml
10+
COMMAND gcovr -k --branches -r ${CMAKE_SOURCE_DIR}
11+
--html --html-details -o ${CMAKE_BINARY_DIR}/coverage.html
12+
)
13+
14+
macro(do_test test_name sources)
15+
add_executable(${test_name}
16+
${sources}
17+
)
18+
19+
target_include_directories(${test_name} PUBLIC .
20+
)
21+
22+
target_link_libraries(${test_name}
23+
cbig
24+
Unity
25+
gcov
26+
asan
27+
)
28+
29+
target_compile_features(${test_name} PRIVATE
30+
c_std_99
31+
)
32+
add_test(${test_name} ${test_name})
33+
endmacro()
34+
35+
foreach(arg1 ${TEST_LIB_SOURCES} )
36+
get_filename_component(name ${arg1} NAME_WE)
37+
get_filename_component(file ${arg1} NAME)
38+
do_test(${name} ${file})
39+
endforeach(arg1)

tests/Unity/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
add_library(Unity STATIC
2+
unity.c
3+
)
4+
5+
target_include_directories(Unity PUBLIC
6+
.
7+
)

0 commit comments

Comments
 (0)