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