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