Skip to content

Commit 242c8bd

Browse files
committed
Handle empty string in output type parsing
1 parent 305b6bc commit 242c8bd

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/wallet/wallet.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4012,12 +4012,12 @@ CWallet* CWallet::CreateWalletFromFile(const std::string& name, const fs::path&
40124012
}
40134013
}
40144014

4015-
if (!gArgs.GetArg("-addresstype", "").empty() && !ParseOutputType(gArgs.GetArg("-addresstype", ""), walletInstance->m_default_address_type)) {
4015+
if (!ParseOutputType(gArgs.GetArg("-addresstype", ""), walletInstance->m_default_address_type)) {
40164016
InitError(strprintf("Unknown address type '%s'", gArgs.GetArg("-addresstype", "")));
40174017
return nullptr;
40184018
}
40194019

4020-
if (!gArgs.GetArg("-changetype", "").empty() && !ParseOutputType(gArgs.GetArg("-changetype", ""), walletInstance->m_default_change_type)) {
4020+
if (!ParseOutputType(gArgs.GetArg("-changetype", ""), walletInstance->m_default_change_type)) {
40214021
InitError(strprintf("Unknown change type '%s'", gArgs.GetArg("-changetype", "")));
40224022
return nullptr;
40234023
}
@@ -4207,7 +4207,9 @@ static const std::string OUTPUT_TYPE_STRING_BECH32 = "bech32";
42074207

42084208
bool ParseOutputType(const std::string& type, OutputType& output_type)
42094209
{
4210-
if (type == OUTPUT_TYPE_STRING_LEGACY) {
4210+
if (type.empty()) {
4211+
return true;
4212+
} else if (type == OUTPUT_TYPE_STRING_LEGACY) {
42114213
output_type = OutputType::LEGACY;
42124214
return true;
42134215
} else if (type == OUTPUT_TYPE_STRING_P2SH_SEGWIT) {

test/functional/rpc_fundrawtransaction.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,10 @@ def run_test(self):
224224
outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
225225
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
226226
assert_raises_rpc_error(-1, "JSON value is not a string as expected", self.nodes[2].fundrawtransaction, rawtx, {'change_type': None})
227-
assert_raises_rpc_error(-5, "Unknown change type ''", self.nodes[2].fundrawtransaction, rawtx, {'change_type': ''})
227+
rawtx = self.nodes[2].fundrawtransaction(rawtx, {'change_type': ''})
228+
dec_tx = self.nodes[2].decoderawtransaction(rawtx['hex'])
229+
assert_equal('scripthash', dec_tx['vout'][rawtx['changepos']]['scriptPubKey']['type'])
230+
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
228231
rawtx = self.nodes[2].fundrawtransaction(rawtx, {'change_type': 'bech32'})
229232
dec_tx = self.nodes[2].decoderawtransaction(rawtx['hex'])
230233
assert_equal('witness_v0_keyhash', dec_tx['vout'][rawtx['changepos']]['scriptPubKey']['type'])

test/functional/wallet_address_types.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,12 @@ def run_test(self):
280280
self.log.info('getrawchangeaddress defaults to addresstype if -changetype is not set and argument is absent')
281281
self.test_address(3, self.nodes[3].getrawchangeaddress(), multisig=False, typ='bech32')
282282

283+
self.log.info('test empty address type arguments')
284+
self.nodes[3].addmultisigaddress(2, [compressed_1, compressed_2], None, '')
285+
self.nodes[3].getnewaddress(None, '')
286+
self.nodes[3].getrawchangeaddress('')
287+
283288
self.log.info('test invalid address type arguments')
284-
assert_raises_rpc_error(-5, "Unknown address type ''", self.nodes[3].addmultisigaddress, 2, [compressed_1, compressed_2], None, '')
285-
assert_raises_rpc_error(-5, "Unknown address type ''", self.nodes[3].getnewaddress, None, '')
286-
assert_raises_rpc_error(-5, "Unknown address type ''", self.nodes[3].getrawchangeaddress, '')
287289
assert_raises_rpc_error(-5, "Unknown address type 'bech23'", self.nodes[3].getrawchangeaddress, 'bech23')
288290

289291
self.log.info("Nodes with changetype=p2sh-segwit never use a P2WPKH change output")

0 commit comments

Comments
 (0)