Skip to content

Commit

Permalink
feat: Add gh verify agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed Oct 7, 2024
1 parent 2c9f212 commit 603966c
Show file tree
Hide file tree
Showing 12 changed files with 2,246 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gno_github_agent/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GNO_MNEMONIC=scissors razor beauty delay derive chronic toss burger gravity shallow couch slogan change tray connect frame token slight zone usage sad monkey pyramid change
GNO_CHAIN_ID=dev
GNO_RPC_ADDR=http://127.0.0.1:26657
GNO_REALM_PATH=gno.land/r/teritori/ghverify
95 changes: 95 additions & 0 deletions gno_github_agent/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
KEY = MyKey
BASE = teritori
REMOTE = http://127.0.0.1:26657
CHAIN_ID = dev

.PHONY: add_social_feeds_realm add_utf16_pkg add_ujson_pkg add_flags_index_pkg add_dao_interfaces_pkg add_social_feed all

add_gh_verify_realm:
gnokey maketx addpkg \
-deposit="1ugnot" \
-gas-fee="1ugnot" \
-gas-wanted="50000000" \
-broadcast="true" \
-chainid="${CHAIN_ID}" \
-remote="${REMOTE}" \
-pkgdir="./r/gnoland/ghverify" \
-pkgpath="gno.land/r/${BASE}/ghverify" \
${KEY}

request_verification:
gnokey maketx call \
-pkgpath "gno.land/r/${BASE}/ghverify" \
-func="RequestVerification" \
-gas-fee="1000000ugnot" \
-gas-wanted="3000000" \
-remote="${REMOTE}" \
-chainid="${CHAIN_ID}" \
-broadcast \
-args "villaquiranm" \
MyKey

check_current_feeds:
gnokey query vm/qeval --data 'gno.land/r/${BASE}/ghverify.GnorkleEntrypoint("request")'
-remote="${REMOTE}" \
${KEY}

set_owner:
gnokey maketx call \
-pkgpath "gno.land/r/${BASE}/ghverify" \
-func="SetOwner" \
-gas-fee="1000000ugnot" \
-gas-wanted="3000000" \
-remote="${REMOTE}" \
-chainid="${CHAIN_ID}" \
-broadcast \
-args "g1yr5sts6w9k2arsuk858lh6phegcfvg0y4d0pgf" \
${KEY}

verify:
gnokey maketx call \
-pkgpath "gno.land/r/${BASE}/ghverify" \
-func="GnorkleEntrypoint" \
-gas-fee="1000000ugnot" \
-gas-wanted="3000000" \
-remote="${REMOTE}" \
-chainid="${CHAIN_ID}" \
-broadcast \
-args "ingest,g1pwxuhltfqxcumjmuquuue6y3f2g3f2d0rcq52x,OK" \
${KEY}
relay_verify:
gnokey maketx call \
-pkgpath "gno.land/r/${BASE}/ghverify" \
-func="GnorkleEntrypoint" \
-gas-fee="1000000ugnot" \
-gas-wanted="3000000" \
-remote="${REMOTE}" \
-chainid="${CHAIN_ID}" \
-broadcast \
-args "relay,gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pqgfakcapzvuux7vug9vagyjmvj6k2w2xu723qnue69z9r7wlt9c6zeyxvt8,dd4b4012daddb14fe7587f4bbb5bef5f16d123032e5c6dc718528907c8f71ee267c1a051328d42991fe6f82cdba424efd3866251cf9411c8f38ebb1e144f10ce,ingest,g14vxq5e5pt5sev7rkz2ej438scmxtylnzv5vnkw,OK" \
newkey

check:
gnokey maketx call \
-pkgpath "gno.land/r/${BASE}/ghverify" \
-func="GnorkleEntrypoint" \
-gas-fee="1000000ugnot" \
-gas-wanted="3000000" \
-remote="${REMOTE}" \
-chainid="${CHAIN_ID}" \
-broadcast \
-args "request" \
${KEY}

send:
gnokey maketx send \
-gas-fee="1000000ugnot" \
-gas-wanted="3000000" \
-remote="${REMOTE}" \
-chainid="${CHAIN_ID}" \
-send="100000ugnot" \
-to "g1yr5sts6w9k2arsuk858lh6phegcfvg0y4d0pgf" \
-broadcast \
${KEY}


127 changes: 127 additions & 0 deletions gno_github_agent/clientql/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package clientql

import (
"context"
"fmt"
"io"
"net/http"
"strings"
"time"

"github.com/Khan/genqlient/graphql"
"github.com/TERITORI/gh-verify-agent/db"
"github.com/TERITORI/gh-verify-agent/gnoindexerql"
"github.com/TERITORI/gh-verify-agent/signer"
"go.uber.org/zap"
"gorm.io/gorm"
)

