Skip to content

Sign a message

Usage

Prove ownership of a wallet to a centralised backend. For eg - https://www.tensor.trade/rewards

Screenshot 2024-08-30 at 7.38.32 PM.png

Ref - https://github.com/anza-xyz/wallet-adapter/blob/3761cd8cc867da39da7c0b070bbf8779402cff36/packages/starter/example/src/components/SignMessage.tsx#L9

How to sign messages

  • Install @noble/curves
npm install @noble/curves
  • Coding the SignMessage component
import { ed25519 } from '@noble/curves/ed25519';
import { useWallet } from '@solana/wallet-adapter-react';
import bs58 from 'bs58';
import React from 'react';
export function SignMessage() {
const { publicKey, signMessage } = useWallet();
async function onClick() {
if (!publicKey) throw new Error('Wallet not connected!');
if (!signMessage) throw new Error('Wallet does not support message signing!');
const message = document.getElementById("message").value;
const encodedMessage = new TextEncoder().encode(message);
const signature = await signMessage(encodedMessage);
if (!ed25519.verify(signature, encodedMessage, publicKey.toBytes())) throw new Error('Message signature invalid!');
alert('success', `Message signature: ${bs58.encode(signature)}`);
};
return (
<div>
<input id="message" type="text" placeholder="Message" />
<button onClick={onClick}>
Sign Message
</button>
</div>
);
};