accounts/abi: style changes

This commit is contained in:
MariusVanDerWijden 2025-06-04 14:42:59 +02:00
parent 9574d543d0
commit 566afae9bf
2 changed files with 10 additions and 17 deletions

View file

@ -33,26 +33,20 @@ func packBytesSlice(bytes []byte, l int) []byte {
return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...)
}
// validateNum returns an error if the underlying type of t is uint and the
// value of reflectValue is a negative big.Int
func validateNum(t Type, reflectValue reflect.Value) error {
if t.T == UintTy && reflectValue.Kind() == reflect.Ptr {
val := new(big.Int).Set(reflectValue.Interface().(*big.Int))
if val.Sign() == -1 {
return errInvalidSign
}
}
return nil
}
// packElement packs the given reflect value according to the abi specification in
// t.
func packElement(t Type, reflectValue reflect.Value) ([]byte, error) {
switch t.T {
case IntTy, UintTy:
if err := validateNum(t, reflectValue); err != nil {
return nil, err
case UintTy:
// make sure to not pack a negative value into a uint type.
if reflectValue.Kind() == reflect.Ptr {
val := new(big.Int).Set(reflectValue.Interface().(*big.Int))
if val.Sign() == -1 {
return nil, errInvalidSign
}
}
return packNum(reflectValue), nil
case IntTy:
return packNum(reflectValue), nil
case StringTy:
return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil

View file

@ -179,8 +179,7 @@ func TestMethodPack(t *testing.T) {
}
// test that we can't pack a negative value for a parameter that is specified as a uint
_, err = abi.Pack("send", big.NewInt(-1))
if err == nil {
if _, err := abi.Pack("send", big.NewInt(-1)); err == nil {
t.Fatal("expected error when trying to pack negative big.Int into uint256 value")
}
}