Update ENS Content-Hash with viem #1159
-
Hi, import { encode } from '@ensdomains/content-hash'
import { namehash, normalize } from 'viem/ens'
import { parseAbi } from 'viem/abi'
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
transport: http(),
chain: mainnet,
})
const abi = parseAbi([
'function setContenthash(bytes32 node, bytes calldata hash) external',
])
export const encodeIpfsHashAndUpdateEns = async (cid: string, ens: string) => {
const contentHash = encode('ipfs', cid) as `0x${string}`
const { account } = createClient()
const node = namehash(normalize(ens))
const { request } = await publicClient.simulateContract({
abi,
functionName: 'setContenthash',
account,
address: '0x231b0Ee14048e9dCcD1d247744d114a4EB5E8E63',
args: [node, contentHash],
})
console.log(request)
}
await encodeIpfsHashAndUpdateEns('baq...', 'hello.eth') I get the following error when I run:
What am I doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Almost everything in your code looks good to me I did slight changes, and tested in my end and all worked fine; import ch from 'content-hash';
import { namehash, normalize } from 'viem/ens';
import { privateKeyToAccount } from 'viem/accounts';
import { parseAbi } from 'viem'
import { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';
const publicClient = createPublicClient({
transport: http(),
chain: mainnet,
});
const abi = parseAbi([
'function setContenthash(bytes32 node, bytes calldata hash) external',
]);
const encodeIpfsHashAndUpdateEns = async (cid, ens) => {
// used fromIpfs instead of encode. encode method was giving different hex result when using CIDv0
const contentHash = `0x${ch.fromIpfs(cid)}`;
const account = WHICHEVER_THE_WAY_YOU_GET_YOUR_ACCOUNT;
const node = namehash(normalize(ens));
const { request } = await publicClient.simulateContract({
abi,
functionName: 'setContenthash',
account,
address: '0x231b0Ee14048e9dCcD1d247744d114a4EB5E8E63',
args: [node, contentHash],
});
console.log(request);
};
await encodeIpfsHashAndUpdateEns(
'QmPuarC6uGDTzMNuLCiWrBRDYT7bQGhRqUinWDs4DAqaQm',
'testaccount.eth'
); One important thing is that when you update records in PublicResolver, you should use the account that owns the name you are updating. So in case of |
Beta Was this translation helpful? Give feedback.
Almost everything in your code looks good to me
I did slight changes, and tested in my end and all worked fine;