github.com/ethersphere/bee/v2@v2.2.0/pkg/crypto/eip712/typeddata.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package eip712
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/ethereum/go-ethereum/signer/core/apitypes"
    11  )
    12  
    13  // type aliases to avoid importing "core" everywhere
    14  type TypedData = apitypes.TypedData
    15  type TypedDataDomain = apitypes.TypedDataDomain
    16  type Types = apitypes.Types
    17  type Type = apitypes.Type
    18  type TypedDataMessage = apitypes.TypedDataMessage
    19  
    20  // EncodeForSigning encodes the hash that will be signed for the given EIP712 data
    21  func EncodeForSigning(typedData *TypedData) ([]byte, error) {
    22  	domainSeparator, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map())
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	typedDataHash, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	rawData := []byte(fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(typedDataHash)))
    33  	return rawData, nil
    34  }
    35  
    36  // EIP712DomainType is the type description for the EIP712 Domain
    37  var EIP712DomainType = []Type{
    38  	{
    39  		Name: "name",
    40  		Type: "string",
    41  	},
    42  	{
    43  		Name: "version",
    44  		Type: "string",
    45  	},
    46  	{
    47  		Name: "chainId",
    48  		Type: "uint256",
    49  	},
    50  }