Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 4530292
Author: Lucas Menendez <[email protected]>
Date:   Fri Sep 20 08:33:01 2024 +0200

    log removed

commit 77ac4a3
Merge: c00bd33 8e00532
Author: Lucas Menendez <[email protected]>
Date:   Fri Sep 20 08:24:59 2024 +0200

    Merge branch 'main' into f/extended_faucet_package

commit c00bd33
Author: Lucas Menéndez <[email protected]>
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
  • Loading branch information
lucasmenendez committed Sep 20, 2024
1 parent 8e00532 commit a2078a2
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 12 deletions.
126 changes: 117 additions & 9 deletions api/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit a2078a2

Please sign in to comment.