translate bytes32 to common.Hash

This commit is contained in:
maskpp 2024-07-21 17:46:58 +08:00
parent c3a32e9131
commit a84e8dd5f7
7 changed files with 27 additions and 26 deletions

View file

@ -321,11 +321,9 @@ func bindBasicTypeGo(kind abi.Type) string {
} }
return "*big.Int" return "*big.Int"
case abi.FixedBytesTy: case abi.FixedBytesTy:
if kind.Size == 32 { return fmt.Sprintf("[%d]byte", kind.Size)
return "common.Hash" case abi.HashTy:
} else { return "common.Hash"
return fmt.Sprintf("[%d]byte", kind.Size)
}
case abi.BytesTy: case abi.BytesTy:
return "[]byte" return "[]byte"
case abi.FunctionTy: case abi.FunctionTy:

View file

@ -78,6 +78,8 @@ func typeCheck(t Type, value reflect.Value) error {
return typeErr(t.GetType().Kind(), value.Kind()) return typeErr(t.GetType().Kind(), value.Kind())
} else if t.T == FixedBytesTy && t.Size != value.Len() { } else if t.T == FixedBytesTy && t.Size != value.Len() {
return typeErr(t.GetType(), value.Type()) return typeErr(t.GetType(), value.Type())
} else if t.T == HashTy && t.Size != value.Len() {
return typeErr(t.GetType(), value.Type())
} else { } else {
return nil return nil
} }

View file

