Skip to content

Commit

Permalink
Back out SigCapability rename to UserCapability
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundnoble committed Dec 11, 2023
1 parent 785fc1b commit 0371f46
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 42 deletions.
18 changes: 9 additions & 9 deletions src/Pact/ApiReq.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ data ApiKeyPair = ApiKeyPair {
_akpPublic :: Maybe PublicKeyBS,
_akpAddress :: Maybe Text,
_akpScheme :: Maybe PPKScheme,
_akpCaps :: Maybe [UserCapability]
_akpCaps :: Maybe [SigCapability]
} deriving (Eq, Show, Generic)

instance FromJSON ApiKeyPair where parseJSON = lensyParseJSON 4
Expand Down Expand Up @@ -124,7 +124,7 @@ data ApiSigner = ApiSigner {
_asPublic :: Text,
_asAddress :: Maybe Text,
_asScheme :: Maybe PPKScheme,
_asCaps :: Maybe [UserCapability]
_asCaps :: Maybe [SigCapability]
} deriving (Eq, Show, Generic)

instance FromJSON ApiSigner where parseJSON = lensyParseJSON 3
Expand Down Expand Up @@ -478,7 +478,7 @@ signCmd keyFiles bs = do
withKeypairsOrSigner
:: Bool
-> ApiReq
-> ([(DynKeyPair, [UserCapability])] -> IO a)
-> ([(DynKeyPair, [SigCapability])] -> IO a)
-> ([Signer] -> IO a)
-> IO a
withKeypairsOrSigner unsignedReq ApiReq{..} keypairAction signerAction =
Expand Down Expand Up @@ -546,7 +546,7 @@ mkExec
-- ^ optional environment data
-> PublicMeta
-- ^ public metadata
-> [(DynKeyPair, [UserCapability])]
-> [(DynKeyPair, [SigCapability])]
-- ^ signing keypairs + caplists
-> [Verifier ParsedVerifierArgs]
-- ^ verifiers
Expand Down Expand Up @@ -639,7 +639,7 @@ mkCont
-- ^ environment data
-> PublicMeta
-- ^ command public metadata
-> [(DynKeyPair, [UserCapability])]
-> [(DynKeyPair, [SigCapability])]
-- ^ signing keypairs
-> [Verifier ParsedVerifierArgs]
-- ^ verifiers
Expand Down Expand Up @@ -700,15 +700,15 @@ mkUnsignedCont txid step rollback mdata pubMeta kps ves ridm proof nid = do
-- Parse `APIKeyPair`s into Ed25519 keypairs and WebAuthn keypairs.
-- The keypairs must not be prefixed with "WEBAUTHN-", it accepts
-- only the raw (unprefixed) keys.
mkKeyPairs :: [ApiKeyPair] -> IO [(DynKeyPair, [UserCapability])]
mkKeyPairs :: [ApiKeyPair] -> IO [(DynKeyPair, [SigCapability])]
mkKeyPairs keyPairs = traverse mkPair keyPairs
where

importValidKeyPair
:: Maybe PublicKeyBS
-> PrivateKeyBS
-> Maybe [UserCapability]
-> Either String (Ed25519KeyPair, [UserCapability])
-> Maybe [SigCapability]
-> Either String (Ed25519KeyPair, [SigCapability])
importValidKeyPair pubEd25519 privEd25519 caps = fmap (,maybe [] id caps) $
importEd25519KeyPair pubEd25519 privEd25519

Expand All @@ -718,7 +718,7 @@ mkKeyPairs keyPairs = traverse mkPair keyPairs
Just ED25519 -> True
_ -> False

