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