Skip to content

Commit

Permalink
Merge pull request #126 from ensdomains/fix/allow-empty-address
Browse files Browse the repository at this point in the history
fix: allow empty address to be set as a record
  • Loading branch information
LeonmanRolls authored Apr 3, 2023
2 parents c28552a + 6548cf8 commit c5d2234
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
49 changes: 49 additions & 0 deletions packages/ensjs/src/functions/setRecord.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,55 @@ describe('setRecord', () => {
'0x42D63ae25990889E35F215bC95884039Ba354115'.toLowerCase(),
)
})
it('should allow an empty address record to be set', async () => {
const setRecord = await ensInstance.setRecord('test123.eth', {
type: 'addr',
record: {
key: 'BNB',
value: 'bnb1rrhqmj4fa28vlrnm89f6gjsgz3ya6h40ptckj0',
},
addressOrIndex: 1,
})
await setRecord.wait()

const universalResolver =
await ensInstance.contracts!.getUniversalResolver()!
const publicResolver = await ensInstance.contracts!.getPublicResolver()!
const getEncodedAddr = () =>
universalResolver['resolve(bytes,bytes)'](
hexEncodeName('test123.eth'),
publicResolver.interface.encodeFunctionData('addr(bytes32,uint256)', [
namehash('test123.eth'),
'714',
]),
)
const encodedAddr = await getEncodedAddr()
const [resultAddr] = publicResolver.interface.decodeFunctionResult(
'addr(bytes32,uint256)',
encodedAddr[0],
)
// record as hex
expect(resultAddr).toBe('0x18ee0dcaa9ea8ecf8e7b3953a44a081449dd5eaf')

const tx = await ensInstance.setRecord('test123.eth', {
type: 'addr',
record: {
key: 'BNB',
value: '',
},
addressOrIndex: 1,
})
expect(tx).toBeTruthy()
await tx.wait()

const encodedEmptyAddr = await getEncodedAddr()
const [resultEmptyAddr] = publicResolver.interface.decodeFunctionResult(
'addr(bytes32,uint256)',
encodedEmptyAddr[0],
)
// record as hex
expect(resultEmptyAddr).toBe('0x')
})
it('should allow a contenthash record set', async () => {
const tx = await ensInstance.setRecord('test123.eth', {
type: 'contentHash',
Expand Down
19 changes: 19 additions & 0 deletions packages/ensjs/src/utils/recordHelpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { PublicResolver__factory } from '../generated'
import { namehash } from './normalise'
import { generateSetAddr } from './recordHelpers'

describe('generateSetAddr()', () => {
it('should allow empty string as address', () => {
expect(() =>
generateSetAddr(
namehash('test'),
'BNB',
'',
PublicResolver__factory.connect(
'0x0000000000000000000000000000000000000000',
undefined as any,
),
),
).not.toThrowError()
})
})
3 changes: 2 additions & 1 deletion packages/ensjs/src/utils/recordHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export const generateSetAddr = (
coinTypeInstance = formatsByName[coinType.toUpperCase()]
}
const inputCoinType = coinTypeInstance.coinType
const encodedAddress = coinTypeInstance.decoder(address)
const encodedAddress =
address !== '' ? coinTypeInstance.decoder(address) : '0x'
return resolver?.interface.encodeFunctionData(
'setAddr(bytes32,uint256,bytes)',
[namehash, inputCoinType, encodedAddress],
Expand Down

0 comments on commit c5d2234

Please sign in to comment.