signTypedData
Signs typed data with the Account's private key
signTypedData
Signs typed data and calculates an Ethereum-specific signature in EIP-712 format: sign(keccak256("\x19\x01" ‖ domainSeparator ‖ hashStruct(message))).
- For local accounts (implementing TypedDataSignableAccount), signs locally without an RPC call.
- For JSON-RPC accounts, delegates to the
eth_signTypedData_v4RPC method.
Import
Import the wallet actions package so you can call this action.
import "github.com/ChefBingbong/viem-go/actions/wallet"Usage
See how to construct a wallet client and call this action.
import ( "context" "log" "math/big"
"github.com/ChefBingbong/viem-go/actions/wallet" "github.com/ChefBingbong/viem-go/client" "github.com/ChefBingbong/viem-go/client/transport" "github.com/ChefBingbong/viem-go/chain/definitions" "github.com/ChefBingbong/viem-go/utils/signature")
ctx := context.Background()
walletClient, err := client.CreateWalletClient(client.WalletClientConfig{ Chain: definitions.Mainnet, Transport: transport.HTTP("https://eth.llamarpc.com"),})if err != nil { log.Fatal(err)}defer func() { _ = walletClient.Close() }()
sig, err := wallet.SignTypedData(ctx, walletClient, wallet.SignTypedDataParameters{ Domain: signature.TypedDataDomain{ Name: "Ether Mail", Version: "1", ChainId: big.NewInt(1), VerifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC", }, Types: map[string][]signature.TypedDataField{ "Person": { {Name: "name", Type: "string"}, {Name: "wallet", Type: "address"}, }, "Mail": { {Name: "from", Type: "Person"}, {Name: "to", Type: "Person"}, {Name: "contents", Type: "string"}, }, }, PrimaryType: "Mail", Message: map[string]any{ "from": map[string]any{"name": "Cow", "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"}, "to": map[string]any{"name": "Bob", "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"}, "contents": "Hello, Bob!", },})if err != nil { log.Fatal(err)}log.Printf("Signature: %s", sig)Returns
string
The signed data as a hex string.
Parameters
Account
Control which account is used to sign the typed data.
-
Type:
Account -
Optional
Account to use for signing. If nil, uses the client's account.
Domain
Provide the EIP-712 domain configuration for the typed data.
- Type:
signature.TypedDataDomain
-- Required
The typed data domain. All properties are optional.
import ( "math/big" "github.com/ChefBingbong/viem-go/utils/signature")
sig, err := wallet.SignTypedData(ctx, walletClient, wallet.SignTypedDataParameters{ Domain: signature.TypedDataDomain{ Name: "Ether Mail", Version: "1", ChainId: big.NewInt(1), VerifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC", }, // ... types, primaryType, message})Types
Define the set of structured types used by the typed data.
- Type:
map[string][]signature.TypedDataField
-- Required
The type definitions for the typed data (excluding EIP712Domain, which is auto-generated).
import "github.com/ChefBingbong/viem-go/utils/signature"
sig, err := wallet.SignTypedData(ctx, walletClient, wallet.SignTypedDataParameters{ Types: map[string][]signature.TypedDataField{ "Person": { {Name: "name", Type: "string"}, {Name: "wallet", Type: "address"}, }, "Mail": { {Name: "from", Type: "Person"}, {Name: "to", Type: "Person"}, {Name: "contents", Type: "string"}, }, }, // ... domain, primaryType, message})PrimaryType
Specify the primary type to sign from the types mapping.
- Type:
string
-- Required
The primary type to extract from types and use in message.
sig, err := wallet.SignTypedData(ctx, walletClient, wallet.SignTypedDataParameters{ PrimaryType: "Mail", // ... domain, types, message})Message
Provide the structured message payload that matches the primary type.
- Type:
map[string]any
-- Required
The structured message to sign, matching the PrimaryType structure.
sig, err := wallet.SignTypedData(ctx, walletClient, wallet.SignTypedDataParameters{ Message: map[string]any{ "from": map[string]any{"name": "Cow", "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"}, "to": map[string]any{"name": "Bob", "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"}, "contents": "Hello, Bob!", }, // ... domain, types, primaryType})JSON-RPC Methods
- JSON-RPC Accounts:
eth_signTypedData_v4 - Local Accounts: Signs locally. No JSON-RPC request.