viem-goviem-go

signMessage

Signs a message with the Account's private key

  • For local accounts (implementing SignableAccount), signs locally without an RPC call.
  • For JSON-RPC accounts, delegates to the personal_sign RPC 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"
"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.SignMessage(ctx, walletClient, wallet.SignMessageParameters{
Message: signature.NewSignableMessage("hello world"),
})
if err != nil {
log.Fatal(err)
}
log.Printf("Signature: %s", sig)

Returns

string

The signed message as a hex string.

Parameters

Configuration options accepted by this action.

Account

  • Type: Account

  • Optional

Account to use for signing. If nil, uses the client's account.

Accepts a JSON-RPC Account or Local Account.

import "github.com/ChefBingbong/viem-go/accounts"
account, err := accounts.PrivateKeyToAccount("0x...")
if err != nil {
log.Fatal(err)
}
sig, err := wallet.SignMessage(ctx, walletClient, wallet.SignMessageParameters{
Account: account,
Message: signature.NewSignableMessage("hello world"),
})

Message

  • Type: signature.SignableMessage

  • Required

Message to sign. Can be a string message or raw hex/bytes.

By default, viem-go signs the UTF-8 representation of the message.

import "github.com/ChefBingbong/viem-go/utils/signature"
sig, err := wallet.SignMessage(ctx, walletClient, wallet.SignMessageParameters{
Message: signature.NewSignableMessage("hello world"),
})

To sign the data representation of the message, you can use the Raw field.

import (
"github.com/ChefBingbong/viem-go/utils/signature"
"github.com/ChefBingbong/viem-go/utils/encoding"
)
sig, err := wallet.SignMessage(ctx, walletClient, wallet.SignMessageParameters{
Message: signature.SignableMessage{
Raw: encoding.HexToBytes("0x68656c6c6f20776f726c64"),
},
})

JSON-RPC Methods

  • JSON-RPC Accounts: personal_sign
  • Local Accounts: Signs locally. No JSON-RPC request.