@ar.io/sdk 2.1.0-alpha.1 → 2.1.0-alpha.11

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.
Files changed (43) hide show
  1. package/README.md +103 -22
  2. package/bundles/web.bundle.min.js +105 -105
  3. package/lib/cjs/common/ant-registry.js +87 -0
  4. package/lib/cjs/common/ant.js +3 -9
  5. package/lib/cjs/common/contracts/ao-process.js +2 -19
  6. package/lib/cjs/common/index.js +1 -0
  7. package/lib/cjs/common/io.js +14 -12
  8. package/lib/cjs/constants.js +2 -1
  9. package/lib/cjs/io.js +2 -1
  10. package/lib/cjs/types.js +0 -1
  11. package/lib/cjs/utils/ao.js +43 -3
  12. package/lib/cjs/utils/json.js +3 -2
  13. package/lib/cjs/utils/processes.js +24 -43
  14. package/lib/cjs/version.js +1 -1
  15. package/lib/esm/common/ant-registry.js +81 -0
  16. package/lib/esm/common/ant.js +3 -9
  17. package/lib/esm/common/contracts/ao-process.js +2 -19
  18. package/lib/esm/common/index.js +1 -0
  19. package/lib/esm/common/io.js +14 -12
  20. package/lib/esm/constants.js +1 -0
  21. package/lib/esm/io.js +2 -1
  22. package/lib/esm/types.js +0 -1
  23. package/lib/esm/utils/ao.js +43 -4
  24. package/lib/esm/utils/json.js +1 -0
  25. package/lib/esm/utils/processes.js +24 -43
  26. package/lib/esm/version.js +1 -1
  27. package/lib/types/common/ant-registry.d.ts +26 -0
  28. package/lib/types/common/ant.d.ts +4 -4
  29. package/lib/types/common/contracts/ao-process.d.ts +4 -17
  30. package/lib/types/common/index.d.ts +1 -0
  31. package/lib/types/common/io.d.ts +9 -12
  32. package/lib/types/common.d.ts +2 -25
  33. package/lib/types/constants.d.ts +1 -0
  34. package/lib/types/io.d.ts +180 -116
  35. package/lib/types/token.d.ts +13 -0
  36. package/lib/types/types.d.ts +0 -1
  37. package/lib/types/utils/ao.d.ts +14 -6
  38. package/lib/types/utils/processes.d.ts +11 -5
  39. package/lib/types/version.d.ts +1 -1
  40. package/package.json +8 -3
  41. package/lib/cjs/contract-state.js +0 -68
  42. package/lib/esm/contract-state.js +0 -65
  43. package/lib/types/contract-state.d.ts +0 -201
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AoANTRegistryWriteable = exports.AoANTRegistryReadable = exports.ANTRegistry = void 0;
4
+ /**
5
+ * Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
6
+ *
7
+ * This program is free software: you can redistribute it and/or modify
8
+ * it under the terms of the GNU Affero General Public License as published by
9
+ * the Free Software Foundation, either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU Affero General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Affero General Public License
18
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
19
+ */
20
+ const constants_js_1 = require("../constants.js");
21
+ const types_js_1 = require("../types.js");
22
+ const ao_js_1 = require("../utils/ao.js");
23
+ const index_js_1 = require("./index.js");
24
+ class ANTRegistry {
25
+ static init(config) {
26
+ if (config && config.signer) {
27
+ const { signer, ...rest } = config;
28
+ return new AoANTRegistryWriteable({
29
+ ...rest,
30
+ signer,
31
+ });
32
+ }
33
+ return new AoANTRegistryReadable(config);
34
+ }
35
+ }
36
+ exports.ANTRegistry = ANTRegistry;
37
+ class AoANTRegistryReadable {
38
+ process;
39
+ constructor(config) {
40
+ if (config &&
41
+ ((0, types_js_1.isProcessIdConfiguration)(config) || (0, types_js_1.isProcessConfiguration)(config))) {
42
+ if ((0, types_js_1.isProcessConfiguration)(config)) {
43
+ this.process = config.process;
44
+ }
45
+ else if ((0, types_js_1.isProcessIdConfiguration)(config)) {
46
+ this.process = new index_js_1.AOProcess({
47
+ processId: config.processId,
48
+ });
49
+ }
50
+ else {
51
+ throw new index_js_1.InvalidContractConfigurationError();
52
+ }
53
+ }
54
+ else {
55
+ this.process = new index_js_1.AOProcess({
56
+ processId: constants_js_1.ANT_REGISTRY_ID,
57
+ });
58
+ }
59
+ }
60
+ // Should we rename this to "getANTsByAddress"? seems more clear, though not same as handler name
61
+ async accessControlList({ address, }) {
62
+ return this.process.read({
63
+ tags: [
64
+ { name: 'Action', value: 'Access-Control-List' },
65
+ { name: 'Address', value: address },
66
+ ],
67
+ });
68
+ }
69
+ }
70
+ exports.AoANTRegistryReadable = AoANTRegistryReadable;
71
+ class AoANTRegistryWriteable extends AoANTRegistryReadable {
72
+ signer;
73
+ constructor({ signer, ...config }) {
74
+ super(config);
75
+ this.signer = (0, ao_js_1.createAoSigner)(signer);
76
+ }
77
+ async register({ processId, }) {
78
+ return this.process.send({
79
+ tags: [
80
+ { name: 'Action', value: 'Register' },
81
+ { name: 'Process-Id', value: processId },
82
+ ],
83
+ signer: this.signer,
84
+ });
85
+ }
86
+ }
87
+ exports.AoANTRegistryWriteable = AoANTRegistryWriteable;
@@ -18,6 +18,7 @@ exports.AoANTWriteable = exports.AoANTReadable = exports.ANT = void 0;
18
18
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19
19
  */
