quantumcoin 6.14.2 → 6.14.5

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 (317) hide show
  1. package/CHANGELOG.md +442 -442
  2. package/FUNDING.json +10 -10
  3. package/LICENSE.md +21 -21
  4. package/README.md +132 -142
  5. package/SECURITY.md +34 -34
  6. package/dist/README.md +22 -22
  7. package/dist/quantumcoin.js +1127 -1324
  8. package/dist/quantumcoin.js.map +1 -1
  9. package/dist/quantumcoin.min.js +1 -1
  10. package/dist/quantumcoin.umd.js +1128 -1327
  11. package/dist/quantumcoin.umd.js.map +1 -1
  12. package/dist/quantumcoin.umd.min.js +1 -1
  13. package/dist/wordlists-extra.js +1 -1
  14. package/dist/wordlists-extra.js.map +1 -1
  15. package/dist/wordlists-extra.min.js +1 -1
  16. package/lib.commonjs/README.md +16 -16
  17. package/lib.commonjs/_version.js +1 -1
  18. package/lib.commonjs/crypto/signature.d.ts +3 -76
  19. package/lib.commonjs/crypto/signature.d.ts.map +1 -1
  20. package/lib.commonjs/crypto/signature.js +15 -199
  21. package/lib.commonjs/crypto/signature.js.map +1 -1
  22. package/lib.commonjs/crypto/signing-key.d.ts +1 -1
  23. package/lib.commonjs/crypto/signing-key.d.ts.map +1 -1
  24. package/lib.commonjs/crypto/signing-key.js +19 -10
  25. package/lib.commonjs/crypto/signing-key.js.map +1 -1
  26. package/lib.commonjs/package.json +12 -12
  27. package/lib.commonjs/providers/provider-jsonrpc.d.ts +0 -1
  28. package/lib.commonjs/providers/provider-jsonrpc.d.ts.map +1 -1
  29. package/lib.commonjs/providers/provider-jsonrpc.js +0 -1
  30. package/lib.commonjs/providers/provider-jsonrpc.js.map +1 -1
  31. package/lib.commonjs/quantumcoin.d.ts +2 -0
  32. package/lib.commonjs/quantumcoin.d.ts.map +1 -1
  33. package/lib.commonjs/quantumcoin.js +11 -5
  34. package/lib.commonjs/quantumcoin.js.map +1 -1
  35. package/lib.commonjs/transaction/address.d.ts.map +1 -1
  36. package/lib.commonjs/transaction/address.js +8 -3
  37. package/lib.commonjs/transaction/address.js.map +1 -1
  38. package/lib.commonjs/transaction/transaction.d.ts.map +1 -1
  39. package/lib.commonjs/transaction/transaction.js +7 -40
  40. package/lib.commonjs/transaction/transaction.js.map +1 -1
  41. package/lib.commonjs/wallet/json-keystore.d.ts.map +1 -1
  42. package/lib.commonjs/wallet/json-keystore.js +7 -7
  43. package/lib.commonjs/wallet/json-keystore.js.map +1 -1
  44. package/lib.commonjs/wallet/wallet.d.ts.map +1 -1
  45. package/lib.commonjs/wallet/wallet.js +2 -2
  46. package/lib.commonjs/wallet/wallet.js.map +1 -1
  47. package/lib.esm/README.md +16 -16
  48. package/lib.esm/_version.js +1 -1
  49. package/lib.esm/crypto/signature.d.ts +3 -76
  50. package/lib.esm/crypto/signature.d.ts.map +1 -1
  51. package/lib.esm/crypto/signature.js +16 -202
  52. package/lib.esm/crypto/signature.js.map +1 -1
  53. package/lib.esm/crypto/signing-key.d.ts +1 -1
  54. package/lib.esm/crypto/signing-key.d.ts.map +1 -1
  55. package/lib.esm/crypto/signing-key.js +20 -9
  56. package/lib.esm/crypto/signing-key.js.map +1 -1
  57. package/lib.esm/package.json +12 -12
  58. package/lib.esm/providers/provider-jsonrpc.d.ts +0 -1
  59. package/lib.esm/providers/provider-jsonrpc.d.ts.map +1 -1
  60. package/lib.esm/providers/provider-jsonrpc.js +0 -1
  61. package/lib.esm/providers/provider-jsonrpc.js.map +1 -1
  62. package/lib.esm/quantumcoin.d.ts +2 -0
  63. package/lib.esm/quantumcoin.d.ts.map +1 -1
  64. package/lib.esm/quantumcoin.js +6 -0
  65. package/lib.esm/quantumcoin.js.map +1 -1
  66. package/lib.esm/transaction/address.d.ts.map +1 -1
  67. package/lib.esm/transaction/address.js +8 -2
  68. package/lib.esm/transaction/address.js.map +1 -1
  69. package/lib.esm/transaction/transaction.d.ts.map +1 -1
  70. package/lib.esm/transaction/transaction.js +7 -40
  71. package/lib.esm/transaction/transaction.js.map +1 -1
  72. package/lib.esm/wallet/json-keystore.d.ts.map +1 -1
  73. package/lib.esm/wallet/json-keystore.js +11 -5
  74. package/lib.esm/wallet/json-keystore.js.map +1 -1
  75. package/lib.esm/wallet/wallet.d.ts.map +1 -1
  76. package/lib.esm/wallet/wallet.js +3 -1
  77. package/lib.esm/wallet/wallet.js.map +1 -1
  78. package/package.json +6 -5
  79. package/rollup.config.mjs +50 -50
  80. package/src.ts/_version.ts +1 -1
  81. package/src.ts/abi/abi-coder.ts +237 -237
  82. package/src.ts/abi/bytes32.ts +45 -45
  83. package/src.ts/abi/coders/abstract-coder.ts +541 -541
  84. package/src.ts/abi/coders/address.ts +36 -36
  85. package/src.ts/abi/coders/anonymous.ts +29 -29
  86. package/src.ts/abi/coders/array.ts +199 -199
  87. package/src.ts/abi/coders/boolean.ts +27 -27
  88. package/src.ts/abi/coders/bytes.ts +43 -43
  89. package/src.ts/abi/coders/fixed-bytes.ts +37 -37
  90. package/src.ts/abi/coders/null.ts +28 -28
  91. package/src.ts/abi/coders/number.ts +63 -63
  92. package/src.ts/abi/coders/string.ts +29 -29
  93. package/src.ts/abi/coders/tuple.ts +69 -69
  94. package/src.ts/abi/fragments.ts +1617 -1617
  95. package/src.ts/abi/index.ts +41 -41
  96. package/src.ts/abi/interface.ts +1271 -1271
  97. package/src.ts/abi/typed.ts +796 -796
  98. package/src.ts/address/address.ts +148 -148
  99. package/src.ts/address/checks.ts +123 -123
  100. package/src.ts/address/contract-address.ts +80 -80
  101. package/src.ts/address/index.ts +57 -57
  102. package/src.ts/constants/addresses.ts +8 -8
  103. package/src.ts/constants/hashes.ts +7 -7
  104. package/src.ts/constants/index.ts +16 -16
  105. package/src.ts/constants/numbers.ts +35 -35
  106. package/src.ts/constants/strings.ts +16 -16
  107. package/src.ts/contract/contract.ts +1120 -1120
  108. package/src.ts/contract/factory.ts +143 -143
  109. package/src.ts/contract/index.ts +31 -31
  110. package/src.ts/contract/types.ts +236 -236
  111. package/src.ts/contract/wrappers.ts +225 -225
  112. package/src.ts/crypto/crypto-browser.ts +64 -64
  113. package/src.ts/crypto/crypto.ts +4 -4
  114. package/src.ts/crypto/hmac.ts +51 -51
  115. package/src.ts/crypto/index.ts +59 -59
  116. package/src.ts/crypto/keccak.ts +54 -54
  117. package/src.ts/crypto/pbkdf2.ts +55 -55
  118. package/src.ts/crypto/random.ts +36 -36
  119. package/src.ts/crypto/ripemd160.ts +43 -43
  120. package/src.ts/crypto/scrypt.ts +114 -114
  121. package/src.ts/crypto/sha2.ts +78 -78
  122. package/src.ts/crypto/signature.ts +145 -349
  123. package/src.ts/crypto/signing-key.ts +126 -118
  124. package/src.ts/hash/authorization.ts +38 -38
  125. package/src.ts/hash/id.ts +17 -17
  126. package/src.ts/hash/index.ts +18 -18
  127. package/src.ts/hash/message.ts +51 -51
  128. package/src.ts/hash/namehash.ts +101 -101
  129. package/src.ts/hash/solidity.ts +117 -117
  130. package/src.ts/hash/typed-data.ts +658 -658
  131. package/src.ts/index.ts +12 -12
  132. package/src.ts/providers/abstract-provider.ts +1761 -1761
  133. package/src.ts/providers/abstract-signer.ts +314 -314
  134. package/src.ts/providers/community.ts +49 -49
  135. package/src.ts/providers/contracts.ts +42 -42
  136. package/src.ts/providers/default-provider.ts +96 -96
  137. package/src.ts/providers/ens-resolver.ts +606 -606
  138. package/src.ts/providers/format.ts +320 -320
  139. package/src.ts/providers/formatting.ts +418 -418
  140. package/src.ts/providers/index.ts +125 -125
  141. package/src.ts/providers/network.ts +327 -327
  142. package/src.ts/providers/pagination.ts +8 -8
  143. package/src.ts/providers/plugin-fallback.ts +35 -35
  144. package/src.ts/providers/plugins-network.ts +281 -281
  145. package/src.ts/providers/provider-browser.ts +334 -334
  146. package/src.ts/providers/provider-fallback.ts +801 -801
  147. package/src.ts/providers/provider-ipcsocket-browser.ts +3 -3
  148. package/src.ts/providers/provider-ipcsocket.ts +81 -81
  149. package/src.ts/providers/provider-jsonrpc.ts +1334 -1335
  150. package/src.ts/providers/provider-socket.ts +352 -352
  151. package/src.ts/providers/provider-websocket.ts +103 -103
  152. package/src.ts/providers/provider.ts +2136 -2136
  153. package/src.ts/providers/signer-noncemanager.ts +98 -98
  154. package/src.ts/providers/signer.ts +166 -166
  155. package/src.ts/providers/subscriber-connection.ts +74 -74
  156. package/src.ts/providers/subscriber-filterid.ts +199 -199
  157. package/src.ts/providers/subscriber-polling.ts +321 -321
  158. package/src.ts/providers/ws-browser.ts +11 -11
  159. package/src.ts/providers/ws.ts +3 -3
  160. package/src.ts/quantumcoin.ts +219 -211
  161. package/src.ts/thirdparty.d.ts +16 -16
  162. package/src.ts/transaction/accesslist.ts +43 -43
  163. package/src.ts/transaction/address.ts +35 -31
  164. package/src.ts/transaction/authorization.ts +14 -14
  165. package/src.ts/transaction/index.ts +51 -51
  166. package/src.ts/transaction/transaction.ts +1349 -1379
  167. package/src.ts/utils/base58.ts +73 -73
  168. package/src.ts/utils/base64-browser.ts +25 -25
  169. package/src.ts/utils/base64.ts +56 -56
  170. package/src.ts/utils/data.ts +199 -199
  171. package/src.ts/utils/errors.ts +793 -793
  172. package/src.ts/utils/events.ts +105 -105
  173. package/src.ts/utils/fetch.ts +970 -970
  174. package/src.ts/utils/fixednumber.ts +643 -643
  175. package/src.ts/utils/geturl-browser.ts +81 -81
  176. package/src.ts/utils/geturl.ts +134 -134
  177. package/src.ts/utils/index.ts +95 -95
  178. package/src.ts/utils/maths.ts +240 -240
  179. package/src.ts/utils/properties.ts +60 -60
  180. package/src.ts/utils/rlp-decode.ts +104 -104
  181. package/src.ts/utils/rlp-encode.ts +64 -64
  182. package/src.ts/utils/rlp.ts +20 -20
  183. package/src.ts/utils/units.ts +91 -91
  184. package/src.ts/utils/utf8.ts +325 -325
  185. package/src.ts/utils/uuid.ts +36 -36
  186. package/src.ts/wallet/base-wallet.ts +160 -160
  187. package/src.ts/wallet/index.ts +32 -32
  188. package/src.ts/wallet/json-keystore.ts +108 -106
  189. package/src.ts/wallet/utils.ts +147 -147
  190. package/src.ts/wallet/wallet.ts +138 -139
  191. package/src.ts/wordlists/bit-reader.ts +35 -35
  192. package/src.ts/wordlists/decode-owl.ts +58 -58
  193. package/src.ts/wordlists/decode-owla.ts +33 -33
  194. package/src.ts/wordlists/generation/encode-latin.ts +370 -370
  195. package/src.ts/wordlists/index.ts +26 -26
  196. package/src.ts/wordlists/lang-cz.ts +33 -33
  197. package/src.ts/wordlists/lang-en.ts +33 -33
  198. package/src.ts/wordlists/lang-es.ts +35 -35
  199. package/src.ts/wordlists/lang-fr.ts +34 -34
  200. package/src.ts/wordlists/lang-it.ts +33 -33
  201. package/src.ts/wordlists/lang-ja.ts +181 -181
  202. package/src.ts/wordlists/lang-ko.ts +104 -104
  203. package/src.ts/wordlists/lang-pt.ts +34 -34
  204. package/src.ts/wordlists/lang-zh.ts +112 -112
  205. package/src.ts/wordlists/wordlist-owl.ts +77 -77
  206. package/src.ts/wordlists/wordlist-owla.ts +41 -41
  207. package/src.ts/wordlists/wordlist.ts +59 -59
  208. package/src.ts/wordlists/wordlists-browser.ts +8 -8
  209. package/src.ts/wordlists/wordlists-extra.ts +9 -9
  210. package/src.ts/wordlists/wordlists.ts +38 -38
  211. package/dist/quantumcoin.min.js'.gz' +0 -0
  212. package/dist/quantumcoin.umd.min.js'.gz' +0 -0
  213. package/dist/wordlists-extra.min.js'.gz' +0 -0
  214. package/lib.commonjs/providers/provider-alchemy.d.ts +0 -50
  215. package/lib.commonjs/providers/provider-alchemy.d.ts.map +0 -1
  216. package/lib.commonjs/providers/provider-alchemy.js +0 -151
  217. package/lib.commonjs/providers/provider-alchemy.js.map +0 -1
  218. package/lib.commonjs/providers/provider-ankr.d.ts +0 -61
  219. package/lib.commonjs/providers/provider-ankr.d.ts.map +0 -1
  220. package/lib.commonjs/providers/provider-ankr.js +0 -137
  221. package/lib.commonjs/providers/provider-ankr.js.map +0 -1
  222. package/lib.commonjs/providers/provider-blockscout.d.ts +0 -59
  223. package/lib.commonjs/providers/provider-blockscout.d.ts.map +0 -1
  224. package/lib.commonjs/providers/provider-blockscout.js +0 -145
  225. package/lib.commonjs/providers/provider-blockscout.js.map +0 -1
  226. package/lib.commonjs/providers/provider-chainstack.d.ts +0 -46
  227. package/lib.commonjs/providers/provider-chainstack.d.ts.map +0 -1
  228. package/lib.commonjs/providers/provider-chainstack.js +0 -102
  229. package/lib.commonjs/providers/provider-chainstack.js.map +0 -1
  230. package/lib.commonjs/providers/provider-cloudflare.d.ts +0 -14
  231. package/lib.commonjs/providers/provider-cloudflare.d.ts.map +0 -1
  232. package/lib.commonjs/providers/provider-cloudflare.js +0 -26
  233. package/lib.commonjs/providers/provider-cloudflare.js.map +0 -1
  234. package/lib.commonjs/providers/provider-etherscan.d.ts +0 -147
  235. package/lib.commonjs/providers/provider-etherscan.d.ts.map +0 -1
  236. package/lib.commonjs/providers/provider-etherscan.js +0 -587
  237. package/lib.commonjs/providers/provider-etherscan.js.map +0 -1
  238. package/lib.commonjs/providers/provider-infura.d.ts +0 -101
  239. package/lib.commonjs/providers/provider-infura.d.ts.map +0 -1
  240. package/lib.commonjs/providers/provider-infura.js +0 -206
  241. package/lib.commonjs/providers/provider-infura.js.map +0 -1
  242. package/lib.commonjs/providers/provider-pocket.d.ts +0 -54
  243. package/lib.commonjs/providers/provider-pocket.d.ts.map +0 -1
  244. package/lib.commonjs/providers/provider-pocket.js +0 -109
  245. package/lib.commonjs/providers/provider-pocket.js.map +0 -1
  246. package/lib.commonjs/providers/provider-quicknode.d.ts +0 -59
  247. package/lib.commonjs/providers/provider-quicknode.d.ts.map +0 -1
  248. package/lib.commonjs/providers/provider-quicknode.js +0 -163
  249. package/lib.commonjs/providers/provider-quicknode.js.map +0 -1
  250. package/lib.commonjs/wallet/hdwallet.d.ts +0 -248
  251. package/lib.commonjs/wallet/hdwallet.d.ts.map +0 -1
  252. package/lib.commonjs/wallet/hdwallet.js +0 -505
  253. package/lib.commonjs/wallet/hdwallet.js.map +0 -1
  254. package/lib.commonjs/wallet/json-crowdsale.d.ts +0 -27
  255. package/lib.commonjs/wallet/json-crowdsale.d.ts.map +0 -1
  256. package/lib.commonjs/wallet/json-crowdsale.js +0 -60
  257. package/lib.commonjs/wallet/json-crowdsale.js.map +0 -1
  258. package/lib.commonjs/wallet/mnemonic.d.ts +0 -65
  259. package/lib.commonjs/wallet/mnemonic.d.ts.map +0 -1
  260. package/lib.commonjs/wallet/mnemonic.js +0 -169
  261. package/lib.commonjs/wallet/mnemonic.js.map +0 -1
  262. package/lib.commonjs/wallet/seedwallet.d.ts +0 -4
  263. package/lib.commonjs/wallet/seedwallet.d.ts.map +0 -1
  264. package/lib.commonjs/wallet/seedwallet.js +0 -8
  265. package/lib.commonjs/wallet/seedwallet.js.map +0 -1
  266. package/lib.esm/providers/provider-alchemy.d.ts +0 -50
  267. package/lib.esm/providers/provider-alchemy.d.ts.map +0 -1
  268. package/lib.esm/providers/provider-alchemy.js +0 -147
  269. package/lib.esm/providers/provider-alchemy.js.map +0 -1
  270. package/lib.esm/providers/provider-ankr.d.ts +0 -61
  271. package/lib.esm/providers/provider-ankr.d.ts.map +0 -1
  272. package/lib.esm/providers/provider-ankr.js +0 -133
  273. package/lib.esm/providers/provider-ankr.js.map +0 -1
  274. package/lib.esm/providers/provider-blockscout.d.ts +0 -59
  275. package/lib.esm/providers/provider-blockscout.d.ts.map +0 -1
  276. package/lib.esm/providers/provider-blockscout.js +0 -141
  277. package/lib.esm/providers/provider-blockscout.js.map +0 -1
  278. package/lib.esm/providers/provider-chainstack.d.ts +0 -46
  279. package/lib.esm/providers/provider-chainstack.d.ts.map +0 -1
  280. package/lib.esm/providers/provider-chainstack.js +0 -98
  281. package/lib.esm/providers/provider-chainstack.js.map +0 -1
  282. package/lib.esm/providers/provider-cloudflare.d.ts +0 -14
  283. package/lib.esm/providers/provider-cloudflare.d.ts.map +0 -1
  284. package/lib.esm/providers/provider-cloudflare.js +0 -22
  285. package/lib.esm/providers/provider-cloudflare.js.map +0 -1
  286. package/lib.esm/providers/provider-etherscan.d.ts +0 -147
  287. package/lib.esm/providers/provider-etherscan.d.ts.map +0 -1
  288. package/lib.esm/providers/provider-etherscan.js +0 -584
  289. package/lib.esm/providers/provider-etherscan.js.map +0 -1
  290. package/lib.esm/providers/provider-infura.d.ts +0 -101
  291. package/lib.esm/providers/provider-infura.d.ts.map +0 -1
  292. package/lib.esm/providers/provider-infura.js +0 -201
  293. package/lib.esm/providers/provider-infura.js.map +0 -1
  294. package/lib.esm/providers/provider-pocket.d.ts +0 -54
  295. package/lib.esm/providers/provider-pocket.d.ts.map +0 -1
  296. package/lib.esm/providers/provider-pocket.js +0 -105
  297. package/lib.esm/providers/provider-pocket.js.map +0 -1
  298. package/lib.esm/providers/provider-quicknode.d.ts +0 -59
  299. package/lib.esm/providers/provider-quicknode.d.ts.map +0 -1
  300. package/lib.esm/providers/provider-quicknode.js +0 -159
  301. package/lib.esm/providers/provider-quicknode.js.map +0 -1
  302. package/lib.esm/wallet/hdwallet.d.ts +0 -248
  303. package/lib.esm/wallet/hdwallet.d.ts.map +0 -1
  304. package/lib.esm/wallet/hdwallet.js +0 -498
  305. package/lib.esm/wallet/hdwallet.js.map +0 -1
  306. package/lib.esm/wallet/json-crowdsale.d.ts +0 -27
  307. package/lib.esm/wallet/json-crowdsale.d.ts.map +0 -1
  308. package/lib.esm/wallet/json-crowdsale.js +0 -55
  309. package/lib.esm/wallet/json-crowdsale.js.map +0 -1
  310. package/lib.esm/wallet/mnemonic.d.ts +0 -65
  311. package/lib.esm/wallet/mnemonic.d.ts.map +0 -1
  312. package/lib.esm/wallet/mnemonic.js +0 -165
  313. package/lib.esm/wallet/mnemonic.js.map +0 -1
  314. package/lib.esm/wallet/seedwallet.d.ts +0 -4
  315. package/lib.esm/wallet/seedwallet.d.ts.map +0 -1
  316. package/lib.esm/wallet/seedwallet.js +0 -4
  317. package/lib.esm/wallet/seedwallet.js.map +0 -1
