Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[issue-960] Support domain name for mobile app #985

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"@react-navigation/native": "^6.1.2",
"@react-navigation/native-stack": "^6.9.8",
"@reduxjs/toolkit": "^1.9.2",
"@subwallet/extension-chains": "^1.1.8-0",
"@subwallet/extension-dapp": "^1.1.8-0",
"@subwallet/extension-chains": "^1.1.9-0",
"@subwallet/extension-dapp": "^1.1.9-0",
"@subwallet/keyring": "^0.0.10",
"@subwallet/react-ui": "^5.1.2-b69",
"@subwallet/ui-keyring": "^0.0.10",
Expand All @@ -60,6 +60,7 @@
"eslint-plugin-ft-flow": "^2.0.3",
"eventemitter3": "^5.0.0",
"i18next": "^22.4.9",
"loglevel": "^1.8.1",
"moment": "^2.29.4",
"patch-package": "^6.5.0",
"phosphor-react-native": "^1.1.2",
Expand Down Expand Up @@ -136,7 +137,7 @@
"@polkadot/types": "^9.9.4",
"@react-native-community/eslint-config": "^3.2.0",
"@subwallet/chain-list": "^0.2.10",
"@subwallet/extension-base": "^1.1.8-0",
"@subwallet/extension-base": "^1.1.9-0",
"@tsconfig/react-native": "^2.0.2",
"@types/jest": "^29.2.3",
"@types/react": "^18.0.25",
Expand Down Expand Up @@ -185,7 +186,7 @@
"@polkadot/util": "^12.2.1",
"@polkadot/api": "^10.7.2",
"@polkadot/util-crypto": "^12.2.1",
"@subwallet/extension-base": "^1.1.8-0",
"@subwallet/extension-base": "^1.1.9-0",
"@subwallet/chain-list": "0.2.10",
"react-native-svg": "^13.6.0"
},
Expand Down
92 changes: 72 additions & 20 deletions src/components/Input/InputAddressV2.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Input, { InputProps } from 'components/design-system-ui/input';
import React, { ForwardedRef, forwardRef, useCallback, useEffect, useMemo, useState } from 'react';
import React, { ForwardedRef, forwardRef, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Keyboard, TextInput, View } from 'react-native';
import { useSubWalletTheme } from 'hooks/useSubWalletTheme';
import { isAddress } from '@polkadot/util-crypto';
Expand All @@ -10,7 +10,8 @@ import { AddressBookModal } from 'components/Modal/AddressBook/AddressBookModal'
import { NativeSyntheticEvent } from 'react-native/Libraries/Types/CoreEventTypes';
import { TextInputFocusEventData } from 'react-native/Libraries/Components/TextInput/TextInput';
import { AddressScanner, AddressScannerProps } from 'components/Scanner/AddressScanner';
import { saveRecentAccountId } from 'messaging/index';
import { CHAINS_SUPPORTED_DOMAIN, isAzeroDomain } from '@subwallet/extension-base/koni/api/dotsama/domain';
import { saveRecentAccountId, resolveAddressToDomain, resolveDomainToAddress } from 'messaging/index';
import { requestCameraPermission } from 'utils/permission/camera';
import { RESULTS } from 'react-native-permissions';
import createStylesheet from './style/InputAddress';
Expand All @@ -21,6 +22,8 @@ import i18n from 'utils/i18n/i18n';
import { setAdjustResize } from 'rn-android-keyboard-adjust';

