Skip to content

Commit

Permalink
Expose API to directly call HashStruct from EIP-712, without the full…
Browse files Browse the repository at this point in the history
… TypedDataV4 domain

Signed-off-by: Peter Broadhurst <peter.broadhurst@kaleido.io>
  • Loading branch information
peterbroadhurst committed Jul 15, 2024
1 parent a85f6e1 commit a947511
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/eip712/typed_data_v4.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -223,6 +223,11 @@ func encodeData(ctx context.Context, typeName string, v interface{}, allTypes Ty
return encoded, nil
}

// HashStruct allows hashing of an individual structure, without the EIP-712 domain
func HashStruct(ctx context.Context, typeName string, v interface{}, allTypes TypeSet) (result ethtypes.HexBytes0xPrefix, err error) {
return hashStruct(ctx, typeName, v, allTypes, "")
}

func hashStruct(ctx context.Context, typeName string, v interface{}, allTypes TypeSet, breadcrumbs string) (result ethtypes.HexBytes0xPrefix, err error) {
encoded, err := encodeData(ctx, typeName, v, allTypes, breadcrumbs)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/eip712/typed_data_v4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func TestMessage_ExampleFromEIP712Spec(t *testing.T) {
ed, err := EncodeTypedDataV4(ctx, &p)
assert.NoError(t, err)
assert.Equal(t, "0xde26f53b35dd5ffdc13f8297e5cc7bbcb1a04bf33803bd2bf4a45eb251360cb8", ed.String())

hs, err := HashStruct(ctx, p.PrimaryType, p.Message, p.Types)
assert.NoError(t, err)
assert.Equal(t, "0xc52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e", hs.String())
}

func TestMessage_EmptyMessage(t *testing.T) {
Expand Down

0 comments on commit a947511

Please sign in to comment.