116 lines
2.9 KiB
Markdown
116 lines
2.9 KiB
Markdown
---
|
|
description: Verifies if a signed message was generated by the provided address.
|
|
---
|
|
|
|
# verifyMessage
|
|
|
|
Verify that a message was signed by the provided address.
|
|
|
|
:::warning[Warning]
|
|
This utility can only verify a message that was signed by an Externally Owned Account (EOA).
|
|
To verify messages from Contract Accounts (& EOA), use the [`publicClient.verifyMessage` Action](/docs/actions/public/verifyMessage) instead.
|
|
:::
|
|
|
|
## Usage
|
|
|
|
:::code-group
|
|
|
|
```ts [example.ts]
|
|
import { verifyMessage } from 'viem'
|
|
import { account, walletClient } from './client'
|
|
|
|
const signature = await walletClient.signMessage({
|
|
account,
|
|
message: 'hello world',
|
|
})
|
|
|
|
const valid = await verifyMessage({ // [!code focus:99]
|
|
address: account.address,
|
|
message: 'hello world',
|
|
signature,
|
|
})
|
|
// true
|
|
```
|
|
|
|
```ts [config.ts]
|
|
import { createWalletClient, custom } from 'viem'
|
|
import { privateKeyToAccount } from 'viem/accounts'
|
|
|
|
export const walletClient = createWalletClient({
|
|
transport: custom(window.ethereum)
|
|
})
|
|
|
|
// JSON-RPC Account
|
|
export const [account] = await walletClient.getAddresses()
|
|
// Local Account
|
|
export const account = privateKeyToAccount(...)
|
|
```
|
|
|
|
:::
|
|
|
|
## Returns
|
|
|
|
`boolean`
|
|
|
|
Whether the provided `address` generated the `signature`.
|
|
|
|
## Parameters
|
|
|
|
### address
|
|
|
|
- **Type:** [`Address`](/docs/glossary/types#address)
|
|
|
|
The Ethereum address that signed the original message.
|
|
|
|
```ts
|
|
const valid = await verifyMessage({
|
|
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', // [!code focus:1]
|
|
message: 'hello world',
|
|
signature:
|
|
'0x66edc32e2ab001213321ab7d959a2207fcef5190cc9abb6da5b0d2a8a9af2d4d2b0700e2c317c4106f337fd934fbbb0bf62efc8811a78603b33a8265d3b8f8cb1c',
|
|
})
|
|
```
|
|
|
|
### message
|
|
|
|
- **Type:** `string`
|
|
|
|
The message to be verified.
|
|
|
|
By default, viem signs the UTF-8 representation of the message.
|
|
|
|
```ts
|
|
const valid = await verifyMessage({
|
|
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
|
|
message: 'hello world', // [!code focus:1]
|
|
signature:
|
|
'0x66edc32e2ab001213321ab7d959a2207fcef5190cc9abb6da5b0d2a8a9af2d4d2b0700e2c317c4106f337fd934fbbb0bf62efc8811a78603b33a8265d3b8f8cb1c',
|
|
})
|
|
```
|
|
|
|
To sign the data representation of the message, you can use the `raw` attribute.
|
|
|
|
```ts
|
|
const valid = await verifyMessage({
|
|
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
|
|
message: { raw: '0x68656c6c6f20776f726c64' }, // [!code focus:1]
|
|
signature:
|
|
'0x66edc32e2ab001213321ab7d959a2207fcef5190cc9abb6da5b0d2a8a9af2d4d2b0700e2c317c4106f337fd934fbbb0bf62efc8811a78603b33a8265d3b8f8cb1c',
|
|
})
|
|
```
|
|
|
|
### signature
|
|
|
|
- **Type:** `Hex | ByteArray | Signature`
|
|
|
|
The signature that was generated by signing the message with the address's private key.
|
|
|
|
```ts
|
|
const valid = await verifyMessage({
|
|
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
|
|
message: 'hello world',
|
|
signature: // [!code focus:2]
|
|
'0x66edc32e2ab001213321ab7d959a2207fcef5190cc9abb6da5b0d2a8a9af2d4d2b0700e2c317c4106f337fd934fbbb0bf62efc8811a78603b33a8265d3b8f8cb1c',
|
|
})
|
|
```
|