Skip to content

Commit

Permalink
Save the union of the signature maps to file
Browse files Browse the repository at this point in the history
  • Loading branch information
DOBEN committed May 5, 2024
1 parent 19f09d8 commit 7d485e3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion deps/concordium-base
55 changes: 35 additions & 20 deletions src/Concordium/Client/Runner.hs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ processTransactionCmd action baseCfgDir verbose backend =
TransactionSubmit fname intOpts -> do
fileContent <- liftIO $ BSL8.readFile fname

-- Decode JSON file content into a AccountTransaction
-- Decode JSON file content into an AccountTransaction
let accountTransaction :: Types.AccountTransaction
accountTransaction = case decode fileContent of
Just item -> item
Expand Down Expand Up @@ -709,36 +709,49 @@ processTransactionCmd action baseCfgDir verbose backend =
TransactionAddSignature fname signers -> do
fileContent <- liftIO $ BSL8.readFile fname

-- Decode JSON file content into a AccountTransaction
-- Decode JSON file content into an AccountTransaction
let accountTransaction :: Types.AccountTransaction
accountTransaction = case decode fileContent of
Just item -> item
Nothing -> error "Failed to decode file content into AccountTransaction type"

-- Display transaction from the file
-- TODO: Properly decode and display, especially the payload part
logInfo ["Transaction in file: "]
logInfo [[i| #{showPrettyJSON accountTransaction}.|]]

-- Extract accountKeyMap to be used to sign the transaction
baseCfg <- getBaseConfig baseCfgDir verbose

let header = Types.atrHeader accountTransaction
let encPayload = Types.atrPayload accountTransaction
let signerAccountAddress = Types.thSender header
let signerAccountAddressText = Text.pack $ show signerAccountAddress
let signerAccountAddressText = Text.pack $ show (Types.thSender header)

-- TODO: should we check if the `nonce` still makes sense as read from the file vs the one on-chain.
-- (and throw error if additional txs have been sent by account meanwhile meaning the account nonce is not valid anymore).

-- TODO: better name than `getAccountCfgFromTxOpts2` and consolidate both functions
encryptedSigningData <- getAccountCfgFromTxOpts2 baseCfg signerAccountAddressText signers
accountKeyMap <- liftIO $ failOnError $ decryptAccountKeyMapInteractive (esdKeys encryptedSigningData) Nothing Nothing

let signatures = signEncodedTransaction encPayload header accountKeyMap
-- Sign transaction and extract the signature map B (new signatures to be added)
let encPayload = Types.atrPayload accountTransaction
let transactionB = signEncodedTransaction encPayload header accountKeyMap
let sigBMap = Types.tsSignatures (Types.atrSignature transactionB)

-- Extract the signature map A (original signatures as stored in the file)
let sigAMap = Types.tsSignatures (Types.atrSignature accountTransaction)

logInfo [[i| "Signatures: " |]]
logInfo [[i| ""|]]
logInfo [[i| #{signatures}.|]]
-- Create the union of the signature map A and the signature map B
let unionSignaturesMap = Map.unionWith Map.union sigAMap sigBMap

-- TODO: write signatures into file
-- Create final signed transaction including signtures A and B
let finalTransaction = AE.encodePretty accountTransaction{Types.atrSignature = Types.TransactionSignature unionSignaturesMap}

logSuccess [[i|Added signature successfully to the transaction in the file '#{fname}'|]]
-- TODO: remove the extra confirmation
-- Write finalTransaction to file
success <- liftIO $ handleWriteFile BSL.writeFile PromptBeforeOverwrite verbose fname finalTransaction
if success
then liftIO $ logSuccess [[i|Added signature successfully to the transaction in the file '#{fname}'|]]
else liftIO $ logError [[i|Failed to write signature to the file '#{fname}'|]]
TransactionDeployCredential fname intOpts -> do
source <- handleReadFile BSL.readFile fname
withClient backend $ do
Expand Down Expand Up @@ -1692,8 +1705,11 @@ signAndProcessTransaction verbose txCfg pl intOpts = do

let txJson = AE.encodePretty tx
success <- liftIO $ handleWriteFile BSL.writeFile PromptBeforeOverwrite verbose outFile txJson
when success $ logSuccess [[i|Wrote transaction successfully to the file '#{outFile}'|]]
-- TODO: write error if not failing to write to file

if success
then liftIO $ logSuccess [[i|Wrote transaction successfully to the file '#{outFile}'|]]
else liftIO $ logError [[i|Failed to write transaction to the file '#{outFile}'|]]

return Nothing

-- | Continuously query and display transaction status until the transaction is finalized.
Expand Down Expand Up @@ -4626,7 +4642,7 @@ formatAndSignTransaction ::
AccountKeyMap ->
Types.BareBlockItem
formatAndSignTransaction encPayload sender energy nonce expiry accKeys =
signEncodedTransaction encPayload header accKeys
Types.NormalTransaction $ signEncodedTransaction encPayload header accKeys
where
header =
Types.TransactionHeader
Expand All @@ -4638,13 +4654,12 @@ formatAndSignTransaction encPayload sender energy nonce expiry accKeys =
}

-- | Sign an encoded transaction payload, and header with the account key map
-- and return a "normal" transaction, which is ready to be sent.
-- and return a "normal" AccountTransaction.
signEncodedTransaction ::
Types.EncodedPayload ->
Types.TransactionHeader ->
AccountKeyMap ->
Types.BareBlockItem
Types.AccountTransaction
signEncodedTransaction encPayload header accKeys =
Types.NormalTransaction $
let keys = Map.toList $ fmap Map.toList accKeys
in Types.signTransaction keys header encPayload
let keys = Map.toList $ fmap Map.toList accKeys
in Types.signTransaction keys header encPayload
1 change: 1 addition & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml
resolver: lts-22.9
system-ghc: true

# User packages to be built.
# Various formats can be used as shown in the example below.
Expand Down

0 comments on commit 7d485e3

Please sign in to comment.