In this example, we are going to sign a message for Bitcoin.
import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
import { isBitcoinWallet } from '@dynamic-labs/bitcoin';
const SignMessageButton = () => {
const { primaryWallet } = useDynamicContext();
const signMessage = async () => {
if (!primaryWallet || !isBitcoinWallet(primaryWallet)) return;
const signature = await primaryWallet.signMessage('example');
console.log('signature', signature);
};
return <button onClick={signMessage}>Sign message</button>;
};
You can also sign a message with a specific address type (payment or ordinal) or protocol (ecdsa or bip322-simple) by passing extra params in the signMessage
options.
import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
import { isBitcoinWallet } from '@dynamic-labs/bitcoin';
const SignMessageButton = () => {
const { primaryWallet } = useDynamicContext();
const signMessageWithOrdinalsAddress = async () => {
if (!primaryWallet || !isBitcoinWallet(primaryWallet)) return;
const signature = await primaryWallet.signMessage('example', { addressType: 'ordinals' });
console.log('signature', signature);
};
const signMessageWithPaymentAddress = async () => {
if (!primaryWallet || !isBitcoinWallet(primaryWallet)) return;
const signature = await primaryWallet.signMessage('example', { addressType: 'payment' });
console.log('signature', signature);
};
const signMessageWithEcdsaProtocol = async () => {
if (!primaryWallet || !isBitcoinWallet(primaryWallet)) return;
const signature = await primaryWallet.signMessage('example', { addressType: 'ordinals', protocol: 'ecdsa' });
console.log('signature', signature);
};
return <>
<button onClick={signMessageWithOrdinalsAddress}>Sign message with ordinals address</button>
<button onClick={signMessageWithPaymentAddress}>Sign message with payment address</button>
<button onClick={signMessageWithEcdsaProtocol}>Sign message with ecdsa protocol</button>
</>;
};
Notes:
- Some wallets don’t allow you to specify the address type or protocol. In this case, we’ll just default to the address type and protocol that the wallet supports.
- If you don’t specify an address type, we’ll default to the address type that the wallet supports or ordinals address.
- If you don’t specify a protocol, we’ll default to the protocol that the wallet supports or bip322-simple.