github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/consensus/bft/utils.go (about) 1 package bft 2 3 import ( 4 "github.com/quickchainproject/quickchain/common" 5 "github.com/quickchainproject/quickchain/crypto" 6 "github.com/quickchainproject/quickchain/crypto/sha3" 7 "github.com/quickchainproject/quickchain/log" 8 "github.com/quickchainproject/quickchain/rlp" 9 ) 10 11 func RLPHash(v interface{}) (h common.Hash) { 12 hw := sha3.NewKeccak256() 13 rlp.Encode(hw, v) 14 hw.Sum(h[:0]) 15 return h 16 } 17 18 // GetSignatureAddress gets the signer address from the signature 19 func GetSignatureAddress(data []byte, sig []byte) (common.Address, error) { 20 // 1. Keccak data 21 hashData := crypto.Keccak256([]byte(data)) 22 // 2. Recover public key 23 pubkey, err := crypto.SigToPub(hashData, sig) 24 if err != nil { 25 return common.Address{}, err 26 } 27 return crypto.PubkeyToAddress(*pubkey), nil 28 } 29 30 func CheckValidatorSignature(valSet ValidatorSet, data []byte, sig []byte) (common.Address, error) { 31 // 1. Get signature address 32 signer, err := GetSignatureAddress(data, sig) 33 if err != nil { 34 log.Error("Failed to get signer address", "err", err) 35 return common.Address{}, err 36 } 37 38 // 2. Check validator 39 if _, val := valSet.GetByAddress(signer); val != nil { 40 return val.Address(), nil 41 } 42 43 return common.Address{}, ErrUnauthorizedAddress 44 }