From 8504c84d3c488e04a6eebe6bab59b9a84544fbca Mon Sep 17 00:00:00 2001 From: HeesungB Date: Tue, 13 Dec 2022 23:46:32 +0900 Subject: [PATCH] [WIP] Simulate --- components/chain-list/all-chains-item.tsx | 2 +- components/chain-list/chain-item.tsx | 3 +- components/chain-list/chain-list.tsx | 5 +- constants/icns.ts | 7 ++ pages/verification/index.tsx | 133 ++++++++++++++++++++-- 5 files changed, 137 insertions(+), 13 deletions(-) create mode 100644 constants/icns.ts diff --git a/components/chain-list/all-chains-item.tsx b/components/chain-list/all-chains-item.tsx index 22d959d..06c7c0f 100644 --- a/components/chain-list/all-chains-item.tsx +++ b/components/chain-list/all-chains-item.tsx @@ -52,7 +52,7 @@ export const AllChainsItem: FunctionComponent = (props) => { - + ); diff --git a/components/chain-list/chain-item.tsx b/components/chain-list/chain-item.tsx index b1e0cd1..00a63a3 100644 --- a/components/chain-list/chain-item.tsx +++ b/components/chain-list/chain-item.tsx @@ -41,6 +41,7 @@ export const ChainItem: FunctionComponent = (props) => { @@ -57,7 +58,7 @@ export const ChainItem: FunctionComponent = (props) => { - + ); }; diff --git a/components/chain-list/chain-list.tsx b/components/chain-list/chain-list.tsx index ef0f80e..ab7ba36 100644 --- a/components/chain-list/chain-list.tsx +++ b/components/chain-list/chain-list.tsx @@ -73,7 +73,10 @@ export const ChainContainer = styled.div` background-color: ${(props) => props.color}; `; -export const ChainItemContainer = styled.div<{ isLoading: boolean }>` +export const ChainItemContainer = styled.div<{ + isLoading: boolean; + checked?: boolean; +}>` display: flex; flex-direction: row; align-items: center; diff --git a/constants/icns.ts b/constants/icns.ts new file mode 100644 index 0000000..b708260 --- /dev/null +++ b/constants/icns.ts @@ -0,0 +1,7 @@ +export const RPC_URL = ""; +export const REST_URL = "https://lcd.testnet.osmosis.zone"; + +export const REGISTRAR_ADDRESS = + "osmo1npn97g7hsgqlp70rw8nhd7c7vyvkukv9x0n25sn4fk5mgcjlz4gq9zlgf3"; +export const RESOLVER_ADDRESS = + "osmo1002awr7frr9wk44lc3vfzt0d2w6vz5z03ql6fszjsjy8vdcvk0sskruz3c"; diff --git a/pages/verification/index.tsx b/pages/verification/index.tsx index 8e79ab8..2ff09b3 100644 --- a/pages/verification/index.tsx +++ b/pages/verification/index.tsx @@ -22,12 +22,22 @@ import { TwitterProfile } from "../../components/twitter-profile"; import { ChainList } from "../../components/chain-list"; import { useRouter } from "next/router"; import { MainChainId } from "../../constants/wallet"; -import { getKeplrFromWindow, KeplrWallet } from "../../wallets"; +import { + getKeplrFromWindow, + KeplrWallet, + makeCosmwasmExecMsg, + simulateMsgs, +} from "../../wallets"; import { ChainIdHelper } from "@keplr-wallet/cosmos"; import AllChainsIcon from "../../public/images/svg/all-chains-icon.svg"; import { AllChainsItem } from "../../components/chain-list/all-chains-item"; import { SearchInput } from "../../components/search-input"; +import { + REGISTRAR_ADDRESS, + RESOLVER_ADDRESS, + REST_URL, +} from "../../constants/icns"; export default function VerificationPage() { const router = useRouter(); @@ -51,7 +61,8 @@ export default function VerificationPage() { return; } - await fetchTwitterInfo(); + const twitterInfo = await fetchTwitterInfo(); + setTwitterAuthInfo(twitterInfo); await initWallet(); @@ -62,7 +73,7 @@ export default function VerificationPage() { handleVerification(); }, []); - const fetchTwitterInfo = async () => { + const fetchTwitterInfo = async (): Promise => { const [, state, code] = window.location.search.match( /^(?=.*state=([^&]+)|)(?=.*code=([^&]+)|).+$/, @@ -72,7 +83,7 @@ export default function VerificationPage() { `/api/twitter-auth-info?state=${state}&code=${code}`, ); - setTwitterAuthInfo(newTwitterAuthInfo); + return newTwitterAuthInfo; }; const initWallet = async () => { @@ -144,8 +155,8 @@ export default function VerificationPage() { } }; - const verifyTwitterAccount = async () => { - if (twitterAuthInfo && wallet) { + const verifyTwitterAccount = async (accessToken: string) => { + if (wallet) { const key = await wallet.getKey(MainChainId); const icnsVerificationList = ( @@ -156,19 +167,121 @@ export default function VerificationPage() { }, body: JSON.stringify({ claimer: key.bech32Address, - authToken: twitterAuthInfo.accessToken, + authToken: accessToken, }), }) ).verificationList; - console.log(icnsVerificationList); + return icnsVerificationList; + } + }; + + const checkAdr36 = async () => { + if (twitterAuthInfo && wallet) { + const key = await wallet.getKey(MainChainId); + + const chainIds = Array.from(checkedItems).map( + (chain) => (chain as ChainItemType).chainId, + ); + + const adr36Infos = await wallet.signICNSAdr36( + MainChainId, + RESOLVER_ADDRESS, + key.bech32Address, + twitterAuthInfo.username, + chainIds, + ); + + return adr36Infos; } }; const onClickRegistration = async () => { - await verifyTwitterAccount(); + const adr36Infos = await checkAdr36(); - await router.push("/complete"); + const twitterInfo = await fetchTwitterInfo(); + + const icnsVerificationList = await verifyTwitterAccount( + twitterInfo.accessToken, + ); + + if (wallet && icnsVerificationList && adr36Infos) { + const key = await wallet.getKey(MainChainId); + + const registerMsg = makeCosmwasmExecMsg( + key.bech32Address, + REGISTRAR_ADDRESS, + { + claim: { + name: twitterInfo.username, + verifying_msg: + icnsVerificationList[0].status === "fulfilled" + ? icnsVerificationList[0].value.data.verifying_msg + : "", + verifications: icnsVerificationList.map((verification) => { + if (verification.status === "fulfilled") { + return { + public_key: verification.value.data.public_key, + signature: verification.value.data.signature, + }; + } + }), + }, + }, + [ + { + denom: "uosmo", + amount: "500000", + }, + ], + ); + + const addressMsgs = adr36Infos.map((adr36Info) => { + return makeCosmwasmExecMsg( + key.bech32Address, + RESOLVER_ADDRESS, + { + set_record: { + name: twitterInfo.username, + bech32_prefix: adr36Info.bech32Prefix, + adr36_info: { + signer_bech32_address: adr36Info.bech32Address, + address_hash: adr36Info.addressHash, + pub_key: Buffer.from(adr36Info.pubKey).toString("base64"), + signature: Buffer.from(adr36Info.signature).toString("base64"), + signature_salt: adr36Info.signatureSalt.toString(), + }, + }, + }, + [], + ); + }); + + const aminoMsgs = [registerMsg.amino]; + const protoMsgs = [registerMsg.proto]; + for (const addressMsg of addressMsgs) { + aminoMsgs.push(addressMsg.amino); + protoMsgs.push(addressMsg.proto); + } + + const chainInfo = { + chainId: MainChainId, + rest: REST_URL, + }; + + const simulated = await simulateMsgs( + chainInfo, + key.bech32Address, + { + proto: protoMsgs, + }, + { + amount: [], + }, + ); + + console.log("simulated", simulated); + } }; return (