github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/internal/guide/guide_test.go (about) 1 package guide 2 3 import ( 4 "io/ioutil" 5 "math/big" 6 "os" 7 "path/filepath" 8 "testing" 9 "time" 10 11 "github.com/neatlab/neatio/chain/accounts/keystore" 12 "github.com/neatlab/neatio/chain/core/types" 13 ) 14 15 func TestAccountManagement(t *testing.T) { 16 17 workdir, err := ioutil.TempDir("", "") 18 if err != nil { 19 t.Fatalf("Failed to create temporary work dir: %v", err) 20 } 21 defer os.RemoveAll(workdir) 22 23 ks := keystore.NewKeyStore(filepath.Join(workdir, "keystore"), keystore.StandardScryptN, keystore.StandardScryptP) 24 25 newAcc, err := ks.NewAccount("Creation password") 26 if err != nil { 27 t.Fatalf("Failed to create new account: %v", err) 28 } 29 30 jsonAcc, err := ks.Export(newAcc, "Creation password", "Export password") 31 if err != nil { 32 t.Fatalf("Failed to export account: %v", err) 33 } 34 35 if err := ks.Update(newAcc, "Creation password", "Update password"); err != nil { 36 t.Fatalf("Failed to update account: %v", err) 37 } 38 39 if err := ks.Delete(newAcc, "Update password"); err != nil { 40 t.Fatalf("Failed to delete account: %v", err) 41 } 42 43 if _, err := ks.Import(jsonAcc, "Export password", "Import password"); err != nil { 44 t.Fatalf("Failed to import account: %v", err) 45 } 46 47 signer, err := ks.NewAccount("Signer password") 48 if err != nil { 49 t.Fatalf("Failed to create signer account: %v", err) 50 } 51 tx, chain := new(types.Transaction), big.NewInt(1) 52 53 if _, err := ks.SignTxWithPassphrase(signer, "Signer password", tx, chain); err != nil { 54 t.Fatalf("Failed to sign with passphrase: %v", err) 55 } 56 57 if err := ks.Unlock(signer, "Signer password"); err != nil { 58 t.Fatalf("Failed to unlock account: %v", err) 59 } 60 if _, err := ks.SignTx(signer, tx, chain); err != nil { 61 t.Fatalf("Failed to sign with unlocked account: %v", err) 62 } 63 if err := ks.Lock(signer.Address); err != nil { 64 t.Fatalf("Failed to lock account: %v", err) 65 } 66 67 if err := ks.TimedUnlock(signer, "Signer password", time.Second); err != nil { 68 t.Fatalf("Failed to time unlock account: %v", err) 69 } 70 if _, err := ks.SignTx(signer, tx, chain); err != nil { 71 t.Fatalf("Failed to sign with time unlocked account: %v", err) 72 } 73 }