Skip to content

Commit

Permalink
feat: add sign message component
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Sep 5, 2024
1 parent 93dd54d commit de4c639
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
22 changes: 20 additions & 2 deletions packages/sats-wagmi/src/connectors/mm-snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,26 @@ class MMSnapConnector extends SatsConnector {
return pubkey.toString('hex');
}

signMessage(): Promise<string> {
throw new Error('Not implemented');
async signMessage(message: string): Promise<string> {
try {
return (await ethereum.request({
method: 'wallet_invokeSnap',
params: {
snapId,
request: {
method: 'btc_signMessage',
params: {
message,
hdPath: getDefaultBip32Path(DEFAULT_SCRIPT_TYPE, this.snapNetwork)
}
}
}
})) as string;
} catch (err: any) {
const error = new SnapError(err?.message || 'Could not sign message');

throw error;
}
}

async signInput(inputIndex: number, psbt: Psbt) {
Expand Down
1 change: 1 addition & 0 deletions packages/sats-wagmi/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './useBalance';
export * from './useFeeRate';
export * from './useFeeEstimate';
export * from './useSendTransaction';
export * from './useSignMessage';
2 changes: 1 addition & 1 deletion packages/sats-wagmi/src/hooks/useSendTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useSatsWagmi } from '../provider';
import { useAccount } from './useAccount';

type UseSendTransactionProps = Omit<
UseMutationOptions<any, unknown, { to: string; value: bigint }, unknown>,
UseMutationOptions<string | undefined, unknown, { to: string; value: bigint }, unknown>,
'mutationKey' | 'mutationFn'
>;

Expand Down
33 changes: 33 additions & 0 deletions packages/sats-wagmi/src/hooks/useSignMessage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useMutation, UseMutationOptions } from '@tanstack/react-query';

import { useSatsWagmi } from '../provider';

import { useAccount } from './useAccount';

type UseSignMessageProps = Omit<
UseMutationOptions<string | undefined, unknown, { message: string }, unknown>,
'mutationKey' | 'mutationFn'
>;

const useSignMessage = (props: UseSignMessageProps = {}) => {
const { connector } = useSatsWagmi();
const { address } = useAccount();

const { mutate, mutateAsync, ...result } = useMutation({
mutationKey: ['sats-sign-message', address],
mutationFn: async ({ message }: { message: string }) => {
if (!connector) return undefined;

return await connector.signMessage(message);
},
...props
});

return {
...result,
signMessage: mutate,
signMessageAsync: mutateAsync
};
};

export { useSignMessage };
28 changes: 27 additions & 1 deletion playgrounds/vite-react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
useBalance,
useConnect,
useDisconnect,
useSendTransaction
useSendTransaction,
useSignMessage
// useWaitForTransactionReceipt,
} from '@gobob/sats-wagmi';

Expand All @@ -23,6 +24,7 @@ function App() {
<>
<Account />
<Connect />
<SignMessage />
<Balance />
<SendTransaction />
</>
Expand Down Expand Up @@ -71,6 +73,30 @@ function Connect() {
);
}

function SignMessage() {
const { data, signMessage } = useSignMessage();

return (
<div>
<h2>Sign Message</h2>

<form
onSubmit={(event) => {
event.preventDefault();
const formData = new FormData(event.target as HTMLFormElement);

signMessage({ message: formData.get('message') as string });
}}
>
<input name='message' />
<button type='submit'>Sign Message</button>
</form>

{data}
</div>
);
}

function Balance() {
const { data: account_ } = useBalance();

Expand Down

0 comments on commit de4c639

Please sign in to comment.