github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/internal/guide/guide_test.go (about)

     1  // This file contains the code snippets from the developer's guide embedded into
     2  // Go tests. This ensures that any code published in out guides will not break
     3  // accidentally via some code update. If some API changes nonetheless that needs
     4  // modifying this file, please port any modification over into the developer's
     5  // guide wiki pages too!
     6  
     7  package guide
     8  
     9  import (
    10  	"io/ioutil"
    11  	"math/big"
    12  	"os"
    13  	"path/filepath"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/quickchainproject/quickchain/accounts/keystore"
    18  	"github.com/quickchainproject/quickchain/core/types"
    19  )
    20  
    21  // Tests that the account management snippets work correctly.
    22  func TestAccountManagement(t *testing.T) {
    23  	// Create a temporary folder to work with
    24  	workdir, err := ioutil.TempDir("", "")
    25  	if err != nil {
    26  		t.Fatalf("Failed to create temporary work dir: %v", err)
    27  	}
    28  	defer os.RemoveAll(workdir)
    29  
    30  	// Create an encrypted keystore with standard crypto parameters
    31  	ks := keystore.NewKeyStore(filepath.Join(workdir, "keystore"), keystore.StandardScryptN, keystore.StandardScryptP)
    32  
    33  	// Create a new account with the specified encryption passphrase
    34  	newAcc, err := ks.NewAccount("Creation password")
    35  	if err != nil {
    36  		t.Fatalf("Failed to create new account: %v", err)
    37  	}
    38  	// Export the newly created account with a different passphrase. The returned
    39  	// data from this method invocation is a JSON encoded, encrypted key-file
    40  	jsonAcc, err := ks.Export(newAcc, "Creation password", "Export password")
    41  	if err != nil {
    42  		t.Fatalf("Failed to export account: %v", err)
    43  	}
    44  	// Update the passphrase on the account created above inside the local keystore
    45  	if err := ks.Update(newAcc, "Creation password", "Update password"); err != nil {
    46  		t.Fatalf("Failed to update account: %v", err)
    47  	}
    48  	// Delete the account updated above from the local keystore
    49  	if err := ks.Delete(newAcc, "Update password"); err != nil {
    50  		t.Fatalf("Failed to delete account: %v", err)
    51  	}
    52  	// Import back the account we've exported (and then deleted) above with yet
    53  	// again a fresh passphrase
    54  	if _, err := ks.Import(jsonAcc, "Export password", "Import password"); err != nil {
    55  		t.Fatalf("Failed to import account: %v", err)
    56  	}
    57  	// Create a new account to sign transactions with
    58  	signer, err := ks.NewAccount("Signer password")
    59  	if err != nil {
    60  		t.Fatalf("Failed to create signer account: %v", err)
    61  	}
    62  	tx, chain := new(types.Transaction), big.NewInt(1)
    63  
    64  	// Sign a transaction with a single authorization
    65  	if _, err := ks.SignTxWithPassphrase(signer, "Signer password", tx, chain); err != nil {
    66  		t.Fatalf("Failed to sign with passphrase: %v", err)
    67  	}
    68  	// Sign a transaction with multiple manually cancelled authorizations
    69  	if err := ks.Unlock(signer, "Signer password"); err != nil {
    70  		t.Fatalf("Failed to unlock account: %v", err)
    71  	}
    72  	if _, err := ks.SignTx(signer, tx, chain); err != nil {
    73  		t.Fatalf("Failed to sign with unlocked account: %v", err)
    74  	}
    75  	if err := ks.Lock(signer.Address); err != nil {
    76  		t.Fatalf("Failed to lock account: %v", err)
    77  	}
    78  	// Sign a transaction with multiple automatically cancelled authorizations
    79  	if err := ks.TimedUnlock(signer, "Signer password", time.Second); err != nil {
    80  		t.Fatalf("Failed to time unlock account: %v", err)
    81  	}
    82  	if _, err := ks.SignTx(signer, tx, chain); err != nil {
    83  		t.Fatalf("Failed to sign with time unlocked account: %v", err)
    84  	}
    85  }