From a84e8dd5f74eff4d2b9b4437c3df507bf2daa00e Mon Sep 17 00:00:00 2001 From: maskpp Date: Sun, 21 Jul 2024 17:46:58 +0800 Subject: [PATCH] translate bytes32 to common.Hash --- accounts/abi/bind/bind.go | 8 +++----- accounts/abi/error_handling.go | 2 ++ accounts/abi/pack.go | 3 +-- accounts/abi/packing_test.go | 12 ++++++------ accounts/abi/type.go | 16 +++++++++------- accounts/abi/type_test.go | 10 +++++----- accounts/abi/unpack_test.go | 2 +- 7 files changed, 27 insertions(+), 26 deletions(-) diff --git a/accounts/abi/bind/bind.go b/accounts/abi/bind/bind.go index 4ca3cd75fe..42853244bf 100644 --- a/accounts/abi/bind/bind.go +++ b/accounts/abi/bind/bind.go @@ -321,11 +321,9 @@ func bindBasicTypeGo(kind abi.Type) string { } return "*big.Int" case abi.FixedBytesTy: - if kind.Size == 32 { - return "common.Hash" - } else { - return fmt.Sprintf("[%d]byte", kind.Size) - } + return fmt.Sprintf("[%d]byte", kind.Size) + case abi.HashTy: + return "common.Hash" case abi.BytesTy: return "[]byte" case abi.FunctionTy: diff --git a/accounts/abi/error_handling.go b/accounts/abi/error_handling.go index c106e9ac43..92023f665b 100644 --- a/accounts/abi/error_handling.go +++ b/accounts/abi/error_handling.go @@ -78,6 +78,8 @@ func typeCheck(t Type, value reflect.Value) error { return typeErr(t.GetType().Kind(), value.Kind()) } else if t.T == FixedBytesTy && t.Size != value.Len() { return typeErr(t.GetType(), value.Type()) + } else if t.T == HashTy && t.Size != value.Len() { + return typeErr(t.GetType(), value.Type()) } else { return nil } diff --git a/accounts/abi/pack.go b/accounts/abi/pack.go index beef1fa37f..93e81e9d7e 100644 --- a/accounts/abi/pack.go +++ b/accounts/abi/pack.go @@ -41,11 +41,10 @@ func packElement(t Type, reflectValue reflect.Value) ([]byte, error) { return packNum(reflectValue), nil case StringTy: return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil - case AddressTy: + case AddressTy, HashTy: if reflectValue.Kind() == reflect.Array { reflectValue = mustArrayToByteSlice(reflectValue) } - return common.LeftPadBytes(reflectValue.Bytes(), 32), nil case BoolTy: if reflectValue.Bool() { diff --git a/accounts/abi/packing_test.go b/accounts/abi/packing_test.go index eae3b0df20..6b49bd6d16 100644 --- a/accounts/abi/packing_test.go +++ b/accounts/abi/packing_test.go @@ -620,7 +620,7 @@ var packUnpackTests = []packUnpackTest{ { def: `[{"type": "bytes32[]"}]`, - unpacked: [][32]byte{{1}, {2}}, + unpacked: []common.Hash{{1}, {2}}, packed: "0000000000000000000000000000000000000000000000000000000000000020" + "0000000000000000000000000000000000000000000000000000000000000002" + "0100000000000000000000000000000000000000000000000000000000000000" + @@ -792,7 +792,7 @@ var packUnpackTests = []packUnpackTest{ }, { def: `[{"type": "bytes32[][]"}]`, - unpacked: [][][32]byte{{{1}, {2}}, {{3}, {4}, {5}}}, + unpacked: [][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}}, packed: "0000000000000000000000000000000000000000000000000000000000000020" + "0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2 "0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0 @@ -807,7 +807,7 @@ var packUnpackTests = []packUnpackTest{ }, { def: `[{"type": "bytes32[][2]"}]`, - unpacked: [2][][32]byte{{{1}, {2}}, {{3}, {4}, {5}}}, + unpacked: [2][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}}, packed: "0000000000000000000000000000000000000000000000000000000000000020" + "0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0 "00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1 @@ -821,7 +821,7 @@ var packUnpackTests = []packUnpackTest{ }, { 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] "0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1] "0300000000000000000000000000000000000000000000000000000000000000" + // array[0][2] @@ -841,8 +841,8 @@ var packUnpackTests = []packUnpackTest{ B *big.Int C *big.Int D bool - E [2][3][32]byte - }{1, big.NewInt(1), big.NewInt(-1), true, [2][3][32]byte{{{1}, {2}, {3}}, {{3}, {4}, {5}}}}, + E [2][3]common.Hash + }{1, big.NewInt(1), big.NewInt(-1), true, [2][3]common.Hash{{{1}, {2}, {3}}, {{3}, {4}, {5}}}}, packed: "0000000000000000000000000000000000000000000000000000000000000001" + // struct[a] "0000000000000000000000000000000000000000000000000000000000000001" + // struct[b] "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // struct[c] diff --git a/accounts/abi/type.go b/accounts/abi/type.go index d57fa3d4e6..6dd4e2197b 100644 --- a/accounts/abi/type.go +++ b/accounts/abi/type.go @@ -153,13 +153,16 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty case "string": typ.T = StringTy case "bytes": - if varSize == 0 { - typ.T = BytesTy + if varSize > 32 { + return Type{}, fmt.Errorf("unsupported arg type: %s", t) } else { - if varSize > 32 { - return Type{}, fmt.Errorf("unsupported arg type: %s", t) + if varSize == 0 { + typ.T = BytesTy + } else if varSize == 32 { + typ.T = HashTy + } else { + typ.T = FixedBytesTy } - typ.T = FixedBytesTy typ.Size = varSize } case "tuple": @@ -249,8 +252,7 @@ func (t Type) GetType() reflect.Type { case BytesTy: return reflect.SliceOf(reflect.TypeOf(byte(0))) case HashTy: - // hashtype currently not used - return reflect.ArrayOf(32, reflect.TypeOf(byte(0))) + return reflect.TypeOf(common.Hash{}) case FixedPointTy: // fixedpoint type currently not used return reflect.ArrayOf(32, reflect.TypeOf(byte(0))) diff --git a/accounts/abi/type_test.go b/accounts/abi/type_test.go index 95922548c4..c3794888ca 100644 --- a/accounts/abi/type_test.go +++ b/accounts/abi/type_test.go @@ -17,12 +17,12 @@ package abi import ( + "github.com/ethereum/go-ethereum/common" "math/big" "reflect" "testing" "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 @@ -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]"}}, {"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]"}}, - {"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[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[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[2]"}}, + {"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: HashTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[2]"}}, {"string", nil, Type{T: StringTy, 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]"}}, @@ -249,7 +249,7 @@ func TestTypeCheck(t *testing.T) { {"bytes3", nil, [3]byte{}, ""}, {"bytes2", nil, [2]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}, ""}, {"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"}, diff --git a/accounts/abi/unpack_test.go b/accounts/abi/unpack_test.go index 29891ec0a4..bdbc375e0a 100644 --- a/accounts/abi/unpack_test.go +++ b/accounts/abi/unpack_test.go @@ -126,7 +126,7 @@ var unpackTests = []unpackTest{ def: `[{"type": "bytes32"}]`, enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000", 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"}]`,