hawk-names 0.1.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/src/index.ts ADDED
@@ -0,0 +1,48 @@
1
+ // hawk-names — resolve and register .hawk names on Base.
2
+ //
3
+ // One-line dapp integration:
4
+ // import { createPublicClient, http } from "viem";
5
+ // import { base } from "hawk-names";
6
+ // const client = createPublicClient({ chain: base, transport: http() });
7
+ // await client.getEnsName({ address: "0x..." }); // → "trader.hawk"
8
+
9
+ export {
10
+ base,
11
+ baseSepolia,
12
+ withHawk,
13
+ } from "./chains.js";
14
+
15
+ export {
16
+ HAWK_ADDRESSES,
17
+ getHawkAddresses,
18
+ hawkAddressesFrom,
19
+ type HawkAddresses,
20
+ } from "./addresses.js";
21
+
22
+ export {
23
+ getHawkName,
24
+ getHawkAddress,
25
+ getHawkText,
26
+ getHawkAvatar,
27
+ } from "./actions.js";
28
+
29
+ export {
30
+ HAWK_NODE,
31
+ REVERSE_RECORD_NONE,
32
+ REVERSE_RECORD_CHAIN,
33
+ REVERSE_RECORD_DEFAULT,
34
+ SECONDS_PER_YEAR,
35
+ MIN_REGISTRATION_DURATION_MAINNET,
36
+ MAX_REGISTRATION_DURATION,
37
+ type Registration,
38
+ makeRegistration,
39
+ makeCommitment,
40
+ randomSecret,
41
+ hawkNode,
42
+ hawkTokenId,
43
+ validateLabel,
44
+ } from "./registration.js";
45
+
46
+ export { normalize, namehash, labelhash } from "viem/ens";
47
+
48
+ export * from "./generated/abis.js";
@@ -0,0 +1,115 @@
1
+ import {
2
+ encodeAbiParameters,
3
+ keccak256,
4
+ toHex,
5
+ zeroAddress,
6
+ zeroHash,
7
+ type Address,
8
+ type Hex,
9
+ } from "viem";
10
+ import { namehash, normalize } from "viem/ens";
11
+
12
+ /** namehash('hawk') */
13
+ export const HAWK_NODE =
14
+ "0xf431efc1fb1b854a38191ac67de7e1ee70205b40f71cb927c938685e82e05403" as const;
15
+
16
+ /** Reverse-record bits for Registration.reverseRecord. */
17
+ export const REVERSE_RECORD_NONE = 0;
18
+ export const REVERSE_RECORD_CHAIN = 1; // addr.reverse on Base
19
+ export const REVERSE_RECORD_DEFAULT = 2; // default.reverse (EVM-wide fallback)
20
+
21
+ /** Mirror of HawkRegistrarController's Registration struct. */
22
+ export type Registration = {
23
+ label: string;
24
+ owner: Address;
25
+ duration: bigint;
26
+ secret: Hex;
27
+ resolver: Address;
28
+ data: readonly Hex[];
29
+ reverseRecord: number;
30
+ referrer: Hex;
31
+ };
32
+
33
+ export function makeRegistration(params: {
34
+ label: string;
35
+ owner: Address;
36
+ duration: bigint;
37
+ secret: Hex;
38
+ resolver?: Address;
39
+ data?: readonly Hex[];
40
+ reverseRecord?: number;
41
+ referrer?: Hex;
42
+ }): Registration {
43
+ return {
44
+ label: params.label,
45
+ owner: params.owner,
46
+ duration: params.duration,
47
+ secret: params.secret,
48
+ resolver: params.resolver ?? zeroAddress,
49
+ data: params.data ?? [],
50
+ reverseRecord: params.reverseRecord ?? REVERSE_RECORD_NONE,
51
+ referrer: params.referrer ?? zeroHash,
52
+ };
53
+ }
54
+
55
+ const REGISTRATION_TUPLE = {
56
+ type: "tuple",
57
+ components: [
58
+ { name: "label", type: "string" },
59
+ { name: "owner", type: "address" },
60
+ { name: "duration", type: "uint256" },
61
+ { name: "secret", type: "bytes32" },
62
+ { name: "resolver", type: "address" },
63
+ { name: "data", type: "bytes[]" },
64
+ { name: "reverseRecord", type: "uint8" },
65
+ { name: "referrer", type: "bytes32" },
66
+ ],
67
+ } as const;
68
+
69
+ /**
70
+ * Computes the commit-reveal commitment for a registration —
71
+ * `keccak256(abi.encode(registration))`, byte-identical to the
72
+ * controller's `makeCommitment`.
73
+ */
74
+ export function makeCommitment(registration: Registration): Hex {
75
+ return keccak256(
76
+ encodeAbiParameters([REGISTRATION_TUPLE], [registration]),
77
+ );
78
+ }
79
+
80
+ /** Random 32-byte commit-reveal secret (browser + node). */
81
+ export function randomSecret(): Hex {
82
+ const bytes = new Uint8Array(32);
83
+ globalThis.crypto.getRandomValues(bytes);
84
+ return toHex(bytes);
85
+ }
86
+
87
+ /** The namehash of `<label>.hawk`. */
88
+ export function hawkNode(label: string): Hex {
89
+ return namehash(`${normalize(label)}.hawk`);
90
+ }
91
+
92
+ /** The ERC-721 token id (labelhash) for a .hawk second-level name. */
93
+ export function hawkTokenId(label: string): bigint {
94
+ return BigInt(keccak256(toHex(normalize(label))));
95
+ }
96
+
97
+ /**
98
+ * Client-side registrability check, mirroring the controller's `valid` plus
99
+ * ENSIP-15 normalization (the on-chain check is length-only; apps must
100
+ * normalize). Returns the normalized label or throws with the reason.
101
+ */
102
+ export function validateLabel(rawLabel: string): string {
103
+ const label = normalize(rawLabel);
104
+ if (label.includes(".")) {
105
+ throw new Error("A label cannot contain dots.");
106
+ }
107
+ if ([...label].length < 3) {
108
+ throw new Error("Names must be at least 3 characters.");
109
+ }
110
+ return label;
111
+ }
112
+
113
+ export const SECONDS_PER_YEAR = 31_536_000n; // 365 days, the pricing year
114
+ export const MIN_REGISTRATION_DURATION_MAINNET = 2_419_200n; // 28 days
115
+ export const MAX_REGISTRATION_DURATION = 315_360_000n; // 3650 days