Skip to content

Commit

Permalink
Add serialiseToBech32CIP129
Browse files Browse the repository at this point in the history
  • Loading branch information
smelc committed Feb 7, 2025
1 parent 96dc687 commit 35821ce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cardano-api/internal/Cardano/Api/SerialiseBech32.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
module Cardano.Api.SerialiseBech32
( SerialiseAsBech32 (..)
, serialiseToBech32
, serialiseToBech32CIP129
, Bech32DecodeError (..)
, deserialiseFromBech32
, deserialiseAnyOfFromBech32
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ module Cardano.Api
-- ** Bech32
, SerialiseAsBech32
, serialiseToBech32
, serialiseToBech32CIP129
, deserialiseFromBech32
, deserialiseAnyOfFromBech32
, Bech32DecodeError (..)
Expand Down

0 comments on commit 35821ce

Please sign in to comment.