@nktkas/hyperliquid 0.13.1 → 0.13.2

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 (183) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +33 -17
  3. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
  4. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
  5. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.js +2 -0
  6. package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
  7. package/esm/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
  8. package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.js +1 -1
  9. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
  10. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
  11. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.js +2 -0
  12. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
  13. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.js +1 -1
  14. package/{script/deps/jsr.io/@std/encoding/1.0.6 → esm/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
  15. package/esm/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
  16. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/hex.js +1 -1
  17. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
  18. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
  19. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.js +2 -0
  20. package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
  21. package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
  22. package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.js +2 -2
  23. package/esm/mod.d.ts +1 -0
  24. package/esm/mod.d.ts.map +1 -1
  25. package/esm/src/clients/event.d.ts +57 -16
  26. package/esm/src/clients/event.d.ts.map +1 -1
  27. package/esm/src/clients/event.js +72 -16
  28. package/esm/src/clients/public.d.ts +241 -49
  29. package/esm/src/clients/public.d.ts.map +1 -1
  30. package/esm/src/clients/public.js +243 -47
  31. package/esm/src/clients/wallet.d.ts +120 -51
  32. package/esm/src/clients/wallet.d.ts.map +1 -1
  33. package/esm/src/clients/wallet.js +192 -58
  34. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts +27 -21
  35. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -1
  36. package/esm/src/transports/websocket/hyperliquid_event_target.js +37 -0
  37. package/esm/src/transports/websocket/websocket_request_dispatcher.js +3 -3
  38. package/esm/src/transports/websocket/websocket_transport.d.ts +7 -10
  39. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  40. package/esm/src/transports/websocket/websocket_transport.js +41 -35
  41. package/esm/src/types/exchange/common.d.ts +6 -6
  42. package/esm/src/types/exchange/common.d.ts.map +1 -1
  43. package/esm/src/types/exchange/requests.d.ts +131 -64
  44. package/esm/src/types/exchange/requests.d.ts.map +1 -1
  45. package/esm/src/types/exchange/responses.d.ts +7 -7
  46. package/esm/src/types/exchange/responses.d.ts.map +1 -1
  47. package/esm/src/types/explorer/common.d.ts +15 -15
  48. package/esm/src/types/explorer/common.d.ts.map +1 -1
  49. package/esm/src/types/explorer/requests.d.ts +19 -6
  50. package/esm/src/types/explorer/requests.d.ts.map +1 -1
  51. package/esm/src/types/explorer/responses.d.ts +9 -2
  52. package/esm/src/types/explorer/responses.d.ts.map +1 -1
  53. package/esm/src/types/info/accounts.d.ts +189 -124
  54. package/esm/src/types/info/accounts.d.ts.map +1 -1
  55. package/esm/src/types/info/assets.d.ts +82 -78
  56. package/esm/src/types/info/assets.d.ts.map +1 -1
  57. package/esm/src/types/info/delegations.d.ts +117 -0
  58. package/esm/src/types/info/delegations.d.ts.map +1 -0
  59. package/esm/src/types/info/delegations.js +1 -0
  60. package/esm/src/types/info/orders.d.ts +67 -67
  61. package/esm/src/types/info/orders.d.ts.map +1 -1
  62. package/esm/src/types/info/requests.d.ts +128 -63
  63. package/esm/src/types/info/requests.d.ts.map +1 -1
  64. package/esm/src/types/info/vaults.d.ts +41 -84
  65. package/esm/src/types/info/vaults.d.ts.map +1 -1
  66. package/esm/src/types/subscriptions/common.d.ts +3 -0
  67. package/esm/src/types/subscriptions/common.d.ts.map +1 -1
  68. package/esm/src/types/subscriptions/requests.d.ts +10 -0
  69. package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
  70. package/esm/src/utils/key_sort.d.ts +2 -2
  71. package/esm/src/utils/key_sort.d.ts.map +1 -1
  72. package/esm/src/utils/signing.d.ts +1 -1
  73. package/esm/src/utils/signing.js +2 -2
  74. package/package.json +1 -1
  75. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +13 -3
  76. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +23 -13
  77. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.js +55 -45
  78. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.js +97 -87
  79. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.js +14 -4
  80. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.js +288 -278
  81. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.js +238 -228
  82. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
  83. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
  84. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.js +13 -0
  85. package/script/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
  86. package/script/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
  87. package/script/deps/jsr.io/@std/bytes/1.0.5/concat.js +45 -0
  88. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
  89. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
  90. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.js +13 -0
  91. package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
  92. package/script/deps/jsr.io/@std/encoding/1.0.7/_validate_binary_like.js +39 -0
  93. package/{esm/deps/jsr.io/@std/encoding/1.0.6 → script/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
  94. package/script/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
  95. package/script/deps/jsr.io/@std/encoding/1.0.7/hex.js +123 -0
  96. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
  97. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
  98. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.js +13 -0
  99. package/script/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
  100. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
  101. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.js +250 -0
  102. package/script/mod.d.ts +1 -0
  103. package/script/mod.d.ts.map +1 -1
  104. package/script/mod.js +23 -13
  105. package/script/src/clients/event.d.ts +57 -16
  106. package/script/src/clients/event.d.ts.map +1 -1
  107. package/script/src/clients/event.js +551 -485
  108. package/script/src/clients/public.d.ts +241 -49
  109. package/script/src/clients/public.d.ts.map +1 -1
  110. package/script/src/clients/public.js +914 -708
  111. package/script/src/clients/wallet.d.ts +120 -51
  112. package/script/src/clients/wallet.d.ts.map +1 -1
  113. package/script/src/clients/wallet.js +1121 -977
  114. package/script/src/transports/base.js +25 -15
  115. package/script/src/transports/http/http_transport.js +174 -164
  116. package/script/src/transports/websocket/hyperliquid_event_target.d.ts +27 -21
  117. package/script/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -1
  118. package/script/src/transports/websocket/hyperliquid_event_target.js +80 -33
  119. package/script/src/transports/websocket/reconnecting_websocket.js +364 -354
  120. package/script/src/transports/websocket/websocket_request_dispatcher.js +196 -186
  121. package/script/src/transports/websocket/websocket_transport.d.ts +7 -10
  122. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  123. package/script/src/transports/websocket/websocket_transport.js +236 -220
  124. package/script/src/types/common.js +12 -2
  125. package/script/src/types/exchange/common.d.ts +6 -6
  126. package/script/src/types/exchange/common.d.ts.map +1 -1
  127. package/script/src/types/exchange/common.js +12 -2
  128. package/script/src/types/exchange/requests.d.ts +131 -64
  129. package/script/src/types/exchange/requests.d.ts.map +1 -1
  130. package/script/src/types/exchange/requests.js +12 -2
  131. package/script/src/types/exchange/responses.d.ts +7 -7
  132. package/script/src/types/exchange/responses.d.ts.map +1 -1
  133. package/script/src/types/exchange/responses.js +12 -2
  134. package/script/src/types/explorer/common.d.ts +15 -15
  135. package/script/src/types/explorer/common.d.ts.map +1 -1
  136. package/script/src/types/explorer/common.js +12 -2
  137. package/script/src/types/explorer/requests.d.ts +19 -6
  138. package/script/src/types/explorer/requests.d.ts.map +1 -1
  139. package/script/src/types/explorer/requests.js +12 -2
  140. package/script/src/types/explorer/responses.d.ts +9 -2
  141. package/script/src/types/explorer/responses.d.ts.map +1 -1
  142. package/script/src/types/explorer/responses.js +12 -2
  143. package/script/src/types/info/accounts.d.ts +189 -124
  144. package/script/src/types/info/accounts.d.ts.map +1 -1
  145. package/script/src/types/info/accounts.js +12 -2
  146. package/script/src/types/info/assets.d.ts +82 -78
  147. package/script/src/types/info/assets.d.ts.map +1 -1
  148. package/script/src/types/info/assets.js +12 -2
  149. package/script/src/types/info/delegations.d.ts +117 -0
  150. package/script/src/types/info/delegations.d.ts.map +1 -0
  151. package/script/src/types/info/delegations.js +12 -0
  152. package/script/src/types/info/orders.d.ts +67 -67
  153. package/script/src/types/info/orders.d.ts.map +1 -1
  154. package/script/src/types/info/orders.js +12 -2
  155. package/script/src/types/info/requests.d.ts +128 -63
  156. package/script/src/types/info/requests.d.ts.map +1 -1
  157. package/script/src/types/info/requests.js +12 -2
  158. package/script/src/types/info/vaults.d.ts +41 -84
  159. package/script/src/types/info/vaults.d.ts.map +1 -1
  160. package/script/src/types/info/vaults.js +12 -2
  161. package/script/src/types/subscriptions/common.d.ts +3 -0
  162. package/script/src/types/subscriptions/common.d.ts.map +1 -1
  163. package/script/src/types/subscriptions/common.js +12 -2
  164. package/script/src/types/subscriptions/requests.d.ts +10 -0
  165. package/script/src/types/subscriptions/requests.d.ts.map +1 -1
  166. package/script/src/types/subscriptions/requests.js +12 -2
  167. package/script/src/utils/key_sort.d.ts +2 -2
  168. package/script/src/utils/key_sort.d.ts.map +1 -1
  169. package/script/src/utils/key_sort.js +133 -123
  170. package/script/src/utils/signing.d.ts +1 -1
  171. package/script/src/utils/signing.js +172 -162
  172. package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
  173. package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
  174. package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
  175. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
  176. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.js +0 -35
  177. package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.js +0 -29
  178. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
  179. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.js +0 -113
  180. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
  181. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.js +0 -240
  182. /package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
  183. /package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
@@ -1,288 +1,298 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.shake256 = exports.shake128 = exports.keccak_512 = exports.keccak_384 = exports.keccak_256 = exports.keccak_224 = exports.sha3_512 = exports.sha3_384 = exports.sha3_256 = exports.sha3_224 = exports.Keccak = void 0;
4
- exports.keccakP = keccakP;
5
- /**
6
- * SHA3 (keccak) hash function, based on a new "Sponge function" design.
7
- * Different from older hashes, the internal state is bigger than output size.
8
- *
9
- * Check out [FIPS-202](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf),
10
- * [Website](https://keccak.team/keccak.html),
11
- * [the differences between SHA-3 and Keccak](https://crypto.stackexchange.com/questions/15727/what-are-the-key-differences-between-the-draft-sha-3-standard-and-the-keccak-sub).
12
- *
13
- * Check out `sha3-addons` module for cSHAKE, k12, and others.
14
- * @module
15
- */
16
- const _assert_js_1 = require("./_assert.js");
17
- const _u64_js_1 = require("./_u64.js");
18
- const utils_js_1 = require("./utils.js");
19
- // Various per round constants calculations
20
- const SHA3_PI = [];
21
- const SHA3_ROTL = [];
22
- const _SHA3_IOTA = [];
23
- const _0n = /* @__PURE__ */ BigInt(0);
24
- const _1n = /* @__PURE__ */ BigInt(1);
25
- const _2n = /* @__PURE__ */ BigInt(2);
26
- const _7n = /* @__PURE__ */ BigInt(7);
27
- const _256n = /* @__PURE__ */ BigInt(256);
28
- const _0x71n = /* @__PURE__ */ BigInt(0x71);
29
- for (let round = 0, R = _1n, x = 1, y = 0; round < 24; round++) {
30
- // Pi
31
- [x, y] = [y, (2 * x + 3 * y) % 5];
32
- SHA3_PI.push(2 * (5 * y + x));
33
- // Rotational
34
- SHA3_ROTL.push((((round + 1) * (round + 2)) / 2) % 64);
35
- // Iota
36
- let t = _0n;
37
- for (let j = 0; j < 7; j++) {
38
- R = ((R << _1n) ^ ((R >> _7n) * _0x71n)) % _256n;
39
- if (R & _2n)
40
- t ^= _1n << ((_1n << /* @__PURE__ */ BigInt(j)) - _1n);
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
41
5
  }
42
- _SHA3_IOTA.push(t);
43
- }
44
- const [SHA3_IOTA_H, SHA3_IOTA_L] = /* @__PURE__ */ (0, _u64_js_1.split)(_SHA3_IOTA, true);
45
- // Left rotation (without 0, 32, 64)
46
- const rotlH = (h, l, s) => (s > 32 ? (0, _u64_js_1.rotlBH)(h, l, s) : (0, _u64_js_1.rotlSH)(h, l, s));
47
- const rotlL = (h, l, s) => (s > 32 ? (0, _u64_js_1.rotlBL)(h, l, s) : (0, _u64_js_1.rotlSL)(h, l, s));
48
- /** `keccakf1600` internal function, additionally allows to adjust round count. */
49
- function keccakP(s, rounds = 24) {
50
- const B = new Uint32Array(5 * 2);
51
- // NOTE: all indices are x2 since we store state as u32 instead of u64 (bigints to slow in js)
52
- for (let round = 24 - rounds; round < 24; round++) {
53
- // Theta θ
54
- for (let x = 0; x < 10; x++)
55
- B[x] = s[x] ^ s[x + 10] ^ s[x + 20] ^ s[x + 30] ^ s[x + 40];
56
- for (let x = 0; x < 10; x += 2) {
57
- const idx1 = (x + 8) % 10;
58
- const idx0 = (x + 2) % 10;
59
- const B0 = B[idx0];
60
- const B1 = B[idx0 + 1];
61
- const Th = rotlH(B0, B1, 1) ^ B[idx1];
62
- const Tl = rotlL(B0, B1, 1) ^ B[idx1 + 1];
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "./_assert.js", "./_u64.js", "./utils.js"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.shake256 = exports.shake128 = exports.keccak_512 = exports.keccak_384 = exports.keccak_256 = exports.keccak_224 = exports.sha3_512 = exports.sha3_384 = exports.sha3_256 = exports.sha3_224 = exports.Keccak = void 0;
13
+ exports.keccakP = keccakP;
14
+ /**
15
+ * SHA3 (keccak) hash function, based on a new "Sponge function" design.
16
+ * Different from older hashes, the internal state is bigger than output size.
17
+ *
18
+ * Check out [FIPS-202](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf),
19
+ * [Website](https://keccak.team/keccak.html),
20
+ * [the differences between SHA-3 and Keccak](https://crypto.stackexchange.com/questions/15727/what-are-the-key-differences-between-the-draft-sha-3-standard-and-the-keccak-sub).
21
+ *
22
+ * Check out `sha3-addons` module for cSHAKE, k12, and others.
23
+ * @module
24
+ */
25
+ const _assert_js_1 = require("./_assert.js");
26
+ const _u64_js_1 = require("./_u64.js");
27
+ const utils_js_1 = require("./utils.js");
28
+ // Various per round constants calculations
29
+ const SHA3_PI = [];
30
+ const SHA3_ROTL = [];
31
+ const _SHA3_IOTA = [];
32
+ const _0n = /* @__PURE__ */ BigInt(0);
33
+ const _1n = /* @__PURE__ */ BigInt(1);
34
+ const _2n = /* @__PURE__ */ BigInt(2);
35
+ const _7n = /* @__PURE__ */ BigInt(7);
36
+ const _256n = /* @__PURE__ */ BigInt(256);
37
+ const _0x71n = /* @__PURE__ */ BigInt(0x71);
38
+ for (let round = 0, R = _1n, x = 1, y = 0; round < 24; round++) {
39
+ // Pi
40
+ [x, y] = [y, (2 * x + 3 * y) % 5];
41
+ SHA3_PI.push(2 * (5 * y + x));
42
+ // Rotational
43
+ SHA3_ROTL.push((((round + 1) * (round + 2)) / 2) % 64);
44
+ // Iota
45
+ let t = _0n;
46
+ for (let j = 0; j < 7; j++) {
47
+ R = ((R << _1n) ^ ((R >> _7n) * _0x71n)) % _256n;
48
+ if (R & _2n)
49
+ t ^= _1n << ((_1n << /* @__PURE__ */ BigInt(j)) - _1n);
50
+ }
51
+ _SHA3_IOTA.push(t);
52
+ }
53
+ const [SHA3_IOTA_H, SHA3_IOTA_L] = /* @__PURE__ */ (0, _u64_js_1.split)(_SHA3_IOTA, true);
54
+ // Left rotation (without 0, 32, 64)
55
+ const rotlH = (h, l, s) => (s > 32 ? (0, _u64_js_1.rotlBH)(h, l, s) : (0, _u64_js_1.rotlSH)(h, l, s));
56
+ const rotlL = (h, l, s) => (s > 32 ? (0, _u64_js_1.rotlBL)(h, l, s) : (0, _u64_js_1.rotlSL)(h, l, s));
57
+ /** `keccakf1600` internal function, additionally allows to adjust round count. */
58
+ function keccakP(s, rounds = 24) {
59
+ const B = new Uint32Array(5 * 2);
60
+ // NOTE: all indices are x2 since we store state as u32 instead of u64 (bigints to slow in js)
61
+ for (let round = 24 - rounds; round < 24; round++) {
62
+ // Theta θ
63
+ for (let x = 0; x < 10; x++)
64
+ B[x] = s[x] ^ s[x + 10] ^ s[x + 20] ^ s[x + 30] ^ s[x + 40];
65
+ for (let x = 0; x < 10; x += 2) {
66
+ const idx1 = (x + 8) % 10;
67
+ const idx0 = (x + 2) % 10;
68
+ const B0 = B[idx0];
69
+ const B1 = B[idx0 + 1];
70
+ const Th = rotlH(B0, B1, 1) ^ B[idx1];
71
+ const Tl = rotlL(B0, B1, 1) ^ B[idx1 + 1];
72
+ for (let y = 0; y < 50; y += 10) {
73
+ s[x + y] ^= Th;
74
+ s[x + y + 1] ^= Tl;
75
+ }
76
+ }
77
+ // Rho (ρ) and Pi (π)
78
+ let curH = s[2];
79
+ let curL = s[3];
80
+ for (let t = 0; t < 24; t++) {
81
+ const shift = SHA3_ROTL[t];
82
+ const Th = rotlH(curH, curL, shift);
83
+ const Tl = rotlL(curH, curL, shift);
84
+ const PI = SHA3_PI[t];
85
+ curH = s[PI];
86
+ curL = s[PI + 1];
87
+ s[PI] = Th;
88
+ s[PI + 1] = Tl;
89
+ }
90
+ // Chi (χ)
63
91
  for (let y = 0; y < 50; y += 10) {
64
- s[x + y] ^= Th;
65
- s[x + y + 1] ^= Tl;
92
+ for (let x = 0; x < 10; x++)
93
+ B[x] = s[y + x];
94
+ for (let x = 0; x < 10; x++)
95
+ s[y + x] ^= ~B[(x + 2) % 10] & B[(x + 4) % 10];
66
96
  }
97
+ // Iota (ι)
98
+ s[0] ^= SHA3_IOTA_H[round];
99
+ s[1] ^= SHA3_IOTA_L[round];
67
100
  }
68
- // Rho (ρ) and Pi (π)
69
- let curH = s[2];
70
- let curL = s[3];
71
- for (let t = 0; t < 24; t++) {
72
- const shift = SHA3_ROTL[t];
73
- const Th = rotlH(curH, curL, shift);
74
- const Tl = rotlL(curH, curL, shift);
75
- const PI = SHA3_PI[t];
76
- curH = s[PI];
77
- curL = s[PI + 1];
78
- s[PI] = Th;
79
- s[PI + 1] = Tl;
101
+ B.fill(0);
102
+ }
103
+ /** Keccak sponge function. */
104
+ class Keccak extends utils_js_1.Hash {
105
+ // NOTE: we accept arguments in bytes instead of bits here.
106
+ constructor(blockLen, suffix, outputLen, enableXOF = false, rounds = 24) {
107
+ super();
108
+ Object.defineProperty(this, "blockLen", {
109
+ enumerable: true,
110
+ configurable: true,
111
+ writable: true,
112
+ value: blockLen
113
+ });
114
+ Object.defineProperty(this, "suffix", {
115
+ enumerable: true,
116
+ configurable: true,
117
+ writable: true,
118
+ value: suffix
119
+ });
120
+ Object.defineProperty(this, "outputLen", {
121
+ enumerable: true,
122
+ configurable: true,
123
+ writable: true,
124
+ value: outputLen
125
+ });
126
+ Object.defineProperty(this, "enableXOF", {
127
+ enumerable: true,
128
+ configurable: true,
129
+ writable: true,
130
+ value: enableXOF
131
+ });
132
+ Object.defineProperty(this, "rounds", {
133
+ enumerable: true,
134
+ configurable: true,
135
+ writable: true,
136
+ value: rounds
137
+ });
138
+ Object.defineProperty(this, "state", {
139
+ enumerable: true,
140
+ configurable: true,
141
+ writable: true,
142
+ value: void 0
143
+ });
144
+ Object.defineProperty(this, "pos", {
145
+ enumerable: true,
146
+ configurable: true,
147
+ writable: true,
148
+ value: 0
149
+ });
150
+ Object.defineProperty(this, "posOut", {
151
+ enumerable: true,
152
+ configurable: true,
153
+ writable: true,
154
+ value: 0
155
+ });
156
+ Object.defineProperty(this, "finished", {
157
+ enumerable: true,
158
+ configurable: true,
159
+ writable: true,
160
+ value: false
161
+ });
162
+ Object.defineProperty(this, "state32", {
163
+ enumerable: true,
164
+ configurable: true,
165
+ writable: true,
166
+ value: void 0
167
+ });
168
+ Object.defineProperty(this, "destroyed", {
169
+ enumerable: true,
170
+ configurable: true,
171
+ writable: true,
172
+ value: false
173
+ });
174
+ // Can be passed from user as dkLen
175
+ (0, _assert_js_1.anumber)(outputLen);
176
+ // 1600 = 5x5 matrix of 64bit. 1600 bits === 200 bytes
177
+ // 0 < blockLen < 200
178
+ if (0 >= this.blockLen || this.blockLen >= 200)
179
+ throw new Error('Sha3 supports only keccak-f1600 function');
180
+ this.state = new Uint8Array(200);
181
+ this.state32 = (0, utils_js_1.u32)(this.state);
80
182
  }
81
- // Chi (χ)
82
- for (let y = 0; y < 50; y += 10) {
83
- for (let x = 0; x < 10; x++)
84
- B[x] = s[y + x];
85
- for (let x = 0; x < 10; x++)
86
- s[y + x] ^= ~B[(x + 2) % 10] & B[(x + 4) % 10];
183
+ keccak() {
184
+ if (!utils_js_1.isLE)
185
+ (0, utils_js_1.byteSwap32)(this.state32);
186
+ keccakP(this.state32, this.rounds);
187
+ if (!utils_js_1.isLE)
188
+ (0, utils_js_1.byteSwap32)(this.state32);
189
+ this.posOut = 0;
190
+ this.pos = 0;
87
191
  }
88
- // Iota (ι)
89
- s[0] ^= SHA3_IOTA_H[round];
90
- s[1] ^= SHA3_IOTA_L[round];
91
- }
92
- B.fill(0);
93
- }
94
- /** Keccak sponge function. */
95
- class Keccak extends utils_js_1.Hash {
96
- // NOTE: we accept arguments in bytes instead of bits here.
97
- constructor(blockLen, suffix, outputLen, enableXOF = false, rounds = 24) {
98
- super();
99
- Object.defineProperty(this, "blockLen", {
100
- enumerable: true,
101
- configurable: true,
102
- writable: true,
103
- value: blockLen
104
- });
105
- Object.defineProperty(this, "suffix", {
106
- enumerable: true,
107
- configurable: true,
108
- writable: true,
109
- value: suffix
110
- });
111
- Object.defineProperty(this, "outputLen", {
112
- enumerable: true,
113
- configurable: true,
114
- writable: true,
115
- value: outputLen
116
- });
117
- Object.defineProperty(this, "enableXOF", {
118
- enumerable: true,
119
- configurable: true,
120
- writable: true,
121
- value: enableXOF
122
- });
123
- Object.defineProperty(this, "rounds", {
124
- enumerable: true,
125
- configurable: true,
126
- writable: true,
127
- value: rounds
128
- });
129
- Object.defineProperty(this, "state", {
130
- enumerable: true,
131
- configurable: true,
132
- writable: true,
133
- value: void 0
134
- });
135
- Object.defineProperty(this, "pos", {
136
- enumerable: true,
137
- configurable: true,
138
- writable: true,
139
- value: 0
140
- });
141
- Object.defineProperty(this, "posOut", {
142
- enumerable: true,
143
- configurable: true,
144
- writable: true,
145
- value: 0
146
- });
147
- Object.defineProperty(this, "finished", {
148
- enumerable: true,
149
- configurable: true,
150
- writable: true,
151
- value: false
152
- });
153
- Object.defineProperty(this, "state32", {
154
- enumerable: true,
155
- configurable: true,
156
- writable: true,
157
- value: void 0
158
- });
159
- Object.defineProperty(this, "destroyed", {
160
- enumerable: true,
161
- configurable: true,
162
- writable: true,
163
- value: false
164
- });
165
- // Can be passed from user as dkLen
166
- (0, _assert_js_1.anumber)(outputLen);
167
- // 1600 = 5x5 matrix of 64bit. 1600 bits === 200 bytes
168
- // 0 < blockLen < 200
169
- if (0 >= this.blockLen || this.blockLen >= 200)
170
- throw new Error('Sha3 supports only keccak-f1600 function');
171
- this.state = new Uint8Array(200);
172
- this.state32 = (0, utils_js_1.u32)(this.state);
173
- }
174
- keccak() {
175
- if (!utils_js_1.isLE)
176
- (0, utils_js_1.byteSwap32)(this.state32);
177
- keccakP(this.state32, this.rounds);
178
- if (!utils_js_1.isLE)
179
- (0, utils_js_1.byteSwap32)(this.state32);
180
- this.posOut = 0;
181
- this.pos = 0;
182
- }
183
- update(data) {
184
- (0, _assert_js_1.aexists)(this);
185
- const { blockLen, state } = this;
186
- data = (0, utils_js_1.toBytes)(data);
187
- const len = data.length;
188
- for (let pos = 0; pos < len;) {
189
- const take = Math.min(blockLen - this.pos, len - pos);
190
- for (let i = 0; i < take; i++)
191
- state[this.pos++] ^= data[pos++];
192
- if (this.pos === blockLen)
193
- this.keccak();
192
+ update(data) {
193
+ (0, _assert_js_1.aexists)(this);
194
+ const { blockLen, state } = this;
195
+ data = (0, utils_js_1.toBytes)(data);
196
+ const len = data.length;
197
+ for (let pos = 0; pos < len;) {
198
+ const take = Math.min(blockLen - this.pos, len - pos);
199
+ for (let i = 0; i < take; i++)
200
+ state[this.pos++] ^= data[pos++];
201
+ if (this.pos === blockLen)
202
+ this.keccak();
203
+ }
204
+ return this;
194
205
  }
195
- return this;
196
- }
197
- finish() {
198
- if (this.finished)
199
- return;
200
- this.finished = true;
201
- const { state, suffix, pos, blockLen } = this;
202
- // Do the padding
203
- state[pos] ^= suffix;
204
- if ((suffix & 0x80) !== 0 && pos === blockLen - 1)
205
- this.keccak();
206
- state[blockLen - 1] ^= 0x80;
207
- this.keccak();
208
- }
209
- writeInto(out) {
210
- (0, _assert_js_1.aexists)(this, false);
211
- (0, _assert_js_1.abytes)(out);
212
- this.finish();
213
- const bufferOut = this.state;
214
- const { blockLen } = this;
215
- for (let pos = 0, len = out.length; pos < len;) {
216
- if (this.posOut >= blockLen)
206
+ finish() {
207
+ if (this.finished)
208
+ return;
209
+ this.finished = true;
210
+ const { state, suffix, pos, blockLen } = this;
211
+ // Do the padding
212
+ state[pos] ^= suffix;
213
+ if ((suffix & 0x80) !== 0 && pos === blockLen - 1)
217
214
  this.keccak();
218
- const take = Math.min(blockLen - this.posOut, len - pos);
219
- out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
220
- this.posOut += take;
221
- pos += take;
215
+ state[blockLen - 1] ^= 0x80;
216
+ this.keccak();
217
+ }
218
+ writeInto(out) {
219
+ (0, _assert_js_1.aexists)(this, false);
220
+ (0, _assert_js_1.abytes)(out);
221
+ this.finish();
222
+ const bufferOut = this.state;
223
+ const { blockLen } = this;
224
+ for (let pos = 0, len = out.length; pos < len;) {
225
+ if (this.posOut >= blockLen)
226
+ this.keccak();
227
+ const take = Math.min(blockLen - this.posOut, len - pos);
228
+ out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
229
+ this.posOut += take;
230
+ pos += take;
231
+ }
232
+ return out;
233
+ }
234
+ xofInto(out) {
235
+ // Sha3/Keccak usage with XOF is probably mistake, only SHAKE instances can do XOF
236
+ if (!this.enableXOF)
237
+ throw new Error('XOF is not possible for this instance');
238
+ return this.writeInto(out);
239
+ }
240
+ xof(bytes) {
241
+ (0, _assert_js_1.anumber)(bytes);
242
+ return this.xofInto(new Uint8Array(bytes));
243
+ }
244
+ digestInto(out) {
245
+ (0, _assert_js_1.aoutput)(out, this);
246
+ if (this.finished)
247
+ throw new Error('digest() was already called');
248
+ this.writeInto(out);
249
+ this.destroy();
250
+ return out;
251
+ }
252
+ digest() {
253
+ return this.digestInto(new Uint8Array(this.outputLen));
254
+ }
255
+ destroy() {
256
+ this.destroyed = true;
257
+ this.state.fill(0);
258
+ }
259
+ _cloneInto(to) {
260
+ const { blockLen, suffix, outputLen, rounds, enableXOF } = this;
261
+ to ||= new Keccak(blockLen, suffix, outputLen, enableXOF, rounds);
262
+ to.state32.set(this.state32);
263
+ to.pos = this.pos;
264
+ to.posOut = this.posOut;
265
+ to.finished = this.finished;
266
+ to.rounds = rounds;
267
+ // Suffix can change in cSHAKE
268
+ to.suffix = suffix;
269
+ to.outputLen = outputLen;
270
+ to.enableXOF = enableXOF;
271
+ to.destroyed = this.destroyed;
272
+ return to;
222
273
  }
223
- return out;
224
- }
225
- xofInto(out) {
226
- // Sha3/Keccak usage with XOF is probably mistake, only SHAKE instances can do XOF
227
- if (!this.enableXOF)
228
- throw new Error('XOF is not possible for this instance');
229
- return this.writeInto(out);
230
- }
231
- xof(bytes) {
232
- (0, _assert_js_1.anumber)(bytes);
233
- return this.xofInto(new Uint8Array(bytes));
234
- }
235
- digestInto(out) {
236
- (0, _assert_js_1.aoutput)(out, this);
237
- if (this.finished)
238
- throw new Error('digest() was already called');
239
- this.writeInto(out);
240
- this.destroy();
241
- return out;
242
- }
243
- digest() {
244
- return this.digestInto(new Uint8Array(this.outputLen));
245
- }
246
- destroy() {
247
- this.destroyed = true;
248
- this.state.fill(0);
249
- }
250
- _cloneInto(to) {
251
- const { blockLen, suffix, outputLen, rounds, enableXOF } = this;
252
- to ||= new Keccak(blockLen, suffix, outputLen, enableXOF, rounds);
253
- to.state32.set(this.state32);
254
- to.pos = this.pos;
255
- to.posOut = this.posOut;
256
- to.finished = this.finished;
257
- to.rounds = rounds;
258
- // Suffix can change in cSHAKE
259
- to.suffix = suffix;
260
- to.outputLen = outputLen;
261
- to.enableXOF = enableXOF;
262
- to.destroyed = this.destroyed;
263
- return to;
264
274
  }
265
- }
266
- exports.Keccak = Keccak;
267
- const gen = (suffix, blockLen, outputLen) => (0, utils_js_1.wrapConstructor)(() => new Keccak(blockLen, suffix, outputLen));
268
- /** SHA3-224 hash function. */
269
- exports.sha3_224 = gen(0x06, 144, 224 / 8);
270
- /** SHA3-256 hash function. Different from keccak-256. */
271
- exports.sha3_256 = gen(0x06, 136, 256 / 8);
272
- /** SHA3-384 hash function. */
273
- exports.sha3_384 = gen(0x06, 104, 384 / 8);
274
- /** SHA3-512 hash function. */
275
- exports.sha3_512 = gen(0x06, 72, 512 / 8);
276
- /** keccak-224 hash function. */
277
- exports.keccak_224 = gen(0x01, 144, 224 / 8);
278
- /** keccak-256 hash function. Different from SHA3-256. */
279
- exports.keccak_256 = gen(0x01, 136, 256 / 8);
280
- /** keccak-384 hash function. */
281
- exports.keccak_384 = gen(0x01, 104, 384 / 8);
282
- /** keccak-512 hash function. */
283
- exports.keccak_512 = gen(0x01, 72, 512 / 8);
284
- const genShake = (suffix, blockLen, outputLen) => (0, utils_js_1.wrapXOFConstructorWithOpts)((opts = {}) => new Keccak(blockLen, suffix, opts.dkLen === undefined ? outputLen : opts.dkLen, true));
285
- /** SHAKE128 XOF with 128-bit security. */
286
- exports.shake128 = genShake(0x1f, 168, 128 / 8);
287
- /** SHAKE256 XOF with 256-bit security. */
288
- exports.shake256 = genShake(0x1f, 136, 256 / 8);
275
+ exports.Keccak = Keccak;
276
+ const gen = (suffix, blockLen, outputLen) => (0, utils_js_1.wrapConstructor)(() => new Keccak(blockLen, suffix, outputLen));
277
+ /** SHA3-224 hash function. */
278
+ exports.sha3_224 = gen(0x06, 144, 224 / 8);
279
+ /** SHA3-256 hash function. Different from keccak-256. */
280
+ exports.sha3_256 = gen(0x06, 136, 256 / 8);
281
+ /** SHA3-384 hash function. */
282
+ exports.sha3_384 = gen(0x06, 104, 384 / 8);
283
+ /** SHA3-512 hash function. */
284
+ exports.sha3_512 = gen(0x06, 72, 512 / 8);
285
+ /** keccak-224 hash function. */
286
+ exports.keccak_224 = gen(0x01, 144, 224 / 8);
287
+ /** keccak-256 hash function. Different from SHA3-256. */
288
+ exports.keccak_256 = gen(0x01, 136, 256 / 8);
289
+ /** keccak-384 hash function. */
290
+ exports.keccak_384 = gen(0x01, 104, 384 / 8);
291
+ /** keccak-512 hash function. */
292
+ exports.keccak_512 = gen(0x01, 72, 512 / 8);
293
+ const genShake = (suffix, blockLen, outputLen) => (0, utils_js_1.wrapXOFConstructorWithOpts)((opts = {}) => new Keccak(blockLen, suffix, opts.dkLen === undefined ? outputLen : opts.dkLen, true));
294
+ /** SHAKE128 XOF with 128-bit security. */
295
+ exports.shake128 = genShake(0x1f, 168, 128 / 8);
296
+ /** SHAKE256 XOF with 256-bit security. */
297
+ exports.shake256 = genShake(0x1f, 136, 256 / 8);
298
+ });