github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/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 out 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  	"io/ioutil"
    27  	"math/big"
    28  	"os"
    29  	"path/filepath"
    30  	"testing"
    31  	"time"
    32  
    33  	"github.com/intfoundation/intchain/accounts/keystore"
    34  	"github.com/intfoundation/intchain/core/types"
    35  )
    36  
    37  // Tests that the account management snippets work correctly.
    38  func TestAccountManagement(t *testing.T) {
    39  	// Create a temporary folder to work with
    40  	workdir, err := ioutil.TempDir("", "")
    41  	if err != nil {
    42  		t.Fatalf("Failed to create temporary work dir: %v", err)
    43  	}
    44  	defer os.RemoveAll(workdir)
    45  
    46  	// Create an encrypted keystore with standard crypto parameters
    47  	ks := keystore.NewKeyStore(filepath.Join(workdir, "keystore"), keystore.StandardScryptN, keystore.StandardScryptP)
    48  
    49  	// Create a new account with the specified encryption passphrase
    50  	newAcc, err := ks.NewAccount("Creation password")
    51  	if err != nil {
    52  		t.Fatalf("Failed to create new account: %v", err)
    53  	}
    54  	// Export the newly created account with a different passphrase. The returned
    55  	// data from this method invocation is a JSON encoded, encrypted key-file
    56  	jsonAcc, err := ks.Export(newAcc, "Creation password", "Export password")
    57  	if err != nil {
    58  		t.Fatalf("Failed to export account: %v", err)
    59  	}
    60  	// Update the passphrase on the account created above inside the local keystore
    61  	if err := ks.Update(newAcc, "Creation password", "Update password"); err != nil {
    62  		t.Fatalf("Failed to update account: %v", err)
    63  	}
    64  	// Delete the account updated above from the local keystore
    65  	if err := ks.Delete(newAcc, "Update password"); err != nil {
    66  		t.Fatalf("Failed to delete account: %v", err)
    67  	}
    68  	// Import back the account we've exported (and then deleted) above with yet
    69  	// again a fresh passphrase
    70  	if _, err := ks.Import(jsonAcc, "Export password", "Import password"); err != nil {
    71  		t.Fatalf("Failed to import account: %v", err)
    72  	}
    73  	// Create a new account to sign transactions with
    74  	signer, err := ks.NewAccount("Signer password")
    75  	if err != nil {
    76  		t.Fatalf("Failed to create signer account: %v", err)
    77  	}
    78  	tx, chain := new(types.Transaction), big.NewInt(1)
    79  
    80  	// Sign a transaction with a single authorization
    81  	if _, err := ks.SignTxWithPassphrase(signer, "Signer password", tx, chain); err != nil {
    82  		t.Fatalf("Failed to sign with passphrase: %v", err)
    83  	}
    84  	// Sign a transaction with multiple manually cancelled authorizations
    85  	if err := ks.Unlock(signer, "Signer password"); err != nil {
    86  		t.Fatalf("Failed to unlock account: %v", err)
    87  	}
    88  	if _, err := ks.SignTx(signer, tx, chain); err != nil {
    89  		t.Fatalf("Failed to sign with unlocked account: %v", err)
    90  	}
    91  	if err := ks.Lock(signer.Address); err != nil {
    92  		t.Fatalf("Failed to lock account: %v", err)
    93  	}
    94  	// Sign a transaction with multiple automatically cancelled authorizations
    95  	if err := ks.TimedUnlock(signer, "Signer password", time.Second); err != nil {
    96  		t.Fatalf("Failed to time unlock account: %v", err)
    97  	}
    98  	if _, err := ks.SignTx(signer, tx, chain); err != nil {
    99  		t.Fatalf("Failed to sign with time unlocked account: %v", err)
   100  	}
   101  }