Skip to content

Commit b1a672d

Browse files
committed
Merge bitcoin/bitcoin#22337: wallet: Use bilingual_str for errors
92993aa Change SignTransaction's input_errors to use bilingual_str (Andrew Chow) 171366e Use bilingual_str for address fetching functions (Andrew Chow) 9571c69 Add bilingual_str::clear() (Andrew Chow) Pull request description: In a couple of places in the wallet, errors are `std::string`. In order for these errors to be translated, change them to use `bilingual_str`. ACKs for top commit: hebasto: re-ACK 92993aa, only rebased since my [previous](bitcoin/bitcoin#22337 (review)) review, verified with klementtan: Code review ACK 92993aa meshcollider: Code review ACK 92993aa Tree-SHA512: 5400e419dd87db8c49b67ed0964de2d44b58010a566ca246f2f0760ed9ef6a9b6f6df7a6adcb211b315b74c727bfe8c7d07eb5690b5922fa5828ceef4c83461f
2 parents a162edf + 92993aa commit b1a672d

17 files changed

+74
-60
lines changed

src/rpc/rawtransaction_util.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <univalue.h>
1919
#include <util/rbf.h>
2020
#include <util/strencodings.h>
21+
#include <util/translation.h>
2122

2223
CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniValue& outputs_in, const UniValue& locktime, bool rbf)
2324
{
@@ -280,22 +281,22 @@ void SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore,
280281
int nHashType = ParseSighashString(hashType);
281282

282283
// Script verification errors
283-
std::map<int, std::string> input_errors;
284+
std::map<int, bilingual_str> input_errors;
284285

285286
bool complete = SignTransaction(mtx, keystore, coins, nHashType, input_errors);
286287
SignTransactionResultToJSON(mtx, complete, coins, input_errors, result);
287288
}
288289

289-
void SignTransactionResultToJSON(CMutableTransaction& mtx, bool complete, const std::map<COutPoint, Coin>& coins, const std::map<int, std::string>& input_errors, UniValue& result)
290+
void SignTransactionResultToJSON(CMutableTransaction& mtx, bool complete, const std::map<COutPoint, Coin>& coins, const std::map<int, bilingual_str>& input_errors, UniValue& result)
290291
{
291292
// Make errors UniValue
292293
UniValue vErrors(UniValue::VARR);
293294
for (const auto& err_pair : input_errors) {
294-
if (err_pair.second == "Missing amount") {
295+
if (err_pair.second.original == "Missing amount") {
295296
// This particular error needs to be an exception for some reason
296297
throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing amount for %s", coins.at(mtx.vin.at(err_pair.first).prevout).out.ToString()));
297298
}
298-
TxInErrorToJSON(mtx.vin.at(err_pair.first), vErrors, err_pair.second);
299+
TxInErrorToJSON(mtx.vin.at(err_pair.first), vErrors, err_pair.second.original);
299300
}
300301

301302
result.pushKV("hex", EncodeHexTx(CTransaction(mtx)));

src/rpc/rawtransaction_util.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <map>
99
#include <string>
1010

11+
struct bilingual_str;
1112
class FillableSigningProvider;
1213
class UniValue;
1314
struct CMutableTransaction;
@@ -25,7 +26,7 @@ class SigningProvider;
2526
* @param result JSON object where signed transaction results accumulate
2627
*/
2728
void SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, const std::map<COutPoint, Coin>& coins, const UniValue& hashType, UniValue& result);
28-
void SignTransactionResultToJSON(CMutableTransaction& mtx, bool complete, const std::map<COutPoint, Coin>& coins, const std::map<int, std::string>& input_errors, UniValue& result);
29+
void SignTransactionResultToJSON(CMutableTransaction& mtx, bool complete, const std::map<COutPoint, Coin>& coins, const std::map<int, bilingual_str>& input_errors, UniValue& result);
2930

3031
/**
3132
* Parse a prevtxs UniValue array and get the map of coins from it

src/script/sign.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <script/signingprovider.h>
1212
#include <script/standard.h>
1313
#include <uint256.h>
14+
#include <util/translation.h>
1415
#include <util/vector.h>
1516

1617
typedef std::vector<unsigned char> valtype;
@@ -629,7 +630,7 @@ bool IsSegWitOutput(const SigningProvider& provider, const CScript& script)
629630
return false;
630631
}
631632

632-
bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, const std::map<COutPoint, Coin>& coins, int nHashType, std::map<int, std::string>& input_errors)
633+
bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, const std::map<COutPoint, Coin>& coins, int nHashType, std::map<int, bilingual_str>& input_errors)
633634
{
634635
bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
635636

@@ -661,7 +662,7 @@ bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore,
661662
CTxIn& txin = mtx.vin[i];
662663
auto coin = coins.find(txin.prevout);
663664
if (coin == coins.end() || coin->second.IsSpent()) {
664-
input_errors[i] = "Input not found or already spent";
665+
input_errors[i] = _("Input not found or already spent");
665666
continue;
666667
}
667668
const CScript& prevPubKey = coin->second.out.scriptPubKey;
@@ -677,20 +678,20 @@ bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore,
677678

678679
// amount must be specified for valid segwit signature
679680
if (amount == MAX_MONEY && !txin.scriptWitness.IsNull()) {
680-
input_errors[i] = "Missing amount";
681+
input_errors[i] = _("Missing amount");
681682
continue;
682683
}
683684

684685
ScriptError serror = SCRIPT_ERR_OK;
685686
if (!VerifyScript(txin.scriptSig, prevPubKey, &txin.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, TransactionSignatureChecker(&txConst, i, amount, txdata, MissingDataBehavior::FAIL), &serror)) {
686687
if (serror == SCRIPT_ERR_INVALID_STACK_OPERATION) {
687688
// Unable to sign input and verification failed (possible attempt to partially sign).
688-
input_errors[i] = "Unable to sign input, invalid stack size (possibly missing key)";
689+
input_errors[i] = Untranslated("Unable to sign input, invalid stack size (possibly missing key)");
689690
} else if (serror == SCRIPT_ERR_SIG_NULLFAIL) {
690691
// Verification failed (possibly due to insufficient signatures).
691-
input_errors[i] = "CHECK(MULTI)SIG failing with non-zero signature (possibly need more signatures)";
692+
input_errors[i] = Untranslated("CHECK(MULTI)SIG failing with non-zero signature (possibly need more signatures)");
692693
} else {
693-
input_errors[i] = ScriptErrorString(serror);
694+
input_errors[i] = Untranslated(ScriptErrorString(serror));
694695
}
695696
} else {
696697
// If this input succeeds, make sure there is no error set for it

src/script/sign.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class CScript;
2121
class CTransaction;
2222
class SigningProvider;
2323

24+
struct bilingual_str;
2425
struct CMutableTransaction;
2526

2627
/** Interface for signature creators. */
@@ -178,6 +179,6 @@ bool IsSolvable(const SigningProvider& provider, const CScript& script);
178179
bool IsSegWitOutput(const SigningProvider& provider, const CScript& script);
179180

180181
/** Sign the CMutableTransaction */
181-
bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* provider, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, std::string>& input_errors);
182+
bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* provider, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors);
182183

