|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { request } from '@stacks/connect'; |
| 4 | +import type { ClarityValue, TupleCV } from '@stacks/transactions'; |
| 5 | +import { useCallback, useMemo, useState } from 'react'; |
| 6 | + |
| 7 | +import type { MutationStatus } from '../provider/stacks-wallet-provider.types'; |
| 8 | +import { useAddress } from './use-address'; |
| 9 | + |
| 10 | +export interface SignStructuredMessageVariables { |
| 11 | + message: ClarityValue; |
| 12 | + domain: TupleCV; |
| 13 | +} |
| 14 | + |
| 15 | +export interface SignStructuredMessageData { |
| 16 | + publicKey: string; |
| 17 | + signature: string; |
| 18 | +} |
| 19 | + |
| 20 | +export interface SignStructuredMessageOptions { |
| 21 | + onSuccess?: (data: SignStructuredMessageData) => void; |
| 22 | + onError?: (error: Error) => void; |
| 23 | + onSettled?: ( |
| 24 | + data: SignStructuredMessageData | undefined, |
| 25 | + error: Error | null |
| 26 | + ) => void; |
| 27 | +} |
| 28 | + |
| 29 | +export const useSignStructuredMessage = () => { |
| 30 | + const { isConnected, provider } = useAddress(); |
| 31 | + const [data, setData] = useState<SignStructuredMessageData | undefined>( |
| 32 | + undefined |
| 33 | + ); |
| 34 | + const [error, setError] = useState<Error | null>(null); |
| 35 | + const [status, setStatus] = useState<MutationStatus>('idle'); |
| 36 | + |
| 37 | + const signStructuredMessageAsync = useCallback( |
| 38 | + async ( |
| 39 | + variables: SignStructuredMessageVariables |
| 40 | + ): Promise<SignStructuredMessageData> => { |
| 41 | + if (!isConnected) { |
| 42 | + throw new Error('Wallet is not connected'); |
| 43 | + } |
| 44 | + |
| 45 | + if (provider === 'okx') { |
| 46 | + throw new Error( |
| 47 | + 'Structured message signing is not supported by OKX wallet' |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + setStatus('pending'); |
| 52 | + setError(null); |
| 53 | + setData(undefined); |
| 54 | + |
| 55 | + try { |
| 56 | + const result = await request('stx_signStructuredMessage', { |
| 57 | + message: variables.message, |
| 58 | + domain: variables.domain, |
| 59 | + }); |
| 60 | + |
| 61 | + setData(result); |
| 62 | + setStatus('success'); |
| 63 | + return result; |
| 64 | + } catch (err) { |
| 65 | + const error = |
| 66 | + err instanceof Error ? err : new Error(String(err)); |
| 67 | + setError(error); |
| 68 | + setStatus('error'); |
| 69 | + throw error; |
| 70 | + } |
| 71 | + }, |
| 72 | + [isConnected, provider] |
| 73 | + ); |
| 74 | + |
| 75 | + const signStructuredMessage = useCallback( |
| 76 | + ( |
| 77 | + variables: SignStructuredMessageVariables, |
| 78 | + options?: SignStructuredMessageOptions |
| 79 | + ) => { |
| 80 | + signStructuredMessageAsync(variables) |
| 81 | + .then((data) => { |
| 82 | + options?.onSuccess?.(data); |
| 83 | + options?.onSettled?.(data, null); |
| 84 | + }) |
| 85 | + .catch((error) => { |
| 86 | + options?.onError?.(error); |
| 87 | + options?.onSettled?.(undefined, error); |
| 88 | + }); |
| 89 | + }, |
| 90 | + [signStructuredMessageAsync] |
| 91 | + ); |
| 92 | + |
| 93 | + const reset = useCallback(() => { |
| 94 | + setData(undefined); |
| 95 | + setError(null); |
| 96 | + setStatus('idle'); |
| 97 | + }, []); |
| 98 | + |
| 99 | + return useMemo( |
| 100 | + () => ({ |
| 101 | + signStructuredMessage, |
| 102 | + signStructuredMessageAsync, |
| 103 | + reset, |
| 104 | + data, |
| 105 | + error, |
| 106 | + isError: status === 'error', |
| 107 | + isIdle: status === 'idle', |
| 108 | + isPending: status === 'pending', |
| 109 | + isSuccess: status === 'success', |
| 110 | + status, |
| 111 | + }), |
| 112 | + [ |
| 113 | + signStructuredMessage, |
| 114 | + signStructuredMessageAsync, |
| 115 | + reset, |
| 116 | + data, |
| 117 | + error, |
| 118 | + status, |
| 119 | + ] |
| 120 | + ); |
| 121 | +}; |
0 commit comments