Skip to content

Commit

Permalink
Regist in community works
Browse files Browse the repository at this point in the history
  • Loading branch information
OmegaTymbJIep committed Jun 30, 2024
1 parent f5ae210 commit de13818
Show file tree
Hide file tree
Showing 20 changed files with 2,970 additions and 145 deletions.
Binary file added circom/PostMessage.wasm
Binary file not shown.
Binary file added circom/PostMessage.zkey
Binary file not shown.
Binary file added circom/VerifiableCommitment.wasm
Binary file not shown.
Binary file added circom/VerifiableCommitment.zkey
Binary file not shown.
1,112 changes: 1,112 additions & 0 deletions contracts/auth_storage.go

Large diffs are not rendered by default.

1,139 changes: 1,139 additions & 0 deletions contracts/chat.go

Large diffs are not rendered by default.

35 changes: 33 additions & 2 deletions contracts/erc721.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ require (
github.com/go-chi/chi v4.1.2+incompatible
github.com/go-ozzo/ozzo-validation/v4 v4.2.1
github.com/google/uuid v1.4.0
github.com/iden3/go-iden3-crypto v0.0.15
github.com/iden3/go-rapidsnark/prover v0.0.10
github.com/iden3/go-rapidsnark/witness/v2 v2.0.0
github.com/iden3/go-rapidsnark/witness/wazero v0.0.0-20230524142950-0986cf057d4e
github.com/pkg/errors v0.9.1
github.com/rubenv/sql-migrate v1.6.1
gitlab.com/distributed_lab/ape v1.7.1
Expand All @@ -25,6 +29,7 @@ require (
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/certifi/gocertifi v0.0.0-20200211180108-c7c1fbc02894 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dchest/blake512 v1.0.0 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
Expand All @@ -40,6 +45,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.0 // indirect
github.com/iden3/go-rapidsnark/types v0.0.3 // indirect
github.com/jmoiron/sqlx v1.3.5 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
Expand All @@ -63,6 +69,7 @@ require (
github.com/spf13/viper v1.18.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tetratelabs/wazero v1.1.0 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
gitlab.com/distributed_lab/figure/v3 v3.1.4 // indirect
Expand Down
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/blake512 v1.0.0 h1:oDFEQFIqFSeuA34xLtXZ/rWxCXdSjirjzPhey5EUvmA=
github.com/dchest/blake512 v1.0.0/go.mod h1:FV1x7xPPLWukZlpDpWQ88rF/SFwZ5qbskrzhLMB92JI=
github.com/deckarep/golang-set v1.8.0 h1:sk9/l/KqpunDwP7pSjUg0keiOOLEnOBHzykLrsPppp4=
github.com/deckarep/golang-set v1.8.0/go.mod h1:5nI87KwE7wgsBU1F4GKAw2Qod7p5kyS383rP6+o6qqo=
github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0=
Expand Down Expand Up @@ -1612,6 +1614,16 @@ github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/iden3/go-iden3-crypto v0.0.15 h1:4MJYlrot1l31Fzlo2sF56u7EVFeHHJkxGXXZCtESgK4=
github.com/iden3/go-iden3-crypto v0.0.15/go.mod h1:dLpM4vEPJ3nDHzhWFXDjzkn1qHoBeOT/3UEhXsEsP3E=
github.com/iden3/go-rapidsnark/prover v0.0.10 h1:NvOfRPpex/k646UsqOcUy7a7uVl17t4ok9kWvpQg4+k=
github.com/iden3/go-rapidsnark/prover v0.0.10/go.mod h1:wgDsmKOGCuWGtgVtuW9ARWNguNr4NJAIyg2G7+uTax0=
github.com/iden3/go-rapidsnark/types v0.0.3 h1:f0s1Qdut1qHe1O67+m+xUVRBPwSXnq5j0xSrBi0jqM4=
github.com/iden3/go-rapidsnark/types v0.0.3/go.mod h1:ApgcaUxKIgSRA6fAeFxK7p+lgXXfG4oA2HN5DhFlfF4=
github.com/iden3/go-rapidsnark/witness/v2 v2.0.0 h1:mkY6VDfwKVJc83QGKmwVXY2LYepidPrFAxskrjr8UCs=
github.com/iden3/go-rapidsnark/witness/v2 v2.0.0/go.mod h1:3JRjqUfW1hgI9hzLDO0v8z/DUkR0ZUehhYLlnIfRxnA=
github.com/iden3/go-rapidsnark/witness/wazero v0.0.0-20230524142950-0986cf057d4e h1:WeiFCrpj5pLRtSA4Mg03yTrSZhHHqN/k5b6bwxd9/tY=
github.com/iden3/go-rapidsnark/witness/wazero v0.0.0-20230524142950-0986cf057d4e/go.mod h1:UEBifEzw62T6VzIHJeHuUgeLg2U/J9ttf7hOwQEqnYk=
github.com/imkira/go-interpol v1.1.0/go.mod h1:z0h2/2T3XF8kyEPpRgJ3kmNv+C43p+I/CoI+jC3w2iA=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/flux v0.65.1/go.mod h1:J754/zds0vvpfwuq7Gc2wRdVwEodfpCFM7mYlOw2LqY=
Expand Down Expand Up @@ -1699,6 +1711,7 @@ github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk=
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
Expand Down Expand Up @@ -1996,6 +2009,8 @@ github.com/tailscale/depaware v0.0.0-20210622194025-720c4b409502/go.mod h1:p9lPs
github.com/tdewolff/minify/v2 v2.12.4/go.mod h1:h+SRvSIX3kwgwTFOpSckvSxgax3uy8kZTSF1Ojrr3bk=
github.com/tdewolff/parse/v2 v2.6.4/go.mod h1:woz0cgbLwFdtbjJu8PIKxhW05KplTFQkOdX78o+Jgrs=
github.com/tdewolff/test v1.0.7/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
github.com/tetratelabs/wazero v1.1.0 h1:EByoAhC+QcYpwSZJSs/aV0uokxPwBgKxfiokSUwAknQ=
github.com/tetratelabs/wazero v1.1.0/go.mod h1:wYx2gNRg8/WihJfSDxA1TIL8H+GkfLYm+bIfbblu9VQ=
github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
github.com/tklauser/go-sysconf v0.3.5/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI=
github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM=
Expand Down
26 changes: 26 additions & 0 deletions internal/config/circom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package config

import (
"gitlab.com/distributed_lab/figure"
"gitlab.com/distributed_lab/kit/kv"
"gitlab.com/distributed_lab/logan/v3/errors"
)

type CircomConfig struct {
PathToCircuits string `fig:"path_to_circuits,required"` //nolint
}

func (c *config) Circom() *CircomConfig {
return c.circom.Do(func() interface{} {
config := CircomConfig{}
err := figure.
Out(&config).
From(kv.MustGetStringMap(c.getter, "circom")).
Please()
if err != nil {
panic(errors.Wrap(err, "failed to figure out ethereum config"))
}

return &config
}).(*CircomConfig)
}
19 changes: 13 additions & 6 deletions internal/config/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"crypto/ecdsa"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"gitlab.com/distributed_lab/figure"
Expand All @@ -11,13 +12,17 @@ import (
)

type EthClientConfig struct {
EthClient *ethclient.Client
PrivateKey *ecdsa.PrivateKey
EthClient *ethclient.Client
PrivateKey *ecdsa.PrivateKey
AuthStorageContract common.Address
ChatContract common.Address
}

type ethClientConfigRaw struct {
RpcURL string `fig:"rpc_url,required"` //nolint
PrivateKey string `fig:"private_key,required"`
RpcURL string `fig:"rpc_url,required"` //nolint
PrivateKey string `fig:"private_key,required"`
AuthStorageContract string `fig:"auth_storage_contract,required"`
ChatContract string `fig:"chat_contract,required"`
}

func (c *config) EthClient() *EthClientConfig {
Expand All @@ -42,8 +47,10 @@ func (c *config) EthClient() *EthClientConfig {
}

return &EthClientConfig{
EthClient: ethClient,
PrivateKey: privateKey,
EthClient: ethClient,
PrivateKey: privateKey,
AuthStorageContract: common.HexToAddress(configRaw.AuthStorageContract),
ChatContract: common.HexToAddress(configRaw.ChatContract),
}
}).(*EthClientConfig)
}
2 changes: 2 additions & 0 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Config interface {

EthClient() *EthClientConfig
API() *APIConfig
Circom() *CircomConfig
}

type config struct {
Expand All @@ -27,6 +28,7 @@ type config struct {

ethClient comfig.Once
api comfig.Once
circom comfig.Once
}

func New(getter kv.Getter) Config {
Expand Down
40 changes: 40 additions & 0 deletions internal/service/api/handlers/register_in_community.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package handlers

import (
"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"
)

func RegisterInCommunity(w http.ResponseWriter, r *http.Request) {
if MockAPI(r) {
ape.Render(w, responses.MockedCreateCommunity())
return
}

req, err := requests.NewRegisterInCommunity(r)
if err != nil {
Log(r).WithField("reason", err).Debug("Bad request")
ape.RenderErr(w, problems.BadRequest(err)...)
return
}

registerRequest, err := Core(r).RegisterInCommunity(
req.BJJPublicKey,
req.NFTOwner,
req.ContractId,
req.NFTID,
)
if err != nil {
Log(r).WithError(err).
Error("Failed register in community")
ape.RenderErr(w, problems.InternalError())
return
}

ape.Render(w, responses.NewRegisterInCommunity(registerRequest))
}
80 changes: 80 additions & 0 deletions internal/service/api/requests/register_in_community.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package requests

import (
"encoding/json"
"math/big"
"net/http"

"github.com/ethereum/go-ethereum/common"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/iden3/go-iden3-crypto/babyjub"
"gitlab.com/distributed_lab/logan/v3/errors"
)

type registerInCommunityRequest struct {
NFTID big.Int `json:"nft_id"`
NFTOwner string `json:"nft_owner"`
ContractId string `json:"contract_id"`
BJJPublicKey string `json:"bjj_public_key"`
}

type RegisterInCommunityRequest struct {
NFTID big.Int
NFTOwner common.Address
ContractId common.Address
BJJPublicKey babyjub.PublicKey
}

func NewRegisterInCommunity(r *http.Request) (*RegisterInCommunityRequest, error) {
var requestBody registerInCommunityRequest

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 *registerInCommunityRequest) validate() error {
return validation.Errors{
"body/nft_owner": validation.Validate(
r.NFTOwner, validation.Required, validation.By(MustBeValidAddress),
),
"body/contract_id": validation.Validate(
r.ContractId, validation.Required, validation.By(MustBeValidAddress),
),
"body/bjj_public_key": validation.Validate(
r.BJJPublicKey, validation.Required, validation.By(MustBeValidBJJPublicKey),
),
}.Filter()
}

func (r *registerInCommunityRequest) parse() *RegisterInCommunityRequest {
var bjjPublicKey babyjub.PublicKey
(&bjjPublicKey).UnmarshalText([]byte(r.BJJPublicKey))

return &RegisterInCommunityRequest{
NFTID: r.NFTID,
NFTOwner: common.HexToAddress(r.NFTOwner),
ContractId: common.HexToAddress(r.ContractId),
BJJPublicKey: bjjPublicKey,
}
}

func MustBeValidBJJPublicKey(value interface{}) error {
valueStr, ok := value.(string)
if !ok {
return errors.New("invalid BJJ public key")
}

if err := new(babyjub.PublicKey).UnmarshalText([]byte(valueStr)); err != nil {
return errors.Wrap(err, "invalid BJJ public key")
}

return nil
}
19 changes: 19 additions & 0 deletions internal/service/api/responses/register_in_community.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package responses

import (
"github.com/google/uuid"

"github.com/black-pepper-team/community-indexer/internal/service/core"
)

type RegisterRequest struct {
Id uuid.UUID `json:"id"`
Status string `json:"status"`
}

func NewRegisterInCommunity(registerRequest *core.RegisterRequest) *RegisterRequest {
return &RegisterRequest{
Id: registerRequest.Id,
Status: string(registerRequest.Status),
}
}
1 change: 1 addition & 0 deletions internal/service/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (s *service) router() chi.Router {
r.Post("/", handlers.CreateCommunity)
r.Get("/list", handlers.CommunityList)
r.Post("/import", handlers.ImportCommunity)
r.Post("/register", handlers.RegisterInCommunity)
})
})

Expand Down
Loading

0 comments on commit de13818

Please sign in to comment.