@algorandfoundation/algokit-utils 10.0.0-alpha.8 → 10.0.0-alpha.9

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 (233) hide show
  1. package/_virtual/rolldown_runtime.js +20 -11
  2. package/_virtual/rolldown_runtime.mjs +10 -5
  3. package/algod-client/index.d.ts +2 -2
  4. package/package.json +1 -1
  5. package/packages/abi/src/abi-type.d.ts +1 -1
  6. package/packages/abi/src/abi-type.js +2 -1
  7. package/packages/abi/src/abi-type.js.map +1 -1
  8. package/packages/abi/src/abi-type.mjs +2 -1
  9. package/packages/abi/src/abi-type.mjs.map +1 -1
  10. package/packages/abi/src/arc56-contract.js +1 -0
  11. package/packages/abi/src/arc56-contract.js.map +1 -1
  12. package/packages/abi/src/arc56-contract.mjs +1 -0
  13. package/packages/abi/src/arc56-contract.mjs.map +1 -1
  14. package/packages/algod_client/src/apis/api-service.d.ts +44 -44
  15. package/packages/algod_client/src/apis/api-service.js +153 -153
  16. package/packages/algod_client/src/apis/api-service.js.map +1 -1
  17. package/packages/algod_client/src/apis/api-service.mjs +153 -153
  18. package/packages/algod_client/src/apis/api-service.mjs.map +1 -1
  19. package/packages/algod_client/src/core/api-error.js +3 -1
  20. package/packages/algod_client/src/core/api-error.js.map +1 -1
  21. package/packages/algod_client/src/core/api-error.mjs +3 -1
  22. package/packages/algod_client/src/core/api-error.mjs.map +1 -1
  23. package/packages/algod_client/src/core/model-runtime.js +4 -4
  24. package/packages/algod_client/src/core/model-runtime.js.map +1 -1
  25. package/packages/algod_client/src/core/model-runtime.mjs +6 -6
  26. package/packages/algod_client/src/core/model-runtime.mjs.map +1 -1
  27. package/packages/algod_client/src/models/account-participation.js +4 -4
  28. package/packages/algod_client/src/models/account-participation.js.map +1 -1
  29. package/packages/algod_client/src/models/account-participation.mjs +4 -4
  30. package/packages/algod_client/src/models/account-participation.mjs.map +1 -1
  31. package/packages/algod_client/src/models/asset-params.js +2 -1
  32. package/packages/algod_client/src/models/asset-params.js.map +1 -1
  33. package/packages/algod_client/src/models/asset-params.mjs +2 -1
  34. package/packages/algod_client/src/models/asset-params.mjs.map +1 -1
  35. package/packages/algod_client/src/models/block-response.js +1 -2
  36. package/packages/algod_client/src/models/block-response.js.map +1 -1
  37. package/packages/algod_client/src/models/block-response.mjs +2 -3
  38. package/packages/algod_client/src/models/block-response.mjs.map +1 -1
  39. package/packages/algod_client/src/models/block.d.ts +47 -27
  40. package/packages/algod_client/src/models/block.js +173 -97
  41. package/packages/algod_client/src/models/block.js.map +1 -1
  42. package/packages/algod_client/src/models/block.mjs +173 -97
  43. package/packages/algod_client/src/models/block.mjs.map +1 -1
  44. package/packages/algod_client/src/models/ledger-state-delta.js +1 -1
  45. package/packages/algod_client/src/models/ledger-state-delta.js.map +1 -1
  46. package/packages/algod_client/src/models/ledger-state-delta.mjs +2 -2
  47. package/packages/algod_client/src/models/ledger-state-delta.mjs.map +1 -1
  48. package/packages/algod_client/src/models/transaction-parameters-response.js +2 -2
  49. package/packages/algod_client/src/models/transaction-parameters-response.js.map +1 -1
  50. package/packages/algod_client/src/models/transaction-parameters-response.mjs +2 -2
  51. package/packages/algod_client/src/models/transaction-parameters-response.mjs.map +1 -1
  52. package/packages/common/src/codecs/composite/map.js +7 -4
  53. package/packages/common/src/codecs/composite/map.js.map +1 -1
  54. package/packages/common/src/codecs/composite/map.mjs +7 -4
  55. package/packages/common/src/codecs/composite/map.mjs.map +1 -1
  56. package/packages/common/src/codecs/composite/record.js +0 -1
  57. package/packages/common/src/codecs/composite/record.js.map +1 -1
  58. package/packages/common/src/codecs/primitives/address.js +0 -1
  59. package/packages/common/src/codecs/primitives/address.js.map +1 -1
  60. package/packages/common/src/codecs/primitives/bytes.js +0 -1
  61. package/packages/common/src/codecs/primitives/bytes.js.map +1 -1
  62. package/packages/common/src/codecs/primitives/fixed-bytes.js +0 -1
  63. package/packages/common/src/codecs/primitives/fixed-bytes.js.map +1 -1
  64. package/packages/common/src/codecs/wire.js +0 -1
  65. package/packages/common/src/codecs/wire.js.map +1 -1
  66. package/packages/common/src/msgpack.js +0 -1
  67. package/packages/common/src/msgpack.js.map +1 -1
  68. package/packages/indexer_client/src/apis/api-service.d.ts +1 -1
  69. package/packages/indexer_client/src/apis/api-service.js +12 -12
  70. package/packages/indexer_client/src/apis/api-service.js.map +1 -1
  71. package/packages/indexer_client/src/apis/api-service.mjs +12 -12
  72. package/packages/indexer_client/src/apis/api-service.mjs.map +1 -1
  73. package/packages/indexer_client/src/core/api-error.js +3 -1
  74. package/packages/indexer_client/src/core/api-error.js.map +1 -1
  75. package/packages/indexer_client/src/core/api-error.mjs +3 -1
  76. package/packages/indexer_client/src/core/api-error.mjs.map +1 -1
  77. package/packages/indexer_client/src/core/model-runtime.js +4 -4
  78. package/packages/indexer_client/src/core/model-runtime.js.map +1 -1
  79. package/packages/indexer_client/src/core/model-runtime.mjs +6 -6
  80. package/packages/indexer_client/src/core/model-runtime.mjs.map +1 -1
  81. package/packages/indexer_client/src/models/account-participation.js +4 -4
  82. package/packages/indexer_client/src/models/account-participation.js.map +1 -1
  83. package/packages/indexer_client/src/models/account-participation.mjs +4 -4
  84. package/packages/indexer_client/src/models/account-participation.mjs.map +1 -1
  85. package/packages/indexer_client/src/models/asset-params.js +2 -1
  86. package/packages/indexer_client/src/models/asset-params.js.map +1 -1
  87. package/packages/indexer_client/src/models/asset-params.mjs +2 -1
  88. package/packages/indexer_client/src/models/asset-params.mjs.map +1 -1
  89. package/packages/indexer_client/src/models/block.js +8 -8
  90. package/packages/indexer_client/src/models/block.js.map +1 -1
  91. package/packages/indexer_client/src/models/block.mjs +8 -8
  92. package/packages/indexer_client/src/models/block.mjs.map +1 -1
  93. package/packages/indexer_client/src/models/eval-delta-key-value.d.ts +1 -1
  94. package/packages/indexer_client/src/models/eval-delta-key-value.js +2 -2
  95. package/packages/indexer_client/src/models/eval-delta-key-value.js.map +1 -1
  96. package/packages/indexer_client/src/models/eval-delta-key-value.mjs +2 -2
  97. package/packages/indexer_client/src/models/eval-delta-key-value.mjs.map +1 -1
  98. package/packages/indexer_client/src/models/eval-delta.d.ts +1 -1
  99. package/packages/indexer_client/src/models/eval-delta.js +2 -2
  100. package/packages/indexer_client/src/models/eval-delta.js.map +1 -1
  101. package/packages/indexer_client/src/models/eval-delta.mjs +2 -2
  102. package/packages/indexer_client/src/models/eval-delta.mjs.map +1 -1
  103. package/packages/indexer_client/src/models/hb-proof-fields.js +6 -6
  104. package/packages/indexer_client/src/models/hb-proof-fields.js.map +1 -1
  105. package/packages/indexer_client/src/models/hb-proof-fields.mjs +6 -6
  106. package/packages/indexer_client/src/models/hb-proof-fields.mjs.map +1 -1
  107. package/packages/indexer_client/src/models/state-proof-verifier.js +2 -2
  108. package/packages/indexer_client/src/models/state-proof-verifier.js.map +1 -1
  109. package/packages/indexer_client/src/models/state-proof-verifier.mjs +2 -2
  110. package/packages/indexer_client/src/models/state-proof-verifier.mjs.map +1 -1
  111. package/packages/indexer_client/src/models/teal-key-value.d.ts +1 -1
  112. package/packages/indexer_client/src/models/teal-key-value.js +2 -2
  113. package/packages/indexer_client/src/models/teal-key-value.js.map +1 -1
  114. package/packages/indexer_client/src/models/teal-key-value.mjs +2 -2
  115. package/packages/indexer_client/src/models/teal-key-value.mjs.map +1 -1
  116. package/packages/indexer_client/src/models/transaction-heartbeat.js +2 -1
  117. package/packages/indexer_client/src/models/transaction-heartbeat.js.map +1 -1
  118. package/packages/indexer_client/src/models/transaction-heartbeat.mjs +2 -1
  119. package/packages/indexer_client/src/models/transaction-heartbeat.mjs.map +1 -1
  120. package/packages/indexer_client/src/models/transaction-keyreg.js +4 -4
  121. package/packages/indexer_client/src/models/transaction-keyreg.js.map +1 -1
  122. package/packages/indexer_client/src/models/transaction-keyreg.mjs +4 -4
  123. package/packages/indexer_client/src/models/transaction-keyreg.mjs.map +1 -1
  124. package/packages/indexer_client/src/models/transaction-signature-logicsig.js +2 -1
  125. package/packages/indexer_client/src/models/transaction-signature-logicsig.js.map +1 -1
  126. package/packages/indexer_client/src/models/transaction-signature-logicsig.mjs +2 -1
  127. package/packages/indexer_client/src/models/transaction-signature-logicsig.mjs.map +1 -1
  128. package/packages/indexer_client/src/models/transaction-signature-multisig-subsignature.js +3 -3
  129. package/packages/indexer_client/src/models/transaction-signature-multisig-subsignature.js.map +1 -1
  130. package/packages/indexer_client/src/models/transaction-signature-multisig-subsignature.mjs +3 -3
  131. package/packages/indexer_client/src/models/transaction-signature-multisig-subsignature.mjs.map +1 -1
  132. package/packages/indexer_client/src/models/transaction.js +4 -3
  133. package/packages/indexer_client/src/models/transaction.js.map +1 -1
  134. package/packages/indexer_client/src/models/transaction.mjs +4 -3
  135. package/packages/indexer_client/src/models/transaction.mjs.map +1 -1
  136. package/packages/kmd_client/src/apis/api-service.d.ts +5 -5
  137. package/packages/kmd_client/src/apis/api-service.js +32 -32
  138. package/packages/kmd_client/src/apis/api-service.js.map +1 -1
  139. package/packages/kmd_client/src/apis/api-service.mjs +32 -32
  140. package/packages/kmd_client/src/apis/api-service.mjs.map +1 -1
  141. package/packages/kmd_client/src/core/api-error.js +3 -1
  142. package/packages/kmd_client/src/core/api-error.js.map +1 -1
  143. package/packages/kmd_client/src/core/api-error.mjs +3 -1
  144. package/packages/kmd_client/src/core/api-error.mjs.map +1 -1
  145. package/packages/kmd_client/src/core/model-runtime.js +4 -4
  146. package/packages/kmd_client/src/core/model-runtime.js.map +1 -1
  147. package/packages/kmd_client/src/core/model-runtime.mjs +6 -6
  148. package/packages/kmd_client/src/core/model-runtime.mjs.map +1 -1
  149. package/packages/sdk/src/encoding/encoding.js +12 -2
  150. package/packages/sdk/src/encoding/encoding.js.map +1 -1
  151. package/packages/sdk/src/encoding/encoding.mjs +12 -1
  152. package/packages/sdk/src/encoding/encoding.mjs.map +1 -1
  153. package/packages/sdk/src/encoding/schema/map.js +0 -2
  154. package/packages/sdk/src/encoding/schema/map.js.map +1 -1
  155. package/packages/transact/src/logicsig.js +3 -3
  156. package/packages/transact/src/logicsig.js.map +1 -1
  157. package/packages/transact/src/logicsig.mjs +3 -3
  158. package/packages/transact/src/logicsig.mjs.map +1 -1
  159. package/packages/transact/src/multisig.js +24 -24
  160. package/packages/transact/src/multisig.js.map +1 -1
  161. package/packages/transact/src/multisig.mjs +24 -24
  162. package/packages/transact/src/multisig.mjs.map +1 -1
  163. package/packages/transact/src/transactions/app-call.d.ts +2 -2
  164. package/packages/transact/src/transactions/app-call.js.map +1 -1
  165. package/packages/transact/src/transactions/app-call.mjs.map +1 -1
  166. package/packages/transact/src/transactions/signed-transaction-meta.js +2 -2
  167. package/packages/transact/src/transactions/signed-transaction-meta.js.map +1 -1
  168. package/packages/transact/src/transactions/signed-transaction-meta.mjs +2 -2
  169. package/packages/transact/src/transactions/signed-transaction-meta.mjs.map +1 -1
  170. package/packages/transact/src/transactions/signed-transaction.d.ts +2 -2
  171. package/packages/transact/src/transactions/signed-transaction.js.map +1 -1
  172. package/packages/transact/src/transactions/signed-transaction.mjs.map +1 -1
  173. package/packages/transact/src/transactions/transaction-meta.js +7 -1
  174. package/packages/transact/src/transactions/transaction-meta.js.map +1 -1
  175. package/packages/transact/src/transactions/transaction-meta.mjs +7 -1
  176. package/packages/transact/src/transactions/transaction-meta.mjs.map +1 -1
  177. package/packages/transact/src/transactions/transaction.js +1 -0
  178. package/packages/transact/src/transactions/transaction.js.map +1 -1
  179. package/packages/transact/src/transactions/transaction.mjs +1 -0
  180. package/packages/transact/src/transactions/transaction.mjs.map +1 -1
  181. package/sdk/index.js +1 -1
  182. package/testing/fixtures/algorand-fixture.d.ts +3 -3
  183. package/testing/fixtures/algorand-fixture.js.map +1 -1
  184. package/testing/fixtures/algorand-fixture.mjs.map +1 -1
  185. package/transaction/transaction.js +2 -2
  186. package/transaction/transaction.js.map +1 -1
  187. package/transaction/transaction.mjs +2 -2
  188. package/transaction/transaction.mjs.map +1 -1
  189. package/transactions/app-call.d.ts +2 -1
  190. package/transactions/app-call.js.map +1 -1
  191. package/transactions/app-call.mjs.map +1 -1
  192. package/types/account-manager.js +1 -1
  193. package/types/account-manager.js.map +1 -1
  194. package/types/account-manager.mjs +1 -1
  195. package/types/account-manager.mjs.map +1 -1
  196. package/types/algorand-client-transaction-creator.d.ts +36 -22
  197. package/types/algorand-client-transaction-creator.js +8 -0
  198. package/types/algorand-client-transaction-creator.js.map +1 -1
  199. package/types/algorand-client-transaction-creator.mjs +8 -0
  200. package/types/algorand-client-transaction-creator.mjs.map +1 -1
  201. package/types/algorand-client-transaction-sender.d.ts +36 -22
  202. package/types/algorand-client-transaction-sender.js +8 -1
  203. package/types/algorand-client-transaction-sender.js.map +1 -1
  204. package/types/algorand-client-transaction-sender.mjs +8 -0
  205. package/types/algorand-client-transaction-sender.mjs.map +1 -1
  206. package/types/app-client.d.ts +33 -28
  207. package/types/app-client.js +0 -1
  208. package/types/app-client.js.map +1 -1
  209. package/types/app-deployer.js +1 -1
  210. package/types/app-deployer.js.map +1 -1
  211. package/types/app-deployer.mjs +1 -1
  212. package/types/app-deployer.mjs.map +1 -1
  213. package/types/app-factory.d.ts +16 -13
  214. package/types/app-manager.d.ts +2 -0
  215. package/types/app-manager.js +5 -4
  216. package/types/app-manager.js.map +1 -1
  217. package/types/app-manager.mjs +5 -4
  218. package/types/app-manager.mjs.map +1 -1
  219. package/types/asset-manager.js +1 -1
  220. package/types/asset-manager.js.map +1 -1
  221. package/types/asset-manager.mjs +1 -1
  222. package/types/asset-manager.mjs.map +1 -1
  223. package/types/composer.d.ts +8 -0
  224. package/types/composer.js +11 -4
  225. package/types/composer.js.map +1 -1
  226. package/types/composer.mjs +11 -3
  227. package/types/composer.mjs.map +1 -1
  228. package/types/kmd-account-manager.d.ts +1 -0
  229. package/types/kmd-account-manager.js +21 -10
  230. package/types/kmd-account-manager.js.map +1 -1
  231. package/types/kmd-account-manager.mjs +22 -11
  232. package/types/kmd-account-manager.mjs.map +1 -1
  233. package/types/testing.d.ts +2 -2
@@ -3,9 +3,19 @@ const require_intDecoding = require('../types/intDecoding.js');
3
3
  const require_utils = require('../utils/utils.js');
4
4
  const require_binarydata = require('./binarydata.js');
5
5
  let algorand_msgpack = require("algorand-msgpack");
6
- algorand_msgpack = require_rolldown_runtime.__toESM(algorand_msgpack);
7
6
 
8
7
  //#region packages/sdk/src/encoding/encoding.ts
8
+ /**
9
+ * This file is a wrapper of msgpack.js.
10
+ * The wrapper was written in order to ensure correct encoding of Algorand Transaction and other formats.
11
+ * In particular, it matches go-algorand blockchain client, written in go (https://www.github.com/algorand/go-algorand.
12
+ * Algorand's msgpack encoding follows to following rules -
13
+ * 1. Every integer must be encoded to the smallest type possible (0-255-\>8bit, 256-65535-\>16bit, etx)
14
+ * 2. All fields names must be sorted
15
+ * 3. All empty and 0 fields should be omitted
16
+ * 4. Every positive number must be encoded as uint
17
+ * 5. Binary blob should be used for binary data and string for strings
18
+ * */
9
19
  const ERROR_CONTAINS_EMPTY_STRING = "The object contains empty or 0 values. First empty or 0 value encountered during encoding: ";
10
20
  /**
11
21
  * containsEmpty returns true if any of the object's values are empty, false otherwise.
@@ -280,7 +290,7 @@ function decodeJSON(encoded, c) {
280
290
  * @returns A JSON string encoding of the object
281
291
  */
282
292
  function encodeJSON(e, options) {
283
- const { space,...prepareJSONOptions } = options ?? {};
293
+ const { space, ...prepareJSONOptions } = options ?? {};
284
294
  return require_utils.stringifyJSON(e.getEncodingSchema().prepareJSON(e.toEncodingData(), prepareJSONOptions), void 0, space);
285
295
  }
286
296
 
