From 104b3a5069c937383e6f88f2f3fb804ef61b208f Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 8 May 2019 14:01:35 -0700 Subject: Factor out checksum checking from descriptor parsing --- src/script/descriptor.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'src/script') diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index 50119ba184..95b949e9c2 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -910,22 +910,29 @@ std::unique_ptr InferScript(const CScript& script, ParseScriptCo } // namespace -std::unique_ptr Parse(const std::string& descriptor, FlatSigningProvider& out, bool require_checksum) +/** Check a descriptor checksum, and update desc to be the checksum-less part. */ +bool CheckChecksum(Span& sp, bool require_checksum, std::string* out_checksum = nullptr) { - Span sp(descriptor.data(), descriptor.size()); - - // Checksum checks auto check_split = Split(sp, '#'); - if (check_split.size() > 2) return nullptr; // Multiple '#' symbols - if (check_split.size() == 1 && require_checksum) return nullptr; // Missing checksum + if (check_split.size() > 2) return false; // Multiple '#' symbols + if (check_split.size() == 1 && require_checksum) return false; // Missing checksum + if (check_split.size() == 2) { + if (check_split[1].size() != 8) return false; // Unexpected length for checksum + } + auto checksum = DescriptorChecksum(check_split[0]); + if (checksum.empty()) return false; // Invalid characters in payload if (check_split.size() == 2) { - if (check_split[1].size() != 8) return nullptr; // Unexpected length for checksum - auto checksum = DescriptorChecksum(check_split[0]); - if (checksum.empty()) return nullptr; // Invalid characters in payload - if (!std::equal(checksum.begin(), checksum.end(), check_split[1].begin())) return nullptr; // Checksum mismatch + if (!std::equal(checksum.begin(), checksum.end(), check_split[1].begin())) return false; // Checksum mismatch } + if (out_checksum) *out_checksum = std::move(checksum); sp = check_split[0]; + return true; +} +std::unique_ptr Parse(const std::string& descriptor, FlatSigningProvider& out, bool require_checksum) +{ + Span sp(descriptor.data(), descriptor.size()); + if (!CheckChecksum(sp, require_checksum)) return nullptr; auto ret = ParseScript(sp, ParseScriptContext::TOP, out); if (sp.size() == 0 && ret) return std::unique_ptr(std::move(ret)); return nullptr; -- cgit v1.2.3