@@ -1,325 +1,325 @@
1
- /**
2
- * Using strings in Ethereum (or any security-basd system) requires
3
- * additional care. These utilities attempt to mitigate some of the
4
- * safety issues as well as provide the ability to recover and analyse
5
- * strings.
6
- *
7
- * @_subsection api/utils:Strings and UTF-8 [about-strings]
8
- */
9
- import { getBytes } from "./data.js";
10
- import { assertArgument, assertNormalize } from "./errors.js";
11
-
12
- import type { BytesLike } from "./index.js";
13
-
14
-
15
- ///////////////////////////////
16
-
17
- /**
18
- * The stanard normalization forms.
19
- */
20
- export type UnicodeNormalizationForm = "NFC" | "NFD" | "NFKC" | "NFKD";
21
-
22
- /**
23
- * When using the UTF-8 error API the following errors can be intercepted
24
- * and processed as the %%reason%% passed to the [[Utf8ErrorFunc]].
25
- *
26
- * **``"UNEXPECTED_CONTINUE"``** - a continuation byte was present where there
27
- * was nothing to continue.
28
- *
29
- * **``"BAD_PREFIX"``** - an invalid (non-continuation) byte to start a
30
- * UTF-8 codepoint was found.
31
- *
32
- * **``"OVERRUN"``** - the string is too short to process the expected
33
- * codepoint length.
34
- *
35
- * **``"MISSING_CONTINUE"``** - a missing continuation byte was expected but
36
- * not found. The %%offset%% indicates the index the continuation byte
37
- * was expected at.
38
- *
39
- * **``"OUT_OF_RANGE"``** - the computed code point is outside the range
40
- * for UTF-8. The %%badCodepoint%% indicates the computed codepoint, which was
41
- * outside the valid UTF-8 range.
42
- *
43
- * **``"UTF16_SURROGATE"``** - the UTF-8 strings contained a UTF-16 surrogate
44
- * pair. The %%badCodepoint%% is the computed codepoint, which was inside the
45
- * UTF-16 surrogate range.
46
- *
47
- * **``"OVERLONG"``** - the string is an overlong representation. The
48
- * %%badCodepoint%% indicates the computed codepoint, which has already
49
- * been bounds checked.
50
- *
51
- *
52
- * @returns string
53
- */
54
- export type Utf8ErrorReason = "UNEXPECTED_CONTINUE" | "BAD_PREFIX" | "OVERRUN" |
55
- "MISSING_CONTINUE" | "OUT_OF_RANGE" | "UTF16_SURROGATE" | "OVERLONG";
56
-
57
-
58
- /**
59
- * A callback that can be used with [[toUtf8String]] to analysis or
60
- * recovery from invalid UTF-8 data.
61
- *
62
- * Parsing UTF-8 data is done through a simple Finite-State Machine (FSM)
63
- * which calls the ``Utf8ErrorFunc`` if a fault is detected.
64
- *
65
- * The %%reason%% indicates where in the FSM execution the fault
66
- * occurred and the %%offset%% indicates where the input failed.
67
- *
68
- * The %%bytes%% represents the raw UTF-8 data that was provided and
69
- * %%output%% is the current array of UTF-8 code-points, which may
70
- * be updated by the ``Utf8ErrorFunc``.
71
- *
72
- * The value of the %%badCodepoint%% depends on the %%reason%%. See
73
- * [[Utf8ErrorReason]] for details.
74
- *
75
- * The function should return the number of bytes that should be skipped
76
- * when control resumes to the FSM.
77
- */
78
- export type Utf8ErrorFunc = (reason: Utf8ErrorReason, offset: number, bytes: Uint8Array, output: Array<number>, badCodepoint?: number) => number;
79
-
80
-
81
- function errorFunc(reason: Utf8ErrorReason, offset: number, bytes: Uint8Array, output: Array<number>, badCodepoint?: number): number {
82
- assertArgument(false, `invalid codepoint at offset ${ offset }; ${ reason }`, "bytes", bytes);
83
- }
84
-
85
- function ignoreFunc(reason: Utf8ErrorReason, offset: number, bytes: Uint8Array, output: Array<number>, badCodepoint?: number): number {
86
-
87
- // If there is an invalid prefix (including stray continuation), skip any additional continuation bytes
88
- if (reason === "BAD_PREFIX" || reason === "UNEXPECTED_CONTINUE") {
89
- let i = 0;
90
- for (let o = offset + 1; o < bytes.length; o++) {
91
- if (bytes[o] >> 6 !== 0x02) { break; }
92
- i++;
93
- }
94
- return i;
95
- }
96
-
97
- // This byte runs us past the end of the string, so just jump to the end
98
- // (but the first byte was read already read and therefore skipped)
99
- if (reason === "OVERRUN") {
100
- return bytes.length - offset - 1;
101
- }
102
-
103
- // Nothing to skip
104
- return 0;
105
- }
106
-
107
- function replaceFunc(reason: Utf8ErrorReason, offset: number, bytes: Uint8Array, output: Array<number>, badCodepoint?: number): number {
108
-
109
- // Overlong representations are otherwise "valid" code points; just non-deistingtished
110
- if (reason === "OVERLONG") {
111
- assertArgument(typeof(badCodepoint) === "number", "invalid bad code point for replacement", "badCodepoint", badCodepoint);
112
- output.push(badCodepoint);
113
- return 0;
114
- }
115
-
116
- // Put the replacement character into the output
117
- output.push(0xfffd);
118
-
119
- // Otherwise, process as if ignoring errors
120
- return ignoreFunc(reason, offset, bytes, output, badCodepoint);
121
- }
122
-
123
- /**
124
- * A handful of popular, built-in UTF-8 error handling strategies.
125
- *
126
- * **``"error"``** - throws on ANY illegal UTF-8 sequence or
127
- * non-canonical (overlong) codepoints (this is the default)
128
- *
129
- * **``"ignore"``** - silently drops any illegal UTF-8 sequence
130
- * and accepts non-canonical (overlong) codepoints
131
- *
132
- * **``"replace"``** - replace any illegal UTF-8 sequence with the
133
- * UTF-8 replacement character (i.e. ``"\\ufffd"``) and accepts
134
- * non-canonical (overlong) codepoints
135
- *
136
- * @returns: Record<"error" | "ignore" | "replace", Utf8ErrorFunc>
137
- */
138
- export const Utf8ErrorFuncs: Readonly<Record<"error" | "ignore" | "replace", Utf8ErrorFunc>> = Object.freeze({
139
- error: errorFunc,
140
- ignore: ignoreFunc,
141
- replace: replaceFunc
142
- });
143
-
144
- // http://stackoverflow.com/questions/13356493/decode-utf-8-with-javascript#13691499
145
- function getUtf8CodePoints(_bytes: BytesLike, onError?: Utf8ErrorFunc): Array<number> {
146
- if (onError == null) { onError = Utf8ErrorFuncs.error; }
147
-
148
- const bytes = getBytes(_bytes, "bytes");
149
-
150
- const result: Array<number> = [];
151
- let i = 0;
152
-
153
- // Invalid bytes are ignored
154
- while(i < bytes.length) {
155
-
156
- const c = bytes[i++];
157
-
158
- // 0xxx xxxx
159
- if (c >> 7 === 0) {
160
- result.push(c);
161
- continue;
162
- }
163
-
164
- // Multibyte; how many bytes left for this character?
165
- let extraLength: null | number = null;
166
- let overlongMask: null | number = null;
167
-
168
- // 110x xxxx 10xx xxxx
169
- if ((c & 0xe0) === 0xc0) {
170
- extraLength = 1;
171
- overlongMask = 0x7f;
172
-
173
- // 1110 xxxx 10xx xxxx 10xx xxxx
174
- } else if ((c & 0xf0) === 0xe0) {
175
- extraLength = 2;
176
- overlongMask = 0x7ff;
177
-
178
- // 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx
179
- } else if ((c & 0xf8) === 0xf0) {
180
- extraLength = 3;
181
- overlongMask = 0xffff;
182
-
183
- } else {
184
- if ((c & 0xc0) === 0x80) {
185
- i += onError("UNEXPECTED_CONTINUE", i - 1, bytes, result);
186
- } else {
187
- i += onError("BAD_PREFIX", i - 1, bytes, result);
188
- }
189
- continue;
190
- }
191
-
192
- // Do we have enough bytes in our data?
193
- if (i - 1 + extraLength >= bytes.length) {
194
- i += onError("OVERRUN", i - 1, bytes, result);
195
- continue;
196
- }
197
-
198
- // Remove the length prefix from the char
199
- let res: null | number = c & ((1 << (8 - extraLength - 1)) - 1);
200
-
201
- for (let j = 0; j < extraLength; j++) {
202
- let nextChar = bytes[i];
203
-
204
- // Invalid continuation byte
205
- if ((nextChar & 0xc0) != 0x80) {
206
- i += onError("MISSING_CONTINUE", i, bytes, result);
207
- res = null;
208
- break;
209
- };
210
-
211
- res = (res << 6) | (nextChar & 0x3f);
212
- i++;
213
- }
214
-
215
- // See above loop for invalid continuation byte
216
- if (res === null) { continue; }
217
-
218
- // Maximum code point
219
- if (res > 0x10ffff) {
220
- i += onError("OUT_OF_RANGE", i - 1 - extraLength, bytes, result, res);
221
- continue;
222
- }
223
-
224
- // Reserved for UTF-16 surrogate halves
225
- if (res >= 0xd800 && res <= 0xdfff) {
226
- i += onError("UTF16_SURROGATE", i - 1 - extraLength, bytes, result, res);
227
- continue;
228
- }
229
-
230
- // Check for overlong sequences (more bytes than needed)
231
- if (res <= overlongMask) {
232
- i += onError("OVERLONG", i - 1 - extraLength, bytes, result, res);
233
- continue;
234
- }
235
-
236
- result.push(res);
237
- }
238
-
239
- return result;
240
- }
241
-
242
- // http://stackoverflow.com/questions/18729405/how-to-convert-utf8-string-to-byte-array
243
-
244
- /**
245
- * Returns the UTF-8 byte representation of %%str%%.
246
- *
247
- * If %%form%% is specified, the string is normalized.
248
- */
249
- export function toUtf8Bytes(str: string, form?: UnicodeNormalizationForm): Uint8Array {
250
- assertArgument(typeof(str) === "string", "invalid string value", "str", str);
251
-
252
- if (form != null) {
253
- assertNormalize(form);
254
- str = str.normalize(form);
255
- }
256
-
257
- let result: Array<number> = [];
258
- for (let i = 0; i < str.length; i++) {
259
- const c = str.charCodeAt(i);
260
-
261
- if (c < 0x80) {
262
- result.push(c);
263
-
264
- } else if (c < 0x800) {
265
- result.push((c >> 6) | 0xc0);
266
- result.push((c & 0x3f) | 0x80);
267
-
268
- } else if ((c & 0xfc00) == 0xd800) {
269
- i++;
270
- const c2 = str.charCodeAt(i);
271
-
272
- assertArgument(i < str.length && ((c2 & 0xfc00) === 0xdc00),
273
- "invalid surrogate pair", "str", str);
274
-
275
- // Surrogate Pair
276
- const pair = 0x10000 + ((c & 0x03ff) << 10) + (c2 & 0x03ff);
277
- result.push((pair >> 18) | 0xf0);
278
- result.push(((pair >> 12) & 0x3f) | 0x80);
279
- result.push(((pair >> 6) & 0x3f) | 0x80);
280
- result.push((pair & 0x3f) | 0x80);
281
-
282
- } else {
283
- result.push((c >> 12) | 0xe0);
284
- result.push(((c >> 6) & 0x3f) | 0x80);
285
- result.push((c & 0x3f) | 0x80);
286
- }
287
- }
288
-
289
- return new Uint8Array(result);
290
- };
291
-
292
- //export
293
- function _toUtf8String(codePoints: Array<number>): string {
294
- return codePoints.map((codePoint) => {
295
- if (codePoint <= 0xffff) {
296
- return String.fromCharCode(codePoint);
297
- }
298
- codePoint -= 0x10000;
299
- return String.fromCharCode(
300
- (((codePoint >> 10) & 0x3ff) + 0xd800),
301
- ((codePoint & 0x3ff) + 0xdc00)
302
- );
303
- }).join("");
304
- }
305
-
306
- /**
307
- * Returns the string represented by the UTF-8 data %%bytes%%.
308
- *
309
- * When %%onError%% function is specified, it is called on UTF-8
310
- * errors allowing recovery using the [[Utf8ErrorFunc]] API.
311
- * (default: [error](Utf8ErrorFuncs))
312
- */
313
- export function toUtf8String(bytes: BytesLike, onError?: Utf8ErrorFunc): string {
314
- return _toUtf8String(getUtf8CodePoints(bytes, onError));
315
- }
316
-
317
- /**
318
- * Returns the UTF-8 code-points for %%str%%.
319
- *
320
- * If %%form%% is specified, the string is normalized.
321
- */
322
- export function toUtf8CodePoints(str: string, form?: UnicodeNormalizationForm): Array<number> {
323
- return getUtf8CodePoints(toUtf8Bytes(str, form));
324
- }
325
-
1
+ /**
2
+ * Using strings in Ethereum (or any security-basd system) requires
3
+ * additional care. These utilities attempt to mitigate some of the
4
+ * safety issues as well as provide the ability to recover and analyse
5
+ * strings.
6
+ *
7
+ * @_subsection api/utils:Strings and UTF-8 [about-strings]
8
+ */
9
+ import { getBytes } from "./data.js";
10
+ import { assertArgument, assertNormalize } from "./errors.js";
11
+
12
+ import type { BytesLike } from "./index.js";
13
+
14
+
15
+ ///////////////////////////////
16
+
17
+ /**
18
+ * The stanard normalization forms.
19
+ */
20
+ export type UnicodeNormalizationForm = "NFC" | "NFD" | "NFKC" | "NFKD";
21
+
22
+ /**
23
+ * When using the UTF-8 error API the following errors can be intercepted
24
+ * and processed as the %%reason%% passed to the [[Utf8ErrorFunc]].
25
+ *
26
+ * **``"UNEXPECTED_CONTINUE"``** - a continuation byte was present where there
27
+ * was nothing to continue.
28
+ *
29
+ * **``"BAD_PREFIX"``** - an invalid (non-continuation) byte to start a
30
+ * UTF-8 codepoint was found.
31
+ *
32
+ * **``"OVERRUN"``** - the string is too short to process the expected
33
+ * codepoint length.
34
+ *
35
+ * **``"MISSING_CONTINUE"``** - a missing continuation byte was expected but
36
+ * not found. The %%offset%% indicates the index the continuation byte
37
+ * was expected at.
38
+ *
39
+ * **``"OUT_OF_RANGE"``** - the computed code point is outside the range
40
+ * for UTF-8. The %%badCodepoint%% indicates the computed codepoint, which was
41
+ * outside the valid UTF-8 range.
42
+ *
43
+ * **``"UTF16_SURROGATE"``** - the UTF-8 strings contained a UTF-16 surrogate
44
+ * pair. The %%badCodepoint%% is the computed codepoint, which was inside the
45
+ * UTF-16 surrogate range.
46
+ *
47
+ * **``"OVERLONG"``** - the string is an overlong representation. The
48
+ * %%badCodepoint%% indicates the computed codepoint, which has already
49
+ * been bounds checked.
50
+ *
51
+ *
52
+ * @returns string
53
+ */
54
+ export type Utf8ErrorReason = "UNEXPECTED_CONTINUE" | "BAD_PREFIX" | "OVERRUN" |
55
+ "MISSING_CONTINUE" | "OUT_OF_RANGE" | "UTF16_SURROGATE" | "OVERLONG";
56
+
57
+
58
+ /**
59
+ * A callback that can be used with [[toUtf8String]] to analysis or
60
+ * recovery from invalid UTF-8 data.
61
+ *
62
+ * Parsing UTF-8 data is done through a simple Finite-State Machine (FSM)
63
+ * which calls the ``Utf8ErrorFunc`` if a fault is detected.
64
+ *
65
+ * The %%reason%% indicates where in the FSM execution the fault
66
+ * occurred and the %%offset%% indicates where the input failed.
67
+ *
68
+ * The %%bytes%% represents the raw UTF-8 data that was provided and
69
+ * %%output%% is the current array of UTF-8 code-points, which may
70
+ * be updated by the ``Utf8ErrorFunc``.
71
+ *
72
+ * The value of the %%badCodepoint%% depends on the %%reason%%. See
73
+ * [[Utf8ErrorReason]] for details.
74
+ *
75
+ * The function should return the number of bytes that should be skipped
76
+ * when control resumes to the FSM.
77
+ */
78
+ export type Utf8ErrorFunc = (reason: Utf8ErrorReason, offset: number, bytes: Uint8Array, output: Array<number>, badCodepoint?: number) => number;
79
+
80
+
81
+ function errorFunc(reason: Utf8ErrorReason, offset: number, bytes: Uint8Array, output: Array<number>, badCodepoint?: number): number {
82
+ assertArgument(false, `invalid codepoint at offset ${ offset }; ${ reason }`, "bytes", bytes);
83
+ }
84
+
85
+ function ignoreFunc(reason: Utf8ErrorReason, offset: number, bytes: Uint8Array, output: Array<number>, badCodepoint?: number): number {
86
+
87
+ // If there is an invalid prefix (including stray continuation), skip any additional continuation bytes
88
+ if (reason === "BAD_PREFIX" || reason === "UNEXPECTED_CONTINUE") {
89
+ let i = 0;
90
+ for (let o = offset + 1; o < bytes.length; o++) {
91
+ if (bytes[o] >> 6 !== 0x02) { break; }
92
+ i++;
93
+ }
94
+ return i;
95
+ }
96
+
97
+ // This byte runs us past the end of the string, so just jump to the end
98
+ // (but the first byte was read already read and therefore skipped)
99
+ if (reason === "OVERRUN") {
100
+ return bytes.length - offset - 1;
101
+ }
102
+
103
+ // Nothing to skip
104
+ return 0;
105
+ }
106
+
107
+ function replaceFunc(reason: Utf8ErrorReason, offset: number, bytes: Uint8Array, output: Array<number>, badCodepoint?: number): number {
108
+
109
+ // Overlong representations are otherwise "valid" code points; just non-deistingtished
110
+ if (reason === "OVERLONG") {
111
+ assertArgument(typeof(badCodepoint) === "number", "invalid bad code point for replacement", "badCodepoint", badCodepoint);
112
+ output.push(badCodepoint);
113
+ return 0;
114
+ }
115
+
116
+ // Put the replacement character into the output
117
+ output.push(0xfffd);
118
+
119
+ // Otherwise, process as if ignoring errors
120
+ return ignoreFunc(reason, offset, bytes, output, badCodepoint);
121
+ }
122
+
123
+ /**
124
+ * A handful of popular, built-in UTF-8 error handling strategies.
125
+ *
126
+ * **``"error"``** - throws on ANY illegal UTF-8 sequence or
127
+ * non-canonical (overlong) codepoints (this is the default)
128
+ *
129
+ * **``"ignore"``** - silently drops any illegal UTF-8 sequence
130
+ * and accepts non-canonical (overlong) codepoints
131
+ *
132
+ * **``"replace"``** - replace any illegal UTF-8 sequence with the
133
+ * UTF-8 replacement character (i.e. ``"\\ufffd"``) and accepts
134
+ * non-canonical (overlong) codepoints
135
+ *
136
+ * @returns: Record<"error" | "ignore" | "replace", Utf8ErrorFunc>
137
+ */
138
+ export const Utf8ErrorFuncs: Readonly<Record<"error" | "ignore" | "replace", Utf8ErrorFunc>> = Object.freeze({
139
+ error: errorFunc,
140
+ ignore: ignoreFunc,
141
+ replace: replaceFunc
142
+ });
143
+
144
+ // http://stackoverflow.com/questions/13356493/decode-utf-8-with-javascript#13691499
145
+ function getUtf8CodePoints(_bytes: BytesLike, onError?: Utf8ErrorFunc): Array<number> {
146
+ if (onError == null) { onError = Utf8ErrorFuncs.error; }
147
+
148
+ const bytes = getBytes(_bytes, "bytes");
149
+
150
+ const result: Array<number> = [];
151
+ let i = 0;
152
+
153
+ // Invalid bytes are ignored
154
+ while(i < bytes.length) {
155
+
156
+ const c = bytes[i++];
157
+
158
+ // 0xxx xxxx
159
+ if (c >> 7 === 0) {
160
+ result.push(c);
161
+ continue;
162
+ }
163
+
164
+ // Multibyte; how many bytes left for this character?
165
+ let extraLength: null | number = null;
166
+ let overlongMask: null | number = null;
167
+
168
+ // 110x xxxx 10xx xxxx
169
+ if ((c & 0xe0) === 0xc0) {
170
+ extraLength = 1;
171
+ overlongMask = 0x7f;
172
+
173
+ // 1110 xxxx 10xx xxxx 10xx xxxx
174
+ } else if ((c & 0xf0) === 0xe0) {
175
+ extraLength = 2;
176
+ overlongMask = 0x7ff;
177
+
178
+ // 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx
179
+ } else if ((c & 0xf8) === 0xf0) {
180
+ extraLength = 3;
181
+ overlongMask = 0xffff;
182
+
183
+ } else {
184
+ if ((c & 0xc0) === 0x80) {
185
+ i += onError("UNEXPECTED_CONTINUE", i - 1, bytes, result);
186
+ } else {
187
+ i += onError("BAD_PREFIX", i - 1, bytes, result);
188
+ }
189
+ continue;
190
+ }
191
+
192
+ // Do we have enough bytes in our data?
193
+ if (i - 1 + extraLength >= bytes.length) {
194
+ i += onError("OVERRUN", i - 1, bytes, result);
195
+ continue;
196
+ }
197
+
198
+ // Remove the length prefix from the char
199
+ let res: null | number = c & ((1 << (8 - extraLength - 1)) - 1);
200
+
201
+ for (let j = 0; j < extraLength; j++) {
202
+ let nextChar = bytes[i];
203
+
204
+ // Invalid continuation byte
205
+ if ((nextChar & 0xc0) != 0x80) {
206
+ i += onError("MISSING_CONTINUE", i, bytes, result);
207
+ res = null;
208
+ break;
209
+ };
210
+
211
+ res = (res << 6) | (nextChar & 0x3f);
212
+ i++;
213
+ }
214
+
215
+ // See above loop for invalid continuation byte
216
+ if (res === null) { continue; }
217
+
218
+ // Maximum code point
219
+ if (res > 0x10ffff) {
220
+ i += onError("OUT_OF_RANGE", i - 1 - extraLength, bytes, result, res);
221
+ continue;
222
+ }
223
+
224
+ // Reserved for UTF-16 surrogate halves
225
+ if (res >= 0xd800 && res <= 0xdfff) {
226
+ i += onError("UTF16_SURROGATE", i - 1 - extraLength, bytes, result, res);
227
+ continue;
228
+ }
229
+
230
+ // Check for overlong sequences (more bytes than needed)
231
+ if (res <= overlongMask) {
232
+ i += onError("OVERLONG", i - 1 - extraLength, bytes, result, res);
233
+ continue;
234
+ }
235
+
236
+ result.push(res);
237
+ }
238
+
239
+ return result;
240
+ }
241
+
242
+ // http://stackoverflow.com/questions/18729405/how-to-convert-utf8-string-to-byte-array
243
+
244
+ /**
245
+ * Returns the UTF-8 byte representation of %%str%%.
246
+ *
247
+ * If %%form%% is specified, the string is normalized.
248
+ */
249
+ export function toUtf8Bytes(str: string, form?: UnicodeNormalizationForm): Uint8Array {
250
+ assertArgument(typeof(str) === "string", "invalid string value", "str", str);
251
+
252
+ if (form != null) {
253
+ assertNormalize(form);
254
+ str = str.normalize(form);
255
+ }
256
+
257
+ let result: Array<number> = [];
258
+ for (let i = 0; i < str.length; i++) {
259
+ const c = str.charCodeAt(i);
260
+
261
+ if (c < 0x80) {
262
+ result.push(c);
263
+
264
+ } else if (c < 0x800) {
265
+ result.push((c >> 6) | 0xc0);
266
+ result.push((c & 0x3f) | 0x80);
267
+
268
+ } else if ((c & 0xfc00) == 0xd800) {
269
+ i++;
270
+ const c2 = str.charCodeAt(i);
271
+
272
+ assertArgument(i < str.length && ((c2 & 0xfc00) === 0xdc00),
273
+ "invalid surrogate pair", "str", str);
274
+
275
+ // Surrogate Pair
276
+ const pair = 0x10000 + ((c & 0x03ff) << 10) + (c2 & 0x03ff);
277
+ result.push((pair >> 18) | 0xf0);
278
+ result.push(((pair >> 12) & 0x3f) | 0x80);
279
+ result.push(((pair >> 6) & 0x3f) | 0x80);
280
+ result.push((pair & 0x3f) | 0x80);
281
+
282
+ } else {
283
+ result.push((c >> 12) | 0xe0);
284
+ result.push(((c >> 6) & 0x3f) | 0x80);
285
+ result.push((c & 0x3f) | 0x80);
286
+ }
287
+ }
288
+
289
+ return new Uint8Array(result);
290
+ };
291
+
292
+ //export
293
+ function _toUtf8String(codePoints: Array<number>): string {
294
+ return codePoints.map((codePoint) => {
295
+ if (codePoint <= 0xffff) {
296
+ return String.fromCharCode(codePoint);
297
+ }
298
+ codePoint -= 0x10000;
299
+ return String.fromCharCode(
300
+ (((codePoint >> 10) & 0x3ff) + 0xd800),
301
+ ((codePoint & 0x3ff) + 0xdc00)
302
+ );
303
+ }).join("");
304
+ }
305
+
306
+ /**
307
+ * Returns the string represented by the UTF-8 data %%bytes%%.
308
+ *
309
+ * When %%onError%% function is specified, it is called on UTF-8
310
+ * errors allowing recovery using the [[Utf8ErrorFunc]] API.
311
+ * (default: [error](Utf8ErrorFuncs))
312
+ */
313
+ export function toUtf8String(bytes: BytesLike, onError?: Utf8ErrorFunc): string {
314
+ return _toUtf8String(getUtf8CodePoints(bytes, onError));
315
+ }
316
+
317
+ /**
318
+ * Returns the UTF-8 code-points for %%str%%.
319
+ *
320
+ * If %%form%% is specified, the string is normalized.
321
+ */
322
+ export function toUtf8CodePoints(str: string, form?: UnicodeNormalizationForm): Array<number> {
323
+ return getUtf8CodePoints(toUtf8Bytes(str, form));
324
+ }
325
+