diff --git a/connect/main.go b/connect/connect.go similarity index 85% rename from connect/main.go rename to connect/connect.go index c8f8dc3..f536916 100644 --- a/connect/main.go +++ b/connect/connect.go @@ -1,4 +1,4 @@ -package main +package connect import ( "context" @@ -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" { @@ -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 } @@ -159,25 +159,41 @@ 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 } @@ -185,7 +201,7 @@ func runValidation(publicKey string, redirectURL string, input Services) (map[st isPublicKeyValid := validatePublicKey(publicKey) if !isPublicKeyValid { return nil, &GandalfError{ - Message: "Public key does not exist", + Message: "Invalid public key", Code: InvalidPublicKey, } } diff --git a/connect/example/main.go b/connect/example/main.go new file mode 100644 index 0000000..d88b343 --- /dev/null +++ b/connect/example/main.go @@ -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) +} \ No newline at end of file diff --git a/connect/main_test.go b/connect/main_test.go deleted file mode 100644 index e69de29..0000000 diff --git a/connect/types.go b/connect/types.go index 704a864..696aa6f 100644 --- a/connect/types.go +++ b/connect/types.go @@ -1,4 +1,4 @@ -package main +package connect type GandalfErrorCode int @@ -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"` @@ -20,7 +32,7 @@ type SupportedService struct { DeprecationReason string `json:"deprecationReason"` } -type SupportedServices []SupportedService +type SupportedServices []Value type Service struct { Name string diff --git a/go.mod b/go.mod index 4940ada..656b70a 100644 --- a/go.mod +++ b/go.mod @@ -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