Skip to content

Commit

Permalink
Merge pull request #403 from cffls/v0.3.0-dev
Browse files Browse the repository at this point in the history
Implement and enable 'unlock' flag
  • Loading branch information
cffls authored May 18, 2022
2 parents 30641c0 + 8de6903 commit cd1a899
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
32 changes: 32 additions & 0 deletions internal/cli/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,25 @@ func (c *Config) buildEth(stack *node.Node) (*ethconfig.Config, error) {
}
}

// unlock accounts
if len(c.Accounts.Unlock) > 0 {
if !stack.Config().InsecureUnlockAllowed && stack.Config().ExtRPCEnabled() {
return nil, fmt.Errorf("Account unlock with HTTP access is forbidden!")
}
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
passwords, err := MakePasswordListFromFile(c.Accounts.PasswordFile)
if err != nil {
return nil, err
}
if len(passwords) < len(c.Accounts.Unlock) {
return nil, fmt.Errorf("Number of passwords provided (%v) is less than number of accounts (%v) to unlock",
len(passwords), len(c.Accounts.Unlock))
}
for i, account := range c.Accounts.Unlock {
ks.Unlock(accounts.Account{Address: common.HexToAddress(account)}, passwords[i])
}
}

// update for developer mode
if c.Developer.Enabled {
// Get a keystore
Expand Down Expand Up @@ -990,3 +1009,16 @@ func Hostname() string {
}
return hostname
}

func MakePasswordListFromFile(path string) ([]string, error) {
text, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("Failed to read password file: %v", err)
}
lines := strings.Split(string(text), "\n")
// Sanitise DOS line endings.
for i := range lines {
lines[i] = strings.TrimRight(lines[i], "\r")
}
return lines, nil
}
7 changes: 7 additions & 0 deletions internal/cli/server/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,10 @@ func TestConfigBootnodesDefault(t *testing.T) {
assert.Len(t, cfg.P2P.BootstrapNodes, 1)
})
}

func TestMakePasswordListFromFile(t *testing.T) {
t.Run("ReadPasswordFile", func(t *testing.T) {
result, _ := MakePasswordListFromFile("./testdata/password.txt")
assert.Equal(t, []string{"test1", "test2"}, result)
})
}
2 changes: 2 additions & 0 deletions internal/cli/server/testdata/password.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test1
test2

0 comments on commit cd1a899

Please sign in to comment.