-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathlegacyNameGenerator.ts
More file actions
130 lines (110 loc) · 4.08 KB
/
legacyNameGenerator.ts
File metadata and controls
130 lines (110 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* NOTE: This generator uses register/commit methods to generate names. It is not used in a test yet so it might
* have some bugs.
*/
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-await-in-loop */
import { encodeFunctionData, Hash } from 'viem'
import { transferName } from '@ensdomains/ensjs/wallet'
import { Accounts, User } from '../../accounts.js'
import {
publicClient,
waitForTransaction,
walletClient,
} from '../../contracts/utils/addTestContracts.js'
import { legacyEthRegistrarControllerAbi } from '../constants/abis.js'
import { Name } from '../index'
import { getLegacyRentPrice } from '../utils/getLegacyRentPrice.js'
import { generateLegacySubname, LegacySubname } from './generateLegacySubname.js'
const DEFAULT_DURATION = 31536000
export type LegacyName = {
label: string
type: 'legacy-register'
owner?: User
manager?: User
duration?: number
secret?: Hash
subnames?: Omit<LegacySubname, 'name' | 'nameOwner'>[]
}
type Dependencies = {
accounts: Accounts
}
export const isLegacyName = (name: Name): name is LegacyName => name.type === 'legacy-register'
const nameWithDefaults = (name: LegacyName) => ({
...name,
duration: name.duration ?? DEFAULT_DURATION,
secret: name.secret ?? '0x0000000000000000000000000000000000000000000000000000000000000000',
owner: name.owner ?? 'user',
manager: name.manager ?? name.owner ?? 'user',
})
export const makeLegacyNameGenerator = ({ accounts }: Dependencies) => ({
commit: async (nameConfig: LegacyName) => {
const { label, owner, secret } = nameWithDefaults(nameConfig)
const name = `${label}.eth`
const ownerAccount = accounts.getAccountForUser(owner)
console.log('make commit:', name)
const { data: commitment } = await publicClient.call({
to: walletClient.chain.contracts.legacyRegistrarController.address,
data: encodeFunctionData({
functionName: 'makeCommitment',
abi: legacyEthRegistrarControllerAbi,
args: [label, ownerAccount.address, secret],
}),
})
const preparedTransaction = await walletClient.prepareTransactionRequest({
to: walletClient.chain.contracts.legacyRegistrarController.address,
data: encodeFunctionData({
functionName: 'commit',
abi: legacyEthRegistrarControllerAbi,
args: [commitment],
}),
gas: 1000000n,
account: ownerAccount,
nonceManager: ownerAccount.nonceManager,
})
return walletClient.sendTransaction(preparedTransaction)
},
register: async (nameConfig: LegacyName) => {
const { label, owner, duration, secret } = nameWithDefaults(nameConfig)
const ownerAccount = accounts.getAccountForUser(owner)
const price = await getLegacyRentPrice({ label, duration })
const preparedTransaction = await walletClient.prepareTransactionRequest({
to: walletClient.chain.contracts.legacyRegistrarController.address,
data: encodeFunctionData({
functionName: 'register',
abi: legacyEthRegistrarControllerAbi,
args: [label, ownerAccount.address, duration, secret],
}),
value: price,
gas: 1000000n,
account: ownerAccount,
nonceManager: ownerAccount.nonceManager,
})
return walletClient.sendTransaction(preparedTransaction)
},
configure: async (nameConfig: LegacyName) => {
const { label, owner, manager, subnames = [], secret } = nameWithDefaults(nameConfig)
const name = `${label}.eth`
// Create subnames
for (const subname of subnames) {
await generateLegacySubname({ accounts })({
...subname,
name: `${label}.eth`,
nameOwner: owner,
})
}
if (!!manager && manager !== owner) {
console.log('setting manager:', name, manager)
const ownerAccount = accounts.getAccountForUser(owner)
const tx = await transferName(walletClient, {
name,
newOwnerAddress: accounts.getAddress(manager) as `0x${string}`,
contract: 'registry',
account: ownerAccount,
// @ts-expect-error
nonceManager: ownerAccount.nonceManager,
})
await waitForTransaction(tx)
}
},
})