Skip to content

Commit

Permalink
Connect example
Browse files Browse the repository at this point in the history
Signed-off-by: Bashorun97 <[email protected]>
  • Loading branch information
Bashorun97 committed Jun 5, 2024
1 parent b84799c commit d7bb170
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 17 deletions.
44 changes: 30 additions & 14 deletions connect/main.go → connect/connect.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package connect

import (
"context"
Expand Down Expand Up @@ -103,7 +103,7 @@ func validatePublicKey(publicKey string) bool {
return publicKeyRequest(publicKey)
}

func getSupportedServices() interface{} {
func getSupportedServices() []Value {
gqlSchema := introspectSauron()
for _, val := range gqlSchema.Schema.Types {
if val.Kind == "ENUM" && val.Name == "Source" {
Expand All @@ -114,10 +114,10 @@ func getSupportedServices() interface{} {
}

func validateInputServices(input Services) (map[string]Service, error) {
supportedServicesInterface := getSupportedServices()
supportedServices := supportedServicesInterface.(SupportedServices)
// supportedServicesInterface := getSupportedServices()
supportedServices := getSupportedServices()

serviceMap := make(map[string]SupportedService)
serviceMap := make(map[string]Value)
for _, val := range supportedServices {
serviceMap[val.Name] = val
}
Expand Down Expand Up @@ -159,33 +159,49 @@ func validateInputServices(input Services) (map[string]Service, error) {
func publicKeyRequest(publicKey string) bool {
graphqlRequest := graphqlClient.NewRequest(`
query GetAppByPublicKey($publicKey: String!) {
getAppByPublicKey(publicKey: $publicKey) {
appName
gandalfID
getAppByPublicKey(
publicKey: $publicKey
) {
appName
gandalfID
}
}
`)

graphqlRequest.Var("publicKey", publicKey)

graphqlRequest.Var("publicKey", publicKey)
client := graphqlClient.NewClient(SAURON_BASE_URL)

ctx := context.Background()

var respData Application
var graphqlResponse map[string]interface{}

if err := client.Run(ctx, graphqlRequest, &respData); err != nil {
log.Fatalf("Error making introspection query: %v", err)
if err := client.Run(ctx, graphqlRequest, &graphqlResponse); err != nil {
log.Fatalf("Error making publicKey request query: %v", err)
}

responseData, ok := graphqlResponse["getAppByPublicKey"].(map[string]interface{})
if !ok {
log.Fatalf("Unexpected response structure: %v", graphqlResponse)
}

body, err := json.Marshal(responseData)
if err != nil {
return false
}

var respData Application
err = json.Unmarshal(body, &respData)
if err != nil {
return false
}
return respData.GandalfID > 0
}

func runValidation(publicKey string, redirectURL string, input Services) (map[string]Service, error) {
isPublicKeyValid := validatePublicKey(publicKey)
if !isPublicKeyValid {
return nil, &GandalfError{
Message: "Public key does not exist",
Message: "Invalid public key",
Code: InvalidPublicKey,
}
}
Expand Down
36 changes: 36 additions & 0 deletions connect/example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"log"

"github.com/gandalf-network/gandalf-sdk-go/connect"
)

const publicKey = "0x036518f1c7a10fc77f835becc0aca9916c54505f771c82d87dd5943bb01ba5ca08";
const redirectURL = "https://example.com"


func main() {
qrCodeServices := connect.Services{
{Name: "Netflix", Status: true},
{Name: "Playstation", Status: false},
}
qrCodeURL, err := connect.GenerateQRCode(publicKey, redirectURL, qrCodeServices)
if err != nil {
log.Fatalf("An error occurred generating QR Code url: %v", err)
}

fmt.Println("QR Code URL => ", qrCodeURL)

services := connect.Services{
{Name: "Netflix", Status: false},
{Name: "Playstation", Status: true},
}
url, err := connect.GenerateURL(publicKey, redirectURL, services)
if err != nil {
log.Fatalf("An error occurred url: %v", err)
}

fmt.Println("URL => ", url)
}
Empty file removed connect/main_test.go
Empty file.
18 changes: 15 additions & 3 deletions connect/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package connect


type GandalfErrorCode int
Expand All @@ -10,8 +10,20 @@ type GandalfError struct {
}

type Application struct {
GandalfID int64
// The human-readable name of the application.
AppName string `json:"appName"`
// A public key associated with the application, used for cryptographic operations such as
// verifying the identity of the application.
PublicKey string `json:"publicKey"`
// The URL pointing to the icon graphic for the application. This URL should link to an image
// that visually represents the application, aiding in its identification and branding.
IconURL string `json:"iconURL"`
// A unique identifier assigned to the application upon registration.
GandalfID int64 `json:"gandalfID"`
// The address of the user who registered the application.
AppRegistrar string `json:"appRegistrar"`
}
// type Application map[string]interface{}

type SupportedService struct {
Name string `json:"name"`
Expand All @@ -20,7 +32,7 @@ type SupportedService struct {
DeprecationReason string `json:"deprecationReason"`
}

type SupportedServices []SupportedService
type SupportedServices []Value

type Service struct {
Name string
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.1

require (
github.com/btcsuite/btcd/btcec/v2 v2.3.3
github.com/davecgh/go-spew v1.1.1
github.com/gandalf-network/genqlient v1.0.2
github.com/google/uuid v1.6.0
github.com/pkg/errors v0.9.1
Expand Down

0 comments on commit d7bb170

Please sign in to comment.