From a2078a24b1cfb559dc06ca9addf7dc67fa7b5bff Mon Sep 17 00:00:00 2001 From: Lucas Menendez Date: Fri, 20 Sep 2024 11:55:22 +0200 Subject: [PATCH] Squashed commit of the following: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit commit 4530292ba43839249a6f8f2a7a0a476cb995aaa0 Author: Lucas Menendez Date: Fri Sep 20 08:33:01 2024 +0200 log removed commit 77ac4a3c71b2113883e36e5712a1b357c4da2ea3 Merge: c00bd33 8e00532 Author: Lucas Menendez Date: Fri Sep 20 08:24:59 2024 +0200 Merge branch 'main' into f/extended_faucet_package commit c00bd335f8fa70f9609d499e49247ec1e972d17e Author: Lucas Menéndez Date: Thu Sep 19 08:18:47 2024 +0200 proto dependencies updated, tx sign extended to include faucet package in more tx with some formal checks --- api/transaction.go | 126 +++++++++++++++++++++++++++++++++++++++++---- go.mod | 2 +- go.sum | 4 +- 3 files changed, 120 insertions(+), 12 deletions(-) diff --git a/api/transaction.go b/api/transaction.go index e37a1f4..242a832 100644 --- a/api/transaction.go +++ b/api/transaction.go @@ -68,21 +68,21 @@ func (a *API) signTxHandler(w http.ResponseWriter, r *http.Request) { ErrInvalidTxFormat.Write(w) return } - // check the tx payload + // check if the api is not in transparent mode if !a.transparentMode { switch tx.Payload.(type) { case *models.Tx_SetAccount: - // check the account is the same as the user txSetAccount := tx.GetSetAccount() + // check the tx fields if txSetAccount == nil || txSetAccount.Account == nil || txSetAccount.InfoURI == nil { ErrInvalidTxFormat.With("missing fields").Write(w) return } + // check the account is the same as the user if !bytes.Equal(txSetAccount.GetAccount(), organizationSigner.Address().Bytes()) { ErrUnauthorized.With("invalid account").Write(w) return } - log.Infow("signing SetAccount transaction", "user", user.Email, "type", txSetAccount.Txtype.String()) // check the tx subtype switch txSetAccount.Txtype { case models.TxType_CREATE_ACCOUNT: @@ -101,19 +101,127 @@ func (a *API) signTxHandler(w http.ResponseWriter, r *http.Request) { } } } + case *models.Tx_NewProcess: + txNewProcess := tx.GetNewProcess() + // check the tx fields + if txNewProcess == nil || txNewProcess.Process == nil || txNewProcess.Nonce == 0 { + ErrInvalidTxFormat.With("missing fields").Write(w) + return + } + // check the tx subtype + switch txNewProcess.Txtype { + case models.TxType_NEW_PROCESS: + // generate a new faucet package if it's not present and include it in the tx + if txNewProcess.FaucetPackage == nil { + faucetPkg, err := a.account.FaucetPackage(organizationSigner.AddressString(), bootStrapFaucetAmount) + if err != nil { + ErrCouldNotCreateFaucetPackage.WithErr(err).Write(w) + return + } + txNewProcess.FaucetPackage = faucetPkg + tx = &models.Tx{ + Payload: &models.Tx_NewProcess{ + NewProcess: txNewProcess, + }, + } + } + } case *models.Tx_SetProcess: - log.Infow("signing SetProcess transaction", "user", user.Email) + txSetProcess := tx.GetSetProcess() + // check the tx fields + if txSetProcess == nil || txSetProcess.ProcessId == nil { + ErrInvalidTxFormat.With("missing fields").Write(w) + return + } + // check the tx subtype + switch txSetProcess.Txtype { + case models.TxType_SET_PROCESS_STATUS: + // check if the process status is in the tx + if txSetProcess.Status == nil { + ErrInvalidTxFormat.With("missing status field").Write(w) + return + } + case models.TxType_SET_PROCESS_CENSUS: + // check if the process census is in the tx + if (txSetProcess.CensusRoot == nil || txSetProcess.CensusURI == nil) && txSetProcess.CensusSize == nil { + ErrInvalidTxFormat.With("missing census fields").Write(w) + return + } + case models.TxType_SET_PROCESS_QUESTION_INDEX: + // check if the process question index is in the tx + if txSetProcess.QuestionIndex == nil { + ErrInvalidTxFormat.With("missing question index field").Write(w) + return + } + case models.TxType_SET_PROCESS_RESULTS: + // check if the process results are in the tx + if txSetProcess.Results == nil { + ErrInvalidTxFormat.With("missing results field").Write(w) + return + } + case models.TxType_SET_PROCESS_DURATION: + // check if the process duration is in the tx + if txSetProcess.Duration == nil { + ErrInvalidTxFormat.With("missing duration field").Write(w) + return + } + } + // include the faucet package in the tx if it's not present + if txSetProcess.FaucetPackage == nil { + faucetPkg, err := a.account.FaucetPackage(organizationSigner.AddressString(), bootStrapFaucetAmount) + if err != nil { + ErrCouldNotCreateFaucetPackage.WithErr(err).Write(w) + return + } + txSetProcess.FaucetPackage = faucetPkg + tx = &models.Tx{ + Payload: &models.Tx_SetProcess{ + SetProcess: txSetProcess, + }, + } + } + case *models.Tx_SetSIK, *models.Tx_DelSIK: + txSetSIK := tx.GetSetSIK() + // check the tx fields + if txSetSIK == nil || txSetSIK.SIK == nil { + ErrInvalidTxFormat.With("missing fields").Write(w) + return + } + // include the faucet package in the tx if it's not present + if txSetSIK.FaucetPackage == nil { + faucetPkg, err := a.account.FaucetPackage(organizationSigner.AddressString(), bootStrapFaucetAmount) + if err != nil { + ErrCouldNotCreateFaucetPackage.WithErr(err).Write(w) + return + } + txSetSIK.FaucetPackage = faucetPkg + tx = &models.Tx{ + Payload: &models.Tx_SetSIK{ + SetSIK: txSetSIK, + }, + } + } case *models.Tx_CollectFaucet: - log.Infow("signing CollectFaucet transaction", "user", user.Email) - case *models.Tx_NewProcess: - log.Infow("signing NewProcess transaction", "user", user.Email) + txCollectFaucet := tx.GetCollectFaucet() + // include the faucet package in the tx if it's not present + if txCollectFaucet.FaucetPackage == nil { + faucetPkg, err := a.account.FaucetPackage(organizationSigner.AddressString(), bootStrapFaucetAmount) + if err != nil { + ErrCouldNotCreateFaucetPackage.WithErr(err).Write(w) + return + } + txCollectFaucet.FaucetPackage = faucetPkg + tx = &models.Tx{ + Payload: &models.Tx_CollectFaucet{ + CollectFaucet: txCollectFaucet, + }, + } + } default: log.Warnw("transaction type not allowed", "user", user.Email, "type", fmt.Sprintf("%T", tx.Payload)) ErrTxTypeNotAllowed.Write(w) return } - } else { - log.Infow("signing transaction in full transparent mode", "user", user.Email, "type", fmt.Sprintf("%T", tx.Payload)) } // sign the tx stx, err := a.account.SignTransaction(tx, organizationSigner) diff --git a/go.mod b/go.mod index 26be066..74b45cc 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/twilio/twilio-go v1.23.0 go.mongodb.org/mongo-driver v1.14.0 go.vocdoni.io/dvote v1.10.2-0.20240726114655-b510ac8a7e42 - go.vocdoni.io/proto v1.15.8 + go.vocdoni.io/proto v1.15.10 google.golang.org/protobuf v1.34.0 ) diff --git a/go.sum b/go.sum index 0ac0384..a9b2b71 100644 --- a/go.sum +++ b/go.sum @@ -1664,8 +1664,8 @@ go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= go.vocdoni.io/dvote v1.10.2-0.20240726114655-b510ac8a7e42 h1:w7Yp/IzkGNKuXKWRPyQoVasOjwNIadQOS9o3RQqJyF4= go.vocdoni.io/dvote v1.10.2-0.20240726114655-b510ac8a7e42/go.mod h1:NvAi97FrSOJBX45H9SntOjoj28bA2lX+Qh26F40hPY4= -go.vocdoni.io/proto v1.15.8 h1:I5HVHffQwXyp0jootnCVj83+PoJ8L703RJ2CFSPTa2Q= -go.vocdoni.io/proto v1.15.8/go.mod h1:oi/WtiBFJ6QwNDv2aUQYwOnUKzYuS/fBqXF8xDNwcGo= +go.vocdoni.io/proto v1.15.10 h1:AMMpfLp5+hCaOFMaylwbhJE2uieWno4d+iSNkXEDtDQ= +go.vocdoni.io/proto v1.15.10/go.mod h1:oi/WtiBFJ6QwNDv2aUQYwOnUKzYuS/fBqXF8xDNwcGo= go4.org v0.0.0-20180809161055-417644f6feb5/go.mod h1:MkTOUMDaeVYJUOUsaDXIhWPZYa1yOyC1qaOBpL57BhE= go4.org v0.0.0-20200411211856-f5505b9728dd/go.mod h1:CIiUVy99QCPfoE13bO4EZaz5GZMZXMSBGhxRdsvzbkg= go4.org v0.0.0-20230225012048-214862532bf5 h1:nifaUDeh+rPaBCMPMQHZmvJf+QdpLFnuQPwx+LxVmtc=