Skip to content

Commit

Permalink
CIP-0129/CIP-0105 backward compatibility (#4894)
Browse files Browse the repository at this point in the history
<!--
Detail in a few bullet points the work accomplished in this PR.

Before you submit, don't forget to:

* Make sure the GitHub PR fields are correct:
   ✓ Set a good Title for your PR.
   ✓ Assign yourself to the PR.
   ✓ Assign one or more reviewer(s).
   ✓ Link to a Jira issue, and/or other GitHub issues or PRs.
   ✓ In the PR description delete any empty sections
     and all text commented in <!--, so that this text does not appear
     in merge commit messages.

* Don't waste reviewers' time:
   ✓ If it's a draft, select the Create Draft PR option.
✓ Self-review your changes to make sure nothing unexpected slipped
through.

* Try to make your intent clear:
   ✓ Write a good Description that explains what this PR is meant to do.
   ✓ Jira will detect and link to this PR once created, but you can also
     link this PR in the description of the corresponding Jira ticket.
   ✓ Highlight what Testing you have done.
   ✓ Acknowledge any changes required to the Documentation.
-->

- [x] adjust swagger
- [x] Adding backward compability to toText/fromText
- [x] Fixing Malformed
- [x] Fixing generator
- [x] Regenerate  golden

### Comments

<!-- Additional comments, links, or screenshots to attach, if any. -->

### Issue Number
#4888
<!-- Reference the Jira/GitHub issue that this PR relates to, and which
requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->
  • Loading branch information
paweljakubas authored Dec 20, 2024
2 parents 88b0c51 + 4345d9d commit 630ef00
Show file tree
Hide file tree
Showing 7 changed files with 12,327 additions and 9,610 deletions.
36 changes: 7 additions & 29 deletions lib/api/src/Cardano/Wallet/Api/Types/Certificate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,16 @@ import Cardano.Wallet.Primitive.Types
)
import Cardano.Wallet.Primitive.Types.DRep
( DRep (..)
, decodeDRepIDBech32
, encodeDRepIDBech32
)
import Cardano.Wallet.Util
( ShowFmt (..)
)
import Control.Applicative
( (<|>)
)
import Control.DeepSeq
( NFData
)
import Control.Monad
( (>=>)
)
import Data.Aeson.Types
( FromJSON (parseJSON)
, KeyValue ((.=))
Expand All @@ -93,7 +91,6 @@ import Data.Aeson.Types
, genericParseJSON
, genericToJSON
, withObject
, withText
, (.:)
)
import Data.Bifunctor
Expand All @@ -109,7 +106,8 @@ import Data.Quantity
( Quantity (..)
)
import Data.Text.Class
( TextDecodingError (..)
( FromText (..)
, ToText (..)
)
import GHC.Generics
( Generic
Expand Down Expand Up @@ -338,26 +336,6 @@ mkApiAnyCertificate acct' acctPath' = \case
JoinPoolCastVoteExternal (ApiRewardAccount rewardKey) (ApiT poolId') (ApiT vote')

instance ToJSON (ApiT DRep) where
toJSON (ApiT Abstain) = "abstain"
toJSON (ApiT NoConfidence) = "no_confidence"
toJSON (ApiT (FromDRepID drepid)) =
String $ encodeDRepIDBech32 drepid
toJSON = toJSON . toText . getApiT
instance FromJSON (ApiT DRep) where
parseJSON t =
parseAbstain t <|> parseNoConfidence t <|> parseDrepID t
where
parseDrepID = withText "DRepID" $ \txt ->
case decodeDRepIDBech32 txt of
Left (TextDecodingError err) -> fail err
Right drepid ->
pure $ ApiT $ FromDRepID drepid
parseAbstain = withText "Abstain" $ \txt ->
if txt == "abstain" then
pure $ ApiT Abstain
else
fail "'abstain' is expected."
parseNoConfidence = withText "NoConfidence" $ \txt ->
if txt == "no_confidence" then
pure $ ApiT NoConfidence
else
fail "'no_confidence' is expected."
parseJSON = parseJSON >=> eitherToParser . bimap ShowFmt ApiT . fromText
36 changes: 21 additions & 15 deletions lib/primitive/lib/Cardano/Wallet/Primitive/Types/DRep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ encodeDRepIDBech32 drepid =
decodeDRepIDBech32 :: Text -> Either TextDecodingError DRepID
decodeDRepIDBech32 t =
case fmap Bech32.dataPartToBytes <$> Bech32.decodeLenient t of
Left _ -> Left textDecodingError
Left _ ->
Left textDecodingError
Right (hrp', Just bytes)
| hrp' == hrp ->
| hrp' == hrpDrep && BS.length bytes == 29 ->
let (fstByte, payload) = first BS.head $ BS.splitAt 1 bytes
in case fstByte of
0b00100010 ->
Expand All @@ -109,20 +110,28 @@ decodeDRepIDBech32 t =
Right $ DRepFromScriptHash (DRepScriptHash payload)
_ ->
Left textFirstByteError
Right _ -> Left textDecodingError
| hrp' == hrpDrep && BS.length bytes == 28 ->
Right $ DRepFromKeyHash (DRepKeyHash bytes)
| hrp' == hrpDrepVKH && BS.length bytes == 28 ->
Right $ DRepFromKeyHash (DRepKeyHash bytes)
| hrp' == hrpDrepScript && BS.length bytes == 28 ->
Right $ DRepFromScriptHash (DRepScriptHash bytes)
| otherwise ->
Left textDecodingError
_ ->
Left textDecodingError
where
textDecodingError = TextDecodingError $ unwords
[ "Invalid DRep key hash: expecting a Bech32 encoded value"
, "with human readable part of 'drep'."
[ "Invalid DRep key/script hash: expecting a Bech32 encoded value"
, "with human readable part of 'drep', 'drep_vkh' or 'drep_script'."
]
textFirstByteError = TextDecodingError $ unwords
[ "Invalid DRep metadata: expecting a byte '00100010' value for key hash or"
, "a byte '0b00100011' value for script hash."
[ "Invalid DRep credential: expecting a first byte '0b00100010' value for key hash or"
, "a first byte '0b00100011' value for script hash."
]
hrp = [Bech32.humanReadablePart|drep|]

instance Buildable DRepID where
build = build . encodeDRepIDBech32
hrpDrep = [Bech32.humanReadablePart|drep|]
hrpDrepVKH = [Bech32.humanReadablePart|drep_vkh|]
hrpDrepScript = [Bech32.humanReadablePart|drep_script|]

-- | A decentralized representation ('DRep') will
-- vote on behalf of the stake delegated to it.
Expand All @@ -145,7 +154,4 @@ instance FromText DRep where
_ -> second FromDRepID (decodeDRepIDBech32 txt)

instance Buildable DRep where
build = \case
Abstain -> "abstain"
NoConfidence -> "no_confidence"
FromDRepID drep -> "delegating voting to " <> build drep
build = build . toText
Loading

0 comments on commit 630ef00

Please sign in to comment.