mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
translate bytes32 to common.Hash
This commit is contained in:
parent
c3a32e9131
commit
a84e8dd5f7
7 changed files with 27 additions and 26 deletions
|
|
@ -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)
|
||||
}
|
||||
case abi.HashTy:
|
||||
return "common.Hash"
|
||||
case abi.BytesTy:
|
||||
return "[]byte"
|
||||
case abi.FunctionTy:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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
|
||||
} else {
|
||||
if varSize > 32 {
|
||||
return Type{}, fmt.Errorf("unsupported arg type: %s", t)
|
||||
}
|
||||
} else {
|
||||
if varSize == 0 {
|
||||
typ.T = BytesTy
|
||||
} else if varSize == 32 {
|
||||
typ.T = HashTy
|
||||
} else {
|
||||
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)))
|
||||
|
|
|
|||
|
|
@ -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"},
|
||||
|
|
|
|||
|
|
@ -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"}]`,
|
||||
|
|
|
|||
Loading…
Reference in a new issue