ledger-core
Bech32.h
1 /*
2  *
3  * Bech32
4  *
5  * Created by El Khalil Bellakrid on 13/02/2019.
6  *
7  * The MIT License (MIT)
8  *
9  * Copyright (c) 2019 Ledger
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights
14  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  * copies of the Software, and to permit persons to whom the Software is
16  * furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in all
19  * copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  *
29  */
30 
31 
32 #ifndef LEDGER_CORE_BECH32_H
33 #define LEDGER_CORE_BECH32_H
34 
35 // References:
36 // BIP173: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
37 // Implementation: https://github.com/sipa/bech32/tree/master/ref/c%2B%2B
38 
39 #include <vector>
40 #include <string>
41 #include "Bech32Parameters.h"
42 
43 // HRP refers to Human Readable Part
44 namespace ledger {
45  namespace core {
46  class Bech32 {
47  public:
48  // Find the polynomial with value coefficients mod the generator as 64-bit.
49  virtual uint64_t polymod(const std::vector<uint8_t>& values) = 0;
50 
51  // Expand a HRP for use in checksum computation.
52  virtual std::vector<uint8_t> expandHrp(const std::string& hrp) = 0;
53 
54  bool verifyChecksum(const std::vector<uint8_t>& values);
55 
56  std::vector<uint8_t> createChecksum(const std::vector<uint8_t>& values);
57 
58  virtual std::string encode(const std::vector<uint8_t>& hash,
59  const std::vector<uint8_t>& version) = 0;
60 
61  // Decode from bech32 address
62  // @return pair<hrp, hash>
63  std::pair<std::string, std::vector<uint8_t>>
64  decodeBech32(const std::string& str);
65  // @return tuple<witnessVersion, hash>
66  virtual std::pair<std::vector<uint8_t>, std::vector<uint8_t>>
67  decode(const std::string& str) = 0;
68 
69  static unsigned char toLowerCase(unsigned char c);
70 
71  static bool convertBits(const std::vector<uint8_t>& in,
72  int fromBits,
73  int toBits,
74  bool pad,
75  std::vector<uint8_t>& out);
76 
77  Bech32Parameters::Bech32Struct getBech32Params() {
78  return _bech32Params;
79  }
80 
81  protected:
82  std::string encodeBech32(const std::vector<uint8_t>& values);
83  Bech32Parameters::Bech32Struct _bech32Params;
84  };
85  }
86 }
87 #endif //LEDGER_CORE_BECH32_H
Definition: Bech32.h:46
Definition: Account.cpp:8
Definition: Bech32Parameters.h:50