Skip to content

Commit 6ad620d

Browse files
authored
cmd/ethkey: use accounts.TextHash (#25069)
1 parent 1cf58c7 commit 6ad620d

File tree

3 files changed

+7
-19
lines changed

3 files changed

+7
-19
lines changed

cmd/ethkey/message.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os"
2323

24+
"github.com/ethereum/go-ethereum/accounts"
2425
"github.com/ethereum/go-ethereum/accounts/keystore"
2526
"github.com/ethereum/go-ethereum/cmd/utils"
2627
"github.com/ethereum/go-ethereum/common"
@@ -68,7 +69,7 @@ To sign a message contained in a file, use the --msgfile flag.
6869
utils.Fatalf("Error decrypting key: %v", err)
6970
}
7071

71-
signature, err := crypto.Sign(signHash(message), key.PrivateKey)
72+
signature, err := crypto.Sign(accounts.TextHash(message), key.PrivateKey)
7273
if err != nil {
7374
utils.Fatalf("Failed to sign message: %v", err)
7475
}
@@ -113,7 +114,7 @@ It is possible to refer to a file containing the message.`,
113114
utils.Fatalf("Signature encoding is not hexadecimal: %v", err)
114115
}
115116

116-
recoveredPubkey, err := crypto.SigToPub(signHash(message), signature)
117+
recoveredPubkey, err := crypto.SigToPub(accounts.TextHash(message), signature)
117118
if err != nil || recoveredPubkey == nil {
118119
utils.Fatalf("Signature verification failed: %v", err)
119120
}

cmd/ethkey/utils.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"strings"
2424

2525
"github.com/ethereum/go-ethereum/cmd/utils"
26-
"github.com/ethereum/go-ethereum/crypto"
2726
"gopkg.in/urfave/cli.v1"
2827
)
2928

@@ -46,18 +45,6 @@ func getPassphrase(ctx *cli.Context, confirmation bool) string {
4645
return utils.GetPassPhrase("", confirmation)
4746
}
4847

49-
// signHash is a helper function that calculates a hash for the given message
50-
// that can be safely used to calculate a signature from.
51-
//
52-
// The hash is calculated as
53-
// keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).
54-
//
55-
// This gives context to the signed message and prevents signing of transactions.
56-
func signHash(data []byte) []byte {
57-
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
58-
return crypto.Keccak256([]byte(msg))
59-
}
60-
6148
// mustPrintJSON prints the JSON encoding of the given object and
6249
// exits the program with an error message when the marshaling fails.
6350
func mustPrintJSON(jsonObject interface{}) {

signer/core/signed_data.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (api *SignerAPI) determineSignatureFormat(ctx context.Context, contentType
169169
req = &SignDataRequest{ContentType: mediaType, Rawdata: cliqueRlp, Messages: messages, Hash: sighash}
170170
default: // also case TextPlain.Mime:
171171
// Calculates an Ethereum ECDSA signature for:
172-
// hash = keccak256("\x19${byteVersion}Ethereum Signed Message:\n${message length}${message}")
172+
// hash = keccak256("\x19Ethereum Signed Message:\n${message length}${message}")
173173
// We expect it to be a string
174174
if stringData, ok := data.(string); !ok {
175175
return nil, useEthereumV, fmt.Errorf("input for text/plain must be an hex-encoded string")
@@ -194,7 +194,7 @@ func (api *SignerAPI) determineSignatureFormat(ctx context.Context, contentType
194194
return req, useEthereumV, nil
195195
}
196196

197-
// SignTextWithValidator signs the given message which can be further recovered
197+
// SignTextValidator signs the given message which can be further recovered
198198
// with the given validator.
199199
// hash = keccak256("\x19\x00"${address}${data}).
200200
func SignTextValidator(validatorData apitypes.ValidatorData) (hexutil.Bytes, string) {
@@ -271,11 +271,11 @@ func (api *SignerAPI) EcRecover(ctx context.Context, data hexutil.Bytes, sig hex
271271
//
272272
// Note, this function is compatible with eth_sign and personal_sign. As such it recovers
273273
// the address of:
274-
// hash = keccak256("\x19${byteVersion}Ethereum Signed Message:\n${message length}${message}")
274+
// hash = keccak256("\x19Ethereum Signed Message:\n${message length}${message}")
275275
// addr = ecrecover(hash, signature)
276276
//
277277
// Note, the signature must conform to the secp256k1 curve R, S and V values, where
278-
// the V value must be be 27 or 28 for legacy reasons.
278+
// the V value must be 27 or 28 for legacy reasons.
279279
//
280280
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_ecRecover
281281
if len(sig) != 65 {

0 commit comments

Comments
 (0)