message.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2017 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. "encoding/hex"
  19. "fmt"
  20. "io/ioutil"
  21. "github.com/ethereum/go-ethereum/accounts/keystore"
  22. "github.com/ethereum/go-ethereum/cmd/utils"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "gopkg.in/urfave/cli.v1"
  26. )
  27. type outputSign struct {
  28. Signature string
  29. }
  30. var msgfileFlag = cli.StringFlag{
  31. Name: "msgfile",
  32. Usage: "file containing the message to sign/verify",
  33. }
  34. var commandSignMessage = cli.Command{
  35. Name: "signmessage",
  36. Usage: "sign a message",
  37. ArgsUsage: "<keyfile> <message>",
  38. Description: `
  39. Sign the message with a keyfile.
  40. To sign a message contained in a file, use the --msgfile flag.
  41. `,
  42. Flags: []cli.Flag{
  43. passphraseFlag,
  44. jsonFlag,
  45. msgfileFlag,
  46. },
  47. Action: func(ctx *cli.Context) error {
  48. message := getMessage(ctx, 1)
  49. // Load the keyfile.
  50. keyfilepath := ctx.Args().First()
  51. keyjson, err := ioutil.ReadFile(keyfilepath)
  52. if err != nil {
  53. utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err)
  54. }
  55. // Decrypt key with passphrase.
  56. passphrase := getPassPhrase(ctx, false)
  57. key, err := keystore.DecryptKey(keyjson, passphrase)
  58. if err != nil {
  59. utils.Fatalf("Error decrypting key: %v", err)
  60. }
  61. signature, err := crypto.Sign(signHash(message), key.PrivateKey)
  62. if err != nil {
  63. utils.Fatalf("Failed to sign message: %v", err)
  64. }
  65. out := outputSign{Signature: hex.EncodeToString(signature)}
  66. if ctx.Bool(jsonFlag.Name) {
  67. mustPrintJSON(out)
  68. } else {
  69. fmt.Println("Signature:", out.Signature)
  70. }
  71. return nil
  72. },
  73. }
  74. type outputVerify struct {
  75. Success bool
  76. RecoveredAddress string
  77. RecoveredPublicKey string
  78. }
  79. var commandVerifyMessage = cli.Command{
  80. Name: "verifymessage",
  81. Usage: "verify the signature of a signed message",
  82. ArgsUsage: "<address> <signature> <message>",
  83. Description: `
  84. Verify the signature of the message.
  85. It is possible to refer to a file containing the message.`,
  86. Flags: []cli.Flag{
  87. jsonFlag,
  88. msgfileFlag,
  89. },
  90. Action: func(ctx *cli.Context) error {
  91. addressStr := ctx.Args().First()
  92. signatureHex := ctx.Args().Get(1)
  93. message := getMessage(ctx, 2)
  94. if !common.IsHexAddress(addressStr) {
  95. utils.Fatalf("Invalid address: %s", addressStr)
  96. }
  97. address := common.HexToAddress(addressStr)
  98. signature, err := hex.DecodeString(signatureHex)
  99. if err != nil {
  100. utils.Fatalf("Signature encoding is not hexadecimal: %v", err)
  101. }
  102. recoveredPubkey, err := crypto.SigToPub(signHash(message), signature)
  103. if err != nil || recoveredPubkey == nil {
  104. utils.Fatalf("Signature verification failed: %v", err)
  105. }
  106. recoveredPubkeyBytes := crypto.FromECDSAPub(recoveredPubkey)
  107. recoveredAddress := crypto.PubkeyToAddress(*recoveredPubkey)
  108. success := address == recoveredAddress
  109. out := outputVerify{
  110. Success: success,
  111. RecoveredPublicKey: hex.EncodeToString(recoveredPubkeyBytes),
  112. RecoveredAddress: recoveredAddress.Hex(),
  113. }
  114. if ctx.Bool(jsonFlag.Name) {
  115. mustPrintJSON(out)
  116. } else {
  117. if out.Success {
  118. fmt.Println("Signature verification successful!")
  119. } else {
  120. fmt.Println("Signature verification failed!")
  121. }
  122. fmt.Println("Recovered public key:", out.RecoveredPublicKey)
  123. fmt.Println("Recovered address:", out.RecoveredAddress)
  124. }
  125. return nil
  126. },
  127. }
  128. func getMessage(ctx *cli.Context, msgarg int) []byte {
  129. if file := ctx.String("msgfile"); file != "" {
  130. if len(ctx.Args()) > msgarg {
  131. utils.Fatalf("Can't use --msgfile and message argument at the same time.")
  132. }
  133. msg, err := ioutil.ReadFile(file)
  134. if err != nil {
  135. utils.Fatalf("Can't read message file: %v", err)
  136. }
  137. return msg
  138. } else if len(ctx.Args()) == msgarg+1 {
  139. return []byte(ctx.Args().Get(msgarg))
  140. }
  141. utils.Fatalf("Invalid number of arguments: want %d, got %d", msgarg+1, len(ctx.Args()))
  142. return nil
  143. }