Skip to content

Commit

Permalink
CIP-129 wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Scitz0 committed Aug 24, 2024
1 parent ce60276 commit 80171a9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
35 changes: 29 additions & 6 deletions files/grest/rpc/03_utilities/cip129.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
$$;
Expand All @@ -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;
$$;
Expand All @@ -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;
$$;
Expand All @@ -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
19 changes: 4 additions & 15 deletions files/grest/rpc/governance/committee_info.sql
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit 80171a9

Please sign in to comment.