-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_bech32--2.0.sql
39 lines (32 loc) · 1.17 KB
/
pg_bech32--2.0.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
\echo Execute "CREATE EXTENSION pg_bech32;" to use this extension. \quit
--
-- Encoding/decoding functions
--
CREATE FUNCTION bech32_encode(hrp text, bit varying) RETURNS text
LANGUAGE c IMMUTABLE STRICT PARALLEL SAFE COST 10
AS 'MODULE_PATHNAME', 'pg_bech32_encode';
CREATE FUNCTION bech32_decode(text) RETURNS bit varying
LANGUAGE c IMMUTABLE STRICT PARALLEL SAFE COST 10
AS 'MODULE_PATHNAME', 'pg_bech32_decode';
CREATE FUNCTION bech32_hrp(text) RETURNS text
LANGUAGE c IMMUTABLE STRICT PARALLEL SAFE COST 10
AS 'MODULE_PATHNAME', 'pg_bech32_hrp';
CREATE OR REPLACE FUNCTION b32_decode(encodedstr text) RETURNS text
LANGUAGE plpgsql STABLE AS $$
DECLARE
bit_string bit varying;
BEGIN
SELECT INTO bit_string bech32_decode(encodedstr);
RETURN (
SELECT
CASE
WHEN LENGTH(bit_string) % 8 = 0 THEN ENCODE(SUBSTR(varbit_send(bit_string), 5)::bytea, 'hex')
ELSE LEFT(ENCODE(SUBSTR(varbit_send(bit_string), 5)::bytea, 'hex'), -2)
END
);
END;
$$;
CREATE OR REPLACE FUNCTION b32_encode(pre text, bytea text) RETURNS text
LANGUAGE SQL STABLE AS $$
SELECT bech32_encode(pre, RIGHT(bytea::text, -1)::varbit);
$$;