Skip to content

Commit

Permalink
🎨 Remove wallet linking from auth to seperate command 'address'
Browse files Browse the repository at this point in the history
  • Loading branch information
beskay committed Oct 17, 2023
1 parent dfbdace commit e8610cc
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 49 deletions.
87 changes: 87 additions & 0 deletions cmd/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package cmd

import (
"bytes"
"fmt"
"github.com/ethernautdao/evm-runners-cli/internal/utils"
"github.com/spf13/cobra"
"net/http"
)

// addressCmd represents the address command
var addressCmd = &cobra.Command{
Use: "address [address]",
Short: "Link your Optimism address to your account",
Long: `Link your Optimism address to your account.
Linking your wallet address enables submissions from the website.
Additionally, you will receive NFTs on Optimism for completing levels,
showcasing your skills.
To update your wallet address, simply run this command again.`,

RunE: func(cmd *cobra.Command, args []string) error {
// load config
config, err := utils.LoadConfig()
if err != nil {
return err
}

fmt.Println("Please enter your Optimism address: ")

// declare address
var address string

if len(args) != 0 {
address = args[0]
} else {
fmt.Scanln(&address)
}

// check if valid ethereum address
if !utils.IsValidEthereumAddress(address) {
return fmt.Errorf("Invalid Optimism address!\n")
}

fmt.Printf("\nLinking Optimism address '%s' to your account...\n\n", address)

err = linkWallet(config, address)
if err != nil {
return fmt.Errorf("failed to link wallet address: %v", err)
} else {
fmt.Printf("Success!\n")
}

return nil
},
}

func init() {
rootCmd.AddCommand(addressCmd)
}

func linkWallet(config utils.Config, address string) error {
// Define the JSON payload
jsonPayload := []byte(fmt.Sprintf(`{"address":"%s"}`, address))

// Make the HTTP request
url := config.EVMR_SERVER + "users/wallet"
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+config.EVMR_TOKEN)

// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error sending the request: %v", err)
}
defer resp.Body.Close()

// Check for errors in the response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("http request failed with status: %s", resp.Status)
}

return nil
}
52 changes: 3 additions & 49 deletions cmd/auth.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"github.com/ethernautdao/evm-runners-cli/internal/utils"
Expand All @@ -26,14 +25,11 @@ type Config struct {
}

var authCmd = &cobra.Command{
Use: "auth <discord | wallet>",
Short: "Authenticate your account or link your wallet address",
Long: `Authenticate your account or link your wallet address.
Use: "auth discord",
Short: "Authenticate your account with Discord",
Long: `Authenticate your account with Discord.
Currently, Discord is the only available platform for authentication.
Linking your wallet address enables submissions from the website. To link it, run
'evmr auth wallet' and enter your wallet address.
If your Discord username has changed and you want to update it, run
'evmr auth discord' again.`,

Expand Down Expand Up @@ -69,22 +65,6 @@ If your Discord username has changed and you want to update it, run
}

fmt.Println("\nSuccessfully authenticated with Discord!")
} else if args[0] == ("wallet") || args[0] == ("address") {
fmt.Println("Please enter your wallet address: ")
var address string
fmt.Scanln(&address)

// check if valid ethereum address
if !utils.IsValidEthereumAddress(address) {
return fmt.Errorf("Invalid Ethereum address!\n")
}

fmt.Printf("\nLinking wallet address %s to your account...\n", address)

err := linkWallet(config, address)
if err != nil {
return fmt.Errorf("failed to link wallet address: %v", err)
}
} else {
return fmt.Errorf("Invalid authentication method!\n")
}
Expand Down Expand Up @@ -158,29 +138,3 @@ func authDiscord(config utils.Config) error {

return nil
}

func linkWallet(config utils.Config, address string) error {
// Define the JSON payload
jsonPayload := []byte(fmt.Sprintf(`{"address":"%s"}`, address))

// Make the HTTP request
url := config.EVMR_SERVER + "users/wallet"
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+config.EVMR_TOKEN)

// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error sending the request: %v", err)
}
defer resp.Body.Close()

// Check for errors in the response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("http request failed with status: %s", resp.Status)
}

return nil
}

0 comments on commit e8610cc

Please sign in to comment.