Skip to content

Commit

Permalink
Merge pull request #129 from ensdomains/fix/max-date-int
Browse files Browse the repository at this point in the history
fix: correct max date int in getWrapperData
  • Loading branch information
LeonmanRolls authored Apr 5, 2023
2 parents 427419c + 216d1f1 commit bf00408
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
13 changes: 12 additions & 1 deletion packages/ensjs/deploy/00_register_wrapped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BigNumber } from '@ethersproject/bignumber'
import { ethers } from 'hardhat'
import { DeployFunction } from 'hardhat-deploy/types'
import { HardhatRuntimeEnvironment } from 'hardhat/types'
import { MAX_DATE_INT } from '../src/utils/consts'
import { encodeFuses } from '../src/utils/fuses'
import { namehash } from '../src/utils/normalise'

Expand All @@ -19,7 +20,7 @@ export const names: {
fuses?: number
expiry?: number
}[]
duration?: number
duration?: number | BigNumber
}[] = [
{
label: 'wrapped',
Expand All @@ -42,6 +43,16 @@ export const names: {
subnames: [{ label: 'test', namedOwner: 'owner2' }],
duration: 2419200,
},
{
label: 'wrapped-big-duration',
namedOwner: 'owner3',
duration: Math.floor((MAX_DATE_INT - Date.now()) / 1000),
},
{
label: 'wrapped-max-duration',
namedOwner: 'owner3',
duration: BigNumber.from('18446744073709'),
},
{
label: 'wrapped-with-expiring-subnames',
namedOwner: 'owner',
Expand Down
1 change: 1 addition & 0 deletions packages/ensjs/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const config: HardhatUserConfig = {
default: 1,
},
owner2: 2,
owner3: 3,
},
external: {
contracts: [
Expand Down
18 changes: 18 additions & 0 deletions packages/ensjs/src/functions/getWrapperData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,22 @@ describe('getWrapperData', () => {
expect(Number.isNaN(result.expiryDate?.getTime())).toBe(false)
}
})
it('should return correct expiry for large expiry', async () => {
const result = await ensInstance.getWrapperData('wrapped-big-duration.eth')
expect(result).toBeTruthy()
if (result) {
expect(result.expiryDate).toBeInstanceOf(Date)
expect(result.expiryDate!.getFullYear()).toBe(275760)
expect(Number.isNaN(result.expiryDate?.getTime())).toBe(false)
}
})
it('should return correct max expiry for expiry larger than maximum for date', async () => {
const result = await ensInstance.getWrapperData('wrapped-max-duration.eth')
expect(result).toBeTruthy()
if (result) {
expect(result.expiryDate).toBeInstanceOf(Date)
expect(result.expiryDate!.getFullYear()).toBe(275760)
expect(Number.isNaN(result.expiryDate?.getTime())).toBe(false)
}
})
})
15 changes: 11 additions & 4 deletions packages/ensjs/src/functions/getWrapperData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BigNumber } from '@ethersproject/bignumber'
import { ENSArgs } from '../index'
import { MAX_DATE_INT } from '../utils/consts'
import { decodeFuses } from '../utils/fuses'
import { namehash } from '../utils/normalise'

Expand All @@ -21,10 +22,16 @@ const decode = async ({ contracts }: ENSArgs<'contracts'>, data: string) => {

const fuseObj = decodeFuses(fuses)

const expiryDate =
expiry.gt(0) && expiry.lt(BigNumber.from(2).pow(32))
? new Date(expiry.mul(1000).toNumber())
: undefined
let expiryDate: Date | undefined

if (expiry.gt(0)) {
const msBigNumber = expiry.mul(1000)
if (msBigNumber.gte(MAX_DATE_INT)) {
expiryDate = new Date(MAX_DATE_INT)
} else {
expiryDate = new Date(msBigNumber.toNumber())
}
}

return {
...fuseObj,
Expand Down
1 change: 1 addition & 0 deletions packages/ensjs/src/utils/consts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const EMPTY_ADDRESS = '0x0000000000000000000000000000000000000000'
export const MAX_INT_64 = BigInt('18446744073709551615')
export const MAX_DATE_INT = 8640000000000000
export const MINIMUM_DOT_ETH_CHARS = 3

0 comments on commit bf00408

Please sign in to comment.