-
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
811c971
commit 5a35ab6
Showing
11 changed files
with
1,721 additions
and
29 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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)) | ||
} |
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,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, | ||
} | ||
} |
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 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) | ||
} |
Oops, something went wrong.