Skip to content

Commit 7cfa1f9

Browse files
authored
Merge pull request ClickHouse#80239 from glarik/add_bech32_enc_dec
Add bech32 encoding and decoding funcs
2 parents 8233289 + b422228 commit 7cfa1f9

File tree

16 files changed

+1188
-5
lines changed

16 files changed

+1188
-5
lines changed

ci/jobs/scripts/check_style/aspell-ignore/en/aspell-dict.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
personal_ws-1.1 en 3238
1+
personal_ws-1.1 en 3283
22
AArch
33
ACLs
44
ALTERs
@@ -82,6 +82,7 @@ BackupsIOThreads
8282
BackupsIOThreadsActive
8383
BackupsThreads
8484
BackupsThreadsActive
85+
Bech
8586
BestEffort
8687
BestEffortOrNull
8788
BestEffortOrZero
@@ -334,6 +335,7 @@ FilesystemMainPathTotalINodes
334335
FilesystemMainPathUsedBytes
335336
FilesystemMainPathUsedINodes
336337
FixedString
338+
FixedStrings
337339
FlameGraph
338340
Flink
339341
ForEach
@@ -597,6 +599,7 @@ MSan
597599
MVCC
598600
MacBook
599601
MacOS
602+
Mainnet
600603
MapState
601604
MarkCacheBytes
602605
MarkCacheFiles
@@ -984,6 +987,7 @@ Schemas
984987
Schwartzian
985988
SeasClick
986989
SeekTable
990+
SegWit
987991
SelfManaged
988992
Sematext
989993
SendExternalTables
@@ -1088,6 +1092,7 @@ TemporaryFilesForJoin
10881092
TemporaryFilesForSort
10891093
TemporaryFilesUnknown
10901094
Testflows
1095+
Testnet
10911096
Tgz
10921097
Theil's
10931098
Theils
@@ -1387,6 +1392,7 @@ backupview
13871392
balancer
13881393
basename
13891394
bcrypt
1395+
bech
13901396
benchmarked
13911397
benchmarking
13921398
bfloat
@@ -1960,8 +1966,8 @@ grpc
19601966
grpcio
19611967
gtest
19621968
gtid
1963-
gzip
19641969
gunzip
1970+
gzip
19651971
gzipped
19661972
hadoop
19671973
halfMD
@@ -2347,9 +2353,9 @@ namespaces
23472353
natively
23482354
nats
23492355
ncat
2350-
netcat
23512356
ness
23522357
nestjs
2358+
netcat
23532359
netloc
23542360
newjson
23552361
ngram

contrib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ add_contrib (jwt-cpp-cmake jwt-cpp)
247247

248248
add_contrib (sha3iuf-cmake SHA3IUF)
249249

250+
add_contrib (bech32)
250251

251252
# Put all targets defined here and in subdirectories under "contrib/<immediate-subdir>" folders in GUI-based IDEs.
252253
# Some of third-party projects may override CMAKE_FOLDER or FOLDER property of their targets, so they would not appear

contrib/bech32/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
option(ENABLE_BECH32 "Enable bech32 support" ${ENABLE_LIBRARIES})
2+
if ((NOT ENABLE_BECH32))
3+
message (STATUS "Not using bech32")
4+
return()
5+
endif()
6+
7+
add_library(_bech32
8+
include/bech32.h
9+
src/bech32.cpp)
10+
11+
target_include_directories(_bech32 PUBLIC include)
12+
target_include_directories(_bech32 PRIVATE src)
13+
14+
add_library(ch_contrib::bech32 ALIAS _bech32)

contrib/bech32/LICENSE-MIT

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2017, 2021 Pieter Wuille
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

contrib/bech32/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Code copied from the [bech32](https://github.com/sipa/bech32) repository. It contains reference implementations
2+
of the Bech32 and Bech32m algorithms, along with an implementation of the SegWit algorithm which is a
3+
specialization of Bech32(m) for Bitcoin. The code used in ClickHouse is only the Bech32 algorithms, not SegWit.
4+
See [this link](https://en.bitcoin.it/wiki/Bech32) for more info on the algorithms and their applications.
5+
6+
Modifications to the code:
7+
- typedef changed to using as per compiler warnings
8+
- Problematic asserts were changed to if statement + empty return
9+
- convertbits function was copied directly into src/Functions/FunctionsBech32Representation.cpp

contrib/bech32/include/bech32.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#pragma once
2+
/* Copyright (c) 2017, 2021 Pieter Wuille
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
23+
#include <cstdint>
24+
#include <string>
25+
#include <vector>
26+
27+
namespace bech32
28+
{
29+
30+
enum class Encoding
31+
{
32+
INVALID,
33+
34+
BECH32, //! Bech32 encoding as defined in BIP173
35+
BECH32M, //! Bech32m encoding as defined in BIP350
36+
};
37+
38+
/** Encode a Bech32 or Bech32m string. If hrp contains uppercase characters, this will cause an
39+
* assertion error. Encoding must be one of BECH32 or BECH32M. */
40+
std::string encode(const std::string & hrp, const std::vector<uint8_t> & values, Encoding encoding);
41+
42+
/** A type for the result of decoding. */
43+
struct DecodeResult
44+
{
45+
Encoding encoding; //!< What encoding was detected in the result; Encoding::INVALID if failed.
46+
std::string hrp; //!< The human readable part
47+
std::vector<uint8_t> data; //!< The payload (excluding checksum)
48+
49+
DecodeResult()
50+
: encoding(Encoding::INVALID)
51+
{
52+
}
53+
DecodeResult(Encoding enc, std::string && h, std::vector<uint8_t> && d)
54+
: encoding(enc)
55+
, hrp(std::move(h))
56+
, data(std::move(d))
57+
{
58+
}
59+
};
60+
61+
/** Decode a Bech32 or Bech32m string. */
62+
DecodeResult decode(const std::string & str);
63+
64+
}

0 commit comments

Comments
 (0)