9

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.

0

1 Answer 1

18

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
}
3
  • 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? Commented Apr 6, 2016 at 12:37
  • From the source code (see above), excluding the third duration argument would unlock the account until the program exits. Commented Apr 6, 2016 at 13:44
  • Github link not working . Commented Jan 16, 2017 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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