|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/hex" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io/ioutil" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + |
| 13 | + "github.com/ethereum/go-ethereum/common" |
| 14 | + "github.com/ethereum/go-ethereum/crypto" |
| 15 | + "github.com/keep-network/keep-common/pkg/chain/ethereum/ethutil" |
| 16 | + "github.com/keep-network/keep-ecdsa/internal/config" |
| 17 | + "github.com/urfave/cli" |
| 18 | +) |
| 19 | + |
| 20 | +// EthereumSigningCommand contains the definition of the `signing ethereum` |
| 21 | +// command-line subcommand and its own subcommands. |
| 22 | +var EthereumSigningCommand = cli.Command{ |
| 23 | + Name: "ethereum", |
| 24 | + Usage: "Ethereum signatures calculation", |
| 25 | + Subcommands: []cli.Command{ |
| 26 | + { |
| 27 | + Name: "sign", |
| 28 | + Usage: "Sign a message using the operator's key", |
| 29 | + Description: ethereumSignDescription, |
| 30 | + Action: EthereumSign, |
| 31 | + ArgsUsage: "[message]", |
| 32 | + Flags: []cli.Flag{ |
| 33 | + cli.StringFlag{ |
| 34 | + Name: "eth-key-file,k", |
| 35 | + Usage: "Path to the ethereum key file. " + |
| 36 | + "If not provided read the path from a config file.", |
| 37 | + }, |
| 38 | + cli.StringFlag{ |
| 39 | + Name: "output-file,o", |
| 40 | + Usage: "Output file for the signature", |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + { |
| 45 | + Name: "verify", |
| 46 | + Usage: "Verifies a signature", |
| 47 | + Description: ethereumVerifyDescription, |
| 48 | + Action: EthereumVerify, |
| 49 | + ArgsUsage: "[ethereum-signature]", |
| 50 | + Flags: []cli.Flag{ |
| 51 | + cli.StringFlag{ |
| 52 | + Name: "input-file,i", |
| 53 | + Usage: "Input file with the signature", |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | +} |
| 59 | + |
| 60 | +const ethereumSignDescription = `Calculates an ethereum signature for a given message. |
| 61 | +The message is expected to be provided as a string, it is later hashed with SHA-256 |
| 62 | +and passed to Ethereum ECDSA signing. Signature is calculated in Ethereum specific |
| 63 | +format as a hexadecimal string representation of 65-byte {R, S, V} parameters. |
| 64 | +
|
| 65 | +It requires an Ethereum key to be provided in an encrypted file. A path to the key file |
| 66 | +can be configured in a config file or specified directly with an 'eth-key-file' flag. |
| 67 | +
|
| 68 | +The key file is expected to be encrypted with a password provided as ` + config.PasswordEnvVariable + ` |
| 69 | +environment variable. |
| 70 | + |
| 71 | +The result is outputted in a common Ethereum signature format: |
| 72 | +{ |
| 73 | + "address": "<address>", |
| 74 | + "msg": "<content>", |
| 75 | + "sig": "<signature>", |
| 76 | + "version": "2" |
| 77 | +} |
| 78 | +
|
| 79 | +If 'output-file' flag is set the result will be stored in a specified file path. |
| 80 | +` |
| 81 | + |
| 82 | +const ethereumVerifyDescription = `Verifies if a signature was calculated for a message |
| 83 | +by an ethereum account identified by an address. |
| 84 | +
|
| 85 | +It expects a signature to be provided in a common Ethereum signature format: |
| 86 | +{ |
| 87 | + "address": "<address>", |
| 88 | + "msg": "<content>", |
| 89 | + "sig": "<signature>", |
| 90 | + "version": "2" |
| 91 | +} |
| 92 | +
|
| 93 | +If 'input-file' flag is set the input will be read from a specified file path. |
| 94 | +` |
| 95 | + |
| 96 | +// EthereumSignature is a common Ethereum signature format. |
| 97 | +type EthereumSignature struct { |
| 98 | + Address common.Address `json:"address"` |
| 99 | + Message string `json:"msg"` |
| 100 | + Signature string `json:"sig"` |
| 101 | + Version uint `json:"version"` |
| 102 | +} |
| 103 | + |
| 104 | +const ethSignatureVersion uint = 2 |
| 105 | + |
| 106 | +// EthereumSign signs a string using operator's ethereum key. |
| 107 | +func EthereumSign(c *cli.Context) error { |
| 108 | + message := c.Args().First() |
| 109 | + if len(message) == 0 { |
| 110 | + return fmt.Errorf("invalid digest") |
| 111 | + } |
| 112 | + |
| 113 | + var ethKeyFilePath, ethKeyPassword string |
| 114 | + // Check if `eth-key-file` flag was set. If not read the key file path from |
| 115 | + // a config file. |
| 116 | + if ethKeyFilePath = c.String("eth-key-file"); len(ethKeyFilePath) > 0 { |
| 117 | + ethKeyPassword = os.Getenv(config.PasswordEnvVariable) |
| 118 | + } else { |
| 119 | + ethereumConfig, err := config.ReadEthereumConfig(c.GlobalString("config")) |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("failed while reading config file: [%v]", err) |
| 122 | + } |
| 123 | + |
| 124 | + ethKeyFilePath = ethereumConfig.Account.KeyFile |
| 125 | + ethKeyPassword = ethereumConfig.Account.KeyFilePassword |
| 126 | + } |
| 127 | + |
| 128 | + ethereumKey, err := ethutil.DecryptKeyFile(ethKeyFilePath, ethKeyPassword) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf( |
| 131 | + "failed to read key file [%s]: [%v]", |
| 132 | + ethKeyFilePath, |
| 133 | + err, |
| 134 | + ) |
| 135 | + } |
| 136 | + |
| 137 | + digest := sha256.Sum256([]byte(message)) |
| 138 | + |
| 139 | + signature, err := crypto.Sign(digest[:], ethereumKey.PrivateKey) |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("failed to sign: [%v]", err) |
| 142 | + } |
| 143 | + |
| 144 | + ethereumSignature := &EthereumSignature{ |
| 145 | + Address: ethereumKey.Address, |
| 146 | + Message: message, |
| 147 | + Signature: hex.EncodeToString(signature), |
| 148 | + Version: ethSignatureVersion, |
| 149 | + } |
| 150 | + |
| 151 | + marshaledSignature, err := json.Marshal(ethereumSignature) |
| 152 | + if err != nil { |
| 153 | + return fmt.Errorf("failed to marshal ethereum signature: [%v]", err) |
| 154 | + } |
| 155 | + |
| 156 | + return outputData(c, marshaledSignature, 0644) // store to user writeable file |
| 157 | +} |
| 158 | + |
| 159 | +// EthereumVerify verifies if a signature was calculated by a signer with the |
| 160 | +// given ethereum address. |
| 161 | +func EthereumVerify(c *cli.Context) error { |
| 162 | + var marshaledSignature []byte |
| 163 | + if inputFilePath := c.String("input-file"); len(inputFilePath) > 0 { |
| 164 | + fileContent, err := ioutil.ReadFile(filepath.Clean(inputFilePath)) |
| 165 | + if err != nil { |
| 166 | + return fmt.Errorf("failed to read a file: [%v]", err) |
| 167 | + } |
| 168 | + marshaledSignature = fileContent |
| 169 | + } else { |
| 170 | + signatureArg := c.Args().First() |
| 171 | + if len(signatureArg) == 0 { |
| 172 | + return fmt.Errorf("missing argument") |
| 173 | + } |
| 174 | + |
| 175 | + marshaledSignature = []byte(signatureArg) |
| 176 | + } |
| 177 | + |
| 178 | + ethereumSignature := &EthereumSignature{} |
| 179 | + err := json.Unmarshal(marshaledSignature, ethereumSignature) |
| 180 | + if err != nil { |
| 181 | + return fmt.Errorf("failed to unmarshal ethereum signature: [%v]", err) |
| 182 | + } |
| 183 | + |
| 184 | + if ethereumSignature.Version != ethSignatureVersion { |
| 185 | + return fmt.Errorf( |
| 186 | + "unsupported ethereum signature version\n"+ |
| 187 | + "\texpected: %d\n"+ |
| 188 | + "\tactual: %d", |
| 189 | + ethSignatureVersion, |
| 190 | + ethereumSignature.Version, |
| 191 | + ) |
| 192 | + } |
| 193 | + |
| 194 | + digest := sha256.Sum256([]byte(ethereumSignature.Message)) |
| 195 | + |
| 196 | + signatureBytes, err := hex.DecodeString(ethereumSignature.Signature) |
| 197 | + if err != nil { |
| 198 | + return fmt.Errorf("failed to decode signature: [%v]", err) |
| 199 | + } |
| 200 | + |
| 201 | + publicKey, err := crypto.SigToPub(digest[:], signatureBytes) |
| 202 | + if err != nil { |
| 203 | + return fmt.Errorf("could not recover public key from signature [%v]", err) |
| 204 | + } |
| 205 | + |
| 206 | + recoveredAddress := crypto.PubkeyToAddress(*publicKey) |
| 207 | + |
| 208 | + if !bytes.Equal(recoveredAddress.Bytes(), ethereumSignature.Address.Bytes()) { |
| 209 | + return fmt.Errorf( |
| 210 | + "signature verification failed: invalid signer\n"+ |
| 211 | + "\texpected: %s\n"+ |
| 212 | + "\tactual: %s", |
| 213 | + ethereumSignature.Address.Hex(), |
| 214 | + recoveredAddress.Hex(), |
| 215 | + ) |
| 216 | + } |
| 217 | + |
| 218 | + fmt.Printf( |
| 219 | + "signature verified correctly, message [%s] was signed by [%s]\n", |
| 220 | + ethereumSignature.Message, |
| 221 | + recoveredAddress.Hex(), |
| 222 | + ) |
| 223 | + |
| 224 | + return nil |
| 225 | +} |
0 commit comments