@ensdomains/ensjs 3.0.0-alpha.33 → 3.0.0-alpha.35

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.
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var getAvailable_exports = {};
20
+ __export(getAvailable_exports, {
21
+ default: () => getAvailable_default
22
+ });
23
+ module.exports = __toCommonJS(getAvailable_exports);
24
+ var import_labels = require("../utils/labels");
25
+ const raw = async ({ contracts }, name) => {
26
+ const baseRegistrar = await contracts?.getBaseRegistrar();
27
+ const labels = name.split(".");
28
+ if (labels.length !== 2 || labels[1] !== "eth") {
29
+ throw new Error("Currently only .eth names can be checked for availability");
30
+ }
31
+ return {
32
+ to: baseRegistrar.address,
33
+ data: baseRegistrar.interface.encodeFunctionData("available", [
34
+ (0, import_labels.labelhash)(labels[0])
35
+ ])
36
+ };
37
+ };
38
+ const decode = async ({ contracts }, data) => {
39
+ if (data === null)
40
+ return;
41
+ const baseRegistrar = await contracts?.getBaseRegistrar();
42
+ try {
43
+ const result = baseRegistrar.interface.decodeFunctionResult(
44
+ "available",
45
+ data
46
+ );
47
+ return result["0"];
48
+ } catch {
49
+ return;
50
+ }
51
+ };
52
+ var getAvailable_default = {
53
+ raw,
54
+ decode
55
+ };
@@ -31,18 +31,21 @@ __export(getDNSOwner_exports, {
31
31
  });
32
32
  module.exports = __toCommonJS(getDNSOwner_exports);
33
33
  var packet = __toESM(require("dns-packet"));
34
+ var import_utils = require("ethers/lib/utils");
34
35
  function encodeURLParams(p) {
35
36
  return Object.entries(p).map((kv) => kv.map(encodeURIComponent).join("=")).join("&");
36
37
  }
