@algorandfoundation/algokit-utils 7.0.0-beta.9 → 8.0.0-beta.1

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 (231) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +21 -3
  3. package/account/account.d.ts +4 -1
  4. package/account/account.js +10 -7
  5. package/account/account.js.map +1 -1
  6. package/account/account.mjs +11 -8
  7. package/account/account.mjs.map +1 -1
  8. package/account/get-account-config-from-environment.js.map +1 -1
  9. package/account/get-account-config-from-environment.mjs.map +1 -1
  10. package/account/get-account.js.map +1 -1
  11. package/account/get-account.mjs.map +1 -1
  12. package/account/get-dispenser-account.d.ts +1 -1
  13. package/account/get-dispenser-account.js.map +1 -1
  14. package/account/get-dispenser-account.mjs.map +1 -1
  15. package/account/mnemonic-account.js.map +1 -1
  16. package/account/mnemonic-account.mjs.map +1 -1
  17. package/amount.d.ts +1 -0
  18. package/amount.js +3 -2
  19. package/amount.js.map +1 -1
  20. package/amount.mjs +3 -3
  21. package/amount.mjs.map +1 -1
  22. package/app-client.d.ts +4 -4
  23. package/app-client.js +4 -4
  24. package/app-client.js.map +1 -1
  25. package/app-client.mjs +4 -4
  26. package/app-client.mjs.map +1 -1
  27. package/app-deploy.js +25 -15
  28. package/app-deploy.js.map +1 -1
  29. package/app-deploy.mjs +27 -17
  30. package/app-deploy.mjs.map +1 -1
  31. package/app.d.ts +2 -2
  32. package/app.js +9 -6
  33. package/app.js.map +1 -1
  34. package/app.mjs +9 -6
  35. package/app.mjs.map +1 -1
  36. package/asset.js.map +1 -1
  37. package/asset.mjs.map +1 -1
  38. package/debugging/debugging.js.map +1 -1
  39. package/debugging/debugging.mjs.map +1 -1
  40. package/dispenser-client.js.map +1 -1
  41. package/dispenser-client.mjs.map +1 -1
  42. package/index.d.ts +1 -0
  43. package/index.js +7 -1
  44. package/index.js.map +1 -1
  45. package/index.mjs +3 -2
  46. package/index.mjs.map +1 -1
  47. package/indexer-lookup.d.ts +10 -8
  48. package/indexer-lookup.js +14 -10
  49. package/indexer-lookup.js.map +1 -1
  50. package/indexer-lookup.mjs +14 -10
  51. package/indexer-lookup.mjs.map +1 -1
  52. package/localnet/get-kmd-wallet-account.js.map +1 -1
  53. package/localnet/get-kmd-wallet-account.mjs.map +1 -1
  54. package/localnet/get-localnet-dispenser-account.js.map +1 -1
  55. package/localnet/get-localnet-dispenser-account.mjs.map +1 -1
  56. package/localnet/get-or-create-kmd-wallet-account.js.map +1 -1
  57. package/localnet/get-or-create-kmd-wallet-account.mjs.map +1 -1
  58. package/localnet/is-localnet.js.map +1 -1
  59. package/localnet/is-localnet.mjs.map +1 -1
  60. package/network-client.d.ts +1 -7
  61. package/network-client.js +2 -9
  62. package/network-client.js.map +1 -1
  63. package/network-client.mjs +2 -9
  64. package/network-client.mjs.map +1 -1
  65. package/package.json +5 -5
  66. package/testing/_asset.d.ts +2 -1
  67. package/testing/account.d.ts +4 -3
  68. package/testing/account.js +8 -2
  69. package/testing/account.js.map +1 -1
  70. package/testing/account.mjs +9 -3
  71. package/testing/account.mjs.map +1 -1
  72. package/testing/fixtures/algokit-log-capture-fixture.js.map +1 -1
  73. package/testing/fixtures/algokit-log-capture-fixture.mjs.map +1 -1
  74. package/testing/fixtures/algorand-fixture.js +5 -6
  75. package/testing/fixtures/algorand-fixture.js.map +1 -1
  76. package/testing/fixtures/algorand-fixture.mjs +5 -6
  77. package/testing/fixtures/algorand-fixture.mjs.map +1 -1
  78. package/testing/indexer.js.map +1 -1
  79. package/testing/indexer.mjs.map +1 -1
  80. package/testing/test-logger.js +7 -1
  81. package/testing/test-logger.js.map +1 -1
  82. package/testing/test-logger.mjs +7 -1
  83. package/testing/test-logger.mjs.map +1 -1
  84. package/testing/transaction-logger.js.map +1 -1
  85. package/testing/transaction-logger.mjs.map +1 -1
  86. package/transaction/legacy-bridge.js +2 -2
  87. package/transaction/legacy-bridge.js.map +1 -1
  88. package/transaction/legacy-bridge.mjs +3 -3
  89. package/transaction/legacy-bridge.mjs.map +1 -1
  90. package/transaction/perform-atomic-transaction-composer-simulate.d.ts +4 -1
  91. package/transaction/perform-atomic-transaction-composer-simulate.js +14 -9
  92. package/transaction/perform-atomic-transaction-composer-simulate.js.map +1 -1
  93. package/transaction/perform-atomic-transaction-composer-simulate.mjs +15 -10
  94. package/transaction/perform-atomic-transaction-composer-simulate.mjs.map +1 -1
  95. package/transaction/transaction.d.ts +13 -22
  96. package/transaction/transaction.js +164 -110
  97. package/transaction/transaction.js.map +1 -1
  98. package/transaction/transaction.mjs +163 -109
  99. package/transaction/transaction.mjs.map +1 -1
  100. package/transfer/transfer-algos.js.map +1 -1
  101. package/transfer/transfer-algos.mjs.map +1 -1
  102. package/transfer/transfer.js +3 -1
  103. package/transfer/transfer.js.map +1 -1
  104. package/transfer/transfer.mjs +3 -1
  105. package/transfer/transfer.mjs.map +1 -1
  106. package/types/account-manager.d.ts +20 -20
  107. package/types/account-manager.js +36 -27
  108. package/types/account-manager.js.map +1 -1
  109. package/types/account-manager.mjs +38 -29
  110. package/types/account-manager.mjs.map +1 -1
  111. package/types/account.d.ts +17 -8
  112. package/types/account.js +2 -2
  113. package/types/account.js.map +1 -1
  114. package/types/account.mjs +3 -3
  115. package/types/account.mjs.map +1 -1
  116. package/types/algo-http-client-with-retry.d.ts +1 -2
  117. package/types/algo-http-client-with-retry.js +33 -3
  118. package/types/algo-http-client-with-retry.js.map +1 -1
  119. package/types/algo-http-client-with-retry.mjs +32 -2
  120. package/types/algo-http-client-with-retry.mjs.map +1 -1
  121. package/types/algorand-client-interface.d.ts +2 -2
  122. package/types/algorand-client-transaction-creator.d.ts +16 -14
  123. package/types/algorand-client-transaction-creator.js +3 -1
  124. package/types/algorand-client-transaction-creator.js.map +1 -1
  125. package/types/algorand-client-transaction-creator.mjs +3 -1
  126. package/types/algorand-client-transaction-creator.mjs.map +1 -1
  127. package/types/algorand-client-transaction-sender.d.ts +109 -105
  128. package/types/algorand-client-transaction-sender.js +6 -2
  129. package/types/algorand-client-transaction-sender.js.map +1 -1
  130. package/types/algorand-client-transaction-sender.mjs +6 -2
  131. package/types/algorand-client-transaction-sender.mjs.map +1 -1
  132. package/types/algorand-client.d.ts +6 -7
  133. package/types/algorand-client.js +3 -6
  134. package/types/algorand-client.js.map +1 -1
  135. package/types/algorand-client.mjs +5 -5
  136. package/types/algorand-client.mjs.map +1 -1
  137. package/types/amount.js.map +1 -1
  138. package/types/amount.mjs.map +1 -1
  139. package/types/app-arc56.d.ts +31 -20
  140. package/types/app-arc56.js.map +1 -1
  141. package/types/app-arc56.mjs.map +1 -1
  142. package/types/app-client.d.ts +298 -286
  143. package/types/app-client.js +148 -41
  144. package/types/app-client.js.map +1 -1
  145. package/types/app-client.mjs +147 -40
  146. package/types/app-client.mjs.map +1 -1
  147. package/types/app-deployer.d.ts +4 -4
  148. package/types/app-deployer.js +23 -24
  149. package/types/app-deployer.js.map +1 -1
  150. package/types/app-deployer.mjs +25 -26
  151. package/types/app-deployer.mjs.map +1 -1
  152. package/types/app-factory.d.ts +130 -130
  153. package/types/app-factory.js +4 -5
  154. package/types/app-factory.js.map +1 -1
  155. package/types/app-factory.mjs +5 -6
  156. package/types/app-factory.mjs.map +1 -1
  157. package/types/app-manager.d.ts +5 -5
  158. package/types/app-manager.js +116 -38
  159. package/types/app-manager.js.map +1 -1
  160. package/types/app-manager.mjs +117 -39
  161. package/types/app-manager.mjs.map +1 -1
  162. package/types/app-spec.js +8 -2
  163. package/types/app-spec.js.map +1 -1
  164. package/types/app-spec.mjs +8 -2
  165. package/types/app-spec.mjs.map +1 -1
  166. package/types/app.d.ts +12 -11
  167. package/types/app.js.map +1 -1
  168. package/types/app.mjs.map +1 -1
  169. package/types/asset-manager.d.ts +9 -9
  170. package/types/asset-manager.js +10 -13
  171. package/types/asset-manager.js.map +1 -1
  172. package/types/asset-manager.mjs +10 -13
  173. package/types/asset-manager.mjs.map +1 -1
  174. package/types/async-event-emitter.d.ts +1 -10
  175. package/types/async-event-emitter.js +0 -5
  176. package/types/async-event-emitter.js.map +1 -1
  177. package/types/async-event-emitter.mjs +1 -6
  178. package/types/async-event-emitter.mjs.map +1 -1
  179. package/types/client-manager.d.ts +2 -9
  180. package/types/client-manager.js +11 -21
  181. package/types/client-manager.js.map +1 -1
  182. package/types/client-manager.mjs +11 -21
  183. package/types/client-manager.mjs.map +1 -1
  184. package/types/composer.d.ts +58 -46
  185. package/types/composer.js +154 -105
  186. package/types/composer.js.map +1 -1
  187. package/types/composer.mjs +156 -105
  188. package/types/composer.mjs.map +1 -1
  189. package/types/config.js.map +1 -1
  190. package/types/config.mjs.map +1 -1
  191. package/types/debugging.d.ts +1 -1
  192. package/types/debugging.js +1 -1
  193. package/types/debugging.js.map +1 -1
  194. package/types/debugging.mjs +1 -1
  195. package/types/debugging.mjs.map +1 -1
  196. package/types/dispenser-client.d.ts +2 -1
  197. package/types/dispenser-client.js +5 -1
  198. package/types/dispenser-client.js.map +1 -1
  199. package/types/dispenser-client.mjs +5 -1
  200. package/types/dispenser-client.mjs.map +1 -1
  201. package/types/indexer.d.ts +74 -755
  202. package/types/indexer.js.map +1 -1
  203. package/types/indexer.mjs.map +1 -1
  204. package/types/kmd-account-manager.d.ts +2 -2
  205. package/types/kmd-account-manager.js +1 -1
  206. package/types/kmd-account-manager.js.map +1 -1
  207. package/types/kmd-account-manager.mjs +2 -2
  208. package/types/kmd-account-manager.mjs.map +1 -1
  209. package/types/lifecycle-events.d.ts +10 -0
  210. package/types/lifecycle-events.js +8 -0
  211. package/types/lifecycle-events.js.map +1 -0
  212. package/types/lifecycle-events.mjs +8 -0
  213. package/types/lifecycle-events.mjs.map +1 -0
  214. package/types/logging.js.map +1 -1
  215. package/types/logging.mjs.map +1 -1
  216. package/types/logic-error.d.ts +2 -3
  217. package/types/logic-error.js +3 -3
  218. package/types/logic-error.js.map +1 -1
  219. package/types/logic-error.mjs +3 -3
  220. package/types/logic-error.mjs.map +1 -1
  221. package/types/network-client.d.ts +1 -1
  222. package/types/network-client.js.map +1 -1
  223. package/types/network-client.mjs.map +1 -1
  224. package/types/testing.d.ts +6 -7
  225. package/util.js.map +1 -1
  226. package/util.mjs.map +1 -1
  227. package/types/urlTokenBaseHTTPClient.d.ts +0 -40
  228. package/types/urlTokenBaseHTTPClient.js +0 -153
  229. package/types/urlTokenBaseHTTPClient.js.map +0 -1
  230. package/types/urlTokenBaseHTTPClient.mjs +0 -151
  231. package/types/urlTokenBaseHTTPClient.mjs.map +0 -1
