pg_cardano

Cardano相关工具包:加密函数,地址编解码,区块链处理

概览

扩展包名版本分类许可证语言
pg_cardano1.1.1FEATMITRust
ID扩展名BinLibLoadCreateTrustReloc模式
2920pg_cardano-
相关扩展age hll rum pg_graphql pg_jsonschema jsquery pg_hint_plan hypopg

PG18 fix by https://github.com/Vonng/pg_cardano

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY1.1.11817161514pg_cardano-
RPMPIGSTY1.1.11817161514pg_cardano_$v-
DEBPIGSTY1.1.11817161514postgresql-$v-pg-cardano-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
u22.x86_64
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
u22.aarch64
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
u24.x86_64
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
u24.aarch64
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1

构建

您可以使用 pig build 命令构建 pg_cardano 扩展的 RPM / DEB 包:

pig build pkg pg_cardano         # 构建 RPM / DEB 包

安装

您可以直接安装 pg_cardano 扩展包的预置二进制包,首先确保 PGDGPIGSTY 仓库已经添加并启用:

pig repo add pgsql -u          # 添加仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install pg_cardano;          # 当前活跃 PG 版本安装
pig ext install -y pg_cardano -v 18  # PG 18
pig ext install -y pg_cardano -v 17  # PG 17
pig ext install -y pg_cardano -v 16  # PG 16
pig ext install -y pg_cardano -v 15  # PG 15
pig ext install -y pg_cardano -v 14  # PG 14
dnf install -y pg_cardano_18       # PG 18
dnf install -y pg_cardano_17       # PG 17
dnf install -y pg_cardano_16       # PG 16
dnf install -y pg_cardano_15       # PG 15
dnf install -y pg_cardano_14       # PG 14
apt install -y postgresql-18-pg-cardano   # PG 18
apt install -y postgresql-17-pg-cardano   # PG 17
apt install -y postgresql-16-pg-cardano   # PG 16
apt install -y postgresql-15-pg-cardano   # PG 15
apt install -y postgresql-14-pg-cardano   # PG 14

创建扩展

CREATE EXTENSION pg_cardano;

用法

pg_cardano: PostgreSQL 的 Cardano 区块链数据类型和加密函数

pg_cardano 扩展为在 PostgreSQL 中处理 Cardano 区块链数据提供加密和实用函数,包括 Base58、Bech32、CBOR、Blake2b、Ed25519 和 Shelley 地址工具。

CREATE EXTENSION pg_cardano;

Base58 编码/解码

SELECT cardano.base58_encode('Cardano'::bytea);
-- '3Z6ioYHE3x'

SELECT cardano.base58_decode('3Z6ioYHE3x');
-- '\x43617264616e6f'

Bech32 编码/解码

SELECT cardano.bech32_encode('ada', 'is amazing'::bytea);
-- 'ada1d9ejqctdv9axjmn8dypl4d'

SELECT cardano.bech32_decode_prefix('ada1d9ejqctdv9axjmn8dypl4d');
-- 'ada'

SELECT cardano.bech32_decode_data('ada1d9ejqctdv9axjmn8dypl4d');
-- '\x697320616d617a696e67'

CBOR 编码/解码

简单 CBOR(轻量级,满足大多数 Cardano 场景):

SELECT cardano.cbor_decode_jsonb('\xa3636164616b...'::bytea);
SELECT cardano.cbor_encode_jsonb('{"ada": "is amazing!", "bytes": "\\xdeadbeef"}'::jsonb);

扩展 CBOR(完整支持,无限制):

SELECT cardano.cbor_decode_jsonb_ext('\xa3636164616b...'::bytea);
SELECT cardano.cbor_encode_jsonb_ext('{"type":"map","value":[...]}'::jsonb);

Blake2b 哈希

SELECT cardano.blake2b_hash('Cardano is amazing!'::bytea, 32);
-- '\x2244d5c9699fa93b...'

Ed25519 签名与验证

SELECT cardano.ed25519_sign_message(
    '\x43D68AEC...'::bytea,       -- 签名密钥
    'Cardano is amazing!'::bytea   -- 消息
);

SELECT cardano.ed25519_verify_signature(
    '\x432753BD...'::bytea,        -- 验证密钥
    'Cardano is amazing!'::bytea,  -- 消息
    '\x74265f96...'::bytea         -- 签名
);
-- true

dRep 视图 ID 构建器 (CIP-105/CIP-129)

SELECT cardano.tools_drep_id_encode_cip105('\x28111ae1...'::bytea, FALSE);
-- 'drep_vkh19qg34ctllr7lh48nnj4akfc978qzqzuwzkgsdu6r3zc42lnl6a0'

SELECT cardano.tools_drep_id_encode_cip129('\x28111ae1...'::bytea, TRUE);
-- 'drep1yv5pzxhp0lu0m7757ww2hke8qhcuqgqt3c2ezphngwytz4gj324g7'

Shelley 地址工具

-- 构建基础地址
SELECT cardano.tools_shelley_address_build(
    '\x7415251f...'::bytea, FALSE,  -- 支付凭证, 是否为脚本
    '\x7c3ae2f2...'::bytea, FALSE,  -- 权益凭证, 是否为脚本
    0                                -- 网络 ID
);

-- 提取支付/权益凭证
SELECT cardano.tools_shelley_addr_extract_payment_cred('addr_test1qp6p2fgl...');
SELECT cardano.tools_shelley_addr_extract_stake_cred('addr_test1qp6p2fgl...');

-- 获取地址类型
SELECT cardano.tools_shelley_addr_get_type('addr_test1vp6p2fgl...');
-- 'PMT_KEY:NONE'

资产名称转换

SELECT cardano.tools_read_asset_name('hello'::bytea);
-- 'hello'(有效 UTF-8 作为字符串返回)

SELECT cardano.tools_read_asset_name('\xdeadbeef'::bytea);
-- 'deadbeef'(非 UTF-8 以十六进制返回)

CIP-88 矿池密钥注册验证

SELECT cardano.tools_verify_cip88_pool_key_registration('\xa119...'::bytea);
-- true

最后修改 2026-03-14: update extension metadata (953cbd0)