Skip to content

Commit

Permalink
feat: add switch password from file for daemon and gtk
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad committed Dec 22, 2024
1 parent a4aae01 commit 7148bac
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
10 changes: 10 additions & 0 deletions cmd/daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gofrs/flock"
"github.com/pactus-project/pactus/cmd"
"github.com/pactus-project/pactus/util"
"github.com/pactus-project/pactus/wallet"
"github.com/spf13/cobra"
)
Expand All @@ -24,6 +25,9 @@ func buildStartCmd(parentCmd *cobra.Command) {
passwordOpt := startCmd.Flags().StringP("password", "p", "",
"the wallet password")

passwordFromFileOpt := startCmd.Flags().String("password-from-file", "",
"the file containing the wallet password")

Check warning on line 30 in cmd/daemon/start.go

View check run for this annotation

Codecov / codecov/patch

cmd/daemon/start.go#L28-L30

Added lines #L28 - L30 were not covered by tests
startCmd.Run = func(_ *cobra.Command, _ []string) {
workingDir, _ := filepath.Abs(*workingDirOpt)
// change working directory
Expand All @@ -49,8 +53,14 @@ func buildStartCmd(parentCmd *cobra.Command) {
}

var password string

Check warning on line 56 in cmd/daemon/start.go

View check run for this annotation

Codecov / codecov/patch

cmd/daemon/start.go#L56

Added line #L56 was not covered by tests
if *passwordOpt != "" {
password = *passwordOpt
} else if *passwordFromFileOpt != "" {
password, err = util.ReadFileContent(*passwordFromFileOpt)
if err != nil {
return "", false
}

Check warning on line 63 in cmd/daemon/start.go

View check run for this annotation

Codecov / codecov/patch

cmd/daemon/start.go#L59-L63

Added lines #L59 - L63 were not covered by tests
} else {
password = cmd.PromptPassword("Wallet password", false)
}
Expand Down
17 changes: 14 additions & 3 deletions cmd/gtk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ import (
const appID = "com.github.pactus-project.pactus.pactus-gui"

var (
workingDirOpt *string
passwordOpt *string
testnetOpt *bool
workingDirOpt *string
passwordOpt *string
passwordFromFileOpt *string
testnetOpt *bool
)

func init() {
workingDirOpt = flag.String("working-dir", cmd.PactusDefaultHomeDir(), "working directory path")
passwordOpt = flag.String("password", "", "wallet password")
passwordFromFileOpt = flag.String("password-from-file", "", "file containing the wallet password")
testnetOpt = flag.Bool("testnet", false, "initializing for the testnet")
version.NodeAgent.AppType = "gui"

Expand Down Expand Up @@ -153,6 +155,15 @@ func newNode(workingDir string) (*node.Node, *wallet.Wallet, error) {
return *passwordOpt, true
}

if *passwordFromFileOpt != "" {
password, err := util.ReadFileContent(*passwordFromFileOpt)
if err != nil {
return "", false
}

return password, true
}

return getWalletPassword(wlt)
}
n, wlt, err := cmd.StartNode(workingDir, passwordFetcher)
Expand Down
11 changes: 5 additions & 6 deletions util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"
"math/bits"
"os"
"strings"

"golang.org/x/exp/constraints"
)
Expand Down Expand Up @@ -166,7 +167,7 @@ func FormatBytesToHumanReadable(bytes uint64) string {
// ReadFileContent reads the content of the file at the given path
// up to the specified maxSize. It returns the content as a string
// and any error encountered.
func ReadFileContent(filePath string, maxSize int) (string, error) {
func ReadFileContent(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", fmt.Errorf("failed to open file: %w", err)
Expand All @@ -175,12 +176,10 @@ func ReadFileContent(filePath string, maxSize int) (string, error) {
_ = file.Close()
}()

buffer := make([]byte, maxSize)

n, err := file.Read(buffer)
if err != nil && err != io.EOF {
buf, err := io.ReadAll(file)
if err != nil {
return "", fmt.Errorf("failed to read file content: %w", err)
}

Check warning on line 182 in util/utils.go

View check run for this annotation

Codecov / codecov/patch

util/utils.go#L181-L182

Added lines #L181 - L182 were not covered by tests

return string(buffer[:n]), nil
return strings.TrimSpace(string(buf)), nil
}
40 changes: 12 additions & 28 deletions util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,70 +145,54 @@ func TestReadFileContent(t *testing.T) {
testCases := []struct {
name string
fileContent string
maxSize int
expected string
expectErr bool
}{
{
name: "Read full content within maxSize",
name: "Read full content",
fileContent: "Hello, World!",
maxSize: 50,
expected: "Hello, World!",
expectErr: false,
},
{
name: "Read partial content limited by maxSize",
fileContent: "Hello, World!",
maxSize: 5,
expected: "Hello",
expectErr: false,
},
{
name: "Empty file",
fileContent: "",
maxSize: 10,
expected: "",
expectErr: false,
},
{
name: "Zero maxSize",
fileContent: "Content",
maxSize: 0,
expected: "",
expectErr: false,
},
{
name: "Non-existent file",
fileContent: "",
maxSize: 10,
expected: "",
expectErr: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
var filePath string
if tc.fileContent != "" || tc.name == "Empty file" {
if test.fileContent != "" || test.name == "Empty file" {
tmpFile, err := os.CreateTemp(TempDirPath(), "testfile")
assert.NoError(t, err, "Failed to create temp file")
defer os.Remove(tmpFile.Name())
defer func() {
_ = os.Remove(tmpFile.Name())
}()

_, err = tmpFile.WriteString(tc.fileContent)
_, err = tmpFile.WriteString(test.fileContent)
assert.NoError(t, err, "Failed to write to temp file")
tmpFile.Close()
_ = tmpFile.Close()
filePath = tmpFile.Name()
} else {
filePath = "nonexistent_file"
}

content, err := ReadFileContent(filePath, tc.maxSize)
content, err := ReadFileContent(filePath)

if tc.expectErr {
if test.expectErr {
assert.Error(t, err, "Expected an error but got none")
} else {
assert.NoError(t, err, "Unexpected error occurred")
assert.Equal(t, tc.expected, content, "Content does not match expected value")
assert.Equal(t, test.expected, content, "Content does not match expected value")
}
})
}
Expand Down

0 comments on commit 7148bac

Please sign in to comment.