Skip to content

Commit

Permalink
Merge pull request #15 from okp4/feat/resolve-identity
Browse files Browse the repository at this point in the history
Feat/resolve identity
  • Loading branch information
amimart authored Nov 26, 2022
2 parents a240a38 + aafde00 commit cf9003e
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 39 deletions.
4 changes: 3 additions & 1 deletion app/actor/graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package graphql
import (
"context"

"okp4/nemeton-leaderboard/app/keybase"

"okp4/nemeton-leaderboard/app/nemeton"
"okp4/nemeton-leaderboard/graphql"
"okp4/nemeton-leaderboard/graphql/generated"
Expand All @@ -18,7 +20,7 @@ func NewGraphQLServer(ctx context.Context, mongoURI, db string) (*handler.Server

return handler.NewDefaultServer(
generated.NewExecutableSchema(
generated.Config{Resolvers: graphql.NewResolver(store)},
generated.Config{Resolvers: graphql.NewResolver(store, keybase.NewClient())},
),
), nil
}
77 changes: 77 additions & 0 deletions app/keybase/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package keybase

import (
"context"
"fmt"
"net/http"
"net/url"
"time"

"github.com/go-resty/resty/v2"
"github.com/patrickmn/go-cache"
)

type Client struct {
client *resty.Client
picturesCache *cache.Cache
}

func NewClient() *Client {
return &Client{
client: resty.New(),
picturesCache: cache.New(time.Hour, time.Hour),
}
}

type lookupPayload struct {
Them []struct {
Pictures *struct {
Primary *struct {
URL *string
}
}
}
}

func (c *Client) LookupPicture(ctx context.Context, kid string) (*url.URL, error) {
if picture, ok := c.picturesCache.Get(kid); ok {
return picture.(*url.URL), nil
}

lookup, err := c.lookup(ctx, kid)
if err != nil {
return nil, err
}

var picture *url.URL
if len(lookup.Them) > 0 &&
lookup.Them[0].Pictures != nil &&
lookup.Them[0].Pictures.Primary != nil &&
lookup.Them[0].Pictures.Primary.URL != nil {
picture, err = url.Parse(*lookup.Them[0].Pictures.Primary.URL)
if err != nil {
return nil, err
}
c.picturesCache.SetDefault(kid, picture)
}
return picture, err
}

func (c *Client) lookup(ctx context.Context, kid string) (*lookupPayload, error) {
var lookup lookupPayload
res, err := c.client.R().
SetContext(ctx).
SetHeader("Accept", "application/json").
SetQueryParam("key_suffix", kid).
SetQueryParam("fields", "pictures").
SetResult(&lookup).
Get("https://keybase.io/_/api/1.0/user/lookup.json")
if err != nil {
return nil, err
}

if res.StatusCode() != http.StatusOK {
return nil, fmt.Errorf("got non 200 http code: '%d'", res.StatusCode())
}
return &lookup, nil
}
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ go 1.19
require (
github.com/99designs/gqlgen v0.17.20
github.com/asynkron/protoactor-go v0.0.0-20221002142108-880b460fcd1f
github.com/cosmos/btcutil v1.0.4
github.com/cosmos/cosmos-sdk v0.46.4
github.com/go-resty/resty/v2 v2.7.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/rs/zerolog v1.28.0
github.com/smartystreets/goconvey v1.7.2
github.com/spf13/cobra v1.6.1
github.com/tendermint/tendermint v0.34.22
github.com/vektah/gqlparser/v2 v2.5.1
go.mongodb.org/mongo-driver v1.11.0
google.golang.org/grpc v1.50.0
Expand All @@ -31,7 +35,6 @@ require (
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/confio/ics23/go v0.7.0 // indirect
github.com/cosmos/btcutil v1.0.4 // indirect
github.com/cosmos/cosmos-proto v1.0.0-alpha7 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
Expand Down Expand Up @@ -109,7 +112,6 @@ require (
github.com/tendermint/btcd v0.1.1 // indirect
github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/tendermint/tendermint v0.34.22 // indirect
github.com/tendermint/tm-db v0.6.7 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0=
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4=
Expand Down Expand Up @@ -438,6 +440,8 @@ github.com/orcaman/concurrent-map v1.0.0 h1:I/2A2XPCb4IuQWcQhBhSwGfiuybl/J0ev9HD
github.com/orcaman/concurrent-map v1.0.0/go.mod h1:Lu3tH6HLW3feq74c2GC+jIMS/K2CFcDWnWD9XkenwhI=
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
Expand Down Expand Up @@ -710,6 +714,7 @@ golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
Expand Down
4 changes: 2 additions & 2 deletions gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ models:
ValoperAddress:
model:
- okp4/nemeton-leaderboard/graphql/scalar.ValoperAddress
PGPKeyID:
KID:
model:
- github.com/99designs/gqlgen/graphql.Uint64
- okp4/nemeton-leaderboard/graphql/scalar.KID
URI:
model:
- okp4/nemeton-leaderboard/graphql/scalar.URI
Expand Down
46 changes: 23 additions & 23 deletions graphql/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion graphql/model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions graphql/resolver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package graphql

import (
"okp4/nemeton-leaderboard/app/keybase"
"okp4/nemeton-leaderboard/app/nemeton"
)

Expand All @@ -9,11 +10,13 @@ import (
// It serves as dependency injection for your app, add any dependencies you require here.

type Resolver struct {
store *nemeton.Store
store *nemeton.Store
keybaseClient *keybase.Client
}

func NewResolver(store *nemeton.Store) *Resolver {
func NewResolver(store *nemeton.Store, keybaseClient *keybase.Client) *Resolver {
return &Resolver{
store: store,
store: store,
keybaseClient: keybaseClient,
}
}
34 changes: 34 additions & 0 deletions graphql/scalar/kid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package scalar

import (
"encoding/hex"
"fmt"
"io"

"github.com/99designs/gqlgen/graphql"
)

func UnmarshalKID(v interface{}) (string, error) {
kid, ok := v.(string)
if !ok {
return "", fmt.Errorf("expect type 'string' and got type '%T'", v)
}

raw, err := hex.DecodeString(kid)
if err != nil {
return "", err
}
if len(raw) != 35 {
return "", fmt.Errorf("expect a 35 bytes hex encoded key")
}

return kid, nil
}

func MarshalKID(kid string) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = w.Write([]byte(`"`))
_, _ = w.Write([]byte(kid))
_, _ = w.Write([]byte(`"`))
})
}
1 change: 0 additions & 1 deletion graphql/scalar/pgp.go

This file was deleted.

Loading

0 comments on commit cf9003e

Please sign in to comment.