signer: EIP 712, parse bytes and bytesX as hex strings + correct padding#21307
Merged
holiman merged 6 commits intoethereum:masterfrom Aug 3, 2020
curvegrid:typed-data-hex-bytes
Merged
signer: EIP 712, parse bytes and bytesX as hex strings + correct padding#21307holiman merged 6 commits intoethereum:masterfrom curvegrid:typed-data-hex-bytes
bytes and bytesX as hex strings + correct padding#21307holiman merged 6 commits intoethereum:masterfrom
curvegrid:typed-data-hex-bytes
Conversation
Contributor
|
I tried to add the following, but you seem to have disabled maintainer-commit to your repo: diff --git a/signer/core/signed_data_internal_test.go b/signer/core/signed_data_internal_test.go
index 4e6e177084..53f7d8dc86 100644
--- a/signer/core/signed_data_internal_test.go
+++ b/signer/core/signed_data_internal_test.go
@@ -33,6 +33,7 @@ func TestParseBytes(t *testing.T) {
{"0x1234", []byte{0x12, 0x34}},
{[]byte{12, 34}, []byte{12, 34}},
{hexutil.Bytes([]byte{12, 34}), []byte{12, 34}},
+ {"1234", nil}, // not a proper hex-string
{"not a hex string", nil},
{15, nil},
{nil, nil},Another testcase to verify that we don't treat ambiguous strings as hexdata. |
holiman
suggested changes
Jul 23, 2020
Contributor
|
Oh, and thanks for digging into this! |
Contributor
|
Thanks, it's starting to looks good, we're almost there now. Could you please apply this diff? (some nitpicks plus a couple of more testcases) diff --git a/signer/core/signed_data_internal_test.go b/signer/core/signed_data_internal_test.go
index be55bff3d6..9768ee0b3f 100644
--- a/signer/core/signed_data_internal_test.go
+++ b/signer/core/signed_data_internal_test.go
@@ -41,6 +41,11 @@ func TestBytesPadding(t *testing.T) {
Input: []byte{1},
Output: []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
},
+ {
+ Type: "bytes1",
+ Input: []byte{1, 2},
+ Output: nil,
+ },
{
Type: "bytes7",
Input: []byte{1, 2, 3, 4, 5, 6, 7},
@@ -51,24 +56,29 @@ func TestBytesPadding(t *testing.T) {
Input: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
Output: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
},
+ {
+ Type: "bytes32",
+ Input: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33},
+ Output: nil,
+ },
}
d := TypedData{}
- for _, test := range tests {
+ for i, test := range tests {
val, err := d.EncodePrimitiveValue(test.Type, test.Input, 1)
if test.Output == nil {
if err == nil {
- t.Errorf("expected error, got nil error with result %v: case %v", val, test)
+ t.Errorf("test %d: expected error, got no error (result %x)", i, val)
}
} else {
if err != nil {
- t.Errorf("expected nil error, got %v: case %v", err, test)
+ t.Errorf("test %d: expected no error, got %v", i, err)
}
if len(val) != 32 {
- t.Errorf("expected len 32, got %v: case %v", len(val), test)
+ t.Errorf("test %d: expected len 32, got %d", i, len(val))
}
- if fmt.Sprint(val) != fmt.Sprint(test.Output) {
- t.Errorf("expected %x, got %x: case %v", test.Output, val, test)
+ if !bytes.Equal(val, test.Output) {
+ t.Errorf("test %d: expected %x, got %x", i, test.Output, val)
}
}
}
@@ -92,15 +102,15 @@ func TestParseBytes(t *testing.T) {
out, ok := parseBytes(tt.v)
if tt.exp == nil {
if ok || out != nil {
- t.Errorf("Case %d: expecting !ok, got ok = %v with out = %x", i, ok, out)
+ t.Errorf("test %d: expected !ok, got ok = %v with out = %x", i, ok, out)
}
continue
}
if !ok {
- t.Errorf("Case %d: expecting ok got !ok", i)
+ t.Errorf("test %d: expected ok got !ok", i)
}
if !bytes.Equal(out, tt.exp) {
- t.Errorf("Case %d: expecting %x got %x", i, tt.exp, out)
+ t.Errorf("test %d: expected %x got %x", i, tt.exp, out)
}
}
}
|
bytes and bytesX types as a hex stringbytes and bytesX as hex strings + correct padding
This was referenced Feb 24, 2021
enriquefynn
pushed a commit
to enriquefynn/go-ethereum
that referenced
this pull request
Mar 10, 2021
…padding (ethereum#21307) * Handle hex strings for bytesX types * Add tests for parseBytes * Improve tests * Return nil bytes if error is non-nil * Right-pad instead of left-pad bytes * More tests
MariusVanDerWijden
pushed a commit
to MariusVanDerWijden/go-ethereum
that referenced
this pull request
Feb 2, 2026
…padding (ethereum#21307) * Handle hex strings for bytesX types * Add tests for parseBytes * Improve tests * Return nil bytes if error is non-nil * Right-pad instead of left-pad bytes * More tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I've come across a lot of input TypedData where bytes and bytesX is encoded as a hex string (
0x...).This adds support for parsing those inputs in the
TypedData.EncodePritimitiveValuemethod.Tests are included.
This change is