@algorandfoundation/algokit-utils 10.0.0-alpha.1 → 10.0.0-alpha.10

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 (316) hide show
  1. package/README.md +27 -5
  2. package/_virtual/rolldown_runtime.js +20 -11
  3. package/_virtual/rolldown_runtime.mjs +10 -5
  4. package/algo25/index.d.ts +2 -0
  5. package/algo25/index.js +9 -0
  6. package/algo25/index.mjs +3 -0
  7. package/algod-client/index.d.ts +2 -5
  8. package/package.json +1 -1
  9. package/packages/abi/src/abi-method.d.ts +1 -1
  10. package/packages/abi/src/abi-method.js.map +1 -1
  11. package/packages/abi/src/abi-method.mjs.map +1 -1
  12. package/packages/abi/src/abi-type.d.ts +1 -1
  13. package/packages/abi/src/abi-type.js +2 -1
  14. package/packages/abi/src/abi-type.js.map +1 -1
  15. package/packages/abi/src/abi-type.mjs +2 -1
  16. package/packages/abi/src/abi-type.mjs.map +1 -1
  17. package/packages/abi/src/arc56-contract.js +1 -0
  18. package/packages/abi/src/arc56-contract.js.map +1 -1
  19. package/packages/abi/src/arc56-contract.mjs +1 -0
  20. package/packages/abi/src/arc56-contract.mjs.map +1 -1
  21. package/packages/algo25/src/english.js.map +1 -1
  22. package/packages/algo25/src/english.mjs.map +1 -1
  23. package/packages/algo25/src/index.d.ts +40 -0
  24. package/packages/algo25/src/index.js +44 -0
  25. package/packages/algo25/src/index.js.map +1 -1
  26. package/packages/algo25/src/index.mjs +39 -1
  27. package/packages/algo25/src/index.mjs.map +1 -1
  28. package/packages/algod_client/src/apis/api-service.d.ts +44 -44
  29. package/packages/algod_client/src/apis/api-service.js +153 -153
  30. package/packages/algod_client/src/apis/api-service.js.map +1 -1
  31. package/packages/algod_client/src/apis/api-service.mjs +153 -153
  32. package/packages/algod_client/src/apis/api-service.mjs.map +1 -1
  33. package/packages/algod_client/src/core/api-error.js +3 -1
  34. package/packages/algod_client/src/core/api-error.js.map +1 -1
  35. package/packages/algod_client/src/core/api-error.mjs +3 -1
  36. package/packages/algod_client/src/core/api-error.mjs.map +1 -1
  37. package/packages/algod_client/src/core/model-runtime.js +4 -4
  38. package/packages/algod_client/src/core/model-runtime.js.map +1 -1
  39. package/packages/algod_client/src/core/model-runtime.mjs +6 -6
  40. package/packages/algod_client/src/core/model-runtime.mjs.map +1 -1
  41. package/packages/algod_client/src/models/account-participation.js +4 -4
  42. package/packages/algod_client/src/models/account-participation.js.map +1 -1
  43. package/packages/algod_client/src/models/account-participation.mjs +4 -4
  44. package/packages/algod_client/src/models/account-participation.mjs.map +1 -1
  45. package/packages/algod_client/src/models/application-state-schema.d.ts +2 -2
  46. package/packages/algod_client/src/models/application-state-schema.js +2 -2
  47. package/packages/algod_client/src/models/application-state-schema.js.map +1 -1
  48. package/packages/algod_client/src/models/application-state-schema.mjs +2 -2
  49. package/packages/algod_client/src/models/application-state-schema.mjs.map +1 -1
  50. package/packages/algod_client/src/models/asset-params.js +2 -1
  51. package/packages/algod_client/src/models/asset-params.js.map +1 -1
  52. package/packages/algod_client/src/models/asset-params.mjs +2 -1
  53. package/packages/algod_client/src/models/asset-params.mjs.map +1 -1
  54. package/packages/algod_client/src/models/block-response.js +1 -2
  55. package/packages/algod_client/src/models/block-response.js.map +1 -1
  56. package/packages/algod_client/src/models/block-response.mjs +2 -3
  57. package/packages/algod_client/src/models/block-response.mjs.map +1 -1
  58. package/packages/algod_client/src/models/block.d.ts +49 -29
  59. package/packages/algod_client/src/models/block.js +174 -98
  60. package/packages/algod_client/src/models/block.js.map +1 -1
  61. package/packages/algod_client/src/models/block.mjs +174 -98
  62. package/packages/algod_client/src/models/block.mjs.map +1 -1
  63. package/packages/algod_client/src/models/ledger-state-delta.js +1 -1
  64. package/packages/algod_client/src/models/ledger-state-delta.js.map +1 -1
  65. package/packages/algod_client/src/models/ledger-state-delta.mjs +2 -2
  66. package/packages/algod_client/src/models/ledger-state-delta.mjs.map +1 -1
  67. package/packages/algod_client/src/models/simulate-unnamed-resources-accessed.d.ts +3 -5
  68. package/packages/algod_client/src/models/simulate-unnamed-resources-accessed.js +4 -6
  69. package/packages/algod_client/src/models/simulate-unnamed-resources-accessed.js.map +1 -1
  70. package/packages/algod_client/src/models/simulate-unnamed-resources-accessed.mjs +3 -5
  71. package/packages/algod_client/src/models/simulate-unnamed-resources-accessed.mjs.map +1 -1
  72. package/packages/algod_client/src/models/transaction-parameters-response.js +2 -2
  73. package/packages/algod_client/src/models/transaction-parameters-response.js.map +1 -1
  74. package/packages/algod_client/src/models/transaction-parameters-response.mjs +2 -2
  75. package/packages/algod_client/src/models/transaction-parameters-response.mjs.map +1 -1
  76. package/packages/common/src/address.js +0 -14
  77. package/packages/common/src/address.js.map +1 -1
  78. package/packages/common/src/address.mjs +1 -14
  79. package/packages/common/src/address.mjs.map +1 -1
  80. package/packages/common/src/codecs/composite/map.js +7 -4
  81. package/packages/common/src/codecs/composite/map.js.map +1 -1
  82. package/packages/common/src/codecs/composite/map.mjs +7 -4
  83. package/packages/common/src/codecs/composite/map.mjs.map +1 -1
  84. package/packages/common/src/codecs/composite/record.js +0 -1
  85. package/packages/common/src/codecs/composite/record.js.map +1 -1
  86. package/packages/common/src/codecs/primitives/address.js +0 -1
  87. package/packages/common/src/codecs/primitives/address.js.map +1 -1
  88. package/packages/common/src/codecs/primitives/bytes.js +0 -1
  89. package/packages/common/src/codecs/primitives/bytes.js.map +1 -1
  90. package/packages/common/src/codecs/primitives/fixed-bytes.js +0 -1
  91. package/packages/common/src/codecs/primitives/fixed-bytes.js.map +1 -1
  92. package/packages/common/src/codecs/wire.js +0 -1
  93. package/packages/common/src/codecs/wire.js.map +1 -1
  94. package/packages/common/src/json.mjs +2 -2
  95. package/packages/common/src/json.mjs.map +1 -1
  96. package/packages/common/src/msgpack.js +0 -1
  97. package/packages/common/src/msgpack.js.map +1 -1
  98. package/packages/indexer_client/src/apis/api-service.d.ts +1 -1
  99. package/packages/indexer_client/src/apis/api-service.js +12 -12
  100. package/packages/indexer_client/src/apis/api-service.js.map +1 -1
  101. package/packages/indexer_client/src/apis/api-service.mjs +12 -12
  102. package/packages/indexer_client/src/apis/api-service.mjs.map +1 -1
  103. package/packages/indexer_client/src/core/api-error.js +3 -1
  104. package/packages/indexer_client/src/core/api-error.js.map +1 -1
  105. package/packages/indexer_client/src/core/api-error.mjs +3 -1
  106. package/packages/indexer_client/src/core/api-error.mjs.map +1 -1
  107. package/packages/indexer_client/src/core/model-runtime.js +4 -4
  108. package/packages/indexer_client/src/core/model-runtime.js.map +1 -1
  109. package/packages/indexer_client/src/core/model-runtime.mjs +6 -6
  110. package/packages/indexer_client/src/core/model-runtime.mjs.map +1 -1
  111. package/packages/indexer_client/src/models/account-participation.js +4 -4
  112. package/packages/indexer_client/src/models/account-participation.js.map +1 -1
  113. package/packages/indexer_client/src/models/account-participation.mjs +4 -4
  114. package/packages/indexer_client/src/models/account-participation.mjs.map +1 -1
  115. package/packages/indexer_client/src/models/application-state-schema.d.ts +2 -2
  116. package/packages/indexer_client/src/models/application-state-schema.js +2 -2
  117. package/packages/indexer_client/src/models/application-state-schema.js.map +1 -1
  118. package/packages/indexer_client/src/models/application-state-schema.mjs +2 -2
  119. package/packages/indexer_client/src/models/application-state-schema.mjs.map +1 -1
  120. package/packages/indexer_client/src/models/asset-params.js +2 -1
  121. package/packages/indexer_client/src/models/asset-params.js.map +1 -1
  122. package/packages/indexer_client/src/models/asset-params.mjs +2 -1
  123. package/packages/indexer_client/src/models/asset-params.mjs.map +1 -1
  124. package/packages/indexer_client/src/models/block.js +8 -8
  125. package/packages/indexer_client/src/models/block.js.map +1 -1
  126. package/packages/indexer_client/src/models/block.mjs +8 -8
  127. package/packages/indexer_client/src/models/block.mjs.map +1 -1
  128. package/packages/indexer_client/src/models/eval-delta-key-value.d.ts +1 -1
  129. package/packages/indexer_client/src/models/eval-delta-key-value.js +2 -2
  130. package/packages/indexer_client/src/models/eval-delta-key-value.js.map +1 -1
  131. package/packages/indexer_client/src/models/eval-delta-key-value.mjs +2 -2
  132. package/packages/indexer_client/src/models/eval-delta-key-value.mjs.map +1 -1
  133. package/packages/indexer_client/src/models/eval-delta.d.ts +1 -1
  134. package/packages/indexer_client/src/models/eval-delta.js +2 -2
  135. package/packages/indexer_client/src/models/eval-delta.js.map +1 -1
  136. package/packages/indexer_client/src/models/eval-delta.mjs +2 -2
  137. package/packages/indexer_client/src/models/eval-delta.mjs.map +1 -1
  138. package/packages/indexer_client/src/models/hb-proof-fields.js +6 -6
  139. package/packages/indexer_client/src/models/hb-proof-fields.js.map +1 -1
  140. package/packages/indexer_client/src/models/hb-proof-fields.mjs +6 -6
  141. package/packages/indexer_client/src/models/hb-proof-fields.mjs.map +1 -1
  142. package/packages/indexer_client/src/models/state-proof-verifier.js +2 -2
  143. package/packages/indexer_client/src/models/state-proof-verifier.js.map +1 -1
  144. package/packages/indexer_client/src/models/state-proof-verifier.mjs +2 -2
  145. package/packages/indexer_client/src/models/state-proof-verifier.mjs.map +1 -1
  146. package/packages/indexer_client/src/models/state-schema.d.ts +2 -2
  147. package/packages/indexer_client/src/models/state-schema.js +2 -2
  148. package/packages/indexer_client/src/models/state-schema.js.map +1 -1
  149. package/packages/indexer_client/src/models/state-schema.mjs +2 -2
  150. package/packages/indexer_client/src/models/state-schema.mjs.map +1 -1
  151. package/packages/indexer_client/src/models/teal-key-value.d.ts +1 -1
  152. package/packages/indexer_client/src/models/teal-key-value.js +2 -2
  153. package/packages/indexer_client/src/models/teal-key-value.js.map +1 -1
  154. package/packages/indexer_client/src/models/teal-key-value.mjs +2 -2
  155. package/packages/indexer_client/src/models/teal-key-value.mjs.map +1 -1
  156. package/packages/indexer_client/src/models/transaction-heartbeat.js +2 -1
  157. package/packages/indexer_client/src/models/transaction-heartbeat.js.map +1 -1
  158. package/packages/indexer_client/src/models/transaction-heartbeat.mjs +2 -1
  159. package/packages/indexer_client/src/models/transaction-heartbeat.mjs.map +1 -1
  160. package/packages/indexer_client/src/models/transaction-keyreg.js +4 -4
  161. package/packages/indexer_client/src/models/transaction-keyreg.js.map +1 -1
  162. package/packages/indexer_client/src/models/transaction-keyreg.mjs +4 -4
  163. package/packages/indexer_client/src/models/transaction-keyreg.mjs.map +1 -1
  164. package/packages/indexer_client/src/models/transaction-signature-logicsig.js +2 -1
  165. package/packages/indexer_client/src/models/transaction-signature-logicsig.js.map +1 -1
  166. package/packages/indexer_client/src/models/transaction-signature-logicsig.mjs +2 -1
  167. package/packages/indexer_client/src/models/transaction-signature-logicsig.mjs.map +1 -1
  168. package/packages/indexer_client/src/models/transaction-signature-multisig-subsignature.js +3 -3
  169. package/packages/indexer_client/src/models/transaction-signature-multisig-subsignature.js.map +1 -1
  170. package/packages/indexer_client/src/models/transaction-signature-multisig-subsignature.mjs +3 -3
  171. package/packages/indexer_client/src/models/transaction-signature-multisig-subsignature.mjs.map +1 -1
  172. package/packages/indexer_client/src/models/transaction.d.ts +2 -2
  173. package/packages/indexer_client/src/models/transaction.js +6 -5
  174. package/packages/indexer_client/src/models/transaction.js.map +1 -1
  175. package/packages/indexer_client/src/models/transaction.mjs +6 -5
  176. package/packages/indexer_client/src/models/transaction.mjs.map +1 -1
  177. package/packages/kmd_client/src/apis/api-service.d.ts +5 -5
  178. package/packages/kmd_client/src/apis/api-service.js +32 -32
  179. package/packages/kmd_client/src/apis/api-service.js.map +1 -1
  180. package/packages/kmd_client/src/apis/api-service.mjs +32 -32
  181. package/packages/kmd_client/src/apis/api-service.mjs.map +1 -1
  182. package/packages/kmd_client/src/core/api-error.js +3 -1
  183. package/packages/kmd_client/src/core/api-error.js.map +1 -1
  184. package/packages/kmd_client/src/core/api-error.mjs +3 -1
  185. package/packages/kmd_client/src/core/api-error.mjs.map +1 -1
  186. package/packages/kmd_client/src/core/model-runtime.js +4 -4
  187. package/packages/kmd_client/src/core/model-runtime.js.map +1 -1
  188. package/packages/kmd_client/src/core/model-runtime.mjs +6 -6
  189. package/packages/kmd_client/src/core/model-runtime.mjs.map +1 -1
  190. package/packages/sdk/src/encoding/encoding.js +12 -2
  191. package/packages/sdk/src/encoding/encoding.js.map +1 -1
  192. package/packages/sdk/src/encoding/encoding.mjs +12 -1
  193. package/packages/sdk/src/encoding/encoding.mjs.map +1 -1
  194. package/packages/sdk/src/encoding/schema/map.js +0 -2
  195. package/packages/sdk/src/encoding/schema/map.js.map +1 -1
  196. package/packages/sdk/src/utils/utils.mjs +2 -2
  197. package/packages/sdk/src/utils/utils.mjs.map +1 -1
  198. package/packages/transact/src/logicsig.d.ts +3 -15
  199. package/packages/transact/src/logicsig.js +16 -36
  200. package/packages/transact/src/logicsig.js.map +1 -1
  201. package/packages/transact/src/logicsig.mjs +18 -36
  202. package/packages/transact/src/logicsig.mjs.map +1 -1
  203. package/packages/transact/src/multisig.d.ts +7 -115
  204. package/packages/transact/src/multisig.js +86 -136
  205. package/packages/transact/src/multisig.js.map +1 -1
  206. package/packages/transact/src/multisig.mjs +87 -117
  207. package/packages/transact/src/multisig.mjs.map +1 -1
  208. package/packages/transact/src/transactions/app-call.d.ts +10 -10
  209. package/packages/transact/src/transactions/app-call.js.map +1 -1
  210. package/packages/transact/src/transactions/app-call.mjs.map +1 -1
  211. package/packages/transact/src/transactions/reference-types-meta.d.ts +26 -0
  212. package/packages/transact/src/transactions/reference-types-meta.js +71 -0
  213. package/packages/transact/src/transactions/reference-types-meta.js.map +1 -0
  214. package/packages/transact/src/transactions/reference-types-meta.mjs +69 -0
  215. package/packages/transact/src/transactions/reference-types-meta.mjs.map +1 -0
  216. package/packages/transact/src/transactions/signed-transaction-meta.js +2 -3
  217. package/packages/transact/src/transactions/signed-transaction-meta.js.map +1 -1
  218. package/packages/transact/src/transactions/signed-transaction-meta.mjs +3 -3
  219. package/packages/transact/src/transactions/signed-transaction-meta.mjs.map +1 -1
  220. package/packages/transact/src/transactions/signed-transaction.d.ts +2 -2
  221. package/packages/transact/src/transactions/signed-transaction.js.map +1 -1
  222. package/packages/transact/src/transactions/signed-transaction.mjs.map +1 -1
  223. package/packages/transact/src/transactions/transaction-meta.js +29 -23
  224. package/packages/transact/src/transactions/transaction-meta.js.map +1 -1
  225. package/packages/transact/src/transactions/transaction-meta.mjs +29 -23
  226. package/packages/transact/src/transactions/transaction-meta.mjs.map +1 -1
  227. package/packages/transact/src/transactions/transaction.js +1 -0
  228. package/packages/transact/src/transactions/transaction.js.map +1 -1
  229. package/packages/transact/src/transactions/transaction.mjs +1 -0
  230. package/packages/transact/src/transactions/transaction.mjs.map +1 -1
  231. package/sdk/index.js +1 -1
  232. package/testing/fixtures/algorand-fixture.d.ts +3 -3
  233. package/testing/fixtures/algorand-fixture.js.map +1 -1
  234. package/testing/fixtures/algorand-fixture.mjs.map +1 -1
  235. package/transact/index.d.ts +5 -4
  236. package/transact/index.js +4 -22
  237. package/transact/index.mjs +4 -3
  238. package/transaction/transaction.js +2 -2
  239. package/transaction/transaction.js.map +1 -1
  240. package/transaction/transaction.mjs +2 -2
  241. package/transaction/transaction.mjs.map +1 -1
  242. package/transactions/app-call.d.ts +4 -3
  243. package/transactions/app-call.js +15 -15
  244. package/transactions/app-call.js.map +1 -1
  245. package/transactions/app-call.mjs +15 -15
  246. package/transactions/app-call.mjs.map +1 -1
  247. package/transactions/asset-config.d.ts +1 -1
  248. package/transactions/asset-config.js +1 -1
  249. package/transactions/asset-config.js.map +1 -1
  250. package/transactions/asset-config.mjs +1 -1
  251. package/transactions/asset-config.mjs.map +1 -1
  252. package/transactions/method-call.js +12 -12
  253. package/transactions/method-call.js.map +1 -1
  254. package/transactions/method-call.mjs +12 -12
  255. package/transactions/method-call.mjs.map +1 -1
  256. package/types/account-manager.js +2 -2
  257. package/types/account-manager.js.map +1 -1
  258. package/types/account-manager.mjs +2 -2
  259. package/types/account-manager.mjs.map +1 -1
  260. package/types/algorand-client-transaction-creator.d.ts +97 -83
  261. package/types/algorand-client-transaction-creator.js +8 -0
  262. package/types/algorand-client-transaction-creator.js.map +1 -1
  263. package/types/algorand-client-transaction-creator.mjs +8 -0
  264. package/types/algorand-client-transaction-creator.mjs.map +1 -1
  265. package/types/algorand-client-transaction-sender.d.ts +100 -86
  266. package/types/algorand-client-transaction-sender.js +8 -1
  267. package/types/algorand-client-transaction-sender.js.map +1 -1
  268. package/types/algorand-client-transaction-sender.mjs +8 -0
  269. package/types/algorand-client-transaction-sender.mjs.map +1 -1
  270. package/types/app-client.d.ts +265 -260
  271. package/types/app-client.js +0 -1
  272. package/types/app-client.js.map +1 -1
  273. package/types/app-deployer.js +1 -1
  274. package/types/app-deployer.js.map +1 -1
  275. package/types/app-deployer.mjs +1 -1
  276. package/types/app-deployer.mjs.map +1 -1
  277. package/types/app-factory.d.ts +91 -88
  278. package/types/app-manager.d.ts +3 -1
  279. package/types/app-manager.js +16 -10
  280. package/types/app-manager.js.map +1 -1
  281. package/types/app-manager.mjs +16 -10
  282. package/types/app-manager.mjs.map +1 -1
  283. package/types/app-spec.js +12 -5
  284. package/types/app-spec.js.map +1 -1
  285. package/types/app-spec.mjs +12 -5
  286. package/types/app-spec.mjs.map +1 -1
  287. package/types/asset-manager.js +1 -1
  288. package/types/asset-manager.js.map +1 -1
  289. package/types/asset-manager.mjs +1 -1
  290. package/types/asset-manager.mjs.map +1 -1
  291. package/types/composer.d.ts +9 -0
  292. package/types/composer.js +28 -13
  293. package/types/composer.js.map +1 -1
  294. package/types/composer.mjs +28 -12
  295. package/types/composer.mjs.map +1 -1
  296. package/types/kmd-account-manager.d.ts +1 -0
  297. package/types/kmd-account-manager.js +21 -10
  298. package/types/kmd-account-manager.js.map +1 -1
  299. package/types/kmd-account-manager.mjs +22 -11
  300. package/types/kmd-account-manager.mjs.map +1 -1
  301. package/types/testing.d.ts +2 -2
  302. package/packages/algod_client/src/models/application-local-reference.d.ts +0 -20
  303. package/packages/algod_client/src/models/application-local-reference.js +0 -23
  304. package/packages/algod_client/src/models/application-local-reference.js.map +0 -1
  305. package/packages/algod_client/src/models/application-local-reference.mjs +0 -23
  306. package/packages/algod_client/src/models/application-local-reference.mjs.map +0 -1
  307. package/packages/algod_client/src/models/asset-holding-reference.d.ts +0 -20
  308. package/packages/algod_client/src/models/asset-holding-reference.js +0 -23
  309. package/packages/algod_client/src/models/asset-holding-reference.js.map +0 -1
  310. package/packages/algod_client/src/models/asset-holding-reference.mjs +0 -23
  311. package/packages/algod_client/src/models/asset-holding-reference.mjs.map +0 -1
  312. package/packages/algod_client/src/models/box-reference.d.ts +0 -17
  313. package/packages/algod_client/src/models/box-reference.js +0 -23
  314. package/packages/algod_client/src/models/box-reference.js.map +0 -1
  315. package/packages/algod_client/src/models/box-reference.mjs +0 -23
  316. package/packages/algod_client/src/models/box-reference.mjs.map +0 -1
@@ -1,22 +1,22 @@
1
1
  import { ObjectModelCodec } from "../../../common/src/codecs/models/object-model.mjs";
2
2
  import { stringifyJson } from "../../../common/src/json.mjs";
3
- import { decodeMsgpack, encodeMsgpack } from "../../../common/src/msgpack.mjs";
3
+ import { decodeMsgpack as decodeMsgpack$1, encodeMsgpack as encodeMsgpack$1 } from "../../../common/src/msgpack.mjs";
4
4
 
5
5
  //#region packages/kmd_client/src/core/model-runtime.ts
6
6
  function encodeJson(value, meta) {
7
7
  return stringifyJson(new ObjectModelCodec(meta).encode(value, "json"));
8
8
  }
9
- function encodeMsgpack$1(value, meta) {
10
- return encodeMsgpack(new ObjectModelCodec(meta).encode(value, "msgpack"));
9
+ function encodeMsgpack(value, meta) {
10
+ return encodeMsgpack$1(new ObjectModelCodec(meta).encode(value, "msgpack"));
11
11
  }
12
12
  function decodeJson(value, meta) {
13
13
  return new ObjectModelCodec(meta).decode(value, "json");
14
14
  }
15
- function decodeMsgpack$1(value, meta) {
16
- const wire = decodeMsgpack(value);
15
+ function decodeMsgpack(value, meta) {
16
+ const wire = decodeMsgpack$1(value);
17
17
  return new ObjectModelCodec(meta).decode(wire, "msgpack");
18
18
  }
19
19
 
20
20
  //#endregion
21
- export { decodeJson, decodeMsgpack$1 as decodeMsgpack, encodeJson, encodeMsgpack$1 as encodeMsgpack };
21
+ export { decodeJson, decodeMsgpack, encodeJson, encodeMsgpack };
22
22
  //# sourceMappingURL=model-runtime.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"model-runtime.mjs","names":["encodeMsgpack","rawEncodeMsgpack","decodeMsgpack","rawDecodeMsgpack"],"sources":["../../../../../packages/kmd_client/src/core/model-runtime.ts"],"sourcesContent":["import {\n ObjectModelCodec,\n decodeMsgpack as rawDecodeMsgpack,\n encodeMsgpack as rawEncodeMsgpack,\n stringifyJson,\n type ObjectModelMetadata,\n} from '@algorandfoundation/algokit-common'\n\nexport function encodeJson<T extends Record<string, unknown>>(value: T, meta: ObjectModelMetadata<T>): string {\n const wire = new ObjectModelCodec(meta).encode(value, 'json')\n return stringifyJson(wire)\n}\nexport function encodeMsgpack<T extends Record<string, unknown>>(value: T, meta: ObjectModelMetadata<T>): Uint8Array {\n const wire = new ObjectModelCodec(meta).encode(value, 'msgpack')\n return rawEncodeMsgpack(wire)\n}\nexport function decodeJson<T extends Record<string, unknown>>(value: Record<string, unknown>, meta: ObjectModelMetadata<T>): T {\n return new ObjectModelCodec<T>(meta).decode(value, 'json')\n}\nexport function decodeMsgpack<T extends Record<string, unknown>>(value: Uint8Array, meta: ObjectModelMetadata<T>): T {\n const wire = rawDecodeMsgpack(value)\n return new ObjectModelCodec<T>(meta).decode(wire, 'msgpack')\n}\n"],"mappings":";;;;;AAQA,SAAgB,WAA8C,OAAU,MAAsC;AAE5G,QAAO,cADM,IAAI,iBAAiB,KAAK,CAAC,OAAO,OAAO,OAAO,CACnC;;AAE5B,SAAgBA,gBAAiD,OAAU,MAA0C;AAEnH,QAAOC,cADM,IAAI,iBAAiB,KAAK,CAAC,OAAO,OAAO,UAAU,CACnC;;AAE/B,SAAgB,WAA8C,OAAgC,MAAiC;AAC7H,QAAO,IAAI,iBAAoB,KAAK,CAAC,OAAO,OAAO,OAAO;;AAE5D,SAAgBC,gBAAiD,OAAmB,MAAiC;CACnH,MAAM,OAAOC,cAAiB,MAAM;AACpC,QAAO,IAAI,iBAAoB,KAAK,CAAC,OAAO,MAAM,UAAU"}
1
+ {"version":3,"file":"model-runtime.mjs","names":["rawEncodeMsgpack","rawDecodeMsgpack"],"sources":["../../../../../packages/kmd_client/src/core/model-runtime.ts"],"sourcesContent":["import {\n ObjectModelCodec,\n decodeMsgpack as rawDecodeMsgpack,\n encodeMsgpack as rawEncodeMsgpack,\n stringifyJson,\n type ObjectModelMetadata,\n} from '@algorandfoundation/algokit-common'\n\nexport function encodeJson<T extends Record<string, unknown>>(value: T, meta: ObjectModelMetadata<T>): string {\n const wire = new ObjectModelCodec(meta).encode(value, 'json')\n return stringifyJson(wire)\n}\nexport function encodeMsgpack<T extends Record<string, unknown>>(value: T, meta: ObjectModelMetadata<T>): Uint8Array {\n const wire = new ObjectModelCodec(meta).encode(value, 'msgpack')\n return rawEncodeMsgpack(wire)\n}\nexport function decodeJson<T extends Record<string, unknown>>(value: Record<string, unknown>, meta: ObjectModelMetadata<T>): T {\n return new ObjectModelCodec<T>(meta).decode(value, 'json')\n}\nexport function decodeMsgpack<T extends Record<string, unknown>>(value: Uint8Array, meta: ObjectModelMetadata<T>): T {\n const wire = rawDecodeMsgpack(value)\n return new ObjectModelCodec<T>(meta).decode(wire, 'msgpack')\n}\n"],"mappings":";;;;;AAQA,SAAgB,WAA8C,OAAU,MAAsC;AAE5G,QAAO,cADM,IAAI,iBAAiB,KAAK,CAAC,OAAO,OAAO,OAAO,CACnC;;AAE5B,SAAgB,cAAiD,OAAU,MAA0C;AAEnH,QAAOA,gBADM,IAAI,iBAAiB,KAAK,CAAC,OAAO,OAAO,UAAU,CACnC;;AAE/B,SAAgB,WAA8C,OAAgC,MAAiC;AAC7H,QAAO,IAAI,iBAAoB,KAAK,CAAC,OAAO,OAAO,OAAO;;AAE5D,SAAgB,cAAiD,OAAmB,MAAiC;CACnH,MAAM,OAAOC,gBAAiB,MAAM;AACpC,QAAO,IAAI,iBAAoB,KAAK,CAAC,OAAO,MAAM,UAAU"}
@@ -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,KA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AAoB,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,aAAa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BAAY,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 ?? {}\n const pr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AAO,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;;;;;;;;AA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AE,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,uBAAuBA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O,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,MAAM,s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AK,MAAM,SAAS,KAAK,YAAY,CACnC,KAAI,QAAQ,IAAI,MAAM,IAAI,CACxB,KAAI,IAAI,MAAM,KAAK,MAAM,YAAY,oBAAoB,QAAQ,IAAI,MAAM,IAAI,EAAE,kBAAkB,aAAa,MAAM,IAAI,CAAC,CAAC;WACnH,MAAM,UACf,KAAI,IAAI,MAAM,KAAK,MAAM,YAAY,cAAc,CAAC;MAEpD,OAAM,IAAI,MAAM,gBAAgB,MAAM,MAAM;AAGhD,SAAO;;CAGT,AAAO,YAAY,MAAe,SAA+C;AAC/E,MAAI,EAAE,gBAAgB,KACpB,OAAM,IAAI,MAAM,oCAAoC;EAEtD,MAAMC,MAA2C,EAAE;AACnD,OAAK,MAAM,SAAS,KAAK,YAAY,EAAE;GACrC,MAAM,QAAQ,KAAK,IAAI,MAAM,IAAI;AACjC,OAAI,MAAM,aAAa,MAAM,YAAY,eAAe,MAAM,CAC5D;AAEF,OAAI,MAAM,OAAO,MAAM,YAAY,YAAY,OAAO,QAAQ;;AAEhE,SAAO;;CAGT,AAAO,iBAAiB,SAAiD;AACvE,MAAI,WAAW,QAAQ,OAAO,YAAY,YAAY,MAAM,QAAQ,QAAQ,CAC1E,OAAM,IAAI,MAAM,wCAAwC;EAE1D,MAAM,sBAAM,IAAI,KAAsB;AACtC,OAAK,MAAM,SAAS,KAAK,YAAY,CACnC,KAAI,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,8 +1,8 @@
1
1
  import intDecoding_default from "../types/intDecoding.mjs";
2
- import JSONbigWithoutConfig from "json-bigint";
2
+ import JSONbigFactory from "json-bigint";
3
3
 
4
4
  //#region packages/sdk/src/utils/utils.ts
5
- const JSONbig = JSONbigWithoutConfig({
5
+ const JSONbig = JSONbigFactory({
6
6
  useNativeBigInt: true,
7
7
  strict: true
8
8
  });
@@ -1 +1 @@
1
- {"version":3,"file":"utils.mjs","names":["IntDecoding"],"sources":["../../../../../packages/sdk/src/utils/utils.ts"],"sourcesContent":["import JSONbigWithoutConfig from 'json-bigint'\nimport IntDecoding from '../types/intDecoding.js'\n\nconst JSONbig = JSONbigWithoutConfig({\n useNativeBigInt: true,\n strict: true,\n})\n\nexport interface ParseJSONOptions {\n intDecoding: IntDecoding\n}\n\n/**\n * Parse JSON with additional options.\n * @param str - The JSON string to parse.\n * @param options - Configures how integers in this JSON string will be decoded. See the\n * `IntDecoding` enum for more details.\n */\nexport function parseJSON(str: string, { intDecoding }: ParseJSONOptions) {\n if (\n intDecoding !== IntDecoding.SAFE &&\n intDecoding !== IntDecoding.UNSAFE &&\n intDecoding !== IntDecoding.BIGINT &&\n intDecoding !== IntDecoding.MIXED\n ) {\n throw new Error(`Invalid intDecoding option: ${intDecoding}`)\n }\n return JSONbig.parse(str, (_: string, value: any): any => {\n if (value != null && typeof value === 'object' && Object.getPrototypeOf(value) == null) {\n // JSONbig.parse objects are created with Object.create(null) and thus have a null prototype\n // let us remedy that\n Object.setPrototypeOf(value, Object.prototype)\n }\n\n if (typeof value === 'bigint') {\n if (intDecoding === IntDecoding.SAFE && value > Number.MAX_SAFE_INTEGER) {\n throw new Error(`Integer exceeds maximum safe integer: ${value.toString()}. Try parsing with a different intDecoding option.`)\n }\n if (intDecoding === IntDecoding.BIGINT || (intDecoding === IntDecoding.MIXED && value > Number.MAX_SAFE_INTEGER)) {\n return value\n }\n // JSONbig.parse converts number to BigInts if they are >= 10**15. This is smaller than\n // Number.MAX_SAFE_INTEGER, so we can convert some BigInts back to normal numbers.\n return Number(value)\n }\n\n if (typeof value === 'number') {\n if (intDecoding === IntDecoding.BIGINT && Number.isInteger(value)) {\n return BigInt(value)\n }\n }\n\n return value\n })\n}\n\n/**\n * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.\n *\n * This functions differs from the built-in JSON.stringify in that it supports serializing BigInts.\n *\n * This function takes the same arguments as the built-in JSON.stringify function.\n *\n * @param value - A JavaScript value, usually an object or array, to be converted.\n * @param replacer - A function that transforms the results.\n * @param space - Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.\n */\nexport function stringifyJSON(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string {\n return JSONbig.stringify(value, replacer, space)\n}\n\n/**\n * ArrayEqual takes two arrays and return true if equal, false otherwise\n */\nexport function arrayEqual<T>(a: ArrayLike<T>, b: ArrayLike<T>): boolean {\n if (a.length !== b.length) {\n return false\n }\n return Array.from(a).every((val, i) => val === b[i])\n}\n\n/**\n * ConcatArrays takes n number arrays and returns a joint Uint8Array\n * @param arrs - An arbitrary number of n array-like number list arguments\n * @returns [a,b]\n */\nexport function concatArrays(...arrs: ArrayLike<number>[]) {\n const size = arrs.reduce((sum, arr) => sum + arr.length, 0)\n const c = new Uint8Array(size)\n\n let offset = 0\n for (let i = 0; i < arrs.length; i++) {\n c.set(arrs[i], offset)\n offset += arrs[i].length\n }\n\n return c\n}\n\n/**\n * Remove undefined properties from an object\n * @param obj - An object, preferably one with some undefined properties\n * @returns A copy of the object with undefined properties removed\n */\nexport function removeUndefinedProperties(obj: Record<string | number | symbol, any>) {\n const mutableCopy = { ...obj }\n Object.keys(mutableCopy).forEach((key) => {\n if (typeof mutableCopy[key] === 'undefined') delete mutableCopy[key]\n })\n return mutableCopy\n}\n\n/**\n * Check whether the environment is Node.js (as opposed to the browser)\n * @returns True if Node.js environment, false otherwise\n */\nexport function isNode() {\n return (\n // @ts-ignore\n typeof process === 'object' &&\n // @ts-ignore\n typeof process.versions === 'object' &&\n // @ts-ignore\n typeof process.versions.node !== 'undefined'\n )\n}\n\n/**\n * Check whether the environment is ReactNative\n * @returns True if ReactNative, false otherwise\n */\nexport function isReactNative() {\n const { navigator } = globalThis as { navigator?: { product?: string } }\n if (typeof navigator === 'object' && navigator.product === 'ReactNative') {\n return true\n }\n return false\n}\n\nexport function ensureSafeInteger(value: unknown): number {\n if (typeof value === 'undefined') {\n throw new Error('Value is undefined')\n }\n if (typeof value === 'bigint') {\n if (value > BigInt(Number.MAX_SAFE_INTEGER) || value < BigInt(Number.MIN_SAFE_INTEGER)) {\n throw new Error(`BigInt value ${value} is not a safe integer`)\n }\n return Number(value)\n }\n if (typeof value === 'number') {\n if (Number.isSafeInteger(value)) {\n return value\n }\n throw new Error(`Value ${value} is not a safe integer`)\n }\n throw new Error(`Unexpected type ${typeof value}, ${value}`)\n}\n\nexport function ensureSafeUnsignedInteger(value: unknown): number {\n const intValue = ensureSafeInteger(value)\n if (intValue < 0) {\n throw new Error(`Value ${intValue} is negative`)\n }\n return intValue\n}\n\nexport function ensureBigInt(value: unknown): bigint {\n if (typeof value === 'undefined') {\n throw new Error('Value is undefined')\n }\n if (typeof value === 'bigint') {\n return value\n }\n if (typeof value === 'number') {\n if (!Number.isSafeInteger(value)) {\n throw new Error(`Value ${value} is not a safe integer`)\n }\n return BigInt(value)\n }\n throw new Error(`Unexpected type ${typeof value}, ${value}`)\n}\n\nexport function ensureUint64(value: unknown): bigint {\n const bigIntValue = ensureBigInt(value)\n if (bigIntValue < 0 || bigIntValue > BigInt('0xffffffffffffffff')) {\n throw new Error(`Value ${bigIntValue} is not a uint64`)\n }\n return bigIntValue\n}\n"],"mappings":";;;;AAGA,MAAM,UAAU,qBAAqB;CACnC,iBAAiB;CACjB,QAAQ;CACT,CAAC;;;;;;;AAYF,SAAgB,UAAU,KAAa,EAAE,eAAiC;AACxE,KACE,gBAAgBA,oBAAY,QAC5B,gBAAgBA,oBAAY,UAC5B,gBAAgBA,oBAAY,UAC5B,gBAAgBA,oBAAY,MAE5B,OAAM,IAAI,MAAM,+BAA+B,cAAc;AAE/D,QAAO,QAAQ,MAAM,MAAM,GAAW,UAAoB;AACxD,MAAI,SAAS,QAAQ,OAAO,UAA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"}
1
+ {"version":3,"file":"utils.mjs","names":["JSONbigWithoutConfig","IntDecoding"],"sources":["../../../../../packages/sdk/src/utils/utils.ts"],"sourcesContent":["import JSONbigWithoutConfig from 'json-bigint'\nimport IntDecoding from '../types/intDecoding.js'\n\nconst JSONbig = JSONbigWithoutConfig({\n useNativeBigInt: true,\n strict: true,\n})\n\nexport interface ParseJSONOptions {\n intDecoding: IntDecoding\n}\n\n/**\n * Parse JSON with additional options.\n * @param str - The JSON string to parse.\n * @param options - Configures how integers in this JSON string will be decoded. See the\n * `IntDecoding` enum for more details.\n */\nexport function parseJSON(str: string, { intDecoding }: ParseJSONOptions) {\n if (\n intDecoding !== IntDecoding.SAFE &&\n intDecoding !== IntDecoding.UNSAFE &&\n intDecoding !== IntDecoding.BIGINT &&\n intDecoding !== IntDecoding.MIXED\n ) {\n throw new Error(`Invalid intDecoding option: ${intDecoding}`)\n }\n return JSONbig.parse(str, (_: string, value: any): any => {\n if (value != null && typeof value === 'object' && Object.getPrototypeOf(value) == null) {\n // JSONbig.parse objects are created with Object.create(null) and thus have a null prototype\n // let us remedy that\n Object.setPrototypeOf(value, Object.prototype)\n }\n\n if (typeof value === 'bigint') {\n if (intDecoding === IntDecoding.SAFE && value > Number.MAX_SAFE_INTEGER) {\n throw new Error(`Integer exceeds maximum safe integer: ${value.toString()}. Try parsing with a different intDecoding option.`)\n }\n if (intDecoding === IntDecoding.BIGINT || (intDecoding === IntDecoding.MIXED && value > Number.MAX_SAFE_INTEGER)) {\n return value\n }\n // JSONbig.parse converts number to BigInts if they are >= 10**15. This is smaller than\n // Number.MAX_SAFE_INTEGER, so we can convert some BigInts back to normal numbers.\n return Number(value)\n }\n\n if (typeof value === 'number') {\n if (intDecoding === IntDecoding.BIGINT && Number.isInteger(value)) {\n return BigInt(value)\n }\n }\n\n return value\n })\n}\n\n/**\n * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.\n *\n * This functions differs from the built-in JSON.stringify in that it supports serializing BigInts.\n *\n * This function takes the same arguments as the built-in JSON.stringify function.\n *\n * @param value - A JavaScript value, usually an object or array, to be converted.\n * @param replacer - A function that transforms the results.\n * @param space - Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.\n */\nexport function stringifyJSON(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string {\n return JSONbig.stringify(value, replacer, space)\n}\n\n/**\n * ArrayEqual takes two arrays and return true if equal, false otherwise\n */\nexport function arrayEqual<T>(a: ArrayLike<T>, b: ArrayLike<T>): boolean {\n if (a.length !== b.length) {\n return false\n }\n return Array.from(a).every((val, i) => val === b[i])\n}\n\n/**\n * ConcatArrays takes n number arrays and returns a joint Uint8Array\n * @param arrs - An arbitrary number of n array-like number list arguments\n * @returns [a,b]\n */\nexport function concatArrays(...arrs: ArrayLike<number>[]) {\n const size = arrs.reduce((sum, arr) => sum + arr.length, 0)\n const c = new Uint8Array(size)\n\n let offset = 0\n for (let i = 0; i < arrs.length; i++) {\n c.set(arrs[i], offset)\n offset += arrs[i].length\n }\n\n return c\n}\n\n/**\n * Remove undefined properties from an object\n * @param obj - An object, preferably one with some undefined properties\n * @returns A copy of the object with undefined properties removed\n */\nexport function removeUndefinedProperties(obj: Record<string | number | symbol, any>) {\n const mutableCopy = { ...obj }\n Object.keys(mutableCopy).forEach((key) => {\n if (typeof mutableCopy[key] === 'undefined') delete mutableCopy[key]\n })\n return mutableCopy\n}\n\n/**\n * Check whether the environment is Node.js (as opposed to the browser)\n * @returns True if Node.js environment, false otherwise\n */\nexport function isNode() {\n return (\n // @ts-ignore\n typeof process === 'object' &&\n // @ts-ignore\n typeof process.versions === 'object' &&\n // @ts-ignore\n typeof process.versions.node !== 'undefined'\n )\n}\n\n/**\n * Check whether the environment is ReactNative\n * @returns True if ReactNative, false otherwise\n */\nexport function isReactNative() {\n const { navigator } = globalThis as { navigator?: { product?: string } }\n if (typeof navigator === 'object' && navigator.product === 'ReactNative') {\n return true\n }\n return false\n}\n\nexport function ensureSafeInteger(value: unknown): number {\n if (typeof value === 'undefined') {\n throw new Error('Value is undefined')\n }\n if (typeof value === 'bigint') {\n if (value > BigInt(Number.MAX_SAFE_INTEGER) || value < BigInt(Number.MIN_SAFE_INTEGER)) {\n throw new Error(`BigInt value ${value} is not a safe integer`)\n }\n return Number(value)\n }\n if (typeof value === 'number') {\n if (Number.isSafeInteger(value)) {\n return value\n }\n throw new Error(`Value ${value} is not a safe integer`)\n }\n throw new Error(`Unexpected type ${typeof value}, ${value}`)\n}\n\nexport function ensureSafeUnsignedInteger(value: unknown): number {\n const intValue = ensureSafeInteger(value)\n if (intValue < 0) {\n throw new Error(`Value ${intValue} is negative`)\n }\n return intValue\n}\n\nexport function ensureBigInt(value: unknown): bigint {\n if (typeof value === 'undefined') {\n throw new Error('Value is undefined')\n }\n if (typeof value === 'bigint') {\n return value\n }\n if (typeof value === 'number') {\n if (!Number.isSafeInteger(value)) {\n throw new Error(`Value ${value} is not a safe integer`)\n }\n return BigInt(value)\n }\n throw new Error(`Unexpected type ${typeof value}, ${value}`)\n}\n\nexport function ensureUint64(value: unknown): bigint {\n const bigIntValue = ensureBigInt(value)\n if (bigIntValue < 0 || bigIntValue > BigInt('0xffffffffffffffff')) {\n throw new Error(`Value ${bigIntValue} is not a uint64`)\n }\n return bigIntValue\n}\n"],"mappings":";;;;AAGA,MAAM,UAAUA,eAAqB;CACnC,iBAAiB;CACjB,QAAQ;CACT,CAAC;;;;;;;AAYF,SAAgB,UAAU,KAAa,EAAE,eAAiC;AACxE,KACE,gBAAgBC,oBAAY,QAC5B,gBAAgBA,oBAAY,UAC5B,gBAAgBA,oBAAY,UAC5B,gBAAgBA,oBAAY,MAE5B,OAAM,IAAI,MAAM,+BAA+B,cAAc;AAE/D,QAAO,QAAQ,MAAM,MAAM,GAAW,UAAoB;AACxD,MAAI,SAAS,QAAQ,OAAO,UAAU,YAAY,OAAO,eAAe,MAAM,IAAI,KAGhF,QAAO,eAAe,OAAO,OAAO,UAAU;AAGhD,MAAI,OAAO,UAAU,UAAU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src/logicsig.d.ts
7
-
8
- /** sanityCheckProgram performs heuristic program validation:
9
- * check if passed in bytes are Algorand address or is B64 encoded, rather than Teal bytes
10
- *
11
- * @param program - Program bytes to check
12
- */
13
- declare function sanityCheckProgram(program: Uint8Array): void;
14
7
  /** Function for signing logic signatures for delegation */
15
8
  type DelegatedLsigSigner = (lsig: LogicSigAccount, msig?: MultisigAccount) => Promise<Uint8Array>;
16
9
  /** Function for signing program data for a logic signature */
@@ -21,6 +14,8 @@ declare class LogicSigAccount {
21
14
  sig?: Uint8Array;
22
15
  msig?: MultisigSignature;
23
16
  lmsig?: MultisigSignature;
17
+ static fromSignature(signature: LogicSignature): LogicSigAccount;
18
+ static fromBytes(encodedLsig: Uint8Array): LogicSigAccount;
24
19
  constructor(program: Uint8Array, programArgs?: Array<Uint8Array> | null);
25
20
  get signer(): TransactionSigner;
26
21
  get addr(): Address;
@@ -35,13 +30,6 @@ declare class LogicSigAccount {
35
30
  signProgramData(data: Uint8Array, signer: ProgramDataSigner): Promise<Uint8Array>;
36
31
  programDataToSign(data: Uint8Array): Uint8Array;
37
32
  }
38
- /**
39
- * Decodes MsgPack bytes into a logic signature.
40
- *
41
- * @param encodedLogicSignature - The MsgPack encoded logic signature
42
- * @returns The decoded LogicSignature or an error if decoding fails.
43
- */
44
- declare function decodeLogicSignature(encodedLogicSignature: Uint8Array): LogicSignature;
45
33
  //#endregion
46
- export { DelegatedLsigSigner, LogicSigAccount, ProgramDataSigner, decodeLogicSignature, sanityCheckProgram };
34
+ export { DelegatedLsigSigner, LogicSigAccount, ProgramDataSigner };
47
35
  //# sourceMappingURL=logicsig.d.ts.map