diff --git a/modules/common/dap_chain_common.c b/modules/common/dap_chain_common.c index fac77070ae0205b4d5a195f8a09a097f730fb04d..c5c66ffa67a9db408fc330a9242713819cd34652 100644 --- a/modules/common/dap_chain_common.c +++ b/modules/common/dap_chain_common.c @@ -228,27 +228,14 @@ dap_chain_net_srv_uid_t dap_chain_net_srv_uid_from_str( const char * a_net_srv_u * @param a_net_id * @return */ -void dap_chain_addr_fill(dap_chain_addr_t *a_addr, dap_enc_key_t *a_key, dap_chain_net_id_t *a_net_id) +void dap_chain_addr_fill(dap_chain_addr_t *a_addr, dap_sign_type_t a_type, dap_chain_hash_fast_t *a_pkey_hash, dap_chain_net_id_t *a_net_id) { - if(!a_addr || !a_key || !a_net_id) + if(!a_addr || !a_pkey_hash || !a_net_id) return; a_addr->addr_ver = DAP_CHAIN_ADDR_VERSION_CURRENT; a_addr->net_id.uint64 = a_net_id->uint64; - a_addr->sig_type.raw = dap_sign_type_from_key_type(a_key->type).raw; - // key -> serialized key - dap_chain_hash_fast_t l_hash_public_key; - size_t l_pub_key_data_size; - uint8_t *l_pub_key_data = dap_enc_key_serealize_pub_key(a_key, &l_pub_key_data_size); - if ( l_pub_key_data == NULL ){ - log_it(L_ERROR,"Can't fill address from key, its empty"); - return; - } - - // serialized key -> key hash - - if(dap_hash_fast(l_pub_key_data, l_pub_key_data_size, &l_hash_public_key)) - memcpy(a_addr->data.hash, l_hash_public_key.raw, sizeof(l_hash_public_key.raw)); - DAP_DELETE(l_pub_key_data); + a_addr->sig_type.raw = a_type.raw; + memcpy(a_addr->data.hash, a_pkey_hash, sizeof(dap_chain_hash_fast_t)); // calc checksum dap_hash_fast(a_addr, sizeof(dap_chain_addr_t) - sizeof(dap_chain_hash_fast_t), &a_addr->checksum); } diff --git a/modules/common/include/dap_chain_common.h b/modules/common/include/dap_chain_common.h index a4a11b7d27703246ffc778bfcb9940b75f291938..c8617a1dac04c4635fde8d8141a04cde98ef4db5 100644 --- a/modules/common/include/dap_chain_common.h +++ b/modules/common/include/dap_chain_common.h @@ -207,7 +207,7 @@ dap_chain_addr_t* dap_chain_addr_from_str(const char *str); dap_chain_net_id_t dap_chain_net_id_from_str(const char* a_str); dap_chain_net_srv_uid_t dap_chain_net_srv_uid_from_str(const char* a_str); -void dap_chain_addr_fill(dap_chain_addr_t *a_addr, dap_enc_key_t *a_key, dap_chain_net_id_t *a_net_id); +void dap_chain_addr_fill(dap_chain_addr_t *a_addr, dap_sign_type_t a_type, dap_chain_hash_fast_t *a_pkey_hash, dap_chain_net_id_t *a_net_id); int dap_chain_addr_check_sum(const dap_chain_addr_t *a_addr); diff --git a/modules/consensus/dag-pos/dap_chain_cs_dag_pos.c b/modules/consensus/dag-pos/dap_chain_cs_dag_pos.c index a37e7a5fabb1e1a0cbab42cec4b692d894fd3baf..33aa66b2a5e04285172e24ca2cad25fc532440c8 100644 --- a/modules/consensus/dag-pos/dap_chain_cs_dag_pos.c +++ b/modules/consensus/dag-pos/dap_chain_cs_dag_pos.c @@ -230,14 +230,12 @@ static int s_callback_event_verify(dap_chain_cs_dag_t * a_dag, dap_chain_cs_dag_ return -4; } - dap_enc_key_t * l_key = dap_sign_to_enc_key( l_sign); - if ( l_key == NULL){ + dap_chain_hash_fast_t l_pkey_hash; + if (!dap_sign_get_pkey_hash(l_sign, &l_pkey_hash)) { log_it(L_WARNING, "Event's sign has no any key"); return -5; } - - dap_chain_addr_fill (&l_addr,l_key,&a_dag->chain->net_id ); - dap_enc_key_delete (l_key); // TODO cache all this operations to prevent useless memory copy ops + dap_chain_addr_fill(&l_addr, l_sign->header.type, &l_pkey_hash, &a_dag->chain->net_id); /* dap_chain_datum_t *l_datum = dap_chain_cs_dag_event_get_datum(a_dag_event); diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c index 7ed6ac5e6ca03ac0f2bb8faf943a44a9dcc76874..00733298ffafaf5e7cd57e916282a30d6b78cc2c 100644 --- a/modules/net/dap_chain_net.c +++ b/modules/net/dap_chain_net.c @@ -62,6 +62,7 @@ #include "dap_cert.h" #include "dap_chain_common.h" #include "dap_chain_net.h" +#include "dap_chain_net_srv.h" #include "dap_chain_node_client.h" #include "dap_chain_node_cli.h" #include "dap_chain_node_cli_cmd.h" @@ -249,10 +250,8 @@ static void s_gbd_history_callback_notify (void * a_arg, const char a_op_code, c const size_t a_value_len) { (void) a_op_code; - UNUSED(a_key); - UNUSED(a_value_len); UNUSED(a_prefix); - UNUSED(a_group); + UNUSED(a_value_len); if (a_arg) { dap_chain_net_t * l_net = (dap_chain_net_t *) a_arg; s_net_set_go_sync(l_net); @@ -264,6 +263,31 @@ static void s_gbd_history_callback_notify (void * a_arg, const char a_op_code, c } }*/ } + if (!dap_config_get_item_bool_default(g_config, "srv", "order_signed_only", false)) { + return; + } + dap_chain_net_t *l_net = (dap_chain_net_t *)a_arg; + char *l_gdb_group_str = dap_chain_net_srv_order_get_gdb_group(l_net); + if (strcmp(a_group, l_gdb_group_str)) { + dap_chain_net_srv_order_t *l_order = (dap_chain_net_srv_order_t *)a_value; + if (l_order->version == 1) { + dap_chain_global_db_gr_del((char *)a_key, a_group); + } else { + dap_sign_t *l_sign = (dap_sign_t *)&l_order->ext[l_order->ext_size]; + dap_chain_hash_fast_t l_pkey_hash; + if (!dap_sign_get_pkey_hash(l_sign, &l_pkey_hash)) { + return; + } + dap_chain_addr_t l_addr = {0}; + dap_chain_addr_fill(&l_addr, l_sign->header.type, &l_pkey_hash, &l_net->pub.id); + uint64_t l_solvency = dap_chain_ledger_calc_balance(l_net->pub.ledger, &l_addr, l_order->price_ticker); + if (l_solvency < l_order->price) { + dap_chain_global_db_gr_del((char *)a_key, a_group); + } + // TODO check for delegated key + } + DAP_DELETE(l_gdb_group_str); + } } /** diff --git a/modules/net/srv/dap_chain_net_srv.c b/modules/net/srv/dap_chain_net_srv.c index d928fd2ec1dc9af39fa8d4b74672e80b3bebfe4d..798ca104519d6d12fb4e96f087f3e96e71a07efd 100644 --- a/modules/net/srv/dap_chain_net_srv.c +++ b/modules/net/srv/dap_chain_net_srv.c @@ -457,7 +457,7 @@ static int s_cli_net_srv( int argc, char **argv, void *arg_func, char **a_str_re strncpy(l_price_token, l_price_token_str, DAP_CHAIN_TICKER_SIZE_MAX - 1); char * l_order_new_hash_str = dap_chain_net_srv_order_create( l_net,l_direction, l_srv_uid, l_node_addr,l_tx_cond_hash, l_price, l_price_unit, - l_price_token, l_expires, (uint8_t *)l_ext, strlen(l_ext) + 1, l_region_str, l_continent_num); + l_price_token, l_expires, (uint8_t *)l_ext, strlen(l_ext) + 1, l_region_str, l_continent_num, NULL); if (l_order_new_hash_str) dap_string_append_printf( l_string_ret, "Created order %s\n", l_order_new_hash_str); else{ diff --git a/modules/net/srv/dap_chain_net_srv_order.c b/modules/net/srv/dap_chain_net_srv_order.c index 3030b70ed983b28c18232549336acded255c861e..91e4b5c6ca96c0ffe57c5ac0802929acc2978a91 100644 --- a/modules/net/srv/dap_chain_net_srv_order.c +++ b/modules/net/srv/dap_chain_net_srv_order.c @@ -78,7 +78,12 @@ void dap_chain_net_srv_order_deinit() size_t dap_chain_net_srv_order_get_size(dap_chain_net_srv_order_t *a_order) { - return a_order ? sizeof(dap_chain_net_srv_order_t) + a_order->ext_size : 0; + size_t l_sign_size = 0; + if (a_order->version > 1) { + dap_sign_t *l_sign = (dap_sign_t *)&a_order->ext[a_order->ext_size]; + l_sign_size = dap_sign_get_size(l_sign); + } + return a_order ? sizeof(dap_chain_net_srv_order_t) + a_order->ext_size + l_sign_size : 0; } /** @@ -218,7 +223,8 @@ char * dap_chain_net_srv_order_create( const uint8_t *a_ext, uint32_t a_ext_size, const char *a_region, - int8_t a_continent_num + int8_t a_continent_num, + dap_enc_key_t *a_key ) { UNUSED(a_expires); @@ -235,7 +241,7 @@ char * dap_chain_net_srv_order_create( } dap_chain_hash_fast_t l_order_hash; - l_order->version = 1; + l_order->version = a_key ? 2 : 1; l_order->srv_uid = a_srv_uid; l_order->direction = a_direction; l_order->ts_created = (dap_chain_time_t) time(NULL); @@ -250,7 +256,16 @@ char * dap_chain_net_srv_order_create( if ( a_price_ticker) strncpy(l_order->price_ticker, a_price_ticker,sizeof(l_order->price_ticker)-1); - + if (a_key) { + dap_sign_t *l_sign = dap_sign_create(a_key, l_order, sizeof(dap_chain_net_srv_order_t) + l_order->ext_size, 0); + if (!l_sign) { + return NULL; + } + size_t l_sign_size = dap_sign_get_size(l_sign); // sign data + l_order = DAP_REALLOC(l_order, sizeof(dap_chain_net_srv_order_t) + l_order->ext_size + l_sign_size); + memcpy(&l_order->ext[l_order->ext_size], l_sign, l_sign_size); + DAP_DELETE(l_sign); + } size_t l_order_size = dap_chain_net_srv_order_get_size(l_order); dap_hash_fast( l_order, l_order_size, &l_order_hash ); char * l_order_hash_str = dap_chain_hash_fast_to_str_new( &l_order_hash ); diff --git a/modules/net/srv/include/dap_chain_net_srv_order.h b/modules/net/srv/include/dap_chain_net_srv_order.h index 369fba674c2c195c7c788a5c15f87edb6069237d..aa695855af8500fb8a961ab15e74272b1d5aba7c 100644 --- a/modules/net/srv/include/dap_chain_net_srv_order.h +++ b/modules/net/srv/include/dap_chain_net_srv_order.h @@ -104,7 +104,8 @@ char *dap_chain_net_srv_order_create(dap_chain_net_t * a_net, const uint8_t *a_ext, uint32_t a_ext_size, const char *a_region, - int8_t a_continent_num + int8_t a_continent_num, + dap_enc_key_t *a_key ); int dap_chain_net_srv_order_save(dap_chain_net_t * a_net, dap_chain_net_srv_order_t *a_order); diff --git a/modules/service/xchange/dap_chain_net_srv_xchange.c b/modules/service/xchange/dap_chain_net_srv_xchange.c index 551653ebbb27953de202795657678701142354ea..5ac9c7f983bb46e7d0e40ad1b922a2acbda0c7b4 100644 --- a/modules/service/xchange/dap_chain_net_srv_xchange.c +++ b/modules/service/xchange/dap_chain_net_srv_xchange.c @@ -401,7 +401,7 @@ char *s_xchange_order_create(dap_chain_net_srv_xchange_price_t *a_price, dap_cha uint64_t l_datoshi_buy = ceill(a_price->datoshi_sell / a_price->rate); char *l_order_hash_str = dap_chain_net_srv_order_create(a_price->net_buy, SERV_DIR_SELL, l_uid, *l_node_addr, l_tx_hash, l_datoshi_buy, l_unit, a_price->token_buy, 0, - (uint8_t *)&l_ext, l_ext_size, NULL, 0); + (uint8_t *)&l_ext, l_ext_size, NULL, 0, NULL); return l_order_hash_str; } diff --git a/modules/type/dag/dap_chain_cs_dag.c b/modules/type/dag/dap_chain_cs_dag.c index 6c13fe137dea20560006b88dc5218052d9ad64b8..112cd0a8b6c664f4ce54b5dbb97d1cf9975174cd 100644 --- a/modules/type/dag/dap_chain_cs_dag.c +++ b/modules/type/dag/dap_chain_cs_dag.c @@ -1266,9 +1266,10 @@ static int s_cli_dag(int argc, char ** argv, void *arg_func, char **a_str_reply) dap_string_append_printf(l_str_tmp,"\t\t\t\tERROR: wrong sign size 0, stop parsing headers\n"); break; } - dap_enc_key_t * l_sign_key = dap_sign_to_enc_key(l_sign); dap_chain_addr_t l_addr = {0}; - dap_chain_addr_fill(&l_addr,l_sign_key,&l_net->pub.id); + dap_chain_hash_fast_t l_pkey_hash; + dap_sign_get_pkey_hash(l_sign, &l_pkey_hash); + dap_chain_addr_fill(&l_addr, l_sign->header.type, &l_pkey_hash, &l_net->pub.id); char * l_addr_str = dap_chain_addr_to_str(&l_addr); dap_string_append_printf(l_str_tmp,"\t\t\t\t\t\ttype: %s\taddr: %s" "n", dap_sign_type_to_str( l_sign->header.type ), diff --git a/modules/wallet/dap_chain_wallet.c b/modules/wallet/dap_chain_wallet.c index ac14456bde419486265b174b99dd99f16ae910b8..b392881900937d5a02ce6aa61a609d6c1b3096e5 100644 --- a/modules/wallet/dap_chain_wallet.c +++ b/modules/wallet/dap_chain_wallet.c @@ -205,8 +205,19 @@ dap_chain_addr_t* dap_chain_wallet_get_addr(dap_chain_wallet_t * a_wallet, dap_c */ dap_chain_addr_t * dap_cert_to_addr(dap_cert_t * a_cert, dap_chain_net_id_t a_net_id) { + dap_sign_type_t l_type = dap_sign_type_from_key_type(a_cert->enc_key->type); + size_t l_pub_key_data_size; + uint8_t *l_pub_key_data = dap_enc_key_serealize_pub_key(a_cert->enc_key, &l_pub_key_data_size); + if (!l_pub_key_data) { + log_it(L_ERROR,"Can't fill address from key, its empty"); + return NULL; + } + dap_chain_hash_fast_t l_hash_public_key; + // serialized key -> key hash + dap_hash_fast(l_pub_key_data, l_pub_key_data_size, &l_hash_public_key); dap_chain_addr_t * l_addr = DAP_NEW_Z(dap_chain_addr_t); - dap_chain_addr_fill(l_addr, a_cert->enc_key, &a_net_id); + dap_chain_addr_fill(l_addr, l_type, &l_hash_public_key, &a_net_id); + DAP_DELETE(l_pub_key_data); return l_addr; }