Implement and enable 'unlock' flag

This commit is contained in:
Jerry 2022-05-16 14:54:35 -07:00
parent 30641c06b1
commit 8de6903eee
3 changed files with 41 additions and 0 deletions

View file

@ -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
}

View file

@ -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)
})
}

View file

@ -0,0 +1,2 @@
test1
test2