Skip to content

Commit 984d63a

Browse files
refactor and split out legacyregistration and legacyregistraionwithconfig functions
1 parent f530c91 commit 984d63a

File tree

3 files changed

+67
-53
lines changed

3 files changed

+67
-53
lines changed

packages/ensjs/src/functions/wallet/legacyRegisterName.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
makeLegacyRegistrationTuple,
2020
type LegacyRegistrationParameters,
2121
isLegacyRegistrationWithConfig,
22+
makeLegacyRegistrationWithConfigTuple,
2223
} from '../../utils/legacyRegisterHelpers.js'
2324
import {
2425
legacyEthRegistrarControllerRegisterSnippet,
@@ -64,7 +65,7 @@ export const makeFunctionData = <
6465
? encodeFunctionData({
6566
abi: legacyEthRegistrarControllerRegisterWithConfigSnippet,
6667
functionName: 'registerWithConfig',
67-
args: makeLegacyRegistrationTuple(args),
68+
args: makeLegacyRegistrationWithConfigTuple(args),
6869
})
6970
: encodeFunctionData({
7071
abi: legacyEthRegistrarControllerRegisterSnippet,

packages/ensjs/src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export {
111111
makeLegacyRegistrationTuple,
112112
type LegacyCommitmentTuple,
113113
type LegacyRegistrationParameters,
114-
type LegacyRegistrationTuple,
114+
type LegacyRegistrationBaseTuple as LegacyRegistrationTuple,
115115
} from './legacyRegisterHelpers.js'
116116
export { makeSafeSecondsDate } from './makeSafeSecondsDate.js'
117117
export {

packages/ensjs/src/utils/legacyRegisterHelpers.ts

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { EMPTY_ADDRESS } from './consts.js'
99
import { LegacyRegistrationInvalidConfigError } from '../errors/register.js'
1010

11-
export type LegacyRegistrationBaseParameters = {
11+
export type LegacyRegistrationParameters = {
1212
/** Name to register */
1313
name: string
1414
/** Address to set owner to */
@@ -24,15 +24,11 @@ export type LegacyRegistrationBaseParameters = {
2424
}
2525

2626
export type LegacyRegistrationWithConfigParameters =
27-
LegacyRegistrationBaseParameters & {
27+
LegacyRegistrationParameters & {
2828
resolverAddress: Address
2929
address?: Address
3030
}
3131

32-
export type LegacyRegistrationParameters =
33-
| LegacyRegistrationBaseParameters
34-
| LegacyRegistrationWithConfigParameters
35-
3632
export const isLegacyRegistrationWithConfig = (
3733
params: LegacyRegistrationParameters,
3834
): params is LegacyRegistrationWithConfigParameters => {
@@ -47,67 +43,79 @@ export const isLegacyRegistrationWithConfig = (
4743
return resolverAddress !== EMPTY_ADDRESS || address !== EMPTY_ADDRESS
4844
}
4945

50-
export type LegacyCommitmentTuple =
51-
| [label: string, owner: Address, secret: Hex]
52-
| [
53-
label: string,
54-
owner: Address,
55-
resolverAddress: Address,
56-
address: Address,
57-
secret: Hex,
58-
]
59-
60-
export type LegacyRegistrationTuple =
61-
| [label: string, owner: Address, duration: bigint, secret: Hex]
62-
| [
63-
label: string,
64-
owner: Address,
65-
duration: bigint,
66-
secret: Hex,
67-
resolverAddress: Address,
68-
address: Address,
69-
]
46+
export type LegacyCommitmentTuple = [label: string, owner: Address, secret: Hex]
47+
48+
export type LegacyCommitmentWithConfigTuple = [
49+
label: string,
50+
owner: Address,
51+
resolverAddress: Address,
52+
address: Address,
53+
secret: Hex,
54+
]
55+
56+
export type LegacyRegistrationTuple = [
57+
label: string,
58+
owner: Address,
59+
duration: bigint,
60+
secret: Hex,
61+
]
62+
63+
export type LegacyRegistrationWithConfigTuple = [
64+
label: string,
65+
owner: Address,
66+
duration: bigint,
67+
secret: Hex,
68+
resolverAddress: Address,
69+
address: Address,
70+
]
7071

7172
export const makeLegacyCommitmentTuple = (
7273
params: LegacyRegistrationParameters,
7374
): LegacyCommitmentTuple => {
74-
const {
75-
name,
76-
owner,
77-
secret,
78-
resolverAddress = EMPTY_ADDRESS,
79-
address = EMPTY_ADDRESS,
80-
} = params as LegacyRegistrationWithConfigParameters
81-
75+
const { name, owner, secret } = params
8276
const label = name.split('.')[0]
83-
84-
if (isLegacyRegistrationWithConfig(params)) {
85-
return [label, owner, secret, resolverAddress, address]
86-
}
8777
return [label, owner, secret]
8878
}
8979

90-
export const makeLegacyRegistrationTuple = (
91-
params: LegacyRegistrationParameters,
92-
): LegacyRegistrationTuple => {
80+
export const makeLegacyCommitmentWithConfigTuple = (
81+
params: LegacyRegistrationWithConfigParameters,
82+
): LegacyCommitmentWithConfigTuple => {
9383
const {
9484
name,
9585
owner,
9686
secret,
97-
duration,
9887
resolverAddress = EMPTY_ADDRESS,
9988
address = EMPTY_ADDRESS,
10089
} = params as LegacyRegistrationWithConfigParameters
10190
const label = name.split('.')[0]
102-
if (isLegacyRegistrationWithConfig(params))
103-
return [label, owner, BigInt(duration), secret, resolverAddress, address]
104-
return [label, owner, BigInt(params.duration), secret]
91+
return [label, owner, secret, resolverAddress, address]
92+
}
93+
94+
export const makeLegacyRegistrationTuple = ({
95+
name,
96+
owner,
97+
secret,
98+
duration,
99+
}: LegacyRegistrationParameters): LegacyRegistrationTuple => {
100+
const label = name.split('.')[0]
101+
return [label, owner, BigInt(duration), secret]
105102
}
106103

107-
export const makeLegacyCommitmentFromTuple = ([
108-
label,
109-
...others
110-
]: LegacyCommitmentTuple): Hex => {
104+
export const makeLegacyRegistrationWithConfigTuple = ({
105+
name,
106+
owner,
107+
secret,
108+
duration,
109+
resolverAddress,
110+
address = EMPTY_ADDRESS,
111+
}: LegacyRegistrationWithConfigParameters): LegacyRegistrationWithConfigTuple => {
112+
const label = name.split('.')[0]
113+
return [label, owner, BigInt(duration), secret, resolverAddress, address]
114+
}
115+
116+
export const makeLegacyCommitmentFromTuple = ([label, ...others]:
117+
| LegacyCommitmentTuple
118+
| LegacyCommitmentWithConfigTuple): Hex => {
111119
const labelHash = labelhash(label)
112120
const params = [labelHash, ...others] as const
113121

@@ -130,5 +138,10 @@ export const makeLegacyCommitmentFromTuple = ([
130138
}
131139

132140
export const makeLegacyCommitment = (
133-
params: LegacyRegistrationParameters,
134-
): Hex => makeLegacyCommitmentFromTuple(makeLegacyCommitmentTuple(params))
141+
params: LegacyRegistrationParameters | LegacyRegistrationWithConfigParameters,
142+
): Hex => {
143+
const touple = isLegacyRegistrationWithConfig(params)
144+
? makeLegacyCommitmentWithConfigTuple(params)
145+
: makeLegacyCommitmentTuple(params)
146+
return makeLegacyCommitmentFromTuple(touple)
147+
}

0 commit comments

Comments
 (0)