api: accept both hex and decimal for block number

This commit is contained in:
Tarun Sharma 2025-05-08 17:57:46 +04:00
parent 47d4bf4bf6
commit 8d0e24b2e2

View file

@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"math"
"strconv"
"strings"
"github.com/XinFinOrg/XDPoSChain/common"
@ -99,10 +100,21 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
return nil
}
blckNum, err := hexutil.DecodeUint64(input)
var blckNum uint64
var err error
//Check if input is valid hex string before converting.
if hexutil.IsValidHexString(input) {
blckNum, err = hexutil.DecodeUint64(input)
} else {
//Try converting input directly into uint64 value
blckNum, err = strconv.ParseUint(input, 10, 64)
}
if err != nil {
return err
}
if blckNum > math.MaxInt64 {
return errors.New("block number larger than int64")
}