Skip to main content
Update responding to comment about broken links
Source Link
BokkyPooBah
  • 40.4k
  • 14
  • 123
  • 193

From https://github.com/ethereum/go-ethereum/blob/master/rpc/api/personal.go#L120-L139https://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 (selfs *personalApi*PrivateAccountAPI) UnlockAccount(reqaddr *sharedcommon.RequestAddress, password string, duration *uint64) (interface{}bool, error) {
    argsconst :max = newuint64(UnlockAccountArgstime.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}, *args.Passphrasepassword, time.Duration(args.Duration)*time.Secondd)
    return err == nil, err
}

And from https://github.com/ethereum/go-ethereum/blob/master/accounts/account_manager.go#L95-L125https://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 given addresspassphrase. 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 accoutaccount address is already unlocked for a duration, TimedUnlock extends or shortens
// shortens the active unlock timeout. If the address was previously unlocked
// indefinitely the timeout is not altered.
func (am *Manager) TimedUnlock(addra common.AddressAccount, keyAuthpassphrase string, timeout time.Duration) error {
    a, key, err := am.keyStore.GetKeygetDecryptedKey(addra, keyAuthpassphrase)
    if err != nil {
        return err
    }
    var u *unlocked
    am.mutexmu.Lock()
    defer am.mutexmu.Unlock()
    varu, found bool:= am.unlocked[a.Address]
    u,if found = am.unlocked[addr]{
        if foundu.abort == nil {
            // terminateThe dropLateraddress forwas thisunlocked keyindefinitely, toso avoidunlocking
 unexpected drops          // it with a timeout would be confusing.
        if u   zeroKey(key.abortPrivateKey)
 !=           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(addra.Address, u, timeout)
    } else {
        u = &unlocked{Key: key}
    }
    am.unlocked[addr]unlocked[a.Address] = u
    return nil
}

From https://github.com/ethereum/go-ethereum/blob/master/rpc/api/personal.go#L120-L139 :

func (self *personalApi) UnlockAccount(req *shared.Request) (interface{}, error) {
    args := new(UnlockAccountArgs)
    ...
    err := am.TimedUnlock(addr, *args.Passphrase, time.Duration(args.Duration)*time.Second)
    return err == nil, err
}

And from https://github.com/ethereum/go-ethereum/blob/master/accounts/account_manager.go#L95-L125 :

// TimedUnlock unlocks the account with the given address. The account
// stays unlocked for the duration of timeout. A timeout of 0 unlocks the account
// until the program exits.
//
// If the accout is already unlocked, TimedUnlock extends or shortens
// the active unlock timeout.
func (am *Manager) TimedUnlock(addr common.Address, keyAuth string, timeout time.Duration) error {
    key, err := am.keyStore.GetKey(addr, keyAuth)
    if err != nil {
        return err
    }
    var u *unlocked
    am.mutex.Lock()
    defer am.mutex.Unlock()
    var found bool
    u, found = am.unlocked[addr]
    if found {
        // terminate dropLater for this key to avoid unexpected drops.
        if u.abort != nil {
            close(u.abort)
        }
    }
    if timeout > 0 {
        u = &unlocked{Key: key, abort: make(chan struct{})}
        go am.expire(addr, u, timeout)
    } else {
        u = &unlocked{Key: key}
    }
    am.unlocked[addr] = u
    return nil
}

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
}
Default duration
Source Link
BokkyPooBah
  • 40.4k
  • 14
  • 123
  • 193

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/rpc/api/personal.go#L120-L139 :

func (self *personalApi) UnlockAccount(req *shared.Request) (interface{}, error) {
    args := new(UnlockAccountArgs)
    ...
    err := am.TimedUnlock(addr, *args.Passphrase, time.Duration(args.Duration)*time.Second)
    return err == nil, err
}

And from https://github.com/ethereum/go-ethereum/blob/master/accounts/account_manager.go#L95-L125 :

// TimedUnlock unlocks the account with the given address. The account
// stays unlocked for the duration of timeout. A timeout of 0 unlocks the account
// until the program exits.
//
// If the accout is already unlocked, TimedUnlock extends or shortens
// the active unlock timeout.
func (am *Manager) TimedUnlock(addr common.Address, keyAuth string, timeout time.Duration) error {
    key, err := am.keyStore.GetKey(addr, keyAuth)
    if err != nil {
        return err
    }
    var u *unlocked
    am.mutex.Lock()
    defer am.mutex.Unlock()
    var found bool
    u, found = am.unlocked[addr]
    if found {
        // terminate dropLater for this key to avoid unexpected drops.
        if u.abort != nil {
            close(u.abort)
        }
    }
    if timeout > 0 {
        u = &unlocked{Key: key, abort: make(chan struct{})}
        go am.expire(addr, u, timeout)
    } else {
        u = &unlocked{Key: key}
    }
    am.unlocked[addr] = u
    return nil
}

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/rpc/api/personal.go#L120-L139 :

func (self *personalApi) UnlockAccount(req *shared.Request) (interface{}, error) {
    args := new(UnlockAccountArgs)
    ...
    err := am.TimedUnlock(addr, *args.Passphrase, time.Duration(args.Duration)*time.Second)
    return err == nil, err
}

And from https://github.com/ethereum/go-ethereum/blob/master/accounts/account_manager.go#L95-L125 :

// TimedUnlock unlocks the account with the given address. The account
// stays unlocked for the duration of timeout. A timeout of 0 unlocks the account
// until the program exits.
//
// If the accout is already unlocked, TimedUnlock extends or shortens
// the active unlock timeout.
func (am *Manager) TimedUnlock(addr common.Address, keyAuth string, timeout time.Duration) error {
    key, err := am.keyStore.GetKey(addr, keyAuth)
    if err != nil {
        return err
    }
    var u *unlocked
    am.mutex.Lock()
    defer am.mutex.Unlock()
    var found bool
    u, found = am.unlocked[addr]
    if found {
        // terminate dropLater for this key to avoid unexpected drops.
        if u.abort != nil {
            close(u.abort)
        }
    }
    if timeout > 0 {
        u = &unlocked{Key: key, abort: make(chan struct{})}
        go am.expire(addr, u, timeout)
    } else {
        u = &unlocked{Key: key}
    }
    am.unlocked[addr] = u
    return nil
}
Fixing quotes
Source Link
BokkyPooBah
  • 40.4k
  • 14
  • 123
  • 193

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"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"loadScript("unlock'unlock.js"js')'" attach
true

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

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
Improving
Source Link
BokkyPooBah
  • 40.4k
  • 14
  • 123
  • 193
Loading
Added method to load JavaScript from file
Source Link
BokkyPooBah
  • 40.4k
  • 14
  • 123
  • 193
Loading
Source Link
BokkyPooBah
  • 40.4k
  • 14
  • 123
  • 193
Loading