@nktkas/hyperliquid 0.13.0
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.
- package/CONTRIBUTING.md +59 -0
- package/LICENSE +21 -0
- package/README.md +363 -0
- package/SECURITY.md +7 -0
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +2 -0
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +1 -0
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +95 -0
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +1 -0
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +10 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.d.ts +23 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.js +43 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.d.ts +55 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.js +65 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.d.ts +2 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.js +1 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.d.ts +52 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.js +283 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/utils.d.ts +120 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/utils.d.ts.map +1 -0
- package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/utils.js +211 -0
- package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts +19 -0
- package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.js +32 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.d.ts +2 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.js +26 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts +37 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.js +109 -0
- package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts +37 -0
- package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.js +237 -0
- package/esm/mod.d.ts +24 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +9 -0
- package/esm/package.json +3 -0
- package/esm/src/clients/event.d.ts +374 -0
- package/esm/src/clients/event.d.ts.map +1 -0
- package/esm/src/clients/event.js +490 -0
- package/esm/src/clients/public.d.ts +695 -0
- package/esm/src/clients/public.d.ts.map +1 -0
- package/esm/src/clients/public.js +704 -0
- package/esm/src/clients/wallet.d.ts +682 -0
- package/esm/src/clients/wallet.d.ts.map +1 -0
- package/esm/src/clients/wallet.js +984 -0
- package/esm/src/transports/base.d.ts +55 -0
- package/esm/src/transports/base.d.ts.map +1 -0
- package/esm/src/transports/base.js +14 -0
- package/esm/src/transports/http/http_transport.d.ts +78 -0
- package/esm/src/transports/http/http_transport.d.ts.map +1 -0
- package/esm/src/transports/http/http_transport.js +170 -0
- package/esm/src/transports/websocket/hyperliquid_event_target.d.ts +66 -0
- package/esm/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -0
- package/esm/src/transports/websocket/hyperliquid_event_target.js +33 -0
- package/esm/src/transports/websocket/reconnecting_websocket.d.ts +160 -0
- package/esm/src/transports/websocket/reconnecting_websocket.d.ts.map +1 -0
- package/esm/src/transports/websocket/reconnecting_websocket.js +370 -0
- package/esm/src/transports/websocket/websocket_request_dispatcher.d.ts +63 -0
- package/esm/src/transports/websocket/websocket_request_dispatcher.d.ts.map +1 -0
- package/esm/src/transports/websocket/websocket_request_dispatcher.js +201 -0
- package/esm/src/transports/websocket/websocket_transport.d.ts +117 -0
- package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -0
- package/esm/src/transports/websocket/websocket_transport.js +233 -0
- package/esm/src/utils/key_sort.d.ts +21 -0
- package/esm/src/utils/key_sort.d.ts.map +1 -0
- package/esm/src/utils/key_sort.js +124 -0
- package/esm/src/utils/signing.d.ts +109 -0
- package/esm/src/utils/signing.d.ts.map +1 -0
- package/esm/src/utils/signing.js +164 -0
- package/package.json +34 -0
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +2 -0
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +1 -0
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +17 -0
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +95 -0
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +1 -0
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +14 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.d.ts +23 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.js +49 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.d.ts +55 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.js +88 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.d.ts +2 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.js +4 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.d.ts +52 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.js +288 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.d.ts +120 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.d.ts.map +1 -0
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.js +235 -0
- package/script/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts +19 -0
- package/script/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/bytes/1.0.4/concat.js +35 -0
- package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.d.ts +2 -0
- package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.js +29 -0
- package/script/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts +37 -0
- package/script/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/encoding/1.0.6/hex.js +113 -0
- package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts +37 -0
- package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.js +240 -0
- package/script/mod.d.ts +24 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +27 -0
- package/script/package.json +3 -0
- package/script/src/clients/event.d.ts +374 -0
- package/script/src/clients/event.d.ts.map +1 -0
- package/script/src/clients/event.js +494 -0
- package/script/src/clients/public.d.ts +695 -0
- package/script/src/clients/public.d.ts.map +1 -0
- package/script/src/clients/public.js +708 -0
- package/script/src/clients/wallet.d.ts +682 -0
- package/script/src/clients/wallet.d.ts.map +1 -0
- package/script/src/clients/wallet.js +989 -0
- package/script/src/transports/base.d.ts +55 -0
- package/script/src/transports/base.d.ts.map +1 -0
- package/script/src/transports/base.js +18 -0
- package/script/src/transports/http/http_transport.d.ts +78 -0
- package/script/src/transports/http/http_transport.d.ts.map +1 -0
- package/script/src/transports/http/http_transport.js +175 -0
- package/script/src/transports/websocket/hyperliquid_event_target.d.ts +66 -0
- package/script/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -0
- package/script/src/transports/websocket/hyperliquid_event_target.js +37 -0
- package/script/src/transports/websocket/reconnecting_websocket.d.ts +160 -0
- package/script/src/transports/websocket/reconnecting_websocket.d.ts.map +1 -0
- package/script/src/transports/websocket/reconnecting_websocket.js +374 -0
- package/script/src/transports/websocket/websocket_request_dispatcher.d.ts +63 -0
- package/script/src/transports/websocket/websocket_request_dispatcher.d.ts.map +1 -0
- package/script/src/transports/websocket/websocket_request_dispatcher.js +206 -0
- package/script/src/transports/websocket/websocket_transport.d.ts +117 -0
- package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -0
- package/script/src/transports/websocket/websocket_transport.js +237 -0
- package/script/src/utils/key_sort.d.ts +21 -0
- package/script/src/utils/key_sort.d.ts.map +1 -0
- package/script/src/utils/key_sort.js +127 -0
- package/script/src/utils/signing.d.ts +109 -0
- package/script/src/utils/signing.d.ts.map +1 -0
- package/script/src/utils/signing.js +172 -0
|
@@ -0,0 +1,288 @@
|
|
|
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);
|
|
41
|
+
}
|
|
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];
|
|
63
|
+
for (let y = 0; y < 50; y += 10) {
|
|
64
|
+
s[x + y] ^= Th;
|
|
65
|
+
s[x + y + 1] ^= Tl;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
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;
|
|
80
|
+
}
|
|
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];
|
|
87
|
+
}
|
|
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();
|
|
194
|
+
}
|
|
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)
|
|
217
|
+
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;
|
|
222
|
+
}
|
|
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
|
+
}
|
|
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);
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utilities for hex, bytes, CSPRNG.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
6
|
+
export declare function isBytes(a: unknown): a is Uint8Array;
|
|
7
|
+
export type TypedArray = Int8Array | Uint8ClampedArray | Uint8Array | Uint16Array | Int16Array | Uint32Array | Int32Array;
|
|
8
|
+
export declare function u8(arr: TypedArray): Uint8Array;
|
|
9
|
+
export declare function u32(arr: TypedArray): Uint32Array;
|
|
10
|
+
export declare function createView(arr: TypedArray): DataView;
|
|
11
|
+
/** The rotate right (circular right shift) operation for uint32 */
|
|
12
|
+
export declare function rotr(word: number, shift: number): number;
|
|
13
|
+
/** The rotate left (circular left shift) operation for uint32 */
|
|
14
|
+
export declare function rotl(word: number, shift: number): number;
|
|
15
|
+
/** Is current platform little-endian? Most are. Big-Endian platform: IBM */
|
|
16
|
+
export declare const isLE: boolean;
|
|
17
|
+
export declare function byteSwap(word: number): number;
|
|
18
|
+
/** Conditionally byte swap if on a big-endian platform */
|
|
19
|
+
export declare const byteSwapIfBE: (n: number) => number;
|
|
20
|
+
/** In place byte swap for Uint32Array */
|
|
21
|
+
export declare function byteSwap32(arr: Uint32Array): void;
|
|
22
|
+
/**
|
|
23
|
+
* Convert byte array to hex string.
|
|
24
|
+
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
|
25
|
+
*/
|
|
26
|
+
export declare function bytesToHex(bytes: Uint8Array): string;
|
|
27
|
+
/**
|
|
28
|
+
* Convert hex string to byte array.
|
|
29
|
+
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
|
30
|
+
*/
|
|
31
|
+
export declare function hexToBytes(hex: string): Uint8Array;
|
|
32
|
+
/**
|
|
33
|
+
* There is no setImmediate in browser and setTimeout is slow.
|
|
34
|
+
* Call of async fn will return Promise, which will be fullfiled only on
|
|
35
|
+
* next scheduler queue processing step and this is exactly what we need.
|
|
36
|
+
*/
|
|
37
|
+
export declare const nextTick: () => Promise<void>;
|
|
38
|
+
/** Returns control to thread each 'tick' ms to avoid blocking. */
|
|
39
|
+
export declare function asyncLoop(iters: number, tick: number, cb: (i: number) => void): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Convert JS string to byte array.
|
|
42
|
+
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
|
43
|
+
*/
|
|
44
|
+
export declare function utf8ToBytes(str: string): Uint8Array;
|
|
45
|
+
/** Accepted input of hash functions. Strings are converted to byte arrays. */
|
|
46
|
+
export type Input = Uint8Array | string;
|
|
47
|
+
/**
|
|
48
|
+
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
|
49
|
+
* Warning: when Uint8Array is passed, it would NOT get copied.
|
|
50
|
+
* Keep in mind for future mutable operations.
|
|
51
|
+
*/
|
|
52
|
+
export declare function toBytes(data: Input): Uint8Array;
|
|
53
|
+
/**
|
|
54
|
+
* Copies several Uint8Arrays into one.
|
|
55
|
+
*/
|
|
56
|
+
export declare function concatBytes(...arrays: Uint8Array[]): Uint8Array;
|
|
57
|
+
/** For runtime check if class implements interface */
|
|
58
|
+
export declare abstract class Hash<T extends Hash<T>> {
|
|
59
|
+
abstract blockLen: number;
|
|
60
|
+
abstract outputLen: number;
|
|
61
|
+
abstract update(buf: Input): this;
|
|
62
|
+
abstract digestInto(buf: Uint8Array): void;
|
|
63
|
+
abstract digest(): Uint8Array;
|
|
64
|
+
/**
|
|
65
|
+
* Resets internal state. Makes Hash instance unusable.
|
|
66
|
+
* Reset is impossible for keyed hashes if key is consumed into state. If digest is not consumed
|
|
67
|
+
* by user, they will need to manually call `destroy()` when zeroing is necessary.
|
|
68
|
+
*/
|
|
69
|
+
abstract destroy(): void;
|
|
70
|
+
/**
|
|
71
|
+
* Clones hash instance. Unsafe: doesn't check whether `to` is valid. Can be used as `clone()`
|
|
72
|
+
* when no options are passed.
|
|
73
|
+
* Reasons to use `_cloneInto` instead of clone: 1) performance 2) reuse instance => all internal
|
|
74
|
+
* buffers are overwritten => causes buffer overwrite which is used for digest in some cases.
|
|
75
|
+
* There are no guarantees for clean-up because it's impossible in JS.
|
|
76
|
+
*/
|
|
77
|
+
abstract _cloneInto(to?: T): T;
|
|
78
|
+
clone(): T;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* XOF: streaming API to read digest in chunks.
|
|
82
|
+
* Same as 'squeeze' in keccak/k12 and 'seek' in blake3, but more generic name.
|
|
83
|
+
* When hash used in XOF mode it is up to user to call '.destroy' afterwards, since we cannot
|
|
84
|
+
* destroy state, next call can require more bytes.
|
|
85
|
+
*/
|
|
86
|
+
export type HashXOF<T extends Hash<T>> = Hash<T> & {
|
|
87
|
+
xof(bytes: number): Uint8Array;
|
|
88
|
+
xofInto(buf: Uint8Array): Uint8Array;
|
|
89
|
+
};
|
|
90
|
+
type EmptyObj = {};
|
|
91
|
+
export declare function checkOpts<T1 extends EmptyObj, T2 extends EmptyObj>(defaults: T1, opts?: T2): T1 & T2;
|
|
92
|
+
/** Hash function */
|
|
93
|
+
export type CHash = ReturnType<typeof wrapConstructor>;
|
|
94
|
+
/** Hash function with output */
|
|
95
|
+
export type CHashO = ReturnType<typeof wrapConstructorWithOpts>;
|
|
96
|
+
/** XOF with output */
|
|
97
|
+
export type CHashXO = ReturnType<typeof wrapXOFConstructorWithOpts>;
|
|
98
|
+
/** Wraps hash function, creating an interface on top of it */
|
|
99
|
+
export declare function wrapConstructor<T extends Hash<T>>(hashCons: () => Hash<T>): {
|
|
100
|
+
(msg: Input): Uint8Array;
|
|
101
|
+
outputLen: number;
|
|
102
|
+
blockLen: number;
|
|
103
|
+
create(): Hash<T>;
|
|
104
|
+
};
|
|
105
|
+
export declare function wrapConstructorWithOpts<H extends Hash<H>, T extends Object>(hashCons: (opts?: T) => Hash<H>): {
|
|
106
|
+
(msg: Input, opts?: T): Uint8Array;
|
|
107
|
+
outputLen: number;
|
|
108
|
+
blockLen: number;
|
|
109
|
+
create(opts: T): Hash<H>;
|
|
110
|
+
};
|
|
111
|
+
export declare function wrapXOFConstructorWithOpts<H extends HashXOF<H>, T extends Object>(hashCons: (opts?: T) => HashXOF<H>): {
|
|
112
|
+
(msg: Input, opts?: T): Uint8Array;
|
|
113
|
+
outputLen: number;
|
|
114
|
+
blockLen: number;
|
|
115
|
+
create(opts: T): HashXOF<H>;
|
|
116
|
+
};
|
|
117
|
+
/** Cryptographically secure PRNG. Uses internal OS-level `crypto.getRandomValues`. */
|
|
118
|
+
export declare function randomBytes(bytesLength?: number): Uint8Array;
|
|
119
|
+
export {};
|
|
120
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../../../../../src/deps/jsr.io/@noble/hashes/1.7.1/src/utils.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Utilities for hex, bytes, CSPRNG.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.Hash = exports.nextTick = exports.byteSwapIfBE = exports.isLE = void 0;
|
|
9
|
+
exports.isBytes = isBytes;
|
|
10
|
+
exports.u8 = u8;
|
|
11
|
+
exports.u32 = u32;
|
|
12
|
+
exports.createView = createView;
|
|
13
|
+
exports.rotr = rotr;
|
|
14
|
+
exports.rotl = rotl;
|
|
15
|
+
exports.byteSwap = byteSwap;
|
|
16
|
+
exports.byteSwap32 = byteSwap32;
|
|
17
|
+
exports.bytesToHex = bytesToHex;
|
|
18
|
+
exports.hexToBytes = hexToBytes;
|
|
19
|
+
exports.asyncLoop = asyncLoop;
|
|
20
|
+
exports.utf8ToBytes = utf8ToBytes;
|
|
21
|
+
exports.toBytes = toBytes;
|
|
22
|
+
exports.concatBytes = concatBytes;
|
|
23
|
+
exports.checkOpts = checkOpts;
|
|
24
|
+
exports.wrapConstructor = wrapConstructor;
|
|
25
|
+
exports.wrapConstructorWithOpts = wrapConstructorWithOpts;
|
|
26
|
+
exports.wrapXOFConstructorWithOpts = wrapXOFConstructorWithOpts;
|
|
27
|
+
exports.randomBytes = randomBytes;
|
|
28
|
+
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
|
|
29
|
+
// node.js versions earlier than v19 don't declare it in global scope.
|
|
30
|
+
// For node.js, package.json#exports field mapping rewrites import
|
|
31
|
+
// from `crypto` to `cryptoNode`, which imports native module.
|
|
32
|
+
// Makes the utils un-importable in browsers without a bundler.
|
|
33
|
+
// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
|
|
34
|
+
const crypto_js_1 = require("./crypto.js");
|
|
35
|
+
const _assert_js_1 = require("./_assert.js");
|
|
36
|
+
// export { isBytes } from './_assert.js';
|
|
37
|
+
// We can't reuse isBytes from _assert, because somehow this causes huge perf issues
|
|
38
|
+
function isBytes(a) {
|
|
39
|
+
return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
|
|
40
|
+
}
|
|
41
|
+
// Cast array to different type
|
|
42
|
+
function u8(arr) {
|
|
43
|
+
return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
44
|
+
}
|
|
45
|
+
function u32(arr) {
|
|
46
|
+
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
47
|
+
}
|
|
48
|
+
// Cast array to view
|
|
49
|
+
function createView(arr) {
|
|
50
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
51
|
+
}
|
|
52
|
+
/** The rotate right (circular right shift) operation for uint32 */
|
|
53
|
+
function rotr(word, shift) {
|
|
54
|
+
return (word << (32 - shift)) | (word >>> shift);
|
|
55
|
+
}
|
|
56
|
+
/** The rotate left (circular left shift) operation for uint32 */
|
|
57
|
+
function rotl(word, shift) {
|
|
58
|
+
return (word << shift) | ((word >>> (32 - shift)) >>> 0);
|
|
59
|
+
}
|
|
60
|
+
/** Is current platform little-endian? Most are. Big-Endian platform: IBM */
|
|
61
|
+
exports.isLE = (() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
|
|
62
|
+
// The byte swap operation for uint32
|
|
63
|
+
function byteSwap(word) {
|
|
64
|
+
return (((word << 24) & 0xff000000) |
|
|
65
|
+
((word << 8) & 0xff0000) |
|
|
66
|
+
((word >>> 8) & 0xff00) |
|
|
67
|
+
((word >>> 24) & 0xff));
|
|
68
|
+
}
|
|
69
|
+
/** Conditionally byte swap if on a big-endian platform */
|
|
70
|
+
exports.byteSwapIfBE = exports.isLE
|
|
71
|
+
? (n) => n
|
|
72
|
+
: (n) => byteSwap(n);
|
|
73
|
+
/** In place byte swap for Uint32Array */
|
|
74
|
+
function byteSwap32(arr) {
|
|
75
|
+
for (let i = 0; i < arr.length; i++) {
|
|
76
|
+
arr[i] = byteSwap(arr[i]);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// Array where index 0xf0 (240) is mapped to string 'f0'
|
|
80
|
+
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
|
|
81
|
+
/**
|
|
82
|
+
* Convert byte array to hex string.
|
|
83
|
+
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
|
84
|
+
*/
|
|
85
|
+
function bytesToHex(bytes) {
|
|
86
|
+
(0, _assert_js_1.abytes)(bytes);
|
|
87
|
+
// pre-caching improves the speed 6x
|
|
88
|
+
let hex = '';
|
|
89
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
90
|
+
hex += hexes[bytes[i]];
|
|
91
|
+
}
|
|
92
|
+
return hex;
|
|
93
|
+
}
|
|
94
|
+
// We use optimized technique to convert hex string to byte array
|
|
95
|
+
const asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
96
|
+
function asciiToBase16(ch) {
|
|
97
|
+
if (ch >= asciis._0 && ch <= asciis._9)
|
|
98
|
+
return ch - asciis._0; // '2' => 50-48
|
|
99
|
+
if (ch >= asciis.A && ch <= asciis.F)
|
|
100
|
+
return ch - (asciis.A - 10); // 'B' => 66-(65-10)
|
|
101
|
+
if (ch >= asciis.a && ch <= asciis.f)
|
|
102
|
+
return ch - (asciis.a - 10); // 'b' => 98-(97-10)
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Convert hex string to byte array.
|
|
107
|
+
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
|
108
|
+
*/
|
|
109
|
+
function hexToBytes(hex) {
|
|
110
|
+
if (typeof hex !== 'string')
|
|
111
|
+
throw new Error('hex string expected, got ' + typeof hex);
|
|
112
|
+
const hl = hex.length;
|
|
113
|
+
const al = hl / 2;
|
|
114
|
+
if (hl % 2)
|
|
115
|
+
throw new Error('hex string expected, got unpadded hex of length ' + hl);
|
|
116
|
+
const array = new Uint8Array(al);
|
|
117
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
118
|
+
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
119
|
+
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
120
|
+
if (n1 === undefined || n2 === undefined) {
|
|
121
|
+
const char = hex[hi] + hex[hi + 1];
|
|
122
|
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
123
|
+
}
|
|
124
|
+
array[ai] = n1 * 16 + n2; // multiply first octet, e.g. 'a3' => 10*16+3 => 160 + 3 => 163
|
|
125
|
+
}
|
|
126
|
+
return array;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* There is no setImmediate in browser and setTimeout is slow.
|
|
130
|
+
* Call of async fn will return Promise, which will be fullfiled only on
|
|
131
|
+
* next scheduler queue processing step and this is exactly what we need.
|
|
132
|
+
*/
|
|
133
|
+
const nextTick = async () => { };
|
|
134
|
+
exports.nextTick = nextTick;
|
|
135
|
+
/** Returns control to thread each 'tick' ms to avoid blocking. */
|
|
136
|
+
async function asyncLoop(iters, tick, cb) {
|
|
137
|
+
let ts = Date.now();
|
|
138
|
+
for (let i = 0; i < iters; i++) {
|
|
139
|
+
cb(i);
|
|
140
|
+
// Date.now() is not monotonic, so in case if clock goes backwards we return return control too
|
|
141
|
+
const diff = Date.now() - ts;
|
|
142
|
+
if (diff >= 0 && diff < tick)
|
|
143
|
+
continue;
|
|
144
|
+
await (0, exports.nextTick)();
|
|
145
|
+
ts += diff;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Convert JS string to byte array.
|
|
150
|
+
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
|
151
|
+
*/
|
|
152
|
+
function utf8ToBytes(str) {
|
|
153
|
+
if (typeof str !== 'string')
|
|
154
|
+
throw new Error('utf8ToBytes expected string, got ' + typeof str);
|
|
155
|
+
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
|
159
|
+
* Warning: when Uint8Array is passed, it would NOT get copied.
|
|
160
|
+
* Keep in mind for future mutable operations.
|
|
161
|
+
*/
|
|
162
|
+
function toBytes(data) {
|
|
163
|
+
if (typeof data === 'string')
|
|
164
|
+
data = utf8ToBytes(data);
|
|
165
|
+
(0, _assert_js_1.abytes)(data);
|
|
166
|
+
return data;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Copies several Uint8Arrays into one.
|
|
170
|
+
*/
|
|
171
|
+
function concatBytes(...arrays) {
|
|
172
|
+
let sum = 0;
|
|
173
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
174
|
+
const a = arrays[i];
|
|
175
|
+
(0, _assert_js_1.abytes)(a);
|
|
176
|
+
sum += a.length;
|
|
177
|
+
}
|
|
178
|
+
const res = new Uint8Array(sum);
|
|
179
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
180
|
+
const a = arrays[i];
|
|
181
|
+
res.set(a, pad);
|
|
182
|
+
pad += a.length;
|
|
183
|
+
}
|
|
184
|
+
return res;
|
|
185
|
+
}
|
|
186
|
+
/** For runtime check if class implements interface */
|
|
187
|
+
class Hash {
|
|
188
|
+
// Safe version that clones internal state
|
|
189
|
+
clone() {
|
|
190
|
+
return this._cloneInto();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
exports.Hash = Hash;
|
|
194
|
+
function checkOpts(defaults, opts) {
|
|
195
|
+
if (opts !== undefined && {}.toString.call(opts) !== '[object Object]')
|
|
196
|
+
throw new Error('Options should be object or undefined');
|
|
197
|
+
const merged = Object.assign(defaults, opts);
|
|
198
|
+
return merged;
|
|
199
|
+
}
|
|
200
|
+
/** Wraps hash function, creating an interface on top of it */
|
|
201
|
+
function wrapConstructor(hashCons) {
|
|
202
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
203
|
+
const tmp = hashCons();
|
|
204
|
+
hashC.outputLen = tmp.outputLen;
|
|
205
|
+
hashC.blockLen = tmp.blockLen;
|
|
206
|
+
hashC.create = () => hashCons();
|
|
207
|
+
return hashC;
|
|
208
|
+
}
|
|
209
|
+
function wrapConstructorWithOpts(hashCons) {
|
|
210
|
+
const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();
|
|
211
|
+
const tmp = hashCons({});
|
|
212
|
+
hashC.outputLen = tmp.outputLen;
|
|
213
|
+
hashC.blockLen = tmp.blockLen;
|
|
214
|
+
hashC.create = (opts) => hashCons(opts);
|
|
215
|
+
return hashC;
|
|
216
|
+
}
|
|
217
|
+
function wrapXOFConstructorWithOpts(hashCons) {
|
|
218
|
+
const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();
|
|
219
|
+
const tmp = hashCons({});
|
|
220
|
+
hashC.outputLen = tmp.outputLen;
|
|
221
|
+
hashC.blockLen = tmp.blockLen;
|
|
222
|
+
hashC.create = (opts) => hashCons(opts);
|
|
223
|
+
return hashC;
|
|
224
|
+
}
|
|
225
|
+
/** Cryptographically secure PRNG. Uses internal OS-level `crypto.getRandomValues`. */
|
|
226
|
+
function randomBytes(bytesLength = 32) {
|
|
227
|
+
if (crypto_js_1.crypto && typeof crypto_js_1.crypto.getRandomValues === 'function') {
|
|
228
|
+
return crypto_js_1.crypto.getRandomValues(new Uint8Array(bytesLength));
|
|
229
|
+
}
|
|
230
|
+
// Legacy Node.js compatibility
|
|
231
|
+
if (crypto_js_1.crypto && typeof crypto_js_1.crypto.randomBytes === 'function') {
|
|
232
|
+
return crypto_js_1.crypto.randomBytes(bytesLength);
|
|
233
|
+
}
|
|
234
|
+
throw new Error('crypto.getRandomValues must be defined');
|
|
235
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Concatenate an array of byte slices into a single slice.
|
|
3
|
+
*
|
|
4
|
+
* @param buffers Array of byte slices to concatenate.
|
|
5
|
+
* @returns A new byte slice containing all the input slices concatenated.
|
|
6
|
+
*
|
|
7
|
+
* @example Basic usage
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { concat } from "@std/bytes/concat";
|
|
10
|
+
* import { assertEquals } from "@std/assert";
|
|
11
|
+
*
|
|
12
|
+
* const a = new Uint8Array([0, 1, 2]);
|
|
13
|
+
* const b = new Uint8Array([3, 4, 5]);
|
|
14
|
+
*
|
|
15
|
+
* assertEquals(concat([a, b]), new Uint8Array([0, 1, 2, 3, 4, 5]));
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function concat(buffers: Uint8Array[]): Uint8Array;
|
|
19
|
+
//# sourceMappingURL=concat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"concat.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/bytes/1.0.4/concat.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,MAAM,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,UAAU,CAaxD"}
|