Skip to content

Commit

Permalink
automatically create instant.json if it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
dreacot committed Oct 11, 2024
1 parent aea2803 commit 7e4f828
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions libwallet/instantswap/instantswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
_ "embed"

Check failure on line 5 in libwallet/instantswap/instantswap.go

View workflow job for this annotation

GitHub Actions / Build

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)

Check warning on line 5 in libwallet/instantswap/instantswap.go

View workflow job for this annotation

GitHub Actions / Build

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)

Check failure on line 5 in libwallet/instantswap/instantswap.go

View workflow job for this annotation

GitHub Actions / Build

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)

Check warning on line 5 in libwallet/instantswap/instantswap.go

View workflow job for this annotation

GitHub Actions / Build

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"
Expand All @@ -24,7 +27,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 +35,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 +69,52 @@ 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: %w", exampleFile, instantFile, err.Error())

Check failure on line 82 in libwallet/instantswap/instantswap.go

View workflow job for this annotation

GitHub Actions / Build

printf: decred.org/dcrwallet/v4/errors.Errorf format %w has arg err.Error() of wrong type string (govet)

Check failure on line 82 in libwallet/instantswap/instantswap.go

View workflow job for this annotation

GitHub Actions / Build

printf: decred.org/dcrwallet/v4/errors.Errorf format %w has arg err.Error() of wrong type string (govet)

Check failure on line 82 in libwallet/instantswap/instantswap.go

View workflow job for this annotation

GitHub Actions / Build

printf: decred.org/dcrwallet/v4/errors.Errorf format %w has arg err.Error() of wrong type string (govet)

Check failure on line 82 in libwallet/instantswap/instantswap.go

View workflow job for this annotation

GitHub Actions / Build

printf: decred.org/dcrwallet/v4/errors.Errorf format %w has arg err.Error() of wrong type string (govet)
}
fmt.Println("instant.json created from instant_example.json")
} else if err != nil {
return errors.Errorf("failed to check if %s exists: %w", instantFile, err.Error())

Check failure on line 86 in libwallet/instantswap/instantswap.go

View workflow job for this annotation

GitHub Actions / Build

printf: decred.org/dcrwallet/v4/errors.Errorf format %w has arg err.Error() of wrong type string (govet)

Check failure on line 86 in libwallet/instantswap/instantswap.go

View workflow job for this annotation

GitHub Actions / Build

printf: decred.org/dcrwallet/v4/errors.Errorf format %w has arg err.Error() of wrong type string (govet)

Check failure on line 86 in libwallet/instantswap/instantswap.go

View workflow job for this annotation

GitHub Actions / Build

printf: decred.org/dcrwallet/v4/errors.Errorf format %w has arg err.Error() of wrong type string (govet)

Check failure on line 86 in libwallet/instantswap/instantswap.go

View workflow job for this annotation

GitHub Actions / Build

printf: decred.org/dcrwallet/v4/errors.Errorf format %w has arg err.Error() of wrong type string (govet)
}

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 {
// Assuming the code is in "libwallet/instantswap", adjust path accordingly
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 7e4f828

Please sign in to comment.