@parity/product-sdk 0.21.0 → 0.23.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/dist/address/index.d.ts +1 -0
- package/dist/chunk-BRZNHTLQ.js +89 -0
- package/dist/chunk-BRZNHTLQ.js.map +1 -0
- package/dist/{chunk-6BI66XUJ.js → chunk-DHRCWMY5.js} +9 -10
- package/dist/chunk-DHRCWMY5.js.map +1 -0
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +2 -2
- package/dist/dotns-bswWAmR2.d.ts +136 -0
- package/dist/identity/index.d.ts +495 -49
- package/dist/identity/index.js +796 -46
- package/dist/identity/index.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/individuality/index.d.ts +1 -0
- package/dist/individuality/index.js +3 -0
- package/dist/individuality/index.js.map +1 -0
- package/dist/react/index.d.ts +2 -2
- package/dist/react/index.js +2 -2
- package/dist/testing/index.d.ts +2 -2
- package/dist/{types-CztPAgZT.d.ts → types-B5Zx4RNq.d.ts} +1 -1
- package/package.json +17 -12
- package/src/core/createApp.ts +10 -8
- package/src/identity/dotns-abis.test.ts +43 -0
- package/src/identity/dotns-abis.ts +309 -0
- package/src/identity/dotns-addresses.test.ts +536 -0
- package/src/identity/dotns-addresses.ts +320 -0
- package/src/identity/dotns-errors.ts +36 -0
- package/src/identity/dotns-namehash.test.ts +155 -0
- package/src/identity/dotns-namehash.ts +149 -0
- package/src/identity/dotns-owner.test.ts +92 -0
- package/src/identity/dotns-registry.test.ts +1309 -0
- package/src/identity/dotns-registry.ts +868 -0
- package/src/identity/dotns.test.ts +128 -0
- package/src/identity/dotns.ts +91 -78
- package/src/identity/index.test.ts +43 -0
- package/src/identity/index.ts +42 -16
- package/src/identity/types.ts +29 -38
- package/src/individuality/contract.test.ts +326 -0
- package/src/individuality/index.ts +8 -0
- package/dist/chunk-6BI66XUJ.js.map +0 -1
- package/dist/chunk-BIKT6X54.js +0 -70
- package/dist/chunk-BIKT6X54.js.map +0 -1
- package/dist/dotns-hLSvmo6u.d.ts +0 -178
- package/src/identity/product-account.ts +0 -164
|
@@ -0,0 +1,868 @@
|
|
|
1
|
+
// Copyright 2026 Parity Technologies (UK) Ltd.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
/**
|
|
4
|
+
* DotNS registry reads and writes.
|
|
5
|
+
*
|
|
6
|
+
* DotNS is an ENS-style system on Asset Hub: a `DotnsRegistry` maps a node
|
|
7
|
+
* (namehash) to a resolver + owner, a `DotnsResolver` maps a node to an
|
|
8
|
+
* address, and a `DotnsReverseResolver` maps an account back to its name. All
|
|
9
|
+
* are Revive contracts, reached via `@parity/product-sdk-contracts`'
|
|
10
|
+
* `createContract(...).<method>.query(...)`. Contract source:
|
|
11
|
+
* https://github.com/paritytech/dotns
|
|
12
|
+
*
|
|
13
|
+
* **Names are rooted at the network's TLD, which is per deployment.** It is
|
|
14
|
+
* fixed when `DotnsProtocolRegistry.initialize` runs, has no setter, and is
|
|
15
|
+
* published as `tld()`. `.paseo` on Paseo Asset Hub Next V2, `.test` on
|
|
16
|
+
* Previewnet, and operators can choose others. Every entry point that hashes a
|
|
17
|
+
* name therefore asks the chain first, through {@link resolveTld}, and callers
|
|
18
|
+
* can supply `opts.tld` to skip the read. Hashing under the wrong root returns a
|
|
19
|
+
* node the chain never wrote to, so the failure looks exactly like an
|
|
20
|
+
* unregistered name — which is why nothing here assumes a root.
|
|
21
|
+
*
|
|
22
|
+
* **A node's resolver pointer is not necessarily a forward resolver.** The
|
|
23
|
+
* deployment has several — forward, reverse, content, PoP — and only the forward
|
|
24
|
+
* one answers `addressOf`. `resolveDotNs` therefore calls it only when the
|
|
25
|
+
* pointer *is* the forward resolver, and reports every other pointer as
|
|
26
|
+
* "registered, no forward record". The content resolver is the common case for a
|
|
27
|
+
* product name.
|
|
28
|
+
*
|
|
29
|
+
* A rule this module holds to throughout: **a failed read must never become a
|
|
30
|
+
* plausible value.** The one deliberate exception is documented on
|
|
31
|
+
* {@link resolveTld}, where a missing getter identifies a deployment whose TLD
|
|
32
|
+
* could only have been `.dot`.
|
|
33
|
+
*
|
|
34
|
+
* Reads (`resolveDotNs` / `reverseDotNs` / `isDotNsAvailable`) query chain.
|
|
35
|
+
* Writes return prepared calls the caller submits with their own signer:
|
|
36
|
+
* `setDotNsRecord` (the resolver pointer, when it needs moving, plus the
|
|
37
|
+
* record) and `prepareDotNsRegistration` (the commit call plus a thunk for the
|
|
38
|
+
* register call, which cannot be built until the commitment is on chain).
|
|
39
|
+
*
|
|
40
|
+
* Note: this deployment has no name-expiry concept (the registrar exposes no
|
|
41
|
+
* expiry getter), so `DotNsRecord.expiresAt` is always omitted.
|
|
42
|
+
*/
|
|
43
|
+
import { err, ok, type Result } from "@parity/result";
|
|
44
|
+
import type { AbiEntry, BatchableCall, ContractRuntime } from "@parity/product-sdk-contracts";
|
|
45
|
+
import { bytesToHex, randomBytes } from "@parity/product-sdk-crypto";
|
|
46
|
+
import { ss58ToH160 } from "@parity/product-sdk-address";
|
|
47
|
+
import { createLogger } from "@parity/product-sdk-logger";
|
|
48
|
+
import type { SS58String } from "polkadot-api";
|
|
49
|
+
import {
|
|
50
|
+
DOTNS_POP_RULES_ABI,
|
|
51
|
+
DOTNS_PROTOCOL_REGISTRY_ABI,
|
|
52
|
+
DOTNS_REGISTRAR_CONTROLLER_ABI,
|
|
53
|
+
DOTNS_REGISTRY_ABI,
|
|
54
|
+
DOTNS_RESOLVER_ABI,
|
|
55
|
+
DOTNS_RESOLVER_WRITE_ABI,
|
|
56
|
+
DOTNS_REVERSE_RESOLVER_ABI,
|
|
57
|
+
DOTNS_ADDRESSES,
|
|
58
|
+
POP_STATUS,
|
|
59
|
+
} from "./dotns-abis.js";
|
|
60
|
+
import { DotNsError } from "./dotns-errors.js";
|
|
61
|
+
import {
|
|
62
|
+
type DotNsGatewayQueryApi,
|
|
63
|
+
type RuntimeCache,
|
|
64
|
+
cachedPerRuntime,
|
|
65
|
+
contractOf,
|
|
66
|
+
isZero,
|
|
67
|
+
readOrigin,
|
|
68
|
+
resolveDotNsAddresses,
|
|
69
|
+
sameAddress,
|
|
70
|
+
} from "./dotns-addresses.js";
|
|
71
|
+
import { isResolvableDotNsName, isValidDotNsName, normalizeDotNsName } from "./dotns.js";
|
|
72
|
+
import {
|
|
73
|
+
DOT_TLD,
|
|
74
|
+
dotNsTld,
|
|
75
|
+
type DotNsTld,
|
|
76
|
+
isConsistentDotNsTld,
|
|
77
|
+
namehash,
|
|
78
|
+
stripSuffix,
|
|
79
|
+
} from "./dotns-namehash.js";
|
|
80
|
+
import type { DotNsRecord } from "./types.js";
|
|
81
|
+
|
|
82
|
+
const log = createLogger("identity:dotns");
|
|
83
|
+
|
|
84
|
+
type HexString = `0x${string}`;
|
|
85
|
+
|
|
86
|
+
/** Shared inputs for a DotNS registry call. */
|
|
87
|
+
export interface DotNsClientOptions {
|
|
88
|
+
/** A contract runtime for the chain hosting DotNS (Asset Hub). */
|
|
89
|
+
runtime: ContractRuntime;
|
|
90
|
+
/**
|
|
91
|
+
* SS58 account that will submit the prepared calls.
|
|
92
|
+
*
|
|
93
|
+
* Required by the write helpers, which dry-run against it: `setAddress` and
|
|
94
|
+
* `setResolver` are owner-gated, so a dry-run from anyone else reverts.
|
|
95
|
+
* Optional for reads, which fall back to the pallet-revive query account.
|
|
96
|
+
*
|
|
97
|
+
* Must be SS58, not the account's H160. pallet-revive derives `msg.sender`
|
|
98
|
+
* from it; convert with `h160ToSs58` from `@parity/product-sdk-address`.
|
|
99
|
+
*/
|
|
100
|
+
origin?: SS58String;
|
|
101
|
+
/** `DotnsRegistry` address. Defaults to the deployed set, which is the same on every network. */
|
|
102
|
+
registryAddress?: HexString;
|
|
103
|
+
/** `DotnsReverseResolver` address. Defaults to the deployed set. */
|
|
104
|
+
reverseResolverAddress?: HexString;
|
|
105
|
+
/** `DotnsResolver` address (writes: setDotNsRecord). Defaults to Paseo AH. */
|
|
106
|
+
resolverAddress?: HexString;
|
|
107
|
+
/** `DotnsRegistrarController` address (registration). Defaults to Paseo AH. */
|
|
108
|
+
registrarControllerAddress?: HexString;
|
|
109
|
+
/** `PopRules` address (registration price). Defaults to Paseo AH. */
|
|
110
|
+
popRulesAddress?: HexString;
|
|
111
|
+
/**
|
|
112
|
+
* `DotnsProtocolRegistry` address, the contract holding this network's TLD.
|
|
113
|
+
* Defaults to Paseo AH — and the address is the same on every network, since
|
|
114
|
+
* the whole set is CREATE3-deterministic.
|
|
115
|
+
*/
|
|
116
|
+
protocolRegistryAddress?: HexString;
|
|
117
|
+
/**
|
|
118
|
+
* Where the addresses come from. `"pinned"` (the default) uses the
|
|
119
|
+
* {@link DOTNS_ADDRESSES} table; `"discovered"` walks the gateway on the
|
|
120
|
+
* chain `runtime` points at, so nothing is trusted from this bundle.
|
|
121
|
+
*
|
|
122
|
+
* Pinned is the default because the table is currently correct and a walk
|
|
123
|
+
* costs four round trips on the first call. Discovery is the honest choice
|
|
124
|
+
* once a deployment has moved, and its trust root is
|
|
125
|
+
* `DotnsGateway.DispatcherAddress` — a governance-set value on the chain the
|
|
126
|
+
* caller already trusts for every name read. Per-field address overrides win
|
|
127
|
+
* over either source.
|
|
128
|
+
*
|
|
129
|
+
* Discovery also selects the address `setDotNsRecord` and
|
|
130
|
+
* `prepareDotNsRegistration` build calls against, so it decides where a
|
|
131
|
+
* signed transaction goes, not only where a read comes from. A pinned
|
|
132
|
+
* address constrains that destination even against a hostile RPC; a
|
|
133
|
+
* discovered one does not. Use {@link verifyDotNsAddresses} at startup if
|
|
134
|
+
* that matters.
|
|
135
|
+
*/
|
|
136
|
+
addressSource?: "pinned" | "discovered";
|
|
137
|
+
/**
|
|
138
|
+
* Typed API for the chain hosting the `DotnsGateway` pallet, needed only by
|
|
139
|
+
* `"discovered"`.
|
|
140
|
+
*
|
|
141
|
+
* Optional because `runtime.api` is usually the same chain and is used when
|
|
142
|
+
* it carries the pallet. `ContractRuntime` does not declare that storage, so
|
|
143
|
+
* it is probed rather than assumed; on a chain without the pallet, discovery
|
|
144
|
+
* fails with `AddressDiscovery` instead of throwing.
|
|
145
|
+
*/
|
|
146
|
+
gatewayApi?: DotNsGatewayQueryApi;
|
|
147
|
+
/**
|
|
148
|
+
* The deployment's TLD, when you already know it.
|
|
149
|
+
*
|
|
150
|
+
* Omit it and the client reads `protocolRegistry.tld()` once per runtime and
|
|
151
|
+
* caches it, which is the correct default: the TLD is per network and only
|
|
152
|
+
* the chain knows which one is in play. Supply it to skip the read for
|
|
153
|
+
* offline or test use, or to pin a deployment deliberately. Build it with
|
|
154
|
+
* `dotNsTld(".paseo")` rather than by hand — a suffix paired with the wrong
|
|
155
|
+
* node is rejected, because that pairing is the defect this option could
|
|
156
|
+
* otherwise reintroduce.
|
|
157
|
+
*/
|
|
158
|
+
tld?: DotNsTld;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Arguments for {@link prepareDotNsRegistration}. */
|
|
162
|
+
export interface RegisterDotNsArgs {
|
|
163
|
+
/** The name to register, e.g. `"alice.paseo"` — or the bare label `"alice"`. */
|
|
164
|
+
name: string;
|
|
165
|
+
/**
|
|
166
|
+
* The account that will own the registered name: an H160, `0x` and 20
|
|
167
|
+
* bytes. Not SS58 — DotNS is a set of Revive contracts, and the registrar
|
|
168
|
+
* takes the owner as an ABI `address`. Convert with `ss58ToH160` from
|
|
169
|
+
* `@parity/product-sdk-address` if you hold a Substrate-shaped address.
|
|
170
|
+
*
|
|
171
|
+
* Typed `string` rather than `` `0x${string}` `` so callers need no cast,
|
|
172
|
+
* which means the compiler will not catch an SS58 address here: it reaches
|
|
173
|
+
* the contract and reverts on chain.
|
|
174
|
+
*/
|
|
175
|
+
owner: string;
|
|
176
|
+
/** Reserved-name registration (default `false`). */
|
|
177
|
+
reserved?: boolean;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Arguments for {@link setDotNsRecord}. */
|
|
181
|
+
export interface SetRecordArgs {
|
|
182
|
+
/** The name whose resolver record is being set. */
|
|
183
|
+
name: string;
|
|
184
|
+
/**
|
|
185
|
+
* The address the name should resolve to: an H160, `0x` and 20 bytes. It is
|
|
186
|
+
* written straight into `resolver.setAddress` as an ABI `address`, so SS58
|
|
187
|
+
* is not accepted — convert with `ss58ToH160` from
|
|
188
|
+
* `@parity/product-sdk-address` first. Unenforced by the type, as with
|
|
189
|
+
* {@link RegisterDotNsArgs.owner}.
|
|
190
|
+
*/
|
|
191
|
+
address: string;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// ── The deployment's TLD ─────────────────────────────────────────────
|
|
195
|
+
//
|
|
196
|
+
// Every node hash is rooted at the network's TLD, which is fixed when
|
|
197
|
+
// `DotnsProtocolRegistry.initialize` runs and has no setter. Read it once per
|
|
198
|
+
// (runtime, protocol registry) and cache it: it cannot change under us.
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Cached TLD per runtime, then per protocol-registry address. Keyed on the
|
|
202
|
+
* address as well because it is overridable, and one runtime pointed at two
|
|
203
|
+
* registries has two TLDs.
|
|
204
|
+
*/
|
|
205
|
+
const tldCache: RuntimeCache<DotNsTld> = new WeakMap();
|
|
206
|
+
|
|
207
|
+
/** An empty-payload revert: the contract ran and refused, telling us nothing. */
|
|
208
|
+
function isEmptyRevert(value: unknown): boolean {
|
|
209
|
+
return (
|
|
210
|
+
typeof value === "object" &&
|
|
211
|
+
value !== null &&
|
|
212
|
+
(value as { type?: unknown }).type === "ContractRevertedWithPayload" &&
|
|
213
|
+
(value as { data?: unknown }).data === "0x"
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The TLD every name on this deployment is rooted at.
|
|
219
|
+
*
|
|
220
|
+
* Asks `protocolRegistry.tld()` and derives the node from the suffix, which is
|
|
221
|
+
* exactly what `initialize` did — so `tldNode()` is only ever a cross-check, and
|
|
222
|
+
* one read answers the question.
|
|
223
|
+
*
|
|
224
|
+
* Three outcomes, and the middle one is a deliberate exception to this module's
|
|
225
|
+
* rule that a failed read must never become a plausible value:
|
|
226
|
+
*
|
|
227
|
+
* - the getter answers with a usable suffix → that TLD;
|
|
228
|
+
* - the getter **reverts with an empty payload** → `DOT_TLD`, with a warning.
|
|
229
|
+
* That signature means the function does not exist, which dates the
|
|
230
|
+
* deployment before `dotns` `b4096968` ("make the TLD network-configurable
|
|
231
|
+
* via the protocol registry"). Before that commit the TLD was a
|
|
232
|
+
* compile-time constant and `.dot` was the only value it could hold, so
|
|
233
|
+
* this is the one case where the fallback is provably right rather than
|
|
234
|
+
* merely plausible. No live deployment is known to still take this
|
|
235
|
+
* branch: Previewnet was cited for it, but reports `.test`;
|
|
236
|
+
* - anything else → `err`. A dispatch failure, a revert carrying a reason, an
|
|
237
|
+
* undecodable answer, or a TLD we cannot use.
|
|
238
|
+
*
|
|
239
|
+
* That last case includes a getter that *succeeds* with an unusable value. The
|
|
240
|
+
* registry is `UUPSUpgradeable` with no reinitializer, so a pre-`b4096968` proxy
|
|
241
|
+
* upgraded to current contracts without a migration reports `_tld` as `""` and
|
|
242
|
+
* `_tldNode` as zero — both getters succeed and hand back the plain ENS root,
|
|
243
|
+
* which would silently reroot every name. {@link isConsistentDotNsTld} is what
|
|
244
|
+
* catches it.
|
|
245
|
+
*
|
|
246
|
+
* @internal Exposed for unit testing; consumers reach it through the entry points.
|
|
247
|
+
*/
|
|
248
|
+
export async function resolveTld(opts: DotNsClientOptions): Promise<Result<DotNsTld, DotNsError>> {
|
|
249
|
+
if (opts.tld) {
|
|
250
|
+
// A caller-supplied pair gets the same consistency check as a chain read:
|
|
251
|
+
// `.paseo` carrying `DOT_NODE` is the original bug via the override path.
|
|
252
|
+
return isConsistentDotNsTld(opts.tld)
|
|
253
|
+
? ok(opts.tld)
|
|
254
|
+
: err(
|
|
255
|
+
new DotNsError(
|
|
256
|
+
"InvalidTld",
|
|
257
|
+
`opts.tld is inconsistent: "${opts.tld.suffix}" does not hash to ${opts.tld.node}`,
|
|
258
|
+
),
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const addresses = await resolveDotNsAddresses(opts);
|
|
263
|
+
if (!addresses.ok) return addresses;
|
|
264
|
+
const address = addresses.value.protocolRegistry;
|
|
265
|
+
return cachedPerRuntime(tldCache, opts.runtime, address, () => readTld(opts, address));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async function readTld(
|
|
269
|
+
opts: DotNsClientOptions,
|
|
270
|
+
address: HexString,
|
|
271
|
+
): Promise<Result<DotNsTld, DotNsError>> {
|
|
272
|
+
try {
|
|
273
|
+
const registry = contractOf(opts.runtime, address, DOTNS_PROTOCOL_REGISTRY_ABI);
|
|
274
|
+
const origin = readOrigin(opts);
|
|
275
|
+
const suffixRes = await registry.tld.query({ origin });
|
|
276
|
+
|
|
277
|
+
if (!suffixRes.success) {
|
|
278
|
+
if (isEmptyRevert(suffixRes.value)) {
|
|
279
|
+
log.warn(
|
|
280
|
+
"DotNS protocol registry has no tld() getter; treating this as a " +
|
|
281
|
+
"pre-b4096968 deployment, whose TLD was the compile-time .dot",
|
|
282
|
+
{ protocolRegistry: address },
|
|
283
|
+
);
|
|
284
|
+
return ok(DOT_TLD);
|
|
285
|
+
}
|
|
286
|
+
return err(
|
|
287
|
+
new DotNsError("RegistryCall", "protocolRegistry.tld call failed", {
|
|
288
|
+
cause: suffixRes.value,
|
|
289
|
+
}),
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const suffix = suffixRes.value as string;
|
|
294
|
+
const tld = dotNsTld(suffix);
|
|
295
|
+
if (!isConsistentDotNsTld(tld)) {
|
|
296
|
+
return err(
|
|
297
|
+
new DotNsError(
|
|
298
|
+
"InvalidTld",
|
|
299
|
+
`protocolRegistry.tld returned an unusable TLD: ${JSON.stringify(suffix)}`,
|
|
300
|
+
),
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// Cross-check, not the source. A disagreement means the deployment's
|
|
305
|
+
// stored node was not derived from its stored label, which no
|
|
306
|
+
// `initialize` can produce — so trust neither value.
|
|
307
|
+
//
|
|
308
|
+
// Only a well-formed node counts as a disagreement. A revert (the getter
|
|
309
|
+
// is absent) or an unreadable answer means the cross-check did not run,
|
|
310
|
+
// which is not evidence against a suffix that already derived cleanly.
|
|
311
|
+
const nodeRes = await registry.tldNode.query({ origin });
|
|
312
|
+
if (nodeRes.success && isNode(nodeRes.value) && !sameNode(nodeRes.value, tld.node)) {
|
|
313
|
+
return err(
|
|
314
|
+
new DotNsError(
|
|
315
|
+
"InvalidTld",
|
|
316
|
+
`protocolRegistry disagrees with itself: tld() ${suffix} derives ` +
|
|
317
|
+
`${tld.node} but tldNode() reports ${String(nodeRes.value)}`,
|
|
318
|
+
),
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
log.debug("resolveTld", { protocolRegistry: address, suffix: tld.suffix, node: tld.node });
|
|
323
|
+
return ok(tld);
|
|
324
|
+
} catch (cause) {
|
|
325
|
+
return err(new DotNsError("RegistryCall", "DotNS TLD read failed", { cause }));
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Why a normalized name was refused.
|
|
331
|
+
*
|
|
332
|
+
* Two distinct answers, because they call for different things from the caller.
|
|
333
|
+
* `TldMismatch` means the name is well formed but belongs to another
|
|
334
|
+
* deployment — `alice.dot` handed to a `.paseo` network — which a product may
|
|
335
|
+
* want to offer to correct, and which the SDK must never correct silently:
|
|
336
|
+
* the two are separate registrations that may have different owners.
|
|
337
|
+
* `InvalidName` means the labels themselves are malformed, and no rewrite helps.
|
|
338
|
+
*/
|
|
339
|
+
function nameError(input: string, normalized: string, tld: DotNsTld): DotNsError {
|
|
340
|
+
if (!normalized.endsWith(tld.suffix)) {
|
|
341
|
+
return new DotNsError(
|
|
342
|
+
"TldMismatch",
|
|
343
|
+
`DotNS name "${input}" is not on this deployment, which uses "${tld.suffix}"`,
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
return new DotNsError("InvalidName", `Invalid DotNS name: "${input}"`);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** Whether a value is a readable 32-byte node, and so usable as a cross-check. */
|
|
350
|
+
function isNode(value: unknown): value is HexString {
|
|
351
|
+
return typeof value === "string" && /^0x[0-9a-fA-F]{64}$/.test(value);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** Case-insensitive 32-byte node compare, for the same reason as {@link sameAddress}. */
|
|
355
|
+
function sameNode(a: string, b: string): boolean {
|
|
356
|
+
return a.toLowerCase() === b.toLowerCase();
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Resolve a DotNS name. `expiresAt` is omitted (no on-chain expiry here).
|
|
361
|
+
*
|
|
362
|
+
* Three outcomes, because the registry's resolver pointer is configuration, not
|
|
363
|
+
* proof of existence:
|
|
364
|
+
*
|
|
365
|
+
* - `ok(null)` — unregistered (`registry.owner` is zero).
|
|
366
|
+
* - `ok({ name, owner })` — registered, no forward record yet.
|
|
367
|
+
* - `ok({ name, owner, address })` — resolves.
|
|
368
|
+
*/
|
|
369
|
+
export async function resolveDotNs(
|
|
370
|
+
name: string,
|
|
371
|
+
opts: DotNsClientOptions,
|
|
372
|
+
): Promise<Result<DotNsRecord | null, DotNsError>> {
|
|
373
|
+
// The TLD comes first because normalization and validation both need the
|
|
374
|
+
// suffix. One read per runtime, cached; an invalid name therefore costs a
|
|
375
|
+
// chain read on the first call and none after. Do not move validation back
|
|
376
|
+
// in front of this — it cannot know what a valid name looks like yet.
|
|
377
|
+
const tldRes = await resolveTld(opts);
|
|
378
|
+
if (!tldRes.ok) return tldRes;
|
|
379
|
+
const tld = tldRes.value;
|
|
380
|
+
const normalized = normalizeDotNsName(name, tld.suffix);
|
|
381
|
+
if (!isResolvableDotNsName(normalized, tld.suffix)) {
|
|
382
|
+
return err(nameError(name, normalized, tld));
|
|
383
|
+
}
|
|
384
|
+
const node = namehash(normalized, tld);
|
|
385
|
+
const addrs = await resolveDotNsAddresses(opts);
|
|
386
|
+
if (!addrs.ok) return addrs;
|
|
387
|
+
const registryAddr = addrs.value.registry;
|
|
388
|
+
const origin = readOrigin(opts);
|
|
389
|
+
log.debug("resolveDotNs", { name: normalized, node, registry: registryAddr });
|
|
390
|
+
|
|
391
|
+
try {
|
|
392
|
+
const registry = contractOf(opts.runtime, registryAddr as HexString, DOTNS_REGISTRY_ABI);
|
|
393
|
+
|
|
394
|
+
const [ownerRes, resolverRes] = await Promise.all([
|
|
395
|
+
registry.owner.query(node, { origin }),
|
|
396
|
+
registry.resolver.query(node, { origin }),
|
|
397
|
+
]);
|
|
398
|
+
if (!ownerRes.success) {
|
|
399
|
+
return err(
|
|
400
|
+
new DotNsError("RegistryCall", "registry.owner call failed", {
|
|
401
|
+
cause: ownerRes.value,
|
|
402
|
+
}),
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
if (!resolverRes.success) {
|
|
406
|
+
return err(
|
|
407
|
+
new DotNsError("RegistryCall", "registry.resolver call failed", {
|
|
408
|
+
cause: resolverRes.value,
|
|
409
|
+
}),
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// Existence check: the getter falls back to the registrar's ERC-721
|
|
414
|
+
// holder, and returns zero for a node with no record.
|
|
415
|
+
const owner = ownerRes.value as HexString;
|
|
416
|
+
if (isZero(owner)) return ok(null);
|
|
417
|
+
|
|
418
|
+
// Only the forward resolver answers `addressOf`, so that is the one
|
|
419
|
+
// pointer worth calling — an allowlist, not a denylist of the resolvers
|
|
420
|
+
// we happen to have met. A node can legitimately point at the reverse
|
|
421
|
+
// resolver (where registration parks it), the content resolver (a
|
|
422
|
+
// product name serving content, and the ordinary case in practice), the
|
|
423
|
+
// PoP resolver, or one deployed after this was written. None of them has
|
|
424
|
+
// an address record to give, and all of them revert if asked — which
|
|
425
|
+
// would surface as an infrastructure failure for a name that is simply
|
|
426
|
+
// registered without a forward record.
|
|
427
|
+
//
|
|
428
|
+
// `setDotNsRecord` already compares against the forward resolver this
|
|
429
|
+
// way; before this, the read and write paths disagreed about what a
|
|
430
|
+
// pointer meant.
|
|
431
|
+
const resolverAddr = resolverRes.value as HexString;
|
|
432
|
+
const forwardAddr = addrs.value.resolver;
|
|
433
|
+
if (!sameAddress(resolverAddr, forwardAddr)) {
|
|
434
|
+
return ok({ name: normalized, owner });
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
const resolver = contractOf(opts.runtime, resolverAddr as HexString, DOTNS_RESOLVER_ABI);
|
|
438
|
+
const addrRes = await resolver.addressOf.query(node, { origin });
|
|
439
|
+
if (!addrRes.success) {
|
|
440
|
+
return err(
|
|
441
|
+
new DotNsError("RegistryCall", "resolver.addressOf call failed", {
|
|
442
|
+
cause: addrRes.value,
|
|
443
|
+
}),
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
const address = addrRes.value as HexString;
|
|
447
|
+
// A forward resolver holding an empty record is still no forward record.
|
|
448
|
+
if (isZero(address)) return ok({ name: normalized, owner });
|
|
449
|
+
|
|
450
|
+
return ok({ address, name: normalized, owner });
|
|
451
|
+
} catch (cause) {
|
|
452
|
+
return err(
|
|
453
|
+
new DotNsError("RegistryCall", `DotNS resolve failed for "${normalized}"`, { cause }),
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Reverse-resolve an account to its primary DotNS name.
|
|
460
|
+
*
|
|
461
|
+
* Single call: `reverseResolver.nameOf(account)`. `ok(null)` when no primary
|
|
462
|
+
* name is set (empty string on-chain).
|
|
463
|
+
*/
|
|
464
|
+
export async function reverseDotNs(
|
|
465
|
+
address: string,
|
|
466
|
+
opts: DotNsClientOptions,
|
|
467
|
+
): Promise<Result<string | null, DotNsError>> {
|
|
468
|
+
const addrs = await resolveDotNsAddresses(opts);
|
|
469
|
+
if (!addrs.ok) return addrs;
|
|
470
|
+
const reverseAddr = addrs.value.reverseResolver;
|
|
471
|
+
log.debug("reverseDotNs", { address, reverseResolver: reverseAddr });
|
|
472
|
+
try {
|
|
473
|
+
const reverse = contractOf(
|
|
474
|
+
opts.runtime,
|
|
475
|
+
reverseAddr as HexString,
|
|
476
|
+
DOTNS_REVERSE_RESOLVER_ABI,
|
|
477
|
+
);
|
|
478
|
+
const res = await reverse.nameOf.query(address, { origin: readOrigin(opts) });
|
|
479
|
+
if (!res.success) {
|
|
480
|
+
return err(
|
|
481
|
+
new DotNsError("RegistryCall", "reverseResolver.nameOf call failed", {
|
|
482
|
+
cause: res.value,
|
|
483
|
+
}),
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
const name = res.value as string;
|
|
487
|
+
return ok(name && name.length > 0 ? name : null);
|
|
488
|
+
} catch (cause) {
|
|
489
|
+
return err(
|
|
490
|
+
new DotNsError("RegistryCall", `DotNS reverse failed for "${address}"`, { cause }),
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Whether a DotNS name is available to claim.
|
|
497
|
+
*
|
|
498
|
+
* Two reads, because "not minted" and "claimable" are different questions:
|
|
499
|
+
*
|
|
500
|
+
* - `registrarController.available(label)`, the predicate `register` enforces
|
|
501
|
+
* through `_requireAvailableLabel`. Deliberately not inferred from
|
|
502
|
+
* resolution: a registered name has no forward record until its owner sets
|
|
503
|
+
* one, so that would report owned names as free.
|
|
504
|
+
* - `PopRules.classifyName(label)`, which is pure and owner-independent.
|
|
505
|
+
* Labels of 5 base characters or fewer are reserved for governance and can
|
|
506
|
+
* never be claimed, though the registrar still reports them as unminted.
|
|
507
|
+
*
|
|
508
|
+
* `ok(true)` means the name can be registered by someone. It does not mean the
|
|
509
|
+
* caller can register it: labels of 6 to 8 characters additionally require a
|
|
510
|
+
* personhood tier, which depends on the owner and is checked by
|
|
511
|
+
* {@link prepareDotNsRegistration}.
|
|
512
|
+
*
|
|
513
|
+
* Validated first because both reads revert on labels we already reject.
|
|
514
|
+
*/
|
|
515
|
+
export async function isDotNsAvailable(
|
|
516
|
+
name: string,
|
|
517
|
+
opts: DotNsClientOptions,
|
|
518
|
+
): Promise<Result<boolean, DotNsError>> {
|
|
519
|
+
const tldRes = await resolveTld(opts);
|
|
520
|
+
if (!tldRes.ok) return tldRes;
|
|
521
|
+
const tld = tldRes.value;
|
|
522
|
+
const normalized = normalizeDotNsName(name, tld.suffix);
|
|
523
|
+
if (!isValidDotNsName(normalized, tld.suffix)) {
|
|
524
|
+
return err(nameError(name, normalized, tld));
|
|
525
|
+
}
|
|
526
|
+
// The registrar takes the bare label, without the TLD suffix.
|
|
527
|
+
const label = stripSuffix(normalized, tld.suffix);
|
|
528
|
+
const addrs = await resolveDotNsAddresses(opts);
|
|
529
|
+
if (!addrs.ok) return addrs;
|
|
530
|
+
const controllerAddr = addrs.value.registrarController;
|
|
531
|
+
log.debug("isDotNsAvailable", { name: normalized, label, controller: controllerAddr });
|
|
532
|
+
|
|
533
|
+
try {
|
|
534
|
+
const controller = contractOf(
|
|
535
|
+
opts.runtime,
|
|
536
|
+
controllerAddr as HexString,
|
|
537
|
+
DOTNS_REGISTRAR_CONTROLLER_ABI,
|
|
538
|
+
);
|
|
539
|
+
const popRules = contractOf(opts.runtime, addrs.value.popRules, DOTNS_POP_RULES_ABI);
|
|
540
|
+
const origin = readOrigin(opts);
|
|
541
|
+
const [res, classified] = await Promise.all([
|
|
542
|
+
controller.available.query(label, { origin }),
|
|
543
|
+
popRules.classifyName.query(label, { origin }),
|
|
544
|
+
]);
|
|
545
|
+
if (!res.success) {
|
|
546
|
+
return err(
|
|
547
|
+
new DotNsError("RegistryCall", "registrarController.available call failed", {
|
|
548
|
+
cause: res.value,
|
|
549
|
+
}),
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
if (res.value !== true) return ok(false);
|
|
553
|
+
|
|
554
|
+
// classifyName reverts on a label shape the registrar rejects, so a
|
|
555
|
+
// failure here is surfaced rather than folded into `false`.
|
|
556
|
+
if (!classified.success) {
|
|
557
|
+
return err(
|
|
558
|
+
new DotNsError("RegistryCall", "PopRules.classifyName call failed", {
|
|
559
|
+
cause: classified.value,
|
|
560
|
+
}),
|
|
561
|
+
);
|
|
562
|
+
}
|
|
563
|
+
const { requirement } = classified.value as { requirement: number; message: string };
|
|
564
|
+
return ok(requirement !== POP_STATUS.Reserved);
|
|
565
|
+
} catch (cause) {
|
|
566
|
+
return err(
|
|
567
|
+
new DotNsError("RegistryCall", `DotNS availability check failed for "${normalized}"`, {
|
|
568
|
+
cause,
|
|
569
|
+
}),
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// ── Writes ───────────────────────────────────────────────────────────
|
|
575
|
+
//
|
|
576
|
+
// Writes return prepared calls (`BatchableCall`) the caller submits with their
|
|
577
|
+
// own signer via `@parity/product-sdk-tx` — the surface stays signer-free, like
|
|
578
|
+
// the reads. Registration is a two-transaction commit-reveal, so it can't be a
|
|
579
|
+
// single call; `prepareDotNsRegistration` returns both plus the timing window.
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* Prepare the calls binding a name to an address. Submit them in order with the
|
|
583
|
+
* owner's signer via `batchSubmitAndWatch`; both are owner-gated.
|
|
584
|
+
*
|
|
585
|
+
* Returns two calls when the node still points at the reverse resolver (the
|
|
586
|
+
* post-registration default): `registry.setResolver` first, or the record
|
|
587
|
+
* written by `setAddress` is real but unreadable. One call once it is pointed.
|
|
588
|
+
*/
|
|
589
|
+
export async function setDotNsRecord(
|
|
590
|
+
args: SetRecordArgs,
|
|
591
|
+
opts: DotNsClientOptions,
|
|
592
|
+
): Promise<Result<BatchableCall[], DotNsError>> {
|
|
593
|
+
const tldRes = await resolveTld(opts);
|
|
594
|
+
if (!tldRes.ok) return tldRes;
|
|
595
|
+
const tld = tldRes.value;
|
|
596
|
+
const normalized = normalizeDotNsName(args.name, tld.suffix);
|
|
597
|
+
if (!isResolvableDotNsName(normalized, tld.suffix)) {
|
|
598
|
+
return err(nameError(args.name, normalized, tld));
|
|
599
|
+
}
|
|
600
|
+
// Both calls are owner-gated, so a dry-run from the fallback account would
|
|
601
|
+
// revert and the error would look like a contract problem.
|
|
602
|
+
const origin = opts.origin;
|
|
603
|
+
if (!origin) {
|
|
604
|
+
return err(new DotNsError("MissingOrigin", "setDotNsRecord needs opts.origin (SS58)"));
|
|
605
|
+
}
|
|
606
|
+
const node = namehash(normalized, tld);
|
|
607
|
+
const addrs = await resolveDotNsAddresses(opts);
|
|
608
|
+
if (!addrs.ok) return addrs;
|
|
609
|
+
const registryAddr = addrs.value.registry;
|
|
610
|
+
const resolverAddr = addrs.value.resolver;
|
|
611
|
+
try {
|
|
612
|
+
const registry = contractOf(opts.runtime, registryAddr as HexString, DOTNS_REGISTRY_ABI);
|
|
613
|
+
const currentRes = await registry.resolver.query(node, { origin });
|
|
614
|
+
if (!currentRes.success) {
|
|
615
|
+
return err(
|
|
616
|
+
new DotNsError("RegistryCall", "registry.resolver call failed", {
|
|
617
|
+
cause: currentRes.value,
|
|
618
|
+
}),
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
const calls: BatchableCall[] = [];
|
|
623
|
+
if (!sameAddress(currentRes.value, resolverAddr)) {
|
|
624
|
+
const pointer = await registry.setResolver.prepare(node, resolverAddr, { origin });
|
|
625
|
+
if (!pointer.ok) {
|
|
626
|
+
return err(
|
|
627
|
+
new DotNsError("RegistryCall", "registry.setResolver prepare failed", {
|
|
628
|
+
cause: pointer.error,
|
|
629
|
+
}),
|
|
630
|
+
);
|
|
631
|
+
}
|
|
632
|
+
calls.push(pointer.value as BatchableCall);
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
const resolver = contractOf(
|
|
636
|
+
opts.runtime,
|
|
637
|
+
resolverAddr as HexString,
|
|
638
|
+
DOTNS_RESOLVER_WRITE_ABI,
|
|
639
|
+
);
|
|
640
|
+
const prepared = await resolver.setAddress.prepare(node, args.address, { origin });
|
|
641
|
+
if (!prepared.ok) {
|
|
642
|
+
return err(
|
|
643
|
+
new DotNsError("RegistryCall", "resolver.setAddress prepare failed", {
|
|
644
|
+
cause: prepared.error,
|
|
645
|
+
}),
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
calls.push(prepared.value as BatchableCall);
|
|
649
|
+
|
|
650
|
+
return ok(calls);
|
|
651
|
+
} catch (cause) {
|
|
652
|
+
return err(new DotNsError("RegistryCall", "DotNS setRecord failed", { cause }));
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/** The prepared pieces of a DotNS registration. */
|
|
657
|
+
export interface DotNsRegistration {
|
|
658
|
+
/** The random secret bound into both commit and register. */
|
|
659
|
+
secret: HexString;
|
|
660
|
+
/** The commitment hash (also encoded inside `commitCall`). */
|
|
661
|
+
commitment: HexString;
|
|
662
|
+
/** Submit first, with the owner's signer. */
|
|
663
|
+
commitCall: BatchableCall;
|
|
664
|
+
/** Seconds to wait after `commit` before `register` is accepted. */
|
|
665
|
+
minCommitmentAge: bigint;
|
|
666
|
+
/** Seconds after which the commitment expires and `register` is rejected. */
|
|
667
|
+
maxCommitmentAge: bigint;
|
|
668
|
+
/** Quote at prepare time, for display. The submitted value is re-read below. */
|
|
669
|
+
price: bigint;
|
|
670
|
+
/**
|
|
671
|
+
* Build the register call, once `minCommitmentAge` has elapsed.
|
|
672
|
+
*
|
|
673
|
+
* Deferred rather than returned up front: `register` consumes the
|
|
674
|
+
* commitment, so dry-running it before `commitCall` lands reverts with
|
|
675
|
+
* `CommitmentNotFound` and no call could be built at all. Deferring also
|
|
676
|
+
* prices and sizes it against the state it will actually execute in.
|
|
677
|
+
*/
|
|
678
|
+
prepareRegisterCall: () => Promise<Result<BatchableCall, DotNsError>>;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/** What `register` will charge, and whether it will accept the owner at all. */
|
|
682
|
+
async function quoteRegistration(
|
|
683
|
+
popRules: ReturnType<typeof contractOf>,
|
|
684
|
+
label: string,
|
|
685
|
+
owner: string,
|
|
686
|
+
payer: string,
|
|
687
|
+
origin: SS58String,
|
|
688
|
+
): Promise<Result<bigint, DotNsError>> {
|
|
689
|
+
// `price(label)` is not what register charges: it skips every eligibility
|
|
690
|
+
// rule and ignores the owner. The owner-aware variant reports the same
|
|
691
|
+
// classification register applies, without reverting on it.
|
|
692
|
+
const quote = await popRules.priceWithoutCheck.query(label, owner, { origin });
|
|
693
|
+
if (!quote.success) {
|
|
694
|
+
return err(
|
|
695
|
+
new DotNsError("RegistryCall", "PopRules.priceWithoutCheck failed", {
|
|
696
|
+
cause: quote.value,
|
|
697
|
+
}),
|
|
698
|
+
);
|
|
699
|
+
}
|
|
700
|
+
const { price, status, userStatus, message } = quote.value as {
|
|
701
|
+
price: bigint;
|
|
702
|
+
status: number;
|
|
703
|
+
userStatus: number;
|
|
704
|
+
message: string;
|
|
705
|
+
};
|
|
706
|
+
if (status === POP_STATUS.Reserved) {
|
|
707
|
+
return err(new DotNsError("NameReserved", `"${label}" is reserved: ${message}`));
|
|
708
|
+
}
|
|
709
|
+
if (userStatus < status) {
|
|
710
|
+
return err(
|
|
711
|
+
new DotNsError(
|
|
712
|
+
"OwnerStatusInsufficient",
|
|
713
|
+
`"${label}" requires personhood tier ${status}, owner has ${userStatus}: ${message}`,
|
|
714
|
+
),
|
|
715
|
+
);
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
// Cross-payer registrations add a friction floor; register charges the
|
|
719
|
+
// larger of the two.
|
|
720
|
+
if (sameAddress(payer, owner)) return ok(BigInt(price));
|
|
721
|
+
const floor = await popRules.transferFloor.query(label, payer, owner, { origin });
|
|
722
|
+
if (!floor.success) {
|
|
723
|
+
return err(
|
|
724
|
+
new DotNsError("RegistryCall", "PopRules.transferFloor failed", {
|
|
725
|
+
cause: floor.value,
|
|
726
|
+
}),
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
const friction = BigInt(floor.value as string | number | bigint);
|
|
730
|
+
const base = BigInt(price);
|
|
731
|
+
return ok(base > friction ? base : friction);
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Prepare a DotNS registration: the commit call, the shared secret, the timing
|
|
736
|
+
* window, and a thunk that builds the register call afterwards.
|
|
737
|
+
*
|
|
738
|
+
* 1. submit `commitCall`
|
|
739
|
+
* 2. wait `minCommitmentAge`, and register before `maxCommitmentAge`
|
|
740
|
+
* 3. `await prepareRegisterCall()`, then submit what it returns
|
|
741
|
+
*
|
|
742
|
+
* Eligibility is checked here, before the caller pays for the commit: a
|
|
743
|
+
* governance-reserved label, or an owner below the tier the label requires,
|
|
744
|
+
* fails now rather than on the second transaction.
|
|
745
|
+
*/
|
|
746
|
+
export async function prepareDotNsRegistration(
|
|
747
|
+
args: RegisterDotNsArgs,
|
|
748
|
+
opts: DotNsClientOptions,
|
|
749
|
+
): Promise<Result<DotNsRegistration, DotNsError>> {
|
|
750
|
+
const tldRes = await resolveTld(opts);
|
|
751
|
+
if (!tldRes.ok) return tldRes;
|
|
752
|
+
const tld = tldRes.value;
|
|
753
|
+
const normalized = normalizeDotNsName(args.name, tld.suffix);
|
|
754
|
+
if (!isValidDotNsName(normalized, tld.suffix)) {
|
|
755
|
+
return err(nameError(args.name, normalized, tld));
|
|
756
|
+
}
|
|
757
|
+
const origin = opts.origin;
|
|
758
|
+
if (!origin) {
|
|
759
|
+
return err(
|
|
760
|
+
new DotNsError("MissingOrigin", "prepareDotNsRegistration needs opts.origin (SS58)"),
|
|
761
|
+
);
|
|
762
|
+
}
|
|
763
|
+
// The registrar takes the bare label, without the TLD suffix.
|
|
764
|
+
const label = stripSuffix(normalized, tld.suffix);
|
|
765
|
+
const addrs = await resolveDotNsAddresses(opts);
|
|
766
|
+
if (!addrs.ok) return addrs;
|
|
767
|
+
const controllerAddr = addrs.value.registrarController;
|
|
768
|
+
const popRulesAddr = addrs.value.popRules;
|
|
769
|
+
const secret = `0x${bytesToHex(randomBytes(32))}` as HexString;
|
|
770
|
+
const registration = { label, owner: args.owner, secret, reserved: args.reserved ?? false };
|
|
771
|
+
// register compares msg.sender against the owner; msg.sender is derived
|
|
772
|
+
// from the SS58 origin, so derive it the same way to predict the branch.
|
|
773
|
+
// ss58ToH160 throws on an undecodable address, and this sits outside the
|
|
774
|
+
// try below, so guard it here rather than letting it escape the Result.
|
|
775
|
+
let payer: HexString;
|
|
776
|
+
try {
|
|
777
|
+
payer = ss58ToH160(origin);
|
|
778
|
+
} catch (cause) {
|
|
779
|
+
return err(
|
|
780
|
+
new DotNsError("InvalidOrigin", `opts.origin is not a valid SS58 address: ${origin}`, {
|
|
781
|
+
cause,
|
|
782
|
+
}),
|
|
783
|
+
);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
try {
|
|
787
|
+
const controller = contractOf(
|
|
788
|
+
opts.runtime,
|
|
789
|
+
controllerAddr as HexString,
|
|
790
|
+
DOTNS_REGISTRAR_CONTROLLER_ABI,
|
|
791
|
+
);
|
|
792
|
+
const popRules = contractOf(opts.runtime, popRulesAddr as HexString, DOTNS_POP_RULES_ABI);
|
|
793
|
+
|
|
794
|
+
const [commitmentRes, availableRes, quote, minRes, maxRes] = await Promise.all([
|
|
795
|
+
controller.makeCommitment.query(registration, { origin }),
|
|
796
|
+
controller.available.query(label, { origin }),
|
|
797
|
+
quoteRegistration(popRules, label, args.owner, payer, origin),
|
|
798
|
+
controller.minCommitmentAge.query({ origin }),
|
|
799
|
+
controller.maxCommitmentAge.query({ origin }),
|
|
800
|
+
]);
|
|
801
|
+
// `register` runs _requireAvailableLabel before anything else, so a
|
|
802
|
+
// taken name costs the caller a commit fee and then reverts.
|
|
803
|
+
if (!availableRes.success) {
|
|
804
|
+
return err(
|
|
805
|
+
new DotNsError("RegistryCall", "registrarController.available call failed", {
|
|
806
|
+
cause: availableRes.value,
|
|
807
|
+
}),
|
|
808
|
+
);
|
|
809
|
+
}
|
|
810
|
+
if (availableRes.value !== true) {
|
|
811
|
+
return err(new DotNsError("NameUnavailable", `"${label}" is already registered`));
|
|
812
|
+
}
|
|
813
|
+
if (!commitmentRes.success) {
|
|
814
|
+
return err(
|
|
815
|
+
new DotNsError("RegistryCall", "makeCommitment failed", {
|
|
816
|
+
cause: commitmentRes.value,
|
|
817
|
+
}),
|
|
818
|
+
);
|
|
819
|
+
}
|
|
820
|
+
if (!quote.ok) return quote;
|
|
821
|
+
if (!minRes.success || !maxRes.success) {
|
|
822
|
+
return err(
|
|
823
|
+
new DotNsError("RegistryCall", "commitment window read failed", {
|
|
824
|
+
cause: minRes.success ? maxRes.value : minRes.value,
|
|
825
|
+
}),
|
|
826
|
+
);
|
|
827
|
+
}
|
|
828
|
+
const commitment = commitmentRes.value as HexString;
|
|
829
|
+
|
|
830
|
+
const commitPrep = await controller.commit.prepare(commitment, { origin });
|
|
831
|
+
if (!commitPrep.ok) {
|
|
832
|
+
return err(
|
|
833
|
+
new DotNsError("RegistryCall", "controller.commit prepare failed", {
|
|
834
|
+
cause: commitPrep.error,
|
|
835
|
+
}),
|
|
836
|
+
);
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
return ok({
|
|
840
|
+
secret,
|
|
841
|
+
commitment,
|
|
842
|
+
commitCall: commitPrep.value as BatchableCall,
|
|
843
|
+
minCommitmentAge: BigInt(minRes.value as string | number),
|
|
844
|
+
maxCommitmentAge: BigInt(maxRes.value as string | number),
|
|
845
|
+
price: quote.value,
|
|
846
|
+
prepareRegisterCall: async () => {
|
|
847
|
+
// Re-quoted, not reused: the price and the owner's tier can both
|
|
848
|
+
// move during the mandatory wait.
|
|
849
|
+
const current = await quoteRegistration(popRules, label, args.owner, payer, origin);
|
|
850
|
+
if (!current.ok) return current;
|
|
851
|
+
const prep = await controller.register.prepare(registration, {
|
|
852
|
+
value: current.value,
|
|
853
|
+
origin,
|
|
854
|
+
});
|
|
855
|
+
if (!prep.ok) {
|
|
856
|
+
return err(
|
|
857
|
+
new DotNsError("RegistryCall", "controller.register prepare failed", {
|
|
858
|
+
cause: prep.error,
|
|
859
|
+
}),
|
|
860
|
+
);
|
|
861
|
+
}
|
|
862
|
+
return ok(prep.value as BatchableCall);
|
|
863
|
+
},
|
|
864
|
+
});
|
|
865
|
+
} catch (cause) {
|
|
866
|
+
return err(new DotNsError("RegistryCall", "DotNS registration prepare failed", { cause }));
|
|
867
|
+
}
|
|
868
|
+
}
|