-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb4e091
commit 811c971
Showing
8 changed files
with
189 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package handlers | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
|
||
"gitlab.com/distributed_lab/ape" | ||
"gitlab.com/distributed_lab/ape/problems" | ||
|
||
"github.com/black-pepper-team/community-indexer/internal/service/api/requests" | ||
"github.com/black-pepper-team/community-indexer/internal/service/api/responses" | ||
"github.com/black-pepper-team/community-indexer/internal/service/core" | ||
) | ||
|
||
func AddCommunityParticipant(w http.ResponseWriter, r *http.Request) { | ||
req, err := requests.NewAddCommunityParticipant(r) | ||
if err != nil { | ||
Log(r).WithField("reason", err).Debug("Bad request") | ||
ape.RenderErr(w, problems.BadRequest(err)...) | ||
return | ||
} | ||
|
||
mintRequest, err := Core(r).AddCommunityParticipant( | ||
req.PrivateKey, req.ParticipantAddress, req.ContractAddress, | ||
) | ||
switch { | ||
case errors.Is(err, core.ErrContractNotFound): | ||
ape.RenderErr(w, problems.NotFound()) | ||
Log(r).WithError(err). | ||
Debug("Contract not found") | ||
return | ||
case err != nil: | ||
Log(r).WithError(err). | ||
Error("Failed add community participant") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
ape.Render(w, responses.NewRegisterInCommunity(mintRequest)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package requests | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
validation "github.com/go-ozzo/ozzo-validation/v4" | ||
"gitlab.com/distributed_lab/logan/v3/errors" | ||
) | ||
|
||
type addCommunityParticipantRequest struct { | ||
PrivateKey string `json:"private_key"` | ||
ParticipantAddress string `json:"participant_address"` | ||
ContractAddress string `json:"contract_address"` | ||
} | ||
|
||
type AddCommunityParticipantRequest struct { | ||
PrivateKey *ecdsa.PrivateKey | ||
ParticipantAddress common.Address | ||
ContractAddress common.Address | ||
} | ||
|
||
func NewAddCommunityParticipant(r *http.Request) (*AddCommunityParticipantRequest, error) { | ||
var requestBody addCommunityParticipantRequest | ||
|
||
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil { | ||
return nil, errors.Wrap(err, "failed to decode json request body") | ||
} | ||
|
||
if err := requestBody.validate(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return requestBody.parse(), nil | ||
} | ||
|
||
// nolint | ||
func (r *addCommunityParticipantRequest) validate() error { | ||
return validation.Errors{ | ||
"body/private_key": validation.Validate( | ||
r.PrivateKey, validation.Required, validation.Length(64, 64), | ||
), | ||
"body/participant_address": validation.Validate( | ||
r.ParticipantAddress, validation.Required, validation.By(MustBeValidAddress), | ||
), | ||
"body/contract_address": validation.Validate( | ||
r.ContractAddress, validation.Required, validation.By(MustBeValidAddress), | ||
), | ||
}.Filter() | ||
} | ||
|
||
func (r *addCommunityParticipantRequest) parse() *AddCommunityParticipantRequest { | ||
sk, _ := crypto.HexToECDSA(r.PrivateKey) | ||
return &AddCommunityParticipantRequest{ | ||
PrivateKey: sk, | ||
ParticipantAddress: common.HexToAddress(r.ParticipantAddress), | ||
ContractAddress: common.HexToAddress(r.ContractAddress), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters