-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from ensdomains/feat/set-individual-record
Allow setting individual records with setRecord
- Loading branch information
Showing
5 changed files
with
224 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { ENS } from '..' | ||
import setup from '../tests/setup' | ||
import { decodeContenthash } from '../utils/contentHash' | ||
import { hexEncodeName } from '../utils/hexEncodedName' | ||
import { namehash } from '../utils/normalise' | ||
|
||
let ENSInstance: ENS | ||
let revert: Awaited<ReturnType<typeof setup>>['revert'] | ||
|
||
beforeAll(async () => { | ||
;({ ENSInstance, revert } = await setup()) | ||
}) | ||
|
||
afterAll(async () => { | ||
await revert() | ||
}) | ||
|
||
describe('setRecord', () => { | ||
it('should allow a text record set', async () => { | ||
const tx = await ENSInstance.setRecord('parthtejpal.eth', { | ||
type: 'text', | ||
record: { key: 'foo', value: 'bar' }, | ||
}) | ||
expect(tx).toBeTruthy() | ||
await tx.wait() | ||
|
||
const universalResolver = | ||
await ENSInstance.contracts!.getUniversalResolver()! | ||
const publicResolver = await ENSInstance.contracts!.getPublicResolver()! | ||
const encodedText = await universalResolver.resolve( | ||
hexEncodeName('parthtejpal.eth'), | ||
publicResolver.interface.encodeFunctionData('text', [ | ||
namehash('parthtejpal.eth'), | ||
'foo', | ||
]), | ||
) | ||
const [resultText] = publicResolver.interface.decodeFunctionResult( | ||
'text', | ||
encodedText[0], | ||
) | ||
expect(resultText).toBe('bar') | ||
}) | ||
it('should allow an address record set', async () => { | ||
const tx = await ENSInstance.setRecord('parthtejpal.eth', { | ||
type: 'addr', | ||
record: { | ||
key: 'ETC', | ||
value: '0x42D63ae25990889E35F215bC95884039Ba354115', | ||
}, | ||
}) | ||
expect(tx).toBeTruthy() | ||
await tx.wait() | ||
|
||
const universalResolver = | ||
await ENSInstance.contracts!.getUniversalResolver()! | ||
const publicResolver = await ENSInstance.contracts!.getPublicResolver()! | ||
const encodedAddr = await universalResolver.resolve( | ||
hexEncodeName('parthtejpal.eth'), | ||
publicResolver.interface.encodeFunctionData('addr(bytes32,uint256)', [ | ||
namehash('parthtejpal.eth'), | ||
'61', | ||
]), | ||
) | ||
const [resultAddr] = publicResolver.interface.decodeFunctionResult( | ||
'addr(bytes32,uint256)', | ||
encodedAddr[0], | ||
) | ||
expect(resultAddr).toBe( | ||
'0x42D63ae25990889E35F215bC95884039Ba354115'.toLowerCase(), | ||
) | ||
}) | ||
it('should allow a contenthash record set', async () => { | ||
const tx = await ENSInstance.setRecord('parthtejpal.eth', { | ||
type: 'contentHash', | ||
record: | ||
'ipns://k51qzi5uqu5dgox2z23r6e99oqency055a6xt92xzmyvpz8mwz5ycjavm0u150', | ||
}) | ||
expect(tx).toBeTruthy() | ||
await tx.wait() | ||
|
||
const universalResolver = | ||
await ENSInstance.contracts!.getUniversalResolver()! | ||
const publicResolver = await ENSInstance.contracts!.getPublicResolver()! | ||
const encodedContent = await universalResolver.resolve( | ||
hexEncodeName('parthtejpal.eth'), | ||
publicResolver.interface.encodeFunctionData('contenthash', [ | ||
namehash('parthtejpal.eth'), | ||
]), | ||
) | ||
const [resultContent] = publicResolver.interface.decodeFunctionResult( | ||
'contenthash', | ||
encodedContent[0], | ||
) | ||
const content = decodeContenthash(resultContent) | ||
expect(content.decoded).toBe( | ||
'k51qzi5uqu5dgox2z23r6e99oqency055a6xt92xzmyvpz8mwz5ycjavm0u150', | ||
) | ||
expect(content.protocolType).toBe('ipns') | ||
}) | ||
}) |
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,63 @@ | ||
import { ENSArgs } from '..' | ||
import { namehash } from '../utils/normalise' | ||
import { | ||
generateSingleRecordCall, | ||
RecordInput, | ||
RecordTypes, | ||
} from '../utils/recordHelpers' | ||
|
||
type BaseInput = { | ||
resolverAddress?: string | ||
} | ||
|
||
type ContentHashInput = { | ||
record: RecordInput<'contentHash'> | ||
type: 'contentHash' | ||
} | ||
|
||
type AddrOrTextInput = { | ||
record: RecordInput<'addr' | 'text'> | ||
type: 'addr' | 'text' | ||
} | ||
|
||
export default async function <T extends RecordTypes>( | ||
{ | ||
contracts, | ||
provider, | ||
getResolver, | ||
signer, | ||
}: ENSArgs<'contracts' | 'provider' | 'getResolver' | 'signer'>, | ||
name: string, | ||
{ | ||
record, | ||
type, | ||
resolverAddress, | ||
}: BaseInput & (ContentHashInput | AddrOrTextInput), | ||
) { | ||
if (!name.includes('.')) { | ||
throw new Error('Input is not an ENS name') | ||
} | ||
|
||
let resolverToUse: string | ||
if (resolverAddress) { | ||
resolverToUse = resolverAddress | ||
} else { | ||
resolverToUse = await getResolver(name) | ||
} | ||
|
||
if (!resolverToUse) { | ||
throw new Error('No resolver found for input address') | ||
} | ||
|
||
const resolver = ( | ||
await contracts?.getPublicResolver(provider, resolverToUse) | ||
)?.connect(signer)! | ||
const hash = namehash(name) | ||
|
||
const call = generateSingleRecordCall(hash, resolver, type)(record) | ||
|
||
return { | ||
to: resolver.address, | ||
data: call, | ||
} | ||
} |
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
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