From a174336f7dd2e0a19ae0367a8921fff5b5659684 Mon Sep 17 00:00:00 2001
From: "alexey.stratulat" <alexey.stratulat@demlabs.net>
Date: Sat, 9 Nov 2024 21:14:33 +0700
Subject: [PATCH] [*] Port from hotfix-13846: The following changes and checks
 have been added:

1. Added a check to the wallet signature verification function to make sure that the wallet certificate is loaded at all, otherwise an error message is displayed.
2. Added NULL check for a key that was derived from a wallet certificate.
3. Added error output in wallet info if a wallet contains a barb whose type is not supported.
---
 modules/node-cli/dap_chain_node_cli_cmd.c         | 12 ++++++++----
 modules/node-cli/include/dap_chain_node_cli_cmd.h |  1 +
 modules/wallet/dap_chain_wallet.c                 |  7 +++++--
 3 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/modules/node-cli/dap_chain_node_cli_cmd.c b/modules/node-cli/dap_chain_node_cli_cmd.c
index 5d3563ec37..e3524f9056 100644
--- a/modules/node-cli/dap_chain_node_cli_cmd.c
+++ b/modules/node-cli/dap_chain_node_cli_cmd.c
@@ -1949,16 +1949,20 @@ int l_arg_index = 1, l_rc, cmd_num = CMD_NONE;
                 l_addr = dap_chain_addr_from_str(l_addr_str);
             }
             
-            if (!l_addr){
-                if(l_wallet)
+            if (!l_addr || dap_chain_addr_is_blank(l_addr)){
+                if(l_wallet) {
+                    dap_json_rpc_error_add(*a_json_arr_reply, DAP_CHAIN_NODE_CLI_COM_TX_WALLET_CAN_NOT_GET_ADDR,
+                                           "Wallet %s contains an unknown certificate type, the wallet address could not be calculated.", l_wallet_name);
                     dap_chain_wallet_close(l_wallet);
+                    return DAP_CHAIN_NODE_CLI_COM_TX_WALLET_CAN_NOT_GET_ADDR;
+                }
                 dap_json_rpc_error_add(*a_json_arr_reply, DAP_CHAIN_NODE_CLI_COM_TX_WALLET_FOUND_ERR,
-                                       "Wallet not found");
+                                       "Wallet not found or addr not recognized");
                 json_object_put(json_arr_out);
                 return DAP_CHAIN_NODE_CLI_COM_TX_WALLET_FOUND_ERR;
             } else {
                 l_net = dap_chain_net_by_id(l_addr->net_id);
-                if(l_net) {
+                if (l_net) {
                     l_ledger = l_net->pub.ledger;
                     l_net_name = l_net->pub.name;
                 } else {
diff --git a/modules/node-cli/include/dap_chain_node_cli_cmd.h b/modules/node-cli/include/dap_chain_node_cli_cmd.h
index 2c83ebd9ee..984f88d807 100644
--- a/modules/node-cli/include/dap_chain_node_cli_cmd.h
+++ b/modules/node-cli/include/dap_chain_node_cli_cmd.h
@@ -158,6 +158,7 @@ typedef enum s_com_tx_wallet_err{
     DAP_CHAIN_NODE_CLI_COM_TX_WALLET_HASH_ERR,
     DAP_CHAIN_NODE_CLI_COM_TX_WALLET_CHAIN_PARAM_ERR,
     DAP_CHAIN_NODE_CLI_COM_TX_WALLET_INTERNAL_ERR,
+    DAP_CHAIN_NODE_CLI_COM_TX_WALLET_CAN_NOT_GET_ADDR,
 
     /* add custom codes here */
 
diff --git a/modules/wallet/dap_chain_wallet.c b/modules/wallet/dap_chain_wallet.c
index 5ca2f079d1..66f9383487 100644
--- a/modules/wallet/dap_chain_wallet.c
+++ b/modules/wallet/dap_chain_wallet.c
@@ -490,8 +490,10 @@ dap_chain_addr_t *dap_cert_to_addr(dap_cert_t **a_certs, size_t a_count, size_t
     dap_chain_addr_t *l_addr = NULL;
     DAP_NEW_Z_RET_VAL(l_addr, dap_chain_addr_t, NULL, NULL);
     dap_enc_key_t *l_key = dap_cert_get_keys_from_certs(a_certs, a_count, a_key_start_index);
-    dap_chain_addr_fill_from_key(l_addr, l_key, a_net_id);
-    dap_enc_key_delete(l_key);
+    if (l_key) {
+        dap_chain_addr_fill_from_key(l_addr, l_key, a_net_id);
+        dap_enc_key_delete(l_key);
+    }
     return l_addr;
 }
 
@@ -1027,6 +1029,7 @@ const char* dap_chain_wallet_check_sign(dap_chain_wallet_t *a_wallet) {
     dap_chain_wallet_internal_t *l_wallet_internal = DAP_CHAIN_WALLET_INTERNAL(a_wallet);
     dap_return_val_if_pass(!l_wallet_internal->certs || !l_wallet_internal->certs, "" );
     for (size_t i = 0; i < l_wallet_internal->certs_count; ++i) {
+        dap_return_val_if_pass(!l_wallet_internal->certs[i], "The wallet contains an undefined certificate.\n");
         dap_sign_type_t l_sign_type = dap_sign_type_from_key_type(l_wallet_internal->certs[i]->enc_key->type);
         if (SIG_TYPE_BLISS == l_sign_type.type || SIG_TYPE_PICNIC == l_sign_type.type || SIG_TYPE_TESLA == l_sign_type.type) {
             return "The Bliss, Picnic and Tesla signatures is deprecated. We recommend you to create a new wallet with another available signature and transfer funds there.\n";
-- 
GitLab