@ -41,11 +41,10 @@ func packElement(t Type, reflectValue reflect.Value) ([]byte, error) {
return packNum(reflectValue), nil return packNum(reflectValue), nil
case StringTy: case StringTy:
return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil
case AddressTy: case AddressTy, HashTy:
if reflectValue.Kind() == reflect.Array { if reflectValue.Kind() == reflect.Array {
reflectValue = mustArrayToByteSlice(reflectValue) reflectValue = mustArrayToByteSlice(reflectValue)
} }
return common.LeftPadBytes(reflectValue.Bytes(), 32), nil return common.LeftPadBytes(reflectValue.Bytes(), 32), nil
case BoolTy: case BoolTy:
if reflectValue.Bool() { if reflectValue.Bool() {

View file

@ -620,7 +620,7 @@ var packUnpackTests = []packUnpackTest{
{ {
def: `[{"type": "bytes32[]"}]`, def: `[{"type": "bytes32[]"}]`,
unpacked: [][32]byte{{1}, {2}}, unpacked: []common.Hash{{1}, {2}},
packed: "0000000000000000000000000000000000000000000000000000000000000020" + packed: "0000000000000000000000000000000000000000000000000000000000000020" +
"0000000000000000000000000000000000000000000000000000000000000002" + "0000000000000000000000000000000000000000000000000000000000000002" +
"0100000000000000000000000000000000000000000000000000000000000000" + "0100000000000000000000000000000000000000000000000000000000000000" +
@ -792,7 +792,7 @@ var packUnpackTests = []packUnpackTest{
}, },
{ {
def: `[{"type": "bytes32[][]"}]`, def: `[{"type": "bytes32[][]"}]`,
unpacked: [][][32]byte{{{1}, {2}}, {{3}, {4}, {5}}}, unpacked: [][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}},
packed: "0000000000000000000000000000000000000000000000000000000000000020" + packed: "0000000000000000000000000000000000000000000000000000000000000020" +
"0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2 "0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2
"0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0 "0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
@ -807,7 +807,7 @@ var packUnpackTests = []packUnpackTest{
}, },
{ {
def: `[{"type": "bytes32[][2]"}]`, def: `[{"type": "bytes32[][2]"}]`,
unpacked: [2][][32]byte{{{1}, {2}}, {{3}, {4}, {5}}}, unpacked: [2][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}},
packed: "0000000000000000000000000000000000000000000000000000000000000020" + packed: "0000000000000000000000000000000000000000000000000000000000000020" +
"0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0 "0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
"00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1 "00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1
@ -821,7 +821,7 @@ var packUnpackTests = []packUnpackTest{
}, },
{ {
def: `[{"type": "bytes32[3][2]"}]`, def: `[{"type": "bytes32[3][2]"}]`,
unpacked: [2][3][32]byte{{{1}, {2}, {3}}, {{3}, {4}, {5}}}, unpacked: [2][3]common.Hash{{{1}, {2}, {3}}, {{3}, {4}, {5}}},
packed: "0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0] packed: "0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
"0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1] "0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
"0300000000000000000000000000000000000000000000000000000000000000" + // array[0][2] "0300000000000000000000000000000000000000000000000000000000000000" + // array[0][2]
@ -841,8 +841,8 @@ var packUnpackTests = []packUnpackTest{
B *big.Int B *big.Int
C *big.Int C *big.Int
D bool D bool
E [2][3][32]byte E [2][3]common.Hash
}{1, big.NewInt(1), big.NewInt(-1), true, [2][3][32]byte{{{1}, {2}, {3}}, {{3}, {4}, {5}}}}, }{1, big.NewInt(1), big.NewInt(-1), true, [2][3]common.Hash{{{1}, {2}, {3}}, {{3}, {4}, {5}}}},
packed: "0000000000000000000000000000000000000000000000000000000000000001" + // struct[a] packed: "0000000000000000000000000000000000000000000000000000000000000001" + // struct[a]
"0000000000000000000000000000000000000000000000000000000000000001" + // struct[b] "0000000000000000000000000000000000000000000000000000000000000001" + // struct[b]
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // struct[c] "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // struct[c]

View file

@ -153,13 +153,16 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
case "string": case "string":
typ.T = StringTy typ.T = StringTy
case "bytes": case "bytes":
if varSize == 0 { if varSize > 32 {
typ.T = BytesTy return Type{}, fmt.Errorf("unsupported arg type: %s", t)
} else { } else {
if varSize > 32 { if varSize == 0 {
return Type{}, fmt.Errorf("unsupported arg type: %s", t) typ.T = BytesTy
} else if varSize == 32 {
typ.T = HashTy
} else {
typ.T = FixedBytesTy
} }
typ.T = FixedBytesTy
typ.Size = varSize typ.Size = varSize
} }
case "tuple": case "tuple":
@ -249,8 +252,7 @@ func (t Type) GetType() reflect.Type {
case BytesTy: case BytesTy:
return reflect.SliceOf(reflect.TypeOf(byte(0))) return reflect.SliceOf(reflect.TypeOf(byte(0)))
case HashTy: case HashTy:
// hashtype currently not used return reflect.TypeOf(common.Hash{})
return reflect.ArrayOf(32, reflect.TypeOf(byte(0)))
case FixedPointTy: case FixedPointTy:
// fixedpoint type currently not used // fixedpoint type currently not used
return reflect.ArrayOf(32, reflect.TypeOf(byte(0))) return reflect.ArrayOf(32, reflect.TypeOf(byte(0)))

View file

@ -17,12 +17,12 @@
package abi package abi
import ( import (
"github.com/ethereum/go-ethereum/common"
"math/big" "math/big"
"reflect" "reflect"
"testing" "testing"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/common"
) )
// typeWithoutStringer is an alias for the Type type which simply doesn't implement // typeWithoutStringer is an alias for the Type type which simply doesn't implement
@ -78,11 +78,11 @@ func TestTypeRegexp(t *testing.T) {
{"uint64[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[2]"}}, {"uint64[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[2]"}},
{"uint256[]", nil, Type{T: SliceTy, Elem: &Type{Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[]"}}, {"uint256[]", nil, Type{T: SliceTy, Elem: &Type{Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[]"}},
{"uint256[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[2]"}}, {"uint256[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[2]"}},
{"bytes32", nil, Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}}, {"bytes32", nil, Type{T: HashTy, Size: 32, stringKind: "bytes32"}},
{"bytes[]", nil, Type{T: SliceTy, Elem: &Type{T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[]"}}, {"bytes[]", nil, Type{T: SliceTy, Elem: &Type{T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[]"}},
{"bytes[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[2]"}}, {"bytes[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[2]"}},
{"bytes32[]", nil, Type{T: SliceTy, Elem: &Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[]"}}, {"bytes32[]", nil, Type{T: SliceTy, Elem: &Type{T: HashTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[]"}},
{"bytes32[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[2]"}}, {"bytes32[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: HashTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[2]"}},
{"string", nil, Type{T: StringTy, stringKind: "string"}}, {"string", nil, Type{T: StringTy, stringKind: "string"}},
{"string[]", nil, Type{T: SliceTy, Elem: &Type{T: StringTy, stringKind: "string"}, stringKind: "string[]"}}, {"string[]", nil, Type{T: SliceTy, Elem: &Type{T: StringTy, stringKind: "string"}, stringKind: "string[]"}},
{"string[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: StringTy, stringKind: "string"}, stringKind: "string[2]"}}, {"string[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: StringTy, stringKind: "string"}, stringKind: "string[2]"}},
@ -249,7 +249,7 @@ func TestTypeCheck(t *testing.T) {
{"bytes3", nil, [3]byte{}, ""}, {"bytes3", nil, [3]byte{}, ""},
{"bytes2", nil, [2]byte{}, ""}, {"bytes2", nil, [2]byte{}, ""},
{"bytes1", nil, [1]byte{}, ""}, {"bytes1", nil, [1]byte{}, ""},
{"bytes32", nil, [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"}, {"bytes32", nil, [33]byte{}, "abi: cannot use [33]uint8 as type common.Hash as argument"},
{"bytes32", nil, common.Hash{1}, ""}, {"bytes32", nil, common.Hash{1}, ""},
{"bytes31", nil, common.Hash{1}, "abi: cannot use common.Hash as type [31]uint8 as argument"}, {"bytes31", nil, common.Hash{1}, "abi: cannot use common.Hash as type [31]uint8 as argument"},
{"bytes31", nil, [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"}, {"bytes31", nil, [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"},

View file

@ -126,7 +126,7 @@ var unpackTests = []unpackTest{
def: `[{"type": "bytes32"}]`, def: `[{"type": "bytes32"}]`,
enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000", enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
want: []byte(nil), want: []byte(nil),
err: "abi: cannot unmarshal [32]uint8 in to []uint8", err: "abi: cannot unmarshal common.Hash in to []uint8",
}, },
{ {
def: `[{"name":"___","type":"int256"}]`, def: `[{"name":"___","type":"int256"}]`,