@ensdomains/ensjs 3.0.0-alpha.13 → 3.0.0-alpha.16

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.
@@ -1,32 +1,21 @@
1
1
  import { BigNumber } from 'ethers';
2
- import { testable as fuseEnums } from '../utils/fuses';
2
+ import { fuseEnum } from '../utils/fuses';
3
3
  import { namehash } from '../utils/normalise';
4
- const NameSafety = [
5
- 'Safe',
6
- 'RegistrantNotWrapped',
7
- 'ControllerNotWrapped',
8
- 'SubdomainReplacementAllowed',
9
- 'Expired',
10
- ];
11
4
  const raw = async ({ contracts }, name) => {
12
5
  const nameWrapper = await contracts?.getNameWrapper();
13
6
  return {
14
7
  to: nameWrapper.address,
15
- data: nameWrapper.interface.encodeFunctionData('getFuses', [
16
- namehash(name),
17
- ]),
8
+ data: nameWrapper.interface.encodeFunctionData('getData', [namehash(name)]),
18
9
  };
19
10
  };
20
- const decode = async ({ contracts }, data, name) => {
11
+ const decode = async ({ contracts }, data) => {
21
12
  const nameWrapper = await contracts?.getNameWrapper();
22
13
  try {
23
- const [_fuses, expiry] = nameWrapper.interface.decodeFunctionResult('getFuses', data);
14
+ const { owner, fuses: _fuses, expiry, } = nameWrapper.interface.decodeFunctionResult('getData', data);
24
15
  const fuses = BigNumber.from(_fuses);
25
- const fuseObj = Object.fromEntries(Object.keys(fuseEnums).map((fuseEnum) => [
26
- fuseEnum
27
- .toLowerCase()
28
- .replace(/([-_][a-z])/g, (group) => group.toUpperCase().replace('-', '').replace('_', '')),
29
- fuses.and(fuseEnums[fuseEnum]).gt(0),
16
+ const fuseObj = Object.fromEntries(Object.keys(fuseEnum).map((fuse) => [
17
+ fuse,
18
+ fuses.and(fuseEnum[fuse]).gt(0),
30
19
  ]));
31
20
  if (fuses.eq(0)) {
32
21
  fuseObj.canDoEverything = true;
@@ -39,9 +28,11 @@ const decode = async ({ contracts }, data, name) => {
39
28
  fuseObj,
40
29
  expiryDate,
41
30
  rawFuses: fuses,
31
+ owner,
42
32
  };
43
33
  }
44
- catch {
34
+ catch (e) {
35
+ console.error('Error decoding fuses data: ', e);
45
36
  return;
46
37
  }
47
38
  };
@@ -52,21 +52,21 @@ export declare const getText: {
52
52
  decode: ({ contracts, universalWrapper }: ENSArgs<'contracts' | 'universalWrapper'>, data: string) => Promise<any>;
53
53
  };
54
54
  export declare const _getAddr: {
55
- raw: ({ contracts }: ENSArgs<'contracts'>, name: string, coinType?: string | number | undefined, bypassFormat?: boolean | undefined) => Promise<{
55
+ raw: ({ contracts }: ENSArgs<'contracts'>, name: string, coinType?: string | number, bypassFormat?: boolean) => Promise<{
56
56
  to: string;
57
57
  data: string;
58
58
  }>;
59
- decode: ({ contracts }: ENSArgs<'contracts'>, data: string, _name: string, coinType?: string | number | undefined) => Promise<string | {
59
+ decode: ({ contracts }: ENSArgs<'contracts'>, data: string, _name: string, coinType?: string | number) => Promise<string | {
60
60
  coin: string;
61
61
  addr: string;
62
62
  } | undefined>;
63
63
  };
64
64
  export declare const getAddr: {
65
- raw: ({ contracts, universalWrapper }: ENSArgs<'contracts' | 'universalWrapper'>, name: string, coinType?: string | number | undefined) => Promise<{
65
+ raw: ({ contracts, universalWrapper }: ENSArgs<'contracts' | 'universalWrapper'>, name: string, coinType?: string | number) => Promise<{
66
66
  to: string;
67
67
  data: string;
68
68
  }>;
69
- decode: ({ contracts, universalWrapper }: ENSArgs<'contracts' | 'universalWrapper'>, data: string, _name: string, coinType?: string | number | undefined) => Promise<string | {
69
+ decode: ({ contracts, universalWrapper }: ENSArgs<'contracts' | 'universalWrapper'>, data: string, _name: string, coinType?: string | number) => Promise<string | {
70
70
  coin: string;
71
71
  addr: string;
72
72
  } | undefined>;
@@ -75,6 +75,16 @@ export declare class ENS {
75
75
  contracts?: ContractManager;
76
76
  getContractAddress: (networkId: SupportedNetworkId) => import("./contracts/getContractAddress").ContractAddressFetch;
77
77
  gqlInstance: GqlManager;
78
+ fuses: {
79
+ CAN_DO_EVERYTHING: number;
80
+ CANNOT_UNWRAP: 1;
81
+ CANNOT_BURN_FUSES: 2;
82
+ CANNOT_TRANSFER: 4;
83
+ CANNOT_SET_RESOLVER: 8;
84
+ CANNOT_SET_TTL: 16;
85
+ CANNOT_CREATE_SUBDOMAIN: 32;
86
+ PARENT_CANNOT_CONTROL: 64;
87
+ };
78
88
  constructor(options?: ENSOptions);
79
89
  /**
80
90
  * Checks for an initial provider and if it exists, sets it as the provider
@@ -229,12 +239,13 @@ export declare class ENS {
229
239
  to: string;
230
240
  data: string;
231
241
  }>;
232
- decode: ({ contracts }: ENSArgs<"contracts">, data: string, name: string) => Promise<{
242
+ decode: ({ contracts }: ENSArgs<"contracts">, data: string) => Promise<{
233
243
  fuseObj: {
234
244
  [k: string]: boolean;
235
245
  };
236
246
  expiryDate: Date;
237
247
  rawFuses: ethers.BigNumber;
248
+ owner: any;
238
249
  } | undefined>;
239
250
  }>;
240
251
  getHistory: (name: string) => Promise<{
package/dist/esm/index.js CHANGED
@@ -3,6 +3,7 @@ import { getContractAddress as _getContractAddress } from './contracts/getContra
3
3
  import GqlManager from './GqlManager';
4
4
  import singleCall from './utils/singleCall';
5
5
  import writeTx from './utils/writeTx';
6
+ import fuseEnum from './utils/fuses';
6
7
  const graphURIEndpoints = {
7
8
  1: 'https://api.thegraph.com/subgraphs/name/ensdomains/ens',
8
9
  3: 'https://api.thegraph.com/subgraphs/name/ensdomains/ensropsten',
@@ -13,6 +14,7 @@ export class ENS {
13
14
  constructor(options) {
14
15
  this.getContractAddress = _getContractAddress;
15
16
  this.gqlInstance = new GqlManager();
17
+ this.fuses = fuseEnum;
16
18
  /**
17
19
  * Checks for an initial provider and if it exists, sets it as the provider
18
20
  * @returns {Promise<void>} - A promise that resolves when the provider is checked, and set if needed
@@ -1,20 +1,21 @@
1
- export declare const testable: {
2
- CANNOT_UNWRAP: number;
3
- CANNOT_BURN_FUSES: number;
4
- CANNOT_TRANSFER: number;
5
- CANNOT_SET_RESOLVER: number;
6
- CANNOT_SET_TTL: number;
7
- CANNOT_CREATE_SUBDOMAIN: number;
8
- PARENT_CANNOT_CONTROL: number;
1
+ export declare const fuseEnum: {
2
+ readonly CANNOT_UNWRAP: 1;
3
+ readonly CANNOT_BURN_FUSES: 2;
4
+ readonly CANNOT_TRANSFER: 4;
5
+ readonly CANNOT_SET_RESOLVER: 8;
6
+ readonly CANNOT_SET_TTL: 16;
7
+ readonly CANNOT_CREATE_SUBDOMAIN: 32;
8
+ readonly PARENT_CANNOT_CONTROL: 64;
9
9
  };
10
- declare const _default: {
10
+ export declare const unnamedFuses: readonly [128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296];
11
+ declare const fullFuseEnum: {
11
12
  CAN_DO_EVERYTHING: number;
12
- CANNOT_UNWRAP: number;
13
- CANNOT_BURN_FUSES: number;
14
- CANNOT_TRANSFER: number;
15
- CANNOT_SET_RESOLVER: number;
16
- CANNOT_SET_TTL: number;
17
- CANNOT_CREATE_SUBDOMAIN: number;
18
- PARENT_CANNOT_CONTROL: number;
13
+ CANNOT_UNWRAP: 1;
14
+ CANNOT_BURN_FUSES: 2;
15
+ CANNOT_TRANSFER: 4;
16
+ CANNOT_SET_RESOLVER: 8;
17
+ CANNOT_SET_TTL: 16;
18
+ CANNOT_CREATE_SUBDOMAIN: 32;
19
+ PARENT_CANNOT_CONTROL: 64;
19
20
  };
20
- export default _default;
21
+ export default fullFuseEnum;
@@ -6,7 +6,7 @@ const CANNOT_SET_TTL = 16;
6
6
  const CANNOT_CREATE_SUBDOMAIN = 32;
7
7
  const PARENT_CANNOT_CONTROL = 64;
8
8
  const CAN_DO_EVERYTHING = 0;
9
- export const testable = {
9
+ export const fuseEnum = {
10
10
  CANNOT_UNWRAP,
11
11
  CANNOT_BURN_FUSES,
12
12
  CANNOT_TRANSFER,
@@ -15,7 +15,13 @@ export const testable = {
15
15
  CANNOT_CREATE_SUBDOMAIN,
16
16
  PARENT_CANNOT_CONTROL,
17
17
  };
18
- export default {
19
- ...testable,
18
+ export const unnamedFuses = [
19
+ 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144,
20
+ 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864,
21
+ 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296,
22
+ ];
23
+ const fullFuseEnum = {
24
+ ...fuseEnum,
20
25
  CAN_DO_EVERYTHING,
21
26
  };
27
+ export default fullFuseEnum;
@@ -2,4 +2,4 @@ import { BigNumber } from 'ethers';
2
2
  import { ENSArgs } from '..';
3
3
  export declare type Expiry = string | number | Date | BigNumber;
4
4
  export declare const MAX_EXPIRY: BigNumber;
5
- export declare const makeExpiry: ({ getExpiry }: ENSArgs<'getExpiry'>, name: string, expiry?: Expiry | undefined) => Promise<BigNumber>;
5
+ export declare const makeExpiry: ({ getExpiry }: ENSArgs<'getExpiry'>, name: string, expiry?: Expiry) => Promise<BigNumber>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ensdomains/ensjs",
3
- "version": "3.0.0-alpha.13",
3
+ "version": "3.0.0-alpha.16",
4
4
  "description": "ENS javascript library for contract interaction",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "types": "./dist/cjs/index.d.ts",
@@ -1,7 +1,7 @@
1
+ import { parse, print, visit } from 'graphql'
1
2
  import traverse from 'traverse'
2
- import { visit, parse } from 'graphql'
3
3
 
4
- import { requestMiddleware, responseMiddleware, enter } from './GqlManager'
4
+ import { requestMiddleware, responseMiddleware } from './GqlManager'
5
5
  import { namehash } from './utils/normalise'
6
6
 
7
7
  describe('GqlManager', () => {
@@ -153,8 +153,9 @@ query getNames($id: ID!, $expiryDate: Int) {
153
153
 
154
154
  describe('requestMiddleware', () => {
155
155
  it('should add id to a SelectionSet if name is present and id is not', () => {
156
- const result = requestMiddleware(visit, parse)(mockRequest)
157
- expect(result.body.includes('id')).toBe(true)
156
+ const result = requestMiddleware(visit, parse, print)(mockRequest)
157
+ const body = result.body as string
158
+ expect(body.match(/domain.*id.*id.*domains.*id.*id/)?.length).toBe(1)
158
159
  })
159
160
  })
160
161
  describe('responseMiddleware', () => {
package/src/GqlManager.ts CHANGED
@@ -1,10 +1,17 @@
1
- // @ts-nocheck
1
+ import type {
2
+ Kind,
3
+ parse as Parse,
4
+ print as Print,
5
+ SelectionSetNode,
6
+ visit as Visit,
7
+ } from 'graphql'
8
+ import type Traverse from 'traverse'
2
9
  import { namehash } from './utils/normalise'
3
10
 
4
11
  const generateSelection = (selection: any) => ({
5
- kind: 'Field',
12
+ kind: 'Field' as Kind.FIELD,
6
13
  name: {
7
- kind: 'Name',
14
+ kind: 'Name' as Kind.NAME,
8
15
  value: selection,
9
16
  },
10
17
  arguments: [],
@@ -13,47 +20,58 @@ const generateSelection = (selection: any) => ({
13
20
  selectionSet: undefined,
14
21
  })
15
22
 
16
- export const enter = (node: any) => {
17
- if (node.kind === 'SelectionSet') {
18
- const id = node.selections.find((x) => x.name && x.name.value === 'id')
19
- const name = node.selections.find((x) => x.name && x.name.value === 'name')
23
+ export const enter = (node: SelectionSetNode) => {
24
+ let hasName = false
25
+ let hasId = false
20
26
 
21
- if (!id && name) {
22
- node.selections = [...node.selections, generateSelection('id')]
23
- return node
27
+ for (const selection of node.selections) {
28
+ if ('name' in selection) {
29
+ if (selection.name.value === 'name') hasName = true
30
+ else if (selection.name.value === 'id') hasId = true
24
31
  }
25
32
  }
33
+
34
+ if (hasName && !hasId) {
35
+ node.selections = [...node.selections, generateSelection('id')]
36
+ return node
37
+ }
26
38
  }
27
39
 
28
- export const requestMiddleware = (visit, parse) => (request: any) => {
29
- const requestBody = JSON.parse(request.body)
30
- const rawQuery = requestBody.query
31
- const parsedQuery = parse(rawQuery)
32
- const updatedQuery = visit(parsedQuery, { enter })
33
- const updatedBody = { ...requestBody, query: updatedQuery.loc.source.body }
40
+ export const requestMiddleware =
41
+ (visit: typeof Visit, parse: typeof Parse, print: typeof Print) =>
42
+ (request: any) => {
43
+ const requestBody = JSON.parse(request.body)
44
+ const rawQuery = requestBody.query
45
+ const parsedQuery = parse(rawQuery)
46
+ const updatedQuery = visit(parsedQuery, {
47
+ SelectionSet: {
48
+ enter,
49
+ },
50
+ })
34
51
 
35
- return {
36
- ...request,
37
- body: JSON.stringify(updatedBody),
52
+ return {
53
+ ...request,
54
+ body: JSON.stringify({ ...requestBody, query: print(updatedQuery) }),
55
+ }
38
56
  }
39
- }
40
57
 
41
- export const responseMiddleware = (traverse) => (response: any) => {
42
- traverse(response).forEach(function (responseItem: any) {
43
- if (responseItem instanceof Object && responseItem.name) {
44
- //Name already in hashed form
45
- if (responseItem.name && responseItem.name.includes('[')) {
46
- return
47
- }
58
+ export const responseMiddleware =
59
+ (traverse: typeof Traverse) => (response: any) => {
60
+ traverse(response).forEach(function (responseItem: any) {
61
+ if (responseItem instanceof Object && responseItem.name) {
62
+ //Name already in hashed form
63
+ if (responseItem.name && responseItem.name.includes('[')) {
64
+ return
65
+ }
48
66
 
49
- const hashedName = namehash(responseItem.name)
50
- if (responseItem.id !== hashedName) {
51
- this.update({ ...responseItem, name: hashedName, invalidName: true })
67
+ const hashedName = namehash(responseItem.name)
68
+ if (responseItem.id !== hashedName) {
69
+ this.update({ ...responseItem, name: hashedName, invalidName: true })
70
+ }
52
71
  }
53
- }
54
- })
55
- return response
56
- }
72
+ })
73
+ return response
74
+ }
57
75
 
58
76
  export default class GqlManager {
59
77
  public gql: any = () => null
@@ -61,14 +79,14 @@ export default class GqlManager {
61
79
 
62
80
  public setUrl = async (url: string | null) => {
63
81
  if (url) {
64
- const [imported, traverse, { visit, parse }] = await Promise.all([
82
+ const [imported, traverse, { visit, parse, print }] = await Promise.all([
65
83
  import('graphql-request'),
66
84
  import('traverse'),
67
85
  import('graphql/language'),
68
86
  ])
69
87
 
70
88
  this.client = new imported.GraphQLClient(url, {
71
- requestMiddleware: requestMiddleware(visit, parse),
89
+ requestMiddleware: requestMiddleware(visit, parse, print),
72
90
  responseMiddleware: responseMiddleware(traverse.default),
73
91
  })
74
92
  this.gql = imported.gql
@@ -21,20 +21,128 @@ describe('burnFuses', () => {
21
21
  beforeEach(async () => {
22
22
  await revert()
23
23
  })
24
- it('should return a burnFuses transaction and succeed', async () => {
25
- const tx = await ENSInstance.burnFuses('wrapped.eth', {
26
- fusesToBurn: {
27
- cannotUnwrap: true,
28
- cannotCreateSubdomain: true,
29
- cannotSetTtl: true,
30
- },
31
- addressOrIndex: accounts[1],
32
- })
33
- expect(tx).toBeTruthy()
34
- await tx.wait()
35
-
36
- const nameWrapper = await ENSInstance.contracts!.getNameWrapper()!
37
- const [fuses] = await nameWrapper.getFuses(namehash('wrapped.eth'))
38
- expect(fuses).toBe(113)
24
+ describe('Array', () => {
25
+ it('should return a burnFuses transaction from a named fuse array and succeed', async () => {
26
+ const tx = await ENSInstance.burnFuses('wrapped.eth', {
27
+ namedFusesToBurn: [
28
+ 'CANNOT_UNWRAP',
29
+ 'CANNOT_CREATE_SUBDOMAIN',
30
+ 'CANNOT_SET_TTL',
31
+ ],
32
+ addressOrIndex: accounts[1],
33
+ })
34
+ expect(tx).toBeTruthy()
35
+ await tx.wait()
36
+
37
+ const nameWrapper = await ENSInstance.contracts!.getNameWrapper()!
38
+ const [fuses] = await nameWrapper.getFuses(namehash('wrapped.eth'))
39
+ expect(fuses).toBe(113)
40
+ })
41
+ it('should return a burnFuses transaction from an unnamed fuse array and succeed', async () => {
42
+ const tx0 = await ENSInstance.burnFuses('wrapped.eth', {
43
+ namedFusesToBurn: ['CANNOT_UNWRAP'],
44
+ addressOrIndex: accounts[1],
45
+ })
46
+ expect(tx0).toBeTruthy()
47
+ await tx0.wait()
48
+
49
+ const tx = await ENSInstance.burnFuses('wrapped.eth', {
50
+ unnamedFusesToBurn: [128, 256, 512],
51
+ addressOrIndex: accounts[1],
52
+ })
53
+ expect(tx).toBeTruthy()
54
+ await tx.wait()
55
+
56
+ const nameWrapper = await ENSInstance.contracts!.getNameWrapper()!
57
+ const [fuses] = await nameWrapper.getFuses(namehash('wrapped.eth'))
58
+ expect(fuses).toBe(961)
59
+ })
60
+ it('should return a burnFuses transaction from both an unnamed and named fuse array and succeed', async () => {
61
+ const tx = await ENSInstance.burnFuses('wrapped.eth', {
62
+ namedFusesToBurn: [
63
+ 'CANNOT_UNWRAP',
64
+ 'CANNOT_CREATE_SUBDOMAIN',
65
+ 'CANNOT_SET_TTL',
66
+ ],
67
+ unnamedFusesToBurn: [128, 256, 512],
68
+ addressOrIndex: accounts[1],
69
+ })
70
+ expect(tx).toBeTruthy()
71
+ await tx.wait()
72
+
73
+ const nameWrapper = await ENSInstance.contracts!.getNameWrapper()!
74
+ const [fuses] = await nameWrapper.getFuses(namehash('wrapped.eth'))
75
+ expect(fuses).toBe(1009)
76
+ })
77
+ it('should throw an error when trying to burn a named fuse in an unnamed fuse array', async () => {
78
+ try {
79
+ await ENSInstance.burnFuses('wrapped.eth', {
80
+ unnamedFusesToBurn: [64] as any,
81
+ })
82
+ expect(false).toBeTruthy()
83
+ } catch (e: any) {
84
+ expect(e.message).toBe(
85
+ '64 is not a valid unnamed fuse. If you are trying to burn a named fuse, use the namedFusesToBurn property.',
86
+ )
87
+ }
88
+ })
89
+ it('should throw an error when trying to burn an unnamed fuse in a named fuse array', async () => {
90
+ try {
91
+ await ENSInstance.burnFuses('wrapped.eth', {
92
+ namedFusesToBurn: ['COOL_SWAG_FUSE'] as any,
93
+ })
94
+ expect(false).toBeTruthy()
95
+ } catch (e: any) {
96
+ expect(e.message).toBe('COOL_SWAG_FUSE is not a valid named fuse.')
97
+ }
98
+ })
99
+ })
100
+ describe('Number', () => {
101
+ it('should return a burnFuses transaction from a number and succeed', async () => {
102
+ const tx = await ENSInstance.burnFuses('wrapped.eth', {
103
+ fuseNumberToBurn: 49,
104
+ addressOrIndex: accounts[1],
105
+ })
106
+ expect(tx).toBeTruthy()
107
+ await tx.wait()
108
+
109
+ const nameWrapper = await ENSInstance.contracts!.getNameWrapper()!
110
+ const [fuses] = await nameWrapper.getFuses(namehash('wrapped.eth'))
111
+ expect(fuses).toBe(113)
112
+ })
113
+ it('should throw an error if the number is too high', async () => {
114
+ try {
115
+ await ENSInstance.burnFuses('wrapped.eth', {
116
+ fuseNumberToBurn: 4294967297,
117
+ })
118
+ expect(false).toBeTruthy()
119
+ } catch (e: any) {
120
+ expect(e.message).toBe(
121
+ 'Fuse number must be limited to uint32, 4294967297 was too high.',
122
+ )
123
+ }
124
+ })
125
+ it('should throw an error if the number is too low', async () => {
126
+ try {
127
+ await ENSInstance.burnFuses('wrapped.eth', {
128
+ fuseNumberToBurn: -1,
129
+ })
130
+ expect(false).toBeTruthy()
131
+ } catch (e: any) {
132
+ expect(e.message).toBe(
133
+ 'Fuse number must be limited to uint32, -1 was too low.',
134
+ )
135
+ }
136
+ })
137
+ it('should throw an error if the number is not an integer', async () => {
138
+ try {
139
+ await ENSInstance.burnFuses('wrapped.eth', {
140
+ fuseNumberToBurn: 7.5,
141
+ })
142
+ expect(false).toBeTruthy()
143
+ } catch (e: any) {
144
+ expect(e.message).toBe('Fuse number must be an integer, 7.5 was not.')
145
+ }
146
+ })
39
147
  })
40
148
  })
@@ -1,21 +1,112 @@
1
1
  import { ENSArgs } from '..'
2
- import { FuseOptions } from '../@types/FuseOptions'
3
- import generateFuseInput from '../utils/generateFuseInput'
2
+ import { fuseEnum, unnamedFuses } from '../utils/fuses'
4
3
  import { namehash } from '../utils/normalise'
5
4
 
6
- export default async function (
5
+ type FuseObj = typeof fuseEnum
6
+ type UnnamedFuseType = typeof unnamedFuses
7
+ type Fuse = keyof FuseObj
8
+ type NamedFuseValues = FuseObj[Fuse]
9
+ type UnnamedFuseValues = UnnamedFuseType[number]
10
+
11
+ // We need this type so that the following type isn't infinite. This type limits the max length of the fuse array to 7.
12
+ type FuseArrayPossibilities =
13
+ | [Fuse]
14
+ | [Fuse, Fuse]
15
+ | [Fuse, Fuse, Fuse]
16
+ | [Fuse, Fuse, Fuse, Fuse]
17
+ | [Fuse, Fuse, Fuse, Fuse, Fuse]
18
+ | [Fuse, Fuse, Fuse, Fuse, Fuse, Fuse]
19
+ | [Fuse, Fuse, Fuse, Fuse, Fuse, Fuse, Fuse]
20
+
21
+ /**
22
+ * This type creates a type error if there are any duplicate fuses.
23
+ * It effectively works like a reduce function, starting with 0 included types, adding a type each time, and then checking for duplicates.
24
+ *
25
+ * @template A The array to check for duplicates.
26
+ * @template B The union of all checked existing types.
27
+ */
28
+ // CLAUSE A: This extension unwraps the type as a fuse tuple.
29
+ type FusesWithoutDuplicates<A, B = never> = A extends FuseArrayPossibilities
30
+ ? // CLAUSE A > TRUE: CLAUSE B: Pick out the first item in the current array, separating the current item from the rest.
31
+ A extends [infer Head, ...infer Tail]
32
+ ? // CLAUSE B > TRUE: CLAUSE C: Check if the current item is a duplicate based on the input union.
33
+ Head extends B
34
+ ? // CLAUSE C > TRUE: Duplicate found, return an empty array to throw a type error.
35
+ []
36
+ : // CLAUSE C > FALSE: Return a new array to continue the recursion, adds the current item type to the union.
37
+ [Head, ...FusesWithoutDuplicates<Tail, Head | B>]
38
+ : // CLAUSE B > FALSE: Return the input array as there is no more array elements to check.
39
+ A
40
+ : // CLAUSE A > FALSE: Return an empty array as it isn't a fuse tuple.
41
+ []
42
+
43
+ type FusePropsNamedArray<A extends FuseArrayPossibilities> = {
44
+ namedFusesToBurn: FusesWithoutDuplicates<A>
45
+ }
46
+
47
+ type FusePropsUnnamedArray = {
48
+ unnamedFusesToBurn: UnnamedFuseValues[]
49
+ }
50
+
51
+ type FusePropsNumber = {
52
+ fuseNumberToBurn: number
53
+ }
54
+
55
+ type FuseProps<A extends FuseArrayPossibilities> =
56
+ | (Partial<FusePropsNamedArray<A>> & FusePropsUnnamedArray)
57
+ | (FusePropsNamedArray<A> & Partial<FusePropsUnnamedArray>)
58
+ | FusePropsNumber
59
+
60
+ export default async function <A extends FuseArrayPossibilities>(
7
61
  { contracts, signer }: ENSArgs<'contracts' | 'signer'>,
8
62
  name: string,
9
- {
10
- fusesToBurn,
11
- }: {
12
- fusesToBurn: FuseOptions
13
- },
63
+ props: FuseProps<A>,
14
64
  ) {
65
+ const isNumber = 'fuseNumberToBurn' in props
66
+ const hasNamedArray = 'namedFusesToBurn' in props
67
+ const hasUnnamedArray = 'unnamedFusesToBurn' in props
68
+
69
+ let encodedFuses: number = 0
70
+
71
+ if (isNumber) {
72
+ if (props.fuseNumberToBurn > 2 ** 32 || props.fuseNumberToBurn < 1) {
73
+ throw new Error(
74
+ `Fuse number must be limited to uint32, ${
75
+ props.fuseNumberToBurn
76
+ } was too ${props.fuseNumberToBurn < 1 ? 'low' : 'high'}.`,
77
+ )
78
+ } else if (props.fuseNumberToBurn % 1 !== 0) {
79
+ throw new Error(
80
+ `Fuse number must be an integer, ${props.fuseNumberToBurn} was not.`,
81
+ )
82
+ }
83
+ encodedFuses = props.fuseNumberToBurn
84
+ } else {
85
+ if (!hasNamedArray && !hasUnnamedArray) {
86
+ throw new Error('Please provide fuses to burn')
87
+ }
88
+ if (hasNamedArray) {
89
+ for (const fuse of props.namedFusesToBurn!) {
90
+ if (!(fuse in fuseEnum)) {
91
+ throw new Error(`${fuse} is not a valid named fuse.`)
92
+ }
93
+ encodedFuses |= fuseEnum[fuse]
94
+ }
95
+ }
96
+ if (hasUnnamedArray) {
97
+ for (const fuse of props.unnamedFusesToBurn!) {
98
+ if (!unnamedFuses.includes(fuse)) {
99
+ throw new Error(
100
+ `${fuse} is not a valid unnamed fuse. If you are trying to burn a named fuse, use the namedFusesToBurn property.`,
101
+ )
102
+ }
103
+ encodedFuses |= fuse
104
+ }
105
+ }
106
+ }
107
+
15
108
  const nameWrapper = (await contracts?.getNameWrapper()!).connect(signer)
16
109
  const hash = namehash(name)
17
110
 
18
- const encodedFuses = generateFuseInput(fusesToBurn)
19
-
20
111
  return nameWrapper.populateTransaction.setFuses(hash, encodedFuses)
21
112
  }