mkPair :: ApiKeyPair -> IO (DynKeyPair, [UserCapability])
mkPair :: ApiKeyPair -> IO (DynKeyPair, [SigCapability])
mkPair akp = case (_akpScheme akp, _akpPublic akp, _akpSecret akp, _akpAddress akp) of
(scheme, pub, priv, Nothing) | isEd25519 scheme ->
either dieAR (return . first DynEd25519KeyPair) (importValidKeyPair pub priv (_akpCaps akp))
Expand Down
4 changes: 2 additions & 2 deletions src/Pact/Bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ main = do
!bench10Cmds <- parseCode (intercalate " " (replicate 10 "(bench.bench)"))
let
!params = [PLiteral $ LString "Acct1",PLiteral $ LString "Acct2", PLiteral $ LDecimal 1.0]
!mcaps = [UserCapability (QualifiedName "bench" "MTRANSFER" def) params
,UserCapability (QualifiedName "bench" "TRANSFER" def) params]
!mcaps = [SigCapability (QualifiedName "bench" "MTRANSFER" def) params
,SigCapability (QualifiedName "bench" "TRANSFER" def) params]

!signer = [Signer Nothing pk Nothing []]
!msigner = [Signer Nothing pk Nothing mcaps]
Expand Down
2 changes: 1 addition & 1 deletion src/Pact/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ enforceGuard i g = case g of
GCapability CapabilityGuard{..} -> do
traverse_ (enforcePactId True) _cgPactId
args <- traverse enforcePactValue _cgArgs
acquired <- capabilityAcquired $ UserCapability _cgName args
acquired <- capabilityAcquired $ SigCapability _cgName args
unless acquired $ failTx' i "Capability not acquired"
where
enforcePactId doFail pid = do
Expand Down
2 changes: 1 addition & 1 deletion src/Pact/GasModel/GasTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ requireCapabilityTests = tests
requireCapExpr =
defPactExpression [text| (require-capability ($acctModuleNameText.GOV)) |]

cap = UserCapability (QualifiedName acctModuleName "GOV" def) []
cap = SigCapability (QualifiedName acctModuleName "GOV" def) []
capSlot = CapSlot CapCallStack cap []
updateGrantedCap = setState (set (evalCapabilities . capStack) [capSlot])

Expand Down
4 changes: 2 additions & 2 deletions src/Pact/GasModel/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import qualified Data.Text as T


import Pact.Compile (compileExps, mkTextInfo)
import Pact.Types.Capability (UserCapability)
import Pact.Types.Capability (SigCapability)
import Pact.Types.Command
import Pact.Types.Lang
import Pact.Types.PactValue (PactValue(..))
Expand Down Expand Up @@ -386,7 +386,7 @@ sampleMultiPubKeys =
, PublicKeyText "key2"
]

samplePubKeysWithCaps :: [(PublicKeyText, S.Set UserCapability)]
samplePubKeysWithCaps :: [(PublicKeyText, S.Set SigCapability)]
samplePubKeysWithCaps = map (\p -> (p,S.empty)) samplePubKeys

sampleKeyset :: KeySet
Expand Down
2 changes: 1 addition & 1 deletion src/Pact/Native/Capabilities.hs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ capFuns :: (ApplyMgrFun e,InstallMgd e)
capFuns = (applyMgrFun,installSigCap)

installSigCap :: InstallMgd e
installSigCap UserCapability{..} cdef = do
installSigCap SigCapability{..} cdef = do
r <- evalCap cdef CapManaged True $ mkApp cdef (map fromPactValue _scArgs)
case r of
NewlyInstalled mc -> return mc
Expand Down
2 changes: 1 addition & 1 deletion src/Pact/Native/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ appToCap
-> Eval e (UserCapability, Def Ref, ([Term Name], FunType (Term Name)))
appToCap a@App{..} = requireDefApp Defcap a >>= \d@Def{..} -> do
prep@(args,_) <- prepareUserAppArgs d _appArgs _appInfo
cap <- UserCapability (QualifiedName _dModule (asString _dDefName) (getInfo a)) <$> argsToParams _appInfo args
cap <- SigCapability (QualifiedName _dModule (asString _dDefName) (getInfo a)) <$> argsToParams _appInfo args
return (cap,d,prep)

-- | Function intended for use as a View pattern
Expand Down
4 changes: 2 additions & 2 deletions src/Pact/Runtime/Capabilities.hs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ evalUserCapability i af scope cap cdef test = go scope
emitCapability :: HasInfo i => i -> UserCapability -> Eval e ()
emitCapability i cap = emitEvent i (_scName cap) (_scArgs cap)

defCapMetaParts :: UserCapability -> Text -> Def Ref
defCapMetaParts :: SigCapability -> Text -> Def Ref
-> Either Doc (Int, UserCapability, PactValue)
defCapMetaParts cap argName cdef = case findArg argName of
Nothing -> Left $ "Invalid managed argument name: " <> pretty argName
Expand All @@ -212,7 +212,7 @@ checkManaged
-> UserCapability
-> Def Ref
-> Eval e (Maybe [UserCapability])
checkManaged i (applyF,installF) cap@UserCapability{} cdef = case _dDefMeta cdef of
checkManaged i (applyF,installF) cap@SigCapability{} cdef = case _dDefMeta cdef of
-- managed: go
Just (DMDefcap dcm@DefcapManaged {}) ->
use (evalCapabilities . capManaged) >>= go dcm . S.toList
Expand Down
36 changes: 19 additions & 17 deletions src/Pact/Types/Capability.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
module Pact.Types.Capability
( Capability(..)
, CapEvalResult(..)
, UserCapability(..)
, SigCapability(..)
, UserCapability
, ManagedCapability(..), mcInstalled, mcStatic, mcManaged
, UserManagedCap(..), umcManagedValue, umcManageParamIndex, umcManageParamName, umcMgrFun
, AutoManagedCap(..), amcActive
Expand Down Expand Up @@ -60,29 +61,30 @@ instance Pretty Capability where

-- | Both UX type (thus the name) and "UserCapability".
-- TODO rename when downstream deps are more stable.
data UserCapability = UserCapability
type UserCapability = SigCapability
data SigCapability = SigCapability
{ _scName :: !QualifiedName
, _scArgs :: ![PactValue]
} deriving (Eq,Show,Generic,Ord)
instance NFData UserCapability
instance NFData SigCapability

instance Pretty UserCapability where
pretty UserCapability{..} = parens $ hsep (pretty _scName:map pretty _scArgs)
instance Pretty SigCapability where
pretty SigCapability{..} = parens $ hsep (pretty _scName:map pretty _scArgs)

instance J.Encode UserCapability where
instance J.Encode SigCapability where
build o = J.object
[ "args" J..= J.Array (_scArgs o)
, "name" J..= _scName o
]
{-# INLINABLE build #-}

instance FromJSON UserCapability where
parseJSON = withObject "UserCapability" $ \o -> UserCapability
instance FromJSON SigCapability where
parseJSON = withObject "SigCapability" $ \o -> SigCapability
<$> o .: "name"
<*> o .: "args"

instance Arbitrary UserCapability where
arbitrary = UserCapability <$> arbitrary <*> scale (min 10) arbitrary
instance Arbitrary SigCapability where
arbitrary = SigCapability <$> arbitrary <*> scale (min 10) arbitrary

-- | Various results of evaluating a capability.
-- Note: dupe managed install is an error, thus no case here.
Expand Down Expand Up @@ -145,22 +147,22 @@ data ManagedCapability c = ManagedCapability
} deriving (Show,Generic,Foldable)

-- | Given arg index, split capability args into (before,at,after)
decomposeManaged :: Int -> UserCapability -> Maybe ([PactValue],PactValue,[PactValue])
decomposeManaged idx UserCapability{..}
decomposeManaged :: Int -> SigCapability -> Maybe ([PactValue],PactValue,[PactValue])
decomposeManaged idx SigCapability{..}
| idx < 0 || idx >= length _scArgs = Nothing
| otherwise = Just (take idx _scArgs,_scArgs !! idx,drop (succ idx) _scArgs)
{-# INLINABLE decomposeManaged #-}

-- | Given arg index, get "static" capability and value
decomposeManaged' :: Int -> UserCapability -> Maybe (UserCapability,PactValue)
decomposeManaged' idx cap@UserCapability{..} = case decomposeManaged idx cap of
decomposeManaged' :: Int -> SigCapability -> Maybe (SigCapability,PactValue)
decomposeManaged' idx cap@SigCapability{..} = case decomposeManaged idx cap of
Nothing -> Nothing
Just (h,v,t) -> Just (UserCapability _scName (h ++ t),v)
Just (h,v,t) -> Just (SigCapability _scName (h ++ t),v)
{-# INLINABLE decomposeManaged' #-}

-- | Match static value to managed.
matchManaged :: ManagedCapability UserCapability -> UserCapability -> Bool
matchManaged ManagedCapability{..} cap@UserCapability{} = case _mcManaged of
matchManaged :: ManagedCapability SigCapability -> SigCapability -> Bool
matchManaged ManagedCapability{..} cap@SigCapability{} = case _mcManaged of
Left {} -> _mcStatic == cap
Right UserManagedCap{..} -> case decomposeManaged' _umcManageParamIndex cap of
Nothing -> False
Expand Down
6 changes: 3 additions & 3 deletions src/Pact/Types/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ data ProcessedCommand m a =
instance (NFData a,NFData m) => NFData (ProcessedCommand m a)


type Ed25519KeyPairCaps = (Ed25519KeyPair ,[UserCapability])
type Ed25519KeyPairCaps = (Ed25519KeyPair ,[SigCapability])

-- CREATING AND SIGNING TRANSACTIONS

mkCommand
:: J.Encode c
=> J.Encode m
=> [(Ed25519KeyPair, [UserCapability])]
=> [(Ed25519KeyPair, [SigCapability])]
-> [Verifier ParsedVerifierArgs]
-> m
-> Text
Expand Down Expand Up @@ -337,7 +337,7 @@ data Signer = Signer
-- ^ pub key value
, _siAddress :: !(Maybe Text)
-- ^ optional "address", for different pub key formats like ETH
, _siCapList :: [UserCapability]
, _siCapList :: [SigCapability]
-- ^ clist for designating signature to specific caps
} deriving (Eq, Ord, Show, Generic)

Expand Down
4 changes: 2 additions & 2 deletions tests/SchemeSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Pact.Types.Crypto
import Pact.Types.Command
import Pact.Types.Util (toB16Text, fromText')
import Pact.Types.RPC
import Pact.Types.Capability (UserCapability)
import Pact.Types.Capability (SigCapability)
import qualified Pact.Types.Hash as PactHash
import Pact.JSON.Legacy.Value
import qualified Pact.JSON.Encode as J
Expand Down Expand Up @@ -76,7 +76,7 @@ toApiKeyPairs kps = map makeAKP kps
ApiKeyPair priv (Just pub) add (Just scheme) Nothing


mkCommandTest :: [(DynKeyPair, [UserCapability])] -> [Signer] -> Text -> IO (Command ByteString)
mkCommandTest :: [(DynKeyPair, [SigCapability])] -> [Signer] -> Text -> IO (Command ByteString)
mkCommandTest kps signers code = mkCommandWithDynKeys' kps $ toExecPayload signers code


Expand Down
2 changes: 1 addition & 1 deletion tests/Test/Pact/Utils/LegacyValue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ spec_pact_types_spv =
spec_pact_types_capability :: Spec
spec_pact_types_capability =
describe "Pact.Types.Capability" $ do
spec_case @UserCapability
spec_case @SigCapability
[ Case checkRoundtrip
, Case checkRoundtrip2
, Case checkAesonCompat
Expand Down

0 comments on commit 0371f46

Please sign in to comment.