37
38
  const getDNS = async (q) => {
38
- const response = await global.fetch(
39
- `https://cloudflare-dns.com/dns-query?${encodeURLParams({
40
- ct: "application/dns-udpwireformat",
41
- dns: packet.encode(q)?.toString("base64"),
42
- ts: Date.now().toString()
43
- })}`
39
+ const url = `https://cloudflare-dns.com/dns-query?${encodeURLParams({
40
+ ct: "application/dns-udpwireformat",
41
+ dns: packet.encode(q)?.toString("base64"),
42
+ ts: Date.now().toString()
43
+ })}`;
44
+ const response = await (0, import_utils._fetchData)(url, void 0);
45
+ const arrayBuffer = response.buffer.slice(
46
+ response.byteOffset,
47
+ response.byteLength + response.byteOffset
44
48
  );
45
- const arrayBuffer = await response.arrayBuffer();
46
49
  const fromArrayBuffer = Buffer.from(arrayBuffer);
47
50
  return packet.decode(fromArrayBuffer);
48
51
  };
@@ -25,7 +25,8 @@ var import_ethers = require("ethers");
25
25
  var import_normalise = require("../utils/normalise");
26
26
  async function transferName_default({ contracts, signer }, name, {
27
27
  newOwner,
28
- contract
28
+ contract,
29
+ reclaim
29
30
  }) {
30
31
  const address = await signer.getAddress();
31
32
  switch (contract) {
@@ -41,11 +42,11 @@ async function transferName_default({ contracts, signer }, name, {
41
42
  if (labels.length > 2 || labels[labels.length - 1] !== "eth") {
42
43
  throw new Error("Invalid name for baseRegistrar");
43
44
  }
44
- return baseRegistrar.populateTransaction["safeTransferFrom(address,address,uint256)"](
45
- address,
46
- newOwner,
47
- import_ethers.ethers.utils.solidityKeccak256(["string"], [labels[0]])
48
- );
45
+ const tokenId = import_ethers.ethers.utils.solidityKeccak256(["string"], [labels[0]]);
46
+ if (reclaim) {
47
+ return baseRegistrar.populateTransaction.reclaim(tokenId, newOwner);
48
+ }
49
+ return baseRegistrar.populateTransaction["safeTransferFrom(address,address,uint256)"](address, newOwner, tokenId);
49
50
  }
50
51
  case "nameWrapper": {
51
52
  const nameWrapper = (await contracts?.getNameWrapper()).connect(signer);
package/dist/cjs/index.js CHANGED
@@ -261,6 +261,10 @@ class ENS {
261
261
  "getDNSOwner",
262
262
  []
263
263
  );
264
+ getAvailable = this.generateRawFunction(
265
+ "getAvailable",
266
+ ["contracts"]
267
+ );
264
268
  universalWrapper = this.generateRawFunction(
265
269
  "initialGetters",
266
270
  ["contracts"],
@@ -0,0 +1,36 @@
1
+ // src/functions/getAvailable.ts
2
+ import { labelhash } from "../utils/labels.mjs";
3
+ var raw = async ({ contracts }, name) => {
4
+ const baseRegistrar = await contracts?.getBaseRegistrar();
5
+ const labels = name.split(".");
6
+ if (labels.length !== 2 || labels[1] !== "eth") {
7
+ throw new Error("Currently only .eth names can be checked for availability");
8
+ }
9
+ return {
10
+ to: baseRegistrar.address,
11
+ data: baseRegistrar.interface.encodeFunctionData("available", [
12
+ labelhash(labels[0])
13
+ ])
14
+ };
15
+ };
16
+ var decode = async ({ contracts }, data) => {
17
+ if (data === null)
18
+ return;
19
+ const baseRegistrar = await contracts?.getBaseRegistrar();
20
+ try {
21
+ const result = baseRegistrar.interface.decodeFunctionResult(
22
+ "available",
23
+ data
24
+ );
25
+ return result["0"];
26
+ } catch {
27
+ return;
28
+ }
29
+ };
30
+ var getAvailable_default = {
31
+ raw,
32
+ decode
33
+ };
34
+ export {
35
+ getAvailable_default as default
36
+ };
@@ -1,17 +1,20 @@
1
1
  // src/functions/getDNSOwner.ts
2
2
  import * as packet from "dns-packet";
3
+ import { _fetchData } from "ethers/lib/utils.js";
3
4
  function encodeURLParams(p) {
4
5
  return Object.entries(p).map((kv) => kv.map(encodeURIComponent).join("=")).join("&");
5
6
  }
6
7
  var getDNS = async (q) => {
7
- const response = await global.fetch(
8
- `https://cloudflare-dns.com/dns-query?${encodeURLParams({
9
- ct: "application/dns-udpwireformat",
10
- dns: packet.encode(q)?.toString("base64"),
11
- ts: Date.now().toString()
12
- })}`
8
+ const url = `https://cloudflare-dns.com/dns-query?${encodeURLParams({
9
+ ct: "application/dns-udpwireformat",
10
+ dns: packet.encode(q)?.toString("base64"),
11
+ ts: Date.now().toString()
12
+ })}`;
13
+ const response = await _fetchData(url, void 0);
14
+ const arrayBuffer = response.buffer.slice(
15
+ response.byteOffset,
16
+ response.byteLength + response.byteOffset
13
17
  );
14
- const arrayBuffer = await response.arrayBuffer();
15
18
  const fromArrayBuffer = Buffer.from(arrayBuffer);
16
19
  return packet.decode(fromArrayBuffer);
17
20
  };
@@ -3,7 +3,8 @@ import { ethers } from "ethers";
3
3
  import { namehash } from "../utils/normalise.mjs";
4
4
  async function transferName_default({ contracts, signer }, name, {
5
5
  newOwner,
6
- contract
6
+ contract,
7
+ reclaim
7
8
  }) {
8
9
  const address = await signer.getAddress();
9
10
  switch (contract) {
@@ -19,11 +20,11 @@ async function transferName_default({ contracts, signer }, name, {
19
20
  if (labels.length > 2 || labels[labels.length - 1] !== "eth") {
20
21
  throw new Error("Invalid name for baseRegistrar");
21
22
  }
22
- return baseRegistrar.populateTransaction["safeTransferFrom(address,address,uint256)"](
23
- address,
24
- newOwner,
25
- ethers.utils.solidityKeccak256(["string"], [labels[0]])
26
- );
23
+ const tokenId = ethers.utils.solidityKeccak256(["string"], [labels[0]]);
24
+ if (reclaim) {
25
+ return baseRegistrar.populateTransaction.reclaim(tokenId, newOwner);
26
+ }
27
+ return baseRegistrar.populateTransaction["safeTransferFrom(address,address,uint256)"](address, newOwner, tokenId);
27
28
  }
28
29
  case "nameWrapper": {
29
30
  const nameWrapper = (await contracts?.getNameWrapper()).connect(signer);
@@ -233,6 +233,10 @@ var ENS = class {
233
233
  "getDNSOwner",
234
234
  []
235
235
  );
236
+ getAvailable = this.generateRawFunction(
237
+ "getAvailable",
238
+ ["contracts"]
239
+ );
236
240
  universalWrapper = this.generateRawFunction(
237
241
  "initialGetters",
238
242
  ["contracts"],
@@ -0,0 +1,9 @@
1
+ import { ENSArgs } from '..';
2
+ declare const _default: {
3
+ raw: ({ contracts }: ENSArgs<"contracts">, name: string) => Promise<{
4
+ to: string;
5
+ data: string;
6
+ }>;
7
+ decode: ({ contracts }: ENSArgs<"contracts">, data: string) => Promise<boolean | undefined>;
8
+ };
9
+ export default _default;
@@ -42,14 +42,14 @@ export declare const _getText: {
42
42
  to: string;
43
43
  data: string;
44
44
  }>;
45
- decode: ({ contracts }: ENSArgs<'contracts'>, data: string) => Promise<any>;
45
+ decode: ({ contracts }: ENSArgs<'contracts'>, data: string) => Promise<string | undefined>;
46
46
  };
47
47
  export declare const getText: {
48
48
  raw: ({ contracts, universalWrapper }: ENSArgs<'contracts' | 'universalWrapper'>, name: string, key: string) => Promise<{
49
49
  to: string;
50
50
  data: string;
51
51
  }>;
52
- decode: ({ contracts, universalWrapper }: ENSArgs<'contracts' | 'universalWrapper'>, data: string) => Promise<any>;
52
+ decode: ({ contracts, universalWrapper }: ENSArgs<'contracts' | 'universalWrapper'>, data: string) => Promise<string | undefined>;
53
53
  };
54
54
  export declare const _getAddr: {
55
55
  raw: ({ contracts }: ENSArgs<'contracts'>, name: string, coinType?: string | number, bypassFormat?: boolean) => Promise<{
@@ -1,6 +1,7 @@
1
1
  import { ethers } from 'ethers';
2
2
  import { ENSArgs } from '..';
3
- export default function ({ contracts, signer }: ENSArgs<'contracts' | 'signer'>, name: string, { newOwner, contract, }: {
3
+ export default function ({ contracts, signer }: ENSArgs<'contracts' | 'signer'>, name: string, { newOwner, contract, reclaim, }: {
4
4
  newOwner: string;
5
5
  contract: 'registry' | 'nameWrapper' | 'baseRegistrar';
6
+ reclaim?: boolean;
6
7
  }): Promise<ethers.PopulatedTransaction>;
@@ -7,7 +7,7 @@ import type burnFuses from './functions/burnFuses';
7
7
  import type commitName from './functions/commitName';
8
8
  import type createSubname from './functions/createSubname';
9
9
  import type deleteSubname from './functions/deleteSubname';
10
- import importDNSSECName from './functions/importDNSSECName';
10
+ import type importDNSSECName from './functions/importDNSSECName';
11
11
  import type registerName from './functions/registerName';
12
12
  import type { default as renewNames, renewNameWithData } from './functions/renewNames';
13
13
  import type setName from './functions/setName';
@@ -69,6 +69,12 @@ interface GeneratedRawFunction<F extends RawFunction> extends Function, RawFunct
69
69
  }
70
70
  export interface GenericGeneratedRawFunction extends Function, RawFunctionWithBatch {
71
71
  }
72
+ interface GeneratedBatchFunction<F extends RawFunction> extends Function, RawFunction {
73
+ <I extends BatchFunctionResult<RawFunction>[]>(...args: I): Promise<{
74
+ [N in keyof I]: I[N] extends BatchFunctionResult<infer U> ? Awaited<ReturnType<U['decode']>> : never;
75
+ } | undefined>;
76
+ batch: BatchFunction<F>;
77
+ }
72
78
  export declare class ENS {
73
79
  [x: string]: any;
74
80
  protected options?: ENSOptions;
@@ -135,7 +141,7 @@ export declare class ENS {
135
141
  * @returns {ENS} - A new ENS instance with the given provider
136
142
  */
137
143
  withProvider: (provider: ethers.providers.JsonRpcProvider) => ENS;
138
- batch: GeneratedRawFunction<{
144
+ batch: GeneratedBatchFunction<{
139
145
  raw: ({ multicallWrapper }: ENSArgs<"multicallWrapper">, ...items: BatchFunctionResult<RawFunction>[]) => Promise<{
140
146
  to: string;
141
147
  data: string;
@@ -330,14 +336,14 @@ export declare class ENS {
330
336
  to: string;
331
337
  data: string;
332
338
  }>;
333
- decode: ({ contracts, universalWrapper }: ENSArgs<"contracts" | "universalWrapper">, data: string) => Promise<any>;
339
+ decode: ({ contracts, universalWrapper }: ENSArgs<"contracts" | "universalWrapper">, data: string) => Promise<string | undefined>;
334
340
  }>;
335
341
  _getText: GeneratedRawFunction<{
336
342
  raw: ({ contracts }: ENSArgs<"contracts">, name: string, key: string) => Promise<{
337
343
  to: string;
338
344
  data: string;
339
345
  }>;
340
- decode: ({ contracts }: ENSArgs<"contracts">, data: string) => Promise<any>;
346
+ decode: ({ contracts }: ENSArgs<"contracts">, data: string) => Promise<string | undefined>;
341
347
  }>;
342
348
  getOwner: GeneratedRawFunction<{
343
349
  raw: ({ contracts, multicallWrapper }: ENSArgs<"contracts" | "multicallWrapper">, name: string, contract?: "nameWrapper" | "registrar" | "registry" | undefined) => Promise<{
@@ -421,6 +427,13 @@ export declare class ENS {
421
427
  } | undefined>;
422
428
  }>;
423
429
  getDNSOwner: (dnsName: string) => Promise<any>;
430
+ getAvailable: GeneratedRawFunction<{
431
+ raw: ({ contracts }: ENSArgs<"contracts">, name: string) => Promise<{
432
+ to: string;
433
+ data: string;
434
+ }>;
435
+ decode: ({ contracts }: ENSArgs<"contracts">, data: string) => Promise<boolean | undefined>;
436
+ }>;
424
437
  universalWrapper: GeneratedRawFunction<{
425
438
  raw: ({ contracts }: ENSArgs<"contracts">, name: string, data: string) => Promise<{
426
439
  to: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ensdomains/ensjs",
3
- "version": "3.0.0-alpha.33",
3
+ "version": "3.0.0-alpha.35",
4
4
  "description": "ENS javascript library for contract interaction",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.mjs",
@@ -0,0 +1,21 @@
1
+ import { ENS } from '..'
2
+ import setup from '../tests/setup'
3
+
4
+ let ensInstance: ENS
5
+
6
+ beforeAll(async () => {
7
+ ;({ ensInstance } = await setup())
8
+ })
9
+
10
+ describe('getAvailable', () => {
11
+ it('should return false for a name that is unavailable', async () => {
12
+ const result = await ensInstance.getAvailable('test123.eth')
13
+ expect(typeof result).toBe('boolean')
14
+ expect(result).toBe(false)
15
+ })
16
+ it('should return true for a name that is available', async () => {
17
+ const result = await ensInstance.getAvailable('available-name.eth')
18
+ expect(typeof result).toBe('boolean')
19
+ expect(result).toBe(true)
20
+ })
21
+ })
@@ -0,0 +1,37 @@
1
+ import { ENSArgs } from '..'
2
+ import { labelhash } from '../utils/labels'
3
+
4
+ const raw = async ({ contracts }: ENSArgs<'contracts'>, name: string) => {
5
+ const baseRegistrar = await contracts?.getBaseRegistrar()!
6
+
7
+ const labels = name.split('.')
8
+ if (labels.length !== 2 || labels[1] !== 'eth') {
9
+ throw new Error('Currently only .eth names can be checked for availability')
10
+ }
11
+
12
+ return {
13
+ to: baseRegistrar.address,
14
+ data: baseRegistrar.interface.encodeFunctionData('available', [
15
+ labelhash(labels[0]),
16
+ ]),
17
+ }
18
+ }
19
+
20
+ const decode = async ({ contracts }: ENSArgs<'contracts'>, data: string) => {
21
+ if (data === null) return
22
+ const baseRegistrar = await contracts?.getBaseRegistrar()!
23
+ try {
24
+ const result = baseRegistrar.interface.decodeFunctionResult(
25
+ 'available',
26
+ data,
27
+ )
28
+ return result['0'] as boolean
29
+ } catch {
30
+ return
31
+ }
32
+ }
33
+
34
+ export default {
35
+ raw,
36
+ decode,
37
+ }
@@ -1,5 +1,4 @@
1
1
  import nock from 'nock'
2
- import fetch from 'node-fetch'
3
2
  import getDNSOwner, { encodeURLParams } from './getDNSOwner'
4
3
 
5
4
  function hexDecode(string: string) {
@@ -24,12 +23,6 @@ describe('encodeURLParams', () => {
24
23
  })
25
24
 
26
25
  describe('getDNSOwner', () => {
27
- beforeAll(() => {
28
- global.fetch = fetch as any
29
- })
30
- afterAll(() => {
31
- global.fetch = undefined as any
32
- })
33
26
  it('should return the address from the result of a dnsQuery', async () => {
34
27
  const dnsName = 'brantly.xyz'
35
28
 
@@ -1,4 +1,5 @@
1
1
  import * as packet from 'dns-packet'
2
+ import { _fetchData } from 'ethers/lib/utils'
2
3
 
3
4
  export function encodeURLParams(p: { [key: string]: string }): string {
4
5
  return Object.entries(p)
@@ -7,15 +8,16 @@ export function encodeURLParams(p: { [key: string]: string }): string {
7
8
  }
8
9
 
9
10
  export const getDNS = async (q: packet.Packet): Promise<packet.Packet> => {
10
- const response = await global.fetch(
11
- `https://cloudflare-dns.com/dns-query?${encodeURLParams({
12
- ct: 'application/dns-udpwireformat',
13
- dns: packet.encode(q)?.toString('base64'),
14
- ts: Date.now().toString(),
15
- })}`,
11
+ const url = `https://cloudflare-dns.com/dns-query?${encodeURLParams({
12
+ ct: 'application/dns-udpwireformat',
13
+ dns: packet.encode(q)?.toString('base64'),
14
+ ts: Date.now().toString(),
15
+ })}`
16
+ const response = await _fetchData(url, undefined)
17
+ const arrayBuffer = response.buffer.slice(
18
+ response.byteOffset,
19
+ response.byteLength + response.byteOffset,
16
20
  )
17
- const arrayBuffer = await response.arrayBuffer()
18
- // @ts-ignore:next-line
19
21
  const fromArrayBuffer = Buffer.from(arrayBuffer)
20
22
  return packet.decode(fromArrayBuffer)
21
23
  }
@@ -87,7 +87,7 @@ export const _getText = {
87
87
  if (!response) {
88
88
  return
89
89
  }
90
- return response
90
+ return response as string
91
91
  },
92
92
  }
93
93
 
@@ -8,9 +8,11 @@ export default async function (
8
8
  {
9
9
  newOwner,
10
10
  contract,
11
+ reclaim,
11
12
  }: {
12
13
  newOwner: string
13
14
  contract: 'registry' | 'nameWrapper' | 'baseRegistrar'
15
+ reclaim?: boolean
14
16
  },
15
17
  ) {
16
18
  const address = await signer.getAddress()
@@ -28,13 +30,16 @@ export default async function (
28
30
  if (labels.length > 2 || labels[labels.length - 1] !== 'eth') {
29
31
  throw new Error('Invalid name for baseRegistrar')
30
32
  }
33
+ const tokenId = ethers.utils.solidityKeccak256(['string'], [labels[0]])
34
+
35
+ // reclaim if sending manager on unwrapped name
36
+ if (reclaim) {
37
+ return baseRegistrar.populateTransaction.reclaim(tokenId, newOwner)
38
+ }
39
+
31
40
  return baseRegistrar.populateTransaction[
32
41
  'safeTransferFrom(address,address,uint256)'
33
- ](
34
- address,
35
- newOwner,
36
- ethers.utils.solidityKeccak256(['string'], [labels[0]]),
37
- )
42
+ ](address, newOwner, tokenId)
38
43
  }
39
44
  case 'nameWrapper': {
40
45
  const nameWrapper = (await contracts?.getNameWrapper())!.connect(signer)
package/src/index.ts CHANGED
@@ -13,6 +13,7 @@ import type burnFuses from './functions/burnFuses'
13
13
  import type commitName from './functions/commitName'
14
14
  import type createSubname from './functions/createSubname'
15
15
  import type deleteSubname from './functions/deleteSubname'
16
+ import type getAvailable from './functions/getAvailable'
16
17
  import type getDNSOwner from './functions/getDNSOwner'
17
18
  import type getExpiry from './functions/getExpiry'
18
19
  import type { getHistory } from './functions/getHistory'
@@ -33,7 +34,7 @@ import type {
33
34
  } from './functions/getSpecificRecord'
34
35
  import type getSubnames from './functions/getSubnames'
35
36
  import type getWrapperData from './functions/getWrapperData'
36
- import importDNSSECName from './functions/importDNSSECName'
37
+ import type importDNSSECName from './functions/importDNSSECName'
37
38
  import type registerName from './functions/registerName'
38
39
  import type {
39
40
  // eslint-disable-next-line import/no-named-default
@@ -165,6 +166,20 @@ export interface GenericGeneratedRawFunction
165
166
  extends Function,
166
167
  RawFunctionWithBatch {}
167
168
 
169
+ interface GeneratedBatchFunction<F extends RawFunction>
170
+ extends Function,
171
+ RawFunction {
172
+ <I extends BatchFunctionResult<RawFunction>[]>(...args: I): Promise<
173
+ | {
174
+ [N in keyof I]: I[N] extends BatchFunctionResult<infer U>
175
+ ? Awaited<ReturnType<U['decode']>>
176
+ : never
177
+ }
178
+ | undefined
179
+ >
180
+ batch: BatchFunction<F>
181
+ }
182
+
168
183
  export class ENS {
169
184
  [x: string]: any
170
185
 
@@ -417,7 +432,7 @@ export class ENS {
417
432
  'initialGetters',
418
433
  ['multicallWrapper'],
419
434
  'batch',
420
- )
435
+ ) as GeneratedBatchFunction<typeof batch>
421
436
 
422
437
  public getProfile = this.generateFunction<typeof getProfile>(
423
438
  'initialGetters',
@@ -533,6 +548,11 @@ export class ENS {
533
548
  [],
534
549
  )
535
550
 
551
+ public getAvailable = this.generateRawFunction<typeof getAvailable>(
552
+ 'getAvailable',
553
+ ['contracts'],
554
+ )
555
+
536
556
  public universalWrapper = this.generateRawFunction<typeof universalWrapper>(
537
557
  'initialGetters',
538
558
  ['contracts'],