mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-13 22:59:34 +02:00
rpc: use HexToPubKey
helper for all legacy pubkey-parsing RPCs
This deduplicates code and leads to more consistent and detailed error messages. Affected are legacy import RPCs (`importpubkey`, `importmulti`) and other ones where solving data can be provided (`fundrawtransaction`, `walletcreatefundedpsbt`, `send`, `sendall`).
This commit is contained in:
parent
100e8a75bf
commit
c740b154d1
@ -460,12 +460,7 @@ RPCHelpMan importpubkey()
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
|
||||
}
|
||||
|
||||
if (!IsHex(request.params[0].get_str()))
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey must be a hex string");
|
||||
std::vector<unsigned char> data(ParseHex(request.params[0].get_str()));
|
||||
CPubKey pubKey(data);
|
||||
if (!pubKey.IsFullyValid())
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey is not a valid public key");
|
||||
CPubKey pubKey = HexToPubKey(request.params[0].get_str());
|
||||
|
||||
{
|
||||
LOCK(pwallet->cs_wallet);
|
||||
@ -986,15 +981,7 @@ static UniValue ProcessImportLegacy(ImportData& import_data, std::map<CKeyID, CP
|
||||
import_data.witnessscript = std::make_unique<CScript>(parsed_witnessscript.begin(), parsed_witnessscript.end());
|
||||
}
|
||||
for (size_t i = 0; i < pubKeys.size(); ++i) {
|
||||
const auto& str = pubKeys[i].get_str();
|
||||
if (!IsHex(str)) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey \"" + str + "\" must be a hex string");
|
||||
}
|
||||
auto parsed_pubkey = ParseHex(str);
|
||||
CPubKey pubkey(parsed_pubkey);
|
||||
if (!pubkey.IsFullyValid()) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey \"" + str + "\" is not a valid public key");
|
||||
}
|
||||
CPubKey pubkey = HexToPubKey(pubKeys[i].get_str());
|
||||
pubkey_map.emplace(pubkey.GetID(), pubkey);
|
||||
ordered_pubkeys.push_back(pubkey.GetID());
|
||||
}
|
||||
|
@ -627,15 +627,7 @@ CreatedTransactionResult FundTransaction(CWallet& wallet, const CMutableTransact
|
||||
const UniValue solving_data = options["solving_data"].get_obj();
|
||||
if (solving_data.exists("pubkeys")) {
|
||||
for (const UniValue& pk_univ : solving_data["pubkeys"].get_array().getValues()) {
|
||||
const std::string& pk_str = pk_univ.get_str();
|
||||
if (!IsHex(pk_str)) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("'%s' is not hex", pk_str));
|
||||
}
|
||||
const std::vector<unsigned char> data(ParseHex(pk_str));
|
||||
const CPubKey pubkey(data.begin(), data.end());
|
||||
if (!pubkey.IsFullyValid()) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("'%s' is not a valid public key", pk_str));
|
||||
}
|
||||
const CPubKey pubkey = HexToPubKey(pk_univ.get_str());
|
||||
coinControl.m_external_provider.pubkeys.emplace(pubkey.GetID(), pubkey);
|
||||
// Add witness script for pubkeys
|
||||
const CScript wit_script = GetScriptForDestination(WitnessV0KeyHash(pubkey));
|
||||
|
@ -459,10 +459,11 @@ class WalletTest(BitcoinTestFramework):
|
||||
assert_raises_rpc_error(-5, "Invalid Bitcoin address or script", self.nodes[0].importaddress, "invalid")
|
||||
|
||||
# This will raise an exception for attempting to import a pubkey that isn't in hex
|
||||
assert_raises_rpc_error(-5, "Pubkey must be a hex string", self.nodes[0].importpubkey, "not hex")
|
||||
assert_raises_rpc_error(-5, 'Pubkey "not hex" must be a hex string', self.nodes[0].importpubkey, "not hex")
|
||||
|
||||
# This will raise an exception for importing an invalid pubkey
|
||||
assert_raises_rpc_error(-5, "Pubkey is not a valid public key", self.nodes[0].importpubkey, "5361746f736869204e616b616d6f746f")
|
||||
# This will raise an exception for importing a pubkey with invalid length
|
||||
too_short_pubkey = "5361746f736869204e616b616d6f746f"
|
||||
assert_raises_rpc_error(-5, f'Pubkey "{too_short_pubkey}" must have a length of either 33 or 65 bytes', self.nodes[0].importpubkey, too_short_pubkey)
|
||||
|
||||
# Bech32m addresses cannot be imported into a legacy wallet
|
||||
assert_raises_rpc_error(-5, "Bech32m addresses cannot be imported into legacy wallets", self.nodes[0].importaddress, "bcrt1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqc8gma6")
|
||||
|
@ -1055,8 +1055,8 @@ class RawTransactionsTest(BitcoinTestFramework):
|
||||
assert_raises_rpc_error(-4, "Not solvable pre-selected input COutPoint(%s, %s)" % (ext_utxo["txid"][0:10], ext_utxo["vout"]), wallet.fundrawtransaction, raw_tx)
|
||||
|
||||
# Error conditions
|
||||
assert_raises_rpc_error(-5, "'not a pubkey' is not hex", wallet.fundrawtransaction, raw_tx, solving_data={"pubkeys":["not a pubkey"]})
|
||||
assert_raises_rpc_error(-5, "'01234567890a0b0c0d0e0f' is not a valid public key", wallet.fundrawtransaction, raw_tx, solving_data={"pubkeys":["01234567890a0b0c0d0e0f"]})
|
||||
assert_raises_rpc_error(-5, 'Pubkey "not a pubkey" must be a hex string', wallet.fundrawtransaction, raw_tx, solving_data={"pubkeys":["not a pubkey"]})
|
||||
assert_raises_rpc_error(-5, 'Pubkey "01234567890a0b0c0d0e0f" must have a length of either 33 or 65 bytes', wallet.fundrawtransaction, raw_tx, solving_data={"pubkeys":["01234567890a0b0c0d0e0f"]})
|
||||
assert_raises_rpc_error(-5, "'not a script' is not hex", wallet.fundrawtransaction, raw_tx, solving_data={"scripts":["not a script"]})
|
||||
assert_raises_rpc_error(-8, "Unable to parse descriptor 'not a descriptor'", wallet.fundrawtransaction, raw_tx, solving_data={"descriptors":["not a descriptor"]})
|
||||
assert_raises_rpc_error(-8, "Invalid parameter, missing vout key", wallet.fundrawtransaction, raw_tx, input_weights=[{"txid": ext_utxo["txid"]}])
|
||||
|
Loading…
x
Reference in New Issue
Block a user