Skip to content

Commit

Permalink
Moved the allowed block number to a constant and error message return…
Browse files Browse the repository at this point in the history
…ed back
  • Loading branch information
anandsnet committed Jun 4, 2019
1 parent 017986f commit 3bb4e0e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 7 additions & 2 deletions authutils/auth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import (

// Extracts the signer address from signature given the signature
// It returns signer address and error. nil error indicates the successful function execution

const (
AllowedBlockChainDifference = 5
)

func GetSignerAddressFromMessage(message, signature []byte) (signer *common.Address, err error) {
log := log.WithFields(log.Fields{
"message": blockchain.BytesToBase64(message),
Expand Down Expand Up @@ -71,8 +76,8 @@ func CompareWithLatestBlockNumber(blockNumberPassed *big.Int) error {
return err
}
differenceInBlockNumber := blockNumberPassed.Sub(blockNumberPassed, latestBlockNumber)
if differenceInBlockNumber.Abs(differenceInBlockNumber).Uint64() > 5 {
return fmt.Errorf("difference between the latest block chain number and the block number passed is %v ", differenceInBlockNumber)
if differenceInBlockNumber.Abs(differenceInBlockNumber).Uint64() > AllowedBlockChainDifference {
return fmt.Errorf("authentication failed as the signature passed has expired")
}
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion authutils/auth_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (

func TestCompareWithLatestBlockNumber(t *testing.T) {
config.Vip().Set(config.EthereumJsonRpcEndpointKey, "https://ropsten.infura.io")
config.Validate()
currentBlockNum, _ := CurrentBlock()
err := CompareWithLatestBlockNumber(currentBlockNum.Add(currentBlockNum, big.NewInt(13)))
assert.Equal(t, err.Error(), "difference between the latest block chain number and the block number passed is 13 ")
assert.Equal(t, err.Error(), "authentication failed as the signature passed has expired")

currentBlockNum, _ = CurrentBlock()
err = CompareWithLatestBlockNumber(currentBlockNum.Add(currentBlockNum, big.NewInt(1)))
Expand Down

0 comments on commit 3bb4e0e

Please sign in to comment.