20
20
  const types_js_1 = require("../types.js");
21
+ const ao_js_1 = require("../utils/ao.js");
21
22
  const index_js_1 = require("./index.js");
22
23
  class ANT {
23
24
  static init({ signer, ...config }) {
@@ -81,7 +82,7 @@ class AoANTReadable {
81
82
  return record;
82
83
  }
83
84
  /**
84
- * @returns {Promise<Record<string, ANTRecord>>} All the undernames managed by the ANT.
85
+ * @returns {Promise<Record<string, AoANTRecord>>} All the undernames managed by the ANT.
85
86
  * @example
86
87
  * Get the current records
87
88
  * ```ts
@@ -186,7 +187,7 @@ class AoANTWriteable extends AoANTReadable {
186
187
  signer;
187
188
  constructor({ signer, ...config }) {
188
189
  super(config);
189
- this.signer = signer;
190
+ this.signer = (0, ao_js_1.createAoSigner)(signer);
190
191
  }
191
192
  /**
192
193
  * @param target @type {string} The address of the account you want to transfer the ANT to.
@@ -203,7 +204,6 @@ class AoANTWriteable extends AoANTReadable {
203
204
  ];
204
205
  return this.process.send({
205
206
  tags,
206
- data: {},
207
207
  signer: this.signer,
208
208
  });
209
209
  }
@@ -222,7 +222,6 @@ class AoANTWriteable extends AoANTReadable {
222
222
  ];
223
223
  return this.process.send({
224
224
  tags,
225
- data: {},
226
225
  signer: this.signer,
227
226
  });
228
227
  }
@@ -241,7 +240,6 @@ class AoANTWriteable extends AoANTReadable {
241
240
  ];
242
241
  return this.process.send({
243
242
  tags,
244
- data: {},
245
243
  signer: this.signer,
246
244
  });
247
245
  }
@@ -263,7 +261,6 @@ class AoANTWriteable extends AoANTReadable {
263
261
  { name: 'Transaction-Id', value: transactionId },
264
262
  { name: 'TTL-Seconds', value: ttlSeconds.toString() },
265
263
  ],
266
- data: { transactionId, ttlSeconds },
267
264
  signer: this.signer,
268
265
  });
269
266
  }
@@ -281,7 +278,6 @@ class AoANTWriteable extends AoANTReadable {
281
278
  { name: 'Action', value: 'Remove-Record' },
282
279
  { name: 'Sub-Domain', value: undername },
283
280
  ],
284
- data: { undername },
285
281
  signer: this.signer,
286
282
  });
287
283
  }
@@ -299,7 +295,6 @@ class AoANTWriteable extends AoANTReadable {
299
295
  { name: 'Action', value: 'Set-Ticker' },
300
296
  { name: 'Ticker', value: ticker },
301
297
  ],
302
- data: { ticker },
303
298
  signer: this.signer,
304
299
  });
305
300
  }
@@ -317,7 +312,6 @@ class AoANTWriteable extends AoANTReadable {
317
312
  { name: 'Action', value: 'Set-Name' },
318
313
  { name: 'Name', value: name },
319
314
  ],
320
- data: { name },
321
315
  signer: this.signer,
322
316
  });
323
317
  }
@@ -18,7 +18,6 @@ exports.AOProcess = void 0;
18
18
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19
19
  */
20
20
  const aoconnect_1 = require("@permaweb/aoconnect");
21
- const arbundles_1 = require("arbundles");
22
21
  const json_js_1 = require("../../utils/json.js");
23
22
  const version_js_1 = require("../../version.js");
24
23
  const error_js_1 = require("../error.js");
@@ -32,22 +31,6 @@ class AOProcess {
32
31
  this.logger = logger;
33
32
  this.ao = ao;
34
33
  }
35
- // TODO: could abstract into our own interface that constructs different signers
36
- static async createAoSigner(signer) {
37
- // ensure appropriate permissions are granted with injected signers.
38
- if (signer.publicKey === undefined && 'setPublicKey' in signer) {
39
- await signer.setPublicKey();
40
- }
41
- const aoSigner = async ({ data, tags, target, anchor }) => {
42
- const dataItem = (0, arbundles_1.createData)(data, signer, { tags, target, anchor });
43
- const signedData = dataItem.sign(signer).then(async () => ({
44
- id: await dataItem.id,
45
- raw: await dataItem.getRaw(),
46
- }));
47
- return signedData;
48
- };
49
- return aoSigner;
50
- }
51
34
  async read({ tags, retries = 3, }) {
52
35
  let attempts = 0;
53
36
  let lastError;
@@ -108,8 +91,8 @@ class AOProcess {
108
91
  process: this.processId,
109
92
  // TODO: any other default tags we want to add?
110
93
  tags: [...tags, { name: 'AR-IO-SDK', value: version_js_1.version }],
111
- data: typeof data !== 'string' ? JSON.stringify(data) : data,
112
- signer: await AOProcess.createAoSigner(signer),
94
+ data,
95
+ signer,
113
96
  });
114
97
  this.logger.debug(`Sent message to process`, {
115
98
  messageId,
@@ -33,6 +33,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
33
33
  __exportStar(require("./error.js"), exports);
34
34
  __exportStar(require("./logger.js"), exports);
35
35
  __exportStar(require("./ant.js"), exports);
36
+ __exportStar(require("./ant-registry.js"), exports);
36
37
  // ao
37
38
  __exportStar(require("./io.js"), exports);
38
39
  __exportStar(require("./contracts/ao-process.js"), exports);
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.IOWriteable = exports.IOReadable = exports.IO = void 0;
4
4
  const constants_js_1 = require("../constants.js");
5
5
  const io_js_1 = require("../io.js");
6
+ const ao_js_1 = require("../utils/ao.js");
6
7
  const arweave_js_1 = require("./arweave.js");
7
8
  const ao_process_js_1 = require("./contracts/ao-process.js");
8
9
  const error_js_1 = require("./error.js");
@@ -46,6 +47,11 @@ class IOReadable {
46
47
  tags: [{ name: 'Action', value: 'Info' }],
47
48
  });
48
49
  }
50
+ async getTokenSupply() {
51
+ return this.process.read({
52
+ tags: [{ name: 'Action', value: 'Total-Token-Supply' }],
53
+ });
54
+ }
49
55
  async getEpochSettings(params) {
50
56
  const allTags = [
51
57
  { name: 'Action', value: 'Epoch-Settings' },
@@ -341,11 +347,11 @@ class IOWriteable extends IOReadable {
341
347
  processId: constants_js_1.IO_TESTNET_PROCESS_ID,
342
348
  }),
343
349
  });
344
- this.signer = signer;
350
+ this.signer = (0, ao_js_1.createAoSigner)(signer);
345
351
  }
