From 32e3d21ead59efd6cac5389d2a9ba84b36507ef6 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Mon, 27 Mar 2023 21:47:52 +0000 Subject: [PATCH] codex32: provide user-readable error types Github-Pull: #27351 Rebased-From: 1fa04b11b2452a2f15906d97cf1370d43f0dc23e --- src/codex32.cpp | 20 ++++++++++++++++++++ src/codex32.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/src/codex32.cpp b/src/codex32.cpp index 13b5361129..2fd2271902 100644 --- a/src/codex32.cpp +++ b/src/codex32.cpp @@ -219,6 +219,26 @@ uint8_t lagrange_coefficient(std::vector& indices, uint8_t idx, uint8_t } // namespace +std::string ErrorString(Error e) { + switch (e) { + case OK: return "ok"; + case BAD_CHECKSUM: return "bad checksum"; + case BECH32_DECODE: return "bech32 decode failure (invalid character, no HRP, or inconsistent case)"; + case INVALID_HRP: return "hrp differed from 'ms'"; + case INVALID_ID_LEN: return "seed ID was not 4 characters"; + case INVALID_ID_CHAR: return "seed ID used a non-bech32 character"; + case INVALID_LENGTH: return "invalid length"; + case INVALID_K: return "invalid threshold (k) value"; + case INVALID_SHARE_IDX: return "invalid share index"; + case TOO_FEW_SHARES: return "tried to derive a share but did not have enough input shares"; + case DUPLICATE_SHARE: return "tried to derive a share but two input shares had the same index"; + case MISMATCH_K: return "tried to derive a share but input shares had inconsistent threshold (k) values"; + case MISMATCH_ID: return "tried to derive a share but input shares had inconsistent seed IDs"; + case MISMATCH_LENGTH: return "tried to derive a share but input shares had inconsistent lengths"; + } + assert(0); +} + /** Encode a codex32 string. */ std::string Result::Encode() const { assert(IsValid()); diff --git a/src/codex32.h b/src/codex32.h index 6f2f9ba1bf..93d9f813f9 100644 --- a/src/codex32.h +++ b/src/codex32.h @@ -41,6 +41,8 @@ enum Error { MISMATCH_LENGTH, }; +std::string ErrorString(Error e); + class Result { public: