Skip to content

Commit

Permalink
batch signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
imalsogreg committed Jul 29, 2024
1 parent baa78b9 commit 2977553
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 171 deletions.
41 changes: 19 additions & 22 deletions pact-request-api/Pact/Core/Command/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ module Pact.Core.Command.Client (
SubmitBatch(..),
) where

import Control.Applicative
import Control.Lens
import Control.Applicative((<|>))
import Control.Monad.Except
import Control.Exception.Safe
import Control.Monad
import qualified Crypto.Hash.Algorithms as Crypto
import Data.Default(def)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified Data.ByteString.Short as SBS
import qualified Data.Map.Strict as M
import qualified Data.Set as S
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
Expand All @@ -71,28 +74,22 @@ import System.FilePath

import qualified Pact.JSON.Decode as JD
import qualified Pact.JSON.Encode as J
import qualified Data.Set as S
import qualified Data.Map.Strict as M
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Short as SBS

import Pact.Core.Capabilities
import Pact.Core.ChainData
import Pact.Core.Command.Crypto
import Pact.Core.Gas.Types
import Pact.Core.Guards
import Pact.Core.Command.RPC
import Pact.Core.Command.SigData
import Pact.Core.Command.Types
import Pact.Core.Command.Util
import Pact.Core.Command.Crypto
import Pact.Core.Gas
import Pact.Core.Guards
import Pact.Core.Hash
import Pact.Core.Hash as PactHash
import Pact.Core.PactValue
import Pact.Core.Names
import Pact.Core.SPV
import Pact.Core.StableEncoding
import Pact.Core.Verifiers
import qualified Pact.Core.Hash as PactHash
import Pact.Core.Command.SigData


-- -------------------------------------------------------------------------- --
Expand Down Expand Up @@ -208,7 +205,7 @@ instance J.Encode ApiPublicMeta where

data ApiReq = ApiReq {
_ylType :: Maybe Text,
_ylPactTxHash :: Maybe Hash,
_ylPactTxHash :: Maybe PactHash.Hash,
_ylStep :: Maybe Int,
_ylRollback :: Maybe Bool,
_ylData :: Maybe PactValue,
Expand Down Expand Up @@ -405,7 +402,7 @@ returnSigDataOrCommand outputLocal sd
Left "Number of signers in the payload does not match number of signers in the sigData"
usrSigs <- traverse (toSignerPair sigMap) (_pSigners payload)
traverse_ Left $ verifyUserSigs h [ (signer, sig) | (sig, Just signer) <- usrSigs ]
_ <- verifyHash h (T.encodeUtf8 cmd)
_ <- PactHash.verifyHash h (T.encodeUtf8 cmd)
pure ()
where
toSignerPair sigMap signer =
Expand Down Expand Up @@ -533,7 +530,7 @@ signCmd keyFiles bs = do
Right h -> do
kps <- mapM importKeyFile keyFiles
fmap (encodeYaml . J.Object) $ forM kps $ \kp -> do
let sig = signHash (Hash $ SBS.toShort h) kp
let sig = signHash (PactHash.Hash $ SBS.toShort h) kp
return ((toB16Text . _b16JsonBytes) (B16JsonBytes (getPublic kp)), sig)

withKeypairsOrSigner
Expand Down Expand Up @@ -689,7 +686,7 @@ mkApiReqCont unsignedReq ar@ApiReq{..} fp = do
JD.eitherDecode
(Nothing,Nothing) -> return PUnit
_ -> dieAR "Expected either a 'data' or 'dataFile' entry, or neither"
let pactId = (DefPactId . hashToText) apiPactId
let pactId = (DefPactId . PactHash.hashToText) apiPactId
pubMeta <- mkPubMeta _ylPublicMeta
cmd <- withKeypairsOrSigner unsignedReq ar
(\ks -> mkCont pactId step rollback cdata pubMeta ks (fromMaybe [] _ylVerifiers) _ylNonce _ylProof _ylNetworkId)
Expand Down Expand Up @@ -980,15 +977,15 @@ nonemptyVerifiers vs = Just vs
-- 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, [CapToken QualifiedName PactValue])]
mkKeyPairs :: [ApiKeyPair] -> IO [(DynKeyPair, [UserCapability])]
mkKeyPairs keyPairs = traverse mkPair keyPairs
where

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

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

mkPair :: ApiKeyPair -> IO (DynKeyPair, [CapToken QualifiedName PactValue])
mkPair :: ApiKeyPair -> IO (DynKeyPair, [UserCapability])
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
143 changes: 0 additions & 143 deletions pact-request-api/Pact/Core/Command/Server.hs

This file was deleted.

8 changes: 2 additions & 6 deletions pact-tng.cabal
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cabal-version: 3.8
cabal-version: 3.12
name: pact-tng
version: 5.0
-- ^ 4 digit is prerelease, 3- or 2-digit for prod release
Expand Down Expand Up @@ -384,11 +384,7 @@ executable pact
default-language: Haskell2010

-- beware of the autogen modules. Remember to `cabal clean`!
other-modules:
-- TODO: Uncomment once this is finally fixed
-- and stops crapping out both LSP and our CI,
-- PackageInfo_pact_tng
Paths_pact_tng
other-modules: PackageInfo_pact_tng

benchmark bench
type: exitcode-stdio-1.0
Expand Down
2 changes: 2 additions & 0 deletions pact/Pact/Core/StableEncoding.hs
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,5 @@ instance JD.FromJSON (StableEncoding PublicMeta) where
instance J.Encode (StableEncoding a) => J.Encode (StableEncoding (Maybe a)) where
build (StableEncoding a) = J.build (StableEncoding <$> a)

-- instance J.Encode (StableEncoding a) => J.Encode (StableEncoding [a]) where
-- build (StableEncoding a) = J.build (J.Array (fmap StableEncoding a))

0 comments on commit 2977553

Please sign in to comment.