Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
e76813eb
Unverified
Commit
e76813eb
authored
Nov 24, 2022
by
6xiaowu9
Committed by
GitHub
Nov 24, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
signer/core/apitypes: deep convert types in slice (#26203)
parent
8846c07d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
3 deletions
+52
-3
signed_data_internal_test.go
signer/core/apitypes/signed_data_internal_test.go
+36
-0
types.go
signer/core/apitypes/types.go
+16
-3
No files found.
signer/core/apitypes/signed_data_internal_test.go
View file @
e76813eb
...
...
@@ -23,6 +23,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
)
func
TestBytesPadding
(
t
*
testing
.
T
)
{
...
...
@@ -197,3 +198,38 @@ func TestParseInteger(t *testing.T) {
}
}
}
func
TestConvertStringDataToSlice
(
t
*
testing
.
T
)
{
slice
:=
[]
string
{
"a"
,
"b"
,
"c"
}
var
it
interface
{}
=
slice
_
,
err
:=
convertDataToSlice
(
it
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
}
func
TestConvertUint256DataToSlice
(
t
*
testing
.
T
)
{
slice
:=
[]
*
math
.
HexOrDecimal256
{
math
.
NewHexOrDecimal256
(
1
),
math
.
NewHexOrDecimal256
(
2
),
math
.
NewHexOrDecimal256
(
3
),
}
var
it
interface
{}
=
slice
_
,
err
:=
convertDataToSlice
(
it
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
}
func
TestConvertAddressDataToSlice
(
t
*
testing
.
T
)
{
slice
:=
[]
common
.
Address
{
common
.
HexToAddress
(
"0x0000000000000000000000000000000000000001"
),
common
.
HexToAddress
(
"0x0000000000000000000000000000000000000002"
),
common
.
HexToAddress
(
"0x0000000000000000000000000000000000000003"
),
}
var
it
interface
{}
=
slice
_
,
err
:=
convertDataToSlice
(
it
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
}
signer/core/apitypes/types.go
View file @
e76813eb
...
...
@@ -367,8 +367,8 @@ func (typedData *TypedData) EncodeData(primaryType string, data map[string]inter
encType
:=
field
.
Type
encValue
:=
data
[
field
.
Name
]
if
encType
[
len
(
encType
)
-
1
:
]
==
"]"
{
arrayValue
,
ok
:=
encValue
.
([]
interface
{}
)
if
!
ok
{
arrayValue
,
err
:=
convertDataToSlice
(
encValue
)
if
err
!=
nil
{
return
nil
,
dataMismatchError
(
encType
,
encValue
)
}
...
...
@@ -573,6 +573,19 @@ func dataMismatchError(encType string, encValue interface{}) error {
return
fmt
.
Errorf
(
"provided data '%v' doesn't match type '%s'"
,
encValue
,
encType
)
}
func
convertDataToSlice
(
encValue
interface
{})
([]
interface
{},
error
)
{
var
outEncValue
[]
interface
{}
rv
:=
reflect
.
ValueOf
(
encValue
)
if
rv
.
Kind
()
==
reflect
.
Slice
{
for
i
:=
0
;
i
<
rv
.
Len
();
i
++
{
outEncValue
=
append
(
outEncValue
,
rv
.
Index
(
i
)
.
Interface
())
}
}
else
{
return
outEncValue
,
fmt
.
Errorf
(
"provided data '%v' is not slice"
,
encValue
)
}
return
outEncValue
,
nil
}
// validate makes sure the types are sound
func
(
typedData
*
TypedData
)
validate
()
error
{
if
err
:=
typedData
.
Types
.
validate
();
err
!=
nil
{
...
...
@@ -632,7 +645,7 @@ func (typedData *TypedData) formatData(primaryType string, data map[string]inter
Typ
:
field
.
Type
,
}
if
field
.
isArray
()
{
arrayValue
,
_
:=
encValue
.
([]
interface
{}
)
arrayValue
,
_
:=
convertDataToSlice
(
encValue
)
parsedType
:=
field
.
typeName
()
for
_
,
v
:=
range
arrayValue
{
if
typedData
.
Types
[
parsedType
]
!=
nil
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment