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

fix(starknetid): fix issue where starknet ID does not resolve #358

Merged
merged 33 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
0cadd7b
hotfix: remove console log [skip ci]
metalboyrick Oct 14, 2024
3bd05ed
Merge branch 'main' of https://github.com/Quantum3-Labs/scaffold-stark-2
metalboyrick Oct 15, 2024
84f0b34
Merge branch 'main' of https://github.com/Quantum3-Labs/scaffold-stark-2
metalboyrick Oct 15, 2024
296fcad
Merge branch 'main' of https://github.com/Quantum3-Labs/scaffold-stark-2
metalboyrick Oct 15, 2024
e2a4687
Merge branch 'main' of https://github.com/Quantum3-Labs/scaffold-stark-2
metalboyrick Oct 18, 2024
9d10b03
Merge branch 'main' of https://github.com/Quantum3-Labs/scaffold-stark-2
metalboyrick Oct 18, 2024
6cbeb33
Merge branch 'main' of https://github.com/Quantum3-Labs/scaffold-stark-2
metalboyrick Oct 22, 2024
976c292
Merge branch 'main' of https://github.com/Quantum3-Labs/scaffold-stark-2
metalboyrick Oct 25, 2024
d455a21
Merge branch 'main' of https://github.com/Quantum3-Labs/scaffold-stark-2
metalboyrick Nov 3, 2024
472ad57
Merge branch 'main' of https://github.com/Quantum3-Labs/scaffold-stark-2
metalboyrick Nov 7, 2024
a3a66e1
Merge branch 'main' of https://github.com/Quantum3-Labs/scaffold-stark-2
metalboyrick Nov 13, 2024
a6e739c
wip: fix on starknet id address to name resolution
metalboyrick Nov 19, 2024
cd20722
fix: svg config
metalboyrick Nov 21, 2024
41ec244
chore: revert scaffold config
metalboyrick Nov 21, 2024
e037db4
Merge branch 'main' of https://github.com/Quantum3-Labs/scaffold-stark-2
metalboyrick Nov 21, 2024
26d4181
chore: move dependencies to next js package
metalboyrick Nov 21, 2024
45a8307
fix: point to correct network in the sdk
metalboyrick Nov 21, 2024
bf1e53a
Merge branch 'main' of https://github.com/Quantum3-Labs/scaffold-stark-2
metalboyrick Nov 22, 2024
7f9330c
Merge branch 'main' of https://github.com/Quantum3-Labs/scaffold-stark-2
metalboyrick Dec 3, 2024
28c15ef
fix(starknetid): sepolia resolver
metalboyrick Dec 6, 2024
1e67b25
Merge branch 'main' of https://github.com/Quantum3-Labs/scaffold-stark-2
metalboyrick Dec 6, 2024
0f7b5d9
Merge branch 'main' into fix/starknetid-issue
metalboyrick Dec 6, 2024
6252964
chore(starknetid): rename and move hooks
metalboyrick Dec 6, 2024
96e638e
chore: code cleanup
metalboyrick Dec 6, 2024
1957eb4
refactor: use starknet id api instead to remove rate limit contract c…
metalboyrick Dec 7, 2024
854da91
fix: json parse error
metalboyrick Dec 8, 2024
99ca556
chore: format
metalboyrick Dec 8, 2024
06ea2aa
fix: remove debug address
metalboyrick Dec 8, 2024
03c5677
chore: revert config to devnet
metalboyrick Dec 8, 2024
703085a
chore: revert deployed contracts
metalboyrick Dec 8, 2024
6450ca3
fix: changing account should change profile state
metalboyrick Dec 8, 2024
3a41bfd
chore: remove unused ts expect error
metalboyrick Dec 8, 2024
8c09ea9
chore: remove unused imports
metalboyrick Dec 8, 2024
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
2 changes: 1 addition & 1 deletion packages/nextjs/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1 @@
NEXT_PUBLIC_PROVIDER_URL=https://starknet-sepolia.public.blastapi.io/rpc/v0_7
NEXT_PUBLIC_PROVIDER_URL=https://starknet-sepolia.blastapi.io/64168c77-3fa5-4e1e-9fe4-41675d212522/rpc/v0_7
1 change: 1 addition & 0 deletions packages/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Link from "next/link";
import Image from "next/image";
import { ConnectedAddress } from "~~/components/ConnectedAddress";
import { Address } from "~~/components/scaffold-stark";
metalboyrick marked this conversation as resolved.
Show resolved Hide resolved

