Wie werden Konten programmgesteuert entsperrt, nachdem der Knoten gestartet wurde?

Gibt es eine Möglichkeit, Konten einfach zu entsperren, ohne die Konsole mit zu öffnen geth?

Ich schreibe einige Skripte, um die Cluster-Generierung zu automatisieren, und ich wünschte, ich könnte einfach getheinige Konten entsperren, ohne eine andere Instanz zu starten oder an eine vorhandene anzuhängen.


Bearbeiten

Wenn ich so etwas ausführe:

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

Nach dem Start des Knotens werden die Konten nicht entsperrt .

Die einzige Möglichkeit, Konten zu entsperren, bestand darin, sie beim Ausführen zu entsperren geth, um den Knoten zu starten.

Antworten (1)

Nur um klarzustellen:

  • Sie möchten einen Geth-Knoten ausführen, der mit standardmäßig gesperrten Konten beginnt
  • Sie möchten später einen „geth Attach“-Befehl ausführen, um ein oder mehrere Konten für einen bestimmten Zeitraum zu entsperren

Sie können den folgenden Befehl zum Anhängen an Ihren Geth-Knoten verwenden, um ein Konto mithilfe der JavaScript-API personal.unlockAccount(...) zu entsperren ( https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console# personalunlockaccount ), wobei der dritte Parameter die Dauer in Sekunden ist:

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

Oder Sie erstellen eine Datei, z. B. unlock.js mit folgendem Inhalt:

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

Verwenden Sie dann den folgenden Befehl, um das Konto zu entsperren:

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

Als Antwort auf die folgende Frage bezüglich der Standarddauer gilt: Wenn keine Dauer angegeben ist, wird eine Dauer von 0 impliziert. Und eine Dauer von 0 entsperrt das Konto, bis das Programm beendet wird, dh es gibt kein Ablaufdatum.

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

Und von https://github.com/ethereum/go-ethereum/blob/master/accounts/account_manager.go#L181-L216 , wo Sie den Kommentar sehen können 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. Eine weitere Frage: Die Dokumentation ist in diesem Punkt nicht eindeutig: Wenn ich das dritte Argument unterdrücke duration, wie lange bleibt das Konto entsperrt?
Aus dem Quellcode (siehe oben) würde das Ausschließen des dritten Arguments für die Dauer das Konto entsperren, bis das Programm beendet wird.
Github-Link funktioniert nicht.