github.com/ethereum/go-ethereum@v1.16.1/internal/guide/guide_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // This file contains the code snippets from the developer's guide embedded into
    18  // Go tests. This ensures that any code published in our guides will not break
    19  // accidentally via some code update. If some API changes nonetheless that needs
    20  // modifying this file, please port any modification over into the developer's
    21  // guide wiki pages too!
    22  
    23  package guide
    24  
    25  import (
    26  	"math/big"
    27  	"path/filepath"
    28  	"testing"
    29  	"time"
    30  
    31  	"github.com/ethereum/go-ethereum/accounts/keystore"
    32  	"github.com/ethereum/go-ethereum/common"
    33  	"github.com/ethereum/go-ethereum/core/types"
    34  )
    35  
    36  // Tests that the account management snippets work correctly.
    37  func TestAccountManagement(t *testing.T) {
    38  	t.Parallel()
    39  
    40  	// Create a temporary folder to work with
    41  	workdir := t.TempDir()
    42  
    43  	// Create an encrypted keystore (using light scrypt parameters)
    44  	ks := keystore.NewKeyStore(filepath.Join(workdir, "keystore"), keystore.LightScryptN, keystore.LightScryptP)
    45  
    46  	// Create a new account with the specified encryption passphrase
    47  	newAcc, err := ks.NewAccount("Creation password")
    48  	if err != nil {
    49  		t.Fatalf("Failed to create new account: %v", err)
    50  	}
    51  	// Export the newly created account with a different passphrase. The returned
    52  	// data from this method invocation is a JSON encoded, encrypted key-file
    53  	jsonAcc, err := ks.Export(newAcc, "Creation password", "Export password")
    54  	if err != nil {
    55  		t.Fatalf("Failed to export account: %v", err)
    56  	}
    57  	// Update the passphrase on the account created above inside the local keystore
    58  	if err := ks.Update(newAcc, "Creation password", "Update password"); err != nil {
    59  		t.Fatalf("Failed to update account: %v", err)
    60  	}
    61  	// Delete the account updated above from the local keystore
    62  	if err := ks.Delete(newAcc, "Update password"); err != nil {
    63  		t.Fatalf("Failed to delete account: %v", err)
    64  	}
    65  	// Import back the account we've exported (and then deleted) above with yet
    66  	// again a fresh passphrase
    67  	if _, err := ks.Import(jsonAcc, "Export password", "Import password"); err != nil {
    68  		t.Fatalf("Failed to import account: %v", err)
    69  	}
    70  	// Create a new account to sign transactions with
    71  	signer, err := ks.NewAccount("Signer password")
    72  	if err != nil {
    73  		t.Fatalf("Failed to create signer account: %v", err)
    74  	}
    75  	tx := types.NewTransaction(0, common.Address{}, big.NewInt(0), 0, big.NewInt(0), nil)
    76  	chain := big.NewInt(1)
    77  
    78  	// Sign a transaction with a single authorization
    79  	if _, err := ks.SignTxWithPassphrase(signer, "Signer password", tx, chain); err != nil {
    80  		t.Fatalf("Failed to sign with passphrase: %v", err)
    81  	}
    82  	// Sign a transaction with multiple manually cancelled authorizations
    83  	if err := ks.Unlock(signer, "Signer password"); err != nil {
    84  		t.Fatalf("Failed to unlock account: %v", err)
    85  	}
    86  	if _, err := ks.SignTx(signer, tx, chain); err != nil {
    87  		t.Fatalf("Failed to sign with unlocked account: %v", err)
    88  	}
    89  	if err := ks.Lock(signer.Address); err != nil {
    90  		t.Fatalf("Failed to lock account: %v", err)
    91  	}
    92  	// Sign a transaction with multiple automatically cancelled authorizations
    93  	if err := ks.TimedUnlock(signer, "Signer password", time.Second); err != nil {
    94  		t.Fatalf("Failed to time unlock account: %v", err)
    95  	}
    96  	if _, err := ks.SignTx(signer, tx, chain); err != nil {
    97  		t.Fatalf("Failed to sign with time unlocked account: %v", err)
    98  	}
    99  }