From 295774821b274397b0acf1b0bfa613a8add11f74 Mon Sep 17 00:00:00 2001 From: levonpetrosyan93 <45027856+levonpetrosyan93@users.noreply.github.com> Date: Thu, 25 Jan 2024 20:16:12 +0400 Subject: [PATCH] getsparkcoinaddr rpc added (#1397) * getsparkcoinaddr rpc added * Fix crashing ui bug * Bug fixed --- src/qt/walletmodel.cpp | 9 ++++---- src/wallet/rpcwallet.cpp | 45 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp index 5230375437..e6908f973d 100644 --- a/src/qt/walletmodel.cpp +++ b/src/qt/walletmodel.cpp @@ -1547,18 +1547,19 @@ WalletModel::SendCoinsReturn WalletModel::mintSparkCoins(std::vectorCommitTransaction(wtxAndFee[i].first, *reservekey++, g_connman.get(), state)) + return SendCoinsReturn(TransactionCommitFailed, QString::fromStdString(state.GetRejectReason())); + Q_FOREACH(const SendCoinsRecipient &rcp, transactions[i].getRecipients()) { // CWalletTx* newTx = transactions[i].getTransaction(); if (!rcp.message.isEmpty()) // Message from normal firo:URI (firo:123...?message=example) wtxAndFee[i].first.vOrderForm.push_back(make_pair("Message", rcp.message.toStdString())); - if (!wallet->CommitTransaction(wtxAndFee[i].first, *reservekey++, g_connman.get(), state)) - return SendCoinsReturn(TransactionCommitFailed, QString::fromStdString(state.GetRejectReason())); - + CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); ssTx << *wtxAndFee[i].first.tx; transaction_array.append(&(ssTx[0]), ssTx.size()); - + { std::string strAddress = rcp.address.toStdString(); std::string strLabel = rcp.label.toStdString(); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 45d90dc4b5..3357bbac11 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -3937,7 +3937,7 @@ UniValue identifysparkcoins(const JSONRPCRequest& request) if (request.fHelp || request.params.size() != 1) throw std::runtime_error( - "identifysparkcoin \"txHash\"\n" + "identifysparkcoins \"txHash\"\n" "Identifies coins in transaction, and adds into wallet if yours"); EnsureSparkWalletIsAvailable(); @@ -3968,6 +3968,48 @@ UniValue identifysparkcoins(const JSONRPCRequest& request) return results; } +UniValue getsparkcoinaddr(const JSONRPCRequest& request) +{ + CWallet * const pwallet = GetWalletForJSONRPCRequest(request); + if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) { + return NullUniValue; + } + + if (request.fHelp || request.params.size() != 1) + throw std::runtime_error( + "getsparkcoinaddr \"txHash\"\n" + "Returns all spark outputs to you, address memo and amount for each"); + + EnsureSparkWalletIsAvailable(); + + uint256 txHash; + txHash.SetHex(request.params[0].get_str()); + + CTransactionRef tx; + uint256 hashBlock; + GetTransaction(txHash, tx, Params().GetConsensus(), hashBlock); + + UniValue results(UniValue::VARR);; + assert(pwallet != NULL); + + std::unordered_map coins = pwallet->sparkWallet->getMintMap(); + unsigned char network = spark::GetNetworkType(); + + for (const auto& coin : coins) + { + if (txHash == coin.second.txid) { + spark::Address address = pwallet->sparkWallet->getAddress(coin.second.i); + UniValue entry(UniValue::VOBJ); + entry.push_back(Pair("address", address.encode(network))); + entry.push_back(Pair("memo", SanitizeString(coin.second.memo))); + entry.push_back(Pair("amount", ValueFromAmount(coin.second.v))); + results.push_back(entry); + } + } + + return results; +} + UniValue mint(const JSONRPCRequest& request) { CWallet * const pwallet = GetWalletForJSONRPCRequest(request); @@ -5720,6 +5762,7 @@ static const CRPCCommand commands[] = { "wallet", "spendspark", &spendspark, false }, { "wallet", "lelantustospark", &lelantustospark, false }, { "wallet", "identifysparkcoins", &identifysparkcoins, false }, + { "wallet", "getsparkcoinaddr", &getsparkcoinaddr, false }, //bip47