guide_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // This file contains the code snippets from the developer's guide embedded into
  17. // Go tests. This ensures that any code published in out guides will not break
  18. // accidentally via some code update. If some API changes nonetheless that needs
  19. // modifying this file, please port any modification over into the developer's
  20. // guide wiki pages too!
  21. package guide
  22. import (
  23. "io/ioutil"
  24. "math/big"
  25. "os"
  26. "path/filepath"
  27. "testing"
  28. "time"
  29. "github.com/ethereum/go-ethereum/accounts/keystore"
  30. "github.com/ethereum/go-ethereum/common"
  31. "github.com/ethereum/go-ethereum/core/types"
  32. )
  33. // Tests that the account management snippets work correctly.
  34. func TestAccountManagement(t *testing.T) {
  35. // Create a temporary folder to work with
  36. workdir, err := ioutil.TempDir("", "")
  37. if err != nil {
  38. t.Fatalf("Failed to create temporary work dir: %v", err)
  39. }
  40. defer os.RemoveAll(workdir)
  41. // Create an encrypted keystore with standard crypto parameters
  42. ks := keystore.NewKeyStore(filepath.Join(workdir, "keystore"), keystore.StandardScryptN, keystore.StandardScryptP)
  43. // Create a new account with the specified encryption passphrase
  44. newAcc, err := ks.NewAccount("Creation password")
  45. if err != nil {
  46. t.Fatalf("Failed to create new account: %v", err)
  47. }
  48. // Export the newly created account with a different passphrase. The returned
  49. // data from this method invocation is a JSON encoded, encrypted key-file
  50. jsonAcc, err := ks.Export(newAcc, "Creation password", "Export password")
  51. if err != nil {
  52. t.Fatalf("Failed to export account: %v", err)
  53. }
  54. // Update the passphrase on the account created above inside the local keystore
  55. if err := ks.Update(newAcc, "Creation password", "Update password"); err != nil {
  56. t.Fatalf("Failed to update account: %v", err)
  57. }
  58. // Delete the account updated above from the local keystore
  59. if err := ks.Delete(newAcc, "Update password"); err != nil {
  60. t.Fatalf("Failed to delete account: %v", err)
  61. }
  62. // Import back the account we've exported (and then deleted) above with yet
  63. // again a fresh passphrase
  64. if _, err := ks.Import(jsonAcc, "Export password", "Import password"); err != nil {
  65. t.Fatalf("Failed to import account: %v", err)
  66. }
  67. // Create a new account to sign transactions with
  68. signer, err := ks.NewAccount("Signer password")
  69. if err != nil {
  70. t.Fatalf("Failed to create signer account: %v", err)
  71. }
  72. tx := types.NewTransaction(0, common.Address{}, big.NewInt(0), 0, big.NewInt(0), nil)
  73. chain := big.NewInt(1)
  74. // Sign a transaction with a single authorization
  75. if _, err := ks.SignTxWithPassphrase(signer, "Signer password", tx, chain); err != nil {
  76. t.Fatalf("Failed to sign with passphrase: %v", err)
  77. }
  78. // Sign a transaction with multiple manually cancelled authorizations
  79. if err := ks.Unlock(signer, "Signer password"); err != nil {
  80. t.Fatalf("Failed to unlock account: %v", err)
  81. }
  82. if _, err := ks.SignTx(signer, tx, chain); err != nil {
  83. t.Fatalf("Failed to sign with unlocked account: %v", err)
  84. }
  85. if err := ks.Lock(signer.Address); err != nil {
  86. t.Fatalf("Failed to lock account: %v", err)
  87. }
  88. // Sign a transaction with multiple automatically cancelled authorizations
  89. if err := ks.TimedUnlock(signer, "Signer password", time.Second); err != nil {
  90. t.Fatalf("Failed to time unlock account: %v", err)
  91. }
  92. if _, err := ks.SignTx(signer, tx, chain); err != nil {
  93. t.Fatalf("Failed to sign with time unlocked account: %v", err)
  94. }
  95. }