github.com/status-im/status-go@v1.1.0/services/wallet/bigint/hex_big_int.go (about)

     1  package bigint
     2  
     3  import (
     4  	"math/big"
     5  	"reflect"
     6  
     7  	"github.com/ethereum/go-ethereum/common/hexutil"
     8  )
     9  
    10  // Unmarshals a u256 as a fixed-length hex string with 0x prefix and leading zeros
    11  type HexBigInt struct {
    12  	*big.Int
    13  }
    14  
    15  const FixedLength = 32 // u256 -> 32 bytes
    16  
    17  var (
    18  	hexBigIntT = reflect.TypeOf(HexBigInt{})
    19  )
    20  
    21  func (b *HexBigInt) UnmarshalJSON(input []byte) error {
    22  	var buf [FixedLength]byte
    23  	err := hexutil.UnmarshalFixedJSON(hexBigIntT, input, buf[:])
    24  
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	z := new(big.Int)
    30  	z.SetBytes(buf[:])
    31  	b.Int = z
    32  	return nil
    33  }