Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-jentzsch committed Nov 5, 2021
2 parents 694c135 + b0b4f7b commit dba622a
Show file tree
Hide file tree
Showing 113 changed files with 5,739 additions and 1,134 deletions.
4 changes: 4 additions & 0 deletions c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ if (IN3_LIB)
target_link_libraries(in3_lib zk_crypto_rs )
endif()

if (VADE)
target_link_libraries(in3_lib vade_evan )
endif()


# install
INSTALL(TARGETS in3_lib
Expand Down
1 change: 1 addition & 0 deletions c/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ examples_arm:

examples_macos:
stage: test
allow_failure: true
needs:
- wasm
- mac_os
Expand Down
6 changes: 3 additions & 3 deletions c/compiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ else(MSVC)
endif()
endif(MSVC)
if (WASM)
set (CMAKE_C_FLAGS "-Wall -funsigned-char -Wextra -std=c99 -Wno-unused-function")
set (CMAKE_CXX_FLAGS "-Wall -funsigned-char -Wextra -std=c99 -Wno-unused-function -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
set (CMAKE_C_FLAGS "-Wall -funsigned-char -Wextra -std=c99 -Wno-unused-function -Wno-null-pointer-arithmetic")
set (CMAKE_CXX_FLAGS "-Wall -funsigned-char -Wextra -std=c99 -Wno-unused-function -Wno-null-pointer-arithmetic -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
if (CMAKE_BUILD_TYPE MATCHES Debug)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined,address -fsanitize-minimal-runtime")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined,address -fsanitize-minimal-runtime")
Expand All @@ -23,7 +23,7 @@ endif(WASM)
if(MSVC)
set (CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} /W4 /WX")
elseif(WASM)
set (CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wno-missing-braces")
set (CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wno-missing-braces -Wno-null-pointer-arithmetic")
else()
set (CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -Werror=vla -Wno-missing-braces")
endif()
Expand Down
57 changes: 29 additions & 28 deletions c/include/in3/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,34 +142,35 @@ typedef struct in3_chain {

/** plugin action list */
typedef enum {
PLGN_ACT_INIT = 0x1, /**< initialize plugin - use for allocating/setting-up internal resources . Plugins will be initialized before first used. The plgn_ctx will be the first request ctx in3_req_t */
PLGN_ACT_TERM = 0x2, /**< terminate plugin - use for releasing internal resources and cleanup. */
PLGN_ACT_TRANSPORT_SEND = 0x4, /**< sends out a request - the transport plugin will receive a request_t as plgn_ctx, it may set a cptr which will be passed back when fetching more responses. */
PLGN_ACT_TRANSPORT_RECEIVE = 0x8, /**< fetch next response - the transport plugin will receive a request_t as plgn_ctx, which contains a cptr if set previously*/
PLGN_ACT_TRANSPORT_CLEAN = 0x10, /**< free-up transport resources - the transport plugin will receive a request_t as plgn_ctx if the cptr was set.*/
PLGN_ACT_SIGN_ACCOUNT = 0x20, /**< returns the default account of the signer */
PLGN_ACT_SIGN_PREPARE = 0x40, /**< allows a wallet to manipulate the payload before signing - the plgn_ctx will be in3_sign_ctx_t. This way a tx can be send through a multisig */
PLGN_ACT_SIGN = 0x80, /**< signs the payload - the plgn_ctx will be in3_sign_ctx_t. */
PLGN_ACT_RPC_HANDLE = 0x100, /**< a plugin may respond to a rpc-request directly (without sending it to the node). */
PLGN_ACT_RPC_VERIFY = 0x200, /**< verifies the response. the plgn_ctx will be a in3_vctx_t holding all data */
PLGN_ACT_CACHE_SET = 0x400, /**< stores data to be reused later - the plgn_ctx will be a in3_cache_ctx_t containing the data */
PLGN_ACT_CACHE_GET = 0x800, /**< reads data to be previously stored - the plgn_ctx will be a in3_cache_ctx_t containing the key. if the data was found the data-property needs to be set. */
PLGN_ACT_CACHE_CLEAR = 0x1000, /**< clears all stored data - plgn_ctx will be NULL */
PLGN_ACT_CONFIG_SET = 0x2000, /**< gets a config-token and reads data from it */
PLGN_ACT_CONFIG_GET = 0x4000, /**< gets a string-builder and adds all config to it. */
PLGN_ACT_PAY_PREPARE = 0x8000, /**< prepares a payment */
PLGN_ACT_PAY_FOLLOWUP = 0x10000, /**< called after a request to update stats. */
PLGN_ACT_PAY_HANDLE = 0x20000, /**< handles the payment */
PLGN_ACT_PAY_SIGN_REQ = 0x40000, /**< signs a request */
PLGN_ACT_LOG_ERROR = 0x80000, /**< report an error */
PLGN_ACT_NL_PICK = 0x100000, /**< picks the data nodes, plgn_ctx will be a pointer to in3_req_t */
PLGN_ACT_NL_PICK_FOLLOWUP = 0x200000, /**< called after receiving a response in order to decide whether a update is needed, plgn_ctx will be a pointer to in3_req_t */
PLGN_ACT_NL_BLACKLIST = 0x400000, /**< blacklist a particular node in the nodelist, plgn_ctx will be a pointer to the node's address. */
PLGN_ACT_NL_FAILABLE = 0x800000, /**< handle fail-able request, plgn_ctx will be a pointer to in3_req_t */
PLGN_ACT_NL_OFFLINE = 0x1000000, /**< mark a particular node in the nodelist as offline, plgn_ctx will be a pointer to in3_nl_offline_ctx_t. */
PLGN_ACT_CHAIN_CHANGE = 0x2000000, /**< chain id change event, called after setting new chain id */
PLGN_ACT_GET_DATA = 0x4000000, /**< get access to plugin data as a void ptr */
PLGN_ACT_ADD_PAYLOAD = 0x8000000, /**< add plugin specific metadata to payload, plgn_ctx will be a sb_t pointer, make sure to begin with a comma */
PLGN_ACT_INIT = 0x1, /**< initialize plugin - use for allocating/setting-up internal resources . Plugins will be initialized before first used. The plgn_ctx will be the first request ctx in3_req_t */
PLGN_ACT_TERM = 0x2, /**< terminate plugin - use for releasing internal resources and cleanup. ( ctx: in3_t )*/
PLGN_ACT_TRANSPORT_SEND = 0x4, /**< sends out a request - the transport plugin will receive a request_t as plgn_ctx, it may set a cptr which will be passed back when fetching more responses. */
PLGN_ACT_TRANSPORT_RECEIVE = 0x8, /**< fetch next response - the transport plugin will receive a request_t as plgn_ctx, which contains a cptr if set previously*/
PLGN_ACT_TRANSPORT_CLEAN = 0x10, /**< free-up transport resources - the transport plugin will receive a request_t as plgn_ctx if the cptr was set.*/
PLGN_ACT_SIGN_ACCOUNT = 0x20, /**< returns the accounts or addresses of the signer */
PLGN_ACT_SIGN_PUBLICKEY = 0x40, /**< returns the public key for a given address ( ctx: in3_sign_public_key_ctx_t )*/
PLGN_ACT_SIGN_PREPARE = 0x80, /**< allows a wallet to manipulate the payload before signing - the plgn_ctx will be in3_sign_ctx_t. This way a tx can be send through a multisig */
PLGN_ACT_SIGN = 0x100, /**< signs the payload - the plgn_ctx will be in3_sign_ctx_t. */
PLGN_ACT_RPC_HANDLE = 0x200, /**< a plugin may respond to a rpc-request directly (without sending it to the node). */
PLGN_ACT_RPC_VERIFY = 0x400, /**< verifies the response. the plgn_ctx will be a in3_vctx_t holding all data */
PLGN_ACT_CACHE_SET = 0x800, /**< stores data to be reused later - the plgn_ctx will be a in3_cache_ctx_t containing the data */
PLGN_ACT_CACHE_GET = 0x1000, /**< reads data to be previously stored - the plgn_ctx will be a in3_cache_ctx_t containing the key. if the data was found the data-property needs to be set. */
PLGN_ACT_CACHE_CLEAR = 0x2000, /**< clears all stored data - plgn_ctx will be NULL */
PLGN_ACT_CONFIG_SET = 0x4000, /**< gets a config-token and reads data from it */
PLGN_ACT_CONFIG_GET = 0x8000, /**< gets a string-builder and adds all config to it. */
PLGN_ACT_PAY_PREPARE = 0x10000, /**< prepares a payment */
PLGN_ACT_PAY_FOLLOWUP = 0x20000, /**< called after a request to update stats. */
PLGN_ACT_PAY_HANDLE = 0x40000, /**< handles the payment */
PLGN_ACT_PAY_SIGN_REQ = 0x80000, /**< signs a request */
PLGN_ACT_LOG_ERROR = 0x100000, /**< report an error */
PLGN_ACT_NL_PICK = 0x200000, /**< picks the data nodes, plgn_ctx will be a pointer to in3_req_t */
PLGN_ACT_NL_PICK_FOLLOWUP = 0x400000, /**< called after receiving a response in order to decide whether a update is needed, plgn_ctx will be a pointer to in3_req_t */
PLGN_ACT_NL_BLACKLIST = 0x800000, /**< blacklist a particular node in the nodelist, plgn_ctx will be a pointer to the node's address. */
PLGN_ACT_NL_FAILABLE = 0x1000000, /**< handle fail-able request, plgn_ctx will be a pointer to in3_req_t */
PLGN_ACT_NL_OFFLINE = 0x2000000, /**< mark a particular node in the nodelist as offline, plgn_ctx will be a pointer to in3_nl_offline_ctx_t. */
PLGN_ACT_CHAIN_CHANGE = 0x4000000, /**< chain id change event, called after setting new chain id */
PLGN_ACT_GET_DATA = 0x8000000, /**< get access to plugin data as a void ptr */
PLGN_ACT_ADD_PAYLOAD = 0x10000000, /**< add plugin specific metadata to payload, plgn_ctx will be a sb_t pointer, make sure to begin with a comma */
} in3_plugin_act_t;

/**
Expand Down
45 changes: 39 additions & 6 deletions c/include/in3/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,22 @@ typedef enum {
SIGNER_EIP1271 = 2
} in3_signer_type_t;
/**
* action context when retrieving the account of a signer.
* action context when retrieving the addresses or accounts of a signer.
*/
typedef struct sign_account_ctx {
struct in3_req* req; /**< the context of the request in order report errors */
uint8_t* accounts; /**< the account to use for the signature */
int accounts_len; /**< number of accounts */
in3_signer_type_t signer_type; /**< the type of the signer used for this account.*/
} in3_sign_account_ctx_t;
/**
* action context when retrieving the public key of the signer.
*/
typedef struct sign_public_key_ctx {
struct in3_req* req; /**< the context of the request in order report errors */
uint8_t* account; /**< the account to use for the signature */
uint8_t public_key[64]; /**< the public key in case the plugin returns IN3_OK */
} in3_sign_public_key_ctx_t;

// ----------- SIGN_PREPARE ---------------

Expand All @@ -287,17 +295,28 @@ typedef enum {
SIGN_EC_RAW = 0, /**< sign the data directly */
SIGN_EC_HASH = 1, /**< hash and sign the data */
SIGN_EC_PREFIX = 2, /**< add Ethereum Signed Message-Proefix, hash and sign the data */
SIGN_EC_BTC = 3, /**< hashes the data twice with sha256 and signs it */
} d_signature_type_t;

/** payload type of the requested signature. It describes how to deserialize the payload. */
typedef enum {
PL_SIGN_ANY = 0, /**< custom data to be signed*/
PL_SIGN_ETHTX = 1, /**< the payload is a ethereum-tx */
PL_SIGN_BTCTX = 2, /**< the payload is a BTC-Tx-Input */
PL_SIGN_SAFETX = 3, /**< The payload is a rlp-encoded data of a Gnosys Safe Tx */
} d_payload_type_t;

/**
* signing context. This Context is passed to the signer-function.
*/
typedef struct sign_ctx {
bytes_t signature; /**< the resulting signature */
d_signature_type_t type; /**< the type of signature*/
struct in3_req* req; /**< the context of the request in order report errors */
bytes_t message; /**< the message to sign*/
bytes_t account; /**< the account to use for the signature */
bytes_t signature; /**< the resulting signature */
d_signature_type_t type; /**< the type of signature*/
d_payload_type_t payload_type; /**< the type of payload in order to deserialize the payload */
struct in3_req* req; /**< the context of the request in order report errors */
bytes_t message; /**< the message to sign*/
bytes_t account; /**< the account to use for the signature */
d_token_t* meta; /**< optional metadata to pass a long, which could include data to present to the user before signing */
} in3_sign_ctx_t;

/**
Expand Down Expand Up @@ -552,6 +571,20 @@ typedef struct {
} \
}

/**
* sets the string as taken from the given property to the target and raises an error if the len does not fit.
*/
#define CNF_SET_STRING(dst, token, property) \
{ \
const d_token_t* t = d_get(token, key(property)); \
if (d_type(t) != T_NULL && d_type(t) != T_STRING) CNF_ERROR("Invalid config for " property "!") \
const char* tmp = d_string(t); \
if (tmp) { \
if (dst) _free(dst); \
dst = _strdupn(tmp, -1); \
} \
}

#ifdef __cplusplus
}
#endif
Expand Down
31 changes: 31 additions & 0 deletions c/include/in3/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,37 @@ NONULL in3_proof_t in3_req_get_proof(
int i /**< [in] the index within the request. */
);

#define TRY_SUB_REQUEST(req, name, res, fmt, ...) \
{ \
sb_t sb = {0}; \
sb_printx(&sb, fmt, __VA_ARGS__); \
in3_ret_t r = req_send_sub_request(req, name, sb.data, NULL, res, NULL); \
_free(sb.data); \
if (r) return r; \
}

#define TRY_CATCH_SUB_REQUEST(req, name, res, _catch, fmt, ...) \
{ \
sb_t sb = {0}; \
sb_printx(&sb, fmt, __VA_ARGS__); \
in3_ret_t r = req_send_sub_request(req, name, sb.data, NULL, res, NULL); \
_free(sb.data); \
if (r) { \
_catch; \
return r; \
} \
}
#define TRY_FINAL_SUB_REQUEST(req, name, res, _catch, fmt, ...) \
{ \
sb_t sb = {0}; \
sb_printx(&sb, fmt, __VA_ARGS__); \
in3_ret_t r = req_send_sub_request(req, name, sb.data, NULL, res, NULL); \
_free(sb.data); \
_catch; \
if (r) \
return r; \
}

#ifdef __cplusplus
}
#endif
Expand Down
13 changes: 7 additions & 6 deletions c/include/in3/scache.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ extern "C" {
#include <stdlib.h>

typedef enum cache_props {
CACHE_PROP_MUST_FREE = 0x1, /**< indicates the content must be freed*/
CACHE_PROP_SRC_REQ = 0x2, /**< the value holds the src-request */
CACHE_PROP_ONLY_EXTERNAL = 0x4, /**< should only be freed if the context is external */
CACHE_PROP_JSON = 0x8, /**< indicates the content is a json_ctxt and must be freed as such*/
CACHE_PROP_INHERIT = 0x10, /**< indicates the content will be inherited when creating sub_request*/
CACHE_PROP_PAYMENT = 0x80 /**< This cache-entry is a payment.data */
CACHE_PROP_MUST_FREE = 0x1, /**< indicates the content must be freed*/
CACHE_PROP_SRC_REQ = 0x2, /**< the value holds the src-request */
CACHE_PROP_ONLY_EXTERNAL = 0x4, /**< should only be freed if the context is external */
CACHE_PROP_ONLY_NOT_EXTERNAL = 0x20, /**< should only be freed if the context is not external */
CACHE_PROP_JSON = 0x8, /**< indicates the content is a json_ctxt and must be freed as such*/
CACHE_PROP_INHERIT = 0x10, /**< indicates the content will be inherited when creating sub_request*/
CACHE_PROP_PAYMENT = 0x80 /**< This cache-entry is a payment.data */
} cache_props_t;
/**
* represents a single cache entry in a linked list.
Expand Down
4 changes: 4 additions & 0 deletions c/include/in3/signer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ extern "C" {
#endif

#include "client.h"
#include "plugin.h"

typedef enum {
hasher_sha2,
Expand Down Expand Up @@ -90,6 +91,9 @@ in3_ret_t ec_sign_pk_raw(uint8_t* message, uint8_t* pk, uint8_t* dst);

/** hashes the msg by adding the Ethereum Signed Message-Prefix */
void eth_create_prefixed_msg_hash(bytes32_t dst, bytes_t msg);

/** signs with a pk bases on the type */
bytes_t sign_with_pk(const bytes32_t pk, const bytes_t data, const d_signature_type_t type);
#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions c/include/in3/stringbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ sb_t* sb_add_rawbytes(sb_t* sb, char* prefix, bytes_t b, int fix_size);
sb_t* sb_print(sb_t* sb, const char* fmt, ...);
sb_t* sb_vprint(sb_t* sb, const char* fmt, va_list args);
sb_t* sb_add_json(sb_t* sb, const char* prefix, d_token_t* token);
sb_t* sb_printx(sb_t* sb, const char* fmt, ...);

#ifdef __cplusplus
}
Expand Down
58 changes: 58 additions & 0 deletions c/include/in3/swift.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*******************************************************************************
* This file is part of the Incubed project.
* Sources: https://github.com/slockit/in3-c
*
* Copyright (C) 2018-2020 slock.it GmbH, Blockchains LLC
*
*
* COMMERCIAL LICENSE USAGE
*
* Licensees holding a valid commercial license may use this file in accordance
* with the commercial license agreement provided with the Software or, alternatively,
* in accordance with the terms contained in a written agreement between you and
* slock.it GmbH/Blockchains LLC. For licensing terms and conditions or further
* information please contact slock.it at [email protected].
*
* Alternatively, this file may be used under the AGPL license as follows:
*
* AGPL LICENSE USAGE
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
* [Permissions of this strong copyleft license are conditioned on making available
* complete source code of licensed works and modifications, which include larger
* works using a licensed work, under the same license. Copyright and license notices
* must be preserved. Contributors provide an express grant of patent rights.]
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see <https://www.gnu.org/licenses/>.
*******************************************************************************/
// @PUBLIC_HEADER
#ifndef IN3_SWIFT_H
#define IN3_SWIFT_H
#ifdef __clang__
#define _NONULL _Nonnull
#else
#define _NONULL
#endif
#include "plugin.h"

typedef struct in3_swift_cb {
in3_ret_t (*_NONULL cache_get)(in3_cache_ctx_t* _Nonnull ctx);
in3_ret_t (*_NONULL cache_set)(in3_cache_ctx_t* _Nonnull ctx);
in3_ret_t (*_NONULL cache_clear)();
char* (*_NONULL sign_accounts)(in3_sign_account_ctx_t* _Nonnull ctx);
} swift_cb_t;

in3_ret_t in3_register_swift(in3_t* _NONULL c, swift_cb_t* _NONULL cbs);

char* sign_get_method(in3_req_t* r);
bytes_t sign_get_message(in3_req_t* r);
uint8_t* sign_get_from(in3_req_t* r);
int sign_get_payload_type(in3_req_t* r);
char* sign_get_metadata(in3_req_t* r);
#endif // IN3_SWIFT_H
Loading

0 comments on commit dba622a

Please sign in to comment.