forked from LaconicNetwork/icns-frontend
[WIP] : ICNS Query
This commit is contained in:
parent
2655c35c32
commit
8897eac973
@ -17,6 +17,7 @@ export const ConnectWalletModal: FunctionComponent<Props> = (props) => {
|
|||||||
<ReactModal
|
<ReactModal
|
||||||
isOpen={isModalOpen}
|
isOpen={isModalOpen}
|
||||||
onRequestClose={onCloseModal}
|
onRequestClose={onCloseModal}
|
||||||
|
ariaHideApp={false}
|
||||||
style={{
|
style={{
|
||||||
overlay: { background: "#181818b3" },
|
overlay: { background: "#181818b3" },
|
||||||
content: {
|
content: {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
export const MainChainId = "osmo-test-4";
|
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 REST_URL = "https://lcd.testnet.osmosis.zone";
|
||||||
|
|
||||||
export const REGISTRAR_ADDRESS =
|
export const REGISTRAR_ADDRESS =
|
||||||
|
@ -5,7 +5,9 @@ import { useEffect, useState } from "react";
|
|||||||
import {
|
import {
|
||||||
ChainItemType,
|
ChainItemType,
|
||||||
IcnsVerificationResponse,
|
IcnsVerificationResponse,
|
||||||
|
RegisteredAddresses,
|
||||||
TwitterAuthInfoResponse,
|
TwitterAuthInfoResponse,
|
||||||
|
TwitterProfileType,
|
||||||
} from "../../types";
|
} from "../../types";
|
||||||
import { request } from "../../utils/url";
|
import { request } from "../../utils/url";
|
||||||
|
|
||||||
@ -41,18 +43,24 @@ import {
|
|||||||
REST_URL,
|
REST_URL,
|
||||||
} from "../../constants/icns";
|
} from "../../constants/icns";
|
||||||
|
|
||||||
import { fetchTwitterInfo } from "../../repository";
|
import {
|
||||||
|
fetchTwitterInfo,
|
||||||
|
queryAddressesFromTwitterName,
|
||||||
|
queryRegisteredTwitterId,
|
||||||
|
} from "../../repository";
|
||||||
|
|
||||||
export default function VerificationPage() {
|
export default function VerificationPage() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [twitterAuthInfo, setTwitterAuthInfo] =
|
const [twitterAuthInfo, setTwitterAuthInfo] = useState<TwitterProfileType>();
|
||||||
useState<TwitterAuthInfoResponse | null>();
|
|
||||||
|
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
|
||||||
const [wallet, setWallet] = useState<KeplrWallet>();
|
const [wallet, setWallet] = useState<KeplrWallet>();
|
||||||
const [allChains, setAllChains] = useState<ChainItemType>();
|
const [allChains, setAllChains] = useState<ChainItemType>();
|
||||||
const [chainList, setChainList] = useState<ChainItemType[]>([]);
|
const [chainList, setChainList] = useState<ChainItemType[]>([]);
|
||||||
|
const [registeredAddressList, setRegisteredAddressList] = useState<string[]>(
|
||||||
|
[],
|
||||||
|
);
|
||||||
const [checkedItems, setCheckedItems] = useState(new Set());
|
const [checkedItems, setCheckedItems] = useState(new Set());
|
||||||
const [allChecked, setAllChecked] = useState(false);
|
const [allChecked, setAllChecked] = useState(false);
|
||||||
const [searchValue, setSearchValue] = useState("");
|
const [searchValue, setSearchValue] = useState("");
|
||||||
@ -81,15 +89,39 @@ export default function VerificationPage() {
|
|||||||
|
|
||||||
const { state, code } = fetchUrlQueryParameter();
|
const { state, code } = fetchUrlQueryParameter();
|
||||||
|
|
||||||
// Fetch Twitter Profile
|
try {
|
||||||
const twitterInfo = await fetchTwitterInfo(state, code);
|
|
||||||
setTwitterAuthInfo(twitterInfo);
|
|
||||||
|
|
||||||
// Initialize Wallet
|
// Initialize Wallet
|
||||||
await initWallet();
|
await initWallet();
|
||||||
|
|
||||||
|
// Fetch Twitter Profile
|
||||||
|
const twitterInfo = await fetchTwitterInfo(state, code);
|
||||||
|
|
||||||
|
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);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
init();
|
init();
|
||||||
@ -111,6 +143,14 @@ export default function VerificationPage() {
|
|||||||
}
|
}
|
||||||
}, [wallet]);
|
}, [wallet]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const filteredChainList = chainList.filter((chain) => {
|
||||||
|
return registeredAddressList.includes(chain.address);
|
||||||
|
});
|
||||||
|
|
||||||
|
setCheckedItems(new Set(filteredChainList));
|
||||||
|
}, [registeredAddressList]);
|
||||||
|
|
||||||
const fetchChainList = async () => {
|
const fetchChainList = async () => {
|
||||||
if (wallet) {
|
if (wallet) {
|
||||||
const chainIds = (await wallet.getChainInfosWithoutEndpoints()).map(
|
const chainIds = (await wallet.getChainInfosWithoutEndpoints()).map(
|
||||||
@ -193,9 +233,9 @@ export default function VerificationPage() {
|
|||||||
if (twitterAuthInfo && wallet) {
|
if (twitterAuthInfo && wallet) {
|
||||||
const key = await wallet.getKey(MainChainId);
|
const key = await wallet.getKey(MainChainId);
|
||||||
|
|
||||||
const chainIds = Array.from(checkedItems).map(
|
const chainIds = Array.from(checkedItems).map((chain) => {
|
||||||
(chain) => (chain as ChainItemType).chainId,
|
return (chain as ChainItemType).chainId;
|
||||||
);
|
});
|
||||||
|
|
||||||
return await wallet.signICNSAdr36(
|
return await wallet.signICNSAdr36(
|
||||||
MainChainId,
|
MainChainId,
|
||||||
@ -264,13 +304,20 @@ export default function VerificationPage() {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
const aminoMsgs = [registerMsg.amino];
|
const aminoMsgs = twitterAuthInfo?.isRegistered
|
||||||
const protoMsgs = [registerMsg.proto];
|
? []
|
||||||
|
: [registerMsg.amino];
|
||||||
|
const protoMsgs = twitterAuthInfo?.isRegistered
|
||||||
|
? []
|
||||||
|
: [registerMsg.proto];
|
||||||
|
|
||||||
for (const addressMsg of addressMsgs) {
|
for (const addressMsg of addressMsgs) {
|
||||||
aminoMsgs.push(addressMsg.amino);
|
aminoMsgs.push(addressMsg.amino);
|
||||||
protoMsgs.push(addressMsg.proto);
|
protoMsgs.push(addressMsg.proto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(aminoMsgs);
|
||||||
|
|
||||||
const chainInfo = {
|
const chainInfo = {
|
||||||
chainId: MainChainId,
|
chainId: MainChainId,
|
||||||
rest: REST_URL,
|
rest: REST_URL,
|
||||||
|
43
repository/icns.ts
Normal file
43
repository/icns.ts
Normal 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"),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
};
|
@ -1 +1,2 @@
|
|||||||
export * from "./twitter";
|
export * from "./twitter";
|
||||||
|
export * from "./icns";
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
@ -2,3 +2,4 @@ export * from "./width-height-props";
|
|||||||
export * from "./api-response";
|
export * from "./api-response";
|
||||||
export * from "./chain-item-type";
|
export * from "./chain-item-type";
|
||||||
export * from "./msg";
|
export * from "./msg";
|
||||||
|
export * from "./twitter";
|
||||||
|
5
types/twitter.ts
Normal file
5
types/twitter.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import { TwitterAuthInfoResponse } from "./api-response";
|
||||||
|
|
||||||
|
export interface TwitterProfileType extends TwitterAuthInfoResponse {
|
||||||
|
isRegistered: boolean;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user