icns-frontend/pages/verification/index.tsx

506 lines
13 KiB
TypeScript
Raw Normal View History

2022-12-07 09:17:59 +00:00
// React
2022-12-01 08:33:51 +00:00
import { useEffect, useState } from "react";
2022-12-07 09:17:59 +00:00
// Types
2022-12-15 10:06:44 +00:00
import {
ChainItemType,
2022-12-15 15:43:18 +00:00
QueryError,
2022-12-15 10:06:44 +00:00
RegisteredAddresses,
TwitterProfileType,
} from "../../types";
import { checkTwitterAuthQueryParameter } from "../../utils/url";
2022-12-01 08:33:51 +00:00
2022-12-07 09:17:59 +00:00
// Styles
2022-12-07 13:55:22 +00:00
import styled from "styled-components";
2022-12-06 14:53:31 +00:00
import color from "../../styles/color";
2022-12-01 08:33:51 +00:00
2022-12-07 09:17:59 +00:00
// Components
import { Logo } from "../../components/logo";
2022-12-09 11:59:52 +00:00
import { SkeletonChainList } from "../../components/skeleton";
2022-12-07 09:17:59 +00:00
import { PrimaryButton } from "../../components/primary-button";
2022-12-12 07:10:11 +00:00
import { TwitterProfile } from "../../components/twitter-profile";
2022-12-09 11:59:52 +00:00
import { ChainList } from "../../components/chain-list";
import { useRouter } from "next/router";
2022-12-13 14:46:32 +00:00
import {
getKeplrFromWindow,
KeplrWallet,
2022-12-14 07:52:05 +00:00
sendMsgs,
2022-12-13 14:46:32 +00:00
simulateMsgs,
} from "../../wallets";
import { ChainIdHelper } from "@keplr-wallet/cosmos";
2022-12-07 09:17:59 +00:00
import AllChainsIcon from "../../public/images/svg/all-chains-icon.svg";
import { AllChainsItem } from "../../components/chain-list/all-chains-item";
2022-12-13 13:10:31 +00:00
import { SearchInput } from "../../components/search-input";
2022-12-15 15:43:18 +00:00
import {
MainChainId,
REFERRAL_KEY,
RESOLVER_ADDRESS,
REST_URL,
} from "../../constants/icns";
2022-12-14 11:36:08 +00:00
import {
fetchTwitterInfo,
queryAddressesFromTwitterName,
2022-12-15 15:43:18 +00:00
queryOwnerOfTwitterName,
2022-12-14 11:36:08 +00:00
queryRegisteredTwitterId,
2022-12-15 06:02:50 +00:00
verifyTwitterAccount,
2022-12-15 10:06:44 +00:00
} from "../../queries";
2022-12-15 06:02:50 +00:00
import { ErrorHandler } from "../../utils/error";
import {
KEPLR_NOT_FOUND_ERROR,
TWITTER_LOGIN_ERROR,
} from "../../constants/error-message";
2022-12-15 10:06:44 +00:00
import { makeClaimMessage, makeSetRecordMessage } from "../../messages";
2022-12-15 15:43:18 +00:00
import Axios, { AxiosError } from "axios";
import { BackButton } from "../../components/back-button";
2022-12-14 07:52:05 +00:00
2022-12-01 08:33:51 +00:00
export default function VerificationPage() {
const router = useRouter();
2022-12-14 11:36:08 +00:00
const [twitterAuthInfo, setTwitterAuthInfo] = useState<TwitterProfileType>();
2022-12-01 08:33:51 +00:00
2022-12-07 09:17:59 +00:00
const [isLoading, setIsLoading] = useState(true);
const [wallet, setWallet] = useState<KeplrWallet>();
2022-12-15 10:06:44 +00:00
const [chainList, setChainList] = useState<ChainItemType[]>([]);
2022-12-15 10:06:44 +00:00
const [disabledChainList, setDisabledChainList] = useState<ChainItemType[]>(
2022-12-14 11:36:08 +00:00
[],
);
2022-12-15 10:06:44 +00:00
const [registeredChainList, setRegisteredChainList] = useState<
RegisteredAddresses[]
>([]);
const [allChains, setAllChains] = useState<ChainItemType>();
const [allChecked, setAllChecked] = useState(false);
2022-12-15 10:06:44 +00:00
const [checkedItems, setCheckedItems] = useState(new Set());
2022-12-13 13:10:31 +00:00
const [searchValue, setSearchValue] = useState("");
2022-12-15 15:43:18 +00:00
const [isOwner, setIsOwner] = useState(false);
const [isAgree, setIsAgree] = useState(false);
useEffect(() => {
2022-12-14 07:52:05 +00:00
const init = async () => {
if (window.location.search) {
2022-12-14 11:36:08 +00:00
try {
2022-12-15 06:02:50 +00:00
const { state, code } = checkTwitterAuthQueryParameter(
window.location.search,
);
2022-12-14 11:36:08 +00:00
// Initialize Wallet
2022-12-15 15:43:18 +00:00
const keplrWallet = await initWallet();
2022-12-14 11:36:08 +00:00
// Fetch Twitter Profile
const twitterInfo = await fetchTwitterInfo(state, code);
2022-12-15 10:06:44 +00:00
// contract check registered
2022-12-14 11:36:08 +00:00
const registeredQueryResponse = await queryRegisteredTwitterId(
twitterInfo.id,
);
setTwitterAuthInfo({
...twitterInfo,
isRegistered: "data" in registeredQueryResponse,
});
if ("data" in registeredQueryResponse) {
2022-12-15 15:43:18 +00:00
const ownerOfQueryResponse = await queryOwnerOfTwitterName(
registeredQueryResponse.data.name,
);
2022-12-14 11:36:08 +00:00
const addressesQueryResponse = await queryAddressesFromTwitterName(
registeredQueryResponse.data.name,
);
2022-12-15 15:43:18 +00:00
if (keplrWallet) {
const key = await keplrWallet.getKey(MainChainId);
setIsOwner(ownerOfQueryResponse.data.owner === key.bech32Address);
}
2022-12-15 10:06:44 +00:00
setRegisteredChainList(addressesQueryResponse.data.addresses);
2022-12-14 11:36:08 +00:00
}
2022-12-15 06:02:50 +00:00
} catch (error) {
if (error instanceof Error && error.message === TWITTER_LOGIN_ERROR) {
await router.push("/");
}
console.error(error);
2022-12-14 11:36:08 +00:00
} finally {
setIsLoading(false);
}
}
};
2022-12-01 08:33:51 +00:00
2022-12-14 07:52:05 +00:00
init();
2022-12-01 08:33:51 +00:00
}, []);
2022-12-16 15:52:37 +00:00
useEffect(() => {
setAllChains({
chainId: "all chains",
chainName: "all chains",
prefix: `all chains(${chainList.length})`,
address: chainList.map((chain) => chain.chainName).join(", "),
chainImageUrl: AllChainsIcon,
});
}, [chainList]);
useEffect(() => {
2022-12-15 10:06:44 +00:00
const disabledChainList = chainList.filter((chain) => {
for (const registeredChain of registeredChainList) {
if (
chain.prefix === registeredChain.bech32_prefix &&
chain.address === registeredChain.address
) {
return true;
}
}
2022-12-15 10:06:44 +00:00
return false;
2022-12-14 11:36:08 +00:00
});
2022-12-15 10:06:44 +00:00
const filteredChainList = chainList.filter(
(chain) => !disabledChainList.includes(chain),
);
setAllChains({
chainId: "all chains",
chainName: "all chains",
prefix: `all chains(${filteredChainList.length})`,
address: filteredChainList.map((chain) => chain.chainName).join(", "),
chainImageUrl: AllChainsIcon,
});
setChainList(filteredChainList);
setDisabledChainList(disabledChainList);
}, [registeredChainList]);
2022-12-14 11:36:08 +00:00
2022-12-15 06:02:50 +00:00
const initWallet = async () => {
const keplr = await getKeplrFromWindow();
if (keplr) {
const keplrWallet = new KeplrWallet(keplr);
2022-12-15 10:06:44 +00:00
await fetchChainList(keplrWallet);
2022-12-15 06:02:50 +00:00
setWallet(keplrWallet);
2022-12-15 15:43:18 +00:00
return keplrWallet;
2022-12-15 06:02:50 +00:00
} else {
ErrorHandler(KEPLR_NOT_FOUND_ERROR);
}
};
2022-12-15 10:06:44 +00:00
const fetchChainList = async (wallet: KeplrWallet) => {
const chainIds = (await wallet.getChainInfosWithoutEndpoints()).map(
(c) => c.chainId,
);
const chainKeys = await Promise.all(
chainIds.map((chainId) => wallet.getKey(chainId)),
);
2022-12-15 10:06:44 +00:00
const chainInfos = (await wallet.getChainInfosWithoutEndpoints()).map(
(chainInfo) => {
return {
chainId: chainInfo.chainId,
chainName: chainInfo.chainName,
prefix: chainInfo.bech32Config.bech32PrefixAccAddr,
chainImageUrl: `https://raw.githubusercontent.com/chainapsis/keplr-chain-registry/main/images/${
ChainIdHelper.parse(chainInfo.chainId).identifier
}/chain.png`,
};
},
);
2022-12-15 10:06:44 +00:00
const chainArray = [];
for (let i = 0; i < chainKeys.length; i++) {
chainArray.push({
address: chainKeys[i].bech32Address,
...chainInfos[i],
2022-12-14 07:52:05 +00:00
});
2022-12-15 10:06:44 +00:00
}
2022-12-15 10:06:44 +00:00
// remove duplicated item
const filteredChainList = chainArray.filter((nextChain, index, self) => {
return (
index ===
self.findIndex((prevChain) => {
const isDuplicated = prevChain.prefix === nextChain.prefix;
2022-12-15 10:06:44 +00:00
if (isDuplicated && prevChain.chainName !== nextChain.chainName) {
console.log(
`${nextChain.chainName} has been deleted due to a duplicate name with ${prevChain.chainName}`,
);
}
return isDuplicated;
})
);
});
setChainList(filteredChainList);
};
2022-12-13 14:46:32 +00:00
const checkAdr36 = async () => {
if (twitterAuthInfo && wallet) {
const key = await wallet.getKey(MainChainId);
2022-12-14 15:39:51 +00:00
2022-12-14 11:36:08 +00:00
const chainIds = Array.from(checkedItems).map((chain) => {
return (chain as ChainItemType).chainId;
});
2022-12-14 15:39:51 +00:00
2022-12-15 06:02:50 +00:00
return wallet.signICNSAdr36(
2022-12-13 14:46:32 +00:00
MainChainId,
RESOLVER_ADDRESS,
key.bech32Address,
twitterAuthInfo.username,
chainIds,
);
}
};
const onClickRegistration = async () => {
2022-12-15 15:43:18 +00:00
try {
const { state, code } = checkTwitterAuthQueryParameter(
window.location.search,
);
const twitterInfo = await fetchTwitterInfo(state, code);
2022-12-15 15:43:18 +00:00
const adr36Infos = await checkAdr36();
2022-12-13 14:46:32 +00:00
2022-12-15 15:43:18 +00:00
if (wallet && adr36Infos) {
const key = await wallet.getKey(MainChainId);
2022-12-15 06:02:50 +00:00
2022-12-15 15:43:18 +00:00
const icnsVerificationList = await verifyTwitterAccount(
key.bech32Address,
twitterInfo.accessToken,
);
2022-12-13 14:46:32 +00:00
2022-12-15 15:43:18 +00:00
const registerMsg = makeClaimMessage(
2022-12-13 14:46:32 +00:00
key.bech32Address,
2022-12-15 06:02:50 +00:00
twitterInfo.username,
2022-12-15 15:43:18 +00:00
icnsVerificationList,
localStorage.getItem(REFERRAL_KEY) ?? undefined,
2022-12-13 14:46:32 +00:00
);
2022-12-14 11:36:08 +00:00
2022-12-15 15:43:18 +00:00
const addressMsgs = adr36Infos.map((adr36Info) => {
return makeSetRecordMessage(
key.bech32Address,
twitterInfo.username,
adr36Info,
);
});
const aminoMsgs = twitterAuthInfo?.isRegistered
? []
: [registerMsg.amino];
const protoMsgs = twitterAuthInfo?.isRegistered
? []
: [registerMsg.proto];
for (const addressMsg of addressMsgs) {
aminoMsgs.push(addressMsg.amino);
protoMsgs.push(addressMsg.proto);
}
2022-12-13 14:46:32 +00:00
2022-12-15 15:43:18 +00:00
const chainInfo = {
chainId: MainChainId,
rest: REST_URL,
};
2022-12-13 14:46:32 +00:00
2022-12-15 15:43:18 +00:00
const simulated = await simulateMsgs(
chainInfo,
key.bech32Address,
{
proto: protoMsgs,
},
{
amount: [],
},
);
2022-12-13 14:46:32 +00:00
2022-12-15 15:43:18 +00:00
const txHash = await sendMsgs(
wallet,
chainInfo,
key.bech32Address,
{
amino: aminoMsgs,
proto: protoMsgs,
},
{
amount: [],
gas: Math.floor(simulated.gasUsed * 1.5).toString(),
},
);
2022-12-14 07:52:05 +00:00
2022-12-15 15:43:18 +00:00
await router.push({
pathname: "complete",
query: {
txHash: Buffer.from(txHash).toString("hex"),
twitterUsername: twitterInfo.username,
},
});
}
} catch (error) {
if (Axios.isAxiosError(error)) {
console.error((error?.response?.data as QueryError).message);
}
2022-12-13 14:46:32 +00:00
}
};
2022-12-15 15:43:18 +00:00
const isRegisterButtonDisable =
checkedItems.size < 1 ||
(!isOwner && registeredChainList.length > 0) ||
!isAgree;
2022-12-01 08:33:51 +00:00
return (
2022-12-06 14:53:31 +00:00
<Container>
<Logo />
2022-12-01 08:33:51 +00:00
2022-12-06 14:53:31 +00:00
<MainContainer>
2022-12-07 09:17:59 +00:00
{isLoading ? (
2022-12-09 11:59:52 +00:00
<SkeletonChainList />
2022-12-07 09:17:59 +00:00
) : (
<ContentContainer>
2022-12-15 15:43:18 +00:00
<BackButton />
2022-12-09 11:59:52 +00:00
<TwitterProfile twitterProfileInformation={twitterAuthInfo} />
2022-12-07 09:17:59 +00:00
<ChainListTitleContainer>
<ChainListTitle>Chain List</ChainListTitle>
2022-12-13 13:10:31 +00:00
<SearchInput
searchValue={searchValue}
setSearchValue={setSearchValue}
2022-12-14 15:39:51 +00:00
/>
2022-12-07 09:17:59 +00:00
</ChainListTitleContainer>
2022-12-14 15:39:51 +00:00
2022-12-13 13:10:31 +00:00
{allChains && !searchValue ? (
<AllChainsItem
allChecked={allChecked}
setAllChecked={setAllChecked}
chainItem={allChains}
/>
) : null}
<ChainList
allChecked={allChecked}
setAllChecked={setAllChecked}
2022-12-13 13:10:31 +00:00
chainList={chainList.filter(
(chain) =>
chain.chainId.includes(searchValue) ||
chain.address.includes(searchValue) ||
chain.prefix.includes(searchValue),
)}
2022-12-15 10:06:44 +00:00
disabledChainList={disabledChainList.filter(
(chain) =>
chain.chainId.includes(searchValue) ||
chain.address.includes(searchValue) ||
chain.prefix.includes(searchValue),
)}
checkedItems={checkedItems}
setCheckedItems={setCheckedItems}
/>
2022-12-07 09:17:59 +00:00
2022-12-15 15:45:55 +00:00
<AgreeContainer
onClick={() => {
setIsAgree(!isAgree);
}}
>
<AgreeCheckBox type="checkbox" checked={isAgree} readOnly />I
check that Osmo is required for this transaction
2022-12-15 15:43:18 +00:00
</AgreeContainer>
<ButtonContainer disabled={isRegisterButtonDisable}>
<PrimaryButton
2022-12-15 15:43:18 +00:00
disabled={isRegisterButtonDisable}
onClick={onClickRegistration}
>
Register
</PrimaryButton>
2022-12-07 09:17:59 +00:00
</ButtonContainer>
</ContentContainer>
)}
2022-12-06 14:53:31 +00:00
</MainContainer>
</Container>
2022-12-01 08:33:51 +00:00
);
}
2022-12-07 13:55:22 +00:00
const Container = styled.div`
width: 100vw;
height: 100vh;
`;
const MainContainer = styled.div`
display: flex;
justify-content: center;
2022-12-15 15:43:18 +00:00
height: 100vh;
padding: 2.7rem 0;
2022-12-07 13:55:22 +00:00
color: white;
`;
2022-12-09 11:59:52 +00:00
export const ContentContainer = styled.div`
2022-12-07 13:55:22 +00:00
display: flex;
flex-direction: column;
align-items: center;
width: 40rem;
`;
2022-12-15 15:43:18 +00:00
export const ButtonContainer = styled.div<{ disabled?: boolean }>`
2022-12-16 06:55:31 +00:00
width: 11rem;
height: 3.5rem;
2022-12-07 13:55:22 +00:00
2022-12-15 15:43:18 +00:00
background-color: ${(props) =>
props.disabled ? color.orange["300"] : color.orange["100"]};
2022-12-07 13:55:22 +00:00
`;
2022-12-09 11:59:52 +00:00
export const ChainListTitleContainer = styled.div`
2022-12-07 13:55:22 +00:00
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
width: 100%;
margin-top: 2rem;
margin-bottom: 1rem;
`;
const ChainListTitle = styled.div`
font-weight: 700;
font-size: 1.5rem;
line-height: 1.9rem;
color: ${color.white};
`;
2022-12-15 15:43:18 +00:00
const AgreeContainer = styled.div`
display: flex;
align-items: center;
gap: 0.5rem;
font-family: "Inter", serif;
font-style: normal;
font-weight: 500;
font-size: 0.8rem;
line-height: 0.8rem;
text-transform: uppercase;
2022-12-15 15:45:55 +00:00
user-select: none;
2022-12-15 15:43:18 +00:00
color: ${color.grey["400"]};
padding: 2rem 0;
2022-12-15 15:45:55 +00:00
cursor: pointer;
2022-12-15 15:43:18 +00:00
`;
const AgreeCheckBox = styled.input.attrs({ type: "checkbox" })`
width: 1.2rem;
height: 1.2rem;
2022-12-16 06:55:31 +00:00
accent-color: ${color.orange["200"]};
2022-12-15 15:43:18 +00:00
`;