346
352
  else if ((0, io_js_1.isProcessConfiguration)(config)) {
347
353
  super({ process: config.process });
348
- this.signer = signer;
354
+ this.signer = (0, ao_js_1.createAoSigner)(signer);
349
355
  }
350
356
  else if ((0, io_js_1.isProcessIdConfiguration)(config)) {
351
357
  super({
@@ -353,7 +359,7 @@ class IOWriteable extends IOReadable {
353
359
  processId: config.processId,
354
360
  }),
355
361
  });
356
- this.signer = signer;
362
+ this.signer = (0, ao_js_1.createAoSigner)(signer);
357
363
  }
358
364
  else {
359
365
  throw new error_js_1.InvalidContractConfigurationError();
@@ -388,11 +394,11 @@ class IOWriteable extends IOReadable {
388
394
  },
389
395
  {
390
396
  name: 'Allow-Delegated-Staking',
391
- value: allowDelegatedStaking.toString(),
397
+ value: allowDelegatedStaking?.toString(),
392
398
  },
393
399
  {
394
400
  name: 'Delegate-Reward-Share-Ratio',
395
- value: delegateRewardShareRatio.toString(),
401
+ value: delegateRewardShareRatio?.toString(),
396
402
  },
397
403
  {
398
404
  name: 'FQDN',
@@ -404,7 +410,7 @@ class IOWriteable extends IOReadable {
404
410
  },
405
411
  {
406
412
  name: 'Min-Delegated-Stake',
407
- value: minDelegatedStake.valueOf().toString(),
413
+ value: minDelegatedStake?.valueOf().toString(),
408
414
  },
409
415
  {
410
416
  name: 'Note',
@@ -412,7 +418,7 @@ class IOWriteable extends IOReadable {
412
418
  },
413
419
  {
414
420
  name: 'Port',
415
- value: port.toString(),
421
+ value: port?.toString(),
416
422
  },
417
423
  {
418
424
  name: 'Properties',
@@ -424,7 +430,7 @@ class IOWriteable extends IOReadable {
424
430
  },
425
431
  {
426
432
  name: 'Auto-Stake',
427
- value: autoStake.toString(),
433
+ value: autoStake?.toString(),
428
434
  },
429
435
  {
430
436
  name: 'Observer-Address',
@@ -538,10 +544,6 @@ class IOWriteable extends IOReadable {
538
544
  value: params.failedGateways.join(','),
539
545
  },
540
546
  ],
541
- data: {
542
- reportTxId: params.reportTxId,
543
- failedGateways: params.failedGateways,
544
- },
545
547
  });
546
548
  }
547
549
  async buyRecord(params, options) {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_SCHEDULER_ID = exports.ANT_LUA_ID = exports.AOS_MODULE_ID = exports.MIO_PER_IO = exports.IO_TESTNET_PROCESS_ID = exports.ioDevnetProcessId = exports.IO_DEVNET_PROCESS_ID = exports.ARNS_DEVNET_REGISTRY_TX = exports.ARNS_TESTNET_REGISTRY_TX = exports.SORT_KEY_REGEX = exports.FQDN_REGEX = exports.ARWEAVE_TX_REGEX = void 0;
3
+ exports.DEFAULT_SCHEDULER_ID = exports.ANT_LUA_ID = exports.AOS_MODULE_ID = exports.MIO_PER_IO = exports.ANT_REGISTRY_ID = exports.IO_TESTNET_PROCESS_ID = exports.ioDevnetProcessId = exports.IO_DEVNET_PROCESS_ID = exports.ARNS_DEVNET_REGISTRY_TX = exports.ARNS_TESTNET_REGISTRY_TX = exports.SORT_KEY_REGEX = exports.FQDN_REGEX = exports.ARWEAVE_TX_REGEX = void 0;
4
4
  /**
5
5
  * Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
6
6
  *
@@ -28,6 +28,7 @@ exports.IO_DEVNET_PROCESS_ID = 'GaQrvEMKBpkjofgnBi_B3IgIDmY_XYelVLB6GcRGrHc';
28
28
  // backwards compatibility - TODO: remove in v2.0.0
29
29
  exports.ioDevnetProcessId = exports.IO_DEVNET_PROCESS_ID;
30
30
  exports.IO_TESTNET_PROCESS_ID = 'agYcCFJtrMG6cqMuZfskIkFTGvUPddICmtQSBIoPdiA';
31
+ exports.ANT_REGISTRY_ID = 'i_le_yKKPVstLTDSmkHRqf-wYphMnwB9OhleiTgMkWc';
31
32
  exports.MIO_PER_IO = 1_000_000;
32
33
  exports.AOS_MODULE_ID = 'cbn0KKrBZH7hdNkNokuXLtGryrWM--PjSTBqIzw9Kkk';
33
34
  exports.ANT_LUA_ID = 'Flwio4Lr08g6s6uim6lEJNnVGD9ylvz0_aafvpiL8FI';
package/lib/cjs/io.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isLeasedArNSRecord = exports.isProcessIdConfiguration = exports.isProcessConfiguration = void 0;
4
4
  const arweave_js_1 = require("./utils/arweave.js");
5
+ // Typeguard functions
5
6
  function isProcessConfiguration(config) {
6
7
  return 'process' in config;
7
8
  }
@@ -13,6 +14,6 @@ function isProcessIdConfiguration(config) {
13
14
  }
14
15
  exports.isProcessIdConfiguration = isProcessIdConfiguration;
15
16
  function isLeasedArNSRecord(record) {
16
- return record.type === 'lease' && record.endTimestamp !== undefined;
17
+ return record.type === 'lease';
17
18
  }
18
19
  exports.isLeasedArNSRecord = isLeasedArNSRecord;
package/lib/cjs/types.js CHANGED
@@ -30,7 +30,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
30
30
  * You should have received a copy of the GNU Affero General Public License
31
31
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
32
32
  */
33
- __exportStar(require("./contract-state.js"), exports);
34
33
  __exportStar(require("./common.js"), exports);
35
34
  __exportStar(require("./token.js"), exports);
36
35
  __exportStar(require("./io.js"), exports);
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.evolveANT = exports.spawnANT = void 0;
3
+ exports.createAoSigner = exports.evolveANT = exports.spawnANT = void 0;
4
4
  /**
5
5
  * Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
6
6
  *
@@ -18,10 +18,16 @@ exports.evolveANT = exports.spawnANT = void 0;
18
18
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19
19
  */
20
20
  const aoconnect_1 = require("@permaweb/aoconnect");
21
+ const arbundles_1 = require("arbundles");
21
22
  const arweave_js_1 = require("../common/arweave.js");
22
23
  const index_js_1 = require("../common/index.js");
23
24
  const constants_js_1 = require("../constants.js");
24
- async function spawnANT({ signer, module = constants_js_1.AOS_MODULE_ID, luaCodeTxId = constants_js_1.ANT_LUA_ID, ao = (0, aoconnect_1.connect)(), scheduler = constants_js_1.DEFAULT_SCHEDULER_ID, state, stateContractTxId, }) {
25
+ async function spawnANT({ signer, module = constants_js_1.AOS_MODULE_ID, luaCodeTxId = constants_js_1.ANT_LUA_ID, ao = (0, aoconnect_1.connect)(), scheduler = constants_js_1.DEFAULT_SCHEDULER_ID, state, stateContractTxId, antRegistryId = constants_js_1.ANT_REGISTRY_ID, }) {
26
+ // AoSigner is not a Contract Signer - should probably add that to the contract signer type
27
+ const registryClient = new index_js_1.AOProcess({
28
+ processId: antRegistryId,
29
+ ao,
30
+ });
25
31
  //TODO: cache locally and only fetch if not cached
26
32
  const luaString = (await arweave_js_1.defaultArweave.transactions.getData(luaCodeTxId, {
27
33
  decode: true,
@@ -30,7 +36,13 @@ async function spawnANT({ signer, module = constants_js_1.AOS_MODULE_ID, luaCode
30
36
  const processId = await ao.spawn({
31
37
  module,
32
38
  scheduler,
33
- signer: await index_js_1.AOProcess.createAoSigner(signer),
39
+ signer,
40
+ tags: [
41
+ {
42
+ name: 'ANT-Registry-Id',
43
+ value: antRegistryId,
44
+ },
45
+ ],
34
46
  });
35
47
  const aosClient = new index_js_1.AOProcess({
36
48
  processId,
@@ -57,6 +69,13 @@ async function spawnANT({ signer, module = constants_js_1.AOS_MODULE_ID, luaCode
57
69
  signer,
58
70
  });
59
71
  }
72
+ await registryClient.send({
73
+ tags: [
74
+ { name: 'Action', value: 'Register' },
75
+ { name: 'Process-Id', value: processId },
76
+ ],
77
+ signer,
78
+ });
60
79
  return processId;
61
80
  }
62
81
  exports.spawnANT = spawnANT;
@@ -82,3 +101,24 @@ async function evolveANT({ signer, processId, luaCodeTxId = constants_js_1.ANT_L
82
101
  return id;
83
102
  }
84
103
  exports.evolveANT = evolveANT;
104
+ function createAoSigner(signer) {
105
+ if (!('publicKey' in signer)) {
106
+ return (0, aoconnect_1.createDataItemSigner)(signer);
107
+ }
108
+ const aoSigner = async ({ data, tags, target, anchor }) => {
109
+ // ensure appropriate permissions are granted with injected signers.
110
+ if (signer.publicKey === undefined &&
111
+ 'setPublicKey' in signer &&
112
+ typeof signer.setPublicKey === 'function') {
113
+ await signer.setPublicKey();
114
+ }
115
+ const dataItem = (0, arbundles_1.createData)(data, signer, { tags, target, anchor });
116
+ const signedData = dataItem.sign(signer).then(async () => ({
117
+ id: await dataItem.id,
118
+ raw: await dataItem.getRaw(),
119
+ }));
120
+ return signedData;
121
+ };
122
+ return aoSigner;
123
+ }
124
+ exports.createAoSigner = createAoSigner;
@@ -1,6 +1,4 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.safeDecode = void 0;
4
2
  /**
5
3
  * Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
6
4
  *
@@ -17,6 +15,9 @@ exports.safeDecode = void 0;
17
15
  * You should have received a copy of the GNU Affero General Public License
18
16
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19
17
  */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.safeDecode = void 0;
20
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
21
  function safeDecode(data) {
21
22
  try {
22
23
  return JSON.parse(data);
@@ -19,37 +19,17 @@ exports.fetchAllArNSRecords = exports.ArNSEventEmitter = exports.getANTProcesses
19
19
  */
20
20
  const eventemitter3_1 = require("eventemitter3");
21
21
  const plimit_lit_1 = require("plimit-lit");
22
+ const ant_registry_js_1 = require("../common/ant-registry.js");
22
23
  const ant_js_1 = require("../common/ant.js");
23
24
  const io_js_1 = require("../common/io.js");
24
25
  const logger_js_1 = require("../common/logger.js");
25
26
  const constants_js_1 = require("../constants.js");
26
- const getANTProcessesOwnedByWallet = async ({ address, contract = io_js_1.IO.init({
27
- processId: constants_js_1.IO_TESTNET_PROCESS_ID,
28
- }), }) => {
29
- const throttle = (0, plimit_lit_1.pLimit)(50);
30
- // get the record names of the registry - TODO: this may need to be paginated
31
- const records = await (0, exports.fetchAllArNSRecords)({
32
- contract: contract,
33
- });
34
- const uniqueContractProcessIds = Object.values(records)
35
- .filter((record) => record.processId !== undefined)
36
- .map((record) => record.processId);
37
- // check the contract owner and controllers
38
- const ownedOrControlledByWallet = await Promise.all(uniqueContractProcessIds.map(async (processId) => throttle(async () => {
39
- const ant = ant_js_1.ANT.init({
40
- processId,
41
- });
42
- const { Owner, Controllers } = await ant.getState();
43
- if (Owner === address || Controllers.includes(address)) {
44
- return processId;
45
- }
46
- return;
47
- })));
48
- if (ownedOrControlledByWallet.length === 0) {
49
- return [];
50
- }
51
- // TODO: insert gql query to find ANT processes owned by wallet given wallet not currently in the registry
52
- return [...new Set(ownedOrControlledByWallet)];
27
+ /**
28
+ * @beta This API is in beta and may change in the future.
29
+ */
30
+ const getANTProcessesOwnedByWallet = async ({ address, registry = ant_registry_js_1.ANTRegistry.init(), }) => {
31
+ const res = await registry.accessControlList({ address });
32
+ return [...new Set([...res.Owned, ...res.Controlled])];
53
33
  };
54
34
  exports.getANTProcessesOwnedByWallet = getANTProcessesOwnedByWallet;
55
35
  function timeout(ms, promise) {
@@ -82,9 +62,11 @@ class ArNSEventEmitter extends eventemitter3_1.EventEmitter {
82
62
  this.throttle = (0, plimit_lit_1.pLimit)(concurrency);
83
63
  this.logger = logger;
84
64
  }
85
- async fetchProcessesOwnedByWallet({ address }) {
65
+ async fetchProcessesOwnedByWallet({ address, pageSize, antRegistry = ant_registry_js_1.ANTRegistry.init(), }) {
86
66
  const uniqueContractProcessIds = {};
87
- await timeout(this.timeoutMs, (0, exports.fetchAllArNSRecords)({ contract: this.contract, emitter: this }))
67
+ const antIdRes = await antRegistry.accessControlList({ address });
68
+ const antIds = new Set([...antIdRes.Owned, ...antIdRes.Controlled]);
69
+ await timeout(this.timeoutMs, (0, exports.fetchAllArNSRecords)({ contract: this.contract, emitter: this, pageSize }))
88
70
  .catch((e) => {
89
71
  this.emit('error', `Error getting ArNS records: ${e}`);
90
72
  this.logger.error(`Error getting ArNS records`, {
@@ -94,19 +76,17 @@ class ArNSEventEmitter extends eventemitter3_1.EventEmitter {
94
76
  return {};
95
77
  })
96
78
  .then((records) => {
97
- if (!records)
98
- return;
99
- Object.entries(records).forEach(([name, record]) => {
100
- if (record.processId === undefined) {
101
- return;
102
- }
103
- if (uniqueContractProcessIds[record.processId] === undefined) {
104
- uniqueContractProcessIds[record.processId] = {
105
- state: undefined,
106
- names: {},
107
- };
79
+ Object.entries(records).forEach(([name, arnsRecord]) => {
80
+ if (antIds.has(arnsRecord.processId)) {
81
+ if (uniqueContractProcessIds[arnsRecord.processId] == undefined) {
82
+ uniqueContractProcessIds[arnsRecord.processId] = {
83
+ state: undefined,
84
+ names: {},
85
+ };
86
+ }
87
+ uniqueContractProcessIds[arnsRecord.processId].names[name] =
88
+ arnsRecord;
108
89
  }
109
- uniqueContractProcessIds[record.processId].names[name] = record;
110
90
  });
111
91
  });
112
92
  const idCount = Object.keys(uniqueContractProcessIds).length;
@@ -137,13 +117,14 @@ class ArNSEventEmitter extends eventemitter3_1.EventEmitter {
137
117
  exports.ArNSEventEmitter = ArNSEventEmitter;
138
118
  const fetchAllArNSRecords = async ({ contract = io_js_1.IO.init({
139
119
  processId: constants_js_1.IO_TESTNET_PROCESS_ID,
140
- }), emitter, logger = logger_js_1.Logger.default, }) => {
120
+ }), emitter, logger = logger_js_1.Logger.default, pageSize = 50_000, }) => {
141
121
  let cursor;
142
122
  const startTimestamp = Date.now();
143
123
  const records = {};
144
124
  do {
145
125
  const pageResult = await contract
146
- .getArNSRecords({ cursor })
126
+ .getArNSRecords({ cursor, limit: pageSize })
127
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
147
128
  .catch((e) => {
148
129
  logger?.error(`Error getting ArNS records`, {
149
130
  message: e?.message,
@@ -18,4 +18,4 @@
18
18
  Object.defineProperty(exports, "__esModule", { value: true });
19
19
  exports.version = void 0;
20
20
  // AUTOMATICALLY GENERATED FILE - DO NOT TOUCH
21
- exports.version = '2.1.0-alpha.1';
21
+ exports.version = '2.1.0-alpha.11';
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
3
+ *
4
+ * This program is free software: you can redistribute it and/or modify
5
+ * it under the terms of the GNU Affero General Public License as published by
6
+ * the Free Software Foundation, either version 3 of the License, or
7
+ * (at your option) any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ * GNU Affero General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Affero General Public License
15
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+ import { ANT_REGISTRY_ID } from '../constants.js';
18
+ import { isProcessConfiguration, isProcessIdConfiguration, } from '../types.js';
19
+ import { createAoSigner } from '../utils/ao.js';
20
+ import { AOProcess, InvalidContractConfigurationError } from './index.js';
21
+ export class ANTRegistry {
22
+ static init(config) {
23
+ if (config && config.signer) {
24
+ const { signer, ...rest } = config;
25
+ return new AoANTRegistryWriteable({
26
+ ...rest,
27
+ signer,
28
+ });
29
+ }
30
+ return new AoANTRegistryReadable(config);
31
+ }
32
+ }
33
+ export class AoANTRegistryReadable {
34
+ process;
35
+ constructor(config) {
36
+ if (config &&
37
+ (isProcessIdConfiguration(config) || isProcessConfiguration(config))) {
38
+ if (isProcessConfiguration(config)) {
39
+ this.process = config.process;
40
+ }
41
+ else if (isProcessIdConfiguration(config)) {
42
+ this.process = new AOProcess({
43
+ processId: config.processId,
44
+ });
45
+ }
46
+ else {
47
+ throw new InvalidContractConfigurationError();
48
+ }
49
+ }
50
+ else {
51
+ this.process = new AOProcess({
52
+ processId: ANT_REGISTRY_ID,
53
+ });
54
+ }
55
+ }
56
+ // Should we rename this to "getANTsByAddress"? seems more clear, though not same as handler name
57
+ async accessControlList({ address, }) {
58
+ return this.process.read({
59
+ tags: [
60
+ { name: 'Action', value: 'Access-Control-List' },
61
+ { name: 'Address', value: address },
62
+ ],
63
+ });
64
+ }
65
+ }
66
+ export class AoANTRegistryWriteable extends AoANTRegistryReadable {
67
+ signer;
68
+ constructor({ signer, ...config }) {
69
+ super(config);
70
+ this.signer = createAoSigner(signer);
71
+ }
72
+ async register({ processId, }) {
73
+ return this.process.send({
74
+ tags: [
75
+ { name: 'Action', value: 'Register' },
76
+ { name: 'Process-Id', value: processId },
77
+ ],
78
+ signer: this.signer,
79
+ });
80
+ }
81
+ }