dApp Kit
Wallet Hooks
useSignPersonalMessage

useSignPersonalMessage

Use the useSignPersonalMessage hook to prompt the user to sign a message with their wallet.

import { ConnectButton, useCurrentAccount, useSignPersonalMessage } from '@mysten/dapp-kit';
import { useState } from 'react';
 
function MyComponent() {
	const { mutate: signPersonalMessage } = useSignPersonalMessage();
	const [message, setMessage] = useState('hello, World!');
	const [signature, setSignature] = useState('');
	const currentAccount = useCurrentAccount();
 
	return (
		<div style={{ padding: 20 }}>
			<ConnectButton />
			{currentAccount && (
				<>
					<div>
						<label>
							Message:{' '}
							<input type="text" value={message} onChange={(ev) => setMessage(ev.target.value)} />
						</label>
					</div>
					<button
						onClick={() => {
							signPersonalMessage(
								{
									message: new TextEncoder().encode(message),
								},
								{
									onSuccess: (result) => setSignature(result.signature),
								},
							);
						}}
					>
						Sign message
					</button>
					<div>Signature: {signature}</div>
				</>
			)}
		</div>
	);
}

Example

Arguments

  • message: The message to sign, as a Uint8Array.

Result

  • signature: The signature of the message, as a Base64-encoded string.
  • bytes: The bytes of the message, as a a Base64-encoded string.