Tests that the account management snippets work correctly.
(t *testing.T)
| 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 := t.TempDir() |
| 40 | |
| 41 | // Create an encrypted keystore (using light scrypt parameters) |
| 42 | ks := keystore.NewKeyStore(filepath.Join(workdir, "keystore"), keystore.LightScryptN, keystore.LightScryptP) |
| 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 := types.NewTransaction(0, common.Address{}, big.NewInt(0), 0, big.NewInt(0), nil) |
| 74 | chain := big.NewInt(1) |
| 75 | |
| 76 | // Sign a transaction with a single authorization |
| 77 | if _, err := ks.SignTxWithPassphrase(signer, "Signer password", tx, chain); err != nil { |
| 78 | t.Fatalf("Failed to sign with passphrase: %v", err) |
| 79 | } |
| 80 | // Sign a transaction with multiple manually cancelled authorizations |
| 81 | if err := ks.Unlock(signer, "Signer password"); err != nil { |
| 82 | t.Fatalf("Failed to unlock account: %v", err) |
| 83 | } |
| 84 | if _, err := ks.SignTx(signer, tx, chain); err != nil { |
| 85 | t.Fatalf("Failed to sign with unlocked account: %v", err) |
| 86 | } |
| 87 | if err := ks.Lock(signer.Address); err != nil { |
| 88 | t.Fatalf("Failed to lock account: %v", err) |
| 89 | } |
| 90 | // Sign a transaction with multiple automatically cancelled authorizations |
| 91 | if err := ks.TimedUnlock(signer, "Signer password", time.Second); err != nil { |
| 92 | t.Fatalf("Failed to time unlock account: %v", err) |
| 93 | } |
| 94 | if _, err := ks.SignTx(signer, tx, chain); err != nil { |
nothing calls this directly
no test coverage detected