|
| 1 | +import { useQuery } from '@tanstack/react-query' |
| 2 | +import type { EdgeCurrencyWallet } from 'edge-core-js' |
| 3 | +import * as React from 'react' |
| 4 | +import { View } from 'react-native' |
| 5 | + |
| 6 | +import { useHandler } from '../../hooks/useHandler' |
| 7 | +import { lstrings } from '../../locales/strings' |
| 8 | +import type { EdgeAppSceneProps } from '../../types/routerTypes' |
| 9 | +import { SceneButtons } from '../buttons/SceneButtons' |
| 10 | +import { EdgeCard } from '../cards/EdgeCard' |
| 11 | +import { SceneWrapper } from '../common/SceneWrapper' |
| 12 | +import { withWallet } from '../hoc/withWallet' |
| 13 | +import { EdgeRow } from '../rows/EdgeRow' |
| 14 | +import { showError } from '../services/AirshipInstance' |
| 15 | +import { cacheStyles, type Theme, useTheme } from '../services/ThemeContext' |
| 16 | +import { Paragraph, SmallText } from '../themed/EdgeText' |
| 17 | +import { FilledTextInput } from '../themed/FilledTextInput' |
| 18 | + |
| 19 | +export interface SignMessageParams { |
| 20 | + walletId: string |
| 21 | +} |
| 22 | + |
| 23 | +interface Props extends EdgeAppSceneProps<'signMessage'> { |
| 24 | + wallet: EdgeCurrencyWallet |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Lets a user sign an arbitrary message with an address they control, to prove |
| 29 | + * self-hosted wallet ownership when withdrawing from a CEX/CASP (EU Travel |
| 30 | + * Rule). BTC-first: the menu entry only appears for UTXO wallets whose plugin |
| 31 | + * implements message signing. |
| 32 | + */ |
| 33 | +const SignMessageSceneComponent: React.FC<Props> = props => { |
| 34 | + const { wallet } = props |
| 35 | + |
| 36 | + const theme = useTheme() |
| 37 | + const styles = getStyles(theme) |
| 38 | + |
| 39 | + const [message, setMessage] = React.useState('') |
| 40 | + const [signature, setSignature] = React.useState('') |
| 41 | + const [isSigning, setIsSigning] = React.useState(false) |
| 42 | + |
| 43 | + // The signing address must be one the wallet owns, so we default to the |
| 44 | + // wallet's own receive address rather than accepting an arbitrary one. |
| 45 | + // Prefer the native segwit address (the canonical receive address the user |
| 46 | + // hands the exchange), matching `segwitAddress ?? publicAddress` used |
| 47 | + // elsewhere; the `publicAddress` type is the wrapped/legacy variant. |
| 48 | + const { data: publicAddress, error: addressError } = useQuery({ |
| 49 | + queryKey: ['signMessageAddress', wallet.id], |
| 50 | + queryFn: async () => { |
| 51 | + const addresses = await wallet.getAddresses({ tokenId: null }) |
| 52 | + const receiveAddress = |
| 53 | + addresses.find(address => address.addressType === 'segwitAddress') ?? |
| 54 | + addresses.find(address => address.addressType === 'publicAddress') ?? |
| 55 | + addresses[0] |
| 56 | + if (receiveAddress == null) { |
| 57 | + throw new Error(lstrings.sign_message_no_address_error) |
| 58 | + } |
| 59 | + return receiveAddress.publicAddress |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + React.useEffect(() => { |
| 64 | + if (addressError != null) showError(addressError) |
| 65 | + }, [addressError]) |
| 66 | + |
| 67 | + // Clear any prior signature when the message changes, so a stale signature |
| 68 | + // that no longer matches the message can never be copied. |
| 69 | + const handleChangeMessage = useHandler((text: string) => { |
| 70 | + setMessage(text) |
| 71 | + setSignature('') |
| 72 | + }) |
| 73 | + |
| 74 | + const handleSign = useHandler(async () => { |
| 75 | + if (publicAddress == null) { |
| 76 | + showError(lstrings.sign_message_no_address_error) |
| 77 | + return |
| 78 | + } |
| 79 | + setIsSigning(true) |
| 80 | + try { |
| 81 | + // `signMessage` signs the literal UTF-8 message, which is what exchanges |
| 82 | + // verify. `signBytes` would base64-re-encode the bytes before signing and |
| 83 | + // produce a signature over the wrong data, so it is not usable here. |
| 84 | + // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 85 | + const signedMessage = await wallet.signMessage(message, { |
| 86 | + otherParams: { publicAddress } |
| 87 | + }) |
| 88 | + setSignature(signedMessage) |
| 89 | + } catch (error: unknown) { |
| 90 | + showError(error) |
| 91 | + } finally { |
| 92 | + setIsSigning(false) |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + return ( |
| 97 | + <SceneWrapper scroll> |
| 98 | + <View style={styles.container}> |
| 99 | + <Paragraph>{lstrings.sign_message_instructions}</Paragraph> |
| 100 | + |
| 101 | + <EdgeCard> |
| 102 | + <EdgeRow |
| 103 | + rightButtonType="copy" |
| 104 | + title={lstrings.sign_message_address_label} |
| 105 | + body={publicAddress ?? ''} |
| 106 | + /> |
| 107 | + </EdgeCard> |
| 108 | + |
| 109 | + <FilledTextInput |
| 110 | + aroundRem={0.5} |
| 111 | + autoCorrect={false} |
| 112 | + multiline |
| 113 | + numberOfLines={4} |
| 114 | + placeholder={lstrings.sign_message_input_placeholder} |
| 115 | + value={message} |
| 116 | + onChangeText={handleChangeMessage} |
| 117 | + /> |
| 118 | + |
| 119 | + {signature !== '' && ( |
| 120 | + <EdgeCard> |
| 121 | + <EdgeRow |
| 122 | + rightButtonType="copy" |
| 123 | + title={lstrings.sign_message_signature_label} |
| 124 | + body={signature} |
| 125 | + /> |
| 126 | + </EdgeCard> |
| 127 | + )} |
| 128 | + |
| 129 | + <Paragraph> |
| 130 | + <SmallText>{lstrings.sign_message_safety_note}</SmallText> |
| 131 | + </Paragraph> |
| 132 | + |
| 133 | + <SceneButtons |
| 134 | + primary={{ |
| 135 | + label: lstrings.sign_message_sign_button, |
| 136 | + onPress: handleSign, |
| 137 | + disabled: message === '' || publicAddress == null, |
| 138 | + spinner: isSigning |
| 139 | + }} |
| 140 | + /> |
| 141 | + </View> |
| 142 | + </SceneWrapper> |
| 143 | + ) |
| 144 | +} |
| 145 | + |
| 146 | +const getStyles = cacheStyles((theme: Theme) => ({ |
| 147 | + container: { |
| 148 | + padding: theme.rem(0.5) |
| 149 | + } |
| 150 | +})) |
| 151 | + |
| 152 | +export const SignMessageScene = withWallet(SignMessageSceneComponent) |
0 commit comments