Justin Carper 2ac5bd1b89 tiny poc
2025-03-03 02:00:12 -05:00

98 lines
2.8 KiB
Go

package main
import (
"fmt"
"log"
"math/big"
"net/http"
"os"
"4vif5i.gitea.cloud/numenor-labs/discourse/poc/backend/feedback"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/gin-gonic/gin"
)
var (
rpcURL = os.Getenv("RPC_URL")
contractAddress = os.Getenv("CONTRACT_ADDRESS")
privateKey = os.Getenv("PRIVATE_KEY")
)
func main() {
// Connect to Sepolia
client, err := ethclient.Dial(rpcURL)
if err != nil {
log.Fatalf("Failed to connect to Ethereum client: %v", err)
}
// Set up HTTP server
router := gin.Default()
router.OPTIONS(
"/submit-feedback", func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "POST, OPTIONS")
c.Header("Access-Control-Allow-Headers", "Content-Type")
c.Status(http.StatusOK)
},
)
router.POST(
"/submit-feedback", func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "POST, OPTIONS")
c.Header("Access-Control-Allow-Headers", "Content-Type")
var request struct {
Proof string `json:"proof"`
Feedback string `json:"feedback"`
}
if err := c.BindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
// Mocked ZKP verification
if request.Proof != "valid_proof" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid proof"})
return
}
privateKeyECDSA, err := crypto.HexToECDSA(privateKey)
if err != nil {
fmt.Println("Failed to parse private key:", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to parse private key"})
return
}
// Create the transactor (chain ID 11155111 is for Sepolia testnet, adjust as needed)
auth, err := bind.NewKeyedTransactorWithChainID(privateKeyECDSA, big.NewInt(11155111))
if err != nil {
log.Fatalf("Failed to create transactor: %v", err)
}
// Instantiate the contract
contract, err := feedback.NewFeedback(common.HexToAddress(contractAddress), client)
if err != nil {
fmt.Println("Failed to instantiate contract:", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to instantiate contract"})
return
}
// Submit feedback to blockchain
tx, err := contract.SubmitFeedback(auth, request.Feedback)
if err != nil {
fmt.Println("Failed to submit feedback:", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to submit feedback"})
return
}
fmt.Println("Submitted feedback:", tx.Hash().Hex())
c.JSON(http.StatusOK, gin.H{"transaction": tx.Hash().Hex()})
},
)
log.Println("Server running on :3000")
router.Run(":3000")
}