utils.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser 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. // The go-ethereum library 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 Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package istanbul
  17. import (
  18. "github.com/ethereum/go-ethereum/common"
  19. "github.com/ethereum/go-ethereum/crypto"
  20. "github.com/ethereum/go-ethereum/log"
  21. "github.com/ethereum/go-ethereum/rlp"
  22. "golang.org/x/crypto/sha3"
  23. )
  24. func RLPHash(v interface{}) (h common.Hash) {
  25. hw := sha3.NewLegacyKeccak256()
  26. rlp.Encode(hw, v)
  27. hw.Sum(h[:0])
  28. return h
  29. }
  30. // GetSignatureAddress gets the signer address from the signature
  31. func GetSignatureAddress(data []byte, sig []byte) (common.Address, error) {
  32. // 1. Keccak data
  33. hashData := crypto.Keccak256(data)
  34. // 2. Recover public key
  35. pubkey, err := crypto.SigToPub(hashData, sig)
  36. if err != nil {
  37. return common.Address{}, err
  38. }
  39. return crypto.PubkeyToAddress(*pubkey), nil
  40. }
  41. // GetSignatureAddressNoHashing gets the signer address from the signature without first hashing the data
  42. func GetSignatureAddressNoHashing(data []byte, sig []byte) (common.Address, error) {
  43. pubkey, err := crypto.SigToPub(data, sig)
  44. if err != nil {
  45. return common.Address{}, err
  46. }
  47. return crypto.PubkeyToAddress(*pubkey), nil
  48. }
  49. func CheckValidatorSignature(valSet ValidatorSet, data []byte, sig []byte) (common.Address, error) {
  50. // 1. Get signature address
  51. signer, err := GetSignatureAddress(data, sig)
  52. if err != nil {
  53. log.Error("Failed to get signer address", "err", err)
  54. return common.Address{}, err
  55. }
  56. // 2. Check validator
  57. if _, val := valSet.GetByAddress(signer); val != nil {
  58. return val.Address(), nil
  59. }
  60. return common.Address{}, ErrUnauthorizedAddress
  61. }