diff --git a/internal/cli/server/config.go b/internal/cli/server/config.go index a5e255ad48..e74964606e 100644 --- a/internal/cli/server/config.go +++ b/internal/cli/server/config.go @@ -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 @@ -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 +} diff --git a/internal/cli/server/config_test.go b/internal/cli/server/config_test.go index 6f5f9ff97d..4e6f3ef499 100644 --- a/internal/cli/server/config_test.go +++ b/internal/cli/server/config_test.go @@ -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) + }) +} diff --git a/internal/cli/server/testdata/password.txt b/internal/cli/server/testdata/password.txt new file mode 100644 index 0000000000..1827ffa521 --- /dev/null +++ b/internal/cli/server/testdata/password.txt @@ -0,0 +1,2 @@ +test1 +test2 \ No newline at end of file