type IndexerQL struct {
gqlClient graphql.Client
db *gorm.DB
logger *zap.SugaredLogger
signer *signer.Signer
}

func New(graphqlEndpoint string, db *gorm.DB, logger *zap.SugaredLogger, gnoSigner *signer.Signer) *IndexerQL {
gqlClient := graphql.NewClient(graphqlEndpoint, nil)
return &IndexerQL{gqlClient: gqlClient, db: db, logger: logger, signer: gnoSigner}
}

func (client *IndexerQL) DealWithVerifications() error {
lastBlock, err := getLastTreatedBlock()
if err != nil {
return err
}

validationRequests, err := gnoindexerql.GetValidationRequests(context.Background(), client.gqlClient, lastBlock)
if err != nil {
return err
}
client.logger.Infof("validation requests: %d\n", len(validationRequests.Transactions))

for _, validationRequest := range validationRequests.Transactions {
for _, responseEvent := range validationRequest.Response.Events {
switch event := responseEvent.(type) {
case *gnoindexerql.GetValidationRequestsTransactionsTransactionResponseEventsGnoEvent:
client.logger.Infof("args %v\n", event.Attrs)
err := client.dealWithVerification(event)
if err != nil {
client.logger.Errorf("failed to deal with verification: %s", err.Error())
continue
}

default:
client.logger.Errorf("unexpected event type: %T", event)
}
}
}

return nil
}

func getLastTreatedBlock() (int, error) {
return 0, nil
}

func (client *IndexerQL) dealWithVerification(event *gnoindexerql.GetValidationRequestsTransactionsTransactionResponseEventsGnoEvent) error {
var handle string
var callerAddress string
for _, attr := range event.Attrs {
if attr.Key == "handle" {
handle = attr.Value
}
if attr.Key == "from" {
callerAddress = attr.Value
}
}
var verification db.Verification
err := client.db.Model(&db.Verification{}).Where("handle = ? AND address = ?", handle, callerAddress).Find(&verification).Error
if err != nil {
return err
}

if verification.Status == "verified" {
// Already verified.
return nil
}

client.logger.Infof("handle: %s, callerAddress: %s\n", handle, callerAddress)
res, err := http.DefaultClient.Get(fmt.Sprintf("https://raw.githubusercontent.com/%s/.gno/main/config.yml?version=1", handle))
if err != nil {
return err
}

client.logger.Infof("Get\n")
defer res.Body.Close()
if res.StatusCode != 200 {
return client.updateVerification(handle, callerAddress, "config_not_found")
}
data, err := io.ReadAll(res.Body)
if err != nil {
client.updateVerification(handle, callerAddress, "invalid_data")
return err
}
client.logger.Infof("config.yml: %s\n", string(data))
githubConfiguredAddress := strings.TrimSpace(string(data))
if githubConfiguredAddress == callerAddress {
err = client.signer.CallVerify(githubConfiguredAddress)
if err != nil {
return err
}

return client.updateVerification(handle, callerAddress, "verified")
}
return client.updateVerification(handle, callerAddress, "caller_address_mismatch")
}

func (client *IndexerQL) updateVerification(handle, address, status string) error {
verification := db.Verification{
Handle: handle,
Address: address,
Status: status,
CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
}

return client.db.Model(&verification).Where("handle = ? AND address = ?", handle, address).Assign(db.Verification{Status: status}).FirstOrCreate(&verification).Error
}
36 changes: 36 additions & 0 deletions gno_github_agent/db/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package db

import (
"fmt"

"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

func New() (*gorm.DB, error) {
db, err := gorm.Open(sqlite.Open("github.db"), &gorm.Config{})
if err != nil {
return nil, fmt.Errorf("open sqlite db:%w", err)
}

err = db.AutoMigrate(allModels...)
if err != nil {
return nil, fmt.Errorf("migrate sqlite db:%w", err)
}

return db, nil
}

var allModels = []interface{}{
&Verification{},
}

type Verification struct {
gorm.Model
Id uint `json:"id" gorm:"unique;primaryKey;autoIncrement"`

Handle string
Address string
Status string
CreatedAt string
}
22 changes: 22 additions & 0 deletions gno_github_agent/gnoindexerql/genqlient.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
schema: indexer-schema.graphql
operations:
- indexer-operations.graphql
generated: gnoindexerQL.go

package: gnoindexerql

# We bind github's DateTime scalar type to Go's time.Time (which conveniently
# already defines MarshalJSON and UnmarshalJSON). This means genqlient will
# use time.Time when a query requests a DateTime, and is required for custom
# scalars.
bindings:
DateTime:
type: time.Time
DateTimeUtc:
type: string
I64:
type: string
PublicKey:
type: string
U64:
type: string
Loading

0 comments on commit 603966c

Please sign in to comment.