@monadns/sdk 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -17
- package/README.md +276 -35
- package/dist/index.cjs +225 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +265 -1
- package/dist/index.d.ts +265 -1
- package/dist/index.js +214 -1
- package/dist/index.js.map +1 -1
- package/dist/react.cjs +367 -19
- package/dist/react.cjs.map +1 -1
- package/dist/react.d.cts +440 -8
- package/dist/react.d.ts +440 -8
- package/dist/react.js +362 -18
- package/dist/react.js.map +1 -1
- package/package.json +10 -6
package/LICENSE
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
4
7
|
|
|
5
|
-
|
|
6
|
-
of this software
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
11
15
|
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
14
23
|
|
|
15
|
-
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @monadns/sdk
|
|
2
2
|
|
|
3
|
-
Resolve **.mon** names on the Monad network. The official SDK for the Monad Name Service.
|
|
3
|
+
Resolve and register **.mon** names on the Monad network. The official SDK for the [Monad Name Service](https://monadscan.com).
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -15,31 +15,45 @@ import { resolveName, lookupAddress } from '@monadns/sdk';
|
|
|
15
15
|
|
|
16
16
|
// Forward: name → address
|
|
17
17
|
const address = await resolveName('alice.mon');
|
|
18
|
-
// '
|
|
18
|
+
// '0xa05a8BF1eda5bbC2b3aCAF03D04f77bD7d66Cc47'
|
|
19
19
|
|
|
20
20
|
// Reverse: address → name
|
|
21
|
-
const name = await lookupAddress('
|
|
21
|
+
const name = await lookupAddress('0xa05a8BF1eda5bbC2b3aCAF03D04f77bD7d66Cc47');
|
|
22
22
|
// 'alice.mon'
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
## React Hooks
|
|
26
26
|
|
|
27
|
+
All hooks are compatible with Next.js App Router, Remix, and any React 17+ framework. The `'use client'` directive is included in the build.
|
|
28
|
+
|
|
27
29
|
```bash
|
|
28
30
|
npm install @monadns/sdk viem react
|
|
29
31
|
```
|
|
30
32
|
|
|
33
|
+
### Display .mon names
|
|
34
|
+
|
|
31
35
|
```tsx
|
|
32
|
-
import { useMNSDisplay
|
|
36
|
+
import { useMNSDisplay } from '@monadns/sdk/react';
|
|
33
37
|
|
|
34
|
-
// Display .mon name instead of address
|
|
35
38
|
function UserBadge({ address }: { address: string }) {
|
|
36
|
-
const { displayName, monName } = useMNSDisplay(address);
|
|
37
|
-
|
|
39
|
+
const { displayName, monName, loading } = useMNSDisplay(address);
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<span className={monName ? 'text-purple-400 font-bold' : 'text-gray-400'}>
|
|
43
|
+
{loading ? '...' : displayName}
|
|
44
|
+
</span>
|
|
45
|
+
);
|
|
38
46
|
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Resolve input (name or address)
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
import { useMNSResolve } from '@monadns/sdk/react';
|
|
39
53
|
|
|
40
|
-
// Input field that accepts names or addresses
|
|
41
54
|
function RecipientInput() {
|
|
42
|
-
const { resolve, address, loading, error } = useMNSResolve();
|
|
55
|
+
const { resolve, address, name, loading, error } = useMNSResolve();
|
|
56
|
+
|
|
43
57
|
return (
|
|
44
58
|
<div>
|
|
45
59
|
<input
|
|
@@ -47,8 +61,215 @@ function RecipientInput() {
|
|
|
47
61
|
onChange={(e) => resolve(e.target.value)}
|
|
48
62
|
/>
|
|
49
63
|
{loading && <span>Resolving...</span>}
|
|
50
|
-
{
|
|
51
|
-
{
|
|
64
|
+
{error && <span className="text-red-500">{error}</span>}
|
|
65
|
+
{address && <span className="text-green-500">→ {address}</span>}
|
|
66
|
+
</div>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Get avatar
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
import { useMNSAvatar } from '@monadns/sdk/react';
|
|
75
|
+
|
|
76
|
+
function Avatar({ name }: { name: string }) {
|
|
77
|
+
const { url, loading } = useMNSAvatar(name);
|
|
78
|
+
if (loading)
|
|
79
|
+
return <div className="animate-pulse w-10 h-10 rounded-full bg-gray-700" />;
|
|
80
|
+
return url ? <img src={url} className="w-10 h-10 rounded-full" /> : null;
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Get text records
|
|
85
|
+
|
|
86
|
+
```tsx
|
|
87
|
+
import { useMNSText } from '@monadns/sdk/react';
|
|
88
|
+
|
|
89
|
+
function Twitter({ name }: { name: string }) {
|
|
90
|
+
const { value: handle } = useMNSText(name, 'com.twitter');
|
|
91
|
+
return handle ? <a href={`https://x.com/${handle}`}>@{handle}</a> : null;
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Registration (with wagmi)
|
|
96
|
+
|
|
97
|
+
The SDK returns transaction parameters that work directly with wagmi's `useWriteContract`:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
npm install @monadns/sdk viem wagmi @tanstack/react-query
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Full Registration Page
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
import { useState } from 'react';
|
|
107
|
+
import {
|
|
108
|
+
useAccount,
|
|
109
|
+
useWriteContract,
|
|
110
|
+
useWaitForTransactionReceipt,
|
|
111
|
+
} from 'wagmi';
|
|
112
|
+
import { formatEther } from 'viem';
|
|
113
|
+
import { useRegistrationInfo, useMNSRegister } from '@monadns/sdk/react';
|
|
114
|
+
import { clearCache } from '@monadns/sdk';
|
|
115
|
+
|
|
116
|
+
function RegisterPage() {
|
|
117
|
+
const [label, setLabel] = useState('');
|
|
118
|
+
const { address } = useAccount();
|
|
119
|
+
const { info, loading: checking } = useRegistrationInfo(label, address);
|
|
120
|
+
const { prepare, loading: preparing, error } = useMNSRegister();
|
|
121
|
+
const { writeContract, data: hash } = useWriteContract();
|
|
122
|
+
const { isLoading: confirming, isSuccess } = useWaitForTransactionReceipt({
|
|
123
|
+
hash,
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const handleRegister = async () => {
|
|
127
|
+
const tx = await prepare(label, address!);
|
|
128
|
+
if (tx) writeContract(tx);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// Clear cache after successful registration so lookups reflect new state
|
|
132
|
+
if (isSuccess) clearCache();
|
|
133
|
+
|
|
134
|
+
return (
|
|
135
|
+
<div className="max-w-md mx-auto p-6">
|
|
136
|
+
<h1 className="text-2xl font-bold mb-4">Register a .mon name</h1>
|
|
137
|
+
|
|
138
|
+
<input
|
|
139
|
+
className="w-full p-3 border rounded-lg mb-4"
|
|
140
|
+
placeholder="Search for a name..."
|
|
141
|
+
value={label}
|
|
142
|
+
onChange={(e) => setLabel(e.target.value)}
|
|
143
|
+
/>
|
|
144
|
+
|
|
145
|
+
{checking && <p className="text-gray-400">Checking availability...</p>}
|
|
146
|
+
|
|
147
|
+
{info && !checking && (
|
|
148
|
+
<div className="space-y-2">
|
|
149
|
+
<p>{info.available ? '✅ Available!' : '❌ Already taken'}</p>
|
|
150
|
+
|
|
151
|
+
{info.available && (
|
|
152
|
+
<>
|
|
153
|
+
<p className="text-lg">
|
|
154
|
+
{info.isFreebie
|
|
155
|
+
? '🎉 FREE for 100 years!'
|
|
156
|
+
: `${formatEther(info.price)} MON / year`}
|
|
157
|
+
</p>
|
|
158
|
+
|
|
159
|
+
<button
|
|
160
|
+
className="w-full bg-purple-600 text-white p-3 rounded-lg disabled:opacity-50"
|
|
161
|
+
onClick={handleRegister}
|
|
162
|
+
disabled={preparing || confirming}
|
|
163
|
+
>
|
|
164
|
+
{confirming
|
|
165
|
+
? 'Confirming...'
|
|
166
|
+
: preparing
|
|
167
|
+
? 'Preparing...'
|
|
168
|
+
: `Register ${label}.mon`}
|
|
169
|
+
</button>
|
|
170
|
+
</>
|
|
171
|
+
)}
|
|
172
|
+
</div>
|
|
173
|
+
)}
|
|
174
|
+
|
|
175
|
+
{error && <p className="text-red-500 mt-2">❌ {error}</p>}
|
|
176
|
+
{isSuccess && (
|
|
177
|
+
<p className="text-green-500 mt-4">🎉 {label}.mon is yours!</p>
|
|
178
|
+
)}
|
|
179
|
+
</div>
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Profile Editor (Avatar, Twitter, Bio)
|
|
185
|
+
|
|
186
|
+
```tsx
|
|
187
|
+
import { useState } from 'react';
|
|
188
|
+
import { useWriteContract, useWaitForTransactionReceipt } from 'wagmi';
|
|
189
|
+
import { useMNSText, useMNSTextRecords } from '@monadns/sdk/react';
|
|
190
|
+
|
|
191
|
+
function ProfileEditor({ name }: { name: string }) {
|
|
192
|
+
const { value: currentAvatar } = useMNSText(name, 'avatar');
|
|
193
|
+
const { value: currentTwitter } = useMNSText(name, 'com.twitter');
|
|
194
|
+
const { value: currentBio } = useMNSText(name, 'description');
|
|
195
|
+
|
|
196
|
+
const { setAvatar, setTwitter, setDescription } = useMNSTextRecords();
|
|
197
|
+
const { writeContract, data: hash } = useWriteContract();
|
|
198
|
+
const { isLoading: saving, isSuccess } = useWaitForTransactionReceipt({
|
|
199
|
+
hash,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const [avatar, setAvatarUrl] = useState('');
|
|
203
|
+
const [twitter, setTwitterHandle] = useState('');
|
|
204
|
+
const [bio, setBio] = useState('');
|
|
205
|
+
|
|
206
|
+
return (
|
|
207
|
+
<div className="max-w-md mx-auto p-6 space-y-4">
|
|
208
|
+
<h2 className="text-xl font-bold">Edit {name} Profile</h2>
|
|
209
|
+
|
|
210
|
+
{currentAvatar && (
|
|
211
|
+
<img src={currentAvatar} className="w-20 h-20 rounded-full" />
|
|
212
|
+
)}
|
|
213
|
+
|
|
214
|
+
<div>
|
|
215
|
+
<label className="block text-sm font-medium mb-1">Avatar URL</label>
|
|
216
|
+
<div className="flex gap-2">
|
|
217
|
+
<input
|
|
218
|
+
className="flex-1 p-2 border rounded"
|
|
219
|
+
placeholder={currentAvatar || 'https://example.com/avatar.png'}
|
|
220
|
+
value={avatar}
|
|
221
|
+
onChange={(e) => setAvatarUrl(e.target.value)}
|
|
222
|
+
/>
|
|
223
|
+
<button
|
|
224
|
+
className="px-4 py-2 bg-purple-600 text-white rounded disabled:opacity-50"
|
|
225
|
+
onClick={() => writeContract(setAvatar(name, avatar))}
|
|
226
|
+
disabled={saving || !avatar}
|
|
227
|
+
>
|
|
228
|
+
Save
|
|
229
|
+
</button>
|
|
230
|
+
</div>
|
|
231
|
+
</div>
|
|
232
|
+
|
|
233
|
+
<div>
|
|
234
|
+
<label className="block text-sm font-medium mb-1">Twitter</label>
|
|
235
|
+
<div className="flex gap-2">
|
|
236
|
+
<input
|
|
237
|
+
className="flex-1 p-2 border rounded"
|
|
238
|
+
placeholder={currentTwitter || '@handle'}
|
|
239
|
+
value={twitter}
|
|
240
|
+
onChange={(e) => setTwitterHandle(e.target.value)}
|
|
241
|
+
/>
|
|
242
|
+
<button
|
|
243
|
+
className="px-4 py-2 bg-purple-600 text-white rounded disabled:opacity-50"
|
|
244
|
+
onClick={() => writeContract(setTwitter(name, twitter))}
|
|
245
|
+
disabled={saving || !twitter}
|
|
246
|
+
>
|
|
247
|
+
Save
|
|
248
|
+
</button>
|
|
249
|
+
</div>
|
|
250
|
+
</div>
|
|
251
|
+
|
|
252
|
+
<div>
|
|
253
|
+
<label className="block text-sm font-medium mb-1">Bio</label>
|
|
254
|
+
<div className="flex gap-2">
|
|
255
|
+
<input
|
|
256
|
+
className="flex-1 p-2 border rounded"
|
|
257
|
+
placeholder={currentBio || 'Tell the world about yourself'}
|
|
258
|
+
value={bio}
|
|
259
|
+
onChange={(e) => setBio(e.target.value)}
|
|
260
|
+
/>
|
|
261
|
+
<button
|
|
262
|
+
className="px-4 py-2 bg-purple-600 text-white rounded disabled:opacity-50"
|
|
263
|
+
onClick={() => writeContract(setDescription(name, bio))}
|
|
264
|
+
disabled={saving || !bio}
|
|
265
|
+
>
|
|
266
|
+
Save
|
|
267
|
+
</button>
|
|
268
|
+
</div>
|
|
269
|
+
</div>
|
|
270
|
+
|
|
271
|
+
{saving && <p className="text-gray-400">⏳ Saving to blockchain...</p>}
|
|
272
|
+
{isSuccess && <p className="text-green-500">✅ Profile updated!</p>}
|
|
52
273
|
</div>
|
|
53
274
|
);
|
|
54
275
|
}
|
|
@@ -56,27 +277,37 @@ function RecipientInput() {
|
|
|
56
277
|
|
|
57
278
|
## API Reference
|
|
58
279
|
|
|
59
|
-
### Core Functions
|
|
280
|
+
### Core Functions (`@monadns/sdk`)
|
|
60
281
|
|
|
61
|
-
| Function
|
|
62
|
-
|
|
|
63
|
-
| `resolveName(name)`
|
|
64
|
-
| `lookupAddress(address)`
|
|
65
|
-
| `getDisplayName(address)`
|
|
66
|
-
| `resolveInput(input)`
|
|
67
|
-
| `getTextRecord(name, key)`
|
|
68
|
-
| `
|
|
69
|
-
| `
|
|
282
|
+
| Function | Description |
|
|
283
|
+
| ------------------------------------- | --------------------------------------------- |
|
|
284
|
+
| `resolveName(name)` | Forward resolve: `alice.mon` → `0x...` |
|
|
285
|
+
| `lookupAddress(address)` | Reverse resolve: `0x...` → `alice.mon` |
|
|
286
|
+
| `getDisplayName(address)` | Returns `.mon` name or truncated address |
|
|
287
|
+
| `resolveInput(input)` | Accepts name or address, returns address |
|
|
288
|
+
| `getTextRecord(name, key)` | Get text record (avatar, url, etc.) |
|
|
289
|
+
| `getAvatarUrl(name)` | Get resolved avatar URL (handles IPFS, NFTs) |
|
|
290
|
+
| `isRegistered(name)` | Check if a name is taken |
|
|
291
|
+
| `clearCache()` | Clear the resolution cache |
|
|
292
|
+
| `getRegistrationInfo(label, address)` | Get price, availability, freebie status |
|
|
293
|
+
| `getRegisterTx(label, address)` | Get tx params for registration |
|
|
294
|
+
| `getSetTextTx(name, key, value)` | Get tx params for setting a text record |
|
|
295
|
+
| `getSetAddrTx(name, address)` | Get tx params for updating the address record |
|
|
70
296
|
|
|
71
|
-
### React Hooks
|
|
297
|
+
### React Hooks (`@monadns/sdk/react`)
|
|
72
298
|
|
|
73
|
-
| Hook
|
|
74
|
-
|
|
|
75
|
-
| `useMNSName(address)`
|
|
76
|
-
| `useMNSAddress(name)`
|
|
77
|
-
| `useMNSDisplay(address)`
|
|
78
|
-
| `useMNSResolve()`
|
|
79
|
-
| `useMNSText(name, key)`
|
|
299
|
+
| Hook | Returns | Description |
|
|
300
|
+
| ------------------------------------- | -------------------------------------------- | ----------------------------- |
|
|
301
|
+
| `useMNSName(address)` | `{ name, loading, error }` | Reverse resolution |
|
|
302
|
+
| `useMNSAddress(name)` | `{ address, loading, error }` | Forward resolution |
|
|
303
|
+
| `useMNSDisplay(address)` | `{ displayName, monName, loading }` | Display-ready name |
|
|
304
|
+
| `useMNSResolve()` | `{ resolve, address, name, loading, error }` | On-demand resolver for inputs |
|
|
305
|
+
| `useMNSText(name, key)` | `{ value, loading, error }` | Read text records |
|
|
306
|
+
| `useMNSAvatar(name)` | `{ url, loading, error }` | Resolved avatar URL |
|
|
307
|
+
| `useRegistrationInfo(label, address)` | `{ info, loading, error }` | Pre-registration data |
|
|
308
|
+
| `useMNSRegister()` | `{ prepare, tx, loading, error }` | Prepare registration tx |
|
|
309
|
+
| `useMNSTextRecords()` | `{ setAvatar, setTwitter, ... }` | Prepare text record txs |
|
|
310
|
+
| `useMNSAddr()` | `{ setAddr }` | Prepare address update tx |
|
|
80
311
|
|
|
81
312
|
### Custom RPC
|
|
82
313
|
|
|
@@ -100,13 +331,23 @@ const name = await lookupAddress('0x...', { client });
|
|
|
100
331
|
|
|
101
332
|
## Contracts
|
|
102
333
|
|
|
103
|
-
| Contract
|
|
104
|
-
|
|
|
105
|
-
| Registry
|
|
106
|
-
| Public Resolver
|
|
334
|
+
| Contract | Address |
|
|
335
|
+
| ----------------- | -------------------------------------------- |
|
|
336
|
+
| Registry | `0x13f963486e741c8d3fcdc0a34a910920339a19ce` |
|
|
337
|
+
| Public Resolver | `0xa2eb94c88e55d944aced2066c5cec9b759801f97` |
|
|
338
|
+
| Controller | `0x98866c55adbc73ec6c272bb3604ddbdee3f282a8` |
|
|
339
|
+
| Base Registrar | `0x104a49db9318c284d462841b6940bdb46624ca55` |
|
|
340
|
+
| Reverse Registrar | `0x6fff58419ad964f2357590c3f1bae576ebe45f52` |
|
|
341
|
+
|
|
342
|
+
**Chain:** Monad Mainnet (Chain ID 143)
|
|
343
|
+
|
|
344
|
+
## Compatibility
|
|
107
345
|
|
|
108
|
-
|
|
346
|
+
- **Frameworks:** React 17+, Next.js (App Router & Pages), Remix, Vite, CRA
|
|
347
|
+
- **Wallet libraries:** wagmi, RainbowKit, ConnectKit, Privy, or any viem-based client
|
|
348
|
+
- **Non-React:** All core functions work without React in any JS/TS environment
|
|
349
|
+
- **Server-side:** Core functions can run in Node.js, edge functions, or API routes
|
|
109
350
|
|
|
110
351
|
## License
|
|
111
352
|
|
|
112
|
-
|
|
353
|
+
[Unlicense](https://unlicense.org) — public domain, no attribution required.
|
package/dist/index.cjs
CHANGED
|
@@ -20,16 +20,26 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
+
MNS_CONTROLLER: () => MNS_CONTROLLER,
|
|
23
24
|
MNS_PUBLIC_RESOLVER: () => MNS_PUBLIC_RESOLVER,
|
|
24
25
|
MNS_REGISTRY: () => MNS_REGISTRY,
|
|
26
|
+
TEXT_RECORD_KEYS: () => TEXT_RECORD_KEYS,
|
|
25
27
|
clearCache: () => clearCache,
|
|
28
|
+
controllerReadAbi: () => controllerReadAbi,
|
|
29
|
+
controllerWriteAbi: () => controllerWriteAbi,
|
|
30
|
+
getAvatarUrl: () => getAvatarUrl,
|
|
26
31
|
getDisplayName: () => getDisplayName,
|
|
27
32
|
getMNSClient: () => getMNSClient,
|
|
33
|
+
getRegisterTx: () => getRegisterTx,
|
|
34
|
+
getRegistrationInfo: () => getRegistrationInfo,
|
|
35
|
+
getSetAddrTx: () => getSetAddrTx,
|
|
36
|
+
getSetTextTx: () => getSetTextTx,
|
|
28
37
|
getTextRecord: () => getTextRecord,
|
|
29
38
|
isRegistered: () => isRegistered,
|
|
30
39
|
lookupAddress: () => lookupAddress,
|
|
31
40
|
resolveInput: () => resolveInput,
|
|
32
|
-
resolveName: () => resolveName
|
|
41
|
+
resolveName: () => resolveName,
|
|
42
|
+
resolverWriteAbi: () => resolverWriteAbi
|
|
33
43
|
});
|
|
34
44
|
module.exports = __toCommonJS(src_exports);
|
|
35
45
|
|
|
@@ -60,6 +70,7 @@ function getMNSClient(config) {
|
|
|
60
70
|
// src/constants.ts
|
|
61
71
|
var MNS_REGISTRY = "0x13f963486e741c8d3fcdc0a34a910920339a19ce";
|
|
62
72
|
var MNS_PUBLIC_RESOLVER = "0xa2eb94c88e55d944aced2066c5cec9b759801f97";
|
|
73
|
+
var MNS_CONTROLLER = "0x98866c55adbc73ec6c272bb3604ddbdee3f282a8";
|
|
63
74
|
var ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
64
75
|
var registryAbi = [
|
|
65
76
|
{
|
|
@@ -208,21 +219,233 @@ async function isRegistered(name, config) {
|
|
|
208
219
|
const addr = await resolveName(name, config);
|
|
209
220
|
return addr !== null;
|
|
210
221
|
}
|
|
222
|
+
async function getAvatarUrl(name, config) {
|
|
223
|
+
const raw = await getTextRecord(name, "avatar", config);
|
|
224
|
+
if (!raw) return null;
|
|
225
|
+
if (raw.startsWith("http://") || raw.startsWith("https://")) {
|
|
226
|
+
return raw;
|
|
227
|
+
}
|
|
228
|
+
if (raw.startsWith("ipfs://")) {
|
|
229
|
+
const hash = raw.slice(7);
|
|
230
|
+
return `https://ipfs.io/ipfs/${hash}`;
|
|
231
|
+
}
|
|
232
|
+
const nftMatch = raw.match(
|
|
233
|
+
/^eip155:(\d+)\/(erc721|erc1155):0x([a-fA-F0-9]{40})\/(\d+)$/
|
|
234
|
+
);
|
|
235
|
+
if (nftMatch) {
|
|
236
|
+
try {
|
|
237
|
+
const client = getMNSClient(config);
|
|
238
|
+
const contract = `0x${nftMatch[3]}`;
|
|
239
|
+
const tokenId = BigInt(nftMatch[4]);
|
|
240
|
+
const tokenUriAbi = [
|
|
241
|
+
{
|
|
242
|
+
name: nftMatch[2] === "erc721" ? "tokenURI" : "uri",
|
|
243
|
+
type: "function",
|
|
244
|
+
inputs: [{ name: "tokenId", type: "uint256" }],
|
|
245
|
+
outputs: [{ name: "", type: "string" }],
|
|
246
|
+
stateMutability: "view"
|
|
247
|
+
}
|
|
248
|
+
];
|
|
249
|
+
let tokenUri = await client.readContract({
|
|
250
|
+
address: contract,
|
|
251
|
+
abi: tokenUriAbi,
|
|
252
|
+
functionName: nftMatch[2] === "erc721" ? "tokenURI" : "uri",
|
|
253
|
+
args: [tokenId]
|
|
254
|
+
});
|
|
255
|
+
if (tokenUri.startsWith("ipfs://")) {
|
|
256
|
+
tokenUri = `https://ipfs.io/ipfs/${tokenUri.slice(7)}`;
|
|
257
|
+
}
|
|
258
|
+
const res = await fetch(tokenUri);
|
|
259
|
+
const metadata = await res.json();
|
|
260
|
+
let image = metadata.image || metadata.image_url || null;
|
|
261
|
+
if (image && image.startsWith("ipfs://")) {
|
|
262
|
+
image = `https://ipfs.io/ipfs/${image.slice(7)}`;
|
|
263
|
+
}
|
|
264
|
+
return image;
|
|
265
|
+
} catch {
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
if (raw.startsWith("data:")) {
|
|
270
|
+
return raw;
|
|
271
|
+
}
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
211
274
|
function clearCache() {
|
|
212
275
|
nameCache.clear();
|
|
213
276
|
addrCache.clear();
|
|
214
277
|
}
|
|
278
|
+
|
|
279
|
+
// src/write.ts
|
|
280
|
+
var import_viem3 = require("viem");
|
|
281
|
+
var controllerReadAbi = [
|
|
282
|
+
{
|
|
283
|
+
name: "available",
|
|
284
|
+
type: "function",
|
|
285
|
+
inputs: [{ name: "label", type: "string" }],
|
|
286
|
+
outputs: [{ name: "", type: "bool" }],
|
|
287
|
+
stateMutability: "view"
|
|
288
|
+
},
|
|
289
|
+
{
|
|
290
|
+
name: "rentPrice",
|
|
291
|
+
type: "function",
|
|
292
|
+
inputs: [
|
|
293
|
+
{ name: "label", type: "string" },
|
|
294
|
+
{ name: "duration", type: "uint256" }
|
|
295
|
+
],
|
|
296
|
+
outputs: [{ name: "", type: "uint256" }],
|
|
297
|
+
stateMutability: "view"
|
|
298
|
+
},
|
|
299
|
+
{
|
|
300
|
+
name: "hasClaimedFreebie",
|
|
301
|
+
type: "function",
|
|
302
|
+
inputs: [{ name: "user", type: "address" }],
|
|
303
|
+
outputs: [{ name: "", type: "bool" }],
|
|
304
|
+
stateMutability: "view"
|
|
305
|
+
}
|
|
306
|
+
];
|
|
307
|
+
var controllerWriteAbi = [
|
|
308
|
+
{
|
|
309
|
+
name: "register",
|
|
310
|
+
type: "function",
|
|
311
|
+
inputs: [
|
|
312
|
+
{ name: "label", type: "string" },
|
|
313
|
+
{ name: "owner", type: "address" },
|
|
314
|
+
{ name: "duration", type: "uint256" }
|
|
315
|
+
],
|
|
316
|
+
outputs: [{ name: "", type: "uint256" }],
|
|
317
|
+
stateMutability: "payable"
|
|
318
|
+
}
|
|
319
|
+
];
|
|
320
|
+
var resolverWriteAbi = [
|
|
321
|
+
{
|
|
322
|
+
name: "setText",
|
|
323
|
+
type: "function",
|
|
324
|
+
inputs: [
|
|
325
|
+
{ name: "node", type: "bytes32" },
|
|
326
|
+
{ name: "key", type: "string" },
|
|
327
|
+
{ name: "value", type: "string" }
|
|
328
|
+
],
|
|
329
|
+
outputs: [],
|
|
330
|
+
stateMutability: "nonpayable"
|
|
331
|
+
},
|
|
332
|
+
{
|
|
333
|
+
name: "setAddr",
|
|
334
|
+
type: "function",
|
|
335
|
+
inputs: [
|
|
336
|
+
{ name: "node", type: "bytes32" },
|
|
337
|
+
{ name: "a", type: "address" }
|
|
338
|
+
],
|
|
339
|
+
outputs: [],
|
|
340
|
+
stateMutability: "nonpayable"
|
|
341
|
+
}
|
|
342
|
+
];
|
|
343
|
+
var ONE_YEAR = BigInt(365 * 24 * 60 * 60);
|
|
344
|
+
async function getRegistrationInfo(label, userAddress, durationYears = 1, config) {
|
|
345
|
+
const client = getMNSClient(config);
|
|
346
|
+
const duration = ONE_YEAR * BigInt(durationYears);
|
|
347
|
+
const [available, price, hasClaimed, balance] = await Promise.all([
|
|
348
|
+
client.readContract({
|
|
349
|
+
address: MNS_CONTROLLER,
|
|
350
|
+
abi: controllerReadAbi,
|
|
351
|
+
functionName: "available",
|
|
352
|
+
args: [label]
|
|
353
|
+
}),
|
|
354
|
+
client.readContract({
|
|
355
|
+
address: MNS_CONTROLLER,
|
|
356
|
+
abi: controllerReadAbi,
|
|
357
|
+
functionName: "rentPrice",
|
|
358
|
+
args: [label, duration]
|
|
359
|
+
}),
|
|
360
|
+
client.readContract({
|
|
361
|
+
address: MNS_CONTROLLER,
|
|
362
|
+
abi: controllerReadAbi,
|
|
363
|
+
functionName: "hasClaimedFreebie",
|
|
364
|
+
args: [userAddress]
|
|
365
|
+
}),
|
|
366
|
+
client.getBalance({ address: userAddress })
|
|
367
|
+
]);
|
|
368
|
+
const isFreebie = balance >= 10n * 10n ** 18n && !hasClaimed;
|
|
369
|
+
return { available, price, isFreebie, hasClaimed, duration, label };
|
|
370
|
+
}
|
|
371
|
+
async function getRegisterTx(label, ownerAddress, durationYears = 1, config) {
|
|
372
|
+
const info = await getRegistrationInfo(
|
|
373
|
+
label,
|
|
374
|
+
ownerAddress,
|
|
375
|
+
durationYears,
|
|
376
|
+
config
|
|
377
|
+
);
|
|
378
|
+
if (!info.available) {
|
|
379
|
+
throw new Error(`${label}.mon is not available`);
|
|
380
|
+
}
|
|
381
|
+
return {
|
|
382
|
+
address: MNS_CONTROLLER,
|
|
383
|
+
abi: controllerWriteAbi,
|
|
384
|
+
functionName: "register",
|
|
385
|
+
args: [label, ownerAddress, info.duration],
|
|
386
|
+
value: info.isFreebie ? 0n : info.price,
|
|
387
|
+
gas: 3000000n,
|
|
388
|
+
_meta: {
|
|
389
|
+
label,
|
|
390
|
+
isFreebie: info.isFreebie,
|
|
391
|
+
price: info.price,
|
|
392
|
+
duration: info.duration
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
function getSetTextTx(name, key, value) {
|
|
397
|
+
const node = (0, import_viem3.namehash)(
|
|
398
|
+
name.endsWith(".mon") ? name : `${name}.mon`
|
|
399
|
+
);
|
|
400
|
+
return {
|
|
401
|
+
address: MNS_PUBLIC_RESOLVER,
|
|
402
|
+
abi: resolverWriteAbi,
|
|
403
|
+
functionName: "setText",
|
|
404
|
+
args: [node, key, value]
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
function getSetAddrTx(name, addr) {
|
|
408
|
+
const node = (0, import_viem3.namehash)(
|
|
409
|
+
name.endsWith(".mon") ? name : `${name}.mon`
|
|
410
|
+
);
|
|
411
|
+
return {
|
|
412
|
+
address: MNS_PUBLIC_RESOLVER,
|
|
413
|
+
abi: resolverWriteAbi,
|
|
414
|
+
functionName: "setAddr",
|
|
415
|
+
args: [node, addr]
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
var TEXT_RECORD_KEYS = {
|
|
419
|
+
avatar: "avatar",
|
|
420
|
+
url: "url",
|
|
421
|
+
description: "description",
|
|
422
|
+
email: "email",
|
|
423
|
+
twitter: "com.twitter",
|
|
424
|
+
github: "com.github",
|
|
425
|
+
discord: "com.discord",
|
|
426
|
+
telegram: "org.telegram"
|
|
427
|
+
};
|
|
215
428
|
// Annotate the CommonJS export names for ESM import in node:
|
|
216
429
|
0 && (module.exports = {
|
|
430
|
+
MNS_CONTROLLER,
|
|
217
431
|
MNS_PUBLIC_RESOLVER,
|
|
218
432
|
MNS_REGISTRY,
|
|
433
|
+
TEXT_RECORD_KEYS,
|
|
219
434
|
clearCache,
|
|
435
|
+
controllerReadAbi,
|
|
436
|
+
controllerWriteAbi,
|
|
437
|
+
getAvatarUrl,
|
|
220
438
|
getDisplayName,
|
|
221
439
|
getMNSClient,
|
|
440
|
+
getRegisterTx,
|
|
441
|
+
getRegistrationInfo,
|
|
442
|
+
getSetAddrTx,
|
|
443
|
+
getSetTextTx,
|
|
222
444
|
getTextRecord,
|
|
223
445
|
isRegistered,
|
|
224
446
|
lookupAddress,
|
|
225
447
|
resolveInput,
|
|
226
|
-
resolveName
|
|
448
|
+
resolveName,
|
|
449
|
+
resolverWriteAbi
|
|
227
450
|
});
|
|
228
451
|
//# sourceMappingURL=index.cjs.map
|