mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
signer, core, apitypes: add support for fixed size arrays
This commit is contained in:
parent
661bd45188
commit
97f4238676
2 changed files with 50 additions and 3 deletions
|
|
@ -240,3 +240,49 @@ func TestConvertAddressDataToSlice(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTypedDataArrayValidate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
typedData := TypedData{
|
||||
Types: Types{
|
||||
"BulkOrder": []Type{
|
||||
// Should be able to accept fixed size arrays
|
||||
{Name: "tree", Type: "OrderComponents[2][2]"},
|
||||
},
|
||||
"OrderComponents": []Type{
|
||||
{Name: "offerer", Type: "address"},
|
||||
{Name: "amount", Type: "uint8"},
|
||||
},
|
||||
"EIP712Domain": []Type{
|
||||
{Name: "name", Type: "string"},
|
||||
{Name: "version", Type: "string"},
|
||||
{Name: "chainId", Type: "uint8"},
|
||||
{Name: "verifyingContract", Type: "address"},
|
||||
},
|
||||
},
|
||||
PrimaryType: "BulkOrder",
|
||||
Domain: TypedDataDomain{
|
||||
VerifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
|
||||
},
|
||||
Message: TypedDataMessage{},
|
||||
}
|
||||
|
||||
if err := typedData.validate(); err != nil {
|
||||
t.Errorf("expected typed data to pass validation, got: %v", err)
|
||||
}
|
||||
|
||||
// Should be able to accept dynamic arrays
|
||||
typedData.Types["BulkOrder"][0].Type = "OrderComponents[]"
|
||||
|
||||
if err := typedData.validate(); err != nil {
|
||||
t.Errorf("expected typed data to pass validation, got: %v", err)
|
||||
}
|
||||
|
||||
// Should be able to accept standard types
|
||||
typedData.Types["BulkOrder"][0].Type = "OrderComponents"
|
||||
|
||||
if err := typedData.validate(); err != nil {
|
||||
t.Errorf("expected typed data to pass validation, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/crypto"
|
||||
)
|
||||
|
||||
var typedDataReferenceTypeRegexp = regexp.MustCompile(`^[A-Za-z](\w*)(\[\])?$`)
|
||||
var typedDataReferenceTypeRegexp = regexp.MustCompile(`^[A-Za-z](\w*)(\[\d*\])*$`)
|
||||
|
||||
type ValidationInfo struct {
|
||||
Typ string `json:"type"`
|
||||
|
|
@ -216,8 +216,9 @@ func (t *Type) isArray() bool {
|
|||
// typeName returns the canonical name of the type. If the type is 'Person[]', then
|
||||
// this method returns 'Person'
|
||||
func (t *Type) typeName() string {
|
||||
if strings.HasSuffix(t.Type, "[]") {
|
||||
return strings.TrimSuffix(t.Type, "[]")
|
||||
if strings.Contains(t.Type, "[") {
|
||||
re := regexp.MustCompile(`\[\d*\]`)
|
||||
return re.ReplaceAllString(t.Type, "")
|
||||
}
|
||||
return t.Type
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue