clawchain/wagmi/site/react/api/hooks/useVerifyMessage.md

258 lines
6.9 KiB
Markdown

---
title: useVerifyMessage
description: Hook for verify that a message was signed by the provided address.
---
<script setup>
const packageName = 'wagmi'
const actionName = 'verifyMessage'
const typeName = 'VerifyMessage'
const TData = 'VerifyMessageData'
const TError = 'VerifyMessageErrorType'
</script>
# useVerifyMessage
Hook for verify that a message was signed by the provided address. It supports verifying messages that were signed by either a Smart Contract Account or Externally Owned Account.
## Import
```ts
import { useVerifyMessage } from 'wagmi'
```
## Usage
::: code-group
```tsx [index.tsx]
import { useVerifyMessage } from 'wagmi'
function App() {
const result = useVerifyMessage({
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
message: 'hello world',
signature: '0x66edc32e2ab001213321ab7d959a2207fcef5190cc9abb6da5b0d2a8a9af2d4d2b0700e2c317c4106f337fd934fbbb0bf62efc8811a78603b33a8265d3b8f8cb1c',
})
}
```
<<< @/snippets/react/config.ts[config.ts]
:::
## Parameters
```ts
import { type UseVerifyMessageParameters } from 'wagmi'
```
### address
`Address | undefined`
The Ethereum address that signed the original message.
::: code-group
```tsx [index.tsx]
import { useVerifyMessage } from 'wagmi'
function App() {
const result = useVerifyMessage({
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', // [!code focus]
message: 'hello world',
signature: '0x66edc32e2ab001213321ab7d959a2207fcef5190cc9abb6da5b0d2a8a9af2d4d2b0700e2c317c4106f337fd934fbbb0bf62efc8811a78603b33a8265d3b8f8cb1c',
})
}
```
<<< @/snippets/react/config.ts[config.ts]
:::
### message
`string | { raw: Hex | ByteArray } | undefined`
The message to be verified.
By default, wagmi verifies the UTF-8 representation of the message.
::: code-group
```tsx [index.tsx]
import { useVerifyMessage } from 'wagmi'
function App() {
const result = useVerifyMessage({
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
message: 'hello world', // [!code focus]
signature: '0x66edc32e2ab001213321ab7d959a2207fcef5190cc9abb6da5b0d2a8a9af2d4d2b0700e2c317c4106f337fd934fbbb0bf62efc8811a78603b33a8265d3b8f8cb1c',
})
}
```
<<< @/snippets/react/config.ts[config.ts]
:::
::: tip
By default, viem signs the UTF-8 representation of the message. To sign the data representation of the message, you can use the `raw` attribute.
```ts
import { useVerifyMessage } from 'wagmi'
function App() {
const result = useVerifyMessage({
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
message: { raw: '0x68656c6c6f20776f726c64' } // [!code focus]
signature: '0x66edc32e2ab001213321ab7d959a2207fcef5190cc9abb6da5b0d2a8a9af2d4d2b0700e2c317c4106f337fd934fbbb0bf62efc8811a78603b33a8265d3b8f8cb1c',
})
}
```
:::
### signature
`Hex | ByteArray | undefined`
The signature that was generated by signing the message with the address's signer.
::: code-group
```tsx [index.tsx]
import { useVerifyMessage } from 'wagmi'
function App() {
const result = useVerifyMessage({
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
message: 'hello world',
signature: '0x66edc32e2ab001213321ab7d959a2207fcef5190cc9abb6da5b0d2a8a9af2d4d2b0700e2c317c4106f337fd934fbbb0bf62efc8811a78603b33a8265d3b8f8cb1c', // [!code focus]
})
}
```
<<< @/snippets/react/config.ts[config.ts]
:::
### chainId
`config['chains'][number]['id'] | undefined`
Only used when verifying a message that was signed by a Smart Contract Account. The ID of chain to check if the contract was already deployed.
::: code-group
```tsx [index.tsx]
import { useVerifyMessage } from 'wagmi'
import { mainnet } from 'wagmi/chains'
function App() {
const result = useVerifyMessage({
chainId: mainnet.id, // [!code focus]
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
message: 'hello world',
signature: '0x66edc32e2ab001213321ab7d959a2207fcef5190cc9abb6da5b0d2a8a9af2d4d2b0700e2c317c4106f337fd934fbbb0bf62efc8811a78603b33a8265d3b8f8cb1c',
})
}
```
<<< @/snippets/react/config.ts[config.ts]
:::
### blockNumber
`bigint | undefined`
Only used when verifying a message that was signed by a Smart Contract Account. The block number to check if the contract was already deployed.
::: code-group
```tsx [index.tsx]
import { useVerifyMessage } from 'wagmi'
import { mainnet } from 'wagmi/chains'
function App() {
const result = useVerifyMessage({
blockNumber: 12345678n, // [!code focus]
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
message: 'hello world',
signature: '0x66edc32e2ab001213321ab7d959a2207fcef5190cc9abb6da5b0d2a8a9af2d4d2b0700e2c317c4106f337fd934fbbb0bf62efc8811a78603b33a8265d3b8f8cb1c',
})
}
```
<<< @/snippets/react/config.ts[config.ts]
:::
### blockTag
`'latest' | 'earliest' | 'pending' | 'safe' | 'finalized' | undefined`
Only used when verifying a message that was signed by a Smart Contract Account. The block tag to check if the contract was already deployed.
::: code-group
```tsx [index.tsx]
import { useVerifyMessage } from 'wagmi'
import { mainnet } from 'wagmi/chains'
function App() {
const result = useVerifyMessage({
blockTag: 'pending', // [!code focus]
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
message: 'hello world',
signature: '0x66edc32e2ab001213321ab7d959a2207fcef5190cc9abb6da5b0d2a8a9af2d4d2b0700e2c317c4106f337fd934fbbb0bf62efc8811a78603b33a8265d3b8f8cb1c',
})
}
```
<<< @/snippets/react/config.ts[config.ts]
:::
### config
`Config | undefined`
[`Config`](/react/api/createConfig#config) to use instead of retrieving from the nearest [`WagmiProvider`](/react/api/WagmiProvider).
::: code-group
```tsx [index.tsx]
import { useVerifyMessage } from 'wagmi'
import { config } from './config' // [!code focus]
function App() {
const result = useVerifyMessage({
config, // [!code focus]
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
message: 'hello world',
signature: '0x66edc32e2ab001213321ab7d959a2207fcef5190cc9abb6da5b0d2a8a9af2d4d2b0700e2c317c4106f337fd934fbbb0bf62efc8811a78603b33a8265d3b8f8cb1c',
})
}
```
<<< @/snippets/react/config.ts[config.ts]
:::
### scopeKey
`string | undefined`
Scopes the cache to a given context. Hooks that have identical context will share the same cache.
::: code-group
```tsx [index.tsx]
import { useVerifyMessage } from 'wagmi'
import { config } from './config'
function App() {
const result = useVerifyMessage({
scopeKey: 'foo' // [!code focus]
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
message: 'hello world',
signature: '0x66edc32e2ab001213321ab7d959a2207fcef5190cc9abb6da5b0d2a8a9af2d4d2b0700e2c317c4106f337fd934fbbb0bf62efc8811a78603b33a8265d3b8f8cb1c',
})
}
```
<<< @/snippets/react/config.ts[config.ts]
:::
<!--@include: @shared/query-options.md-->
## Return Type
```ts
import { type UseVerifyMessageReturnType } from 'wagmi'
```
<!--@include: @shared/query-result.md-->
<!--@include: @shared/query-imports.md-->
## Action
- [`verifyMessage`](/core/api/actions/verifyMessage)