Skip to content

useSignMessage ​

The useSignMessage hook provides functionality to sign messages using the connected wallet account.

Import ​

tsx
import { useSignMessage } from '@luno-kit/react'

Usage ​

Basic Usage ​

tsx
import { useSignMessage } from '@luno-kit/react'

function SignMessageButton() {
  const { signMessage, isPending, error, data } = useSignMessage()

  const handleSign = () => {
    signMessage({
      message: 'Hello, this is a test message to sign!'
    })
  }

  return (
    <div>
      <button onClick={handleSign} disabled={isPending}>
        {isPending ? 'Signing...' : 'Sign Message'}
      </button>
      {error && <p>Error: {error.message}</p>}
      {data && (
        <div>
          <p>Signature: {data.signature}</p>
          <p>Address: {data.addressUsed}</p>
        </div>
      )}
    </div>
  )
}
ts
import { createConfig } from '@luno-kit/react'
import { polkadot, kusama } from '@luno-kit/react/chains'
import { polkadotjsConnector } from '@luno-kit/react/connectors'

const config = createConfig({
  appName: 'luno with-vite example',
  chains: [polkadot, kusama],
  connectors: [polkadotjsConnector()],
  autoConnect: true,
});

Async Usage with Callbacks ​

tsx
import { useSignMessage } from '@luno-kit/react'

function AsyncSignMessageButton() {
  const { signMessageAsync, isPending } = useSignMessage()

  const handleSignAsync = async () => {
    try {
      const result = await signMessageAsync({
        message: 'Hello, this is a test message to sign!'
      })

      console.log('Message signed successfully!')
      console.log('Signature:', result.signature)
      console.log('Address used:', result.addressUsed)
    } catch (error) {
      console.error('Signing failed:', error)
    }
  }

  return (
    <button onClick={handleSignAsync} disabled={isPending}>
      Sign Message (Async)
    </button>
  )
}
ts
import { createConfig } from '@luno-kit/react'
import { polkadot, kusama } from '@luno-kit/react/chains'
import { polkadotjsConnector } from '@luno-kit/react/connectors'

const config = createConfig({
  appName: 'luno with-vite example',
  chains: [polkadot, kusama],
  connectors: [polkadotjsConnector()],
  autoConnect: true,
});

Parameters ​

The hook accepts an optional configuration object:

ParameterTypeRequiredDescription
onSuccess(data: SignMessageData) => voidNoCallback when message is signed successfully
onError(error: Error) => voidNoCallback when signing fails
onSettled() => voidNoCallback when signing completes (success or failure)

Return Value ​

The hook returns an object with the following properties:

Methods ​

These are the functions you can call to sign messages.

PropertyTypeDescription
signMessage(variables: { message: string }, options?: { onSuccess?: () => void; onError?: (error: Error) => void; onSettled?: () => void }) => voidSign message synchronously
signMessageAsync(variables: { message: string }, options?: { onSuccess?: () => void; onError?: (error: Error) => void; onSettled?: () => void }) => Promise<SignMessageData>Sign message asynchronously
reset() => voidReset the signing state

Data & State ​

These properties provide information about the current state of the signing process.

PropertyTypeDescription
dataSignMessageData | undefinedSigning result data
errorError | nullSigning error if any
isErrorbooleanWhether there's an error
isIdlebooleanWhether the hook is idle
isPendingbooleanWhether signing is in progress
isSuccessbooleanWhether signing was successful
status'idle' | 'pending' | 'error' | 'success'Overall signing status
variablesSignMessageVariables | undefinedLast used signing variables

SignMessageVariables ​

tsx
interface SignMessageVariables {
  message: string;  // The message to sign
}

SignMessageData ​

tsx
interface SignMessageData {
  signature: string;      // The signature of the message
  rawMessage: string;     // The original message that was signed
  addressUsed: string;    // The address used for signing
}

TIP

This hook requires an active wallet connection and account. The signature is generated using the private key associated with the connected account. The signed message can be verified later using the public key and signature.

Released under the MIT License.