Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new interim governance commands: {create, answer, verify}-poll #5050

Closed
43 changes: 43 additions & 0 deletions cardano-api/src/Cardano/Api/TxMetadata.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ module Cardano.Api.TxMetadata (
-- * Constructing metadata
TxMetadataValue(..),
makeTransactionMetadata,
metaTextChunks,
metaBytesChunks,

-- * Validating metadata
validateTxMetadata,
TxMetadataRangeError (..),
txMetadataTextStringMaxByteLength,
txMetadataByteStringMaxLength,
KtorZ marked this conversation as resolved.
Show resolved Hide resolved

-- * Conversion to\/from JSON
TxMetadataJsonSchema (..),
Expand Down Expand Up @@ -125,6 +129,25 @@ instance SerialiseAsCBOR TxMetadata where
makeTransactionMetadata :: Map Word64 TxMetadataValue -> TxMetadata
makeTransactionMetadata = TxMetadata

-- | Create a 'TxMetadataValue' from a 'Text' as a list of chunks of an
-- acceptable size.
metaTextChunks :: Text -> TxMetadataValue
metaTextChunks =
TxMetaList . chunks
txMetadataTextStringMaxByteLength
TxMetaText
(BS.length . Text.encodeUtf8)
Text.splitAt

-- | Create a 'TxMetadataValue' from a 'ByteString' as a list of chunks of an
-- accaptable size.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo accaptable

metaBytesChunks :: ByteString -> TxMetadataValue
metaBytesChunks =
TxMetaList . chunks
txMetadataByteStringMaxLength
TxMetaBytes
BS.length
BS.splitAt
KtorZ marked this conversation as resolved.
Show resolved Hide resolved

-- ----------------------------------------------------------------------------
-- Internal conversion functions
Expand Down Expand Up @@ -158,6 +181,26 @@ fromShelleyMetadatum (Shelley.Map xs) = TxMetaMap
fromShelleyMetadatum v)
| (k,v) <- xs ]

-- | Transform a string-like structure into chunks with a maximum size; Chunks
-- are filled from left to right.
chunks
:: Int
-- ^ Chunk max size (inclusive)
-> (str -> chunk)
-- ^ Hoisting
-> (str -> Int)
-- ^ Measuring
-> (Int -> str -> (str, str))
-- ^ Splitting
-> str
-- ^ String
-> [chunk]
chunks maxLength strHoist strLength strSplitAt str
| strLength str > maxLength =
let (h, t) = strSplitAt maxLength str
in strHoist h : chunks maxLength strHoist strLength strSplitAt t
| otherwise =
[strHoist str]

-- ----------------------------------------------------------------------------
-- Validate tx metadata
Expand Down