Skip to content

Commit 369d189

Browse files
committed
Minor cleanups
1 parent 30cde78 commit 369d189

File tree

8 files changed

+112
-88
lines changed

8 files changed

+112
-88
lines changed

contrib/bech32/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
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+
17
add_library(_bech32
28
include/bech32.h
39
src/bech32.cpp)

docs/en/sql-reference/functions/encoding-functions.md

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -968,29 +968,31 @@ Result:
968968
1 2
969969
```
970970

971-
## Bech32Encode {#bech32encode}
971+
## bech32Encode {#bech32encode}
972972

973973
Encodes a binary data string, along with a human-readable part (HRP), using the [Bech32 or Bech32m](https://en.bitcoin.it/wiki/Bech32) algorithms.
974974

975975
**Syntax**
976976

977977
```sql
978-
Bech32Encode(hrp, data[, witver])
978+
bech32Encode(hrp, data[, witver])
979979
```
980980

981981
**Parameters**
982982

983-
- `hrp` — String of 1 - 83 lowercase characters specifying the "human-readable part" of the code. Usually 'bc' or 'tb'. [String](../data-types/string.md), [FixedString](../data-types/fixedstring.md).
984-
- `data` — String of binary data to encode. [String](../data-types/string.md), [FixedString](../data-types/fixedstring.md).
985-
- `witver` (optional, default = 1): An [unsigned integer](../../sql-reference/data-types/int-uint.md) specifying the version of the algorithm to run. 0 for Bech32 and 1 or greater for Bech32m.
983+
- `hrp` — String of 1 - 83 lowercase characters specifying the "human-readable part" of the code. Usually 'bc' or 'tb'. [String](../data-types/string.md) or [FixedString](../data-types/fixedstring.md).
984+
- `data` — String of binary data to encode. [String](../data-types/string.md) or [FixedString](../data-types/fixedstring.md).
985+
- `witver` - Witness version. Optional, default = 1. An [UInt*](../data-types/int-uint.md) specifying the version of the algorithm to run. 0 for Bech32 and 1 or greater for Bech32m.
986986

987987
:::note
988-
When using the [FixedString](../data-types/fixedstring.md) data type, if a value does not fully fill the row it is padded with null characters. While the `Bech32Encode` function will handle this automatically for the hrp argument, for the data argument the values must not be padded. For this reason it is not recommended to use the [FixedString](../data-types/fixedstring.md) data type for your data values unless you are certain that they are all the same length and ensure that your [FixedString](../data-types/fixedstring.md) column is set to that length as well.
988+
When using the [FixedString](../data-types/fixedstring.md) data type, if a value does not fully fill the row it is padded with null characters.
989+
While the `bech32Encode` function will handle this automatically for the hrp argument, for the data argument the values must not be padded.
990+
For this reason it is not recommended to use the [FixedString](../data-types/fixedstring.md) data type for your data values unless you are certain that they are all the same length and ensure that your [FixedString](../data-types/fixedstring.md) column is set to that length as well.
989991
:::
990992

991993
**Returned value**
992994

993-
- A Bech32 address string, consisting of the human-readable part, a separator character which is always '1', and a data part. The length of the string will never exceed 90 characters. If the algorithm cannot generate a valid address from the input, it will return an empty string
995+
- A Bech32 address string, consisting of the human-readable part, a separator character which is always '1', and a data part. The length of the string will never exceed 90 characters. If the algorithm cannot generate a valid address from the input, it will return an empty string.
994996

995997
Type: [String](../data-types/string.md).
996998

@@ -1003,57 +1005,58 @@ Query:
10031005
```sql
10041006
SELECT bech32Encode('bc', unhex('751e76e8199196d454941c45d1b3a323f1433bd6'));
10051007
```
1008+
10061009
Result:
10071010

10081011
```response
10091012
bc1w508d6qejxtdg4y5r3zarvary0c5xw7k8zcwmq
10101013
```
10111014

1012-
**Example**
1013-
10141015
A witness version of 0 will result in a different address string.
10151016

10161017
Query:
10171018

10181019
```sql
10191020
SELECT bech32Encode('bc', unhex('751e76e8199196d454941c45d1b3a323f1433bd6'), 0);
10201021
```
1022+
10211023
Result:
10221024

10231025
```response
10241026
bc1w508d6qejxtdg4y5r3zarvary0c5xw7kj7gz7z
10251027
```
10261028

1027-
**Example**
1028-
10291029
While 'bc' (Mainnet) and 'tb' (Testnet) are the only allowed hrp values for the SegWit address format, Bech32 allows any hrp that satisfies the above requirements.
10301030

10311031
Query:
10321032

10331033
```sql
10341034
SELECT bech32Encode('abcdefg', unhex('751e76e8199196d454941c45d1b3a323f1433bd6'), 10);
10351035
```
1036+
10361037
Result:
10371038

10381039
```response
10391040
abcdefg1w508d6qejxtdg4y5r3zarvary0c5xw7k9rp8r4
10401041
```
10411042

1042-
## Bech32Decode {#bech32decode}
1043+
## bech32Decode {#bech32decode}
10431044

1044-
Decodes a Bech32 address string generated by either the Bech32 or Bech32m algorithms.
1045+
Decodes a Bech32 address string generated by either the bech32 or bech32m algorithms.
10451046

10461047
**Syntax**
10471048

10481049
```sql
1049-
Bech32Decode(address)
1050+
bech32Decode(address)
10501051
```
10511052

10521053
**Parameters**
10531054

1054-
- `address` — Bech32 string to decode. [String](../data-types/string.md), [FixedString](../data-types/fixedstring.md).
1055+
- `address` — Bech32 string to decode. [String](../data-types/string.md) or [FixedString](../data-types/fixedstring.md).
10551056

1057+
:::note
10561058
Unlike the encode function, `Bech32Decode` will automatically handle padded [FixedStrings](../data-types/fixedstring.md).
1059+
:::
10571060

10581061
**Returned value**
10591062

@@ -1066,21 +1069,21 @@ Type: ([String](../data-types/string.md), [String](../data-types/string.md)).
10661069
Query:
10671070

10681071
```sql
1069-
select tup.1 as hrp, hex(tup.2) as data from (select bech32Decode('bc1w508d6qejxtdg4y5r3zarvary0c5xw7kj7gz7z') as tup);
1072+
SELECT tup.1 AS hrp, hex(tup.2) AS data FROM (SELECT bech32Decode('bc1w508d6qejxtdg4y5r3zarvary0c5xw7kj7gz7z') AS tup);
10701073
```
1074+
10711075
Result:
10721076

10731077
```response
10741078
bc 751E76E8199196D454941C45D1B3A323F1433BD6
10751079
```
10761080

1077-
**Example**
1078-
10791081
Query:
10801082

10811083
```sql
1082-
select tup.1 as hrp, hex(tup.2) as data from (select bech32Decode('tb1w508d6qejxtdg4y5r3zarvary0c5xw7kzp034v') as tup);
1084+
SELECT tup.1 AS hrp, hex(tup.2) AS data FROM (SELECT bech32Decode('tb1w508d6qejxtdg4y5r3zarvary0c5xw7kzp034v') AS tup);
10831085
```
1086+
10841087
Result:
10851088

10861089
```response

src/Common/config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#cmakedefine01 USE_S2_GEOMETRY
3030
#cmakedefine01 USE_FASTOPS
3131
#cmakedefine01 USE_SQIDS
32+
#cmakedefine01 USE_BECH32
3233
#cmakedefine01 USE_IDNA
3334
#cmakedefine01 USE_NLP
3435
#cmakedefine01 USE_VECTORSCAN

src/Functions/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ list (APPEND PRIVATE_LIBS
6161
boost::filesystem
6262
divide_impl
6363
ch_contrib::xxHash
64-
ch_contrib::bech32
6564
)
6665

6766
if (TARGET OpenSSL::Crypto)
@@ -80,6 +79,10 @@ if (TARGET ch_contrib::llvm)
8079
list (APPEND PRIVATE_LIBS ch_contrib::llvm)
8180
endif ()
8281

82+
if (TARGET ch_contrib::bech32)
83+
list (APPEND PRIVATE_LIBS ch_contrib::bech32)
84+
endif()
85+
8386
if (TARGET ch_contrib::base64)
8487
list (APPEND PRIVATE_LIBS ch_contrib::base64)
8588
endif()

0 commit comments

Comments
 (0)