mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-05 18:52:29 +02:00
Merge bitcoin/bitcoin#32821: rpc: Handle -named argument parsing where '=' character is used
f53dbbc505test: Add functional tests for named argument parsing (zaidmstrr)694f04e2bdrpc: Handle -named argument parsing where '=' character is used (zaidmstrr) Pull request description: Addresses [comment](https://github.com/bitcoin/bitcoin/pull/31375#discussion_r2091886628) and [this](https://github.com/bitcoin/bitcoin/pull/31375#discussion_r2092039999). The [PR #31375](https://github.com/bitcoin/bitcoin/pull/31375) got merged and enables `-named` by default in the `bitcoin rpc` interface; `bitcoin rpc` corresponds to `bitcoin-cli -named` as it's just a wrapper. Now, the problem arises when we try to parse the positional paramater which might contain "=" character. This splits the parameter into two parts first, before the "=" character, which treats this as the parameter name, but the other half is mostly passed as an empty string. Here, the first part of the string is an unknown parameter name; thus, an error is thrown. These types of errors are only applicable to those RPCs which might contain the `=` character as a parameter. Some examples are `finalizepsbt`, `decodepsbt`, `verifymessage` etc. This is the one example of the error in `finalizepsbt` RPC: ``` ./bitcoin-cli -named -regtest finalizepsbt cHNidP8BAJoCAAAAAqvNEjSrzRI0q80SNKvNEjSrzRI0q80SNKvNEjSrzRI0AAAAAAD9////NBLNqzQSzas0Es2rNBLNqzQSzas0Es2rNBLNqzQSzasBAAAAAP3///8CoIYBAAAAAAAWABQVQBGVs/sqFAmC8HZ8O+g1htqivkANAwAAAAAAFgAUir7MzgyzDnRMjdkVa7d+Dwr07jsAAAAAAAAAAAA= error code: -8 error message: Unknown named parameter cHNidP8BAJoCAAAAAqvNEjSrzRI0q80SNKvNEjSrzRI0q80SNKvNEjSrzRI0AAAAAAD9////NBLNqzQSzas0Es2rNBLNqzQSzas0Es2rNBLNqzQSzasBAAAAAP3///8CoIYBAAAAAAAWABQVQBGVs/sqFAmC8HZ8O+g1htqivkANAwAAAAAAFgAUir7MzgyzDnRMjdkVa7d+Dwr07jsAAAAAAAAAAAA ``` This PR fixes this by updating the `vRPCConvertParams` table that identifies parameters that need special handling in `-named` parameter mode. The parser now recognises these parameters and handles strings with "=" char correctly, preventing them from being incorrectly split as parameter assignments. ACKs for top commit: ryanofsky: Code review ACKf53dbbc505. Just applied comment & test suggestions since last review kannapoix: Code review ACK:f53dbbc505achow101: ACKf53dbbc505Tree-SHA512: 1b517144efeff45a4c4256c27a39ddf187f1d6189d133402a45171678214a10ff2925c31edcfd556d67f85bd26d42f63c528b941b68c9880eab443f2c883e681
This commit is contained in:
@@ -369,6 +369,43 @@ class PSBTTest(BitcoinTestFramework):
|
||||
changepos = psbtx["changepos"]
|
||||
assert_equal(decoded_psbt["tx"]["vout"][changepos]["scriptPubKey"]["type"], expected_type)
|
||||
|
||||
def test_psbt_named_parameter_handling(self):
|
||||
"""Test that PSBT Base64 parameters with '=' padding are handled correctly in -named mode"""
|
||||
self.log.info("Testing PSBT Base64 parameter handling with '=' padding characters")
|
||||
node = self.nodes[0]
|
||||
psbt_with_padding = "cHNidP8BAJoCAAAAAqvNEjSrzRI0q80SNKvNEjSrzRI0q80SNKvNEjSrzRI0AAAAAAD9////NBLNqzQSzas0Es2rNBLNqzQSzas0Es2rNBLNqzQSzasBAAAAAP3///8CoIYBAAAAAAAWABQVQBGVs/sqFAmC8HZ8O+g1htqivkANAwAAAAAAFgAUir7MzgyzDnRMjdkVa7d+Dwr07jsAAAAAAAAAAAA="
|
||||
|
||||
# Test decodepsbt with explicit named parameter containing '=' padding
|
||||
result = node.cli("-named", "decodepsbt", f"psbt={psbt_with_padding}").send_cli()
|
||||
assert 'tx' in result
|
||||
|
||||
# Test decodepsbt with positional argument containing '=' padding
|
||||
result = node.cli("-named", "decodepsbt", psbt_with_padding).send_cli()
|
||||
assert 'tx' in result
|
||||
|
||||
# Test analyzepsbt with positional argument containing '=' padding
|
||||
result = node.cli("-named", "analyzepsbt", psbt_with_padding).send_cli()
|
||||
assert 'inputs' in result
|
||||
|
||||
# Test finalizepsbt with positional argument containing '=' padding
|
||||
result = node.cli("-named", "finalizepsbt", psbt_with_padding, "extract=true").send_cli()
|
||||
assert 'complete' in result
|
||||
|
||||
# Test walletprocesspsbt with positional argument containing '=' padding
|
||||
result = node.cli("-named", "walletprocesspsbt", psbt_with_padding).send_cli()
|
||||
assert 'complete' in result
|
||||
|
||||
# Test utxoupdatepsbt with positional argument containing '=' padding
|
||||
result = node.cli("-named", "utxoupdatepsbt", psbt_with_padding).send_cli()
|
||||
assert isinstance(result, str) and len(result) > 0
|
||||
|
||||
# Test that unknown parameter with '=' gets treated as positional and return error
|
||||
unknown_psbt_param = "unknown_param_data=more_data="
|
||||
# This should be treated as positional and fail with decode error, not parameter error
|
||||
assert_raises_rpc_error(-22, "TX decode failed invalid base64", node.cli("-named", "finalizepsbt", unknown_psbt_param).send_cli)
|
||||
|
||||
self.log.info("PSBT parameter handling test completed successfully")
|
||||
|
||||
def run_test(self):
|
||||
# Create and fund a raw tx for sending 10 BTC
|
||||
psbtx1 = self.nodes[0].walletcreatefundedpsbt([], {self.nodes[2].getnewaddress():10})['psbt']
|
||||
@@ -1211,6 +1248,7 @@ class PSBTTest(BitcoinTestFramework):
|
||||
if not self.options.usecli:
|
||||
self.test_sighash_mismatch()
|
||||
self.test_sighash_adding()
|
||||
self.test_psbt_named_parameter_handling()
|
||||
|
||||
if __name__ == '__main__':
|
||||
PSBTTest(__file__).main()
|
||||
|
||||
Reference in New Issue
Block a user