Is there any way of just unlocking accounts without opening the console with geth?

I'm writing some scripts to automate cluster generation and I wish I could just call geth to unlocking some accounts without starting another instance or attaching to an existing one.


Edit

If I run something like:

geth --genesis "$GENESIS_FILE" --datadir "$DATA_DIR" --networkid "$NETWORK_ID" \
     --password <(echo -n $ACCOUNT_PASSWD) --unlock "$UNLOCK_INDEXES" \
     --exec '""' attach > /dev/null

after the node startup, the accounts are not unlocked.

The only way I've managed to get accounts unlocked was to unlock them when running geth in order to start the node.

up vote 15 down vote accepted

Just to clarify:

  • You would like to run a geth node starting up with the accounts all locked as per the default
  • You later want to run a 'geth attach' command to unlock one or more accounts for a period of time

You can use use the following command to attach to your geth node to unlock an account using the personal.unlockAccount(...) JavaScript API (https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console#personalunlockaccount), where the third parameter is the duration in seconds:

> geth --exec "personal.unlockAccount(eth.accounts[0], 'password', 1000)" attach
true

Or you can create a file, e.g. unlock.js with the following content:

personal.unlockAccount(eth.accounts[0], 'password', 1000);

And then use the following command to unlock the account:

> geth --exec "loadScript('unlock.js')" attach
true

Responding to the question below regarding the default duration, if no duration is specified then a duration of 0 is implied. And a duration of 0 will unlock the account until the program exits, e.g. there is no expiry.

From https://github.com/ethereum/go-ethereum/blob/master/internal/ethapi/api.go#L239-L254 :

// UnlockAccount will unlock the account associated with the given address with
// the given password for duration seconds. If duration is nil it will use a
// default of 300 seconds. It returns an indication if the account was unlocked.
func (s *PrivateAccountAPI) UnlockAccount(addr common.Address, password string, duration *uint64) (bool, error) {
    const max = uint64(time.Duration(math.MaxInt64) / time.Second)
    var d time.Duration
    if duration == nil {
        d = 300 * time.Second
    } else if *duration > max {
        return false, errors.New("unlock duration too large")
    } else {
        d = time.Duration(*duration) * time.Second
    }
    err := s.am.TimedUnlock(accounts.Account{Address: addr}, password, d)
    return err == nil, err
}

And from https://github.com/ethereum/go-ethereum/blob/master/accounts/account_manager.go#L181-L216 where you can see the comment A timeout of 0 unlocks the account until the program exits:

// TimedUnlock unlocks the given account with the passphrase. The account
// stays unlocked for the duration of timeout. A timeout of 0 unlocks the account
// until the program exits. The account must match a unique key file.
//
// If the account address is already unlocked for a duration, TimedUnlock extends or
// shortens the active unlock timeout. If the address was previously unlocked
// indefinitely the timeout is not altered.
func (am *Manager) TimedUnlock(a Account, passphrase string, timeout time.Duration) error {
    a, key, err := am.getDecryptedKey(a, passphrase)
    if err != nil {
        return err
    }

    am.mu.Lock()
    defer am.mu.Unlock()
    u, found := am.unlocked[a.Address]
    if found {
        if u.abort == nil {
            // The address was unlocked indefinitely, so unlocking
            // it with a timeout would be confusing.
            zeroKey(key.PrivateKey)
            return nil
        } else {
            // Terminate the expire goroutine and replace it below.
            close(u.abort)
        }
    }
    if timeout > 0 {
        u = &unlocked{Key: key, abort: make(chan struct{})}
        go am.expire(a.Address, u, timeout)
    } else {
        u = &unlocked{Key: key}
    }
    am.unlocked[a.Address] = u
    return nil
}
  • TY. One further question: the docs aren't clear in this point: if I supress the third argument duration, for how long the account will remain unlocked? – Henrique Barcelos Apr 6 '16 at 12:37
  • From the source code (see above), excluding the third duration argument would unlock the account until the program exits. – The Officious BokkyPooBah Apr 6 '16 at 13:44
  • Github link not working . – Himanshu sharma Jan 16 '17 at 10:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.