From 35821ce9ee82af45b0dbd6ac96f5bda0ae14539d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hurlin?= Date: Fri, 7 Feb 2025 15:57:00 +0100 Subject: [PATCH] Add serialiseToBech32CIP129 --- .../internal/Cardano/Api/SerialiseBech32.hs | 22 +++++++++++++++++++ cardano-api/src/Cardano/Api.hs | 1 + 2 files changed, 23 insertions(+) diff --git a/cardano-api/internal/Cardano/Api/SerialiseBech32.hs b/cardano-api/internal/Cardano/Api/SerialiseBech32.hs index 16bcb74aa7..2754ee14ad 100644 --- a/cardano-api/internal/Cardano/Api/SerialiseBech32.hs +++ b/cardano-api/internal/Cardano/Api/SerialiseBech32.hs @@ -7,6 +7,7 @@ module Cardano.Api.SerialiseBech32 ( SerialiseAsBech32 (..) , serialiseToBech32 + , serialiseToBech32CIP129 , Bech32DecodeError (..) , deserialiseFromBech32 , deserialiseAnyOfFromBech32 @@ -52,6 +53,27 @@ serialiseToBech32 a = ++ ", " ++ show err +-- | Implements CIP129 format, which kind of sneaks in an additional +-- prefix in between the bech prefix and the actual data. +-- The official CIP is here: https://github.com/cardano-foundation/CIPs/blob/master/CIP-0129/README.md +-- but the CLI issue is actually more detailed: +-- https://github.com/IntersectMBO/cardano-cli/issues/883 +serialiseToBech32CIP129 :: SerialiseAsBech32 a => ByteString -> a -> Text +serialiseToBech32CIP129 dataPrefix a = + Bech32.encodeLenient + humanReadablePart + (Bech32.dataPartFromBytes (dataPrefix <> serialiseToRawBytes a)) + where + humanReadablePart = + case Bech32.humanReadablePartFromText (bech32PrefixFor a) of + Right p -> p + Left err -> + error $ + "serialiseToBech32: invalid prefix " + ++ show (bech32PrefixFor a) + ++ ", " + ++ show err + deserialiseFromBech32 :: SerialiseAsBech32 a => AsType a -> Text -> Either Bech32DecodeError a diff --git a/cardano-api/src/Cardano/Api.hs b/cardano-api/src/Cardano/Api.hs index dfae71c4ca..54d73de779 100644 --- a/cardano-api/src/Cardano/Api.hs +++ b/cardano-api/src/Cardano/Api.hs @@ -680,6 +680,7 @@ module Cardano.Api -- ** Bech32 , SerialiseAsBech32 , serialiseToBech32 + , serialiseToBech32CIP129 , deserialiseFromBech32 , deserialiseAnyOfFromBech32 , Bech32DecodeError (..)