-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update resolver and publish hooks
- Loading branch information
Showing
6 changed files
with
256 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { decodeContenthash, encodeContenthash, namehash } from '@ensdomains/ui'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { useENSNameResolverContract } from './useENSNameResolverContract'; | ||
|
||
type ContentHashProtocolType = 'ipns' | 'ipfs'; | ||
|
||
interface UseContentnHashResults { | ||
isLoading: boolean; | ||
data: ContenthashStruct; | ||
/** | ||
* Refetches the content hash | ||
* @returns void | ||
*/ | ||
refetch: () => void; | ||
} | ||
|
||
export interface ContenthashStruct { | ||
/** | ||
* Encoded content hash (e.g. 0xe30101701220b9c015918bd73d3e800000000000000) | ||
*/ | ||
contenthash?: '0x' | string; | ||
/** | ||
* Decoded content hash (e.g. bafybeib7z3q2) | ||
*/ | ||
contenthashCID?: string; | ||
/** | ||
* Decoded content hash with protocol (e.g. ipfs://bafybeib7z3q2) | ||
*/ | ||
contenthashURI?: `${ContentHashProtocolType}://${string}`; | ||
/** | ||
* Content hash protocol type (e.g. ipfs) | ||
*/ | ||
protocolType?: ContentHashProtocolType; | ||
} | ||
|
||
/** | ||
* Returns the content hash of an ENS name | ||
* @param ensName The ENS name | ||
* @returns The content hash of the ENS name | ||
*/ | ||
export function useContenthash(ensName: string): UseContentnHashResults { | ||
const [refetchCount, setRefetchCount] = useState(0); | ||
const { data: resolverContract } = useENSNameResolverContract(ensName, true); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [data, setData] = useState<ContenthashStruct>({}); | ||
|
||
useEffect(() => { | ||
if (!resolverContract || !ensName) { | ||
return; | ||
} | ||
|
||
setIsLoading(true); | ||
|
||
resolverContract | ||
.contenthash(namehash(ensName)) | ||
.then((encodeContenthash) => { | ||
const { protocolType, decoded } = decodeContenthash(encodeContenthash); | ||
|
||
const contenthashURI = | ||
protocolType && decoded ? (`${protocolType}://${decoded}` as ContenthashStruct['contenthashURI']) : undefined; | ||
|
||
setData({ | ||
contenthash: encodeContenthash, | ||
contenthashCID: decoded, | ||
contenthashURI, | ||
protocolType: protocolType as ContentHashProtocolType, | ||
}); | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
}) | ||
.finally(() => { | ||
setIsLoading(false); | ||
}); | ||
}, [refetchCount, ensName, resolverContract]); | ||
|
||
const refetch = () => { | ||
setRefetchCount((prev) => prev + 1); | ||
}; | ||
|
||
return { | ||
isLoading, | ||
data, | ||
refetch, | ||
}; | ||
} | ||
|
||
/** | ||
* Transforms a CID into a content hash struct | ||
* @param cid The CID | ||
* @param protocolType The protocol type (e.g. ipfs) | ||
* @returns The content hash struct | ||
*/ | ||
export function getContentHashStruct(cid: string, protocolType: ContentHashProtocolType): ContenthashStruct { | ||
const nextContenthashData: ContenthashStruct = { | ||
contenthashURI: `${protocolType}://${cid}`, | ||
contenthash: encodeContenthash(`${protocolType}://${cid}`).encoded as unknown as string, | ||
contenthashCID: cid, | ||
protocolType, | ||
}; | ||
|
||
return nextContenthashData; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Signer } from '@wagmi/core'; | ||
import { useEffect, useMemo, useState } from 'react'; | ||
import { useSigner } from 'wagmi'; | ||
|
||
import { useRainbow } from './useRainbow'; | ||
import { PUBLIC_RESOLVER_ADDRESSES } from '../constants'; | ||
import { EnsPublicResolver, EnsPublicResolver__factory } from '../generated/contracts'; | ||
|
||
/** | ||
* Returns the ENS Resolver contract instance for a given ENS name | ||
* @param ensName The ENS name | ||
* @param withSignerIfPossible Whether to use a signer if one is available | ||
* @returns The ENS Resolver contract instance | ||
*/ | ||
export function useENSNameResolverContract(ensName?: string, withSignerIfPossible = true) { | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [data, setData] = useState<EnsPublicResolver | null>(null); | ||
const { data: signer } = useSigner(); | ||
const { provider } = useRainbow(); | ||
const signerOrProvider = useMemo(() => { | ||
return withSignerIfPossible === true ? (signer as Signer) : provider; | ||
}, [provider, signer, withSignerIfPossible]); | ||
|
||
useEffect(() => { | ||
if (!provider || !ensName) { | ||
setIsLoading(false); | ||
setData(null); | ||
return; | ||
} | ||
|
||
provider | ||
.getResolver(ensName) | ||
.then((nextResolver) => { | ||
setData( | ||
nextResolver !== null ? EnsPublicResolver__factory.connect(nextResolver.address, signerOrProvider) : null | ||
); | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
setData(null); | ||
}) | ||
.finally(() => { | ||
setIsLoading(false); | ||
}); | ||
}, [ensName, provider, signerOrProvider]); | ||
|
||
return { | ||
isLoading, | ||
data, | ||
}; | ||
} | ||
|
||
/** | ||
* Returns a ENS Public Resolver contract instance | ||
* @param withSignerIfPossible Whether to use a signer if one is available | ||
* @returns The ENS Public Resolver contract instance | ||
*/ | ||
export function useENSPublicResolverContract(withSignerIfPossible = true): EnsPublicResolver | null { | ||
const { chainId, provider } = useRainbow(); | ||
const { data: signer } = useSigner(); | ||
|
||
if (chainId && PUBLIC_RESOLVER_ADDRESSES[chainId] !== undefined) { | ||
return EnsPublicResolver__factory.connect( | ||
PUBLIC_RESOLVER_ADDRESSES[chainId], | ||
withSignerIfPossible === true ? (signer as Signer) : provider | ||
); | ||
} | ||
|
||
return null; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.