|
|
@@ -114,45 +114,3 @@ This can happen if you try to use `signTransaction`, `signAllTransactions`, or `
|
|
|
The other methods are optional APIs, so you have to feature-detect them before using them.
|
|
|
|
|
|
Please see [issue #72](https://github.com/solana-labs/wallet-adapter/issues/72#issuecomment-919232595).
|
|
|
-
|
|
|
-## How can I sign and verify messages?
|
|
|
-
|
|
|
-Some wallet adapters provide a `signMessage` method for signing arbitrary bytes.
|
|
|
-
|
|
|
-The signature string returned by this method can be verified using [tweetnacl-js](https://github.com/dchest/tweetnacl-js/blob/master/README.md#naclsigndetachedverifymessage-signature-publickey) using the public key from the adapter.
|
|
|
-
|
|
|
-This can be used to sign offline — without sending a transaction — and prove a user controls a given private key.
|
|
|
-
|
|
|
-```tsx
|
|
|
-import { ed25519 } from '@noble/curves/ed25519';
|
|
|
-import { useWallet } from '@solana/wallet-adapter-react';
|
|
|
-import bs58 from 'bs58';
|
|
|
-import React, { FC, useCallback } from 'react';
|
|
|
-
|
|
|
-export const SignMessageButton: FC = () => {
|
|
|
- const { publicKey, signMessage } = useWallet();
|
|
|
-
|
|
|
- const onClick = useCallback(async () => {
|
|
|
- try {
|
|
|
- // `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);
|
|
|
- // FIXME: replace with `verifySignMessage`
|
|
|
- // Verify that the bytes were signed using the private key that matches the known public key
|
|
|
- if (!ed25519.verify(signature, message, publicKey.toBytes())) throw new Error('Invalid signature!');
|
|
|
-
|
|
|
- alert(`Message signature: ${bs58.encode(signature)}`);
|
|
|
- } catch (error: any) {
|
|
|
- alert(`Signing failed: ${error?.message}`);
|
|
|
- }
|
|
|
- }, [publicKey, signMessage]);
|
|
|
-
|
|
|
- return signMessage ? (<button onClick={onClick} disabled={!publicKey}>Sign Message</button>) : null;
|
|
|
-};
|
|
|
-```
|