From 80171a93942dee9b9fe43528833c6d4189a57e93 Mon Sep 17 00:00:00 2001 From: Ola Date: Sat, 24 Aug 2024 19:13:44 +0200 Subject: [PATCH] CIP-129 wip --- files/grest/rpc/03_utilities/cip129.sql | 35 +++++++++++++++---- files/grest/rpc/governance/committee_info.sql | 19 +++------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/files/grest/rpc/03_utilities/cip129.sql b/files/grest/rpc/03_utilities/cip129.sql index 43043e8c..92679c38 100644 --- a/files/grest/rpc/03_utilities/cip129.sql +++ b/files/grest/rpc/03_utilities/cip129.sql @@ -27,9 +27,9 @@ LANGUAGE plpgsql STABLE AS $$ BEGIN IF LENGTH(_cc_hot) = 60 THEN - RETURN substring(b32_decode(_cc_hot) from 2); + RETURN DECODE(SUBSTRING(b32_decode(_cc_hot) from 2), 'hex'); ELSE - RETURN b32_decode(_cc_hot); + RETURN DECODE(b32_decode(_cc_hot), 'hex'); END IF; END; $$; @@ -53,9 +53,9 @@ LANGUAGE plpgsql STABLE AS $$ BEGIN IF LENGTH(_cc_cold) = 61 THEN - RETURN substring(b32_decode(_cc_cold) from 2); + RETURN DECODE(SUBSTRING(b32_decode(_cc_cold) from 2), 'hex'); ELSE - RETURN b32_decode(_cc_cold); + RETURN DECODE(b32_decode(_cc_cold), 'hex'); END IF; END; $$; @@ -79,9 +79,9 @@ LANGUAGE plpgsql STABLE AS $$ BEGIN IF LENGTH(_drep_id) = 58 THEN - RETURN substring(b32_decode(_drep_id) from 2); + RETURN DECODE(SUBSTRING(b32_decode(_drep_id) from 2), 'hex'); ELSE - RETURN b32_decode(_drep_id); + RETURN DECODE(b32_decode(_drep_id), 'hex'); END IF; END; $$; @@ -99,9 +99,32 @@ BEGIN END; $$; +CREATE OR REPLACE FUNCTION grest.cip129_from_gov_action_id(_proposal_id text) +RETURNS text[] +LANGUAGE plpgsql STABLE +AS $$ +DECLARE + proposal_id_hex text; +BEGIN + SELECT INTO proposal_id_hex b32_decode(_proposal_id); + RETURN ARRAY[LEFT(proposal_id_hex, 64), ('x' || RIGHT(proposal_id_hex, -64))::bit(8)::int::text]; +END; +$$; + +CREATE OR REPLACE FUNCTION grest.cip129_to_gov_action_id(_tx_hash bytea, _index bigint) +RETURNS text +LANGUAGE plpgsql STABLE +AS $$ +BEGIN + RETURN b32_encode('gov_action', (_tx_hash || DECODE(LPAD(TO_HEX(_index), 2, '0'), 'hex'))::text); +END; +$$; + COMMENT ON FUNCTION grest.cip129_cc_hot_to_hex IS 'Returns binary hex from Constitutional Committee Hot Credential ID in old or new (CIP-129) format'; -- noqa: LT01 COMMENT ON FUNCTION grest.cip129_hex_to_cc_hot IS 'Returns Constitutional Committee Hot Credential ID in CIP-129 format from raw binary hex'; -- noqa: LT01 COMMENT ON FUNCTION grest.cip129_cc_cold_to_hex IS 'Returns binary hex from Constitutional Committee Cold Credential ID in old or new (CIP-129) format'; -- noqa: LT01 COMMENT ON FUNCTION grest.cip129_hex_to_cc_cold IS 'Returns Constitutional Committee Cold Credential ID in CIP-129 format from raw binary hex'; -- noqa: LT01 COMMENT ON FUNCTION grest.cip129_drep_id_to_hex IS 'Returns binary hex from DRep Credential ID in old or new (CIP-129) format'; -- noqa: LT01 COMMENT ON FUNCTION grest.cip129_hex_to_drep_id IS 'Returns DRep Credential ID in CIP-129 format from raw binary hex'; -- noqa: LT01 +COMMENT ON FUNCTION grest.cip129_from_gov_action_id IS 'Returns string array containing transaction hash and certificate index from Governance Action Proposal ID in CIP-129 format'; -- noqa: LT01 +COMMENT ON FUNCTION grest.cip129_to_gov_action_id IS 'Returns Governance Action Proposal ID in CIP-129 format from transaction hash appended by index of certificate within the transaction'; -- noqa: LT01 diff --git a/files/grest/rpc/governance/committee_info.sql b/files/grest/rpc/governance/committee_info.sql index a39c810f..1e01e375 100644 --- a/files/grest/rpc/governance/committee_info.sql +++ b/files/grest/rpc/governance/committee_info.sql @@ -1,7 +1,6 @@ CREATE OR REPLACE FUNCTION grest.committee_info() RETURNS TABLE ( - proposal_tx_hash text, - proposal_index bigint, + proposal_id text, quorum_numerator bigint, quorum_denominator bigint, members jsonb @@ -35,20 +34,12 @@ BEGIN CASE WHEN c.gov_action_proposal_id IS NULL THEN NULL ELSE ( - SELECT ENCODE(tx.hash, 'hex') + SELECT grest.cip129_to_gov_action_id(tx.hash, gap.index) FROM gov_action_proposal AS gap INNER JOIN tx on gap.tx_id = tx.id WHERE gap.id = c.gov_action_proposal_id ) END, - CASE - WHEN c.gov_action_proposal_id IS NULL THEN NULL - ELSE ( - SELECT index - FROM gov_action_proposal AS gap - WHERE gap.id = c.gov_action_proposal_id - ) - END, c.quorum_numerator, c.quorum_denominator, JSONB_AGG( @@ -65,10 +56,8 @@ BEGIN ELSE 'authorized' END, - 'cc_cold_hex', ENCODE(ch_cold.raw, 'hex'), - 'cc_cold_has_script', ch_cold.has_script, - 'cc_hot_hex', CASE WHEN hot_key.raw IS NULL THEN NULL ELSE ENCODE(hot_key.raw, 'hex') END, - 'cc_hot_has_script', CASE WHEN hot_key.has_script IS NULL THEN NULL ELSE hot_key.has_script END, + 'cc_cold_id', (SELECT grest.cip129_hex_to_cc_cold(ch_cold.raw, ch_cold.has_script)), + 'cc_hot_id', CASE WHEN hot_key.raw IS NULL THEN NULL ELSE (SELECT grest.cip129_hex_to_cc_hot(hot_key.raw, hot_key.has_script)) END, 'expiration_epoch', cm.expiration_epoch ) ) AS members