message_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "testing"
  22. )
  23. func TestMessageSignVerify(t *testing.T) {
  24. tmpdir, err := ioutil.TempDir("", "ethkey-test")
  25. if err != nil {
  26. t.Fatal("Can't create temporary directory:", err)
  27. }
  28. defer os.RemoveAll(tmpdir)
  29. keyfile := filepath.Join(tmpdir, "the-keyfile")
  30. message := "test message"
  31. // Create the key.
  32. generate := runEthkey(t, "generate", "--lightkdf", keyfile)
  33. generate.Expect(`
  34. !! Unsupported terminal, password will be echoed.
  35. Password: {{.InputLine "foobar"}}
  36. Repeat password: {{.InputLine "foobar"}}
  37. `)
  38. _, matches := generate.ExpectRegexp(`Address: (0x[0-9a-fA-F]{40})\n`)
  39. address := matches[1]
  40. generate.ExpectExit()
  41. // Sign a message.
  42. sign := runEthkey(t, "signmessage", keyfile, message)
  43. sign.Expect(`
  44. !! Unsupported terminal, password will be echoed.
  45. Password: {{.InputLine "foobar"}}
  46. `)
  47. _, matches = sign.ExpectRegexp(`Signature: ([0-9a-f]+)\n`)
  48. signature := matches[1]
  49. sign.ExpectExit()
  50. // Verify the message.
  51. verify := runEthkey(t, "verifymessage", address, signature, message)
  52. _, matches = verify.ExpectRegexp(`
  53. Signature verification successful!
  54. Recovered public key: [0-9a-f]+
  55. Recovered address: (0x[0-9a-fA-F]{40})
  56. `)
  57. recovered := matches[1]
  58. verify.ExpectExit()
  59. if recovered != address {
  60. t.Error("recovered address doesn't match generated key")
  61. }
  62. }