-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 Remove wallet linking from auth to seperate command 'address'
- Loading branch information
Showing
2 changed files
with
90 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters