Skip to content

Commit

Permalink
automatically create instant.json if it doesn't exist (#681)
Browse files Browse the repository at this point in the history
  • Loading branch information
dreacot authored Oct 23, 2024
1 parent 8380273 commit d5a6cac
Showing 1 changed file with 61 additions and 3 deletions.
64 changes: 61 additions & 3 deletions libwallet/instantswap/instantswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package instantswap

import (
"context"
_ "embed"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"
Expand All @@ -24,7 +26,6 @@ import (
_ "github.com/crypto-power/instantswap/instantswap/exchange/trocador"
)

//go:embed instant.json
var instants []byte
var privKeyMap = map[Server]string{
Trocador: "",
Expand All @@ -33,9 +34,21 @@ var privKeyMap = map[Server]string{
}

func init() {
// Call checkAndCreateInstantJSON to ensure instant.json is available
if err := checkAndCreateInstantJSON(); err != nil {
panic(errors.Errorf("Error setting up instant.json: %s", err.Error()))
}

// Load the instant.json content into the instants variable
instantFilePath := getFilePath("instant.json") // Ensure correct path is used for reading
instants, err := os.ReadFile(instantFilePath)
if err != nil {
panic(errors.Errorf("Error reading instant.json: %s", err.Error()))
}

// init private key map and ensure only supported exchange is filled
var newPrivKeyMap = make(map[Server]string)
err := json.Unmarshal(instants, &newPrivKeyMap)
err = json.Unmarshal(instants, &newPrivKeyMap)
if err != nil {
panic(err)
}
Expand All @@ -55,6 +68,51 @@ func init() {
privKeyMap[FlypMe] = ""
}

// checkAndCreateInstantJSON checks if instant.json exists, and if not, copies instant_example.json to instant.json.
func checkAndCreateInstantJSON() error {
exampleFile := getFilePath("instant_example.json")
instantFile := getFilePath("instant.json")

// Check if instant.json exists
if _, err := os.Stat(instantFile); os.IsNotExist(err) {
// If instant.json doesn't exist, copy from instant_example.json
err := copyFile(exampleFile, instantFile)
if err != nil {
return errors.Errorf("failed to copy %s to %s: %s", exampleFile, instantFile, err.Error())
}
fmt.Println("instant.json created from instant_example.json")
} else if err != nil {
return errors.Errorf("failed to check if %s exists: %s", instantFile, err.Error())
}

return nil
}

// copyFile copies a file from src to dst.
func copyFile(src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()

destFile, err := os.Create(dst)
if err != nil {
return err
}
defer destFile.Close()

_, err = io.Copy(destFile, sourceFile)
return err
}

// getFilePath constructs the file path relative to the current directory.
func getFilePath(fileName string) string {
basePath, _ := os.Getwd() // Get current working directory
filePath := filepath.Join(basePath, "libwallet", "instantswap", fileName)
return filePath
}

func GetInstantExchangePrivKey(server Server) (string, bool) {
key, ok := privKeyMap[server]
return key, ok
Expand Down

0 comments on commit d5a6cac

Please sign in to comment.