From bbcf8e84193c2d8615e2cb5f03f41ab4172fa867 Mon Sep 17 00:00:00 2001
From: "daniil.frolov" <daniil.frolov@demlabs.net>
Date: Tue, 26 Sep 2023 17:25:15 +0700
Subject: [PATCH] ..

---
 .../dap_stream_ch_chain_net_srv.c             | 12 ++---
 modules/net/srv/dap_chain_net_srv.c           | 50 +++++++++++++------
 modules/net/srv/include/dap_chain_net_srv.h   |  4 +-
 3 files changed, 44 insertions(+), 22 deletions(-)

diff --git a/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c b/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c
index 4ba3060be9..fcdf1000a1 100644
--- a/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c
+++ b/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c
@@ -900,12 +900,11 @@ static bool s_grace_period_finish(usages_in_grace_t *a_grace_item)
         char *l_hash_str = dap_hash_fast_to_str_new(&l_grace->usage->tx_cond_hash);
         log_it(L_NOTICE, "Trying create input tx cond from tx %s with active receipt", l_hash_str);
         DAP_DEL_Z(l_hash_str);
-        dap_chain_addr_t *l_wallet_addr = dap_chain_wallet_get_addr(l_grace->usage->price->wallet, l_grace->usage->net->pub.id);
         int ret_status = 0;