@@ -1,9 +1,9 @@
1
1
  'use strict';
2
2
 
3
3
  var algosdk = require('algosdk');
4
+ var transaction = require('../transaction/transaction.js');
4
5
  var types_app = require('./app.js');
5
6
 
6
- var modelsv2 = algosdk.modelsv2;
7
7
  /** Allows management of application information. */
8
8
  class AppManager {
9
9
  /**
@@ -35,7 +35,7 @@ class AppManager {
35
35
  compiled: compiled.result,
36
36
  compiledHash: compiled.hash,
37
37
  compiledBase64ToBytes: new Uint8Array(Buffer.from(compiled.result, 'base64')),
38
- sourceMap: new algosdk.SourceMap(compiled['sourcemap']),
38
+ sourceMap: new algosdk.ProgramSourceMap(JSON.parse(algosdk.encodeJSON(compiled.sourcemap))),
39
39
  };
40
40
  this._compilationResults[tealCode] = result;
41
41
  return result;
@@ -83,7 +83,7 @@ class AppManager {
83
83
  * @returns The app information
84
84
  */
85
85
  async getById(appId) {
86
- const app = modelsv2.Application.from_obj_for_encoding(await this._algod.getApplicationByID(Number(appId)).do());
86
+ const app = await this._algod.getApplicationByID(Number(appId)).do();
87
87
  return {
88
88
  appId: BigInt(app.id),
89
89
  appAddress: algosdk.getApplicationAddress(app.id),
@@ -115,7 +115,7 @@ class AppManager {
115
115
  * @returns The current local state for the given (app, account) combination
116
116
  */
117
117
  async getLocalState(appId, address) {
118
- const appInfo = modelsv2.AccountApplicationResponse.from_obj_for_encoding(await this._algod.accountApplicationInformation(address, Number(appId)).do());
118
+ const appInfo = await this._algod.accountApplicationInformation(address, appId).do();
119
119
  if (!appInfo.appLocalState?.keyValue) {
120
120
  throw new Error("Couldn't find local state");
121
121
  }
@@ -127,7 +127,7 @@ class AppManager {
127
127
  * @returns The current box names
128
128
  */
129
129
  async getBoxNames(appId) {
130
- const boxResult = await this._algod.getApplicationBoxes(Number(appId)).do();
130
+ const boxResult = await this._algod.getApplicationBoxes(appId).do();
131
131
  return boxResult.boxes.map((b) => {
132
132
  return {
133
133
  nameRaw: b.name,
@@ -183,12 +183,8 @@ class AppManager {
183
183
  static getBoxReference(boxId) {
184
184
  const ref = typeof boxId === 'object' && 'appId' in boxId ? boxId : { appId: 0n, name: boxId };
185
185
  return {
186
- appIndex: Number(ref.appId),
187
- name: typeof ref.name === 'string'
188
- ? new TextEncoder().encode(ref.name)
189
- : 'length' in ref.name
190
- ? ref.name
191
- : algosdk.decodeAddress(ref.name.addr).publicKey,
186
+ appIndex: ref.appId,
187
+ name: typeof ref.name === 'string' ? new TextEncoder().encode(ref.name) : 'length' in ref.name ? ref.name : ref.name.addr.publicKey,
192
188
  };
193
189
  }
194
190
  /**
@@ -201,16 +197,17 @@ class AppManager {
201
197
  const stateValues = {};
202
198
  // Start with empty set
203
199
  for (const stateVal of state) {
204
- const keyBase64 = stateVal.key;
205
- const keyRaw = Buffer.from(keyBase64, 'base64');
206
- const key = keyRaw.toString('utf-8');
200
+ const keyBase64 = Buffer.from(stateVal.key).toString('base64');
201
+ const keyRaw = stateVal.key;
202
+ const key = Buffer.from(stateVal.key).toString('utf-8');
207
203
  const tealValue = stateVal.value;
208
204
  const dataTypeFlag = 'action' in tealValue ? tealValue.action : tealValue.type;
209
205
  let valueBase64;
210
206
  let valueRaw;
211
207
  switch (dataTypeFlag) {
212
208
  case 1:
213
- valueBase64 = tealValue.bytes ?? '';
209
+ valueBase64 =
210
+ typeof tealValue.bytes === 'string' ? tealValue.bytes : tealValue.bytes ? Buffer.from(tealValue.bytes).toString('base64') : '';
214
211
  valueRaw = Buffer.from(valueBase64, 'base64');
215
212
  stateValues[key] = {
216
213
  keyRaw,
@@ -251,19 +248,7 @@ class AppManager {
251
248
  method,
252
249
  rawReturnValue: new Uint8Array(),
253
250
  };
254
- const response = algosdk.AtomicTransactionComposer.parseMethodResponse(method, resultDummy, confirmation);
255
- return !response.decodeError
256
- ? {
257
- rawReturnValue: response.rawReturnValue,
258
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
259
- returnValue: response.returnValue,
260
- decodeError: undefined,
261
- }
262
- : {
263
- rawReturnValue: undefined,
264
- returnValue: undefined,
265
- decodeError: response.decodeError,
266
- };
251
+ return transaction.getABIReturnValue(algosdk.AtomicTransactionComposer.parseMethodResponse(method, resultDummy, confirmation));
267
252
  }
268
253
  /**
269
254
  * Replaces AlgoKit deploy-time deployment control parameters within the given TEAL template code.
@@ -283,13 +268,13 @@ class AppManager {
283
268
  if (!tealTemplateCode.includes(types_app.UPDATABLE_TEMPLATE_NAME)) {
284
269
  throw new Error(`Deploy-time updatability control requested for app deployment, but ${types_app.UPDATABLE_TEMPLATE_NAME} not present in TEAL code`);
285
270
  }
286
- tealTemplateCode = tealTemplateCode.replace(new RegExp(types_app.UPDATABLE_TEMPLATE_NAME, 'g'), (params.updatable ? 1 : 0).toString());
271
+ tealTemplateCode = replaceTemplateVariable(tealTemplateCode, types_app.UPDATABLE_TEMPLATE_NAME, (params.updatable ? 1 : 0).toString());
287
272
  }
288
273
  if (params.deletable !== undefined) {
289
274
  if (!tealTemplateCode.includes(types_app.DELETABLE_TEMPLATE_NAME)) {
290
275
  throw new Error(`Deploy-time deletability control requested for app deployment, but ${types_app.DELETABLE_TEMPLATE_NAME} not present in TEAL code`);
291
276
  }
292
- tealTemplateCode = tealTemplateCode.replace(new RegExp(types_app.DELETABLE_TEMPLATE_NAME, 'g'), (params.deletable ? 1 : 0).toString());
277
+ tealTemplateCode = replaceTemplateVariable(tealTemplateCode, types_app.DELETABLE_TEMPLATE_NAME, (params.deletable ? 1 : 0).toString());
293
278
  }
294
279
  return tealTemplateCode;
295
280
  }
@@ -313,7 +298,7 @@ class AppManager {
313
298
  tealTemplateCode = tealTemplateCode.replace(new RegExp(`(?<=bytes )${token}`, 'g'), `0x${value.toString(16).padStart(16, '0')}`);
314
299
  // We could probably return here since mixing pushint and pushbytes is likely not going to happen, but might as well do both
315
300
  }
316
- tealTemplateCode = tealTemplateCode.replace(new RegExp(token, 'g'), typeof value === 'string'
301
+ tealTemplateCode = replaceTemplateVariable(tealTemplateCode, token, typeof value === 'string'
317
302
  ? `0x${Buffer.from(value, 'utf-8').toString('hex')}`
318
303
  : ArrayBuffer.isView(value)
319
304
  ? `0x${Buffer.from(value).toString('hex')}`
@@ -329,17 +314,110 @@ class AppManager {
329
314
  * @returns The TEAL without comments
330
315
  */
331
316
  static stripTealComments(tealCode) {
332
- // find // outside quotes, i.e. won't pick up "//not a comment"
333
- const regex = /\/\/(?=([^"\\]*(\\.|"([^"\\]*\\.)*[^"\\]*"))*[^"]*$)/;
334
- tealCode = tealCode
317
+ const stripCommentFromLine = (line) => {
318
+ const commentIndex = findUnquotedString(line, '//');
319
+ if (commentIndex === undefined) {
320
+ return line;
321
+ }
322
+ return line.slice(0, commentIndex).trimEnd();
323
+ };
324
+ return tealCode
335
325
  .split('\n')
336
- .map((tealCodeLine) => {
337
- return tealCodeLine.split(regex)[0].trim();
338
- })
326
+ .map((line) => stripCommentFromLine(line))
339
327
  .join('\n');
340
- return tealCode;
341
328
  }
342
329
  }
330
+ /**
331
+ * Find the first string within a line of TEAL. Only matches outside of quotes and base64 are returned.
332
+ * Returns undefined if not found
333
+ */
334
+ const findUnquotedString = (line, token, startIndex = 0, _endIndex) => {
335
+ const endIndex = _endIndex === undefined ? line.length : _endIndex;
336
+ let index = startIndex;
337
+ let inQuotes = false;
338
+ let inBase64 = false;
339
+ while (index < endIndex) {
340
+ const currentChar = line[index];
341
+ if ((currentChar === ' ' || currentChar === '(') && !inQuotes && lastTokenBase64(line, index)) {
342
+ // enter base64
343
+ inBase64 = true;
344
+ }
345
+ else if ((currentChar === ' ' || currentChar === ')') && !inQuotes && inBase64) {
346
+ // exit base64
347
+ inBase64 = false;
348
+ }
349
+ else if (currentChar === '\\' && inQuotes) {
350
+ // escaped char, skip next character
351
+ index += 1;
352
+ }
353
+ else if (currentChar === '"') {
354
+ // quote boundary
355
+ inQuotes = !inQuotes;
356
+ }
357
+ else if (!inQuotes && !inBase64 && line.startsWith(token, index)) {
358
+ // can test for match
359
+ return index;
360
+ }
361
+ index += 1;
362
+ }
363
+ return undefined;
364
+ };
365
+ const lastTokenBase64 = (line, index) => {
366
+ try {
367
+ const tokens = line.slice(0, index).split(/\s+/);
368
+ const last = tokens[tokens.length - 1];
369
+ return ['base64', 'b64'].includes(last);
370
+ }
371
+ catch {
372
+ return false;
373
+ }
374
+ };
375
+ function replaceTemplateVariable(program, token, replacement) {
376
+ const result = [];
377
+ const tokenIndexOffset = replacement.length - token.length;
378
+ const programLines = program.split('\n');
379
+ for (const line of programLines) {
380
+ const _commentIndex = findUnquotedString(line, '//');
381
+ const commentIndex = _commentIndex === undefined ? line.length : _commentIndex;
382
+ let code = line.substring(0, commentIndex);
383
+ const comment = line.substring(commentIndex);
384
+ let trailingIndex = 0;
385
+ // eslint-disable-next-line no-constant-condition
386
+ while (true) {
387
+ const tokenIndex = findTemplateToken(code, token, trailingIndex);
388
+ if (tokenIndex === undefined) {
389
+ break;
390
+ }
391
+ trailingIndex = tokenIndex + token.length;
392
+ const prefix = code.substring(0, tokenIndex);
393
+ const suffix = code.substring(trailingIndex);
394
+ code = `${prefix}${replacement}${suffix}`;
395
+ trailingIndex += tokenIndexOffset;
396
+ }
397
+ result.push(code + comment);
398
+ }
399
+ return result.join('\n');
400
+ }
401
+ const findTemplateToken = (line, token, startIndex = 0, _endIndex) => {
402
+ const endIndex = line.length ;
403
+ let index = startIndex;
404
+ while (index < endIndex) {
405
+ const tokenIndex = findUnquotedString(line, token, index, endIndex);
406
+ if (tokenIndex === undefined) {
407
+ break;
408
+ }
409
+ const trailingIndex = tokenIndex + token.length;
410
+ if ((tokenIndex === 0 || !isValidTokenCharacter(line[tokenIndex - 1])) &&
411
+ (trailingIndex >= line.length || !isValidTokenCharacter(line[trailingIndex]))) {
412
+ return tokenIndex;
413
+ }
414
+ index = trailingIndex;
415
+ }
416
+ return undefined;
417
+ };
418
+ function isValidTokenCharacter(char) {
419
+ return char.length === 1 && (/\w/.test(char) || char === '_');
420
+ }
343
421
 
344
422
  exports.AppManager = AppManager;
345
423
  //# sourceMappingURL=app-manager.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"app-manager.js","sources":["../../src/types/app-manager.ts"],"sourcesContent":["import algosdk from 'algosdk'\nimport { TransactionSignerAccount } from './account'\nimport {\n BoxName,\n DELETABLE_TEMPLATE_NAME,\n UPDATABLE_TEMPLATE_NAME,\n type ABIReturn,\n type AppState,\n type CompiledTeal,\n type TealTemplateParams,\n} from './app'\nimport modelsv2 = algosdk.modelsv2\n\n/** Information about an app. */\nexport interface AppInformation {\n /** The ID of the app. */\n appId: bigint\n /** The escrow address that the app operates with. */\n appAddress: string\n /**\n * Approval program.\n */\n approvalProgram: Uint8Array\n /**\n * Clear state program.\n */\n clearStateProgram: Uint8Array\n /**\n * The address that created this application. This is the address where the\n * parameters and global state for this application can be found.\n */\n creator: string\n /**\n * Current global state values.\n */\n globalState: AppState\n /** The number of allocated ints in per-user local state. */\n localInts: number\n /** The number of allocated byte slices in per-user local state. */\n localByteSlices: number\n /** The number of allocated ints in global state. */\n globalInts: number\n /** The number of allocated byte slices in global state. */\n globalByteSlices: number\n /** Any extra pages that are needed for the smart contract. */\n extraProgramPages?: number\n}\n\n/**\n * Something that identifies an app box name - either a:\n * * `Uint8Array` (the actual binary of the box name)\n * * `string` (that will be encoded to a `Uint8Array`)\n * * `TransactionSignerAccount` (that will be encoded into the\n * public key address of the corresponding account)\n */\nexport type BoxIdentifier = string | Uint8Array | TransactionSignerAccount\n\n/**\n * A grouping of the app ID and name identifier to reference an app box.\n */\nexport interface BoxReference {\n /**\n * A unique application id\n */\n appId: bigint\n /**\n * Identifier for a box name\n */\n name: BoxIdentifier\n}\n\n/**\n * Parameters to get and decode a box value as an ABI type.\n */\nexport interface BoxValueRequestParams {\n /** The ID of the app return box names for */\n appId: bigint\n /** The name of the box to return either as a string, binary array or `BoxName` */\n boxName: BoxIdentifier\n /** The ABI type to decode the value using */\n type: algosdk.ABIType\n}\n\n/**\n * Parameters to get and decode a box value as an ABI type.\n */\nexport interface BoxValuesRequestParams {\n /** The ID of the app return box names for */\n appId: bigint\n /** The names of the boxes to return either as a string, binary array or BoxName` */\n boxNames: BoxIdentifier[]\n /** The ABI type to decode the value using */\n type: algosdk.ABIType\n}\n\n/** Allows management of application information. */\nexport class AppManager {\n private _algod: algosdk.Algodv2\n private _compilationResults: Record<string, CompiledTeal> = {}\n\n /**\n * Creates an `AppManager`\n * @param algod An algod instance\n */\n constructor(algod: algosdk.Algodv2) {\n this._algod = algod\n }\n\n /**\n * Compiles the given TEAL using algod and returns the result, including source map.\n *\n * The result of this compilation is also cached keyed by the TEAL\n * code so it can be retrieved via `getCompilationResult`.\n *\n * This function is re-entrant; it will only compile the same code once.\n *\n * @param tealCode The TEAL code\n * @returns The information about the compiled file\n */\n async compileTeal(tealCode: string): Promise<CompiledTeal> {\n if (this._compilationResults[tealCode]) {\n return this._compilationResults[tealCode]\n }\n\n const compiled = await this._algod.compile(tealCode).sourcemap(true).do()\n const result = {\n teal: tealCode,\n compiled: compiled.result,\n compiledHash: compiled.hash,\n compiledBase64ToBytes: new Uint8Array(Buffer.from(compiled.result, 'base64')),\n sourceMap: new algosdk.SourceMap(compiled['sourcemap']),\n }\n this._compilationResults[tealCode] = result\n\n return result\n }\n\n /**\n * Performs template substitution of a teal template and compiles it, returning the compiled result.\n *\n * Looks for `TMPL_{parameter}` for template replacements and replaces AlgoKit deploy-time control parameters\n * if deployment metadata is specified.\n *\n * * `TMPL_UPDATABLE` for updatability / immutability control\n * * `TMPL_DELETABLE` for deletability / permanence control\n *\n * @param tealTemplateCode The TEAL logic to compile\n * @param templateParams Any parameters to replace in the .teal file before compiling\n * @param deploymentMetadata The deployment metadata the app will be deployed with\n * @returns The information about the compiled code\n */\n async compileTealTemplate(\n tealTemplateCode: string,\n templateParams?: TealTemplateParams,\n deploymentMetadata?: { updatable?: boolean; deletable?: boolean },\n ): Promise<CompiledTeal> {\n let tealCode = AppManager.stripTealComments(tealTemplateCode)\n\n tealCode = AppManager.replaceTealTemplateParams(tealCode, templateParams)\n\n if (deploymentMetadata) {\n tealCode = AppManager.replaceTealTemplateDeployTimeControlParams(tealCode, deploymentMetadata)\n }\n\n return await this.compileTeal(tealCode)\n }\n\n /**\n * Returns a previous compilation result.\n * @param tealCode The TEAL code\n * @returns The information about the previously compiled file\n * or `undefined` if that TEAL code wasn't previously compiled\n */\n getCompilationResult(tealCode: string): CompiledTeal | undefined {\n return this._compilationResults[tealCode]\n }\n\n /**\n * Returns the current app information for the app with the given ID.\n *\n * @example\n * ```typescript\n * const appInfo = await appManager.getById(12353n);\n * ```\n *\n * @param appId The ID of the app\n * @returns The app information\n */\n public async getById(appId: bigint): Promise<AppInformation> {\n const app = modelsv2.Application.from_obj_for_encoding(await this._algod.getApplicationByID(Number(appId)).do())\n return {\n appId: BigInt(app.id),\n appAddress: algosdk.getApplicationAddress(app.id),\n approvalProgram: app.params.approvalProgram,\n clearStateProgram: app.params.clearStateProgram,\n creator: app.params.creator,\n localInts: Number(app.params.localStateSchema?.numUint ?? 0),\n localByteSlices: Number(app.params.localStateSchema?.numByteSlice ?? 0),\n globalInts: Number(app.params.globalStateSchema?.numUint ?? 0),\n globalByteSlices: Number(app.params.globalStateSchema?.numByteSlice ?? 0),\n extraProgramPages: Number(app.params.extraProgramPages ?? 0),\n globalState: AppManager.decodeAppState(app.params.globalState ?? []),\n }\n }\n\n /**\n * Returns the current global state values for the given app ID and account address\n *\n * @param appId The ID of the app to return global state for\n * @returns The current global state for the given app\n */\n public async getGlobalState(appId: bigint) {\n return (await this.getById(appId)).globalState\n }\n\n /**\n * Returns the current local state values for the given app ID and account address\n *\n * @param appId The ID of the app to return local state for\n * @param address The string address of the account to get local state for the given app\n * @returns The current local state for the given (app, account) combination\n */\n public async getLocalState(appId: bigint, address: string) {\n const appInfo = modelsv2.AccountApplicationResponse.from_obj_for_encoding(\n await this._algod.accountApplicationInformation(address, Number(appId)).do(),\n )\n\n if (!appInfo.appLocalState?.keyValue) {\n throw new Error(\"Couldn't find local state\")\n }\n\n return AppManager.decodeAppState(appInfo.appLocalState.keyValue)\n }\n\n /**\n * Returns the names of the current boxes for the given app.\n * @param appId The ID of the app return box names for\n * @returns The current box names\n */\n public async getBoxNames(appId: bigint): Promise<BoxName[]> {\n const boxResult = await this._algod.getApplicationBoxes(Number(appId)).do()\n return boxResult.boxes.map((b) => {\n return {\n nameRaw: b.name,\n nameBase64: Buffer.from(b.name).toString('base64'),\n name: Buffer.from(b.name).toString('utf-8'),\n }\n })\n }\n\n /**\n * Returns the value of the given box name for the given app.\n * @param appId The ID of the app return box names for\n * @param boxName The name of the box to return either as a string, binary array or `BoxName`\n * @returns The current box value as a byte array\n */\n public async getBoxValue(appId: bigint, boxName: BoxIdentifier): Promise<Uint8Array> {\n const name = AppManager.getBoxReference(boxName).name\n const boxResult = await this._algod.getApplicationBoxByName(Number(appId), name).do()\n return boxResult.value\n }\n\n /**\n * Returns the value of the given box names for the given app.\n * @param appId The ID of the app return box names for\n * @param boxNames The names of the boxes to return either as a string, binary array or `BoxName`\n * @returns The current box values as a byte array in the same order as the passed in box names\n */\n public async getBoxValues(appId: bigint, boxNames: BoxIdentifier[]): Promise<Uint8Array[]> {\n return await Promise.all(boxNames.map(async (boxName) => await this.getBoxValue(appId, boxName)))\n }\n\n /**\n * Returns the value of the given box name for the given app decoded based on the given ABI type.\n * @param request The parameters for the box value request\n * @returns The current box value as an ABI value\n */\n public async getBoxValueFromABIType(request: BoxValueRequestParams): Promise<algosdk.ABIValue> {\n const { appId, boxName, type } = request\n const value = await this.getBoxValue(appId, boxName)\n return type.decode(value)\n }\n\n /**\n * Returns the value of the given box names for the given app decoded based on the given ABI type.\n * @param request The parameters for the box value request\n * @returns The current box values as an ABI value in the same order as the passed in box names\n */\n public async getBoxValuesFromABIType(request: BoxValuesRequestParams): Promise<algosdk.ABIValue[]> {\n const { appId, boxNames, type } = request\n return await Promise.all(boxNames.map(async (boxName) => await this.getBoxValueFromABIType({ appId, boxName, type })))\n }\n\n /**\n * Returns a `algosdk.BoxReference` given a `BoxIdentifier` or `BoxReference`.\n * @param boxId The box to return a reference for\n * @returns The box reference ready to pass into a `algosdk.Transaction`\n */\n public static getBoxReference(boxId: BoxIdentifier | BoxReference): algosdk.BoxReference {\n const ref = typeof boxId === 'object' && 'appId' in boxId ? boxId : { appId: 0n, name: boxId }\n return {\n appIndex: Number(ref.appId),\n name:\n typeof ref.name === 'string'\n ? new TextEncoder().encode(ref.name)\n : 'length' in ref.name\n ? ref.name\n : algosdk.decodeAddress(ref.name.addr).publicKey,\n } as algosdk.BoxReference\n }\n\n /**\n * Converts an array of global/local state values from the algod api to a more friendly\n * generic object keyed by the UTF-8 value of the key.\n * @param state A `global-state`, `local-state`, `global-state-deltas` or `local-state-deltas`\n * @returns An object keyeed by the UTF-8 representation of the key with various parsings of the values\n */\n public static decodeAppState(state: { key: string; value: modelsv2.TealValue | modelsv2.EvalDelta }[]): AppState {\n const stateValues = {} as AppState\n\n // Start with empty set\n for (const stateVal of state) {\n const keyBase64 = stateVal.key\n const keyRaw = Buffer.from(keyBase64, 'base64')\n const key = keyRaw.toString('utf-8')\n const tealValue = stateVal.value\n\n const dataTypeFlag = 'action' in tealValue ? tealValue.action : tealValue.type\n let valueBase64: string\n let valueRaw: Buffer\n switch (dataTypeFlag) {\n case 1:\n valueBase64 = tealValue.bytes ?? ''\n valueRaw = Buffer.from(valueBase64, 'base64')\n stateValues[key] = {\n keyRaw,\n keyBase64,\n valueRaw: new Uint8Array(valueRaw),\n valueBase64: valueBase64,\n value: valueRaw.toString('utf-8'),\n }\n break\n case 2: {\n const value = tealValue.uint ?? 0\n stateValues[key] = {\n keyRaw,\n keyBase64,\n value: BigInt(value),\n }\n break\n }\n default:\n throw new Error(`Received unknown state data type of ${dataTypeFlag}`)\n }\n }\n\n return stateValues\n }\n\n /**\n * Returns any ABI return values for the given app call arguments and transaction confirmation.\n * @param confirmation The transaction confirmation from algod\n * @param method The ABI method\n * @returns The return value for the method call\n */\n public static getABIReturn(\n confirmation: modelsv2.PendingTransactionResponse | undefined,\n method: algosdk.ABIMethod | undefined,\n ): ABIReturn | undefined {\n if (!method || !confirmation || method.returns.type === 'void') {\n return undefined\n }\n\n // The parseMethodResponse method mutates the second parameter :(\n const resultDummy: algosdk.ABIResult = {\n txID: '',\n method,\n rawReturnValue: new Uint8Array(),\n }\n const response = algosdk.AtomicTransactionComposer.parseMethodResponse(method, resultDummy, confirmation)\n return !response.decodeError\n ? {\n rawReturnValue: response.rawReturnValue,\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n returnValue: response.returnValue!,\n decodeError: undefined,\n }\n : {\n rawReturnValue: undefined,\n returnValue: undefined,\n decodeError: response.decodeError,\n }\n }\n\n /**\n * Replaces AlgoKit deploy-time deployment control parameters within the given TEAL template code.\n *\n * * `TMPL_UPDATABLE` for updatability / immutability control\n * * `TMPL_DELETABLE` for deletability / permanence control\n *\n * Note: If these values are defined, but the corresponding `TMPL_*` value\n * isn't in the teal code it will throw an exception.\n *\n * @param tealTemplateCode The TEAL template code to substitute\n * @param params The deploy-time deployment control parameter value to replace\n * @returns The replaced TEAL code\n */\n static replaceTealTemplateDeployTimeControlParams(tealTemplateCode: string, params: { updatable?: boolean; deletable?: boolean }) {\n if (params.updatable !== undefined) {\n if (!tealTemplateCode.includes(UPDATABLE_TEMPLATE_NAME)) {\n throw new Error(\n `Deploy-time updatability control requested for app deployment, but ${UPDATABLE_TEMPLATE_NAME} not present in TEAL code`,\n )\n }\n tealTemplateCode = tealTemplateCode.replace(new RegExp(UPDATABLE_TEMPLATE_NAME, 'g'), (params.updatable ? 1 : 0).toString())\n }\n\n if (params.deletable !== undefined) {\n if (!tealTemplateCode.includes(DELETABLE_TEMPLATE_NAME)) {\n throw new Error(\n `Deploy-time deletability control requested for app deployment, but ${DELETABLE_TEMPLATE_NAME} not present in TEAL code`,\n )\n }\n tealTemplateCode = tealTemplateCode.replace(new RegExp(DELETABLE_TEMPLATE_NAME, 'g'), (params.deletable ? 1 : 0).toString())\n }\n\n return tealTemplateCode\n }\n\n /**\n * Performs template substitution of a teal file.\n *\n * Looks for `TMPL_{parameter}` for template replacements.\n *\n * @param tealTemplateCode The TEAL template code to make parameter replacements in\n * @param templateParams Any parameters to replace in the teal code\n * @returns The TEAL code with replacements\n */\n static replaceTealTemplateParams(tealTemplateCode: string, templateParams?: TealTemplateParams) {\n if (templateParams !== undefined) {\n for (const key in templateParams) {\n const value = templateParams[key]\n const token = `TMPL_${key.replace(/^TMPL_/, '')}`\n\n // If this is a number, first replace any byte representations of the number\n // These may appear in the TEAL in order to circumvent int compression and preserve PC values\n if (typeof value === 'number' || typeof value === 'bigint') {\n tealTemplateCode = tealTemplateCode.replace(new RegExp(`(?<=bytes )${token}`, 'g'), `0x${value.toString(16).padStart(16, '0')}`)\n\n // We could probably return here since mixing pushint and pushbytes is likely not going to happen, but might as well do both\n }\n\n tealTemplateCode = tealTemplateCode.replace(\n new RegExp(token, 'g'),\n typeof value === 'string'\n ? `0x${Buffer.from(value, 'utf-8').toString('hex')}`\n : ArrayBuffer.isView(value)\n ? `0x${Buffer.from(value).toString('hex')}`\n : value.toString(),\n )\n }\n }\n\n return tealTemplateCode\n }\n\n /**\n * Remove comments from TEAL code (useful to reduce code size before compilation).\n *\n * @param tealCode The TEAL logic to strip\n * @returns The TEAL without comments\n */\n static stripTealComments(tealCode: string) {\n // find // outside quotes, i.e. won't pick up \"//not a comment\"\n const regex = /\\/\\/(?=([^\"\\\\]*(\\\\.|\"([^\"\\\\]*\\\\.)*[^\"\\\\]*\"))*[^\"]*$)/\n\n tealCode = tealCode\n .split('\\n')\n .map((teal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C3C,0BAAE,KAAK,CAAC,QAAQ,EAAE,CACvB,CAAA;aACF;SACF;AAED,QAAA,OAAO,gBAAgB,CAAA;KACxB;AAED;;;;;AAKG;IACH,OAAO,iBAAiB,CAAC,QAAgB,EAAA;;QAEvC,MAAM,KAAK,GAAG,sDAAsD,CAAA;AAEpE,QAAA,QAAQ,GAAG,QAAQ;aAChB,KAAK,CAAC,IAAI,CAAC;AACX,aAAA,GAAG,CAAC,CAAC,YAAY,KAAI;AACpB,YAAA,OAAO,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAA;AAC5C,SAAC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,CAAA;AAEb,QAAA,OAAO,QAAQ,CAAA;KAChB;AACF;;;;"}
1
+ {"version":3,"file":"app-manager.js","sources":["../../src/types/app-manager.ts"],"sourcesContent":["import algosdk, { Address, ProgramSourceMap } from 'algosdk'\nimport { getABIReturnValue } from '../transaction/transaction'\nimport { TransactionSignerAccount } from './account'\nimport {\n BoxName,\n DELETABLE_TEMPLATE_NAME,\n UPDATABLE_TEMPLATE_NAME,\n type ABIReturn,\n type AppState,\n type CompiledTeal,\n type TealTemplateParams,\n} from './app'\nimport modelsv2 = algosdk.modelsv2\n\n/** Information about an app. */\nexport interface AppInformation {\n /** The ID of the app. */\n appId: bigint\n /** The escrow address that the app operates with. */\n appAddress: Address\n /**\n * Approval program.\n */\n approvalProgram: Uint8Array\n /**\n * Clear state program.\n */\n clearStateProgram: Uint8Array\n /**\n * The address that created this application. This is the address where the\n * parameters and global state for this application can be found.\n */\n creator: Address\n /**\n * Current global state values.\n */\n globalState: AppState\n /** The number of allocated ints in per-user local state. */\n localInts: number\n /** The number of allocated byte slices in per-user local state. */\n localByteSlices: number\n /** The number of allocated ints in global state. */\n globalInts: number\n /** The number of allocated byte slices in global state. */\n globalByteSlices: number\n /** Any extra pages that are needed for the smart contract. */\n extraProgramPages?: number\n}\n\n/**\n * Something that identifies an app box name - either a:\n * * `Uint8Array` (the actual binary of the box name)\n * * `string` (that will be encoded to a `Uint8Array`)\n * * `TransactionSignerAccount` (that will be encoded into the\n * public key address of the corresponding account)\n */\nexport type BoxIdentifier = string | Uint8Array | TransactionSignerAccount\n\n/**\n * A grouping of the app ID and name identifier to reference an app box.\n */\nexport interface BoxReference {\n /**\n * A unique application id\n */\n appId: bigint\n /**\n * Identifier for a box name\n */\n name: BoxIdentifier\n}\n\n/**\n * Parameters to get and decode a box value as an ABI type.\n */\nexport interface BoxValueRequestParams {\n /** The ID of the app return box names for */\n appId: bigint\n /** The name of the box to return either as a string, binary array or `BoxName` */\n boxName: BoxIdentifier\n /** The ABI type to decode the value using */\n type: algosdk.ABIType\n}\n\n/**\n * Parameters to get and decode a box value as an ABI type.\n */\nexport interface BoxValuesRequestParams {\n /** The ID of the app return box names for */\n appId: bigint\n /** The names of the boxes to return either as a string, binary array or BoxName` */\n boxNames: BoxIdentifier[]\n /** The ABI type to decode the value using */\n type: algosdk.ABIType\n}\n\n/** Allows management of application information. */\nexport class AppManager {\n private _algod: algosdk.Algodv2\n private _compilationResults: Record<string, CompiledTeal> = {}\n\n /**\n * Creates an `AppManager`\n * @param algod An algod instance\n */\n constructor(algod: algosdk.Algodv2) {\n this._algod = algod\n }\n\n /**\n * Compiles the given TEAL using algod and returns the result, including source map.\n *\n * The result of this compilation is also cached keyed by the TEAL\n * code so it can be retrieved via `getCompilationResult`.\n *\n * This function is re-entrant; it will only compile the same code once.\n *\n * @param tealCode The TEAL code\n * @returns The information about the compiled file\n */\n async compileTeal(tealCode: string): Promise<CompiledTeal> {\n if (this._compilationResults[tealCode]) {\n return this._compilationResults[tealCode]\n }\n\n const compiled = await this._algod.compile(tealCode).sourcemap(true).do()\n const result = {\n teal: tealCode,\n compiled: compiled.result,\n compiledHash: compiled.hash,\n compiledBase64ToBytes: new Uint8Array(Buffer.from(compiled.result, 'base64')),\n sourceMap: new ProgramSourceMap(JSON.parse(algosdk.encodeJSON(compiled.sourcemap!))),\n }\n this._compilationResults[tealCode] = result\n\n return result\n }\n\n /**\n * Performs template substitution of a teal template and compiles it, returning the compiled result.\n *\n * Looks for `TMPL_{parameter}` for template replacements and replaces AlgoKit deploy-time control parameters\n * if deployment metadata is specified.\n *\n * * `TMPL_UPDATABLE` for updatability / immutability control\n * * `TMPL_DELETABLE` for deletability / permanence control\n *\n * @param tealTemplateCode The TEAL logic to compile\n * @param templateParams Any parameters to replace in the .teal file before compiling\n * @param deploymentMetadata The deployment metadata the app will be deployed with\n * @returns The information about the compiled code\n */\n async compileTealTemplate(\n tealTemplateCode: string,\n templateParams?: TealTemplateParams,\n deploymentMetadata?: { updatable?: boolean; deletable?: boolean },\n ): Promise<CompiledTeal> {\n let tealCode = AppManager.stripTealComments(tealTemplateCode)\n\n tealCode = AppManager.replaceTealTemplateParams(tealCode, templateParams)\n\n if (deploymentMetadata) {\n tealCode = AppManager.replaceTealTemplateDeployTimeControlParams(tealCode, deploymentMetadata)\n }\n\n return await this.compileTeal(tealCode)\n }\n\n /**\n * Returns a previous compilation result.\n * @param tealCode The TEAL code\n * @returns The information about the previously compiled file\n * or `undefined` if that TEAL code wasn't previously compiled\n */\n getCompilationResult(tealCode: string): CompiledTeal | undefined {\n return this._compilationResults[tealCode]\n }\n\n /**\n * Returns the current app information for the app with the given ID.\n *\n * @example\n * ```typescript\n * const appInfo = await appManager.getById(12353n);\n * ```\n *\n * @param appId The ID of the app\n * @returns The app information\n */\n public async getById(appId: bigint): Promise<AppInformation> {\n const app = await this._algod.getApplicationByID(Number(appId)).do()\n return {\n appId: BigInt(app.id),\n appAddress: algosdk.getApplicationAddress(app.id),\n approvalProgram: app.params.approvalProgram,\n clearStateProgram: app.params.clearStateProgram,\n creator: app.params.creator,\n localInts: Number(app.params.localStateSchema?.numUint ?? 0),\n localByteSlices: Number(app.params.localStateSchema?.numByteSlice ?? 0),\n globalInts: Number(app.params.globalStateSchema?.numUint ?? 0),\n globalByteSlices: Number(app.params.globalStateSchema?.numByteSlice ?? 0),\n extraProgramPages: Number(app.params.extraProgramPages ?? 0),\n globalState: AppManager.decodeAppState(app.params.globalState ?? []),\n }\n }\n\n /**\n * Returns the current global state values for the given app ID and account address\n *\n * @param appId The ID of the app to return global state for\n * @returns The current global state for the given app\n */\n public async getGlobalState(appId: bigint) {\n return (await this.getById(appId)).globalState\n }\n\n /**\n * Returns the current local state values for the given app ID and account address\n *\n * @param appId The ID of the app to return local state for\n * @param address The string address of the account to get local state for the given app\n * @returns The current local state for the given (app, account) combination\n */\n public async getLocalState(appId: bigint, address: Address | string) {\n const appInfo = await this._algod.accountApplicationInformation(address, appId).do()\n\n if (!appInfo.appLocalState?.keyValue) {\n throw new Error(\"Couldn't find local state\")\n }\n\n return AppManager.decodeAppState(appInfo.appLocalState.keyValue)\n }\n\n /**\n * Returns the names of the current boxes for the given app.\n * @param appId The ID of the app return box names for\n * @returns The current box names\n */\n public async getBoxNames(appId: bigint): Promise<BoxName[]> {\n const boxResult = await this._algod.getApplicationBoxes(appId).do()\n return boxResult.boxes.map((b) => {\n return {\n nameRaw: b.name,\n nameBase64: Buffer.from(b.name).toString('base64'),\n name: Buffer.from(b.name).toString('utf-8'),\n }\n })\n }\n\n /**\n * Returns the value of the given box name for the given app.\n * @param appId The ID of the app return box names for\n * @param boxName The name of the box to return either as a string, binary array or `BoxName`\n * @returns The current box value as a byte array\n */\n public async getBoxValue(appId: bigint, boxName: BoxIdentifier): Promise<Uint8Array> {\n const name = AppManager.getBoxReference(boxName).name\n const boxResult = await this._algod.getApplicationBoxByName(Number(appId), name).do()\n return boxResult.value\n }\n\n /**\n * Returns the value of the given box names for the given app.\n * @param appId The ID of the app return box names for\n * @param boxNames The names of the boxes to return either as a string, binary array or `BoxName`\n * @returns The current box values as a byte array in the same order as the passed in box names\n */\n public async getBoxValues(appId: bigint, boxNames: BoxIdentifier[]): Promise<Uint8Array[]> {\n return await Promise.all(boxNames.map(async (boxName) => await this.getBoxValue(appId, boxName)))\n }\n\n /**\n * Returns the value of the given box name for the given app decoded based on the given ABI type.\n * @param request The parameters for the box value request\n * @returns The current box value as an ABI value\n */\n public async getBoxValueFromABIType(request: BoxValueRequestParams): Promise<algosdk.ABIValue> {\n const { appId, boxName, type } = request\n const value = await this.getBoxValue(appId, boxName)\n return type.decode(value)\n }\n\n /**\n * Returns the value of the given box names for the given app decoded based on the given ABI type.\n * @param request The parameters for the box value request\n * @returns The current box values as an ABI value in the same order as the passed in box names\n */\n public async getBoxValuesFromABIType(request: BoxValuesRequestParams): Promise<algosdk.ABIValue[]> {\n const { appId, boxNames, type } = request\n return await Promise.all(boxNames.map(async (boxName) => await this.getBoxValueFromABIType({ appId, boxName, type })))\n }\n\n /**\n * Returns a `algosdk.BoxReference` given a `BoxIdentifier` or `BoxReference`.\n * @param boxId The box to return a reference for\n * @returns The box reference ready to pass into a `algosdk.Transaction`\n */\n public static getBoxReference(boxId: BoxIdentifier | BoxReference): algosdk.BoxReference {\n const ref = typeof boxId === 'object' && 'appId' in boxId ? boxId : { appId: 0n, name: boxId }\n return {\n appIndex: ref.appId,\n name: typeof ref.name === 'string' ? new TextEncoder().encode(ref.name) : 'length' in ref.name ? ref.name : ref.name.addr.publicKey,\n } as algosdk.BoxReference\n }\n\n /**\n * Converts an array of global/local state values from the algod api to a more friendly\n * generic object keyed by the UTF-8 value of the key.\n * @param state A `global-state`, `local-state`, `global-state-deltas` or `local-state-deltas`\n * @returns An object keyeed by the UTF-8 representation of the key with various parsings of the values\n */\n public static decodeAppState(state: { key: Uint8Array; value: modelsv2.TealValue | modelsv2.EvalDelta }[]): AppState {\n const stateValues = {} as AppState\n\n // Start with empty set\n for (const stateVal of state) {\n const keyBase64 = Buffer.from(stateVal.key).toString('base64')\n const keyRaw = stateVal.key\n const key = Buffer.from(stateVal.key).toString('utf-8')\n const tealValue = stateVal.value\n\n const dataTypeFlag = 'action' in tealValue ? tealValue.action : tealValue.type\n let valueBase64: string\n let valueRaw: Buffer\n switch (dataTypeFlag) {\n case 1:\n valueBase64 =\n typeof tealValue.bytes === 'string' ? tealValue.bytes : tealValue.bytes ? Buffer.from(tealValue.bytes).toString('base64') : ''\n valueRaw = Buffer.from(valueBase64, 'base64')\n stateValues[key] = {\n keyRaw,\n keyBase64,\n valueRaw: new Uint8Array(valueRaw),\n valueBase64: valueBase64,\n value: valueRaw.toString('utf-8'),\n }\n break\n case 2: {\n const value = tealValue.uint ?? 0\n stateValues[key] = {\n keyRaw,\n keyBase64,\n value: BigInt(value),\n }\n break\n }\n default:\n throw new Error(`Received unknown state data type of ${dataTypeFlag}`)\n }\n }\n\n return stateValues\n }\n\n /**\n * Returns any ABI return values for the given app call arguments and transaction confirmation.\n * @param confirmation The transaction confirmation from algod\n * @param method The ABI method\n * @returns The return value for the method call\n */\n public static getABIReturn(\n confirmation: modelsv2.PendingTransactionResponse | undefined,\n method: algosdk.ABIMethod | undefined,\n ): ABIReturn | undefined {\n if (!method || !confirmation || method.returns.type === 'void') {\n return undefined\n }\n\n // The parseMethodResponse method mutates the second parameter :(\n const resultDummy: algosdk.ABIResult = {\n txID: '',\n method,\n rawReturnValue: new Uint8Array(),\n }\n return getABIReturnValue(algosdk.AtomicTransactionComposer.parseMethodResponse(method, resultDummy, confirmation))\n }\n\n /**\n * Replaces AlgoKit deploy-time deployment control parameters within the given TEAL template code.\n *\n * * `TMPL_UPDATABLE` for updatability / immutability control\n * * `TMPL_DELETABLE` for deletability / permanence control\n *\n * Note: If these values are defined, but the corresponding `TMPL_*` value\n * isn't in the teal code it will throw an exception.\n *\n * @param tealTemplateCode The TEAL template code to substitute\n * @param params The deploy-time deployment control parameter value to replace\n * @returns The replaced TEAL code\n */\n static replaceTealTemplateDeployTimeControlParams(tealTemplateCode: string, params: { updatable?: boolean; deletable?: boolean }) {\n if (params.updatable !== undefined) {\n if (!tealTemplateCode.includes(UPDATABLE_TEMPLATE_NAME)) {\n throw new Error(\n `Deploy-time updatability control requested for app deployment, but ${UPDATABLE_TEMPLATE_NAME} not present in TEAL code`,\n )\n }\n tealTemplateCode = replaceTemplateVariable(tealTemplateCode, UPDATABLE_TEMPLATE_NAME, (params.updatable ? 1 : 0).toString())\n }\n\n if (params.deletable !== undefined) {\n if (!tealTemplateCode.includes(DELETABLE_TEMPLATE_NAME)) {\n throw new Error(\n `Deploy-time deletability control requested for app deployment, but ${DELETABLE_TEMPLATE_NAME} not present in TEAL code`,\n )\n }\n tealTemplateCode = replaceTemplateVariable(tealTemplateCode, DELETABLE_TEMPLATE_NAME, (params.deletable ? 1 : 0).toString())\n }\n\n return tealTemplateCode\n }\n\n /**\n * Performs template substitution of a teal file.\n *\n * Looks for `TMPL_{parameter}` for template replacements.\n *\n * @param tealTemplateCode The TEAL template code to make parameter replacements in\n * @param templateParams Any parameters to replace in the teal code\n * @returns The TEAL code with replacements\n */\n static replaceTealTemplateParams(tealTemplateCode: string, templateParams?: TealTemplateParams) {\n if (templateParams !== undefined) {\n for (const key in templateParams) {\n const value = templateParams[key]\n const token = `TMPL_${key.replace(/^TMPL_/, '')}`\n\n // If this is a number, first replace any byte representations of the number\n // These may appear in the TEAL in order to circumvent int compression and preserve PC values\n if (typeof value === 'number' || typeof value === 'bigint') {\n tealTemplateCode = tealTemplateCode.replace(new RegExp(`(?<=bytes )${token}`, 'g'), `0x${value.toString(16).padStart(16, '0')}`)\n\n // We could probably return here since mixing pushint and pushbytes is likely not going to happen, but might as well do both\n }\n\n tealTemplateCode = replaceTemplateVariable(\n tealTemplateCode,\n token,\n typeof value === 'string'\n ? `0x${Buffer.from(value, 'utf-8').toString('hex')}`\n : ArrayBuffer.isView(value)\n ? `0x${Buffer.from(value).toString('hex')}`\n : value.toString(),\n )\n }\n }\n\n return tealTemplateCode\n }\n\n /**\n * Remove comments from TEAL code (useful to reduce code size before compilation).\n *\n * @param tealCode The TEAL logic to strip\n * @returns The TEAL without comments\n */\n static stripTealComments(tealCode: string) {\n const stripCommentFromLine = (line: string) => {\n const commentIndex = findUnquotedString(line, '//')\n if (commentIndex === undefined) {\n return line\n }\n return line.slice(0, commentIndex).trimEnd()\n }\n\n return tealCode\n .split('\\n')\n .map((line) => stripCommentFromLine(line))\n .join('\\n')\n }\n}\n\n/**\n * Find the first string within a line of TEAL. Only matches outside of quotes and base64 are returned.\n * Returns undefined if not found\n */\nconst findUnquotedString = (line: string, token: string, startIndex: number = 0, _endIndex?: number): number | undefined => {\n const endIndex = _endIndex === undefined ? line.length : _endIndex\n let index = startIndex\n let inQuotes = false\n let inBase64 = false\n\n while (index < endIndex) {\n const currentChar = line[index]\n if ((currentChar === ' ' || currentChar === '(') && !inQuotes && lastTokenBase64(line, index)) {\n // enter base64\n inBase64 = true\n } else if ((currentChar === ' ' || currentChar === ')') && !inQuotes && inBase64) {\n // exit base64\n inBase64 = false\n } else if (currentChar === '\\\\' && inQuotes) {\n // escaped char, skip next character\n index += 1\n } else if (currentChar === '\"') {\n // quote boundary\n inQuotes = !inQuotes\n } else if (!inQuotes && !inBase64 && line.startsWith(token, index)) {\n // can test for match\n return index\n }\n index += 1\n }\n return undefined\n}\n\nconst lastTokenBase64 = (line: string, index: number): boolean => {\n try {\n const tokens = line.slice(0, index).split(/\\s+/)\n const last = tokens[tokens.length - 1]\n return ['base64', 'b64'].includes(last)\n } catch {\n return false\n }\n}\n\nfunction replaceTemplateVariable(program: string, token: string, replacement: string): string {\n const result: string[] = []\n const tokenIndexOffset = replacement.length - token.length\n\n const programLines = program.split('\\n')\n\n for (const line of programLines) {\n const _commentIndex = findUnquotedString(line, '//')\n const commentIndex = _commentIndex === undefined ? line.length : _commentIndex\n let code = line.substring(0, commentIndex)\n const comment = line.substring(commentIndex)\n let trailingIndex = 0\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n const tokenIndex = findTemplateToken(code, token, trailingIndex)\n if (tokenIndex === undefined) {\n break\n }\n trailingIndex = tokenIndex + token.length\n const prefix = code.substring(0, tokenIndex)\n const suffix = code.substring(trailingIndex)\n code = `${prefix}${replacement}${suffix}`\n trailingIndex += tokenIndexOffset\n }\n result.push(code + comment)\n }\n\n return result.join('\\n')\n}\n\nconst findTemplateToken = (line: string, token: string, startIndex: number = 0, _endIndex?: number): number | undefined => {\n const endIndex = _endIndex === undefined ? line.length : _endIndex\n\n let index = startIndex\n while (index < endIndex) {\n const tokenIndex = findUnquotedString(line, token, index, endIndex)\n if (tokenIndex === undefined) {\n break\n }\n const trailingIndex = tokenIndex + token.length\n if (\n (tokenIndex === 0 || !isValidTokenCharacter(line[tokenIndex - 1])) &&\n (trailingIndex >= line.length || !isValidTokenCharacter(line[trailingIndex]))\n ) {\n return tokenIndex\n }\n index = trailingIndex\n }\n return undefined\n}\n\nfunction isValidTokenCharacter(char: string): boolean {\n return char.length === 1 && (/\\w/.test(char) || char === '_')\n}\n"],"names":["ProgramSourceMap","getABIReturnValue","UPDATABLE_TEMPLATE_NAME","DELETABLE_TEMPLATE_NAME"],"mappings":";;;;;;AAgGA;MACa,UAAU,CAAA;AAIrB;;;AAGG;AACH,IAAA,WAAA,CAAY,KAAsB,EAAA;QAN1B,IAAmB,CAAA,mBAAA,GAAiC,EAAE;AAO5D,QAAA,IAAI,CAAC,MAAM,GAAG,KAAK;;AAGrB;;;;;;;;;;AAUG;IACH,MAAM,WAAW,CAAC,QAAgB,EAAA;AAChC,QAAA,IAAI,IAAI,CAAC,mBAAmB,CAAC,QAAQ,CAAC,EAAE;AACtC,YAAA,OAAO,IAAI,CAAC,mBAAmB,CAAC,QAAQ,CAAC;;AAG3C,QAAA,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE;AACzE,QAAA,MAAM,MAAM,GAAG;AACb,YAAA,IAAI,EAAE,QAAQ;YACd,QAAQ,EAAE,QAAQ,CAAC,MAAM;YACzB,YAAY,EAAE,QAAQ,CAAC,IAAI;AAC3B,YAAA,qBAAqB,EAAE,IAAI,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC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+ import { getABIReturnValue } from '../transaction/transaction.mjs';
2
3
  import { UPDATABLE_TEMPLATE_NAME, DELETABLE_TEMPLATE_NAME } from './app.mjs';
3
4
 
4
- var modelsv2 = algosdk.modelsv2;
5
5
  /** Allows management of application information. */
6
6
  class AppManager {
7
7
  /**
@@ -33,7 +33,7 @@ class AppManager {
33
33
  compiled: compiled.result,
34
34
  compiledHash: compiled.hash,
35
35
  compiledBase64ToBytes: new Uint8Array(Buffer.from(compiled.result, 'base64')),
36
- sourceMap: new algosdk.SourceMap(compiled['sourcemap']),
36
+ sourceMap: new ProgramSourceMap(JSON.parse(algosdk.encodeJSON(compiled.sourcemap))),
37
37
  };
38
38
  this._compilationResults[tealCode] = result;
39
39
  return result;
@@ -81,7 +81,7 @@ class AppManager {
81
81
  * @returns The app information
82
82
  */
83
83
  async getById(appId) {
84
- const app = modelsv2.Application.from_obj_for_encoding(await this._algod.getApplicationByID(Number(appId)).do());
84
+ const app = await this._algod.getApplicationByID(Number(appId)).do();
85
85
  return {
86
86
  appId: BigInt(app.id),
87
87
  appAddress: algosdk.getApplicationAddress(app.id),
@@ -113,7 +113,7 @@ class AppManager {
113
113
  * @returns The current local state for the given (app, account) combination
114
114
  */
115
115
  async getLocalState(appId, address) {
116
- const appInfo = modelsv2.AccountApplicationResponse.from_obj_for_encoding(await this._algod.accountApplicationInformation(address, Number(appId)).do());
116
+ const appInfo = await this._algod.accountApplicationInformation(address, appId).do();
117
117
  if (!appInfo.appLocalState?.keyValue) {
118
118
  throw new Error("Couldn't find local state");
119
119
  }
@@ -125,7 +125,7 @@ class AppManager {
125
125
  * @returns The current box names
126
126
  */
127
127
  async getBoxNames(appId) {
128
- const boxResult = await this._algod.getApplicationBoxes(Number(appId)).do();
128
+ const boxResult = await this._algod.getApplicationBoxes(appId).do();
129
129
  return boxResult.boxes.map((b) => {
130
130
  return {
131
131
  nameRaw: b.name,
@@ -181,12 +181,8 @@ class AppManager {
181
181
  static getBoxReference(boxId) {
182
182
  const ref = typeof boxId === 'object' && 'appId' in boxId ? boxId : { appId: 0n, name: boxId };
183
183
  return {
184
- appIndex: Number(ref.appId),
185
- name: typeof ref.name === 'string'
186
- ? new TextEncoder().encode(ref.name)
187
- : 'length' in ref.name
188
- ? ref.name
189
- : algosdk.decodeAddress(ref.name.addr).publicKey,
184
+ appIndex: ref.appId,
185
+ name: typeof ref.name === 'string' ? new TextEncoder().encode(ref.name) : 'length' in ref.name ? ref.name : ref.name.addr.publicKey,
190
186
  };
191
187
  }
192
188
  /**
@@ -199,16 +195,17 @@ class AppManager {
199
195
  const stateValues = {};
200
196
  // Start with empty set
201
197
  for (const stateVal of state) {
202
- const keyBase64 = stateVal.key;
203
- const keyRaw = Buffer.from(keyBase64, 'base64');
204
- const key = keyRaw.toString('utf-8');
198
+ const keyBase64 = Buffer.from(stateVal.key).toString('base64');
199
+ const keyRaw = stateVal.key;
200
+ const key = Buffer.from(stateVal.key).toString('utf-8');
205
201
  const tealValue = stateVal.value;
206
202
  const dataTypeFlag = 'action' in tealValue ? tealValue.action : tealValue.type;
207
203
  let valueBase64;
208
204
  let valueRaw;
209
205
  switch (dataTypeFlag) {
210
206
  case 1:
211
- valueBase64 = tealValue.bytes ?? '';
207
+ valueBase64 =
208
+ typeof tealValue.bytes === 'string' ? tealValue.bytes : tealValue.bytes ? Buffer.from(tealValue.bytes).toString('base64') : '';
212
209
  valueRaw = Buffer.from(valueBase64, 'base64');
213
210
  stateValues[key] = {
214
211
  keyRaw,
@@ -249,19 +246,7 @@ class AppManager {
249
246
  method,
250
247
  rawReturnValue: new Uint8Array(),
251
248
  };
252
- const response = algosdk.AtomicTransactionComposer.parseMethodResponse(method, resultDummy, confirmation);
253
- return !response.decodeError
254
- ? {
255
- rawReturnValue: response.rawReturnValue,
256
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
257
- returnValue: response.returnValue,
258
- decodeError: undefined,
259
- }
260
- : {
261
- rawReturnValue: undefined,
262
- returnValue: undefined,
263
- decodeError: response.decodeError,
264
- };
249
+ return getABIReturnValue(algosdk.AtomicTransactionComposer.parseMethodResponse(method, resultDummy, confirmation));
265
250
  }
266
251
  /**
267
252
  * Replaces AlgoKit deploy-time deployment control parameters within the given TEAL template code.
@@ -281,13 +266,13 @@ class AppManager {
281
266
  if (!tealTemplateCode.includes(UPDATABLE_TEMPLATE_NAME)) {
282
267
  throw new Error(`Deploy-time updatability control requested for app deployment, but ${UPDATABLE_TEMPLATE_NAME} not present in TEAL code`);
283
268
  }
284
- tealTemplateCode = tealTemplateCode.replace(new RegExp(UPDATABLE_TEMPLATE_NAME, 'g'), (params.updatable ? 1 : 0).toString());
269
+ tealTemplateCode = replaceTemplateVariable(tealTemplateCode, UPDATABLE_TEMPLATE_NAME, (params.updatable ? 1 : 0).toString());
285
270
  }
286
271
  if (params.deletable !== undefined) {
287
272
  if (!tealTemplateCode.includes(DELETABLE_TEMPLATE_NAME)) {
288
273
  throw new Error(`Deploy-time deletability control requested for app deployment, but ${DELETABLE_TEMPLATE_NAME} not present in TEAL code`);
289
274
  }
290
- tealTemplateCode = tealTemplateCode.replace(new RegExp(DELETABLE_TEMPLATE_NAME, 'g'), (params.deletable ? 1 : 0).toString());
275
+ tealTemplateCode = replaceTemplateVariable(tealTemplateCode, DELETABLE_TEMPLATE_NAME, (params.deletable ? 1 : 0).toString());
291
276
  }
292
277
  return tealTemplateCode;
293
278
  }
@@ -311,7 +296,7 @@ class AppManager {
311
296
  tealTemplateCode = tealTemplateCode.replace(new RegExp(`(?<=bytes )${token}`, 'g'), `0x${value.toString(16).padStart(16, '0')}`);
312
297
  // We could probably return here since mixing pushint and pushbytes is likely not going to happen, but might as well do both
313
298
  }
314
- tealTemplateCode = tealTemplateCode.replace(new RegExp(token, 'g'), typeof value === 'string'
299
+ tealTemplateCode = replaceTemplateVariable(tealTemplateCode, token, typeof value === 'string'
315
300
  ? `0x${Buffer.from(value, 'utf-8').toString('hex')}`
316
301
  : ArrayBuffer.isView(value)
317
302
  ? `0x${Buffer.from(value).toString('hex')}`
@@ -327,17 +312,110 @@ class AppManager {
327
312
  * @returns The TEAL without comments
328
313
  */
329
314
  static stripTealComments(tealCode) {
330
- // find // outside quotes, i.e. won't pick up "//not a comment"
331
- const regex = /\/\/(?=([^"\\]*(\\.|"([^"\\]*\\.)*[^"\\]*"))*[^"]*$)/;
332
- tealCode = tealCode
315
+ const stripCommentFromLine = (line) => {
316
+ const commentIndex = findUnquotedString(line, '//');
317
+ if (commentIndex === undefined) {
318
+ return line;
319
+ }
320
+ return line.slice(0, commentIndex).trimEnd();
321
+ };
322
+ return tealCode
333
323
  .split('\n')
334
- .map((tealCodeLine) => {
335
- return tealCodeLine.split(regex)[0].trim();
336
- })
324
+ .map((line) => stripCommentFromLine(line))
337
325
  .join('\n');
338
- return tealCode;
339
326
  }
340
327
  }
328
+ /**
329
+ * Find the first string within a line of TEAL. Only matches outside of quotes and base64 are returned.
330
+ * Returns undefined if not found
331
+ */
332
+ const findUnquotedString = (line, token, startIndex = 0, _endIndex) => {
333
+ const endIndex = _endIndex === undefined ? line.length : _endIndex;
334
+ let index = startIndex;
335
+ let inQuotes = false;
336
+ let inBase64 = false;
337
+ while (index < endIndex) {
338
+ const currentChar = line[index];
339
+ if ((currentChar === ' ' || currentChar === '(') && !inQuotes && lastTokenBase64(line, index)) {
340
+ // enter base64
341
+ inBase64 = true;
342
+ }
343
+ else if ((currentChar === ' ' || currentChar === ')') && !inQuotes && inBase64) {
344
+ // exit base64
345
+ inBase64 = false;
346
+ }
347
+ else if (currentChar === '\\' && inQuotes) {
348
+ // escaped char, skip next character
349
+ index += 1;
350
+ }
351
+ else if (currentChar === '"') {
352
+ // quote boundary
353
+ inQuotes = !inQuotes;
354
+ }
355
+ else if (!inQuotes && !inBase64 && line.startsWith(token, index)) {
356
+ // can test for match
357
+ return index;
358
+ }
359
+ index += 1;
360
+ }
361
+ return undefined;
362
+ };
363
+ const lastTokenBase64 = (line, index) => {
364
+ try {
365
+ const tokens = line.slice(0, index).split(/\s+/);
366
+ const last = tokens[tokens.length - 1];
367
+ return ['base64', 'b64'].includes(last);
368
+ }
369
+ catch {
370
+ return false;
371
+ }
372
+ };
373
+ function replaceTemplateVariable(program, token, replacement) {
374
+ const result = [];
375
+ const tokenIndexOffset = replacement.length - token.length;
376
+ const programLines = program.split('\n');
377
+ for (const line of programLines) {
378
+ const _commentIndex = findUnquotedString(line, '//');
379
+ const commentIndex = _commentIndex === undefined ? line.length : _commentIndex;
380
+ let code = line.substring(0, commentIndex);
381
+ const comment = line.substring(commentIndex);
382
+ let trailingIndex = 0;
383
+ // eslint-disable-next-line no-constant-condition
384
+ while (true) {
385
+ const tokenIndex = findTemplateToken(code, token, trailingIndex);
386
+ if (tokenIndex === undefined) {
387
+ break;
388
+ }
389
+ trailingIndex = tokenIndex + token.length;
390
+ const prefix = code.substring(0, tokenIndex);
391
+ const suffix = code.substring(trailingIndex);
392
+ code = `${prefix}${replacement}${suffix}`;
393
+ trailingIndex += tokenIndexOffset;
394
+ }
395
+ result.push(code + comment);
396
+ }
397
+ return result.join('\n');
398
+ }
399
+ const findTemplateToken = (line, token, startIndex = 0, _endIndex) => {
400
+ const endIndex = line.length ;
401
+ let index = startIndex;
402
+ while (index < endIndex) {
403
+ const tokenIndex = findUnquotedString(line, token, index, endIndex);
404
+ if (tokenIndex === undefined) {
405
+ break;
406
+ }
407
+ const trailingIndex = tokenIndex + token.length;
408
+ if ((tokenIndex === 0 || !isValidTokenCharacter(line[tokenIndex - 1])) &&
409
+ (trailingIndex >= line.length || !isValidTokenCharacter(line[trailingIndex]))) {
410
+ return tokenIndex;
411
+ }
412
+ index = trailingIndex;
413
+ }
414
+ return undefined;
415
+ };
416
+ function isValidTokenCharacter(char) {
417
+ return char.length === 1 && (/\w/.test(char) || char === '_');
418
+ }
341
419
 
342
420
  export { AppManager };
343
421
  //# sourceMappingURL=app-manager.mjs.map