[WIP] : ICNS Query

This commit is contained in:
HeesungB 2022-12-14 20:36:08 +09:00
parent 2655c35c32
commit 8897eac973
8 changed files with 135 additions and 15 deletions

View File

@ -17,6 +17,7 @@ export const ConnectWalletModal: FunctionComponent<Props> = (props) => {
<ReactModal
isOpen={isModalOpen}
onRequestClose={onCloseModal}
ariaHideApp={false}
style={{
overlay: { background: "#181818b3" },
content: {

View File

@ -1,6 +1,6 @@
export const MainChainId = "osmo-test-4";
export const RPC_URL = "";
export const RPC_URL = "https://rpc.testnet.osmosis.zone";
export const REST_URL = "https://lcd.testnet.osmosis.zone";
export const REGISTRAR_ADDRESS =

View File

@ -5,7 +5,9 @@ import { useEffect, useState } from "react";
import {
ChainItemType,
IcnsVerificationResponse,
RegisteredAddresses,
TwitterAuthInfoResponse,
TwitterProfileType,
} from "../../types";
import { request } from "../../utils/url";
@ -41,18 +43,24 @@ import {
REST_URL,
} from "../../constants/icns";
import { fetchTwitterInfo } from "../../repository";
import {
fetchTwitterInfo,
queryAddressesFromTwitterName,
queryRegisteredTwitterId,
} from "../../repository";
export default function VerificationPage() {
const router = useRouter();
const [twitterAuthInfo, setTwitterAuthInfo] =
useState<TwitterAuthInfoResponse | null>();
const [twitterAuthInfo, setTwitterAuthInfo] = useState<TwitterProfileType>();
const [isLoading, setIsLoading] = useState(true);
const [wallet, setWallet] = useState<KeplrWallet>();
const [allChains, setAllChains] = useState<ChainItemType>();
const [chainList, setChainList] = useState<ChainItemType[]>([]);
const [registeredAddressList, setRegisteredAddressList] = useState<string[]>(
[],
);
const [checkedItems, setCheckedItems] = useState(new Set());
const [allChecked, setAllChecked] = useState(false);
const [searchValue, setSearchValue] = useState("");
@ -81,14 +89,38 @@ export default function VerificationPage() {
const { state, code } = fetchUrlQueryParameter();
// Fetch Twitter Profile
const twitterInfo = await fetchTwitterInfo(state, code);
setTwitterAuthInfo(twitterInfo);
try {
// Initialize Wallet
await initWallet();
// Initialize Wallet
await initWallet();
// Fetch Twitter Profile
const twitterInfo = await fetchTwitterInfo(state, code);
setIsLoading(false);
const registeredQueryResponse = await queryRegisteredTwitterId(
twitterInfo.id,
);
setTwitterAuthInfo({
...twitterInfo,
isRegistered: "data" in registeredQueryResponse,
});
if ("data" in registeredQueryResponse) {
const addressesQueryResponse = await queryAddressesFromTwitterName(
registeredQueryResponse.data.name,
);
setRegisteredAddressList(
addressesQueryResponse.data.addresses.map(
(address) => address.address,
),
);
}
} catch (e) {
console.log(e);
} finally {
setIsLoading(false);
}
}
};
@ -111,6 +143,14 @@ export default function VerificationPage() {
}
}, [wallet]);
useEffect(() => {
const filteredChainList = chainList.filter((chain) => {
return registeredAddressList.includes(chain.address);
});
setCheckedItems(new Set(filteredChainList));
}, [registeredAddressList]);
const fetchChainList = async () => {
if (wallet) {
const chainIds = (await wallet.getChainInfosWithoutEndpoints()).map(
@ -193,9 +233,9 @@ export default function VerificationPage() {
if (twitterAuthInfo && wallet) {
const key = await wallet.getKey(MainChainId);
const chainIds = Array.from(checkedItems).map(
(chain) => (chain as ChainItemType).chainId,
);
const chainIds = Array.from(checkedItems).map((chain) => {
return (chain as ChainItemType).chainId;
});
return await wallet.signICNSAdr36(
MainChainId,
@ -264,13 +304,20 @@ export default function VerificationPage() {
);
});
const aminoMsgs = [registerMsg.amino];
const protoMsgs = [registerMsg.proto];
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);
}
console.log(aminoMsgs);
const chainInfo = {
chainId: MainChainId,
rest: REST_URL,

43
repository/icns.ts Normal file
View File

@ -0,0 +1,43 @@
import { request } from "../utils/url";
import {
REGISTRAR_ADDRESS,
RESOLVER_ADDRESS,
REST_URL,
} from "../constants/icns";
import { Buffer } from "buffer/";
import {
AddressesQueryResponse,
NameByTwitterIdQueryResponse,
QueryError,
} from "../types";
const getCosmwasmQueryUrl = (contractAddress: string, queryMsg: string) =>
`${REST_URL}/cosmwasm/wasm/v1/contract/${contractAddress}/smart/${queryMsg}`;
export const queryRegisteredTwitterId = async (
twitterId: string,
): Promise<NameByTwitterIdQueryResponse | QueryError> => {
const msg = {
name_by_twitter_id: { twitter_id: twitterId },
};
return request<NameByTwitterIdQueryResponse>(
getCosmwasmQueryUrl(
REGISTRAR_ADDRESS,
Buffer.from(JSON.stringify(msg)).toString("base64"),
),
);
};
export const queryAddressesFromTwitterName = async (
twitterUsername: string,
): Promise<AddressesQueryResponse> => {
const msg = {
addresses: { name: twitterUsername },
};
return request<any>(
getCosmwasmQueryUrl(
RESOLVER_ADDRESS,
Buffer.from(JSON.stringify(msg)).toString("base64"),
),
);
};

View File

@ -1 +1,2 @@
export * from "./twitter";
export * from "./icns";

View File

@ -37,3 +37,25 @@ export interface IcnsVerificationResponse {
}
)[];
}
export interface NameByTwitterIdQueryResponse {
data: {
name: string;
};
}
export interface AddressesQueryResponse {
data: {
addresses: RegisteredAddresses[];
};
}
export interface RegisteredAddresses {
address: string;
bech32_prefix: string;
}
export interface QueryError {
code: number;
message: string;
}

View File

@ -2,3 +2,4 @@ export * from "./width-height-props";
export * from "./api-response";
export * from "./chain-item-type";
export * from "./msg";
export * from "./twitter";

5
types/twitter.ts Normal file
View File

@ -0,0 +1,5 @@
import { TwitterAuthInfoResponse } from "./api-response";
export interface TwitterProfileType extends TwitterAuthInfoResponse {
isRegistered: boolean;
}