@@ -1 +1 @@
1
- {"version":3,"file":"encoding.js","names":["IntDecoding","IntMode","bytesToBase64","obj: { [key: string]: JSONEncodingData }","RawBinaryString","parentResolved: MsgpackEncodingData","coerceToBytes","potentialKeyBytes: Uint8Array | undefined","arrayEqual","decoded: JSONEncodingData","parseJSON","stringifyJSON"],"sources":["../../../../../packages/sdk/src/encoding/encoding.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n/**\n * This file is a wrapper of msgpack.js.\n * The wrapper was written in order to ensure correct encoding of Algorand Transaction and other formats.\n * In particular, it matches go-algorand blockchain client, written in go (https://www.github.com/algorand/go-algorand.\n * Algorand's msgpack encoding follows to following rules -\n * 1. Every integer must be encoded to the smallest type possible (0-255-\\>8bit, 256-65535-\\>16bit, etx)\n * 2. All fields names must be sorted\n * 3. All empty and 0 fields should be omitted\n * 4. Every positive number must be encoded as uint\n * 5. Binary blob should be used for binary data and string for strings\n * */\n\nimport {\n DecoderOptions,\n EncoderOptions,\n IntMode,\n RawBinaryString,\n decode as msgpackDecode,\n encode as msgpackEncode,\n} from 'algorand-msgpack'\nimport IntDecoding from '../types/intDecoding.js'\nimport { arrayEqual, parseJSON, stringifyJSON } from '../utils/utils.js'\nimport { bytesToBase64, coerceToBytes } from './binarydata.js'\n\n// Errors\nexport const ERROR_CONTAINS_EMPTY_STRING = 'The object contains empty or 0 values. First empty or 0 value encountered during encoding: '\n\n/**\n * containsEmpty returns true if any of the object's values are empty, false otherwise.\n * Empty arrays considered empty\n * @param obj - The object to check\n * @returns \\{true, empty key\\} if contains empty, \\{false, undefined\\} otherwise\n */\nfunction containsEmpty(obj: Record<string | number | symbol, any>) {\n for (const key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n if (!obj[key] || obj[key].length === 0) {\n return { containsEmpty: true, firstEmptyKey: key }\n }\n }\n }\n return { containsEmpty: false, firstEmptyKey: undefined }\n}\n\n/**\n * msgpackRawEncode encodes objects using msgpack, regardless of whether there are\n * empty or 0 value fields.\n * @param obj - a dictionary to be encoded. May or may not contain empty or 0 values.\n * @returns msgpack representation of the object\n */\nexport function msgpackRawEncode(obj: unknown) {\n // enable the canonical option\n const options: EncoderOptions = { sortKeys: true }\n return msgpackEncode(obj, options)\n}\n\n/**\n * encodeObj takes a javascript object and returns its msgpack encoding\n * Note that the encoding sorts the fields alphabetically\n * @param o - js object to be encoded. Must not contain empty or 0 values.\n * @returns Uint8Array binary representation\n * @throws Error containing ERROR_CONTAINS_EMPTY_STRING if the object contains empty or zero values\n *\n * @deprecated Use {@link msgpackRawEncode} instead. Note that function does not\n * check for empty values like this one does.\n */\nexport function encodeObj(obj: Record<string | number | symbol, any>) {\n // Check for empty values\n const emptyCheck = containsEmpty(obj)\n if (emptyCheck.containsEmpty) {\n throw new Error(ERROR_CONTAINS_EMPTY_STRING + emptyCheck.firstEmptyKey)\n }\n return msgpackRawEncode(obj)\n}\n\nfunction intDecodingToIntMode(intDecoding: IntDecoding): IntMode {\n switch (intDecoding) {\n case IntDecoding.UNSAFE:\n return IntMode.UNSAFE_NUMBER\n case IntDecoding.SAFE:\n return IntMode.SAFE_NUMBER\n case IntDecoding.MIXED:\n return IntMode.MIXED\n case IntDecoding.BIGINT:\n return IntMode.BIGINT\n default:\n throw new Error(`Invalid intDecoding: ${intDecoding}`)\n }\n}\n\n/**\n * Decodes msgpack bytes into a plain JavaScript object.\n * @param buffer - The msgpack bytes to decode\n * @param options - Options for decoding, including int decoding mode. See {@link IntDecoding} for more information.\n * @returns The decoded object\n */\nexport function msgpackRawDecode(buffer: ArrayLike<number>, options?: { intDecoding: IntDecoding }) {\n const decoderOptions: DecoderOptions = {\n intMode: options?.intDecoding ? intDecodingToIntMode(options?.intDecoding) : IntMode.BIGINT,\n }\n return msgpackDecode(buffer, decoderOptions)\n}\n\n/**\n * decodeObj takes a Uint8Array and returns its javascript obj\n * @param o - Uint8Array to decode\n * @returns object\n *\n * @deprecated Use {@link msgpackRawDecode} instead. Note that this function uses `IntDecoding.MIXED`\n * while `msgpackRawDecode` defaults to `IntDecoding.BIGINT` for int decoding, though it is\n * configurable.\n */\nexport function decodeObj(o: ArrayLike<number>) {\n return msgpackRawDecode(o, { intDecoding: IntDecoding.MIXED })\n}\n\n/**\n * Decodes msgpack bytes into a Map object. This supports decoding non-string map keys.\n * @param encoded - The msgpack bytes to decode\n * @param options - Options for decoding, including int decoding mode. See {@link IntDecoding} for more information.\n * @returns The decoded Map object\n */\nexport function msgpackRawDecodeAsMap(encoded: ArrayLike<number>, options?: { intDecoding: IntDecoding }) {\n const decoderOptions: DecoderOptions = {\n intMode: options?.intDecoding ? intDecodingToIntMode(options?.intDecoding) : IntMode.BIGINT,\n useMap: true,\n }\n return msgpackDecode(encoded, decoderOptions)\n}\n\nfunction msgpackRawDecodeAsMapWithRawStrings(encoded: ArrayLike<number>, options?: { intDecoding: IntDecoding }) {\n const decoderOptions: DecoderOptions = {\n intMode: options?.intDecoding ? intDecodingToIntMode(options?.intDecoding) : IntMode.BIGINT,\n useMap: true,\n rawBinaryStringKeys: true,\n rawBinaryStringValues: true,\n useRawBinaryStringClass: true,\n }\n return msgpackDecode(encoded, decoderOptions)\n}\n\nexport type MsgpackEncodingData =\n | null\n | undefined\n | string\n | number\n | bigint\n | boolean\n | Uint8Array\n | MsgpackEncodingData[]\n | Map<string | number | bigint | Uint8Array, MsgpackEncodingData>\n\nexport type JSONEncodingData =\n | null\n | undefined\n | string\n | number\n | bigint\n | boolean\n | JSONEncodingData[]\n | { [key: string]: JSONEncodingData }\n\nexport function msgpackEncodingDataToJSONEncodingData(e: MsgpackEncodingData): JSONEncodingData {\n if (e === null || e === undefined) {\n return e as JSONEncodingData\n }\n if (e instanceof Uint8Array) {\n return bytesToBase64(e)\n }\n if (Array.isArray(e)) {\n return e.map(msgpackEncodingDataToJSONEncodingData)\n }\n if (e instanceof Map) {\n const obj: { [key: string]: JSONEncodingData } = {}\n for (const [k, v] of e) {\n if (typeof k !== 'string') {\n throw new Error(`JSON map key must be a string: ${k}`)\n }\n obj[k] = msgpackEncodingDataToJSONEncodingData(v)\n }\n return obj\n }\n return e\n}\n\nexport function jsonEncodingDataToMsgpackEncodingData(e: JSONEncodingData): MsgpackEncodingData {\n if (e === null || e === undefined) {\n return e as MsgpackEncodingData\n }\n if (\n typeof e === 'string' || // Note, this will not convert base64 to Uint8Array\n typeof e === 'number' ||\n typeof e === 'bigint' ||\n typeof e === 'boolean'\n ) {\n return e\n }\n if (Array.isArray(e)) {\n return e.map(jsonEncodingDataToMsgpackEncodingData)\n }\n if (typeof e === 'object') {\n const obj = new Map<string, MsgpackEncodingData>()\n for (const [key, value] of Object.entries(e)) {\n obj.set(key, jsonEncodingDataToMsgpackEncodingData(value))\n }\n return obj\n }\n throw new Error(`Invalid JSON encoding data: ${e}`)\n}\n\nenum MsgpackObjectPathSegmentKind {\n MAP_VALUE,\n ARRAY_ELEMENT,\n}\n\ninterface MsgpackObjectPathSegment {\n kind: MsgpackObjectPathSegmentKind\n key: string | number | bigint | Uint8Array | RawBinaryString\n}\n\n/**\n * This class is used to index into an encoded msgpack object and extract raw strings.\n */\nexport class MsgpackRawStringProvider {\n private readonly parent?: MsgpackRawStringProvider\n\n private readonly baseObjectBytes?: ArrayLike<number>\n\n private readonly segment?: MsgpackObjectPathSegment\n\n private resolvedCache: MsgpackEncodingData = null\n private resolvedCachePresent = false\n\n public constructor({\n parent,\n segment,\n baseObjectBytes,\n }:\n | {\n parent: MsgpackRawStringProvider\n segment: MsgpackObjectPathSegment\n baseObjectBytes?: undefined\n }\n | {\n parent?: undefined\n segment?: undefined\n baseObjectBytes: ArrayLike<number>\n }) {\n this.parent = parent\n this.segment = segment\n this.baseObjectBytes = baseObjectBytes\n }\n\n /**\n * Create a new provider that resolves to the current provider's map value at the given key.\n */\n public withMapValue(key: string | number | bigint | Uint8Array | RawBinaryString): MsgpackRawStringProvider {\n return new MsgpackRawStringProvider({\n parent: this,\n segment: {\n kind: MsgpackObjectPathSegmentKind.MAP_VALUE,\n key,\n },\n })\n }\n\n /**\n * Create a new provider that resolves to the current provider's array element at the given index.\n */\n public withArrayElement(index: number): MsgpackRawStringProvider {\n return new MsgpackRawStringProvider({\n parent: this,\n segment: {\n kind: MsgpackObjectPathSegmentKind.ARRAY_ELEMENT,\n key: index,\n },\n })\n }\n\n /**\n * Get the raw string at the current location. If the current location is not a raw string, an error is thrown.\n */\n public getRawStringAtCurrentLocation(): Uint8Array {\n const resolved = this.resolve()\n if (resolved instanceof RawBinaryString) {\n // Decoded rawBinaryValue will always be a Uint8Array\n return resolved.rawBinaryValue as Uint8Array\n }\n throw new Error(`Invalid type. Expected RawBinaryString, got ${resolved} (${typeof resolved})`)\n }\n\n /**\n * Get the raw string map keys and values at the current location. If the current location is not a map, an error is thrown.\n */\n public getRawStringKeysAndValuesAtCurrentLocation(): Map<Uint8Array, MsgpackEncodingData> {\n const resolved = this.resolve()\n if (!(resolved instanceof Map)) {\n throw new Error(`Invalid type. Expected Map, got ${resolved} (${typeof resolved})`)\n }\n const keysAndValues = new Map<Uint8Array, MsgpackEncodingData>()\n for (const [key, value] of resolved) {\n if (key instanceof RawBinaryString) {\n // Decoded rawBinaryValue will always be a Uint8Array\n keysAndValues.set(key.rawBinaryValue as Uint8Array, value)\n } else {\n throw new Error(`Invalid type for map key. Expected RawBinaryString, got ${key} (${typeof key})`)\n }\n }\n return keysAndValues\n }\n\n /**\n * Resolve the provider by extracting the value it indicates from the base msgpack object.\n */\n private resolve(): MsgpackEncodingData {\n if (this.resolvedCachePresent) {\n return this.resolvedCache\n }\n let parentResolved: MsgpackEncodingData\n if (this.parent) {\n parentResolved = this.parent.resolve()\n } else {\n // Need to parse baseObjectBytes\n parentResolved = msgpackRawDecodeAsMapWithRawStrings(this.baseObjectBytes!) as MsgpackEncodingData\n }\n if (!this.segment) {\n this.resolvedCache = parentResolved\n this.resolvedCachePresent = true\n return parentResolved\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.MAP_VALUE) {\n if (!(parentResolved instanceof Map)) {\n throw new Error(`Invalid type. Expected Map, got ${parentResolved} (${typeof parentResolved})`)\n }\n // All decoded map keys will be raw strings, and Map objects compare complex values by reference,\n // so we must check all the values for value-equality.\n if (typeof this.segment.key === 'string' || this.segment.key instanceof Uint8Array || this.segment.key instanceof RawBinaryString) {\n const targetBytes =\n this.segment.key instanceof RawBinaryString\n ? // Decoded rawBinaryValue will always be a Uint8Array\n (this.segment.key.rawBinaryValue as Uint8Array)\n : coerceToBytes(this.segment.key)\n const targetIsRawString = typeof this.segment.key === 'string' || this.segment.key instanceof RawBinaryString\n for (const [key, value] of parentResolved) {\n let potentialKeyBytes: Uint8Array | undefined\n if (targetIsRawString) {\n if (key instanceof RawBinaryString) {\n // Decoded rawBinaryValue will always be a Uint8Array\n potentialKeyBytes = key.rawBinaryValue as Uint8Array\n }\n } else if (key instanceof Uint8Array) {\n potentialKeyBytes = key\n }\n if (potentialKeyBytes && arrayEqual(targetBytes, potentialKeyBytes)) {\n this.resolvedCache = value\n break\n }\n }\n } else {\n this.resolvedCache = parentResolved.get(this.segment.key)\n }\n this.resolvedCachePresent = true\n return this.resolvedCache\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.ARRAY_ELEMENT) {\n if (!Array.isArray(parentResolved)) {\n throw new Error(`Invalid type. Expected Array, got ${parentResolved} (${typeof parentResolved})`)\n }\n this.resolvedCache = parentResolved[this.segment.key as number]\n this.resolvedCachePresent = true\n return this.resolvedCache\n }\n throw new Error(`Invalid segment kind: ${this.segment.kind}`)\n }\n\n /**\n * Get the path string of the current location indicated by the provider. Useful for debugging.\n */\n public getPathString(): string {\n const parentPathString = this.parent ? this.parent.getPathString() : 'root'\n if (!this.segment) {\n return parentPathString\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.MAP_VALUE) {\n return `${parentPathString} -> map key \"${this.segment.key}\" (${typeof this.segment.key})`\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.ARRAY_ELEMENT) {\n return `${parentPathString} -> array index ${this.segment.key} (${typeof this.segment.key})`\n }\n return `${parentPathString} -> unknown segment kind ${this.segment.kind}`\n }\n}\n\n/**\n * Options for {@link Schema.prepareJSON}\n */\nexport interface PrepareJSONOptions {\n /**\n * If true, allows invalid UTF-8 binary strings to be converted to JSON strings.\n *\n * Otherwise, an error will be thrown if encoding a binary string to a JSON cannot be done losslessly.\n */\n lossyBinaryStringConversion?: boolean\n}\n\n/**\n * A Schema is used to prepare objects for encoding and decoding from msgpack and JSON.\n *\n * Schemas represent a specific type.\n */\nexport abstract class Schema {\n /**\n * Get the default value for this type.\n */\n public abstract defaultValue(): unknown\n\n /**\n * Checks if the value is the default value for this type.\n * @param data - The value to check\n * @returns True if the value is the default value, false otherwise\n */\n public abstract isDefaultValue(data: unknown): boolean\n\n /**\n * Prepares the encoding data for encoding to msgpack.\n * @param data - Encoding data to be prepared.\n * @returns A value ready to be msgpack encoded.\n */\n public abstract prepareMsgpack(data: unknown): MsgpackEncodingData\n\n /**\n * Restores the encoding data from a msgpack encoding object.\n * @param encoded - The msgpack encoding object to restore.\n * @param rawStringProvider - A provider for raw strings.\n * @returns The original encoding data.\n */\n public abstract fromPreparedMsgpack(encoded: MsgpackEncodingData, rawStringProvider: MsgpackRawStringProvider): unknown\n\n /**\n * Prepares the encoding data for encoding to JSON.\n * @param data - The JSON encoding data to be prepared.\n * @returns A value ready to be JSON encoded.\n */\n public abstract prepareJSON(data: unknown, options: PrepareJSONOptions): JSONEncodingData\n\n /**\n * Restores the encoding data from a JSON encoding object.\n * @param encoded - The JSON encoding object to restore.\n * @returns The original encoding data.\n */\n public abstract fromPreparedJSON(encoded: JSONEncodingData): unknown\n}\n\n/**\n * An interface for objects that can be encoded and decoded to/from msgpack and JSON.\n */\nexport interface Encodable {\n /**\n * Extract the encoding data for this object. This data, after being prepared by the encoding\n * Schema, can be encoded to msgpack or JSON.\n */\n toEncodingData(): unknown\n /**\n * Get the encoding Schema for this object, used to prepare the encoding data for msgpack and JSON.\n */\n getEncodingSchema(): Schema\n}\n\n/**\n * A type that represents the class of an Encodable object.\n */\nexport interface EncodableClass<T extends Encodable> {\n /**\n * Create a new instance of this class from the given encoding data.\n * @param data - The encoding data to create the object from\n */\n fromEncodingData(data: unknown): T\n /**\n * The encoding Schema for this class, used to prepare encoding data from msgpack and JSON.\n */\n readonly encodingSchema: Schema\n}\n\n/**\n * Decode a msgpack byte array to an Encodable object.\n * @param encoded - The msgpack bytes to decode\n * @param c - The class of the object to decode. This class must match the object that was encoded.\n * @returns An instance of the class with the decoded data\n */\nexport function decodeMsgpack<T extends Encodable>(encoded: ArrayLike<number>, c: EncodableClass<T>): T {\n const decoded = msgpackRawDecodeAsMap(encoded) as MsgpackEncodingData\n const rawStringProvider = new MsgpackRawStringProvider({\n baseObjectBytes: encoded,\n })\n return c.fromEncodingData(c.encodingSchema.fromPreparedMsgpack(decoded, rawStringProvider))\n}\n\n/**\n * Encode an Encodable object to a msgpack byte array.\n * @param e - The object to encode\n * @returns A msgpack byte array encoding of the object\n */\nexport function encodeMsgpack(e: Encodable): Uint8Array {\n return msgpackRawEncode(e.getEncodingSchema().prepareMsgpack(e.toEncodingData()))\n}\n\n/**\n * Decode a JSON string to an Encodable object.\n * @param encoded - The JSON string to decode\n * @param c - The class of the object to decode. This class must match the object that was encoded.\n * @returns An instance of the class with the decoded data\n */\nexport function decodeJSON<T extends Encodable>(encoded: string, c: EncodableClass<T>): T {\n const decoded: JSONEncodingData = parseJSON(encoded, {\n intDecoding: IntDecoding.BIGINT,\n })\n return c.fromEncodingData(c.encodingSchema.fromPreparedJSON(decoded) as JSONEncodingData)\n}\n\nexport interface EncodeJSONOptions {\n /**\n * Adds indentation, white space, and line break characters to the return-value JSON text to make\n * it easier to read.\n */\n space?: string | number\n\n /**\n * If true, allows invalid UTF-8 binary strings to be converted to JSON strings.\n *\n * Otherwise, an error will be thrown if encoding a binary string to a JSON cannot be done losslessly.\n */\n lossyBinaryStringConversion?: boolean\n}\n\n/**\n * Encode an Encodable object to a JSON string.\n * @param e - The object to encode\n * @param options - Optional encoding options. See {@link EncodeJSONOptions} for more information.\n * @returns A JSON string encoding of the object\n */\nexport function encodeJSON(e: Encodable, options?: EncodeJSONOptions): string {\n const { space, ...prepareJSONOptions } = options ?? {}\n const prepared = e.getEncodingSchema().prepareJSON(e.toEncodingData(), prepareJSONOptions)\n return stringifyJSON(prepared, undefined, space)\n}\n"],"mappings":";;;;;;;;AA0BA,MAAa,8BAA8B;;;;;;;AAQ3C,SAAS,cAAc,KAA4C;AACjE,MAAK,MAAM,OAAO,IAChB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,oBAAoB,IAAI,yBAAyB,EACrD,iBAAiB,SAClB,CAAC;AACF,QAAO,EAAE,iBAAiB,EAAE,eAAe,oBAAoB,SAAS,kBAAkB,CAAC;;;;;;;AAQ7F,SAAgB,cAAc,GAA0B;AACtD,QAAO,iBAAiB,EAAE,mBAAmB,CAAC,eAAe,EAAE,gBAAgB,CAAC,CAAC;;;;;;;;AASnF,SAAgB,WAAgC,SAAiB,GAAyB;CACxF,MAAMC,UAA4BC,wBAAU,SAAS,EACnD,aAAaV,4BAAY,QAC1B,CAAC;AACF,QAAO,EAAE,iBAAiB,EAAE,eAAe,iBAAiB,QAAQ,CAAqB;;;;;;;;AAwB3F,SAAgB,WAAW,GAAc,SAAqC;CAC5E,MAAM,EAAE,MAAO,GAAG,uBAAuB,WAAW,EAAE;AAEtD,QAAOW,4BADU,EAAE,mBAAmB,CAAC,YAAY,EAAE,gBAAgB,EAAE,mBAAmB,EAC3D,QAAW,MAAM"}
1
+ {"version":3,"file":"encoding.js","names":["IntDecoding","IntMode","bytesToBase64","obj: { [key: string]: JSONEncodingData }","RawBinaryString","parentResolved: MsgpackEncodingData","coerceToBytes","potentialKeyBytes: Uint8Array | undefined","arrayEqual","decoded: JSONEncodingData","parseJSON","stringifyJSON"],"sources":["../../../../../packages/sdk/src/encoding/encoding.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n/**\n * This file is a wrapper of msgpack.js.\n * The wrapper was written in order to ensure correct encoding of Algorand Transaction and other formats.\n * In particular, it matches go-algorand blockchain client, written in go (https://www.github.com/algorand/go-algorand.\n * Algorand's msgpack encoding follows to following rules -\n * 1. Every integer must be encoded to the smallest type possible (0-255-\\>8bit, 256-65535-\\>16bit, etx)\n * 2. All fields names must be sorted\n * 3. All empty and 0 fields should be omitted\n * 4. Every positive number must be encoded as uint\n * 5. Binary blob should be used for binary data and string for strings\n * */\n\nimport {\n DecoderOptions,\n EncoderOptions,\n IntMode,\n RawBinaryString,\n decode as msgpackDecode,\n encode as msgpackEncode,\n} from 'algorand-msgpack'\nimport IntDecoding from '../types/intDecoding.js'\nimport { arrayEqual, parseJSON, stringifyJSON } from '../utils/utils.js'\nimport { bytesToBase64, coerceToBytes } from './binarydata.js'\n\n// Errors\nexport const ERROR_CONTAINS_EMPTY_STRING = 'The object contains empty or 0 values. First empty or 0 value encountered during encoding: '\n\n/**\n * containsEmpty returns true if any of the object's values are empty, false otherwise.\n * Empty arrays considered empty\n * @param obj - The object to check\n * @returns \\{true, empty key\\} if contains empty, \\{false, undefined\\} otherwise\n */\nfunction containsEmpty(obj: Record<string | number | symbol, any>) {\n for (const key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n if (!obj[key] || obj[key].length === 0) {\n return { containsEmpty: true, firstEmptyKey: key }\n }\n }\n }\n return { containsEmpty: false, firstEmptyKey: undefined }\n}\n\n/**\n * msgpackRawEncode encodes objects using msgpack, regardless of whether there are\n * empty or 0 value fields.\n * @param obj - a dictionary to be encoded. May or may not contain empty or 0 values.\n * @returns msgpack representation of the object\n */\nexport function msgpackRawEncode(obj: unknown) {\n // enable the canonical option\n const options: EncoderOptions = { sortKeys: true }\n return msgpackEncode(obj, options)\n}\n\n/**\n * encodeObj takes a javascript object and returns its msgpack encoding\n * Note that the encoding sorts the fields alphabetically\n * @param o - js object to be encoded. Must not contain empty or 0 values.\n * @returns Uint8Array binary representation\n * @throws Error containing ERROR_CONTAINS_EMPTY_STRING if the object contains empty or zero values\n *\n * @deprecated Use {@link msgpackRawEncode} instead. Note that function does not\n * check for empty values like this one does.\n */\nexport function encodeObj(obj: Record<string | number | symbol, any>) {\n // Check for empty values\n const emptyCheck = containsEmpty(obj)\n if (emptyCheck.containsEmpty) {\n throw new Error(ERROR_CONTAINS_EMPTY_STRING + emptyCheck.firstEmptyKey)\n }\n return msgpackRawEncode(obj)\n}\n\nfunction intDecodingToIntMode(intDecoding: IntDecoding): IntMode {\n switch (intDecoding) {\n case IntDecoding.UNSAFE:\n return IntMode.UNSAFE_NUMBER\n case IntDecoding.SAFE:\n return IntMode.SAFE_NUMBER\n case IntDecoding.MIXED:\n return IntMode.MIXED\n case IntDecoding.BIGINT:\n return IntMode.BIGINT\n default:\n throw new Error(`Invalid intDecoding: ${intDecoding}`)\n }\n}\n\n/**\n * Decodes msgpack bytes into a plain JavaScript object.\n * @param buffer - The msgpack bytes to decode\n * @param options - Options for decoding, including int decoding mode. See {@link IntDecoding} for more information.\n * @returns The decoded object\n */\nexport function msgpackRawDecode(buffer: ArrayLike<number>, options?: { intDecoding: IntDecoding }) {\n const decoderOptions: DecoderOptions = {\n intMode: options?.intDecoding ? intDecodingToIntMode(options?.intDecoding) : IntMode.BIGINT,\n }\n return msgpackDecode(buffer, decoderOptions)\n}\n\n/**\n * decodeObj takes a Uint8Array and returns its javascript obj\n * @param o - Uint8Array to decode\n * @returns object\n *\n * @deprecated Use {@link msgpackRawDecode} instead. Note that this function uses `IntDecoding.MIXED`\n * while `msgpackRawDecode` defaults to `IntDecoding.BIGINT` for int decoding, though it is\n * configurable.\n */\nexport function decodeObj(o: ArrayLike<number>) {\n return msgpackRawDecode(o, { intDecoding: IntDecoding.MIXED })\n}\n\n/**\n * Decodes msgpack bytes into a Map object. This supports decoding non-string map keys.\n * @param encoded - The msgpack bytes to decode\n * @param options - Options for decoding, including int decoding mode. See {@link IntDecoding} for more information.\n * @returns The decoded Map object\n */\nexport function msgpackRawDecodeAsMap(encoded: ArrayLike<number>, options?: { intDecoding: IntDecoding }) {\n const decoderOptions: DecoderOptions = {\n intMode: options?.intDecoding ? intDecodingToIntMode(options?.intDecoding) : IntMode.BIGINT,\n useMap: true,\n }\n return msgpackDecode(encoded, decoderOptions)\n}\n\nfunction msgpackRawDecodeAsMapWithRawStrings(encoded: ArrayLike<number>, options?: { intDecoding: IntDecoding }) {\n const decoderOptions: DecoderOptions = {\n intMode: options?.intDecoding ? intDecodingToIntMode(options?.intDecoding) : IntMode.BIGINT,\n useMap: true,\n rawBinaryStringKeys: true,\n rawBinaryStringValues: true,\n useRawBinaryStringClass: true,\n }\n return msgpackDecode(encoded, decoderOptions)\n}\n\nexport type MsgpackEncodingData =\n | null\n | undefined\n | string\n | number\n | bigint\n | boolean\n | Uint8Array\n | MsgpackEncodingData[]\n | Map<string | number | bigint | Uint8Array, MsgpackEncodingData>\n\nexport type JSONEncodingData =\n | null\n | undefined\n | string\n | number\n | bigint\n | boolean\n | JSONEncodingData[]\n | { [key: string]: JSONEncodingData }\n\nexport function msgpackEncodingDataToJSONEncodingData(e: MsgpackEncodingData): JSONEncodingData {\n if (e === null || e === undefined) {\n return e as JSONEncodingData\n }\n if (e instanceof Uint8Array) {\n return bytesToBase64(e)\n }\n if (Array.isArray(e)) {\n return e.map(msgpackEncodingDataToJSONEncodingData)\n }\n if (e instanceof Map) {\n const obj: { [key: string]: JSONEncodingData } = {}\n for (const [k, v] of e) {\n if (typeof k !== 'string') {\n throw new Error(`JSON map key must be a string: ${k}`)\n }\n obj[k] = msgpackEncodingDataToJSONEncodingData(v)\n }\n return obj\n }\n return e\n}\n\nexport function jsonEncodingDataToMsgpackEncodingData(e: JSONEncodingData): MsgpackEncodingData {\n if (e === null || e === undefined) {\n return e as MsgpackEncodingData\n }\n if (\n typeof e === 'string' || // Note, this will not convert base64 to Uint8Array\n typeof e === 'number' ||\n typeof e === 'bigint' ||\n typeof e === 'boolean'\n ) {\n return e\n }\n if (Array.isArray(e)) {\n return e.map(jsonEncodingDataToMsgpackEncodingData)\n }\n if (typeof e === 'object') {\n const obj = new Map<string, MsgpackEncodingData>()\n for (const [key, value] of Object.entries(e)) {\n obj.set(key, jsonEncodingDataToMsgpackEncodingData(value))\n }\n return obj\n }\n throw new Error(`Invalid JSON encoding data: ${e}`)\n}\n\nenum MsgpackObjectPathSegmentKind {\n MAP_VALUE,\n ARRAY_ELEMENT,\n}\n\ninterface MsgpackObjectPathSegment {\n kind: MsgpackObjectPathSegmentKind\n key: string | number | bigint | Uint8Array | RawBinaryString\n}\n\n/**\n * This class is used to index into an encoded msgpack object and extract raw strings.\n */\nexport class MsgpackRawStringProvider {\n private readonly parent?: MsgpackRawStringProvider\n\n private readonly baseObjectBytes?: ArrayLike<number>\n\n private readonly segment?: MsgpackObjectPathSegment\n\n private resolvedCache: MsgpackEncodingData = null\n private resolvedCachePresent = false\n\n public constructor({\n parent,\n segment,\n baseObjectBytes,\n }:\n | {\n parent: MsgpackRawStringProvider\n segment: MsgpackObjectPathSegment\n baseObjectBytes?: undefined\n }\n | {\n parent?: undefined\n segment?: undefined\n baseObjectBytes: ArrayLike<number>\n }) {\n this.parent = parent\n this.segment = segment\n this.baseObjectBytes = baseObjectBytes\n }\n\n /**\n * Create a new provider that resolves to the current provider's map value at the given key.\n */\n public withMapValue(key: string | number | bigint | Uint8Array | RawBinaryString): MsgpackRawStringProvider {\n return new MsgpackRawStringProvider({\n parent: this,\n segment: {\n kind: MsgpackObjectPathSegmentKind.MAP_VALUE,\n key,\n },\n })\n }\n\n /**\n * Create a new provider that resolves to the current provider's array element at the given index.\n */\n public withArrayElement(index: number): MsgpackRawStringProvider {\n return new MsgpackRawStringProvider({\n parent: this,\n segment: {\n kind: MsgpackObjectPathSegmentKind.ARRAY_ELEMENT,\n key: index,\n },\n })\n }\n\n /**\n * Get the raw string at the current location. If the current location is not a raw string, an error is thrown.\n */\n public getRawStringAtCurrentLocation(): Uint8Array {\n const resolved = this.resolve()\n if (resolved instanceof RawBinaryString) {\n // Decoded rawBinaryValue will always be a Uint8Array\n return resolved.rawBinaryValue as Uint8Array\n }\n throw new Error(`Invalid type. Expected RawBinaryString, got ${resolved} (${typeof resolved})`)\n }\n\n /**\n * Get the raw string map keys and values at the current location. If the current location is not a map, an error is thrown.\n */\n public getRawStringKeysAndValuesAtCurrentLocation(): Map<Uint8Array, MsgpackEncodingData> {\n const resolved = this.resolve()\n if (!(resolved instanceof Map)) {\n throw new Error(`Invalid type. Expected Map, got ${resolved} (${typeof resolved})`)\n }\n const keysAndValues = new Map<Uint8Array, MsgpackEncodingData>()\n for (const [key, value] of resolved) {\n if (key instanceof RawBinaryString) {\n // Decoded rawBinaryValue will always be a Uint8Array\n keysAndValues.set(key.rawBinaryValue as Uint8Array, value)\n } else {\n throw new Error(`Invalid type for map key. Expected RawBinaryString, got ${key} (${typeof key})`)\n }\n }\n return keysAndValues\n }\n\n /**\n * Resolve the provider by extracting the value it indicates from the base msgpack object.\n */\n private resolve(): MsgpackEncodingData {\n if (this.resolvedCachePresent) {\n return this.resolvedCache\n }\n let parentResolved: MsgpackEncodingData\n if (this.parent) {\n parentResolved = this.parent.resolve()\n } else {\n // Need to parse baseObjectBytes\n parentResolved = msgpackRawDecodeAsMapWithRawStrings(this.baseObjectBytes!) as MsgpackEncodingData\n }\n if (!this.segment) {\n this.resolvedCache = parentResolved\n this.resolvedCachePresent = true\n return parentResolved\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.MAP_VALUE) {\n if (!(parentResolved instanceof Map)) {\n throw new Error(`Invalid type. Expected Map, got ${parentResolved} (${typeof parentResolved})`)\n }\n // All decoded map keys will be raw strings, and Map objects compare complex values by reference,\n // so we must check all the values for value-equality.\n if (typeof this.segment.key === 'string' || this.segment.key instanceof Uint8Array || this.segment.key instanceof RawBinaryString) {\n const targetBytes =\n this.segment.key instanceof RawBinaryString\n ? // Decoded rawBinaryValue will always be a Uint8Array\n (this.segment.key.rawBinaryValue as Uint8Array)\n : coerceToBytes(this.segment.key)\n const targetIsRawString = typeof this.segment.key === 'string' || this.segment.key instanceof RawBinaryString\n for (const [key, value] of parentResolved) {\n let potentialKeyBytes: Uint8Array | undefined\n if (targetIsRawString) {\n if (key instanceof RawBinaryString) {\n // Decoded rawBinaryValue will always be a Uint8Array\n potentialKeyBytes = key.rawBinaryValue as Uint8Array\n }\n } else if (key instanceof Uint8Array) {\n potentialKeyBytes = key\n }\n if (potentialKeyBytes && arrayEqual(targetBytes, potentialKeyBytes)) {\n this.resolvedCache = value\n break\n }\n }\n } else {\n this.resolvedCache = parentResolved.get(this.segment.key)\n }\n this.resolvedCachePresent = true\n return this.resolvedCache\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.ARRAY_ELEMENT) {\n if (!Array.isArray(parentResolved)) {\n throw new Error(`Invalid type. Expected Array, got ${parentResolved} (${typeof parentResolved})`)\n }\n this.resolvedCache = parentResolved[this.segment.key as number]\n this.resolvedCachePresent = true\n return this.resolvedCache\n }\n throw new Error(`Invalid segment kind: ${this.segment.kind}`)\n }\n\n /**\n * Get the path string of the current location indicated by the provider. Useful for debugging.\n */\n public getPathString(): string {\n const parentPathString = this.parent ? this.parent.getPathString() : 'root'\n if (!this.segment) {\n return parentPathString\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.MAP_VALUE) {\n return `${parentPathString} -> map key \"${this.segment.key}\" (${typeof this.segment.key})`\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.ARRAY_ELEMENT) {\n return `${parentPathString} -> array index ${this.segment.key} (${typeof this.segment.key})`\n }\n return `${parentPathString} -> unknown segment kind ${this.segment.kind}`\n }\n}\n\n/**\n * Options for {@link Schema.prepareJSON}\n */\nexport interface PrepareJSONOptions {\n /**\n * If true, allows invalid UTF-8 binary strings to be converted to JSON strings.\n *\n * Otherwise, an error will be thrown if encoding a binary string to a JSON cannot be done losslessly.\n */\n lossyBinaryStringConversion?: boolean\n}\n\n/**\n * A Schema is used to prepare objects for encoding and decoding from msgpack and JSON.\n *\n * Schemas represent a specific type.\n */\nexport abstract class Schema {\n /**\n * Get the default value for this type.\n */\n public abstract defaultValue(): unknown\n\n /**\n * Checks if the value is the default value for this type.\n * @param data - The value to check\n * @returns True if the value is the default value, false otherwise\n */\n public abstract isDefaultValue(data: unknown): boolean\n\n /**\n * Prepares the encoding data for encoding to msgpack.\n * @param data - Encoding data to be prepared.\n * @returns A value ready to be msgpack encoded.\n */\n public abstract prepareMsgpack(data: unknown): MsgpackEncodingData\n\n /**\n * Restores the encoding data from a msgpack encoding object.\n * @param encoded - The msgpack encoding object to restore.\n * @param rawStringProvider - A provider for raw strings.\n * @returns The original encoding data.\n */\n public abstract fromPreparedMsgpack(encoded: MsgpackEncodingData, rawStringProvider: MsgpackRawStringProvider): unknown\n\n /**\n * Prepares the encoding data for encoding to JSON.\n * @param data - The JSON encoding data to be prepared.\n * @returns A value ready to be JSON encoded.\n */\n public abstract prepareJSON(data: unknown, options: PrepareJSONOptions): JSONEncodingData\n\n /**\n * Restores the encoding data from a JSON encoding object.\n * @param encoded - The JSON encoding object to restore.\n * @returns The original encoding data.\n */\n public abstract fromPreparedJSON(encoded: JSONEncodingData): unknown\n}\n\n/**\n * An interface for objects that can be encoded and decoded to/from msgpack and JSON.\n */\nexport interface Encodable {\n /**\n * Extract the encoding data for this object. This data, after being prepared by the encoding\n * Schema, can be encoded to msgpack or JSON.\n */\n toEncodingData(): unknown\n /**\n * Get the encoding Schema for this object, used to prepare the encoding data for msgpack and JSON.\n */\n getEncodingSchema(): Schema\n}\n\n/**\n * A type that represents the class of an Encodable object.\n */\nexport interface EncodableClass<T extends Encodable> {\n /**\n * Create a new instance of this class from the given encoding data.\n * @param data - The encoding data to create the object from\n */\n fromEncodingData(data: unknown): T\n /**\n * The encoding Schema for this class, used to prepare encoding data from msgpack and JSON.\n */\n readonly encodingSchema: Schema\n}\n\n/**\n * Decode a msgpack byte array to an Encodable object.\n * @param encoded - The msgpack bytes to decode\n * @param c - The class of the object to decode. This class must match the object that was encoded.\n * @returns An instance of the class with the decoded data\n */\nexport function decodeMsgpack<T extends Encodable>(encoded: ArrayLike<number>, c: EncodableClass<T>): T {\n const decoded = msgpackRawDecodeAsMap(encoded) as MsgpackEncodingData\n const rawStringProvider = new MsgpackRawStringProvider({\n baseObjectBytes: encoded,\n })\n return c.fromEncodingData(c.encodingSchema.fromPreparedMsgpack(decoded, rawStringProvider))\n}\n\n/**\n * Encode an Encodable object to a msgpack byte array.\n * @param e - The object to encode\n * @returns A msgpack byte array encoding of the object\n */\nexport function encodeMsgpack(e: Encodable): Uint8Array {\n return msgpackRawEncode(e.getEncodingSchema().prepareMsgpack(e.toEncodingData()))\n}\n\n/**\n * Decode a JSON string to an Encodable object.\n * @param encoded - The JSON string to decode\n * @param c - The class of the object to decode. This class must match the object that was encoded.\n * @returns An instance of the class with the decoded data\n */\nexport function decodeJSON<T extends Encodable>(encoded: string, c: EncodableClass<T>): T {\n const decoded: JSONEncodingData = parseJSON(encoded, {\n intDecoding: IntDecoding.BIGINT,\n })\n return c.fromEncodingData(c.encodingSchema.fromPreparedJSON(decoded) as JSONEncodingData)\n}\n\nexport interface EncodeJSONOptions {\n /**\n * Adds indentation, white space, and line break characters to the return-value JSON text to make\n * it easier to read.\n */\n space?: string | number\n\n /**\n * If true, allows invalid UTF-8 binary strings to be converted to JSON strings.\n *\n * Otherwise, an error will be thrown if encoding a binary string to a JSON cannot be done losslessly.\n */\n lossyBinaryStringConversion?: boolean\n}\n\n/**\n * Encode an Encodable object to a JSON string.\n * @param e - The object to encode\n * @param options - Optional encoding options. See {@link EncodeJSONOptions} for more information.\n * @returns A JSON string encoding of the object\n */\nexport function encodeJSON(e: Encodable, options?: EncodeJSONOptions): string {\n const { space, ...prepareJSONOptions } = options ?? {}\n const prepared = e.getEncodingSchema().prepareJSON(e.toEncodingData(), prepareJSONOptions)\n return stringifyJSON(prepared, undefined, space)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA0BA,MAAa,8BAA8B;;;;;;;AAQ3C,SAAS,cAAc,KAA4C;AACjE,MAAK,MAAM,OAAO,IAChB,KAAI,OAAO,UAAU,eAAe,KAAK,KAAK,IAAI,EAChD;MAAI,CAAC,IAAI,QAAQ,IAAI,KAAK,WAAW,EACnC,QAAO;GAAE,eAAe;GAAM,eAAe;GAAK;;AAIxD,QAAO;EAAE,eAAe;EAAO,eAAe;EAAW;;;;;;;;AAS3D,SAAgB,iBAAiB,KAAc;AAG7C,qCAAqB,KADW,EAAE,UAAU,MAAM,CAChB;;;;;;;;;;;;AAapC,SAAgB,UAAU,KAA4C;CAEpE,MAAM,a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V,4BAAY,QAC1B,CAAC;AACF,QAAO,EAAE,iBAAiB,EAAE,eAAe,iBAAiB,QAAQ,CAAqB;;;;;;;;AAwB3F,SAAgB,WAAW,GAAc,SAAqC;CAC5E,MAAM,EAAE,OAAO,GAAG,uBAAuB,WAAW,EAAE;AAEtD,QAAOW,4BADU,EAAE,mBAAmB,CAAC,YAAY,EAAE,gBAAgB,EAAE,mBAAmB,EAC3D,QAAW,MAAM"}
@@ -4,6 +4,17 @@ import { bytesToBase64, coerceToBytes } from "./binarydata.mjs";
4
4
  import { IntMode, RawBinaryString, decode, encode } from "algorand-msgpack";
5
5
 
6
6
  //#region packages/sdk/src/encoding/encoding.ts
7
+ /**
8
+ * This file is a wrapper of msgpack.js.
9
+ * The wrapper was written in order to ensure correct encoding of Algorand Transaction and other formats.
10
+ * In particular, it matches go-algorand blockchain client, written in go (https://www.github.com/algorand/go-algorand.
11
+ * Algorand's msgpack encoding follows to following rules -
12
+ * 1. Every integer must be encoded to the smallest type possible (0-255-\>8bit, 256-65535-\>16bit, etx)
13
+ * 2. All fields names must be sorted
14
+ * 3. All empty and 0 fields should be omitted
15
+ * 4. Every positive number must be encoded as uint
16
+ * 5. Binary blob should be used for binary data and string for strings
17
+ * */
7
18
  const ERROR_CONTAINS_EMPTY_STRING = "The object contains empty or 0 values. First empty or 0 value encountered during encoding: ";
8
19
  /**
9
20
  * containsEmpty returns true if any of the object's values are empty, false otherwise.
@@ -278,7 +289,7 @@ function decodeJSON(encoded, c) {
278
289
  * @returns A JSON string encoding of the object
279
290
  */
280
291
  function encodeJSON(e, options) {
281
- const { space,...prepareJSONOptions } = options ?? {};
292
+ const { space, ...prepareJSONOptions } = options ?? {};
282
293
  return stringifyJSON(e.getEncodingSchema().prepareJSON(e.toEncodingData(), prepareJSONOptions), void 0, space);
283
294
  }
284
295
 
@@ -1 +1 @@
1
- {"version":3,"file":"encoding.mjs","names":["msgpackEncode","IntDecoding","msgpackDecode","obj: { [key: string]: JSONEncodingData }","parentResolved: MsgpackEncodingData","potentialKeyBytes: Uint8Array | undefined","decoded: JSONEncodingData"],"sources":["../../../../../packages/sdk/src/encoding/encoding.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n/**\n * This file is a wrapper of msgpack.js.\n * The wrapper was written in order to ensure correct encoding of Algorand Transaction and other formats.\n * In particular, it matches go-algorand blockchain client, written in go (https://www.github.com/algorand/go-algorand.\n * Algorand's msgpack encoding follows to following rules -\n * 1. Every integer must be encoded to the smallest type possible (0-255-\\>8bit, 256-65535-\\>16bit, etx)\n * 2. All fields names must be sorted\n * 3. All empty and 0 fields should be omitted\n * 4. Every positive number must be encoded as uint\n * 5. Binary blob should be used for binary data and string for strings\n * */\n\nimport {\n DecoderOptions,\n EncoderOptions,\n IntMode,\n RawBinaryString,\n decode as msgpackDecode,\n encode as msgpackEncode,\n} from 'algorand-msgpack'\nimport IntDecoding from '../types/intDecoding.js'\nimport { arrayEqual, parseJSON, stringifyJSON } from '../utils/utils.js'\nimport { bytesToBase64, coerceToBytes } from './binarydata.js'\n\n// Errors\nexport const ERROR_CONTAINS_EMPTY_STRING = 'The object contains empty or 0 values. First empty or 0 value encountered during encoding: '\n\n/**\n * containsEmpty returns true if any of the object's values are empty, false otherwise.\n * Empty arrays considered empty\n * @param obj - The object to check\n * @returns \\{true, empty key\\} if contains empty, \\{false, undefined\\} otherwise\n */\nfunction containsEmpty(obj: Record<string | number | symbol, any>) {\n for (const key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n if (!obj[key] || obj[key].length === 0) {\n return { containsEmpty: true, firstEmptyKey: key }\n }\n }\n }\n return { containsEmpty: false, firstEmptyKey: undefined }\n}\n\n/**\n * msgpackRawEncode encodes objects using msgpack, regardless of whether there are\n * empty or 0 value fields.\n * @param obj - a dictionary to be encoded. May or may not contain empty or 0 values.\n * @returns msgpack representation of the object\n */\nexport function msgpackRawEncode(obj: unknown) {\n // enable the canonical option\n const options: EncoderOptions = { sortKeys: true }\n return msgpackEncode(obj, options)\n}\n\n/**\n * encodeObj takes a javascript object and returns its msgpack encoding\n * Note that the encoding sorts the fields alphabetically\n * @param o - js object to be encoded. Must not contain empty or 0 values.\n * @returns Uint8Array binary representation\n * @throws Error containing ERROR_CONTAINS_EMPTY_STRING if the object contains empty or zero values\n *\n * @deprecated Use {@link msgpackRawEncode} instead. Note that function does not\n * check for empty values like this one does.\n */\nexport function encodeObj(obj: Record<string | number | symbol, any>) {\n // Check for empty values\n const emptyCheck = containsEmpty(obj)\n if (emptyCheck.containsEmpty) {\n throw new Error(ERROR_CONTAINS_EMPTY_STRING + emptyCheck.firstEmptyKey)\n }\n return msgpackRawEncode(obj)\n}\n\nfunction intDecodingToIntMode(intDecoding: IntDecoding): IntMode {\n switch (intDecoding) {\n case IntDecoding.UNSAFE:\n return IntMode.UNSAFE_NUMBER\n case IntDecoding.SAFE:\n return IntMode.SAFE_NUMBER\n case IntDecoding.MIXED:\n return IntMode.MIXED\n case IntDecoding.BIGINT:\n return IntMode.BIGINT\n default:\n throw new Error(`Invalid intDecoding: ${intDecoding}`)\n }\n}\n\n/**\n * Decodes msgpack bytes into a plain JavaScript object.\n * @param buffer - The msgpack bytes to decode\n * @param options - Options for decoding, including int decoding mode. See {@link IntDecoding} for more information.\n * @returns The decoded object\n */\nexport function msgpackRawDecode(buffer: ArrayLike<number>, options?: { intDecoding: IntDecoding }) {\n const decoderOptions: DecoderOptions = {\n intMode: options?.intDecoding ? intDecodingToIntMode(options?.intDecoding) : IntMode.BIGINT,\n }\n return msgpackDecode(buffer, decoderOptions)\n}\n\n/**\n * decodeObj takes a Uint8Array and returns its javascript obj\n * @param o - Uint8Array to decode\n * @returns object\n *\n * @deprecated Use {@link msgpackRawDecode} instead. Note that this function uses `IntDecoding.MIXED`\n * while `msgpackRawDecode` defaults to `IntDecoding.BIGINT` for int decoding, though it is\n * configurable.\n */\nexport function decodeObj(o: ArrayLike<number>) {\n return msgpackRawDecode(o, { intDecoding: IntDecoding.MIXED })\n}\n\n/**\n * Decodes msgpack bytes into a Map object. This supports decoding non-string map keys.\n * @param encoded - The msgpack bytes to decode\n * @param options - Options for decoding, including int decoding mode. See {@link IntDecoding} for more information.\n * @returns The decoded Map object\n */\nexport function msgpackRawDecodeAsMap(encoded: ArrayLike<number>, options?: { intDecoding: IntDecoding }) {\n const decoderOptions: DecoderOptions = {\n intMode: options?.intDecoding ? intDecodingToIntMode(options?.intDecoding) : IntMode.BIGINT,\n useMap: true,\n }\n return msgpackDecode(encoded, decoderOptions)\n}\n\nfunction msgpackRawDecodeAsMapWithRawStrings(encoded: ArrayLike<number>, options?: { intDecoding: IntDecoding }) {\n const decoderOptions: DecoderOptions = {\n intMode: options?.intDecoding ? intDecodingToIntMode(options?.intDecoding) : IntMode.BIGINT,\n useMap: true,\n rawBinaryStringKeys: true,\n rawBinaryStringValues: true,\n useRawBinaryStringClass: true,\n }\n return msgpackDecode(encoded, decoderOptions)\n}\n\nexport type MsgpackEncodingData =\n | null\n | undefined\n | string\n | number\n | bigint\n | boolean\n | Uint8Array\n | MsgpackEncodingData[]\n | Map<string | number | bigint | Uint8Array, MsgpackEncodingData>\n\nexport type JSONEncodingData =\n | null\n | undefined\n | string\n | number\n | bigint\n | boolean\n | JSONEncodingData[]\n | { [key: string]: JSONEncodingData }\n\nexport function msgpackEncodingDataToJSONEncodingData(e: MsgpackEncodingData): JSONEncodingData {\n if (e === null || e === undefined) {\n return e as JSONEncodingData\n }\n if (e instanceof Uint8Array) {\n return bytesToBase64(e)\n }\n if (Array.isArray(e)) {\n return e.map(msgpackEncodingDataToJSONEncodingData)\n }\n if (e instanceof Map) {\n const obj: { [key: string]: JSONEncodingData } = {}\n for (const [k, v] of e) {\n if (typeof k !== 'string') {\n throw new Error(`JSON map key must be a string: ${k}`)\n }\n obj[k] = msgpackEncodingDataToJSONEncodingData(v)\n }\n return obj\n }\n return e\n}\n\nexport function jsonEncodingDataToMsgpackEncodingData(e: JSONEncodingData): MsgpackEncodingData {\n if (e === null || e === undefined) {\n return e as MsgpackEncodingData\n }\n if (\n typeof e === 'string' || // Note, this will not convert base64 to Uint8Array\n typeof e === 'number' ||\n typeof e === 'bigint' ||\n typeof e === 'boolean'\n ) {\n return e\n }\n if (Array.isArray(e)) {\n return e.map(jsonEncodingDataToMsgpackEncodingData)\n }\n if (typeof e === 'object') {\n const obj = new Map<string, MsgpackEncodingData>()\n for (const [key, value] of Object.entries(e)) {\n obj.set(key, jsonEncodingDataToMsgpackEncodingData(value))\n }\n return obj\n }\n throw new Error(`Invalid JSON encoding data: ${e}`)\n}\n\nenum MsgpackObjectPathSegmentKind {\n MAP_VALUE,\n ARRAY_ELEMENT,\n}\n\ninterface MsgpackObjectPathSegment {\n kind: MsgpackObjectPathSegmentKind\n key: string | number | bigint | Uint8Array | RawBinaryString\n}\n\n/**\n * This class is used to index into an encoded msgpack object and extract raw strings.\n */\nexport class MsgpackRawStringProvider {\n private readonly parent?: MsgpackRawStringProvider\n\n private readonly baseObjectBytes?: ArrayLike<number>\n\n private readonly segment?: MsgpackObjectPathSegment\n\n private resolvedCache: MsgpackEncodingData = null\n private resolvedCachePresent = false\n\n public constructor({\n parent,\n segment,\n baseObjectBytes,\n }:\n | {\n parent: MsgpackRawStringProvider\n segment: MsgpackObjectPathSegment\n baseObjectBytes?: undefined\n }\n | {\n parent?: undefined\n segment?: undefined\n baseObjectBytes: ArrayLike<number>\n }) {\n this.parent = parent\n this.segment = segment\n this.baseObjectBytes = baseObjectBytes\n }\n\n /**\n * Create a new provider that resolves to the current provider's map value at the given key.\n */\n public withMapValue(key: string | number | bigint | Uint8Array | RawBinaryString): MsgpackRawStringProvider {\n return new MsgpackRawStringProvider({\n parent: this,\n segment: {\n kind: MsgpackObjectPathSegmentKind.MAP_VALUE,\n key,\n },\n })\n }\n\n /**\n * Create a new provider that resolves to the current provider's array element at the given index.\n */\n public withArrayElement(index: number): MsgpackRawStringProvider {\n return new MsgpackRawStringProvider({\n parent: this,\n segment: {\n kind: MsgpackObjectPathSegmentKind.ARRAY_ELEMENT,\n key: index,\n },\n })\n }\n\n /**\n * Get the raw string at the current location. If the current location is not a raw string, an error is thrown.\n */\n public getRawStringAtCurrentLocation(): Uint8Array {\n const resolved = this.resolve()\n if (resolved instanceof RawBinaryString) {\n // Decoded rawBinaryValue will always be a Uint8Array\n return resolved.rawBinaryValue as Uint8Array\n }\n throw new Error(`Invalid type. Expected RawBinaryString, got ${resolved} (${typeof resolved})`)\n }\n\n /**\n * Get the raw string map keys and values at the current location. If the current location is not a map, an error is thrown.\n */\n public getRawStringKeysAndValuesAtCurrentLocation(): Map<Uint8Array, MsgpackEncodingData> {\n const resolved = this.resolve()\n if (!(resolved instanceof Map)) {\n throw new Error(`Invalid type. Expected Map, got ${resolved} (${typeof resolved})`)\n }\n const keysAndValues = new Map<Uint8Array, MsgpackEncodingData>()\n for (const [key, value] of resolved) {\n if (key instanceof RawBinaryString) {\n // Decoded rawBinaryValue will always be a Uint8Array\n keysAndValues.set(key.rawBinaryValue as Uint8Array, value)\n } else {\n throw new Error(`Invalid type for map key. Expected RawBinaryString, got ${key} (${typeof key})`)\n }\n }\n return keysAndValues\n }\n\n /**\n * Resolve the provider by extracting the value it indicates from the base msgpack object.\n */\n private resolve(): MsgpackEncodingData {\n if (this.resolvedCachePresent) {\n return this.resolvedCache\n }\n let parentResolved: MsgpackEncodingData\n if (this.parent) {\n parentResolved = this.parent.resolve()\n } else {\n // Need to parse baseObjectBytes\n parentResolved = msgpackRawDecodeAsMapWithRawStrings(this.baseObjectBytes!) as MsgpackEncodingData\n }\n if (!this.segment) {\n this.resolvedCache = parentResolved\n this.resolvedCachePresent = true\n return parentResolved\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.MAP_VALUE) {\n if (!(parentResolved instanceof Map)) {\n throw new Error(`Invalid type. Expected Map, got ${parentResolved} (${typeof parentResolved})`)\n }\n // All decoded map keys will be raw strings, and Map objects compare complex values by reference,\n // so we must check all the values for value-equality.\n if (typeof this.segment.key === 'string' || this.segment.key instanceof Uint8Array || this.segment.key instanceof RawBinaryString) {\n const targetBytes =\n this.segment.key instanceof RawBinaryString\n ? // Decoded rawBinaryValue will always be a Uint8Array\n (this.segment.key.rawBinaryValue as Uint8Array)\n : coerceToBytes(this.segment.key)\n const targetIsRawString = typeof this.segment.key === 'string' || this.segment.key instanceof RawBinaryString\n for (const [key, value] of parentResolved) {\n let potentialKeyBytes: Uint8Array | undefined\n if (targetIsRawString) {\n if (key instanceof RawBinaryString) {\n // Decoded rawBinaryValue will always be a Uint8Array\n potentialKeyBytes = key.rawBinaryValue as Uint8Array\n }\n } else if (key instanceof Uint8Array) {\n potentialKeyBytes = key\n }\n if (potentialKeyBytes && arrayEqual(targetBytes, potentialKeyBytes)) {\n this.resolvedCache = value\n break\n }\n }\n } else {\n this.resolvedCache = parentResolved.get(this.segment.key)\n }\n this.resolvedCachePresent = true\n return this.resolvedCache\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.ARRAY_ELEMENT) {\n if (!Array.isArray(parentResolved)) {\n throw new Error(`Invalid type. Expected Array, got ${parentResolved} (${typeof parentResolved})`)\n }\n this.resolvedCache = parentResolved[this.segment.key as number]\n this.resolvedCachePresent = true\n return this.resolvedCache\n }\n throw new Error(`Invalid segment kind: ${this.segment.kind}`)\n }\n\n /**\n * Get the path string of the current location indicated by the provider. Useful for debugging.\n */\n public getPathString(): string {\n const parentPathString = this.parent ? this.parent.getPathString() : 'root'\n if (!this.segment) {\n return parentPathString\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.MAP_VALUE) {\n return `${parentPathString} -> map key \"${this.segment.key}\" (${typeof this.segment.key})`\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.ARRAY_ELEMENT) {\n return `${parentPathString} -> array index ${this.segment.key} (${typeof this.segment.key})`\n }\n return `${parentPathString} -> unknown segment kind ${this.segment.kind}`\n }\n}\n\n/**\n * Options for {@link Schema.prepareJSON}\n */\nexport interface PrepareJSONOptions {\n /**\n * If true, allows invalid UTF-8 binary strings to be converted to JSON strings.\n *\n * Otherwise, an error will be thrown if encoding a binary string to a JSON cannot be done losslessly.\n */\n lossyBinaryStringConversion?: boolean\n}\n\n/**\n * A Schema is used to prepare objects for encoding and decoding from msgpack and JSON.\n *\n * Schemas represent a specific type.\n */\nexport abstract class Schema {\n /**\n * Get the default value for this type.\n */\n public abstract defaultValue(): unknown\n\n /**\n * Checks if the value is the default value for this type.\n * @param data - The value to check\n * @returns True if the value is the default value, false otherwise\n */\n public abstract isDefaultValue(data: unknown): boolean\n\n /**\n * Prepares the encoding data for encoding to msgpack.\n * @param data - Encoding data to be prepared.\n * @returns A value ready to be msgpack encoded.\n */\n public abstract prepareMsgpack(data: unknown): MsgpackEncodingData\n\n /**\n * Restores the encoding data from a msgpack encoding object.\n * @param encoded - The msgpack encoding object to restore.\n * @param rawStringProvider - A provider for raw strings.\n * @returns The original encoding data.\n */\n public abstract fromPreparedMsgpack(encoded: MsgpackEncodingData, rawStringProvider: MsgpackRawStringProvider): unknown\n\n /**\n * Prepares the encoding data for encoding to JSON.\n * @param data - The JSON encoding data to be prepared.\n * @returns A value ready to be JSON encoded.\n */\n public abstract prepareJSON(data: unknown, options: PrepareJSONOptions): JSONEncodingData\n\n /**\n * Restores the encoding data from a JSON encoding object.\n * @param encoded - The JSON encoding object to restore.\n * @returns The original encoding data.\n */\n public abstract fromPreparedJSON(encoded: JSONEncodingData): unknown\n}\n\n/**\n * An interface for objects that can be encoded and decoded to/from msgpack and JSON.\n */\nexport interface Encodable {\n /**\n * Extract the encoding data for this object. This data, after being prepared by the encoding\n * Schema, can be encoded to msgpack or JSON.\n */\n toEncodingData(): unknown\n /**\n * Get the encoding Schema for this object, used to prepare the encoding data for msgpack and JSON.\n */\n getEncodingSchema(): Schema\n}\n\n/**\n * A type that represents the class of an Encodable object.\n */\nexport interface EncodableClass<T extends Encodable> {\n /**\n * Create a new instance of this class from the given encoding data.\n * @param data - The encoding data to create the object from\n */\n fromEncodingData(data: unknown): T\n /**\n * The encoding Schema for this class, used to prepare encoding data from msgpack and JSON.\n */\n readonly encodingSchema: Schema\n}\n\n/**\n * Decode a msgpack byte array to an Encodable object.\n * @param encoded - The msgpack bytes to decode\n * @param c - The class of the object to decode. This class must match the object that was encoded.\n * @returns An instance of the class with the decoded data\n */\nexport function decodeMsgpack<T extends Encodable>(encoded: ArrayLike<number>, c: EncodableClass<T>): T {\n const decoded = msgpackRawDecodeAsMap(encoded) as MsgpackEncodingData\n const rawStringProvider = new MsgpackRawStringProvider({\n baseObjectBytes: encoded,\n })\n return c.fromEncodingData(c.encodingSchema.fromPreparedMsgpack(decoded, rawStringProvider))\n}\n\n/**\n * Encode an Encodable object to a msgpack byte array.\n * @param e - The object to encode\n * @returns A msgpack byte array encoding of the object\n */\nexport function encodeMsgpack(e: Encodable): Uint8Array {\n return msgpackRawEncode(e.getEncodingSchema().prepareMsgpack(e.toEncodingData()))\n}\n\n/**\n * Decode a JSON string to an Encodable object.\n * @param encoded - The JSON string to decode\n * @param c - The class of the object to decode. This class must match the object that was encoded.\n * @returns An instance of the class with the decoded data\n */\nexport function decodeJSON<T extends Encodable>(encoded: string, c: EncodableClass<T>): T {\n const decoded: JSONEncodingData = parseJSON(encoded, {\n intDecoding: IntDecoding.BIGINT,\n })\n return c.fromEncodingData(c.encodingSchema.fromPreparedJSON(decoded) as JSONEncodingData)\n}\n\nexport interface EncodeJSONOptions {\n /**\n * Adds indentation, white space, and line break characters to the return-value JSON text to make\n * it easier to read.\n */\n space?: string | number\n\n /**\n * If true, allows invalid UTF-8 binary strings to be converted to JSON strings.\n *\n * Otherwise, an error will be thrown if encoding a binary string to a JSON cannot be done losslessly.\n */\n lossyBinaryStringConversion?: boolean\n}\n\n/**\n * Encode an Encodable object to a JSON string.\n * @param e - The object to encode\n * @param options - Optional encoding options. See {@link EncodeJSONOptions} for more information.\n * @returns A JSON string encoding of the object\n */\nexport function encodeJSON(e: Encodable, options?: EncodeJSONOptions): string {\n const { space, ...prepareJSONOptions } = options ?? {}\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F,SAAO,GAAG,iBAAiB,2BAA2B,KAAK,QAAQ;;;;;;;;AAqBvE,IAAsB,SAAtB,MAA6B;;;;;;;AA+E7B,SAAgB,cAAmC,SAA4B,GAAyB;CACtG,MAAM,UAAU,sBAAsB,QAAQ;CAC9C,MAAM,oBAAoB,IAAI,yBAAyB,EACrD,iBAAiB,SAClB,CAAC;AACF,QAAO,EAAE,iBAAiB,EAAE,eAAe,oBAAoB,SAAS,kBAAkB,CAAC;;;;;;;AAQ7F,SAAgB,cAAc,GAA0B;AACtD,QAAO,iBAAiB,EAAE,mBAAmB,CAAC,eAAe,EAAE,gBAAgB,CAAC,CAAC;;;;;;;;AASnF,SAAgB,WAAgC,SAAiB,GAAyB;CACxF,MAAMC,UAA4B,UAAU,SAAS,EACnD,aAAaL,oBAAY,QAC1B,CAAC;AACF,QAAO,EAAE,iBAAiB,EAAE,eAAe,iBAAiB,QAAQ,CAAqB;;;;;;;;AAwB3F,SAAgB,WAAW,GAAc,SAAqC;CAC5E,MAAM,EAAE,MAAO,GAAG,uBAAuB,WAAW,EAAE;AAEtD,QAAO,cADU,EAAE,mBAAmB,CAAC,YAAY,EAAE,gBAAgB,EAAE,mBAAmB,EAC3D,QAAW,MAAM"}
1
+ {"version":3,"file":"encoding.mjs","names":["msgpackEncode","IntDecoding","msgpackDecode","obj: { [key: string]: JSONEncodingData }","parentResolved: MsgpackEncodingData","potentialKeyBytes: Uint8Array | undefined","decoded: JSONEncodingData"],"sources":["../../../../../packages/sdk/src/encoding/encoding.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n/**\n * This file is a wrapper of msgpack.js.\n * The wrapper was written in order to ensure correct encoding of Algorand Transaction and other formats.\n * In particular, it matches go-algorand blockchain client, written in go (https://www.github.com/algorand/go-algorand.\n * Algorand's msgpack encoding follows to following rules -\n * 1. Every integer must be encoded to the smallest type possible (0-255-\\>8bit, 256-65535-\\>16bit, etx)\n * 2. All fields names must be sorted\n * 3. All empty and 0 fields should be omitted\n * 4. Every positive number must be encoded as uint\n * 5. Binary blob should be used for binary data and string for strings\n * */\n\nimport {\n DecoderOptions,\n EncoderOptions,\n IntMode,\n RawBinaryString,\n decode as msgpackDecode,\n encode as msgpackEncode,\n} from 'algorand-msgpack'\nimport IntDecoding from '../types/intDecoding.js'\nimport { arrayEqual, parseJSON, stringifyJSON } from '../utils/utils.js'\nimport { bytesToBase64, coerceToBytes } from './binarydata.js'\n\n// Errors\nexport const ERROR_CONTAINS_EMPTY_STRING = 'The object contains empty or 0 values. First empty or 0 value encountered during encoding: '\n\n/**\n * containsEmpty returns true if any of the object's values are empty, false otherwise.\n * Empty arrays considered empty\n * @param obj - The object to check\n * @returns \\{true, empty key\\} if contains empty, \\{false, undefined\\} otherwise\n */\nfunction containsEmpty(obj: Record<string | number | symbol, any>) {\n for (const key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n if (!obj[key] || obj[key].length === 0) {\n return { containsEmpty: true, firstEmptyKey: key }\n }\n }\n }\n return { containsEmpty: false, firstEmptyKey: undefined }\n}\n\n/**\n * msgpackRawEncode encodes objects using msgpack, regardless of whether there are\n * empty or 0 value fields.\n * @param obj - a dictionary to be encoded. May or may not contain empty or 0 values.\n * @returns msgpack representation of the object\n */\nexport function msgpackRawEncode(obj: unknown) {\n // enable the canonical option\n const options: EncoderOptions = { sortKeys: true }\n return msgpackEncode(obj, options)\n}\n\n/**\n * encodeObj takes a javascript object and returns its msgpack encoding\n * Note that the encoding sorts the fields alphabetically\n * @param o - js object to be encoded. Must not contain empty or 0 values.\n * @returns Uint8Array binary representation\n * @throws Error containing ERROR_CONTAINS_EMPTY_STRING if the object contains empty or zero values\n *\n * @deprecated Use {@link msgpackRawEncode} instead. Note that function does not\n * check for empty values like this one does.\n */\nexport function encodeObj(obj: Record<string | number | symbol, any>) {\n // Check for empty values\n const emptyCheck = containsEmpty(obj)\n if (emptyCheck.containsEmpty) {\n throw new Error(ERROR_CONTAINS_EMPTY_STRING + emptyCheck.firstEmptyKey)\n }\n return msgpackRawEncode(obj)\n}\n\nfunction intDecodingToIntMode(intDecoding: IntDecoding): IntMode {\n switch (intDecoding) {\n case IntDecoding.UNSAFE:\n return IntMode.UNSAFE_NUMBER\n case IntDecoding.SAFE:\n return IntMode.SAFE_NUMBER\n case IntDecoding.MIXED:\n return IntMode.MIXED\n case IntDecoding.BIGINT:\n return IntMode.BIGINT\n default:\n throw new Error(`Invalid intDecoding: ${intDecoding}`)\n }\n}\n\n/**\n * Decodes msgpack bytes into a plain JavaScript object.\n * @param buffer - The msgpack bytes to decode\n * @param options - Options for decoding, including int decoding mode. See {@link IntDecoding} for more information.\n * @returns The decoded object\n */\nexport function msgpackRawDecode(buffer: ArrayLike<number>, options?: { intDecoding: IntDecoding }) {\n const decoderOptions: DecoderOptions = {\n intMode: options?.intDecoding ? intDecodingToIntMode(options?.intDecoding) : IntMode.BIGINT,\n }\n return msgpackDecode(buffer, decoderOptions)\n}\n\n/**\n * decodeObj takes a Uint8Array and returns its javascript obj\n * @param o - Uint8Array to decode\n * @returns object\n *\n * @deprecated Use {@link msgpackRawDecode} instead. Note that this function uses `IntDecoding.MIXED`\n * while `msgpackRawDecode` defaults to `IntDecoding.BIGINT` for int decoding, though it is\n * configurable.\n */\nexport function decodeObj(o: ArrayLike<number>) {\n return msgpackRawDecode(o, { intDecoding: IntDecoding.MIXED })\n}\n\n/**\n * Decodes msgpack bytes into a Map object. This supports decoding non-string map keys.\n * @param encoded - The msgpack bytes to decode\n * @param options - Options for decoding, including int decoding mode. See {@link IntDecoding} for more information.\n * @returns The decoded Map object\n */\nexport function msgpackRawDecodeAsMap(encoded: ArrayLike<number>, options?: { intDecoding: IntDecoding }) {\n const decoderOptions: DecoderOptions = {\n intMode: options?.intDecoding ? intDecodingToIntMode(options?.intDecoding) : IntMode.BIGINT,\n useMap: true,\n }\n return msgpackDecode(encoded, decoderOptions)\n}\n\nfunction msgpackRawDecodeAsMapWithRawStrings(encoded: ArrayLike<number>, options?: { intDecoding: IntDecoding }) {\n const decoderOptions: DecoderOptions = {\n intMode: options?.intDecoding ? intDecodingToIntMode(options?.intDecoding) : IntMode.BIGINT,\n useMap: true,\n rawBinaryStringKeys: true,\n rawBinaryStringValues: true,\n useRawBinaryStringClass: true,\n }\n return msgpackDecode(encoded, decoderOptions)\n}\n\nexport type MsgpackEncodingData =\n | null\n | undefined\n | string\n | number\n | bigint\n | boolean\n | Uint8Array\n | MsgpackEncodingData[]\n | Map<string | number | bigint | Uint8Array, MsgpackEncodingData>\n\nexport type JSONEncodingData =\n | null\n | undefined\n | string\n | number\n | bigint\n | boolean\n | JSONEncodingData[]\n | { [key: string]: JSONEncodingData }\n\nexport function msgpackEncodingDataToJSONEncodingData(e: MsgpackEncodingData): JSONEncodingData {\n if (e === null || e === undefined) {\n return e as JSONEncodingData\n }\n if (e instanceof Uint8Array) {\n return bytesToBase64(e)\n }\n if (Array.isArray(e)) {\n return e.map(msgpackEncodingDataToJSONEncodingData)\n }\n if (e instanceof Map) {\n const obj: { [key: string]: JSONEncodingData } = {}\n for (const [k, v] of e) {\n if (typeof k !== 'string') {\n throw new Error(`JSON map key must be a string: ${k}`)\n }\n obj[k] = msgpackEncodingDataToJSONEncodingData(v)\n }\n return obj\n }\n return e\n}\n\nexport function jsonEncodingDataToMsgpackEncodingData(e: JSONEncodingData): MsgpackEncodingData {\n if (e === null || e === undefined) {\n return e as MsgpackEncodingData\n }\n if (\n typeof e === 'string' || // Note, this will not convert base64 to Uint8Array\n typeof e === 'number' ||\n typeof e === 'bigint' ||\n typeof e === 'boolean'\n ) {\n return e\n }\n if (Array.isArray(e)) {\n return e.map(jsonEncodingDataToMsgpackEncodingData)\n }\n if (typeof e === 'object') {\n const obj = new Map<string, MsgpackEncodingData>()\n for (const [key, value] of Object.entries(e)) {\n obj.set(key, jsonEncodingDataToMsgpackEncodingData(value))\n }\n return obj\n }\n throw new Error(`Invalid JSON encoding data: ${e}`)\n}\n\nenum MsgpackObjectPathSegmentKind {\n MAP_VALUE,\n ARRAY_ELEMENT,\n}\n\ninterface MsgpackObjectPathSegment {\n kind: MsgpackObjectPathSegmentKind\n key: string | number | bigint | Uint8Array | RawBinaryString\n}\n\n/**\n * This class is used to index into an encoded msgpack object and extract raw strings.\n */\nexport class MsgpackRawStringProvider {\n private readonly parent?: MsgpackRawStringProvider\n\n private readonly baseObjectBytes?: ArrayLike<number>\n\n private readonly segment?: MsgpackObjectPathSegment\n\n private resolvedCache: MsgpackEncodingData = null\n private resolvedCachePresent = false\n\n public constructor({\n parent,\n segment,\n baseObjectBytes,\n }:\n | {\n parent: MsgpackRawStringProvider\n segment: MsgpackObjectPathSegment\n baseObjectBytes?: undefined\n }\n | {\n parent?: undefined\n segment?: undefined\n baseObjectBytes: ArrayLike<number>\n }) {\n this.parent = parent\n this.segment = segment\n this.baseObjectBytes = baseObjectBytes\n }\n\n /**\n * Create a new provider that resolves to the current provider's map value at the given key.\n */\n public withMapValue(key: string | number | bigint | Uint8Array | RawBinaryString): MsgpackRawStringProvider {\n return new MsgpackRawStringProvider({\n parent: this,\n segment: {\n kind: MsgpackObjectPathSegmentKind.MAP_VALUE,\n key,\n },\n })\n }\n\n /**\n * Create a new provider that resolves to the current provider's array element at the given index.\n */\n public withArrayElement(index: number): MsgpackRawStringProvider {\n return new MsgpackRawStringProvider({\n parent: this,\n segment: {\n kind: MsgpackObjectPathSegmentKind.ARRAY_ELEMENT,\n key: index,\n },\n })\n }\n\n /**\n * Get the raw string at the current location. If the current location is not a raw string, an error is thrown.\n */\n public getRawStringAtCurrentLocation(): Uint8Array {\n const resolved = this.resolve()\n if (resolved instanceof RawBinaryString) {\n // Decoded rawBinaryValue will always be a Uint8Array\n return resolved.rawBinaryValue as Uint8Array\n }\n throw new Error(`Invalid type. Expected RawBinaryString, got ${resolved} (${typeof resolved})`)\n }\n\n /**\n * Get the raw string map keys and values at the current location. If the current location is not a map, an error is thrown.\n */\n public getRawStringKeysAndValuesAtCurrentLocation(): Map<Uint8Array, MsgpackEncodingData> {\n const resolved = this.resolve()\n if (!(resolved instanceof Map)) {\n throw new Error(`Invalid type. Expected Map, got ${resolved} (${typeof resolved})`)\n }\n const keysAndValues = new Map<Uint8Array, MsgpackEncodingData>()\n for (const [key, value] of resolved) {\n if (key instanceof RawBinaryString) {\n // Decoded rawBinaryValue will always be a Uint8Array\n keysAndValues.set(key.rawBinaryValue as Uint8Array, value)\n } else {\n throw new Error(`Invalid type for map key. Expected RawBinaryString, got ${key} (${typeof key})`)\n }\n }\n return keysAndValues\n }\n\n /**\n * Resolve the provider by extracting the value it indicates from the base msgpack object.\n */\n private resolve(): MsgpackEncodingData {\n if (this.resolvedCachePresent) {\n return this.resolvedCache\n }\n let parentResolved: MsgpackEncodingData\n if (this.parent) {\n parentResolved = this.parent.resolve()\n } else {\n // Need to parse baseObjectBytes\n parentResolved = msgpackRawDecodeAsMapWithRawStrings(this.baseObjectBytes!) as MsgpackEncodingData\n }\n if (!this.segment) {\n this.resolvedCache = parentResolved\n this.resolvedCachePresent = true\n return parentResolved\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.MAP_VALUE) {\n if (!(parentResolved instanceof Map)) {\n throw new Error(`Invalid type. Expected Map, got ${parentResolved} (${typeof parentResolved})`)\n }\n // All decoded map keys will be raw strings, and Map objects compare complex values by reference,\n // so we must check all the values for value-equality.\n if (typeof this.segment.key === 'string' || this.segment.key instanceof Uint8Array || this.segment.key instanceof RawBinaryString) {\n const targetBytes =\n this.segment.key instanceof RawBinaryString\n ? // Decoded rawBinaryValue will always be a Uint8Array\n (this.segment.key.rawBinaryValue as Uint8Array)\n : coerceToBytes(this.segment.key)\n const targetIsRawString = typeof this.segment.key === 'string' || this.segment.key instanceof RawBinaryString\n for (const [key, value] of parentResolved) {\n let potentialKeyBytes: Uint8Array | undefined\n if (targetIsRawString) {\n if (key instanceof RawBinaryString) {\n // Decoded rawBinaryValue will always be a Uint8Array\n potentialKeyBytes = key.rawBinaryValue as Uint8Array\n }\n } else if (key instanceof Uint8Array) {\n potentialKeyBytes = key\n }\n if (potentialKeyBytes && arrayEqual(targetBytes, potentialKeyBytes)) {\n this.resolvedCache = value\n break\n }\n }\n } else {\n this.resolvedCache = parentResolved.get(this.segment.key)\n }\n this.resolvedCachePresent = true\n return this.resolvedCache\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.ARRAY_ELEMENT) {\n if (!Array.isArray(parentResolved)) {\n throw new Error(`Invalid type. Expected Array, got ${parentResolved} (${typeof parentResolved})`)\n }\n this.resolvedCache = parentResolved[this.segment.key as number]\n this.resolvedCachePresent = true\n return this.resolvedCache\n }\n throw new Error(`Invalid segment kind: ${this.segment.kind}`)\n }\n\n /**\n * Get the path string of the current location indicated by the provider. Useful for debugging.\n */\n public getPathString(): string {\n const parentPathString = this.parent ? this.parent.getPathString() : 'root'\n if (!this.segment) {\n return parentPathString\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.MAP_VALUE) {\n return `${parentPathString} -> map key \"${this.segment.key}\" (${typeof this.segment.key})`\n }\n if (this.segment.kind === MsgpackObjectPathSegmentKind.ARRAY_ELEMENT) {\n return `${parentPathString} -> array index ${this.segment.key} (${typeof this.segment.key})`\n }\n return `${parentPathString} -> unknown segment kind ${this.segment.kind}`\n }\n}\n\n/**\n * Options for {@link Schema.prepareJSON}\n */\nexport interface PrepareJSONOptions {\n /**\n * If true, allows invalid UTF-8 binary strings to be converted to JSON strings.\n *\n * Otherwise, an error will be thrown if encoding a binary string to a JSON cannot be done losslessly.\n */\n lossyBinaryStringConversion?: boolean\n}\n\n/**\n * A Schema is used to prepare objects for encoding and decoding from msgpack and JSON.\n *\n * Schemas represent a specific type.\n */\nexport abstract class Schema {\n /**\n * Get the default value for this type.\n */\n public abstract defaultValue(): unknown\n\n /**\n * Checks if the value is the default value for this type.\n * @param data - The value to check\n * @returns True if the value is the default value, false otherwise\n */\n public abstract isDefaultValue(data: unknown): boolean\n\n /**\n * Prepares the encoding data for encoding to msgpack.\n * @param data - Encoding data to be prepared.\n * @returns A value ready to be msgpack encoded.\n */\n public abstract prepareMsgpack(data: unknown): MsgpackEncodingData\n\n /**\n * Restores the encoding data from a msgpack encoding object.\n * @param encoded - The msgpack encoding object to restore.\n * @param rawStringProvider - A provider for raw strings.\n * @returns The original encoding data.\n */\n public abstract fromPreparedMsgpack(encoded: MsgpackEncodingData, rawStringProvider: MsgpackRawStringProvider): unknown\n\n /**\n * Prepares the encoding data for encoding to JSON.\n * @param data - The JSON encoding data to be prepared.\n * @returns A value ready to be JSON encoded.\n */\n public abstract prepareJSON(data: unknown, options: PrepareJSONOptions): JSONEncodingData\n\n /**\n * Restores the encoding data from a JSON encoding object.\n * @param encoded - The JSON encoding object to restore.\n * @returns The original encoding data.\n */\n public abstract fromPreparedJSON(encoded: JSONEncodingData): unknown\n}\n\n/**\n * An interface for objects that can be encoded and decoded to/from msgpack and JSON.\n */\nexport interface Encodable {\n /**\n * Extract the encoding data for this object. This data, after being prepared by the encoding\n * Schema, can be encoded to msgpack or JSON.\n */\n toEncodingData(): unknown\n /**\n * Get the encoding Schema for this object, used to prepare the encoding data for msgpack and JSON.\n */\n getEncodingSchema(): Schema\n}\n\n/**\n * A type that represents the class of an Encodable object.\n */\nexport interface EncodableClass<T extends Encodable> {\n /**\n * Create a new instance of this class from the given encoding data.\n * @param data - The encoding data to create the object from\n */\n fromEncodingData(data: unknown): T\n /**\n * The encoding Schema for this class, used to prepare encoding data from msgpack and JSON.\n */\n readonly encodingSchema: Schema\n}\n\n/**\n * Decode a msgpack byte array to an Encodable object.\n * @param encoded - The msgpack bytes to decode\n * @param c - The class of the object to decode. This class must match the object that was encoded.\n * @returns An instance of the class with the decoded data\n */\nexport function decodeMsgpack<T extends Encodable>(encoded: ArrayLike<number>, c: EncodableClass<T>): T {\n const decoded = msgpackRawDecodeAsMap(encoded) as MsgpackEncodingData\n const rawStringProvider = new MsgpackRawStringProvider({\n baseObjectBytes: encoded,\n })\n return c.fromEncodingData(c.encodingSchema.fromPreparedMsgpack(decoded, rawStringProvider))\n}\n\n/**\n * Encode an Encodable object to a msgpack byte array.\n * @param e - The object to encode\n * @returns A msgpack byte array encoding of the object\n */\nexport function encodeMsgpack(e: Encodable): Uint8Array {\n return msgpackRawEncode(e.getEncodingSchema().prepareMsgpack(e.toEncodingData()))\n}\n\n/**\n * Decode a JSON string to an Encodable object.\n * @param encoded - The JSON string to decode\n * @param c - The class of the object to decode. This class must match the object that was encoded.\n * @returns An instance of the class with the decoded data\n */\nexport function decodeJSON<T extends Encodable>(encoded: string, c: EncodableClass<T>): T {\n const decoded: JSONEncodingData = parseJSON(encoded, {\n intDecoding: IntDecoding.BIGINT,\n })\n return c.fromEncodingData(c.encodingSchema.fromPreparedJSON(decoded) as JSONEncodingData)\n}\n\nexport interface EncodeJSONOptions {\n /**\n * Adds indentation, white space, and line break characters to the return-value JSON text to make\n * it easier to read.\n */\n space?: string | number\n\n /**\n * If true, allows invalid UTF-8 binary strings to be converted to JSON strings.\n *\n * Otherwise, an error will be thrown if encoding a binary string to a JSON cannot be done losslessly.\n */\n lossyBinaryStringConversion?: boolean\n}\n\n/**\n * Encode an Encodable object to a JSON string.\n * @param e - The object to encode\n * @param options - Optional encoding options. See {@link EncodeJSONOptions} for more information.\n * @returns A JSON string encoding of the object\n */\nexport function encodeJSON(e: Encodable, options?: EncodeJSONOptions): string {\n const { space, ...prepareJSONOptions } = options ?? {}\n const prepared = e.getEncodingSchema().prepareJSON(e.toEncodingData(), prepareJSONOptions)\n return stringifyJSON(prepared, undefined, space)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA0BA,MAAa,8BAA8B;;;;;;;AAQ3C,SAAS,cAAc,KAA4C;AACjE,MAAK,MAAM,OAAO,IAChB,KAAI,OAAO,UAAU,eAAe,KAAK,KAAK,IAAI,EAChD;MAAI,CAAC,IAAI,QAAQ,IAAI,KAAK,WAAW,EACnC,QAAO;GAAE,eAAe;GAAM,eAAe;GAAK;;AAIxD,QAAO;EAAE,eAAe;EAAO,eAAe;EAAW;;;;;;;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,EAAE,gBAAgB,CAAC,CAAC;;;;;;;;AASnF,SAAgB,WAAgC,SAAiB,GAAyB;CACxF,MAAMC,UAA4B,UAAU,SAAS,EACnD,aAAaL,oBAAY,QAC1B,CAAC;AACF,QAAO,EAAE,iBAAiB,EAAE,eAAe,iBAAiB,QAAQ,CAAqB;;;;;;;;AAwB3F,SAAgB,WAAW,GAAc,SAAqC;CAC5E,MAAM,EAAE,OAAO,GAAG,uBAAuB,WAAW,EAAE;AAEtD,QAAO,cADU,EAAE,mBAAmB,CAAC,YAAY,EAAE,gBAAgB,EAAE,mBAAmB,EAC3D,QAAW,MAAM"}
@@ -1,7 +1,5 @@
1
- const require_rolldown_runtime = require('../../../../../_virtual/rolldown_runtime.js');
2
1
  const require_encoding = require('../encoding.js');
3
2
  let algorand_msgpack = require("algorand-msgpack");
4
- algorand_msgpack = require_rolldown_runtime.__toESM(algorand_msgpack);
5
3
 
6
4
  //#region packages/sdk/src/encoding/schema/map.ts
7
5
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"map.js","names":["Schema","entries: NamedMapEntry[]","obj: { [key: string]: JSONEncodingData }"],"sources":["../../../../../../packages/sdk/src/encoding/schema/map.ts"],"sourcesContent":["import { RawBinaryString } from 'algorand-msgpack'\nimport { Schema, MsgpackEncodingData, MsgpackRawStringProvider, JSONEncodingData, PrepareJSONOptions } from '../encoding.js'\nimport { ensureUint64, arrayEqual } from '../../utils/utils.js'\nimport { bytesToString, coerceToBytes, bytesToBase64, base64ToBytes } from '../binarydata.js'\n\n/* eslint-disable class-methods-use-this */\n\n/**\n * Describes a key-value entry in a NamedMapSchema.\n */\nexport interface NamedMapEntry {\n /**\n * Key of the entry. Must be unique for this map.\n */\n key: string\n /**\n * The Schema for the entry's value.\n */\n valueSchema: Schema\n /**\n * If true, the entry will be omitted from the encoding if the value is the default value.\n */\n omitEmpty: boolean\n /**\n * If true, valueSchema must be a NamedMapSchema and key must be the empty string. The fields of\n * valueSchema will be embedded directly in the parent map.\n *\n * omitEmpty is ignored for embedded entries. Instead, the individual omitEmpty values of the\n * embedded fields are used.\n */\n embedded?: boolean\n}\n\n/**\n * Applies the omitEmpty flag to all entries in the array.\n * @param entries - The entries to apply the flag to.\n * @returns A new array with the omitEmpty flag applied to all entries.\n */\nexport function allOmitEmpty(entries: Array<Omit<NamedMapEntry, 'omitEmpty'>>): NamedMapEntry[] {\n return entries.map((entry) => ({ ...entry, omitEmpty: true }))\n}\n\n/**\n * Schema for a map/struct with a fixed set of known string fields.\n */\nexport class NamedMapSchema extends Schema {\n private readonly entries: NamedMapEntry[]\n\n constructor(entries: NamedMapEntry[]) {\n super()\n this.entries = entries\n this.checkEntries()\n }\n\n /**\n * Adds new entries to the map schema. WARNING: this is a mutable operation, and you should be very\n * careful when using it. Any error that happens here is non-recoverable and will corrupt the\n * NamedMapSchema object;\n * @param entries - The entries to add.\n */\n public pushEntries(...entries: NamedMapEntry[]) {\n this.entries.push(...entries)\n this.checkEntries()\n }\n\n private checkEntries() {\n for (const entry of this.entries) {\n if (entry.embedded) {\n if (entry.key !== '') {\n throw new Error('Embedded entries must have an empty key')\n }\n if (!(entry.valueSchema instanceof NamedMapSchema)) {\n throw new Error('Embedded entry valueSchema must be a NamedMapSchema')\n }\n }\n }\n\n const keys = new Set<string>()\n for (const entry of this.getEntries()) {\n if (keys.has(entry.key)) {\n throw new Error(`Duplicate key: ${entry.key}`)\n }\n keys.add(entry.key)\n }\n }\n\n /**\n * Returns all top-level entries, properly accounting for fields from embedded entries.\n * @returns An array of all top-level entries for this map.\n */\n public getEntries(): NamedMapEntry[] {\n const entries: NamedMapEntry[] = []\n for (const entry of this.entries) {\n if (entry.embedded) {\n const embeddedMapSchema = entry.valueSchema as NamedMapSchema\n entries.push(...embeddedMapSchema.getEntries())\n } else {\n entries.push(entry)\n }\n }\n return entries\n }\n\n public defaultValue(): Map<string, unknown> {\n const map = new Map<string, unknown>()\n for (const entry of this.getEntries()) {\n map.set(entry.key, entry.valueSchema.defaultValue())\n }\n return map\n }\n\n public isDefaultValue(data: unknown): boolean {\n if (!(data instanceof Map)) return false\n for (const entry of this.getEntries()) {\n if (!entry.valueSchema.isDefaultValue(data.get(entry.key))) {\n return false\n }\n }\n return true\n }\n\n public prepareMsgpack(data: unknown): MsgpackEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`NamedMapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const map = new Map<string, MsgpackEncodingData>()\n for (const entry of this.getEntries()) {\n const value = data.get(entry.key)\n if (entry.omitEmpty && entry.valueSchema.isDefaultValue(value)) {\n continue\n }\n map.set(entry.key, entry.valueSchema.prepareMsgpack(value))\n }\n return map\n }\n\n public fromPreparedMsgpack(encoded: MsgpackEncodingData, rawStringProvider: MsgpackRawStringProvider): Map<string, unknown> {\n if (!(encoded instanceof Map)) {\n throw new Error('NamedMapSchema data must be a Map')\n }\n const map = new Map<string, unknown>()\n for (const entry of this.getEntries()) {\n if (encoded.has(entry.key)) {\n map.set(entry.key, entry.valueSchema.fromPreparedMsgpack(encoded.get(entry.key), rawStringProvider.withMapValue(entry.key)))\n } else if (entry.omitEmpty) {\n map.set(entry.key, entry.valueSchema.defaultValue())\n } else {\n throw new Error(`Missing key: ${entry.key}`)\n }\n }\n return map\n }\n\n public prepareJSON(data: unknown, options: PrepareJSONOptions): JSONEncodingData {\n if (!(data instanceof Map)) {\n throw new Error('NamedMapSchema data must be a Map')\n }\n const obj: { [key: string]: JSONEncodingData } = {}\n for (const entry of this.getEntries()) {\n const value = data.get(entry.key)\n if (entry.omitEmpty && entry.valueSchema.isDefaultValue(value)) {\n continue\n }\n obj[entry.key] = entry.valueSchema.prepareJSON(value, options)\n }\n return obj\n }\n\n public fromPreparedJSON(encoded: JSONEncodingData): Map<string, unknown> {\n if (encoded == null || typeof encoded !== 'object' || Array.isArray(encoded)) {\n throw new Error('NamedMapSchema data must be an object')\n }\n const map = new Map<string, unknown>()\n for (const entry of this.getEntries()) {\n if (Object.prototype.hasOwnProperty.call(encoded, entry.key)) {\n map.set(entry.key, entry.valueSchema.fromPreparedJSON(encoded[entry.key]))\n } else if (entry.omitEmpty) {\n map.set(entry.key, entry.valueSchema.defaultValue())\n } else {\n throw new Error(`Missing key: ${entry.key}`)\n }\n }\n return map\n }\n}\n\n/**\n * Combines multiple maps into a single map. Throws an error if any of the maps have duplicate keys.\n * @param maps - The maps to combine.\n * @returns A new map with all the entries from the input maps.\n */\nexport function combineMaps<K, V>(...maps: Array<Map<K, V>>): Map<K, V> {\n const combined = new Map<K, V>()\n for (const map of maps) {\n for (const [key, value] of map) {\n if (combined.has(key)) {\n throw new Error(`Duplicate key: ${key}`)\n }\n combined.set(key, value)\n }\n }\n return combined\n}\n\n/**\n * Converts a map to a new map with different keys and values.\n * @param map - The map to convert.\n * @param func - The function to convert each entry.\n * @returns A new map with the converted entries.\n */\nexport function convertMap<K1, V1, K2, V2>(map: Map<K1, V1>, func: (k: K1, v: V1) => [K2, V2]): Map<K2, V2> {\n const mapped = new Map<K2, V2>()\n for (const [key, value] of map) {\n const [newKey, newValue] = func(key, value)\n mapped.set(newKey, newValue)\n }\n return mapped\n}\n\n/**\n * Schema for a map with a variable number of uint64 keys.\n */\nexport class Uint64MapSchema extends Schema {\n constructor(public readonly valueSchema: Schema) {\n super()\n }\n\n public defaultValue(): Map<bigint, unknown> {\n return new Map()\n }\n\n public isDefaultValue(data: unknown): boolean {\n return data instanceof Map && data.size === 0\n }\n\n public prepareMsgpack(data: unknown): MsgpackEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`Uint64MapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<bigint, MsgpackEncodingData>()\n for (const [key, value] of data) {\n const bigintKey = ensureUint64(key)\n if (prepared.has(bigintKey)) {\n throw new Error(`Duplicate key: ${bigintKey}`)\n }\n prepared.set(bigintKey, this.valueSchema.prepareMsgpack(value))\n }\n return prepared\n }\n\n public fromPreparedMsgpack(encoded: MsgpackEncodingData, rawStringProvider: MsgpackRawStringProvider): Map<bigint, unknown> {\n if (!(encoded instanceof Map)) {\n throw new Error('Uint64MapSchema data must be a Map')\n }\n const map = new Map<bigint, unknown>()\n for (const [key, value] of encoded) {\n const bigintKey = ensureUint64(key)\n if (map.has(bigintKey)) {\n throw new Error(`Duplicate key: ${bigintKey}`)\n }\n map.set(bigintKey, this.valueSchema.fromPreparedMsgpack(value, rawStringProvider.withMapValue(key)))\n }\n return map\n }\n\n public prepareJSON(data: unknown, options: PrepareJSONOptions): JSONEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`Uint64MapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<bigint, JSONEncodingData>()\n for (const [key, value] of data) {\n const bigintKey = ensureUint64(key)\n if (prepared.has(bigintKey)) {\n throw new Error(`Duplicate key: ${bigintKey}`)\n }\n prepared.set(bigintKey, this.valueSchema.prepareJSON(value, options))\n }\n // Convert map to object\n const obj: { [key: string]: JSONEncodingData } = {}\n for (const [key, value] of prepared) {\n obj[key.toString()] = value\n }\n return obj\n }\n\n public fromPreparedJSON(encoded: JSONEncodingData): Map<bigint, unknown> {\n if (encoded == null || typeof encoded !== 'object' || Array.isArray(encoded)) {\n throw new Error('Uint64MapSchema data must be an object')\n }\n const map = new Map<bigint, unknown>()\n for (const [key, value] of Object.entries(encoded)) {\n const bigintKey = BigInt(key)\n if (map.has(bigintKey)) {\n throw new Error(`Duplicate key: ${bigintKey}`)\n }\n map.set(bigintKey, this.valueSchema.fromPreparedJSON(value))\n }\n return map\n }\n}\n\n/**\n * Schema for a map with a variable number of string keys.\n */\nexport class StringMapSchema extends Schema {\n constructor(public readonly valueSchema: Schema) {\n super()\n }\n\n public defaultValue(): Map<string, unknown> {\n return new Map()\n }\n\n public isDefaultValue(data: unknown): boolean {\n return data instanceof Map && data.size === 0\n }\n\n public prepareMsgpack(data: unknown): MsgpackEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`StringMapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<string, MsgpackEncodingData>()\n for (const [key, value] of data) {\n if (typeof key !== 'string') {\n throw new Error(`Invalid key: ${key}`)\n }\n if (prepared.has(key)) {\n throw new Error(`Duplicate key: ${key}`)\n }\n prepared.set(key, this.valueSchema.prepareMsgpack(value))\n }\n return prepared\n }\n\n public fromPreparedMsgpack(encoded: MsgpackEncodingData, rawStringProvider: MsgpackRawStringProvider): Map<string, unknown> {\n if (!(encoded instanceof Map)) {\n throw new Error('StringMapSchema data must be a Map')\n }\n const map = new Map<string, unknown>()\n for (const [key, value] of encoded) {\n if (typeof key !== 'string') {\n throw new Error(`Invalid key: ${key}`)\n }\n if (map.has(key)) {\n throw new Error(`Duplicate key: ${key}`)\n }\n map.set(key, this.valueSchema.fromPreparedMsgpack(value, rawStringProvider.withMapValue(key)))\n }\n return map\n }\n\n public prepareJSON(data: unknown, options: PrepareJSONOptions): JSONEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`StringMapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<string, JSONEncodingData>()\n for (const [key, value] of data) {\n if (typeof key !== 'string') {\n throw new Error(`Invalid key: ${key}`)\n }\n if (prepared.has(key)) {\n throw new Error(`Duplicate key: ${key}`)\n }\n prepared.set(key, this.valueSchema.prepareJSON(value, options))\n }\n // Convert map to object\n const obj: { [key: string]: JSONEncodingData } = {}\n for (const [key, value] of prepared) {\n obj[key] = value\n }\n return obj\n }\n\n public fromPreparedJSON(encoded: JSONEncodingData): Map<string, unknown> {\n if (encoded == null || typeof encoded !== 'object' || Array.isArray(encoded)) {\n throw new Error('StringMapSchema data must be an object')\n }\n const map = new Map<string, unknown>()\n for (const [key, value] of Object.entries(encoded)) {\n if (map.has(key)) {\n throw new Error(`Duplicate key: ${key}`)\n }\n map.set(key, this.valueSchema.fromPreparedJSON(value))\n }\n return map\n }\n}\n\n/**\n * Schema for a map with a variable number of byte array keys.\n */\nexport class ByteArrayMapSchema extends Schema {\n constructor(public readonly valueSchema: Schema) {\n super()\n }\n\n public defaultValue(): Map<Uint8Array, unknown> {\n return new Map()\n }\n\n public isDefaultValue(data: unknown): boolean {\n return data instanceof Map && data.size === 0\n }\n\n public prepareMsgpack(data: unknown): MsgpackEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`ByteArrayMapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<Uint8Array, MsgpackEncodingData>()\n for (const [key, value] of data) {\n if (!(key instanceof Uint8Array)) {\n throw new Error(`Invalid key: ${key} (${typeof key})`)\n }\n prepared.set(key, this.valueSchema.prepareMsgpack(value))\n }\n return prepared\n }\n\n public fromPreparedMsgpack(encoded: MsgpackEncodingData, rawStringProvider: MsgpackRawStringProvider): Map<Uint8Array, unknown> {\n if (!(encoded instanceof Map)) {\n throw new Error('ByteArrayMapSchema data must be a Map')\n }\n const map = new Map<Uint8Array, unknown>()\n for (const [key, value] of encoded) {\n if (!(key instanceof Uint8Array)) {\n throw new Error(`Invalid key: ${key} (${typeof key})`)\n }\n map.set(key, this.valueSchema.fromPreparedMsgpack(value, rawStringProvider.withMapValue(key)))\n }\n return map\n }\n\n public prepareJSON(data: unknown, options: PrepareJSONOptions): JSONEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`ByteArrayMapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<string, JSONEncodingData>()\n for (const [key, value] of data) {\n if (!(key instanceof Uint8Array)) {\n throw new Error(`Invalid key: ${key} (${typeof key})`)\n }\n const b64Encoded = bytesToBase64(key)\n if (prepared.has(b64Encoded)) {\n throw new Error(`Duplicate key (base64): ${b64Encoded}`)\n }\n prepared.set(b64Encoded, this.valueSchema.prepareJSON(value, options))\n }\n // Convert map to object\n const obj: { [key: string]: JSONEncodingData } = {}\n for (const [key, value] of prepared) {\n obj[key] = value\n }\n return obj\n }\n\n public fromPreparedJSON(encoded: JSONEncodingData): Map<Uint8Array, unknown> {\n if (encoded == null || typeof encoded !== 'object' || Array.isArray(encoded)) {\n throw new Error('ByteArrayMapSchema data must be an object')\n }\n const map = new Map<Uint8Array, unknown>()\n for (const [key, value] of Object.entries(encoded)) {\n map.set(base64ToBytes(key), this.valueSchema.fromPreparedJSON(value))\n }\n return map\n }\n}\n\n/**\n * Converts any RawBinaryString values to regular strings in a MsgpackEncodingData object.\n *\n * Note this conversion may be lossy if the binary data is not valid UTF-8.\n *\n * @returns A new object with RawBinaryString values converted to strings.\n */\nfunction convertRawStringsInMsgpackValue(value: MsgpackEncodingData): MsgpackEncodingData {\n if (value instanceof RawBinaryString) {\n return bytesToString(value.rawBinaryValue as Uint8Array)\n }\n if (value instanceof Map) {\n const newMap = new Map<string | number | bigint | Uint8Array, MsgpackEncodingData>()\n for (const [key, val] of value) {\n newMap.set(convertRawStringsInMsgpackValue(key) as string | number | bigint | Uint8Array, convertRawStringsInMsgpackValue(val))\n }\n return newMap\n }\n if (Array.isArray(value)) {\n return value.map(convertRawStringsInMsgpackValue)\n }\n return value\n}\n\n/**\n * Schema for a map with a variable number of binary string keys.\n *\n * See SpecialCaseBinaryStringSchema for more information about the key type.\n */\nexport class SpecialCaseBinaryStringMapSchema extends Schema {\n constructor(public readonly valueSchema: Schema) {\n super()\n }\n\n public defaultValue(): Map<Uint8Array, unknown> {\n return new Map()\n }\n\n public isDefaultValue(data: unknown): boolean {\n return data instanceof Map && data.size === 0\n }\n\n public prepareMsgpack(data: unknown): MsgpackEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`SpecialCaseBinaryStringMapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<RawBinaryString, MsgpackEncodingData>()\n for (const [key, value] of data) {\n if (!(key instanceof Uint8Array)) {\n throw new Error(`Invalid key: ${key} (${typeof key})`)\n }\n prepared.set(new RawBinaryString(key), this.valueSchema.prepareMsgpack(value))\n }\n // Cast is needed because RawBinaryString is not part of the standard MsgpackEncodingData\n return prepared as unknown as Map<Uint8Array, MsgpackEncodingData>\n }\n\n public fromPreparedMsgpack(_encoded: MsgpackEncodingData, rawStringProvider: MsgpackRawStringProvider): Map<Uint8Array, unknown> {\n const map = new Map<Uint8Array, unknown>()\n const keysAndValues = rawStringProvider.getRawStringKeysAndValuesAtCurrentLocation()\n for (const [key, value] of keysAndValues) {\n map.set(\n key,\n this.valueSchema.fromPreparedMsgpack(\n convertRawStringsInMsgpackValue(value),\n rawStringProvider.withMapValue(new RawBinaryString(key)),\n ),\n )\n }\n return map\n }\n\n public prepareJSON(data: unknown, options: PrepareJSONOptions): JSONEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`SpecialCaseBinaryStringMapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<string, JSONEncodingData>()\n for (const [key, value] of data) {\n if (!(key instanceof Uint8Array)) {\n throw new Error(`Invalid key: ${key}`)\n }\n // Not safe to convert to string for all binary data\n const keyStringValue = bytesToString(key)\n if (!options.lossyBinaryStringConversion && !arrayEqual(coerceToBytes(keyStringValue), key)) {\n throw new Error(\n `Invalid UTF-8 byte array encountered. Encode with lossyBinaryStringConversion enabled to bypass this check. Base64 value: ${bytesToBase64(key)}`,\n )\n }\n prepared.set(keyStringValue, this.valueSchema.prepareJSON(value, options))\n }\n // Convert map to object\n const obj: { [key: string]: JSONEncodingData } = {}\n for (const [key, value] of prepared) {\n obj[key] = value\n }\n return obj\n }\n\n public fromPreparedJSON(encoded: JSONEncodingData): Map<Uint8Array, unknown> {\n if (encoded == null || typeof encoded !== 'object' || Array.isArray(encoded)) {\n throw new Error('SpecialCaseBinaryStringMapSchema data must be an object')\n }\n const map = new Map<Uint8Array, unknown>()\n for (const [key, value] of Object.entries(encoded)) {\n map.set(coerceToBytes(key), this.valueSchema.fromPreparedJSON(value))\n }\n return map\n }\n}\n"],"mappings":";;;;;;;;;;;AAsCA,SAAgB,aAAa,SAAmE;AAC9F,QAAO,QAAQ,KAAK,WAAW;EAAE,GAAG;EAAO,WAAW;EAAM,EAAE;;;;;AAMhE,IAAa,iBAAb,MAAa,uBAA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OAAO,UAAU,eAAe,KAAK,SAAS,MAAM,IAAI,CAC1D,KAAI,IAAI,MAAM,KAAK,MAAM,YAAY,iBAAiB,QAAQ,MAAM,KAAK,CAAC;WACjE,MAAM,UACf,KAAI,IAAI,MAAM,KAAK,MAAM,YAAY,cAAc,CAAC;MAEpD,OAAM,IAAI,MAAM,gBAAgB,MAAM,MAAM;AAGhD,SAAO"}
1
+ {"version":3,"file":"map.js","names":["Schema","entries: NamedMapEntry[]","obj: { [key: string]: JSONEncodingData }"],"sources":["../../../../../../packages/sdk/src/encoding/schema/map.ts"],"sourcesContent":["import { RawBinaryString } from 'algorand-msgpack'\nimport { Schema, MsgpackEncodingData, MsgpackRawStringProvider, JSONEncodingData, PrepareJSONOptions } from '../encoding.js'\nimport { ensureUint64, arrayEqual } from '../../utils/utils.js'\nimport { bytesToString, coerceToBytes, bytesToBase64, base64ToBytes } from '../binarydata.js'\n\n/* eslint-disable class-methods-use-this */\n\n/**\n * Describes a key-value entry in a NamedMapSchema.\n */\nexport interface NamedMapEntry {\n /**\n * Key of the entry. Must be unique for this map.\n */\n key: string\n /**\n * The Schema for the entry's value.\n */\n valueSchema: Schema\n /**\n * If true, the entry will be omitted from the encoding if the value is the default value.\n */\n omitEmpty: boolean\n /**\n * If true, valueSchema must be a NamedMapSchema and key must be the empty string. The fields of\n * valueSchema will be embedded directly in the parent map.\n *\n * omitEmpty is ignored for embedded entries. Instead, the individual omitEmpty values of the\n * embedded fields are used.\n */\n embedded?: boolean\n}\n\n/**\n * Applies the omitEmpty flag to all entries in the array.\n * @param entries - The entries to apply the flag to.\n * @returns A new array with the omitEmpty flag applied to all entries.\n */\nexport function allOmitEmpty(entries: Array<Omit<NamedMapEntry, 'omitEmpty'>>): NamedMapEntry[] {\n return entries.map((entry) => ({ ...entry, omitEmpty: true }))\n}\n\n/**\n * Schema for a map/struct with a fixed set of known string fields.\n */\nexport class NamedMapSchema extends Schema {\n private readonly entries: NamedMapEntry[]\n\n constructor(entries: NamedMapEntry[]) {\n super()\n this.entries = entries\n this.checkEntries()\n }\n\n /**\n * Adds new entries to the map schema. WARNING: this is a mutable operation, and you should be very\n * careful when using it. Any error that happens here is non-recoverable and will corrupt the\n * NamedMapSchema object;\n * @param entries - The entries to add.\n */\n public pushEntries(...entries: NamedMapEntry[]) {\n this.entries.push(...entries)\n this.checkEntries()\n }\n\n private checkEntries() {\n for (const entry of this.entries) {\n if (entry.embedded) {\n if (entry.key !== '') {\n throw new Error('Embedded entries must have an empty key')\n }\n if (!(entry.valueSchema instanceof NamedMapSchema)) {\n throw new Error('Embedded entry valueSchema must be a NamedMapSchema')\n }\n }\n }\n\n const keys = new Set<string>()\n for (const entry of this.getEntries()) {\n if (keys.has(entry.key)) {\n throw new Error(`Duplicate key: ${entry.key}`)\n }\n keys.add(entry.key)\n }\n }\n\n /**\n * Returns all top-level entries, properly accounting for fields from embedded entries.\n * @returns An array of all top-level entries for this map.\n */\n public getEntries(): NamedMapEntry[] {\n const entries: NamedMapEntry[] = []\n for (const entry of this.entries) {\n if (entry.embedded) {\n const embeddedMapSchema = entry.valueSchema as NamedMapSchema\n entries.push(...embeddedMapSchema.getEntries())\n } else {\n entries.push(entry)\n }\n }\n return entries\n }\n\n public defaultValue(): Map<string, unknown> {\n const map = new Map<string, unknown>()\n for (const entry of this.getEntries()) {\n map.set(entry.key, entry.valueSchema.defaultValue())\n }\n return map\n }\n\n public isDefaultValue(data: unknown): boolean {\n if (!(data instanceof Map)) return false\n for (const entry of this.getEntries()) {\n if (!entry.valueSchema.isDefaultValue(data.get(entry.key))) {\n return false\n }\n }\n return true\n }\n\n public prepareMsgpack(data: unknown): MsgpackEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`NamedMapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const map = new Map<string, MsgpackEncodingData>()\n for (const entry of this.getEntries()) {\n const value = data.get(entry.key)\n if (entry.omitEmpty && entry.valueSchema.isDefaultValue(value)) {\n continue\n }\n map.set(entry.key, entry.valueSchema.prepareMsgpack(value))\n }\n return map\n }\n\n public fromPreparedMsgpack(encoded: MsgpackEncodingData, rawStringProvider: MsgpackRawStringProvider): Map<string, unknown> {\n if (!(encoded instanceof Map)) {\n throw new Error('NamedMapSchema data must be a Map')\n }\n const map = new Map<string, unknown>()\n for (const entry of this.getEntries()) {\n if (encoded.has(entry.key)) {\n map.set(entry.key, entry.valueSchema.fromPreparedMsgpack(encoded.get(entry.key), rawStringProvider.withMapValue(entry.key)))\n } else if (entry.omitEmpty) {\n map.set(entry.key, entry.valueSchema.defaultValue())\n } else {\n throw new Error(`Missing key: ${entry.key}`)\n }\n }\n return map\n }\n\n public prepareJSON(data: unknown, options: PrepareJSONOptions): JSONEncodingData {\n if (!(data instanceof Map)) {\n throw new Error('NamedMapSchema data must be a Map')\n }\n const obj: { [key: string]: JSONEncodingData } = {}\n for (const entry of this.getEntries()) {\n const value = data.get(entry.key)\n if (entry.omitEmpty && entry.valueSchema.isDefaultValue(value)) {\n continue\n }\n obj[entry.key] = entry.valueSchema.prepareJSON(value, options)\n }\n return obj\n }\n\n public fromPreparedJSON(encoded: JSONEncodingData): Map<string, unknown> {\n if (encoded == null || typeof encoded !== 'object' || Array.isArray(encoded)) {\n throw new Error('NamedMapSchema data must be an object')\n }\n const map = new Map<string, unknown>()\n for (const entry of this.getEntries()) {\n if (Object.prototype.hasOwnProperty.call(encoded, entry.key)) {\n map.set(entry.key, entry.valueSchema.fromPreparedJSON(encoded[entry.key]))\n } else if (entry.omitEmpty) {\n map.set(entry.key, entry.valueSchema.defaultValue())\n } else {\n throw new Error(`Missing key: ${entry.key}`)\n }\n }\n return map\n }\n}\n\n/**\n * Combines multiple maps into a single map. Throws an error if any of the maps have duplicate keys.\n * @param maps - The maps to combine.\n * @returns A new map with all the entries from the input maps.\n */\nexport function combineMaps<K, V>(...maps: Array<Map<K, V>>): Map<K, V> {\n const combined = new Map<K, V>()\n for (const map of maps) {\n for (const [key, value] of map) {\n if (combined.has(key)) {\n throw new Error(`Duplicate key: ${key}`)\n }\n combined.set(key, value)\n }\n }\n return combined\n}\n\n/**\n * Converts a map to a new map with different keys and values.\n * @param map - The map to convert.\n * @param func - The function to convert each entry.\n * @returns A new map with the converted entries.\n */\nexport function convertMap<K1, V1, K2, V2>(map: Map<K1, V1>, func: (k: K1, v: V1) => [K2, V2]): Map<K2, V2> {\n const mapped = new Map<K2, V2>()\n for (const [key, value] of map) {\n const [newKey, newValue] = func(key, value)\n mapped.set(newKey, newValue)\n }\n return mapped\n}\n\n/**\n * Schema for a map with a variable number of uint64 keys.\n */\nexport class Uint64MapSchema extends Schema {\n constructor(public readonly valueSchema: Schema) {\n super()\n }\n\n public defaultValue(): Map<bigint, unknown> {\n return new Map()\n }\n\n public isDefaultValue(data: unknown): boolean {\n return data instanceof Map && data.size === 0\n }\n\n public prepareMsgpack(data: unknown): MsgpackEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`Uint64MapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<bigint, MsgpackEncodingData>()\n for (const [key, value] of data) {\n const bigintKey = ensureUint64(key)\n if (prepared.has(bigintKey)) {\n throw new Error(`Duplicate key: ${bigintKey}`)\n }\n prepared.set(bigintKey, this.valueSchema.prepareMsgpack(value))\n }\n return prepared\n }\n\n public fromPreparedMsgpack(encoded: MsgpackEncodingData, rawStringProvider: MsgpackRawStringProvider): Map<bigint, unknown> {\n if (!(encoded instanceof Map)) {\n throw new Error('Uint64MapSchema data must be a Map')\n }\n const map = new Map<bigint, unknown>()\n for (const [key, value] of encoded) {\n const bigintKey = ensureUint64(key)\n if (map.has(bigintKey)) {\n throw new Error(`Duplicate key: ${bigintKey}`)\n }\n map.set(bigintKey, this.valueSchema.fromPreparedMsgpack(value, rawStringProvider.withMapValue(key)))\n }\n return map\n }\n\n public prepareJSON(data: unknown, options: PrepareJSONOptions): JSONEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`Uint64MapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<bigint, JSONEncodingData>()\n for (const [key, value] of data) {\n const bigintKey = ensureUint64(key)\n if (prepared.has(bigintKey)) {\n throw new Error(`Duplicate key: ${bigintKey}`)\n }\n prepared.set(bigintKey, this.valueSchema.prepareJSON(value, options))\n }\n // Convert map to object\n const obj: { [key: string]: JSONEncodingData } = {}\n for (const [key, value] of prepared) {\n obj[key.toString()] = value\n }\n return obj\n }\n\n public fromPreparedJSON(encoded: JSONEncodingData): Map<bigint, unknown> {\n if (encoded == null || typeof encoded !== 'object' || Array.isArray(encoded)) {\n throw new Error('Uint64MapSchema data must be an object')\n }\n const map = new Map<bigint, unknown>()\n for (const [key, value] of Object.entries(encoded)) {\n const bigintKey = BigInt(key)\n if (map.has(bigintKey)) {\n throw new Error(`Duplicate key: ${bigintKey}`)\n }\n map.set(bigintKey, this.valueSchema.fromPreparedJSON(value))\n }\n return map\n }\n}\n\n/**\n * Schema for a map with a variable number of string keys.\n */\nexport class StringMapSchema extends Schema {\n constructor(public readonly valueSchema: Schema) {\n super()\n }\n\n public defaultValue(): Map<string, unknown> {\n return new Map()\n }\n\n public isDefaultValue(data: unknown): boolean {\n return data instanceof Map && data.size === 0\n }\n\n public prepareMsgpack(data: unknown): MsgpackEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`StringMapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<string, MsgpackEncodingData>()\n for (const [key, value] of data) {\n if (typeof key !== 'string') {\n throw new Error(`Invalid key: ${key}`)\n }\n if (prepared.has(key)) {\n throw new Error(`Duplicate key: ${key}`)\n }\n prepared.set(key, this.valueSchema.prepareMsgpack(value))\n }\n return prepared\n }\n\n public fromPreparedMsgpack(encoded: MsgpackEncodingData, rawStringProvider: MsgpackRawStringProvider): Map<string, unknown> {\n if (!(encoded instanceof Map)) {\n throw new Error('StringMapSchema data must be a Map')\n }\n const map = new Map<string, unknown>()\n for (const [key, value] of encoded) {\n if (typeof key !== 'string') {\n throw new Error(`Invalid key: ${key}`)\n }\n if (map.has(key)) {\n throw new Error(`Duplicate key: ${key}`)\n }\n map.set(key, this.valueSchema.fromPreparedMsgpack(value, rawStringProvider.withMapValue(key)))\n }\n return map\n }\n\n public prepareJSON(data: unknown, options: PrepareJSONOptions): JSONEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`StringMapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<string, JSONEncodingData>()\n for (const [key, value] of data) {\n if (typeof key !== 'string') {\n throw new Error(`Invalid key: ${key}`)\n }\n if (prepared.has(key)) {\n throw new Error(`Duplicate key: ${key}`)\n }\n prepared.set(key, this.valueSchema.prepareJSON(value, options))\n }\n // Convert map to object\n const obj: { [key: string]: JSONEncodingData } = {}\n for (const [key, value] of prepared) {\n obj[key] = value\n }\n return obj\n }\n\n public fromPreparedJSON(encoded: JSONEncodingData): Map<string, unknown> {\n if (encoded == null || typeof encoded !== 'object' || Array.isArray(encoded)) {\n throw new Error('StringMapSchema data must be an object')\n }\n const map = new Map<string, unknown>()\n for (const [key, value] of Object.entries(encoded)) {\n if (map.has(key)) {\n throw new Error(`Duplicate key: ${key}`)\n }\n map.set(key, this.valueSchema.fromPreparedJSON(value))\n }\n return map\n }\n}\n\n/**\n * Schema for a map with a variable number of byte array keys.\n */\nexport class ByteArrayMapSchema extends Schema {\n constructor(public readonly valueSchema: Schema) {\n super()\n }\n\n public defaultValue(): Map<Uint8Array, unknown> {\n return new Map()\n }\n\n public isDefaultValue(data: unknown): boolean {\n return data instanceof Map && data.size === 0\n }\n\n public prepareMsgpack(data: unknown): MsgpackEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`ByteArrayMapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<Uint8Array, MsgpackEncodingData>()\n for (const [key, value] of data) {\n if (!(key instanceof Uint8Array)) {\n throw new Error(`Invalid key: ${key} (${typeof key})`)\n }\n prepared.set(key, this.valueSchema.prepareMsgpack(value))\n }\n return prepared\n }\n\n public fromPreparedMsgpack(encoded: MsgpackEncodingData, rawStringProvider: MsgpackRawStringProvider): Map<Uint8Array, unknown> {\n if (!(encoded instanceof Map)) {\n throw new Error('ByteArrayMapSchema data must be a Map')\n }\n const map = new Map<Uint8Array, unknown>()\n for (const [key, value] of encoded) {\n if (!(key instanceof Uint8Array)) {\n throw new Error(`Invalid key: ${key} (${typeof key})`)\n }\n map.set(key, this.valueSchema.fromPreparedMsgpack(value, rawStringProvider.withMapValue(key)))\n }\n return map\n }\n\n public prepareJSON(data: unknown, options: PrepareJSONOptions): JSONEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`ByteArrayMapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<string, JSONEncodingData>()\n for (const [key, value] of data) {\n if (!(key instanceof Uint8Array)) {\n throw new Error(`Invalid key: ${key} (${typeof key})`)\n }\n const b64Encoded = bytesToBase64(key)\n if (prepared.has(b64Encoded)) {\n throw new Error(`Duplicate key (base64): ${b64Encoded}`)\n }\n prepared.set(b64Encoded, this.valueSchema.prepareJSON(value, options))\n }\n // Convert map to object\n const obj: { [key: string]: JSONEncodingData } = {}\n for (const [key, value] of prepared) {\n obj[key] = value\n }\n return obj\n }\n\n public fromPreparedJSON(encoded: JSONEncodingData): Map<Uint8Array, unknown> {\n if (encoded == null || typeof encoded !== 'object' || Array.isArray(encoded)) {\n throw new Error('ByteArrayMapSchema data must be an object')\n }\n const map = new Map<Uint8Array, unknown>()\n for (const [key, value] of Object.entries(encoded)) {\n map.set(base64ToBytes(key), this.valueSchema.fromPreparedJSON(value))\n }\n return map\n }\n}\n\n/**\n * Converts any RawBinaryString values to regular strings in a MsgpackEncodingData object.\n *\n * Note this conversion may be lossy if the binary data is not valid UTF-8.\n *\n * @returns A new object with RawBinaryString values converted to strings.\n */\nfunction convertRawStringsInMsgpackValue(value: MsgpackEncodingData): MsgpackEncodingData {\n if (value instanceof RawBinaryString) {\n return bytesToString(value.rawBinaryValue as Uint8Array)\n }\n if (value instanceof Map) {\n const newMap = new Map<string | number | bigint | Uint8Array, MsgpackEncodingData>()\n for (const [key, val] of value) {\n newMap.set(convertRawStringsInMsgpackValue(key) as string | number | bigint | Uint8Array, convertRawStringsInMsgpackValue(val))\n }\n return newMap\n }\n if (Array.isArray(value)) {\n return value.map(convertRawStringsInMsgpackValue)\n }\n return value\n}\n\n/**\n * Schema for a map with a variable number of binary string keys.\n *\n * See SpecialCaseBinaryStringSchema for more information about the key type.\n */\nexport class SpecialCaseBinaryStringMapSchema extends Schema {\n constructor(public readonly valueSchema: Schema) {\n super()\n }\n\n public defaultValue(): Map<Uint8Array, unknown> {\n return new Map()\n }\n\n public isDefaultValue(data: unknown): boolean {\n return data instanceof Map && data.size === 0\n }\n\n public prepareMsgpack(data: unknown): MsgpackEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`SpecialCaseBinaryStringMapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<RawBinaryString, MsgpackEncodingData>()\n for (const [key, value] of data) {\n if (!(key instanceof Uint8Array)) {\n throw new Error(`Invalid key: ${key} (${typeof key})`)\n }\n prepared.set(new RawBinaryString(key), this.valueSchema.prepareMsgpack(value))\n }\n // Cast is needed because RawBinaryString is not part of the standard MsgpackEncodingData\n return prepared as unknown as Map<Uint8Array, MsgpackEncodingData>\n }\n\n public fromPreparedMsgpack(_encoded: MsgpackEncodingData, rawStringProvider: MsgpackRawStringProvider): Map<Uint8Array, unknown> {\n const map = new Map<Uint8Array, unknown>()\n const keysAndValues = rawStringProvider.getRawStringKeysAndValuesAtCurrentLocation()\n for (const [key, value] of keysAndValues) {\n map.set(\n key,\n this.valueSchema.fromPreparedMsgpack(\n convertRawStringsInMsgpackValue(value),\n rawStringProvider.withMapValue(new RawBinaryString(key)),\n ),\n )\n }\n return map\n }\n\n public prepareJSON(data: unknown, options: PrepareJSONOptions): JSONEncodingData {\n if (!(data instanceof Map)) {\n throw new Error(`SpecialCaseBinaryStringMapSchema data must be a Map. Got (${typeof data}) ${data}`)\n }\n const prepared = new Map<string, JSONEncodingData>()\n for (const [key, value] of data) {\n if (!(key instanceof Uint8Array)) {\n throw new Error(`Invalid key: ${key}`)\n }\n // Not safe to convert to string for all binary data\n const keyStringValue = bytesToString(key)\n if (!options.lossyBinaryStringConversion && !arrayEqual(coerceToBytes(keyStringValue), key)) {\n throw new Error(\n `Invalid UTF-8 byte array encountered. Encode with lossyBinaryStringConversion enabled to bypass this check. Base64 value: ${bytesToBase64(key)}`,\n )\n }\n prepared.set(keyStringValue, this.valueSchema.prepareJSON(value, options))\n }\n // Convert map to object\n const obj: { [key: string]: JSONEncodingData } = {}\n for (const [key, value] of prepared) {\n obj[key] = value\n }\n return obj\n }\n\n public fromPreparedJSON(encoded: JSONEncodingData): Map<Uint8Array, unknown> {\n if (encoded == null || typeof encoded !== 'object' || Array.isArray(encoded)) {\n throw new Error('SpecialCaseBinaryStringMapSchema data must be an object')\n }\n const map = new Map<Uint8Array, unknown>()\n for (const [key, value] of Object.entries(encoded)) {\n map.set(coerceToBytes(key), this.valueSchema.fromPreparedJSON(value))\n }\n return map\n }\n}\n"],"mappings":";;;;;;;;;AAsCA,SAAgB,aAAa,SAAmE;AAC9F,QAAO,QAAQ,KAAK,WAAW;EAAE,GAAG;EAAO,WAAW;EAAM,EAAE;;;;;AAMhE,IAAa,iBAAb,MAAa,uBAAuBA,wBAAO;CACzC,AAAiB;CAEjB,YAAY,SAA0B;AACpC,SAAO;AACP,OAAK,UAAU;AACf,OAAK,cAAc;;;;;;;;CASrB,AAAO,YAAY,GAAG,SAA0B;AAC9C,OAAK,QAAQ,KAAK,GAAG,QAAQ;AAC7B,OAAK,cAAc;;CAGrB,AAAQ,eAAe;AACrB,OAAK,MAAM,SAAS,KAAK,QACvB,KAAI,MAAM,UAAU;AAClB,OAAI,MAAM,QAAQ,GAChB,OAAM,IAAI,MAAM,0CAA0C;AAE5D,OAAI,EAAE,MAAM,uBAAuB,gBACjC,OAAM,IAAI,MAA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: msig.params.version,
81
81
  threshold: msig.params.threshold
82
82
  };
83
83
  for (const addrWithSigner of msig.subSigners) {
84
84
  const { lsigSigner, addr } = addrWithSigner;
85
85
  const signature = await lsigSigner(this, msig);
86
- this.lmsig.subsignatures.push({
86
+ this.lmsig.subsigs.push({
87
87
  publicKey: addr.publicKey,
88
- signature
88
+ sig: signature
89
89
  });
90
90
  }
91
91
  }
@@ -1 +1 @@
1
- {"version":3,"file":"logicsig.js","names":["isValidAddress","args: Uint8Array[]","signedTxns: Uint8Array[]","stxn: SignedTransaction","encodeSignedTransaction","Address","hash","concatArrays","decodeMsgpack","logicSignatureCodec"],"sources":["../../../../packages/transact/src/logicsig.ts"],"sourcesContent":["import { Address, concatArrays, decodeMsgpack, hash, isValidAddress } from '@algorandfoundation/algokit-common'\nimport { MultisigAccount } from './multisig'\nimport { TransactionSigner } from './signer'\nimport { LogicSignature, MultisigSignature, SignedTransaction, encodeSignedTransaction } from './transactions/signed-transaction'\nimport { logicSignatureCodec } from './transactions/signed-transaction-meta'\nimport { Transaction } from './transactions/transaction'\n\n// base64regex is the regex to test for base64 strings\nconst base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/\n\n/** sanityCheckProgram performs heuristic program validation:\n * check if passed in bytes are Algorand address or is B64 encoded, rather than Teal bytes\n *\n * @param program - Program bytes to check\n */\nexport function sanityCheckProgram(program: Uint8Array) {\n if (!program || program.length === 0) throw new Error('empty program')\n\n const lineBreakOrd = '\\n'.charCodeAt(0)\n const blankSpaceOrd = ' '.charCodeAt(0)\n const tildeOrd = '~'.charCodeAt(0)\n\n const isPrintable = (x: number) => blankSpaceOrd <= x && x <= tildeOrd\n const isAsciiPrintable = program.every((x: number) => x === lineBreakOrd || isPrintable(x))\n\n if (isAsciiPrintable) {\n const programStr = new TextDecoder().decode(program)\n\n if (isValidAddress(programStr)) throw new Error('requesting program bytes, get Algorand address')\n\n if (base64regex.test(programStr)) throw new Error('program should not be b64 encoded')\n\n throw new Error('program bytes are all ASCII printable characters, not looking like Teal byte code')\n }\n}\n\nconst PROGRAM_TAG = new TextEncoder().encode('Program')\nconst MSIG_PROGRAM_TAG = new TextEncoder().encode('MsigProgram')\nconst SIGN_PROGRAM_DATA_PREFIX = new TextEncoder().encode('ProgData')\n\n/** Function for signing logic signatures for delegation */\nexport type DelegatedLsigSigner = (lsig: LogicSigAccount, msig?: MultisigAccount) => Promise<Uint8Array>\n\n/** Function for signing program data for a logic signature */\nexport type ProgramDataSigner = (data: Uint8Array, lsig: LogicSigAccount) => Promise<Uint8Array>\n\nexport class LogicSigAccount {\n logic: Uint8Array\n args: Uint8Array[]\n sig?: Uint8Array\n msig?: MultisigSignature\n lmsig?: MultisigSignature\n\n constructor(program: Uint8Array, programArgs?: Array<Uint8Array> | null) {\n if (programArgs && (!Array.isArray(programArgs) || !programArgs.every((arg) => arg.constructor === Uint8Array))) {\n throw new TypeError('Invalid arguments')\n }\n\n let args: Uint8Array[] = []\n if (programArgs != null) args = programArgs.map((arg) => new Uint8Array(arg))\n\n sanityCheckProgram(program)\n\n this.logic = program\n this.args = args\n }\n\n get signer(): TransactionSigner {\n return async (txns: Transaction[], indexes: number[]) => {\n const signedTxns: Uint8Array[] = []\n for (const index of indexes) {\n const txn = txns[index]\n\n const stxn: SignedTransaction = {\n txn,\n lsig: { logic: this.logic, args: this.args, msig: this.msig, lmsig: this.lmsig, sig: this.sig },\n }\n\n signedTxns.push(encodeSignedTransaction(stxn))\n }\n\n return signedTxns\n }\n }\n\n get addr(): Address {\n return this.address()\n }\n\n /**\n * Compute hash of the logic sig program (that is the same as escrow account address) as string address\n * @returns String representation of the address\n */\n address(): Address {\n const toBeSigned = concatArrays(PROGRAM_TAG, this.logic)\n const h = hash(toBeSigned)\n return new Address(h)\n }\n\n async delegate(signer: DelegatedLsigSigner) {\n this.sig = await signer(this)\n }\n\n async delegateMultisig(msig: MultisigAccount) {\n if (this.lmsig == undefined) {\n this.lmsig = {\n subsignatures: [],\n version: msig.params.version,\n threshold: msig.params.threshold,\n }\n }\n for (const addrWithSigner of msig.subSigners) {\n const { lsigSigner, addr } = addrWithSigner\n const signature = await lsigSigner(this, msig)\n\n this.lmsig.subsignatures.push({ publicKey: addr.publicKey, signature })\n }\n }\n\n bytesToSignForDelegation(msig?: MultisigAccount): Uint8Array {\n if (msig) {\n return concatArrays(MSIG_PROGRAM_TAG, msig.addr.publicKey, this.logic)\n } else {\n return concatArrays(PROGRAM_TAG, this.logic)\n }\n }\n\n signProgramData(data: Uint8Array, signer: ProgramDataSigner): Promise<Uint8Array> {\n return signer(data, this)\n }\n\n programDataToSign(data: Uint8Array): Uint8Array {\n return concatArrays(SIGN_PROGRAM_DATA_PREFIX, this.address().publicKey, data)\n }\n}\n\n/**\n * Decodes MsgPack bytes into a logic signature.\n *\n * @param encodedLogicSignature - The MsgPack encoded logic signature\n * @returns The decoded LogicSignature or an error if decoding fails.\n */\nexport function decodeLogicSignature(encodedLogicSignature: Uint8Array): LogicSignature {\n const decodedData = decodeMsgpack(encodedLogicSignature)\n return logicSignatureCodec.decode(decodedData, 'msgpack')\n}\n"],"mappings":";;;;;;;;AAQA,MAAM,cAAc;;;;;;AAOpB,SAAgB,mBAAmB,SAAqB;AACtD,KAAI,CAAC,WAAW,QAAQ,WAAW,EAAG,OAAM,IAAI,MAAM,gBAAgB;CAEtE,MAAM,eAAe,KAAK,WAAW,EAAE;CACvC,MAAM,gBAAgB,IAAI,WAAW,EAAE;CACvC,MAAM,WAAW,IAAI,WAAW,EAAE;CAElC,MAAM,eAAe,MAAc,iBAAiB,KAAK,KAAK;AAG9D,KAFyB,QAAQ,OAAO,MAAc,MAAM,gBAAgB,YAAY,EAAE,CAAC,EAErE;EACpB,MAAM,aAAa,IAAI,aAAa,CAAC,OAAO,QAAQ;AAEpD,MAAIA,+BAAe,WAAW,CAAE,OAAM,IAAI,MAAM,iDAAiD;AAEjG,MAAI,YAAY,KAAK,WAAW,CAAE,OAAM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KAAK,OAAO;GACxB;AAEH,OAAK,MAAM,kBAAkB,KAAK,YAAY;GAC5C,MAAM,EAAE,YAAY,SAAS;GAC7B,MAAM,YAAY,MAAM,WAAW,MAAM,KAAK;AAE9C,QAAK,MAAM,cAAc,KAAK;IAAE,WAAW,KAAK;IAAW;IAAW,CAAC;;;CAI3E,yBAAyB,MAAoC;AAC3D,MAAI,KACF,QAAOA,2BAAa,kBAAkB,KAAK,KAAK,WAAW,KAAK,MAAM;MAEtE,QAAOA,2BAAa,aAAa,KAAK,MAAM;;CAIhD,gBAAgB,MAAkB,QAAgD;AAChF,SAAO,OAAO,MAAM,KAAK;;CAG3B,kBAAkB,MAA8B;AAC9C,SAAOA,2BAAa,0BAA0B,KAAK,SAAS,CAAC,WAAW,KAAK;;;;;;;;;AAUjF,SAAgB,qBAAqB,uBAAmD;CACtF,MAAM,cAAcC,8BAAc,sBAAsB;AACxD,QAAOC,oDAAoB,OAAO,aAAa,UAAU"}
1
+ {"version":3,"file":"logicsig.js","names":["isValidAddress","args: Uint8Array[]","signedTxns: Uint8Array[]","stxn: SignedTransaction","encodeSignedTransaction","Address","hash","concatArrays","decodeMsgpack","logicSignatureCodec"],"sources":["../../../../packages/transact/src/logicsig.ts"],"sourcesContent":["import { Address, concatArrays, decodeMsgpack, hash, isValidAddress } from '@algorandfoundation/algokit-common'\nimport { MultisigAccount } from './multisig'\nimport { TransactionSigner } from './signer'\nimport { LogicSignature, MultisigSignature, SignedTransaction, encodeSignedTransaction } from './transactions/signed-transaction'\nimport { logicSignatureCodec } from './transactions/signed-transaction-meta'\nimport { Transaction } from './transactions/transaction'\n\n// base64regex is the regex to test for base64 strings\nconst base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/\n\n/** sanityCheckProgram performs heuristic program validation:\n * check if passed in bytes are Algorand address or is B64 encoded, rather than Teal bytes\n *\n * @param program - Program bytes to check\n */\nexport function sanityCheckProgram(program: Uint8Array) {\n if (!program || program.length === 0) throw new Error('empty program')\n\n const lineBreakOrd = '\\n'.charCodeAt(0)\n const blankSpaceOrd = ' '.charCodeAt(0)\n const tildeOrd = '~'.charCodeAt(0)\n\n const isPrintable = (x: number) => blankSpaceOrd <= x && x <= tildeOrd\n const isAsciiPrintable = program.every((x: number) => x === lineBreakOrd || isPrintable(x))\n\n if (isAsciiPrintable) {\n const programStr = new TextDecoder().decode(program)\n\n if (isValidAddress(programStr)) throw new Error('requesting program bytes, get Algorand address')\n\n if (base64regex.test(programStr)) throw new Error('program should not be b64 encoded')\n\n throw new Error('program bytes are all ASCII printable characters, not looking like Teal byte code')\n }\n}\n\nconst PROGRAM_TAG = new TextEncoder().encode('Program')\nconst MSIG_PROGRAM_TAG = new TextEncoder().encode('MsigProgram')\nconst SIGN_PROGRAM_DATA_PREFIX = new TextEncoder().encode('ProgData')\n\n/** Function for signing logic signatures for delegation */\nexport type DelegatedLsigSigner = (lsig: LogicSigAccount, msig?: MultisigAccount) => Promise<Uint8Array>\n\n/** Function for signing program data for a logic signature */\nexport type ProgramDataSigner = (data: Uint8Array, lsig: LogicSigAccount) => Promise<Uint8Array>\n\nexport class LogicSigAccount {\n logic: Uint8Array\n args: Uint8Array[]\n sig?: Uint8Array\n msig?: MultisigSignature\n lmsig?: MultisigSignature\n\n constructor(program: Uint8Array, programArgs?: Array<Uint8Array> | null) {\n if (programArgs && (!Array.isArray(programArgs) || !programArgs.every((arg) => arg.constructor === Uint8Array))) {\n throw new TypeError('Invalid arguments')\n }\n\n let args: Uint8Array[] = []\n if (programArgs != null) args = programArgs.map((arg) => new Uint8Array(arg))\n\n sanityCheckProgram(program)\n\n this.logic = program\n this.args = args\n }\n\n get signer(): TransactionSigner {\n return async (txns: Transaction[], indexes: number[]) => {\n const signedTxns: Uint8Array[] = []\n for (const index of indexes) {\n const txn = txns[index]\n\n const stxn: SignedTransaction = {\n txn,\n lsig: { logic: this.logic, args: this.args, msig: this.msig, lmsig: this.lmsig, sig: this.sig },\n }\n\n signedTxns.push(encodeSignedTransaction(stxn))\n }\n\n return signedTxns\n }\n }\n\n get addr(): Address {\n return this.address()\n }\n\n /**\n * Compute hash of the logic sig program (that is the same as escrow account address) as string address\n * @returns String representation of the address\n */\n address(): Address {\n const toBeSigned = concatArrays(PROGRAM_TAG, this.logic)\n const h = hash(toBeSigned)\n return new Address(h)\n }\n\n async delegate(signer: DelegatedLsigSigner) {\n this.sig = await signer(this)\n }\n\n async delegateMultisig(msig: MultisigAccount) {\n if (this.lmsig == undefined) {\n this.lmsig = {\n subsigs: [],\n version: msig.params.version,\n threshold: msig.params.threshold,\n }\n }\n for (const addrWithSigner of msig.subSigners) {\n const { lsigSigner, addr } = addrWithSigner\n const signature = await lsigSigner(this, msig)\n\n this.lmsig.subsigs.push({ publicKey: addr.publicKey, sig: signature })\n }\n }\n\n bytesToSignForDelegation(msig?: MultisigAccount): Uint8Array {\n if (msig) {\n return concatArrays(MSIG_PROGRAM_TAG, msig.addr.publicKey, this.logic)\n } else {\n return concatArrays(PROGRAM_TAG, this.logic)\n }\n }\n\n signProgramData(data: Uint8Array, signer: ProgramDataSigner): Promise<Uint8Array> {\n return signer(data, this)\n }\n\n programDataToSign(data: Uint8Array): Uint8Array {\n return concatArrays(SIGN_PROGRAM_DATA_PREFIX, this.address().publicKey, data)\n }\n}\n\n/**\n * Decodes MsgPack bytes into a logic signature.\n *\n * @param encodedLogicSignature - The MsgPack encoded logic signature\n * @returns The decoded LogicSignature or an error if decoding fails.\n */\nexport function decodeLogicSignature(encodedLogicSignature: Uint8Array): LogicSignature {\n const decodedData = decodeMsgpack(encodedLogicSignature)\n return logicSignatureCodec.decode(decodedData, 'msgpack')\n}\n"],"mappings":";;;;;;;;AAQA,MAAM,cAAc;;;;;;AAOpB,SAAgB,mBAAmB,SAAqB;AACtD,KAAI,CAAC,WAAW,QAAQ,WAAW,EAAG,OAAM,IAAI,MAAM,gBAAgB;CAEtE,MAAM,eAAe,KAAK,WAAW,EAAE;CAC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hD,UAAO;;;CAIX,IAAI,OAAgB;AAClB,SAAO,KAAK,SAAS;;;;;;CAOvB,UAAmB;AAGjB,SAAO,IAAIC,wBADDC,oBADSC,2BAAa,aAAa,KAAK,MAAM,CAC9B,CACL;;CAGvB,MAAM,SAAS,QAA6B;AAC1C,OAAK,MAAM,MAAM,OAAO,KAAK;;CAG/B,MAAM,iBAAiB,MAAuB;AAC5C,MAAI,KAAK,SAAS,OAChB,MAAK,QAAQ;GACX,SAAS,EAAE;GACX,SAAS,KAAK,OAAO;GACrB,WAAW,KAAK,OAAO;GACxB;AAEH,OAAK,MAAM,kBAAkB,KAAK,YAAY;GAC5C,MAAM,EAAE,YAAY,SAAS;GAC7B,MAAM,YAAY,MAAM,WAAW,MAAM,KAAK;AAE9C,QAAK,MAAM,QAAQ,KAAK;IAAE,WAAW,KAAK;IAAW,KAAK;IAAW,CAAC;;;CAI1E,yBAAyB,MAAoC;AAC3D,MAAI,KACF,QAAOA,2BAAa,kBAAkB,KAAK,KAAK,WAAW,KAAK,MAAM;MAEtE,QAAOA,2BAAa,aAAa,KAAK,MAAM;;CAIhD,gBAAgB,MAAkB,QAAgD;AAChF,SAAO,OAAO,MAAM,KAAK;;CAG3B,kBAAkB,MAA8B;AAC9C,SAAOA,2BAAa,0BAA0B,KAAK,SAAS,CAAC,WAAW,KAAK;;;;;;;;;AAUjF,SAAgB,qBAAqB,uBAAmD;CACtF,MAAM,cAAcC,8BAAc,sBAAsB;AACxD,QAAOC,oDAAoB,OAAO,aAAa,UAAU"}
@@ -76,16 +76,16 @@ var LogicSigAccount = class {
76
76
  }
77
77
  async delegateMultisig(msig) {
78
78
  if (this.lmsig == void 0) this.lmsig = {
79
- subsignatures: [],
79
+ subsigs: [],
80
80
  version: msig.params.version,
81
81
  threshold: msig.params.threshold
82
82
  };
83
83
  for (const addrWithSigner of msig.subSigners) {
84
84
  const { lsigSigner, addr } = addrWithSigner;
85
85
  const signature = await lsigSigner(this, msig);
86
- this.lmsig.subsignatures.push({
86
+ this.lmsig.subsigs.push({
87
87
  publicKey: addr.publicKey,
88
- signature
88
+ sig: signature
89
89
  });
90
90
  }
91
91
  }
@@ -1 +1 @@
1
- {"version":3,"file":"logicsig.mjs","names":["args: Uint8Array[]","signedTxns: Uint8Array[]","stxn: SignedTransaction"],"sources":["../../../../packages/transact/src/logicsig.ts"],"sourcesContent":["import { Address, concatArrays, decodeMsgpack, hash, isValidAddress } from '@algorandfoundation/algokit-common'\nimport { MultisigAccount } from './multisig'\nimport { TransactionSigner } from './signer'\nimport { LogicSignature, MultisigSignature, SignedTransaction, encodeSignedTransaction } from './transactions/signed-transaction'\nimport { logicSignatureCodec } from './transactions/signed-transaction-meta'\nimport { Transaction } from './transactions/transaction'\n\n// base64regex is the regex to test for base64 strings\nconst base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/\n\n/** sanityCheckProgram performs heuristic program validation:\n * check if passed in bytes are Algorand address or is B64 encoded, rather than Teal bytes\n *\n * @param program - Program bytes to check\n */\nexport function sanityCheckProgram(program: Uint8Array) {\n if (!program || program.length === 0) throw new Error('empty program')\n\n const lineBreakOrd = '\\n'.charCodeAt(0)\n const blankSpaceOrd = ' '.charCodeAt(0)\n const tildeOrd = '~'.charCodeAt(0)\n\n const isPrintable = (x: number) => blankSpaceOrd <= x && x <= tildeOrd\n const isAsciiPrintable = program.every((x: number) => x === lineBreakOrd || isPrintable(x))\n\n if (isAsciiPrintable) {\n const programStr = new TextDecoder().decode(program)\n\n if (isValidAddress(programStr)) throw new Error('requesting program bytes, get Algorand address')\n\n if (base64regex.test(programStr)) throw new Error('program should not be b64 encoded')\n\n throw new Error('program bytes are all ASCII printable characters, not looking like Teal byte code')\n }\n}\n\nconst PROGRAM_TAG = new TextEncoder().encode('Program')\nconst MSIG_PROGRAM_TAG = new TextEncoder().encode('MsigProgram')\nconst SIGN_PROGRAM_DATA_PREFIX = new TextEncoder().encode('ProgData')\n\n/** Function for signing logic signatures for delegation */\nexport type DelegatedLsigSigner = (lsig: LogicSigAccount, msig?: MultisigAccount) => Promise<Uint8Array>\n\n/** Function for signing program data for a logic signature */\nexport type ProgramDataSigner = (data: Uint8Array, lsig: LogicSigAccount) => Promise<Uint8Array>\n\nexport class LogicSigAccount {\n logic: Uint8Array\n args: Uint8Array[]\n sig?: Uint8Array\n msig?: MultisigSignature\n lmsig?: MultisigSignature\n\n constructor(program: Uint8Array, programArgs?: Array<Uint8Array> | null) {\n if (programArgs && (!Array.isArray(programArgs) || !programArgs.every((arg) => arg.constructor === Uint8Array))) {\n throw new TypeError('Invalid arguments')\n }\n\n let args: Uint8Array[] = []\n if (programArgs != null) args = programArgs.map((arg) => new Uint8Array(arg))\n\n sanityCheckProgram(program)\n\n this.logic = program\n this.args = args\n }\n\n get signer(): TransactionSigner {\n return async (txns: Transaction[], indexes: number[]) => {\n const signedTxns: Uint8Array[] = []\n for (const index of indexes) {\n const txn = txns[index]\n\n const stxn: SignedTransaction = {\n txn,\n lsig: { logic: this.logic, args: this.args, msig: this.msig, lmsig: this.lmsig, sig: this.sig },\n }\n\n signedTxns.push(encodeSignedTransaction(stxn))\n }\n\n return signedTxns\n }\n }\n\n get addr(): Address {\n return this.address()\n }\n\n /**\n * Compute hash of the logic sig program (that is the same as escrow account address) as string address\n * @returns String representation of the address\n */\n address(): Address {\n const toBeSigned = concatArrays(PROGRAM_TAG, this.logic)\n const h = hash(toBeSigned)\n return new Address(h)\n }\n\n async delegate(signer: DelegatedLsigSigner) {\n this.sig = await signer(this)\n }\n\n async delegateMultisig(msig: MultisigAccount) {\n if (this.lmsig == undefined) {\n this.lmsig = {\n subsignatures: [],\n version: msig.params.version,\n threshold: msig.params.threshold,\n }\n }\n for (const addrWithSigner of msig.subSigners) {\n const { lsigSigner, addr } = addrWithSigner\n const signature = await lsigSigner(this, msig)\n\n this.lmsig.subsignatures.push({ publicKey: addr.publicKey, signature })\n }\n }\n\n bytesToSignForDelegation(msig?: MultisigAccount): Uint8Array {\n if (msig) {\n return concatArrays(MSIG_PROGRAM_TAG, msig.addr.publicKey, this.logic)\n } else {\n return concatArrays(PROGRAM_TAG, this.logic)\n }\n }\n\n signProgramData(data: Uint8Array, signer: ProgramDataSigner): Promise<Uint8Array> {\n return signer(data, this)\n }\n\n programDataToSign(data: Uint8Array): Uint8Array {\n return concatArrays(SIGN_PROGRAM_DATA_PREFIX, this.address().publicKey, data)\n }\n}\n\n/**\n * Decodes MsgPack bytes into a logic signature.\n *\n * @param encodedLogicSignature - The MsgPack encoded logic signature\n * @returns The decoded LogicSignature or an error if decoding fails.\n */\nexport function decodeLogicSignature(encodedLogicSignature: Uint8Array): LogicSignature {\n const decodedData = decodeMsgpack(encodedLogicSignature)\n return logicSignatureCodec.decode(decodedData, 'msgpack')\n}\n"],"mappings":";;;;;;;;AAQA,MAAM,cAAc;;;;;;AAOpB,SAAgB,mBAAmB,SAAqB;AACtD,KAAI,CAAC,WAAW,QAAQ,WAAW,EAAG,OAAM,IAAI,MAAM,gBAAgB;CAEtE,MAAM,eAAe,KAAK,WAAW,EAAE;CACvC,MAAM,gBAAgB,IAAI,WAAW,EAAE;CACvC,MAAM,WAAW,IAAI,WAAW,EAAE;CAElC,MAAM,eAAe,MAAc,iBAAiB,KAAK,KAAK;AAG9D,KAFyB,QAAQ,OAAO,MAAc,MAAM,gBAAgB,YAAY,EAAE,CAAC,EAErE;EACpB,MAAM,aAAa,IAAI,aAAa,CAAC,OAAO,QAAQ;AAEpD,MAAI,eAAe,WAAW,CAAE,OAAM,IAAI,MAAM,iDAAiD;AAEjG,MAAI,YAAY,KAAK,WAAW,CAAE,OAAM,IAAI,MAAM,oCAAoC;AAEtF,QAAM,IAAI,MAAM,oFAAoF;;;AAIxG,MAAM,cAAc,IAAI,aAAa,CAAC,OAAO,UAAU;AACvD,MAAM,mBAAmB,IAAI,aAAa,CAAC,OAAO,cAAc;AAChE,MAAM,2BAA2B,IAAI,aAAa,CAAC,OAAO,WAAW;AAQ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oC;AAC3D,MAAI,KACF,QAAO,aAAa,kBAAkB,KAAK,KAAK,WAAW,KAAK,MAAM;MAEtE,QAAO,aAAa,aAAa,KAAK,MAAM;;CAIhD,gBAAgB,MAAkB,QAAgD;AAChF,SAAO,OAAO,MAAM,KAAK;;CAG3B,kBAAkB,MAA8B;AAC9C,SAAO,aAAa,0BAA0B,KAAK,SAAS,CAAC,WAAW,KAAK;;;;;;;;;AAUjF,SAAgB,qBAAqB,uBAAmD;CACtF,MAAM,cAAc,cAAc,sBAAsB;AACxD,QAAO,oBAAoB,OAAO,aAAa,UAAU"}
1
+ {"version":3,"file":"logicsig.mjs","names":["args: Uint8Array[]","signedTxns: Uint8Array[]","stxn: SignedTransaction"],"sources":["../../../../packages/transact/src/logicsig.ts"],"sourcesContent":["import { Address, concatArrays, decodeMsgpack, hash, isValidAddress } from '@algorandfoundation/algokit-common'\nimport { MultisigAccount } from './multisig'\nimport { TransactionSigner } from './signer'\nimport { LogicSignature, MultisigSignature, SignedTransaction, encodeSignedTransaction } from './transactions/signed-transaction'\nimport { logicSignatureCodec } from './transactions/signed-transaction-meta'\nimport { Transaction } from './transactions/transaction'\n\n// base64regex is the regex to test for base64 strings\nconst base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/\n\n/** sanityCheckProgram performs heuristic program validation:\n * check if passed in bytes are Algorand address or is B64 encoded, rather than Teal bytes\n *\n * @param program - Program bytes to check\n */\nexport function sanityCheckProgram(program: Uint8Array) {\n if (!program || program.length === 0) throw new Error('empty program')\n\n const lineBreakOrd = '\\n'.charCodeAt(0)\n const blankSpaceOrd = ' '.charCodeAt(0)\n const tildeOrd = '~'.charCodeAt(0)\n\n const isPrintable = (x: number) => blankSpaceOrd <= x && x <= tildeOrd\n const isAsciiPrintable = program.every((x: number) => x === lineBreakOrd || isPrintable(x))\n\n if (isAsciiPrintable) {\n const programStr = new TextDecoder().decode(program)\n\n if (isValidAddress(programStr)) throw new Error('requesting program bytes, get Algorand address')\n\n if (base64regex.test(programStr)) throw new Error('program should not be b64 encoded')\n\n throw new Error('program bytes are all ASCII printable characters, not looking like Teal byte code')\n }\n}\n\nconst PROGRAM_TAG = new TextEncoder().encode('Program')\nconst MSIG_PROGRAM_TAG = new TextEncoder().encode('MsigProgram')\nconst SIGN_PROGRAM_DATA_PREFIX = new TextEncoder().encode('ProgData')\n\n/** Function for signing logic signatures for delegation */\nexport type DelegatedLsigSigner = (lsig: LogicSigAccount, msig?: MultisigAccount) => Promise<Uint8Array>\n\n/** Function for signing program data for a logic signature */\nexport type ProgramDataSigner = (data: Uint8Array, lsig: LogicSigAccount) => Promise<Uint8Array>\n\nexport class LogicSigAccount {\n logic: Uint8Array\n args: Uint8Array[]\n sig?: Uint8Array\n msig?: MultisigSignature\n lmsig?: MultisigSignature\n\n constructor(program: Uint8Array, programArgs?: Array<Uint8Array> | null) {\n if (programArgs && (!Array.isArray(programArgs) || !programArgs.every((arg) => arg.constructor === Uint8Array))) {\n throw new TypeError('Invalid arguments')\n }\n\n let args: Uint8Array[] = []\n if (programArgs != null) args = programArgs.map((arg) => new Uint8Array(arg))\n\n sanityCheckProgram(program)\n\n this.logic = program\n this.args = args\n }\n\n get signer(): TransactionSigner {\n return async (txns: Transaction[], indexes: number[]) => {\n const signedTxns: Uint8Array[] = []\n for (const index of indexes) {\n const txn = txns[index]\n\n const stxn: SignedTransaction = {\n txn,\n lsig: { logic: this.logic, args: this.args, msig: this.msig, lmsig: this.lmsig, sig: this.sig },\n }\n\n signedTxns.push(encodeSignedTransaction(stxn))\n }\n\n return signedTxns\n }\n }\n\n get addr(): Address {\n return this.address()\n }\n\n /**\n * Compute hash of the logic sig program (that is the same as escrow account address) as string address\n * @returns String representation of the address\n */\n address(): Address {\n const toBeSigned = concatArrays(PROGRAM_TAG, this.logic)\n const h = hash(toBeSigned)\n return new Address(h)\n }\n\n async delegate(signer: DelegatedLsigSigner) {\n this.sig = await signer(this)\n }\n\n async delegateMultisig(msig: MultisigAccount) {\n if (this.lmsig == undefined) {\n this.lmsig = {\n subsigs: [],\n version: msig.params.version,\n threshold: msig.params.threshold,\n }\n }\n for (const addrWithSigner of msig.subSigners) {\n const { lsigSigner, addr } = addrWithSigner\n const signature = await lsigSigner(this, msig)\n\n this.lmsig.subsigs.push({ publicKey: addr.publicKey, sig: signature })\n }\n }\n\n bytesToSignForDelegation(msig?: MultisigAccount): Uint8Array {\n if (msig) {\n return concatArrays(MSIG_PROGRAM_TAG, msig.addr.publicKey, this.logic)\n } else {\n return concatArrays(PROGRAM_TAG, this.logic)\n }\n }\n\n signProgramData(data: Uint8Array, signer: ProgramDataSigner): Promise<Uint8Array> {\n return signer(data, this)\n }\n\n programDataToSign(data: Uint8Array): Uint8Array {\n return concatArrays(SIGN_PROGRAM_DATA_PREFIX, this.address().publicKey, data)\n }\n}\n\n/**\n * Decodes MsgPack bytes into a logic signature.\n *\n * @param encodedLogicSignature - The MsgPack encoded logic signature\n * @returns The decoded LogicSignature or an error if decoding fails.\n */\nexport function decodeLogicSignature(encodedLogicSignature: Uint8Array): LogicSignature {\n const decodedData = decodeMsgpack(encodedLogicSignature)\n return logicSignatureCodec.decode(decodedData, 'msgpack')\n}\n"],"mappings":";;;;;;;;AAQA,MAAM,cAAc;;;;;;AAOpB,SAAgB,mBAAmB,SAAqB;AACtD,KAAI,CAAC,WAAW,QAAQ,WAAW,EAAG,OAAM,IAAI,MAAM,gBAAgB;CAEtE,MAAM,eAAe,KAAK,WAAW,EAAE;CACvC,MAAM,gBAAgB,IAAI,WAAW,EAAE;CACvC,MAAM,WAAW,IAAI,WAAW,EAAE;CAElC,MAAM,eAAe,MAAc,iBAAiB,KAAK,KAAK;AAG9D,KAFyB,QAAQ,OAAO,MAAc,MAAM,gBAAgB,YAAY,EAAE,CAAC,EAErE;EACpB,MAAM,aAAa,IAAI,aAAa,CAAC,OAAO,QAAQ;AAEpD,MAAI,eAAe,WAAW,CAAE,OAAM,IAAI,MAAM,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