Skip to content

Commit

Permalink
refactor and split out legacyregistration and legacyregistraionwithco…
Browse files Browse the repository at this point in the history
…nfig functions
  • Loading branch information
storywithoutend committed Jan 10, 2025
1 parent f530c91 commit 984d63a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 53 deletions.
3 changes: 2 additions & 1 deletion packages/ensjs/src/functions/wallet/legacyRegisterName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
makeLegacyRegistrationTuple,
type LegacyRegistrationParameters,
isLegacyRegistrationWithConfig,
makeLegacyRegistrationWithConfigTuple,
} from '../../utils/legacyRegisterHelpers.js'
import {
legacyEthRegistrarControllerRegisterSnippet,
Expand Down Expand Up @@ -64,7 +65,7 @@ export const makeFunctionData = <
? encodeFunctionData({
abi: legacyEthRegistrarControllerRegisterWithConfigSnippet,
functionName: 'registerWithConfig',
args: makeLegacyRegistrationTuple(args),
args: makeLegacyRegistrationWithConfigTuple(args),
})
: encodeFunctionData({
abi: legacyEthRegistrarControllerRegisterSnippet,
Expand Down
2 changes: 1 addition & 1 deletion packages/ensjs/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export {
makeLegacyRegistrationTuple,
type LegacyCommitmentTuple,
type LegacyRegistrationParameters,
type LegacyRegistrationTuple,
type LegacyRegistrationBaseTuple as LegacyRegistrationTuple,
} from './legacyRegisterHelpers.js'
export { makeSafeSecondsDate } from './makeSafeSecondsDate.js'
export {
Expand Down
115 changes: 64 additions & 51 deletions packages/ensjs/src/utils/legacyRegisterHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { EMPTY_ADDRESS } from './consts.js'
import { LegacyRegistrationInvalidConfigError } from '../errors/register.js'

export type LegacyRegistrationBaseParameters = {
export type LegacyRegistrationParameters = {
/** Name to register */
name: string
/** Address to set owner to */
Expand All @@ -24,15 +24,11 @@ export type LegacyRegistrationBaseParameters = {
}

export type LegacyRegistrationWithConfigParameters =
LegacyRegistrationBaseParameters & {
LegacyRegistrationParameters & {
resolverAddress: Address
address?: Address
}

export type LegacyRegistrationParameters =
| LegacyRegistrationBaseParameters
| LegacyRegistrationWithConfigParameters

export const isLegacyRegistrationWithConfig = (
params: LegacyRegistrationParameters,
): params is LegacyRegistrationWithConfigParameters => {
Expand All @@ -47,67 +43,79 @@ export const isLegacyRegistrationWithConfig = (
return resolverAddress !== EMPTY_ADDRESS || address !== EMPTY_ADDRESS
}

export type LegacyCommitmentTuple =
| [label: string, owner: Address, secret: Hex]
| [
label: string,
owner: Address,
resolverAddress: Address,
address: Address,
secret: Hex,
]

export type LegacyRegistrationTuple =
| [label: string, owner: Address, duration: bigint, secret: Hex]
| [
label: string,
owner: Address,
duration: bigint,
secret: Hex,
resolverAddress: Address,
address: Address,
]
export type LegacyCommitmentTuple = [label: string, owner: Address, secret: Hex]

export type LegacyCommitmentWithConfigTuple = [
label: string,
owner: Address,
resolverAddress: Address,
address: Address,
secret: Hex,
]

export type LegacyRegistrationTuple = [
label: string,
owner: Address,
duration: bigint,
secret: Hex,
]

export type LegacyRegistrationWithConfigTuple = [
label: string,
owner: Address,
duration: bigint,
secret: Hex,
resolverAddress: Address,
address: Address,
]

export const makeLegacyCommitmentTuple = (
params: LegacyRegistrationParameters,
): LegacyCommitmentTuple => {
const {
name,
owner,
secret,
resolverAddress = EMPTY_ADDRESS,
address = EMPTY_ADDRESS,
} = params as LegacyRegistrationWithConfigParameters

const { name, owner, secret } = params
const label = name.split('.')[0]

if (isLegacyRegistrationWithConfig(params)) {
return [label, owner, secret, resolverAddress, address]
}
return [label, owner, secret]
}

export const makeLegacyRegistrationTuple = (
params: LegacyRegistrationParameters,
): LegacyRegistrationTuple => {
export const makeLegacyCommitmentWithConfigTuple = (
params: LegacyRegistrationWithConfigParameters,
): LegacyCommitmentWithConfigTuple => {
const {
name,
owner,
secret,
duration,
resolverAddress = EMPTY_ADDRESS,
address = EMPTY_ADDRESS,
} = params as LegacyRegistrationWithConfigParameters
const label = name.split('.')[0]
if (isLegacyRegistrationWithConfig(params))
return [label, owner, BigInt(duration), secret, resolverAddress, address]
return [label, owner, BigInt(params.duration), secret]
return [label, owner, secret, resolverAddress, address]
}

export const makeLegacyRegistrationTuple = ({
name,
owner,
secret,
duration,
}: LegacyRegistrationParameters): LegacyRegistrationTuple => {
const label = name.split('.')[0]
return [label, owner, BigInt(duration), secret]
}

export const makeLegacyCommitmentFromTuple = ([
label,
...others
]: LegacyCommitmentTuple): Hex => {
export const makeLegacyRegistrationWithConfigTuple = ({
name,
owner,
secret,
duration,
resolverAddress,
address = EMPTY_ADDRESS,
}: LegacyRegistrationWithConfigParameters): LegacyRegistrationWithConfigTuple => {
const label = name.split('.')[0]
return [label, owner, BigInt(duration), secret, resolverAddress, address]
}

export const makeLegacyCommitmentFromTuple = ([label, ...others]:
| LegacyCommitmentTuple
| LegacyCommitmentWithConfigTuple): Hex => {
const labelHash = labelhash(label)
const params = [labelHash, ...others] as const

Expand All @@ -130,5 +138,10 @@ export const makeLegacyCommitmentFromTuple = ([
}

export const makeLegacyCommitment = (
params: LegacyRegistrationParameters,
): Hex => makeLegacyCommitmentFromTuple(makeLegacyCommitmentTuple(params))
params: LegacyRegistrationParameters | LegacyRegistrationWithConfigParameters,
): Hex => {
const touple = isLegacyRegistrationWithConfig(params)
? makeLegacyCommitmentWithConfigTuple(params)
: makeLegacyCommitmentTuple(params)
return makeLegacyCommitmentFromTuple(touple)
}

0 comments on commit 984d63a

Please sign in to comment.