diff --git a/electrum/descriptor.py b/electrum/descriptor.py index d23213e64d26..4311112f0af5 100644 --- a/electrum/descriptor.py +++ b/electrum/descriptor.py @@ -841,9 +841,11 @@ def _get_func_expr(s: str) -> Tuple[str, str]: try: start = s.index("(") end = s.rindex(")") - return s[0:start], s[start + 1:end] except ValueError: raise ValueError("A matching pair of parentheses cannot be found") + if end != len(s) - 1: + raise ValueError("Trailing characters '{}' after closing parenthesis".format(s[end + 1:])) + return s[0:start], s[start + 1:end] def _get_const(s: str, const: str) -> str: diff --git a/tests/test_descriptor.py b/tests/test_descriptor.py index 35bd50493b31..b1f61b977ab8 100644 --- a/tests/test_descriptor.py +++ b/tests/test_descriptor.py @@ -181,6 +181,14 @@ def test_parse_descriptor_with_key_at_end_without_origin(self): def test_parse_empty_descriptor(self): self.assertRaises(ValueError, parse_descriptor, "") + @as_testnet + def test_parse_descriptor_rejects_trailing_characters(self): + key = "tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B" + # trailing characters after the closing parenthesis must not be silently ignored + self.assertRaises(ValueError, parse_descriptor, "wpkh({}/0/0)abc".format(key)) + # the same applies to the closing parenthesis of a nested descriptor + self.assertRaises(ValueError, parse_descriptor, "sh(wpkh({}/0/0))x".format(key)) + @as_testnet def test_parse_descriptor_replace_h(self): d = "wpkh([00000001/84'/1'/0']tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/0/0)"