Skip to content

Commit

Permalink
core: update starknetid hooks & add sepolia contract addresses (#403)
Browse files Browse the repository at this point in the history
- Adds the Sepolia contract addresses for starknetid contracts
- Updates the `useStarkProfile` and `useStarkAddress` hooks so they work
on testnet & mainnet while we port all our contracts to the new version
of cairo.
  • Loading branch information
fracek authored Feb 29, 2024
2 parents f6a1489 + 89a55bc commit 00ec03f
Show file tree
Hide file tree
Showing 8 changed files with 926 additions and 723 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-toys-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@starknet-react/core": minor
---

fix starknetid hooks & add sepolia support to starknetid hooks
1 change: 1 addition & 0 deletions packages/core/src/hooks/useAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe("useAccount", () => {
{
"account": Account {
"address": "0x79d719ac68e56635121bf9317fae4f281e23b7ad95b6900ccafd2b9668b410f",
"cairoVersion": undefined,
"deploySelf": [Function],
"provider": RpcProvider {
"blockIdentifier": "pending",
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/hooks/useContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ describe("useContract", () => {
"callStatic": {
"name": [Function],
},
"deployTransactionHash": undefined,
"estimateFee": {
"name": [Function],
},
Expand Down
64 changes: 55 additions & 9 deletions packages/core/src/hooks/useStarkAddress.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { useMemo } from "react";
import { Provider, ProviderInterface } from "starknet";
import {
CallData,
Provider,
ProviderInterface,
RawArgs,
starknetId,
} from "starknet";

import { UseQueryProps, UseQueryResult, useQuery } from "~/query";
import { useProvider } from "./useProvider";
import { useNetwork } from "./useNetwork";

export type UseStarkAddressProps = UseQueryProps<
string,
Expand Down Expand Up @@ -46,35 +53,74 @@ export function useStarkAddress({
...props
}: UseStarkAddressProps): UseStarkAddressResult {
const { provider } = useProvider();
const { chain } = useNetwork();

const enabled = useMemo(() => Boolean(enabled_ && name), [enabled_, name]);

return useQuery({
queryKey: queryKey({ name, contract }),
queryFn: queryFn({ name, contract, provider }),
queryKey: queryKey({ name, contract, network: chain.network }),
queryFn: queryFn({ name, contract, provider, network: chain.network }),
enabled,
...props,
});
}

function queryKey({ name, contract }: { name?: string; contract?: string }) {
return [{ entity: "addressFromStarkName", name, contract }] as const;
function queryKey({
name,
contract,
network,
}: {
name?: string;
contract?: string;
network?: string;
}) {
return [{ entity: "addressFromStarkName", name, contract, network }] as const;
}

function queryFn({
name,
contract,
provider,
}: UseStarkAddressProps & { provider: ProviderInterface }) {
network,
}: UseStarkAddressProps & { provider: ProviderInterface } & {
network: string;
}) {
return async function () {
if (!name) throw new Error("name is required");

const namingContract = contract ?? StarknetIdNamingContract[network];
const p = new Provider(provider);
const result = await p.getAddressFromStarkName(name, contract);
const encodedDomain = decodeDomain(name);
const calldata: RawArgs =
network === "mainnet"
? { domain: encodedDomain }
: { domain: encodedDomain, hint: [] };
const result = await p.callContract({
contractAddress: namingContract as string,
entrypoint: "domain_to_address",
calldata: CallData.compile(calldata),
});

// StarknetID returns 0x0 if no name is found, but that can be dangerous
// since we can't expect the user to know that 0x0 is not a valid address.
if (BigInt(result) === BigInt(0)) throw new Error("Address not found");
if (BigInt(result.result[0] as string) === BigInt(0))
throw new Error("Address not found");

return result;
return result.result[0] as string;
};
}

const StarknetIdNamingContract: Record<string, string> = {
goerli: "0x3bab268e932d2cecd1946f100ae67ce3dff9fd234119ea2f6da57d16d29fce",
sepolia: "0x5847d20f9757de24395a7b3b47303684003753858737bf288716855dfb0aaf2",
mainnet: "0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678",
};

const decodeDomain = (domain: string): string[] => {
if (!domain) return ["0"];

const encoded = [];
for (const subdomain of domain.replace(".stark", "").split("."))
encoded.push(starknetId.useEncoded(subdomain).toString(10));
return encoded;
};
13 changes: 11 additions & 2 deletions packages/core/src/hooks/useStarkName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Provider, ProviderInterface } from "starknet";

import { UseQueryProps, UseQueryResult, useQuery } from "~/query";
import { useProvider } from "./useProvider";
import { useNetwork } from "./useNetwork";

/** Arguments for `useStarkName` hook. */
export type StarkNameArgs = UseQueryProps<
Expand Down Expand Up @@ -65,10 +66,15 @@ export function useStarkName({
...props
}: StarkNameArgs): StarkNameResult {
const { provider } = useProvider();
const { chain } = useNetwork();
contract =
chain.network === "sepolia"
? "0x5847d20f9757de24395a7b3b47303684003753858737bf288716855dfb0aaf2"
: contract;

const enabled = useMemo(
() => Boolean(enabled_ && address),
[enabled_, address],
[enabled_, address]
);

return useQuery({
Expand All @@ -82,7 +88,10 @@ export function useStarkName({
function queryKey({
address,
contract,
}: { address?: string; contract?: string }) {
}: {
address?: string;
contract?: string;
}) {
return [{ entity: "starkName", address, contract }] as const;
}

Expand Down
28 changes: 22 additions & 6 deletions packages/core/src/hooks/useStarkProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function useStarkProfile({

const enabled = useMemo(
() => Boolean(enabled_ && address),
[enabled_, address],
[enabled_, address]
);

return useQuery({
Expand Down Expand Up @@ -188,7 +188,10 @@ function queryFn({
{
execution: staticExecution(),
to: hardcoded(naming),
selector: hardcoded(hash.getSelectorFromName("domain_to_token_id")),
selector:
network === "mainnet"
? hardcoded(hash.getSelectorFromName("domain_to_token_id"))
: hardcoded(hash.getSelectorFromName("domain_to_id")),
calldata: [arrayReference(0, 0)],
},
{
Expand Down Expand Up @@ -251,7 +254,7 @@ function queryFn({
execution: staticExecution(),
to: hardcoded(identity),
selector: hardcoded(
hash.getSelectorFromName("get_extended_verifier_data"),
hash.getSelectorFromName("get_extended_verifier_data")
),
calldata: [
reference(1, 0),
Expand Down Expand Up @@ -284,7 +287,7 @@ function queryFn({
? data[8]
.slice(1)
.map((val: BigInt) =>
shortString.decodeShortString(val.toString()),
shortString.decodeShortString(val.toString())
)
.join("")
: undefined;
Expand All @@ -293,8 +296,8 @@ function queryFn({
const profilePicture = profile
? await fetchImageUrl(profile)
: useDefaultPfp
? `https://starknet.id/api/identicons/${data[1][0].toString()}`
: undefined;
? `https://starknet.id/api/identicons/${data[1][0].toString()}`
: undefined;

return {
name,
Expand Down Expand Up @@ -377,6 +380,19 @@ const StarknetIdcontracts: Record<string, Record<string, string>> = {
multicall:
"0x034ffb8f4452df7a613a0210824d6414dbadcddce6c6e19bf4ddc9e22ce5f970",
},
sepolia: {
naming: "0x5847d20f9757de24395a7b3b47303684003753858737bf288716855dfb0aaf2",
identity:
"0x718d9172f6e36183abeeff1a0db76a1851cef4ed9b9c13896da79ef4bfcb4d0",
verifier:
"0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf",
verifier_pop:
"0x00f80f68443becd0e0a4a08ff5734e36dd8028507333e4a0ec034dcfdf1b793e",
verifier_pfp:
"0x070c035557d6fed57eed2ed7fa861616b487f8a95439347b805639ca076f29f0",
multicall:
"0x034ffb8f4452df7a613a0210824d6414dbadcddce6c6e19bf4ddc9e22ce5f970",
},
mainnet: {
naming: "0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678",
identity:
Expand Down
Loading

0 comments on commit 00ec03f

Please sign in to comment.