99
1010module Kadena.SigningTypes where
1111
12+ import Data.Coerce
1213import Control.Lens hiding ((.=) )
1314import Control.Monad
1415import qualified Data.Aeson as A
16+ import qualified Pact.JSON.Encode as J
1517import Data.Aeson.Types
1618import qualified Data.ByteString.Lazy as BSL
1719import Data.Char as Char
@@ -23,16 +25,9 @@ import qualified Data.Text as T
2325import qualified Data.Text.Encoding as T
2426import GHC.Generics
2527
26- import Pact.ApiReq
27- import qualified Pact.JSON.Encode as J
28- import Pact.Types.Capability (SigCapability (.. ))
29- import Pact.Types.ChainMeta
30- import Pact.Types.Command
31- import Pact.Types.Hash
32- import Pact.Parse
33- import Pact.Types.Runtime (GasLimit (.. ), ChainId , NetworkId , PublicKeyText )
34- -- TODO: Rip out sig data dependency
35- import Pact.Types.SigData (PublicKeyHex (.. ))
28+ import Pact.Core.Command.SigData
29+ import Pact.Core.Command.Types
30+ import Pact.Core.Command.Client
3631
3732-- The spec calls this `Signer` but it clashes too much with Pact`s `Signer` type
3833data CSDSigner = CSDSigner
@@ -88,15 +83,15 @@ instance FromJSON CommandSigData where
8883
8984--------------------------------------------------------------------------------
9085data SigningOutcome =
91- SO_Success PactHash
86+ SO_Success Hash
9287 | SO_Failure Text
9388 | SO_NoSig
9489 deriving (Eq ,Ord ,Show ,Generic )
9590
9691instance ToJSON SigningOutcome where
9792 toJSON a = case a of
9893 SO_Success h -> object [" result" .= (" success" :: Text ), " hash" .= hashTxt ]
99- where hashTxt = hashToText $ toUntypedHash h
94+ where hashTxt = T. decodeUtf8 $ BSL. toStrict $ J. encode h
10095 SO_Failure msg -> object [" result" .= (" failure" :: Text ), " msg" .= msg ]
10196 SO_NoSig -> object [" result" .= (" noSig" :: Text )]
10297
@@ -155,12 +150,14 @@ commandSigDataToCommand = fmap fst . commandSigDataToParsedCommand
155150
156151commandSigDataToParsedCommand :: CommandSigData -> Either String (Command Text , Payload PublicMeta ParsedCode )
157152commandSigDataToParsedCommand (CommandSigData (SignatureList sigList) c) = do
158- payload :: Payload PublicMeta ParsedCode <- traverse parsePact =<< A. eitherDecodeStrict' (T. encodeUtf8 c)
153+ payload :: Payload ( StableEncoding PublicMeta ) ParsedCode <- traverse parsePact' =<< A. eitherDecodeStrict' (T. encodeUtf8 c)
159154 let sigMap = M. fromList $ (\ (CSDSigner k v) -> (k, v)) <$> sigList
160155 -- It is ok to use a map here because we're iterating over the signers list and only using the map for lookup.
161156 sigs = catMaybes $ map (\ signer -> join $ M. lookup (PublicKeyHex $ _siPubKey signer) sigMap) $ _pSigners payload
162157 h = hash (T. encodeUtf8 c)
163- pure (Command c sigs h, payload)
158+ pure (Command c sigs h, coerce payload)
159+ where
160+ parsePact' = either (Left . show ) Right . parsePact
164161
165162--------------------------------------------------------------------------------
166163newtype AccountName = AccountName
@@ -217,10 +214,22 @@ compactEncoding = defaultOptions
217214-- If these orphans conflict with future ToJSON instances, we can remove them.
218215
219216instance ToJSON SigCapability where toJSON = J. toJsonViaEncode
220- instance ToJSON PublicKeyText where toJSON = J. toJsonViaEncode
221- instance ToJSON TTLSeconds where toJSON = J. toJsonViaEncode
222- instance ToJSON GasLimit where toJSON = J. toJsonViaEncode
223- instance ToJSON ChainId where toJSON = J. toJsonViaEncode
217+
218+ instance ToJSON PublicKeyText where toJSON = coerce . J. toJsonViaEncode . StableEncoding
219+ instance FromJSON PublicKeyText where parseJSON = coerce <$> parseJSON @ (StableEncoding PublicKeyText )
220+
221+ instance ToJSON TTLSeconds where toJSON = coerce . J. toJsonViaEncode . StableEncoding
222+ instance FromJSON TTLSeconds where parseJSON = coerce <$> parseJSON @ (StableEncoding TTLSeconds )
223+
224+ instance ToJSON GasLimit where toJSON = coerce . J. toJsonViaEncode . StableEncoding
225+ instance FromJSON GasLimit where parseJSON = coerce <$> parseJSON @ (StableEncoding GasLimit )
226+
227+ instance ToJSON ChainId where toJSON = coerce . J. toJsonViaEncode . StableEncoding
228+ instance FromJSON ChainId where parseJSON = coerce <$> parseJSON @ (StableEncoding ChainId )
229+
230+ instance ToJSON PactValue where toJSON = coerce . J. toJsonViaEncode . StableEncoding
231+ instance FromJSON PactValue where parseJSON = coerce <$> parseJSON @ (StableEncoding PactValue )
232+
224233instance ToJSON NetworkId where toJSON = J. toJsonViaEncode
225234instance J. Encode a => ToJSON (Command a ) where toJSON = J. toJsonViaEncode
226235instance ToJSON ApiSigner where toJSON = J. toJsonViaEncode
0 commit comments