183184
#endif // BITCOIN_SCRIPT_SIGN_H

src/test/fuzz/script_sign.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <test/fuzz/FuzzedDataProvider.h>
1414
#include <test/fuzz/fuzz.h>
1515
#include <test/fuzz/util.h>
16+
#include <util/translation.h>
1617

1718
#include <cassert>
1819
#include <cstdint>
@@ -135,7 +136,7 @@ FUZZ_TARGET_INIT(script_sign, initialize_script_sign)
135136
}
136137
coins[*outpoint] = *coin;
137138
}
138-
std::map<int, std::string> input_errors;
139+
std::map<int, bilingual_str> input_errors;
139140
(void)SignTransaction(sign_transaction_tx_to, &provider, coins, fuzzed_data_provider.ConsumeIntegral<int>(), input_errors);
140141
}
141142
}

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ CMutableTransaction TestChain100Setup::CreateValidMempoolTransaction(CTransactio
292292
input_coins.insert({outpoint_to_spend, utxo_to_spend});
293293
// - Default signature hashing type
294294
int nHashType = SIGHASH_ALL;
295-
std::map<int, std::string> input_errors;
295+
std::map<int, bilingual_str> input_errors;
296296
assert(SignTransaction(mempool_txn, &keystore, input_coins, nHashType, input_errors));
297297

298298
// If submit=true, add transaction to the mempool.

src/test/util/wallet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <outputtype.h>
99
#include <script/standard.h>
1010
#ifdef ENABLE_WALLET
11+
#include <util/translation.h>
1112
#include <wallet/wallet.h>
1213
#endif
1314

@@ -18,7 +19,7 @@ std::string getnewaddress(CWallet& w)
1819
{
1920
constexpr auto output_type = OutputType::BECH32;
2021
CTxDestination dest;
21-
std::string error;
22+
bilingual_str error;
2223
if (!w.GetNewDestination(output_type, "", dest, error)) assert(false);
2324

2425
return EncodeDestination(dest);

src/util/translation.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ struct bilingual_str {
2828
{
2929
return original.empty();
3030
}
31+
32+
void clear()
33+
{
34+
original.clear();
35+
translated.clear();
36+
}
3137
};
3238

3339
inline bilingual_str operator+(bilingual_str lhs, const bilingual_str& rhs)

src/wallet/interfaces.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <uint256.h>
1717
#include <util/check.h>
1818
#include <util/system.h>
19+
#include <util/translation.h>
1920
#include <util/ui_change_type.h>
2021
#include <wallet/context.h>
2122
#include <wallet/feebumper.h>
@@ -130,7 +131,7 @@ class WalletImpl : public Wallet
130131
bool getNewDestination(const OutputType type, const std::string label, CTxDestination& dest) override
131132
{
132133
LOCK(m_wallet->cs_wallet);
133-
std::string error;
134+
bilingual_str error;
134135
return m_wallet->GetNewDestination(type, label, dest, error);
135136
}
136137
bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) override

src/wallet/rpcwallet.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,9 @@ static RPCHelpMan getnewaddress()
276276
}
277277

278278
CTxDestination dest;
279-
std::string error;
279+
bilingual_str error;
280280
if (!pwallet->GetNewDestination(output_type, label, dest, error)) {
281-
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error);
281+
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error.original);
282282
}
283283

284284
return EncodeDestination(dest);
@@ -324,9 +324,9 @@ static RPCHelpMan getrawchangeaddress()
324324
}
325325

326326
CTxDestination dest;
327-
std::string error;
327+
bilingual_str error;
328328
if (!pwallet->GetNewChangeDestination(output_type, dest, error)) {
329-
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error);
329+
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error.original);
330330
}
331331
return EncodeDestination(dest);
332332
},
@@ -3392,7 +3392,7 @@ RPCHelpMan signrawtransactionwithwallet()
33923392
int nHashType = ParseSighashString(request.params[2]);
33933393

33943394
// Script verification errors
3395-
std::map<int, std::string> input_errors;
3395+
std::map<int, bilingual_str> input_errors;
33963396

33973397
bool complete = pwallet->SignTransaction(mtx, coins, nHashType, input_errors);
33983398
UniValue result(UniValue::VOBJ);

0 commit comments

Comments
 (0)