-        char *l_tx_in_hash_str = dap_chain_mempool_tx_create_cond_input(l_grace->usage->net, &l_grace->usage->tx_cond_hash, l_wallet_addr,
-                                                                        dap_chain_wallet_get_key(l_grace->usage->price->wallet, 0),
+        char *l_tx_in_hash_str = dap_chain_mempool_tx_create_cond_input(l_grace->usage->net, &l_grace->usage->tx_cond_hash, l_grace->usage->price->wallet_addr,
+                                                                        l_grace->usage->price->receipt_sign_cert->enc_key,
                                                                         l_receipt, "hex", &ret_status);
-        DAP_DEL_Z(l_wallet_addr);
+//        DAP_DEL_Z(l_wallet_addr);
         if (!ret_status) {
             dap_chain_hash_fast_from_str(l_tx_in_hash_str, &l_grace->usage->tx_cond_hash);
             log_it(L_NOTICE, "Formed tx %s for input with active receipt", l_tx_in_hash_str);
@@ -1175,10 +1174,9 @@ void s_stream_ch_packet_in(dap_stream_ch_t* a_ch , void* a_arg)
             char *l_hash_str = dap_hash_fast_to_str_new(&l_usage->tx_cond_hash);
             log_it(L_NOTICE, "Trying create input tx cond from tx %s with active receipt", l_hash_str);
             DAP_DEL_Z(l_hash_str);
-            dap_chain_addr_t *l_wallet_addr = dap_chain_wallet_get_addr(l_usage->price->wallet, l_usage->net->pub.id);
             int ret_status = 0;
-            char *l_tx_in_hash_str = dap_chain_mempool_tx_create_cond_input(l_usage->net, &l_usage->tx_cond_hash, l_wallet_addr,
-                                                                            dap_chain_wallet_get_key(l_usage->price->wallet, 0),
+            char *l_tx_in_hash_str = dap_chain_mempool_tx_create_cond_input(l_usage->net, &l_usage->tx_cond_hash, l_usage->price->wallet_addr,
+                                                                            l_usage->price->receipt_sign_cert->enc_key,
                                                                             l_receipt, "hex", &ret_status);
             if (!ret_status) {
                 dap_chain_hash_fast_from_str(l_tx_in_hash_str, &l_usage->tx_cond_hash);
diff --git a/modules/net/srv/dap_chain_net_srv.c b/modules/net/srv/dap_chain_net_srv.c
index b9d6e27dbd..0d356fb8f2 100644
--- a/modules/net/srv/dap_chain_net_srv.c
+++ b/modules/net/srv/dap_chain_net_srv.c
@@ -813,16 +813,19 @@ static bool s_pay_verificator_callback(dap_ledger_t * a_ledger, dap_chain_tx_out
 }
 
 int dap_chain_net_srv_price_apply_from_my_order(dap_chain_net_srv_t *a_srv, const char *a_config_section){
-    const char *l_wallet_path = dap_config_get_item_str_default(g_config, "resources", "wallets_path", NULL);
-    const char *l_wallet_name = dap_config_get_item_str_default(g_config, a_config_section, "wallet", NULL);
+//    const char *l_wallet_path = dap_config_get_item_str_default(g_config, "resources", "wallets_path", NULL);
+//    const char *l_wallet_name = dap_config_get_item_str_default(g_config, a_config_section, "wallet", NULL);
+
+    const char *l_wallet_addr = dap_config_get_item_str_default(g_config, a_config_section, "wallet_addr", NULL);
+    const char *l_cert_name = dap_config_get_item_str_default(g_config, a_config_section, "receipt_sign_cert", NULL);
     const char *l_net_name = dap_config_get_item_str_default(g_config, a_config_section, "net", NULL);
-    if (!l_wallet_path || !l_wallet_name || !l_net_name){
+    if (!l_wallet_addr || !l_cert_name || !l_net_name){
         return -2;
     }
-    dap_chain_wallet_t *l_wallet = dap_chain_wallet_open(l_wallet_name, l_wallet_path);
-    if (!l_wallet) {
-        return -3;
-    }
+//    dap_chain_wallet_t *l_wallet = dap_chain_wallet_open(l_wallet_name, l_wallet_path);
+//    if (!l_wallet) {
+//        return -3;
+//    }
     dap_chain_net_t *l_net = dap_chain_net_by_name(l_net_name);
     if (!l_net) {
         return -4;
@@ -875,7 +878,26 @@ int dap_chain_net_srv_price_apply_from_my_order(dap_chain_net_srv_t *a_srv, cons
                     continue;
                 }
             }
-            l_price->wallet = l_wallet;
+//            l_price->wallet = l_wallet;
+            l_price->wallet_addr = dap_chain_addr_from_str(l_wallet_addr);
+            if(!l_price->wallet_addr){
+                log_it(L_ERROR, "Can't get wallet addr from wallet_addr in config file.");
+                DAP_DEL_Z(l_order);
+                DAP_DELETE(l_price);
+                dap_global_db_objs_delete(l_orders, l_orders_count);
+                return -100;
+            }
+
+            l_price->receipt_sign_cert = dap_cert_find_by_name(l_cert_name);
+            if(!l_price->receipt_sign_cert){
+                log_it(L_ERROR, "Can't find cert %s.", l_cert_name);
+                DAP_DEL_Z(l_order);
+                DAP_DELETE(l_price);
+                dap_global_db_objs_delete(l_orders, l_orders_count);
+                return -101;
+            }
+
+
             // TODO: find most advantageous for us order
             DL_APPEND(a_srv->pricelist, l_price);
             break;
@@ -939,11 +961,11 @@ int dap_chain_net_srv_parse_pricelist(dap_chain_net_srv_t *a_srv, const char *a_
                 }
                 continue;
             case 5:
-                if (!(l_price->wallet = dap_chain_wallet_open(l_price_token, dap_config_get_item_str_default(g_config, "resources", "wallets_path", NULL)))) {
-                    log_it(L_ERROR, "Error parsing pricelist: can't open wallet \"%s\"", l_price_token);
-                    l_iter = 0;
-                    break;
-                }
+//                if (!(l_price->wallet = dap_chain_wallet_open(l_price_token, dap_config_get_item_str_default(g_config, "resources", "wallets_path", NULL)))) {
+//                    log_it(L_ERROR, "Error parsing pricelist: can't open wallet \"%s\"", l_price_token);
+//                    l_iter = 0;
+//                    break;
+//                }
                 continue;
             case 6:
                 log_it(L_INFO, "Price item correct, added to service");
@@ -1201,7 +1223,7 @@ dap_chain_datum_tx_receipt_t * dap_chain_net_srv_issue_receipt(dap_chain_net_srv
     dap_chain_datum_tx_receipt_t * l_receipt = dap_chain_datum_tx_receipt_create(
                     a_srv->uid, a_price->units_uid, a_price->units, a_price->value_datoshi, a_ext, a_ext_size);
     // Sign with our wallet
-    return dap_chain_datum_tx_receipt_sign_add(l_receipt, dap_chain_wallet_get_key(a_price->wallet, 0));
+    return dap_chain_datum_tx_receipt_sign_add(l_receipt, a_price->receipt_sign_cert->enc_key);
 }
 
 /**
diff --git a/modules/net/srv/include/dap_chain_net_srv.h b/modules/net/srv/include/dap_chain_net_srv.h
index ce7f9542a4..e2d5784949 100755
--- a/modules/net/srv/include/dap_chain_net_srv.h
+++ b/modules/net/srv/include/dap_chain_net_srv.h
@@ -69,7 +69,9 @@ typedef void (*dap_chain_callback_trafic_t)(dap_events_socket_t *, dap_stream_ch
 
 typedef struct dap_chain_net_srv_price
 {
-    dap_chain_wallet_t *wallet;
+//    dap_chain_wallet_t *wallet;
+    dap_chain_addr_t *wallet_addr;
+    dap_cert_t *receipt_sign_cert;
     char *net_name;
     dap_chain_net_t *net;
     uint256_t value_datoshi;
-- 
GitLab