interface Props extends InputProps {
chain?: string;
reValidate?: () => void;
isValidValue?: boolean;
showAvatar?: boolean;
showAddressBook?: boolean;
Expand All @@ -33,6 +36,7 @@ interface Props extends InputProps {

const Component = (
{
chain,
isValidValue,
showAvatar = true,
showAddressBook,
Expand All @@ -41,18 +45,22 @@ const Component = (
scannerProps = {},
saveAddress = true,
value = '',
reValidate,
onSideEffectChange,
...inputProps
}: Props,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ref: ForwardedRef<TextInput>,
) => {
const theme = useSubWalletTheme().swThemes;
const [domainName, setDomainName] = useState<string | undefined>(undefined);
const [isInputBlur, setInputBlur] = useState<boolean>(true);
const [isShowAddressBookModal, setShowAddressBookModal] = useState<boolean>(false);
const [isShowQrModalVisible, setIsShowQrModalVisible] = useState<boolean>(false);
const isAddressValid = isAddress(value) && (isValidValue !== undefined ? isValidValue : true);
const { accounts, contacts } = useSelector((root: RootState) => root.accountState);
const [error, setError] = useState<string | undefined>(undefined);
const inputRef = useRef<TextInput | null>(null);

const hasLabel = !!inputProps.label;
const isInputVisible = !isAddressValid || !value || !isInputBlur;
Expand All @@ -68,6 +76,60 @@ const Component = (

useEffect(() => setAdjustResize(), []);

const onChangeInputText = useCallback(
(rawText: string) => {
const text = rawText.trim();

if (inputProps.onChangeText) {
inputProps.onChangeText(text);

if (saveAddress && isAddress(text)) {
saveRecentAccountId(text).catch(console.error);
}
}
},
[inputProps, saveAddress],
);

useEffect(() => {
if (chain && value && CHAINS_SUPPORTED_DOMAIN.includes(chain)) {
if (isAzeroDomain(value)) {
resolveDomainToAddress({
chain,
domain: value,
})
.then(result => {
if (result) {
setDomainName(value);
onChangeInputText(result);
if (inputRef.current) {
if (!inputRef.current.isFocused() && reValidate) {
reValidate();
} else {
inputRef.current.blur();
}
}
}
})
.catch(console.error);
} else if (isAddress(value)) {
resolveAddressToDomain({
chain,
address: value,
})
.then(result => {
if (result) {
setDomainName(result);
}
})
.catch(console.error);
}
} else {
setDomainName(undefined);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [chain, onChangeInputText, value]);

const _contacts = useMemo(() => [...accounts, ...contacts], [accounts, contacts]);

const accountName = useMemo(() => {
Expand Down Expand Up @@ -99,16 +161,17 @@ const Component = (
</View>
)}
<Typography.Text ellipsis style={stylesheet.addressText}>
{accountName || toShort(value, 9, 9)}
{accountName || domainName || toShort(value, 9, 9)}
</Typography.Text>
{(accountName || addressPrefix !== undefined) && (
{(accountName || domainName || addressPrefix !== undefined) && (
<Typography.Text style={stylesheet.addressAliasText}>({toShort(formattedAddress, 4, 4)})</Typography.Text>
)}
</>
);
}, [
accountName,
addressPrefix,
domainName,
formattedAddress,
hasLabel,
showAvatar,
Expand Down Expand Up @@ -171,21 +234,6 @@ const Component = (
theme.colorTextLight5,
]);

const onChangeInputText = useCallback(
(rawText: string) => {
const text = rawText.trim();

if (inputProps.onChangeText) {
inputProps.onChangeText(text);

if (saveAddress && isAddress(text)) {
saveRecentAccountId(text).catch(console.error);
}
}
},
[inputProps, saveAddress],
);

const onScanInputText = useCallback(
(data: string) => {
if (isAddress(data)) {
Expand Down Expand Up @@ -226,7 +274,11 @@ const Component = (
return (
<>
<Input
ref={ref}
ref={myRef => {
inputRef.current = myRef;
// @ts-ignored
ref = inputRef.current;
}}
{...inputProps}
leftPart={LeftPart}
leftPartStyle={stylesheet.inputLeftPart}
Expand Down
10 changes: 10 additions & 0 deletions src/messaging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ import {
RequestTuringStakeCompound,
RequestUnbondingSubmit,
RequestUnlockKeyring,
ResolveAddressToDomainRequest,
ResolveDomainRequest,
ResponseAccountCreateSuriV2,
ResponseAccountCreateWithSecretKey,
ResponseAccountExportPrivateKey,
Expand Down Expand Up @@ -1384,6 +1386,14 @@ export async function disconnectWalletConnectConnection(topic: string): Promise<
return sendMessage('pri(walletConnect.session.disconnect)', { topic });
}

export async function resolveDomainToAddress(request: ResolveDomainRequest) {
return sendMessage('pri(accounts.resolveDomainToAddress)', request);
}

export async function resolveAddressToDomain(request: ResolveAddressToDomainRequest) {
return sendMessage('pri(accounts.resolveAddressToDomain)', request);
}

export async function subscribeTransactions(
callback: (rs: Record<string, SWTransactionResult>) => void,
): Promise<Record<string, SWTransactionResult>> {
Expand Down
15 changes: 3 additions & 12 deletions src/predefined/dAppSites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -843,19 +843,10 @@ export const predefinedDApps: PredefinedDApps = {
isSupportEthereumAccount: true,
},
{
name: 'Moonwell Apollo',
name: 'Moonwell',
icon: 'https://raw.githubusercontent.com/nova-wallet/nova-utils/master/icons/dapps/color/Moonwell.svg',
id: 'moonwell.fi/apollo/MOVR',
url: 'https://moonwell.fi/apollo/MOVR',
categories: ['defi', 'evm'],
isSupportSubstrateAccount: false,
isSupportEthereumAccount: true,
},
{
name: 'Moonwell Artemis',
icon: 'https://dotinsights.subwallet.app/assets/images/projects/moonwell.png',
id: 'moonwell.fi/artemis/GLMR',
url: 'https://moonwell.fi/artemis/GLMR',
id: 'https://moonwell.fi/discover',
url: 'https://moonwell.fi/discover',
categories: ['defi', 'evm'],
isSupportSubstrateAccount: false,
isSupportEthereumAccount: true,
Expand Down
2 changes: 1 addition & 1 deletion src/screens/Transaction/NFT/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ const SendNFT: React.FC<SendNFTProps> = ({
label={formState.labels.recipientAddress}
value={formState.data.recipientAddress}
onChangeText={onChangeReceiverAddress}
isValidValue={formState.isValidated.recipientAddress}
placeholder={i18n.placeholder.accountAddress}
onSubmitEditing={handleSend}
disabled={loading}
chain={nftChain}
/>
{!!formState.errors.recipientAddress.length && (
<Warning style={{ marginBottom: 8 }} message={formState.errors.recipientAddress[0]} isDanger />
Expand Down
4 changes: 4 additions & 0 deletions src/screens/Transaction/SendFundV2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,8 @@ export const SendFund = ({
setIsTransferAll(false);
}, []);

const reValidate = () => trigger('to');

const renderAmountInput = useCallback(
({ field: { onBlur, onChange, value, ref } }: UseControllerReturn<TransferFormValues>) => (
<>
Expand Down Expand Up @@ -967,11 +969,13 @@ export const SendFund = ({
value={value}
onChangeText={onChange}
onBlur={onBlur}
reValidate={reValidate}
onSideEffectChange={onBlur}
placeholder={i18n.placeholder.accountAddress}
disabled={loading}
addressPrefix={destChainNetworkPrefix}
networkGenesisHash={destChainGenesisHash}
chain={chainValue}
showAddressBook
saveAddress
/>
Expand Down
Loading