fix sign message decoding hex when needed

This commit is contained in:
Andrea Franz 2019-04-25 16:15:21 +02:00
parent 52f917db0c
commit db003d8a00
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
1 changed files with 14 additions and 4 deletions

View File

@ -675,8 +675,7 @@ func (s *Shell) commandKeycardSignMessage(args ...string) error {
}
originalMessage := strings.Join(args, " ")
wrappedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(originalMessage), originalMessage)
hash := crypto.Keccak256([]byte(wrappedMessage))
hash := hashEthereumMessage(originalMessage)
logger.Info("sign message")
sig, err := s.kCmdSet.Sign(hash)
@ -719,8 +718,7 @@ func (s *Shell) commandKeycardSignMessagePinless(args ...string) error {
}
originalMessage := strings.Join(args, " ")
wrappedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(originalMessage), originalMessage)
hash := crypto.Keccak256([]byte(wrappedMessage))
hash := hashEthereumMessage(originalMessage)
logger.Info("sign message pinless")
sig, err := s.kCmdSet.SignPinless(hash)
@ -824,3 +822,15 @@ func (s *Shell) writeSignatureInfo(sig *types.Signature) {
s.write(fmt.Sprintf("PUBLIC KEY: 0x%x\n", sig.PubKey()))
s.write(fmt.Sprintf("ADDRESS: 0x%x\n\n", address))
}
func hashEthereumMessage(message string) []byte {
data := []byte(message)
if strings.HasPrefix(message, "0x") {
if value, err := hex.DecodeString(message[2:]); err == nil {
data = value
}
}
wrappedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
return crypto.Keccak256([]byte(wrappedMessage))
}