|
@@ -3,29 +3,29 @@ import { useWallet } from '@solana/wallet-adapter-react';
|
|
|
import bs58 from 'bs58';
|
|
import bs58 from 'bs58';
|
|
|
import React, { FC, useCallback } from 'react';
|
|
import React, { FC, useCallback } from 'react';
|
|
|
import { useNotify } from './notify';
|
|
import { useNotify } from './notify';
|
|
|
|
|
+import { sign } from 'tweetnacl';
|
|
|
|
|
|
|
|
const SignMessage: FC = () => {
|
|
const SignMessage: FC = () => {
|
|
|
const { publicKey, signMessage } = useWallet();
|
|
const { publicKey, signMessage } = useWallet();
|
|
|
const notify = useNotify();
|
|
const notify = useNotify();
|
|
|
|
|
|
|
|
const onClick = useCallback(async () => {
|
|
const onClick = useCallback(async () => {
|
|
|
- if (!publicKey) {
|
|
|
|
|
- notify('error', 'Wallet not connected!');
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- if (!signMessage) {
|
|
|
|
|
- notify('error', 'Wallet does not support message signing!');
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
try {
|
|
try {
|
|
|
- const message = new TextEncoder().encode('Hello, world!');
|
|
|
|
|
|
|
+ // `publicKey` will be null if the wallet isn't connected
|
|
|
|
|
+ if (!publicKey) throw new Error('Wallet not connected!');
|
|
|
|
|
+ // `signMessage` will be undefined if the wallet doesn't support it
|
|
|
|
|
+ if (!signMessage) throw new Error('Wallet does not support message signing!');
|
|
|
|
|
|
|
|
|
|
+ // Encode anything as bytes
|
|
|
|
|
+ const message = new TextEncoder().encode('Hello, world!');
|
|
|
|
|
+ // Sign the bytes using the wallet
|
|
|
const signature = await signMessage(message);
|
|
const signature = await signMessage(message);
|
|
|
|
|
+ // Verify that the bytes were signed using the private key that matches the known public key
|
|
|
|
|
+ if (!sign.detached.verify(message, signature, publicKey.toBytes())) throw new Error('Invalid signature!');
|
|
|
|
|
+
|
|
|
notify('success', `Message signature: ${bs58.encode(signature)}`);
|
|
notify('success', `Message signature: ${bs58.encode(signature)}`);
|
|
|
} catch (error: any) {
|
|
} catch (error: any) {
|
|
|
- notify('error', `Signing failed! ${error?.message}`);
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ notify('error', `Signing failed: ${error?.message}`);
|
|
|
}
|
|
}
|
|
|
}, [publicKey, notify, signMessage]);
|
|
}, [publicKey, notify, signMessage]);
|
|
|
|
|
|