Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optional wallet path in env file #70

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .env.wallet.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# mint to connect to
MINT_HOST=127.0.0.1
MINT_PORT=3338
#MINT_URL=http://localhost:3338
MINT_URL=http://127.0.0.1:3338

# Path to store wallet (optional). If not specified, defaults to $HOME/.gonuts/wallet
# WALLET_PATH=<some_path>
94 changes: 49 additions & 45 deletions cmd/nutw/nutw.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
"net/url"
"os"
Expand All @@ -25,38 +26,55 @@ import (

var nutw *wallet.Wallet

func walletConfig() wallet.Config {
path := setWalletPath()
// default config
config := wallet.Config{WalletPath: path, CurrentMintURL: "http://127.0.0.1:3338"}
env := envPath(path)

func walletConfig() (wallet.Config, error) {
env := envPath()
if len(env) > 0 {
err := godotenv.Load(env)
if err == nil {
config.CurrentMintURL = getMintURL()
if err != nil {
// if no .env file to load, use default
return wallet.Config{
WalletPath: defaultWalletPath(),
CurrentMintURL: "http://127.0.0.1:3338",
}, nil
}
}

walletPath := os.Getenv("WALLET_PATH")
if len(walletPath) > 0 {
if !fs.ValidPath(walletPath) {
return wallet.Config{}, fmt.Errorf("invalid WALLET_PATH")
}
} else {
walletPath = defaultWalletPath()
}
if err := os.MkdirAll(walletPath, 0700); err != nil {
return wallet.Config{}, fmt.Errorf("could not create wallet directory: %v", err)
}

return config
mint := os.Getenv("MINT_URL")
if len(mint) == 0 {
mint = "http://127.0.0.1:3338"
}
config := wallet.Config{WalletPath: walletPath, CurrentMintURL: mint}

return config, nil
}

func setWalletPath() string {
func defaultWalletPath() string {
homedir, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}

path := filepath.Join(homedir, ".gonuts", "wallet")
err = os.MkdirAll(path, 0700)
if err != nil {
log.Fatal(err)
}
return path
return filepath.Join(homedir, ".gonuts", "wallet")
}

func envPath(walletPath string) string {
envPath := filepath.Join(walletPath, ".env")
func envPath() string {
defaultPath := defaultWalletPath()

// if .env file present at default wallet path then use that
// if not, look at current dir
envPath := filepath.Join(defaultPath, ".env")
if _, err := os.Stat(envPath); err != nil {
wd, err := os.Getwd()
if err != nil {
Expand All @@ -68,30 +86,12 @@ func envPath(walletPath string) string {
return envPath
}

func getMintURL() string {
mintUrl := os.Getenv("MINT_URL")
if len(mintUrl) > 0 {
return mintUrl
} else {
mintHost := os.Getenv("MINT_HOST")
mintPort := os.Getenv("MINT_PORT")
if len(mintHost) == 0 || len(mintPort) == 0 {
return "http://127.0.0.1:3338"
}

url := &url.URL{
Scheme: "http",
Host: mintHost + ":" + mintPort,
}
mintUrl = url.String()
}
return mintUrl
}

func setupWallet(ctx *cli.Context) error {
config := walletConfig()
config, err := walletConfig()
if err != nil {
printErr(err)
}

var err error
nutw, err = wallet.LoadWallet(config)
if err != nil {
printErr(err)
Expand Down Expand Up @@ -511,7 +511,10 @@ var restoreCmd = &cli.Command{
}

func restore(ctx *cli.Context) error {
config := walletConfig()
config, err := walletConfig()
if err != nil {
printErr(err)
}
fmt.Printf("enter mnemonic: ")

reader := bufio.NewReader(os.Stdin)
Expand Down Expand Up @@ -545,7 +548,10 @@ var currentMintCmd = &cli.Command{
}

func currentMint(ctx *cli.Context) error {
config := walletConfig()
config, err := walletConfig()
if err != nil {
printErr(err)
}
fmt.Printf("current mint: %v\n", config.CurrentMintURL)
return nil
}
Expand All @@ -561,9 +567,7 @@ func setCurrentMint(ctx *cli.Context) error {
printErr(fmt.Errorf("invalid mint url: %v", err))
}

path := setWalletPath()
envFilePath := envPath(path)

envFilePath := envPath()
envFileData, err := os.ReadFile(envFilePath)
if err != nil {
printErr(fmt.Errorf("could not read .env file: %v", err))
Expand Down
Loading