Skip to content

Commit

Permalink
Everything works
Browse files Browse the repository at this point in the history
  • Loading branch information
OmegaTymbJIep committed Jul 1, 2024
1 parent 811c971 commit 5a35ab6
Show file tree
Hide file tree
Showing 11 changed files with 1,721 additions and 29 deletions.
1,247 changes: 1,247 additions & 0 deletions contracts/poseidon_smt.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions internal/config/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ type EthClientConfig struct {
PrivateKey *ecdsa.PrivateKey
AuthStorageContract common.Address
ChatContract common.Address
PoseidonSMTContract common.Address
}

type ethClientConfigRaw struct {
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"`
PoseidonSMTContract string `fig:"poseidon_smt,required"`
}

func (c *config) EthClient() *EthClientConfig {
Expand Down Expand Up @@ -51,6 +53,7 @@ func (c *config) EthClient() *EthClientConfig {
PrivateKey: privateKey,
AuthStorageContract: common.HexToAddress(configRaw.AuthStorageContract),
ChatContract: common.HexToAddress(configRaw.ChatContract),
PoseidonSMTContract: common.HexToAddress(configRaw.PoseidonSMTContract),
}
}).(*EthClientConfig)
}
1 change: 1 addition & 0 deletions internal/service/api/handlers/register_in_community.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func RegisterInCommunity(w http.ResponseWriter, r *http.Request) {
req.NFTOwner,
req.ContractId,
req.NFTID,
req.PrivateKey,
)
if err != nil {
Log(r).WithError(err).
Expand Down
40 changes: 40 additions & 0 deletions internal/service/api/handlers/send_message.go
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 SendMessage(w http.ResponseWriter, r *http.Request) {
req, err := requests.NewSendMessage(r)
if err != nil {
Log(r).WithField("reason", err).Debug("Bad request")
ape.RenderErr(w, problems.BadRequest(err)...)
return
}

sendRequest, err := Core(r).SendMessage(
req.BJJPrivateKey, req.NFTID, req.NFTOwner, req.ContractId, req.Message,
)
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(sendRequest))
}
18 changes: 13 additions & 5 deletions internal/service/api/requests/register_in_community.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
package requests

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

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
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"`
NFTID string `json:"nft_id"`
NFTOwner string `json:"nft_owner"`
ContractId string `json:"contract_id"`
BJJPublicKey string `json:"bjj_public_key"`
PrivateKey string `json:"private_key"`
}

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

func NewRegisterInCommunity(r *http.Request) (*RegisterInCommunityRequest, error) {
Expand Down Expand Up @@ -58,11 +62,15 @@ func (r *registerInCommunityRequest) parse() *RegisterInCommunityRequest {
var bjjPublicKey babyjub.PublicKey
(&bjjPublicKey).UnmarshalText([]byte(r.BJJPublicKey))

nftId, _ := new(big.Int).SetString(r.NFTID, 10)
sk, _ := crypto.HexToECDSA(r.PrivateKey)

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

Expand Down
69 changes: 69 additions & 0 deletions internal/service/api/requests/send_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package requests

import (
"encoding/hex"
"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 sendMessageRequest struct {
NFTID string `json:"nft_id"`
NFTOwner string `json:"nft_owner"`
ContractId string `json:"contract_id"`
BJJPrivateKey string `json:"bjj_private_key"`
Message string `json:"message"`
}

type SendMessageRequest struct {
NFTID *big.Int `json:"nft_id"`
NFTOwner common.Address `json:"nft_owner"`
ContractId common.Address `json:"contract_id"`
BJJPrivateKey babyjub.PrivateKey `json:"bjj_private_key"`
Message string `json:"message"`
}

func NewSendMessage(r *http.Request) (*SendMessageRequest, error) {
var requestBody sendMessageRequest

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 *sendMessageRequest) 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),
),
}.Filter()
}

func (r *sendMessageRequest) parse() *SendMessageRequest {
bjjSkBytes, _ := hex.DecodeString(r.BJJPrivateKey)
bjjPrivateKey := babyjub.PrivateKey(bjjSkBytes)
nftId, _ := new(big.Int).SetString(r.NFTID, 10)

return &SendMessageRequest{
NFTID: nftId,
NFTOwner: common.HexToAddress(r.NFTOwner),
ContractId: common.HexToAddress(r.ContractId),
BJJPrivateKey: bjjPrivateKey,
Message: r.Message,
}
}
1 change: 1 addition & 0 deletions internal/service/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (s *service) router() chi.Router {
r.Post("/register", handlers.RegisterInCommunity)
r.Get("/register/{register-id}", handlers.GetRegisterStatus)
r.Post("/add-participant", handlers.AddCommunityParticipant)
r.Post("/message", handlers.SendMessage)
})
})

Expand Down
62 changes: 62 additions & 0 deletions internal/service/core/build_credentila_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package core

import (
"encoding/hex"
"fmt"
"log"
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/iden3/go-iden3-crypto/babyjub"
)

func TestBuildCredential(t *testing.T) {
contractId := common.HexToAddress("0x0B306BF915C4d645ff596e518fAf3F9669b97016")
nftOwner := common.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")

x, ok := new(big.Int).SetString("14302959900069061493388322109831425126447754027481418724221945884581763476571", 10)
if !ok {
log.Fatalf("failed to set x")
}

y, ok := new(big.Int).SetString("19133337649544864758584673172563393357921375750532468744093033525654718926514", 10)
if !ok {
log.Fatalf("failed to set y")
}

pk := babyjub.PublicKey(babyjub.Point{
X: x,
Y: y,
})

credentialID, err := buildCredentialId(
contractId,
big.NewInt(0),
nftOwner,
pk.X,
pk.Y,
5017,
)
if err != nil {
log.Fatalf("failed to build credential id: %v", err)
}

hexCredentialID := hex.EncodeToString(credentialID.Bytes())

fmt.Println(hexCredentialID)
}

func TestMessageEncoding(t *testing.T) {
message := "Hello World!"

fmt.Println(hex.EncodeToString(crypto.Keccak256([]byte(message))))

hashInt := new(big.Int).SetBytes(crypto.Keccak256([]byte(message)))

mask := new(big.Int).SetBytes([]byte{0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff})
result := new(big.Int).And(hashInt, mask)

fmt.Println(result)
}
Loading

0 comments on commit 5a35ab6

Please sign in to comment.