Skip to content

Commit

Permalink
Update example code
Browse files Browse the repository at this point in the history
Signed-off-by: Bashorun97 <[email protected]>
  • Loading branch information
Bashorun97 committed Jun 7, 2024
1 parent fea918c commit c040074
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.gitpod.yaml
.env
.env
decoded_qrcode.png
43 changes: 14 additions & 29 deletions connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (

var (
IOS_APP_CLIP_BASE_URL = "https://appclip.apple.com/id?p=network.gandalf.connect.Clip"
ANDROID_APP_CLIP_BASE_URL = os.Getenv("ANDROID_APP_CLIP_BASE_URL")
ANDROID_APP_CLIP_BASE_URL = os.Getenv("ANDROID_APP_CLIP_BASE_URL")
UNIVERSAL_APP_CLIP_BASE_URL = os.Getenv("UNIVERSAL_APP_CLIP_BASE_URL")
SAURON_BASE_URL = os.Getenv("SAURON_BASE_URL")
SAURON_BASE_URL = "https://sauron.gandalf.network/public/gql"
)


Expand All @@ -38,23 +38,15 @@ func (e *GandalfError) Error() string {
}


type Connect struct {
PublicKey string
RedirectURL string
Platform PlatformType
VerificationStatus bool
Data InputData
}

func NewConnect(publicKey string, redirectURL string, platform PlatformType, data InputData) (*Connect, error) {
if publicKey == "" || redirectURL == "" {
func NewConnect(config Config) (*Connect, error) {
if config.PublicKey == "" || config.RedirectURL == "" {
return nil, fmt.Errorf("invalid parameters")
}

if platform == "" {
platform = PlatformTypeIOS
if config.Platform == "" {
config.Platform = PlatformTypeIOS
}
return &Connect{PublicKey: publicKey, RedirectURL: redirectURL, Data: data}, nil
return &Connect{PublicKey: config.PublicKey, RedirectURL: config.RedirectURL, Data: config.Data, Platform: config.Platform}, nil
}

func (c *Connect) GenerateURL() (string, error) {
Expand All @@ -75,15 +67,15 @@ func (c *Connect) GenerateURL() (string, error) {
return url, nil
}

func (c *Connect) GenerateQRCode(input InputData) (string, error) {
if input == nil {
func (c *Connect) GenerateQRCode() (string, error) {
if c.Data == nil {
return "", &GandalfError{
Message: "Invalid input parameters",
Code: QRCodeGenNotSupported,
}
}

services, err := runValidation(c.PublicKey, c.RedirectURL, input, c.VerificationStatus)
services, err := runValidation(c.PublicKey, c.RedirectURL, c.Data, c.VerificationStatus)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -310,19 +302,12 @@ func (c *Connect) encodeComponents(data, redirectUrl string, publicKey string) (
}

base64Data := base64.StdEncoding.EncodeToString([]byte(data))
parsedURL, err := url.Parse(baseURL)
if err != nil {
return "", fmt.Errorf("error parsing base URL: %w", err)
}

query := parsedURL.Query()
query.Set("publicKey", publicKey)
query.Set("redirectUrl", redirectUrl)
query.Set("data", base64Data)
encodedServices := url.QueryEscape(string(base64Data))
encodedRedirectURL := url.QueryEscape(redirectUrl)
encodedPublicKey := url.QueryEscape(publicKey)

parsedURL.RawQuery = query.Encode()

return parsedURL.String(), nil
return fmt.Sprintf("%s&services=%s&redirectUrl=%s&publicKey=%s", baseURL, encodedServices, encodedRedirectURL, encodedPublicKey), nil
}

func servicesToJSON(services InputData) []byte {
Expand Down
32 changes: 29 additions & 3 deletions connect/example/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package main

import (
"os"
"fmt"
"log"

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

const publicKey = "0x036518f1c7a10fc77f835becc0aca9916c54505f771c82d87dd5943bb01ba5ca08";
Expand All @@ -18,8 +21,14 @@ func main() {
Activities: []string{"watch"},
},
}

config := connect.Config{
PublicKey: publicKey,
RedirectURL: redirectURL,
Data: services,
}

conn, err := connect.NewConnect(publicKey, redirectURL, connect.PlatformTypeIOS, services)
conn, err := connect.NewConnect(config)
if err != nil {
log.Fatalf("An error occurred with initializing connect: %v", err)
}
Expand All @@ -32,11 +41,28 @@ func main() {
fmt.Println("URL => ", url)


qrCodeURL, err := conn.GenerateQRCode(services)
qrCode, err := conn.GenerateQRCode()
if err != nil {
log.Fatalf("An error occurred generating QR Code url: %v", err)
}
fmt.Println("QR Code URL => ", qrCodeURL)
fmt.Println("Base64 QR Code => ", qrCode)

var base64QRCodeWithPrefix string
if strings.HasPrefix(qrCode, "data:image/png;base64,") {
base64QRCodeWithPrefix = strings.TrimPrefix(qrCode, "data:image/png;base64,")
}

decodedQRCode, err := base64.StdEncoding.DecodeString(base64QRCodeWithPrefix)
if err != nil {
log.Fatalf("Failed to decode base64 QR code: %v", err)
}

outputFile := "decoded_qrcode.png"
err = os.WriteFile(outputFile, decodedQRCode, 0644)
if err != nil {
log.Fatalf("Failed to save decoded QR code as PNG: %v", err)
}

// Notify the user that the QR code has been saved
fmt.Printf("Decoded QR code saved to %s\n", outputFile)
}
16 changes: 16 additions & 0 deletions connect/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package connect


type Connect struct {
PublicKey string
RedirectURL string
Platform PlatformType
VerificationStatus bool
Data InputData
}

type Config struct {
PublicKey string
RedirectURL string
Platform PlatformType
Data InputData
}

type PlatformType string

const (
Expand Down

0 comments on commit c040074

Please sign in to comment.