message_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. "path/filepath"
  19. "testing"
  20. )
  21. func TestMessageSignVerify(t *testing.T) {
  22. tmpdir := t.TempDir()
  23. keyfile := filepath.Join(tmpdir, "the-keyfile")
  24. message := "test message"
  25. // Create the key.
  26. generate := runEthkey(t, "generate", "--lightkdf", keyfile)
  27. generate.Expect(`
  28. !! Unsupported terminal, password will be echoed.
  29. Password: {{.InputLine "foobar"}}
  30. Repeat password: {{.InputLine "foobar"}}
  31. `)
  32. _, matches := generate.ExpectRegexp(`Address: (0x[0-9a-fA-F]{40})\n`)
  33. address := matches[1]
  34. generate.ExpectExit()
  35. // Sign a message.
  36. sign := runEthkey(t, "signmessage", keyfile, message)
  37. sign.Expect(`
  38. !! Unsupported terminal, password will be echoed.
  39. Password: {{.InputLine "foobar"}}
  40. `)
  41. _, matches = sign.ExpectRegexp(`Signature: ([0-9a-f]+)\n`)
  42. signature := matches[1]
  43. sign.ExpectExit()
  44. // Verify the message.
  45. verify := runEthkey(t, "verifymessage", address, signature, message)
  46. _, matches = verify.ExpectRegexp(`
  47. Signature verification successful!
  48. Recovered public key: [0-9a-f]+
  49. Recovered address: (0x[0-9a-fA-F]{40})
  50. `)
  51. recovered := matches[1]
  52. verify.ExpectExit()
  53. if recovered != address {
  54. t.Error("recovered address doesn't match generated key")
  55. }
  56. }