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

[KEPLR-287, Mobile] Add parsing url on reading camera screen(universal link) #1112

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
132 changes: 105 additions & 27 deletions apps/mobile/src/screen/camera/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,35 +98,90 @@ export const CameraScreen: FunctionComponent = observer(() => {
}

navigation.reset({routes: [{name: 'Home'}]});
} else {
const isBech32Address = (() => {
try {
// Check that the data is bech32 address.
// If this is not valid bech32 address, it will throw an error.
Bech32Address.validate(data);
} catch {
return false;
}
return true;
})();

if (isBech32Address) {
const prefix = data.slice(0, data.indexOf('1'));
const chainInfo = chainStore.chainInfosInUI.find(
chainInfo =>
chainInfo.bech32Config.bech32PrefixAccAddr === prefix,
);

if (chainInfo) {
navigation.replace('Send', {
chainId: chainInfo.chainId,
coinMinimalDenom: chainInfo.currencies[0].coinMinimalDenom,
recipientAddress: data,
});
} else {
navigation.reset({routes: [{name: 'Home'}]});
return;
}

if (isBech32Address(data)) {
const prefix = data.slice(0, data.indexOf('1'));
const chainInfo = chainStore.chainInfosInUI.find(
chainInfo =>
chainInfo.bech32Config.bech32PrefixAccAddr === prefix,
);

if (chainInfo) {
navigation.replace('Send', {
chainId: chainInfo.chainId,
coinMinimalDenom: chainInfo.currencies[0].coinMinimalDenom,
recipientAddress: data,
});
} else {
navigation.reset({routes: [{name: 'Home'}]});
}
return;
}

if (isValidHttpUrl(data)) {
const response = await fetch(data);
const originUrl = new URL(response.url);

if (originUrl.hostname === 'deeplink.keplr.app') {
if (originUrl.pathname === '/staking') {
// Coinbase Staking Logic
if (
originUrl.searchParams.get('chainId') &&
originUrl.searchParams.get('userIdentifier') &&
originUrl.searchParams.get('activityName')
) {
// If the chain is not enabled, enable it.
if (
!chainStore.isEnabledChain(
originUrl.searchParams.get('chainId') as string,
)
) {
await chainStore.enableChainInfoInUI(
originUrl.searchParams.get('chainId') as string,
);
}

navigation.replace('Stake', {
screen: 'Stake.ValidateList',
params: {
chainId: originUrl.searchParams.get(
'chainId',
) as string,
fromDeepLink: {
userIdentifier: originUrl.searchParams.get(
'userIdentifier',
) as string,
activityName: originUrl.searchParams.get(
'activityName',
) as string,
},
},
});
}

// Keplr Staking Logic
if (
originUrl.searchParams.get('chainId') &&
originUrl.searchParams.get('validatorAddress')
) {
navigation.replace('Stake', {
screen: 'Stake.ValidateDetail',
params: {
chainId: originUrl.searchParams.get(
'chainId',
) as string,
validatorAddress: originUrl.searchParams.get(
'validatorAddress',
) as string,
},
});
}
}
}

return;
}

setIsCompleted(true);
Expand Down Expand Up @@ -276,3 +331,26 @@ const AimIcon: FunctionComponent = () => {
</Svg>
);
};

const isValidHttpUrl = (data: string) => {
let url;

try {
url = new URL(data);
} catch (_) {
return false;
}

return url.protocol === 'http:' || url.protocol === 'https:';
};

const isBech32Address = (data: string) => {
try {
// Check that the data is bech32 address.
// If this is not valid bech32 address, it will throw an error.
Bech32Address.validate(data);
} catch {
return false;
}
return true;
};
Loading