const Home = () => {
return (
Expand Down
87 changes: 41 additions & 46 deletions packages/nextjs/components/scaffold-stark/Address.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
"use client";

import { useMemo, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import Link from "next/link";
import { CopyToClipboard } from "react-copy-to-clipboard";
import { Address as AddressType } from "@starknet-react/chains";
import {
getChecksumAddress,
validateAndParseAddress,
validateChecksumAddress,
} from "starknet";
import { getChecksumAddress, validateChecksumAddress } from "starknet";
import { devnet } from "@starknet-react/chains";
import {
CheckCircleIcon,
Expand All @@ -17,8 +13,7 @@ import {
import { useTargetNetwork } from "~~/hooks/scaffold-stark/useTargetNetwork";
import { getBlockExplorerAddressLink } from "~~/utils/scaffold-stark";
import { BlockieAvatar } from "~~/components/scaffold-stark/BlockieAvatar";
import { getStarknetPFPIfExists } from "~~/utils/profile";
import useConditionalStarkProfile from "~~/hooks/useConditionalStarkProfile";
import useScaffoldStarkProfile from "~~/hooks/scaffold-stark/useScaffoldStarkProfile";

type AddressProps = {
address?: AddressType;
Expand Down Expand Up @@ -46,33 +41,43 @@ export const Address = ({
format,
size = "base",
}: AddressProps) => {
const [ens, setEns] = useState<string | null>();
const [ensAvatar, setEnsAvatar] = useState<string | null>();
const [addressCopied, setAddressCopied] = useState(false);
const [isUseBlockie, setIsUseBlockie] = useState(false);

const { targetNetwork } = useTargetNetwork();
const { data: profile } = useConditionalStarkProfile(address);
const { data: fetchedProfile, isLoading } = useScaffoldStarkProfile(address);

const checkSumAddress = useMemo(() => {
if (!address) return undefined;
return getChecksumAddress(address);
}, [address]);

// const checkSumAddress = address ? address : undefined;
const blockExplorerAddressLink = getBlockExplorerAddressLink(
targetNetwork,
checkSumAddress || address || "",
);

const [displayAddress, setDisplayAddress] = useState(
checkSumAddress?.slice(0, 6) + "..." + checkSumAddress?.slice(-4),
);

// TODO add starkprofile | not working on devnet now
//const { data: fetchedProfile } = useStarkProfile({ address });
useEffect(() => {
const addressWithFallback = checkSumAddress || address || "";

// We need to apply this pattern to avoid Hydration errors.
// useEffect(() => {
// if (fetchedProfile) {
// setEns(fetchedProfile.name);
// setEnsAvatar(fetchedProfile.profilePicture);
// }
// }, [fetchedProfile]);
if (fetchedProfile?.name) {
setDisplayAddress(fetchedProfile.name);
} else if (format === "long") {
setDisplayAddress(addressWithFallback || "");
} else {
setDisplayAddress(
addressWithFallback.slice(0, 6) + "..." + addressWithFallback.slice(-4),
);
}
}, [fetchedProfile, checkSumAddress, address, format]);

// Skeleton UI
if (!checkSumAddress) {
if (!checkSumAddress || isLoading) {
return (
<div className="animate-pulse flex space-x-4">
<div className="rounded-md bg-slate-300 h-6 w-6"></div>
Expand All @@ -87,47 +92,37 @@ export const Address = ({
return <span className="text-error">Wrong address</span>;
}

const blockExplorerAddressLink = getBlockExplorerAddressLink(
targetNetwork,
checkSumAddress,
);
let displayAddress =
checkSumAddress?.slice(0, 6) + "..." + checkSumAddress?.slice(-4);

if (ens) {
displayAddress = ens;
} else if (format === "long") {
displayAddress = checkSumAddress;
}

return (
<div className="flex items-center">
<div className="flex-shrink-0">
{getStarknetPFPIfExists(profile?.profilePicture) ? (
//eslint-disable-next-line @next/next/no-img-element
{isUseBlockie ? (
<BlockieAvatar
address={checkSumAddress}
ensImage={ensAvatar}
size={(blockieSizeMap[size] * 24) / blockieSizeMap["base"]}
/>
) : (
// eslint-disable-next-line @next/next/no-img-element
<img
src={profile?.profilePicture}
src={fetchedProfile?.profilePicture || ""}
alt="Profile Picture"
className="rounded-full h-6 w-6"
width={24}
height={24}
/>
) : (
<BlockieAvatar
address={checkSumAddress}
ensImage={ensAvatar}
size={(blockieSizeMap[size] * 24) / blockieSizeMap["base"]}
onError={() => {
setIsUseBlockie(true);
}}
/>
)}
</div>
{disableAddressLink ? (
<span className={`ml-1.5 text-${size} font-normal`}>
{profile?.name || displayAddress}
{fetchedProfile?.name || displayAddress}
</span>
) : targetNetwork.network === devnet.network ? (
<span className={`ml-1.5 text-${size} font-normal`}>
<Link href={blockExplorerAddressLink}>
{profile?.name || displayAddress}
{fetchedProfile?.name || displayAddress}
</Link>
</span>
) : (
Expand All @@ -137,7 +132,7 @@ export const Address = ({
href={blockExplorerAddressLink}
rel="noopener noreferrer"
>
{profile?.name || displayAddress}
{fetchedProfile?.name || displayAddress}
</a>
)}
{addressCopied ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { burnerAccounts } from "~~/utils/devnetAccounts";
import { Address } from "@starknet-react/chains";
import { useDisconnect, useNetwork, useConnect } from "@starknet-react/core";
import { getStarknetPFPIfExists } from "~~/utils/profile";
import useConditionalStarkProfile from "~~/hooks/useConditionalStarkProfile";
import useScaffoldStarkProfile from "~~/hooks/scaffold-stark/useScaffoldStarkProfile";
import { useTheme } from "next-themes";
import { default as NextImage } from "next/image";

Expand All @@ -42,7 +42,7 @@ export const AddressInfoDropdown = ({
}: AddressInfoDropdownProps) => {
const { disconnect } = useDisconnect();
const [addressCopied, setAddressCopied] = useState(false);
const { data: profile } = useConditionalStarkProfile(address);
const { data: profile } = useScaffoldStarkProfile(address);
const { chain } = useNetwork();
const [showBurnerAccounts, setShowBurnerAccounts] = useState(false);
const [selectingNetwork, setSelectingNetwork] = useState(false);
Expand Down
84 changes: 84 additions & 0 deletions packages/nextjs/hooks/scaffold-stark/useScaffoldStarkProfile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import * as chains from "@starknet-react/chains";
import scaffoldConfig from "~~/scaffold.config";
import { useEffect, useState } from "react";
import { StarkProfile } from "starknet";

const shouldUseProfile = () => {
const set = new Set(["mainnet", "sepolia"]);
return (
set.has(scaffoldConfig.targetNetworks[0].network) &&
// @ts-expect-error program thinks this is constant
scaffoldConfig.targetNetworks[0].network !== chains.devnet.network
);
};

const starknetIdApiBaseUrl =
// @ts-expect-error program thinks this is constant
scaffoldConfig.targetNetworks[0].network === chains.mainnet.network
? "https://api.starknet.id"
: "https://sepolia.api.starknet.id";

const fetchProfileFromApi = async (address: string) => {
const addrToDomainRes = await fetch(
`${starknetIdApiBaseUrl}/addr_to_domain?addr=${address}`,
);

if (!addrToDomainRes.ok) {
throw new Error(await addrToDomainRes.text());
}

const addrToDomainJson = await addrToDomainRes.json();

const domain = addrToDomainJson.domain;

const profileRes = await fetch(
`${starknetIdApiBaseUrl}/domain_to_data?domain=${domain}`,
);

if (!profileRes.ok) throw new Error(await profileRes.text());

const profileData = await profileRes.json();

return {
name: profileData.domain.domain,

// TODO: figure out where these go in case we have PFP, because its a bit complex to parse the data
// profilePicture?: string;
// discord?: string;
// twitter?: string;
// github?: string;
// proofOfPersonhood?: boolean;
};
};

// this hook is a workaround, basically a re-implement of the starknet react hook with conditional rendering.
const useScaffoldStarkProfile = (address: chains.Address | undefined) => {
const [isLoading, setIsLoading] = useState(false);
const [profile, setProfile] = useState<StarkProfile | undefined>();
const isEnabled = shouldUseProfile();

useEffect(() => {
if (!isEnabled || !address) {
setProfile(undefined);
return;
}

setIsLoading(true);

fetchProfileFromApi(address)
.then((data) => {
setProfile(data);
})
.catch((e) => {
console.error(`[useScaffoldStarkProfile] ` + e.message);
setProfile(undefined);
})
.finally(() => {
setIsLoading(false);
});
}, [address, isEnabled]);

return { data: profile, isLoading };
};

export default useScaffoldStarkProfile;
22 changes: 0 additions & 22 deletions packages/nextjs/hooks/useConditionalStarkProfile.tsx

This file was deleted.

12 changes: 12 additions & 0 deletions packages/nextjs/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
images: {
// make sure no one injects anything funny in svg
dangerouslyAllowSVG: true,
remotePatterns: [
// we might need to add more links. this is just from the starknet ID identicon
{
protocol: "https",
hostname: "identicon.starknet.id",
pathname: "/**", // Allows all paths under this domain
},
],
},
typescript: {
ignoreBuildErrors: process.env.NEXT_PUBLIC_IGNORE_BUILD_ERROR === "true",
},
Expand Down
Loading