@waku/rln 0.1.6-383e0b2.0 → 0.1.6-4c18ca2.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.
Files changed (131) hide show
  1. package/bundle/_virtual/index2.js +1 -1
  2. package/bundle/index.js +2 -1
  3. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/checksum.js +3 -3
  4. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/cipher.js +4 -4
  5. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/class.js +7 -7
  6. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/functional.js +7 -7
  7. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/index.js +6 -6
  8. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/kdf.js +5 -5
  9. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/password.js +1 -1
  10. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/schema-validation-generated.js +1 -1
  11. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/schema-validation.js +2 -2
  12. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/types.js +1 -1
  13. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/_assert.js +1 -1
  14. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/_sha2.js +3 -3
  15. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/_u64.js +1 -1
  16. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/cryptoBrowser.js +1 -1
  17. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/hmac.js +3 -3
  18. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/pbkdf2.js +4 -4
  19. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/scrypt.js +5 -5
  20. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/sha256.js +3 -3
  21. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/sha512.js +4 -4
  22. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/utils.js +2 -2
  23. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/aes.js +3 -3
  24. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/pbkdf2.js +7 -7
  25. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +3 -3
  26. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/scrypt.js +3 -3
  27. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/sha256.js +3 -3
  28. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +7 -7
  29. package/bundle/node_modules/@chainsafe/is-ip/lib/is-ip.js +12 -0
  30. package/bundle/node_modules/@chainsafe/is-ip/lib/parse.js +26 -0
  31. package/bundle/node_modules/@chainsafe/is-ip/lib/parser.js +202 -0
  32. package/bundle/node_modules/@ethersproject/bignumber/lib.esm/bignumber.js +1 -1
  33. package/bundle/node_modules/@ethersproject/keccak256/lib.esm/index.js +1 -1
  34. package/bundle/node_modules/@multiformats/multiaddr/dist/src/constants.js +43 -0
  35. package/bundle/node_modules/@multiformats/multiaddr/dist/src/errors.js +17 -0
  36. package/bundle/node_modules/@multiformats/multiaddr/dist/src/registry.js +245 -0
  37. package/bundle/node_modules/@multiformats/multiaddr/dist/src/utils.js +191 -0
  38. package/bundle/node_modules/@multiformats/multiaddr/dist/src/validation.js +30 -0
  39. package/bundle/node_modules/@noble/hashes/esm/_assert.js +6 -32
  40. package/bundle/node_modules/@noble/hashes/esm/_md.js +22 -11
  41. package/bundle/node_modules/@noble/hashes/esm/_u64.js +4 -3
  42. package/bundle/{packages/rln/node_modules → node_modules}/@noble/hashes/esm/hmac.js +19 -10
  43. package/bundle/{packages/rln/node_modules/@noble/hashes/esm/sha256.js → node_modules/@noble/hashes/esm/sha2.js} +36 -50
  44. package/bundle/node_modules/@noble/hashes/esm/sha256.js +5 -102
  45. package/bundle/node_modules/@noble/hashes/esm/sha3.js +30 -24
  46. package/bundle/node_modules/@noble/hashes/esm/utils.js +69 -18
  47. package/bundle/node_modules/bn.js/lib/bn.js +1 -0
  48. package/bundle/node_modules/debug/src/browser.js +1 -1
  49. package/bundle/node_modules/debug/src/common.js +1 -1
  50. package/bundle/node_modules/ethereum-cryptography/esm/sha256.js +1 -1
  51. package/bundle/node_modules/{@ethersproject/keccak256/node_modules/js-sha3 → js-sha3}/src/sha3.js +2 -2
  52. package/bundle/node_modules/lodash/lodash.js +5 -5
  53. package/bundle/node_modules/multiformats/dist/src/bases/base.js +12 -9
  54. package/bundle/node_modules/multiformats/dist/src/bytes.js +19 -3
  55. package/bundle/node_modules/multiformats/dist/src/cid.js +371 -0
  56. package/bundle/node_modules/multiformats/dist/src/hashes/digest.js +62 -0
  57. package/bundle/node_modules/multiformats/dist/src/varint.js +15 -0
  58. package/bundle/node_modules/multiformats/dist/src/vendor/varint.js +78 -0
  59. package/bundle/node_modules/uint8arrays/dist/src/concat.js +20 -0
  60. package/bundle/node_modules/uint8arrays/dist/src/to-string.js +19 -0
  61. package/bundle/node_modules/uint8arrays/dist/src/util/as-uint8array.js +9 -0
  62. package/bundle/packages/core/dist/lib/connection_manager/connection_manager.js +1 -3
  63. package/bundle/packages/core/dist/lib/message/version_0.js +1 -4
  64. package/bundle/packages/proto/dist/generated/filter.js +2 -0
  65. package/bundle/packages/proto/dist/generated/filter_v2.js +2 -0
  66. package/bundle/packages/proto/dist/generated/light_push.js +2 -0
  67. package/bundle/packages/proto/dist/generated/message.js +2 -0
  68. package/bundle/packages/proto/dist/generated/metadata.js +2 -0
  69. package/bundle/packages/proto/dist/generated/peer_exchange.js +2 -0
  70. package/bundle/packages/proto/dist/generated/sds_message.js +2 -0
  71. package/bundle/packages/proto/dist/generated/store_v3.js +2 -0
  72. package/bundle/packages/proto/dist/generated/topic_only_message.js +2 -0
  73. package/bundle/packages/rln/dist/contract/constants.js +1 -0
  74. package/bundle/packages/rln/dist/contract/rln_base_contract.js +127 -71
  75. package/bundle/packages/rln/dist/credentials_manager.js +3 -3
  76. package/bundle/packages/rln/dist/keystore/cipher.js +3 -3
  77. package/bundle/packages/rln/dist/keystore/keystore.js +1 -1
  78. package/bundle/packages/rln/dist/message.js +11 -0
  79. package/dist/.tsbuildinfo +1 -1
  80. package/dist/codec.test-utils.d.ts +1 -1
  81. package/dist/contract/index.d.ts +1 -0
  82. package/dist/contract/index.js +1 -0
  83. package/dist/contract/index.js.map +1 -1
  84. package/dist/contract/rln_base_contract.d.ts +32 -22
  85. package/dist/contract/rln_base_contract.js +127 -71
  86. package/dist/contract/rln_base_contract.js.map +1 -1
  87. package/dist/contract/test-setup.d.ts +1 -1
  88. package/dist/contract/types.d.ts +5 -0
  89. package/dist/contract/types.js.map +1 -1
  90. package/dist/credentials_manager.js +1 -1
  91. package/dist/credentials_manager.js.map +1 -1
  92. package/dist/index.d.ts +2 -1
  93. package/dist/index.js +1 -0
  94. package/dist/index.js.map +1 -1
  95. package/dist/keystore/credential_validation_generated.d.ts +0 -2
  96. package/dist/keystore/keystore.js.map +1 -1
  97. package/dist/keystore/keystore_validation_generated.d.ts +0 -2
  98. package/dist/keystore/types.d.ts +2 -2
  99. package/dist/message.d.ts +5 -4
  100. package/dist/message.js +2 -0
  101. package/dist/message.js.map +1 -1
  102. package/package.json +1 -1
  103. package/src/contract/index.ts +1 -0
  104. package/src/contract/rln_base_contract.ts +166 -102
  105. package/src/contract/types.ts +5 -0
  106. package/src/credentials_manager.ts +1 -1
  107. package/src/index.ts +3 -1
  108. package/src/keystore/keystore.ts +4 -2
  109. package/src/keystore/types.ts +2 -2
  110. package/src/message.ts +7 -4
  111. package/bundle/node_modules/@multiformats/multiaddr/dist/src/convert.js +0 -15
  112. package/bundle/node_modules/@multiformats/multiaddr/dist/src/multiaddr.js +0 -21
  113. package/bundle/node_modules/@multiformats/multiaddr/dist/src/protocols-table.js +0 -92
  114. package/bundle/packages/rln/node_modules/@noble/hashes/esm/_assert.js +0 -43
  115. package/bundle/packages/rln/node_modules/@noble/hashes/esm/_sha2.js +0 -116
  116. package/bundle/packages/rln/node_modules/@noble/hashes/esm/utils.js +0 -43
  117. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/index.js +0 -0
  118. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/md5.js +0 -0
  119. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/nil.js +0 -0
  120. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/parse.js +0 -0
  121. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/regex.js +0 -0
  122. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/rng.js +0 -0
  123. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/sha1.js +0 -0
  124. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/stringify.js +0 -0
  125. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v1.js +0 -0
  126. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v3.js +0 -0
  127. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v35.js +0 -0
  128. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v4.js +0 -0
  129. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v5.js +0 -0
  130. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/validate.js +0 -0
  131. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/version.js +0 -0
@@ -0,0 +1,191 @@
1
+ import { isIPv4 } from '../../../../@chainsafe/is-ip/lib/is-ip.js';
2
+ import { base32 } from '../../../../multiformats/dist/src/bases/base32.js';
3
+ import { bases } from '../../../../multiformats/dist/src/basics.js';
4
+ import { concat } from '../../../../uint8arrays/dist/src/concat.js';
5
+ import { fromString } from '../../../../uint8arrays/dist/src/from-string.js';
6
+ import { toString } from '../../../../uint8arrays/dist/src/to-string.js';
7
+ import { InvalidMultiaddrError } from './errors.js';
8
+
9
+ function bytesToString(base) {
10
+ return (buf) => {
11
+ return toString(buf, base);
12
+ };
13
+ }
14
+ function stringToBytes(base) {
15
+ return (buf) => {
16
+ return fromString(buf, base);
17
+ };
18
+ }
19
+ function bytes2port(buf) {
20
+ const view = new DataView(buf.buffer);
21
+ return view.getUint16(buf.byteOffset).toString();
22
+ }
23
+ function port2bytes(port) {
24
+ const buf = new ArrayBuffer(2);
25
+ const view = new DataView(buf);
26
+ view.setUint16(0, typeof port === 'string' ? parseInt(port) : port);
27
+ return new Uint8Array(buf);
28
+ }
29
+ function onion2bytes(str) {
30
+ const addr = str.split(':');
31
+ if (addr.length !== 2) {
32
+ throw new Error(`failed to parse onion addr: ["'${addr.join('", "')}'"]' does not contain a port number`);
33
+ }
34
+ if (addr[0].length !== 16) {
35
+ throw new Error(`failed to parse onion addr: ${addr[0]} not a Tor onion address.`);
36
+ }
37
+ // onion addresses do not include the multibase prefix, add it before decoding
38
+ const buf = fromString(addr[0], 'base32');
39
+ // onion port number
40
+ const port = parseInt(addr[1], 10);
41
+ if (port < 1 || port > 65536) {
42
+ throw new Error('Port number is not in range(1, 65536)');
43
+ }
44
+ const portBuf = port2bytes(port);
45
+ return concat([buf, portBuf], buf.length + portBuf.length);
46
+ }
47
+ function onion32bytes(str) {
48
+ const addr = str.split(':');
49
+ if (addr.length !== 2) {
50
+ throw new Error(`failed to parse onion addr: ["'${addr.join('", "')}'"]' does not contain a port number`);
51
+ }
52
+ if (addr[0].length !== 56) {
53
+ throw new Error(`failed to parse onion addr: ${addr[0]} not a Tor onion3 address.`);
54
+ }
55
+ // onion addresses do not include the multibase prefix, add it before decoding
56
+ const buf = base32.decode(`b${addr[0]}`);
57
+ // onion port number
58
+ const port = parseInt(addr[1], 10);
59
+ if (port < 1 || port > 65536) {
60
+ throw new Error('Port number is not in range(1, 65536)');
61
+ }
62
+ const portBuf = port2bytes(port);
63
+ return concat([buf, portBuf], buf.length + portBuf.length);
64
+ }
65
+ function bytes2onion(buf) {
66
+ const addrBytes = buf.subarray(0, buf.length - 2);
67
+ const portBytes = buf.subarray(buf.length - 2);
68
+ const addr = toString(addrBytes, 'base32');
69
+ const port = bytes2port(portBytes);
70
+ return `${addr}:${port}`;
71
+ }
72
+ // Copied from https://github.com/indutny/node-ip/blob/master/lib/ip.js#L7
73
+ // but with buf/offset args removed because we don't use them
74
+ const ip4ToBytes = function (ip) {
75
+ ip = ip.toString().trim();
76
+ const bytes = new Uint8Array(4);
77
+ ip.split(/\./g).forEach((byte, index) => {
78
+ const value = parseInt(byte, 10);
79
+ if (isNaN(value) || value < 0 || value > 0xff) {
80
+ throw new InvalidMultiaddrError('Invalid byte value in IP address');
81
+ }
82
+ bytes[index] = value;
83
+ });
84
+ return bytes;
85
+ };
86
+ // Copied from https://github.com/indutny/node-ip/blob/master/lib/ip.js#L7
87
+ // but with buf/offset args removed because we don't use them
88
+ const ip6ToBytes = function (ip) {
89
+ let offset = 0;
90
+ ip = ip.toString().trim();
91
+ const sections = ip.split(':', 8);
92
+ let i;
93
+ for (i = 0; i < sections.length; i++) {
94
+ const isv4 = isIPv4(sections[i]);
95
+ let v4Buffer;
96
+ if (isv4) {
97
+ v4Buffer = ip4ToBytes(sections[i]);
98
+ sections[i] = toString(v4Buffer.subarray(0, 2), 'base16');
99
+ }
100
+ if (v4Buffer != null && ++i < 8) {
101
+ sections.splice(i, 0, toString(v4Buffer.subarray(2, 4), 'base16'));
102
+ }
103
+ }
104
+ if (sections[0] === '') {
105
+ while (sections.length < 8) {
106
+ sections.unshift('0');
107
+ }
108
+ }
109
+ else if (sections[sections.length - 1] === '') {
110
+ while (sections.length < 8) {
111
+ sections.push('0');
112
+ }
113
+ }
114
+ else if (sections.length < 8) {
115
+ for (i = 0; i < sections.length && sections[i] !== ''; i++) { }
116
+ const argv = [i, 1];
117
+ for (i = 9 - sections.length; i > 0; i--) {
118
+ argv.push('0');
119
+ }
120
+ sections.splice.apply(sections, argv);
121
+ }
122
+ const bytes = new Uint8Array(offset + 16);
123
+ for (i = 0; i < sections.length; i++) {
124
+ if (sections[i] === '') {
125
+ sections[i] = '0';
126
+ }
127
+ const word = parseInt(sections[i], 16);
128
+ if (isNaN(word) || word < 0 || word > 0xffff) {
129
+ throw new InvalidMultiaddrError('Invalid byte value in IP address');
130
+ }
131
+ bytes[offset++] = (word >> 8) & 0xff;
132
+ bytes[offset++] = word & 0xff;
133
+ }
134
+ return bytes;
135
+ };
136
+ // Copied from https://github.com/indutny/node-ip/blob/master/lib/ip.js#L63
137
+ const ip4ToString = function (buf) {
138
+ if (buf.byteLength !== 4) {
139
+ throw new InvalidMultiaddrError('IPv4 address was incorrect length');
140
+ }
141
+ const result = [];
142
+ for (let i = 0; i < buf.byteLength; i++) {
143
+ result.push(buf[i]);
144
+ }
145
+ return result.join('.');
146
+ };
147
+ const ip6ToString = function (buf) {
148
+ if (buf.byteLength !== 16) {
149
+ throw new InvalidMultiaddrError('IPv6 address was incorrect length');
150
+ }
151
+ const result = [];
152
+ for (let i = 0; i < buf.byteLength; i += 2) {
153
+ const byte1 = buf[i];
154
+ const byte2 = buf[i + 1];
155
+ const tuple = `${byte1.toString(16).padStart(2, '0')}${byte2.toString(16).padStart(2, '0')}`;
156
+ result.push(tuple);
157
+ }
158
+ const ip = result.join(':');
159
+ try {
160
+ const url = new URL(`http://[${ip}]`);
161
+ return url.hostname.substring(1, url.hostname.length - 1);
162
+ }
163
+ catch {
164
+ throw new InvalidMultiaddrError(`Invalid IPv6 address "${ip}"`);
165
+ }
166
+ };
167
+ function ip6StringToValue(str) {
168
+ try {
169
+ const url = new URL(`http://[${str}]`);
170
+ return url.hostname.substring(1, url.hostname.length - 1);
171
+ }
172
+ catch {
173
+ throw new InvalidMultiaddrError(`Invalid IPv6 address "${str}"`);
174
+ }
175
+ }
176
+ const decoders = Object.values(bases).map((c) => c.decoder);
177
+ const anybaseDecoder = (function () {
178
+ let acc = decoders[0].or(decoders[1]);
179
+ decoders.slice(2).forEach((d) => (acc = acc.or(d)));
180
+ return acc;
181
+ })();
182
+ function mb2bytes(mbstr) {
183
+ return anybaseDecoder.decode(mbstr);
184
+ }
185
+ function bytes2mb(base) {
186
+ return (buf) => {
187
+ return base.encoder.encode(buf);
188
+ };
189
+ }
190
+
191
+ export { bytes2mb, bytes2onion, bytes2port, bytesToString, ip4ToBytes, ip4ToString, ip6StringToValue, ip6ToBytes, ip6ToString, mb2bytes, onion2bytes, onion32bytes, port2bytes, stringToBytes };
@@ -0,0 +1,30 @@
1
+ import { ValidationError } from './errors.js';
2
+
3
+ function integer(value) {
4
+ const int = parseInt(value);
5
+ if (int.toString() !== value) {
6
+ throw new ValidationError('Value must be an integer');
7
+ }
8
+ }
9
+ function positive(value) {
10
+ if (value < 0) {
11
+ throw new ValidationError('Value must be a positive integer, or zero');
12
+ }
13
+ }
14
+ function maxValue(max) {
15
+ return (value) => {
16
+ if (value > max) {
17
+ throw new ValidationError(`Value must be smaller than or equal to ${max}`);
18
+ }
19
+ };
20
+ }
21
+ function validate(...funcs) {
22
+ return (value) => {
23
+ for (const fn of funcs) {
24
+ fn(value);
25
+ }
26
+ };
27
+ }
28
+ const validatePort = validate(integer, positive, maxValue(65_535));
29
+
30
+ export { integer, maxValue, positive, validate, validatePort };
@@ -1,37 +1,11 @@
1
+ import { abytes as abytes$1 } from './utils.js';
2
+
1
3
  /**
2
4
  * Internal assertion helpers.
3
5
  * @module
6
+ * @deprecated
4
7
  */
5
- /** Asserts something is positive integer. */
6
- function anumber(n) {
7
- if (!Number.isSafeInteger(n) || n < 0)
8
- throw new Error('positive integer expected, got ' + n);
9
- }
10
- /** Is number an Uint8Array? Copied from utils for perf. */
11
- function isBytes(a) {
12
- return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
13
- }
14
- /** Asserts something is Uint8Array. */
15
- function abytes(b, ...lengths) {
16
- if (!isBytes(b))
17
- throw new Error('Uint8Array expected');
18
- if (lengths.length > 0 && !lengths.includes(b.length))
19
- throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
20
- }
21
- /** Asserts a hash instance has not been destroyed / finished */
22
- function aexists(instance, checkFinished = true) {
23
- if (instance.destroyed)
24
- throw new Error('Hash instance has been destroyed');
25
- if (checkFinished && instance.finished)
26
- throw new Error('Hash#digest() has already been called');
27
- }
28
- /** Asserts output is properly-sized byte array */
29
- function aoutput(out, instance) {
30
- abytes(out);
31
- const min = instance.outputLen;
32
- if (out.length < min) {
33
- throw new Error('digestInto() expects output buffer of length at least ' + min);
34
- }
35
- }
8
+ /** @deprecated Use import from `noble/hashes/utils` module */
9
+ const abytes = abytes$1;
36
10
 
37
- export { abytes, aexists, anumber, aoutput };
11
+ export { abytes };
@@ -1,5 +1,4 @@
1
- import { aexists, aoutput } from './_assert.js';
2
- import { Hash, createView, toBytes } from './utils.js';
1
+ import { Hash, createView, aexists, toBytes, abytes, aoutput, clean } from './utils.js';
3
2
 
4
3
  /**
5
4
  * Internal Merkle-Damgard hash utils.
@@ -33,21 +32,22 @@ function Maj(a, b, c) {
33
32
  class HashMD extends Hash {
34
33
  constructor(blockLen, outputLen, padOffset, isLE) {
35
34
  super();
36
- this.blockLen = blockLen;
37
- this.outputLen = outputLen;
38
- this.padOffset = padOffset;
39
- this.isLE = isLE;
40
35
  this.finished = false;
41
36
  this.length = 0;
42
37
  this.pos = 0;
43
38
  this.destroyed = false;
39
+ this.blockLen = blockLen;
40
+ this.outputLen = outputLen;
41
+ this.padOffset = padOffset;
42
+ this.isLE = isLE;
44
43
  this.buffer = new Uint8Array(blockLen);
45
44
  this.view = createView(this.buffer);
46
45
  }
47
46
  update(data) {
48
47
  aexists(this);
49
- const { view, buffer, blockLen } = this;
50
48
  data = toBytes(data);
49
+ abytes(data);
50
+ const { view, buffer, blockLen } = this;
51
51
  const len = data.length;
52
52
  for (let pos = 0; pos < len;) {
53
53
  const take = Math.min(blockLen - this.pos, len - pos);
@@ -81,7 +81,7 @@ class HashMD extends Hash {
81
81
  let { pos } = this;
82
82
  // append the bit '1' to the message
83
83
  buffer[pos++] = 0b10000000;
84
- this.buffer.subarray(pos).fill(0);
84
+ clean(this.buffer.subarray(pos));
85
85
  // we have less than padOffset left in buffer, so we cannot put length in
86
86
  // current block, need process it and pad again
87
87
  if (this.padOffset > blockLen - pos) {
@@ -119,14 +119,25 @@ class HashMD extends Hash {
119
119
  to || (to = new this.constructor());
120
120
  to.set(...this.get());
121
121
  const { blockLen, buffer, length, finished, destroyed, pos } = this;
122
+ to.destroyed = destroyed;
123
+ to.finished = finished;
122
124
  to.length = length;
123
125
  to.pos = pos;
124
- to.finished = finished;
125
- to.destroyed = destroyed;
126
126
  if (length % blockLen)
127
127
  to.buffer.set(buffer);
128
128
  return to;
129
129
  }
130
+ clone() {
131
+ return this._cloneInto();
132
+ }
130
133
  }
134
+ /**
135
+ * Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
136
+ * Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
137
+ */
138
+ /** Initial SHA256 state. Bits 0..32 of frac part of sqrt of primes 2..19 */
139
+ const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
140
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
141
+ ]);
131
142
 
132
- export { Chi, HashMD, Maj, setBigUint64 };
143
+ export { Chi, HashMD, Maj, SHA256_IV, setBigUint64 };
@@ -11,9 +11,10 @@ function fromBig(n, le = false) {
11
11
  return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
12
12
  }
13
13
  function split(lst, le = false) {
14
- let Ah = new Uint32Array(lst.length);
15
- let Al = new Uint32Array(lst.length);
16
- for (let i = 0; i < lst.length; i++) {
14
+ const len = lst.length;
15
+ let Ah = new Uint32Array(len);
16
+ let Al = new Uint32Array(len);
17
+ for (let i = 0; i < len; i++) {
17
18
  const { h, l } = fromBig(lst[i], le);
18
19
  [Ah[i], Al[i]] = [h, l];
19
20
  }
@@ -1,17 +1,19 @@
1
- import assert from './_assert.js';
2
- import { Hash, toBytes } from './utils.js';
1
+ import { Hash, ahash, toBytes, clean, aexists, abytes } from './utils.js';
3
2
 
4
- // HMAC (RFC 2104)
3
+ /**
4
+ * HMAC: RFC2104 message authentication code.
5
+ * @module
6
+ */
5
7
  class HMAC extends Hash {
6
8
  constructor(hash, _key) {
7
9
  super();
8
10
  this.finished = false;
9
11
  this.destroyed = false;
10
- assert.hash(hash);
12
+ ahash(hash);
11
13
  const key = toBytes(_key);
12
14
  this.iHash = hash.create();
13
15
  if (typeof this.iHash.update !== 'function')
14
- throw new TypeError('Expected instance of class which extends utils.Hash');
16
+ throw new Error('Expected instance of class which extends utils.Hash');
15
17
  this.blockLen = this.iHash.blockLen;
16
18
  this.outputLen = this.iHash.outputLen;
17
19
  const blockLen = this.blockLen;
@@ -27,16 +29,16 @@ class HMAC extends Hash {
27
29
  for (let i = 0; i < pad.length; i++)
28
30
  pad[i] ^= 0x36 ^ 0x5c;
29
31
  this.oHash.update(pad);
30
- pad.fill(0);
32
+ clean(pad);
31
33
  }
32
34
  update(buf) {
33
- assert.exists(this);
35
+ aexists(this);
34
36
  this.iHash.update(buf);
35
37
  return this;
36
38
  }
37
39
  digestInto(out) {
38
- assert.exists(this);
39
- assert.bytes(out, this.outputLen);
40
+ aexists(this);
41
+ abytes(out, this.outputLen);
40
42
  this.finished = true;
41
43
  this.iHash.digestInto(out);
42
44
  this.oHash.update(out);
@@ -61,6 +63,9 @@ class HMAC extends Hash {
61
63
  to.iHash = iHash._cloneInto(to.iHash);
62
64
  return to;
63
65
  }
66
+ clone() {
67
+ return this._cloneInto();
68
+ }
64
69
  destroy() {
65
70
  this.destroyed = true;
66
71
  this.oHash.destroy();
@@ -72,8 +77,12 @@ class HMAC extends Hash {
72
77
  * @param hash - function that would be used e.g. sha256
73
78
  * @param key - message key
74
79
  * @param message - message data
80
+ * @example
81
+ * import { hmac } from '@noble/hashes/hmac';
82
+ * import { sha256 } from '@noble/hashes/sha2';
83
+ * const mac1 = hmac(sha256, 'key', 'message');
75
84
  */
76
85
  const hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
77
86
  hmac.create = (hash, key) => new HMAC(hash, key);
78
87
 
79
- export { hmac };
88
+ export { HMAC, hmac };
@@ -1,14 +1,19 @@
1
- import { SHA2 } from './_sha2.js';
2
- import { wrapConstructor, rotr } from './utils.js';
1
+ import { HashMD, SHA256_IV, Chi, Maj } from './_md.js';
2
+ import { createHasher, clean, rotr } from './utils.js';
3
3
 
4
- // Choice: a ? b : c
5
- const Chi = (a, b, c) => (a & b) ^ (~a & c);
6
- // Majority function, true if any two inpust is true
7
- const Maj = (a, b, c) => (a & b) ^ (a & c) ^ (b & c);
8
- // Round constants:
9
- // first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
4
+ /**
5
+ * SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
6
+ * SHA256 is the fastest hash implementable in JS, even faster than Blake3.
7
+ * Check out [RFC 4634](https://datatracker.ietf.org/doc/html/rfc4634) and
8
+ * [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
9
+ * @module
10
+ */
11
+ /**
12
+ * Round constants:
13
+ * First 32 bits of fractional parts of the cube roots of the first 64 primes 2..311)
14
+ */
10
15
  // prettier-ignore
11
- const SHA256_K = new Uint32Array([
16
+ const SHA256_K = /* @__PURE__ */ Uint32Array.from([
12
17
  0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
13
18
  0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
14
19
  0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
@@ -18,27 +23,21 @@ const SHA256_K = new Uint32Array([
18
23
  0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
19
24
  0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
20
25
  ]);
21
- // Initial state (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
22
- // prettier-ignore
23
- const IV = new Uint32Array([
24
- 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
25
- ]);
26
- // Temporary buffer, not used to store anything between runs
27
- // Named this way because it matches specification.
28
- const SHA256_W = new Uint32Array(64);
29
- class SHA256 extends SHA2 {
30
- constructor() {
31
- super(64, 32, 8, false);
26
+ /** Reusable temporary buffer. "W" comes straight from spec. */
27
+ const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
28
+ class SHA256 extends HashMD {
29
+ constructor(outputLen = 32) {
30
+ super(64, outputLen, 8, false);
32
31
  // We cannot use array here since array allows indexing by variable
33
32
  // which means optimizer/compiler cannot use registers.
34
- this.A = IV[0] | 0;
35
- this.B = IV[1] | 0;
36
- this.C = IV[2] | 0;
37
- this.D = IV[3] | 0;
38
- this.E = IV[4] | 0;
39
- this.F = IV[5] | 0;
40
- this.G = IV[6] | 0;
41
- this.H = IV[7] | 0;
33
+ this.A = SHA256_IV[0] | 0;
34
+ this.B = SHA256_IV[1] | 0;
35
+ this.C = SHA256_IV[2] | 0;
36
+ this.D = SHA256_IV[3] | 0;
37
+ this.E = SHA256_IV[4] | 0;
38
+ this.F = SHA256_IV[5] | 0;
39
+ this.G = SHA256_IV[6] | 0;
40
+ this.H = SHA256_IV[7] | 0;
42
41
  }
43
42
  get() {
44
43
  const { A, B, C, D, E, F, G, H } = this;
@@ -94,33 +93,20 @@ class SHA256 extends SHA2 {
94
93
  this.set(A, B, C, D, E, F, G, H);
95
94
  }
96
95
  roundClean() {
97
- SHA256_W.fill(0);
96
+ clean(SHA256_W);
98
97
  }
99
98
  destroy() {
100
99
  this.set(0, 0, 0, 0, 0, 0, 0, 0);
101
- this.buffer.fill(0);
102
- }
103
- }
104
- // Constants from https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
105
- class SHA224 extends SHA256 {
106
- constructor() {
107
- super();
108
- this.A = 0xc1059ed8 | 0;
109
- this.B = 0x367cd507 | 0;
110
- this.C = 0x3070dd17 | 0;
111
- this.D = 0xf70e5939 | 0;
112
- this.E = 0xffc00b31 | 0;
113
- this.F = 0x68581511 | 0;
114
- this.G = 0x64f98fa7 | 0;
115
- this.H = 0xbefa4fa4 | 0;
116
- this.outputLen = 28;
100
+ clean(this.buffer);
117
101
  }
118
102
  }
119
103
  /**
120
- * SHA2-256 hash function
121
- * @param message - data that would be hashed
104
+ * SHA2-256 hash function from RFC 4634.
105
+ *
106
+ * It is the fastest JS hash, even faster than Blake3.
107
+ * To break sha256 using birthday attack, attackers need to try 2^128 hashes.
108
+ * BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
122
109
  */
123
- const sha256 = wrapConstructor(() => new SHA256());
124
- wrapConstructor(() => new SHA224());
110
+ const sha256 = /* @__PURE__ */ createHasher(() => new SHA256());
125
111
 
126
- export { sha256 };
112
+ export { SHA256, sha256 };
@@ -1,5 +1,4 @@
1
- import { HashMD, Chi, Maj } from './_md.js';
2
- import { wrapConstructor, rotr } from './utils.js';
1
+ import { sha256 as sha256$1 } from './sha2.js';
3
2
 
4
3
  /**
5
4
  * SHA2-256 a.k.a. sha256. In JS, it is the fastest hash, even faster than Blake3.
@@ -9,105 +8,9 @@ import { wrapConstructor, rotr } from './utils.js';
9
8
  *
10
9
  * Check out [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
11
10
  * @module
11
+ * @deprecated
12
12
  */
13
- /** Round constants: first 32 bits of fractional parts of the cube roots of the first 64 primes 2..311). */
14
- // prettier-ignore
15
- const SHA256_K = /* @__PURE__ */ new Uint32Array([
16
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
17
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
18
- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
19
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
20
- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
21
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
22
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
23
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
24
- ]);
25
- /** Initial state: first 32 bits of fractional parts of the square roots of the first 8 primes 2..19. */
26
- // prettier-ignore
27
- const SHA256_IV = /* @__PURE__ */ new Uint32Array([
28
- 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
29
- ]);
30
- /**
31
- * Temporary buffer, not used to store anything between runs.
32
- * Named this way because it matches specification.
33
- */
34
- const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
35
- class SHA256 extends HashMD {
36
- constructor() {
37
- super(64, 32, 8, false);
38
- // We cannot use array here since array allows indexing by variable
39
- // which means optimizer/compiler cannot use registers.
40
- this.A = SHA256_IV[0] | 0;
41
- this.B = SHA256_IV[1] | 0;
42
- this.C = SHA256_IV[2] | 0;
43
- this.D = SHA256_IV[3] | 0;
44
- this.E = SHA256_IV[4] | 0;
45
- this.F = SHA256_IV[5] | 0;
46
- this.G = SHA256_IV[6] | 0;
47
- this.H = SHA256_IV[7] | 0;
48
- }
49
- get() {
50
- const { A, B, C, D, E, F, G, H } = this;
51
- return [A, B, C, D, E, F, G, H];
52
- }
53
- // prettier-ignore
54
- set(A, B, C, D, E, F, G, H) {
55
- this.A = A | 0;
56
- this.B = B | 0;
57
- this.C = C | 0;
58
- this.D = D | 0;
59
- this.E = E | 0;
60
- this.F = F | 0;
61
- this.G = G | 0;
62
- this.H = H | 0;
63
- }
64
- process(view, offset) {
65
- // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
66
- for (let i = 0; i < 16; i++, offset += 4)
67
- SHA256_W[i] = view.getUint32(offset, false);
68
- for (let i = 16; i < 64; i++) {
69
- const W15 = SHA256_W[i - 15];
70
- const W2 = SHA256_W[i - 2];
71
- const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
72
- const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
73
- SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
74
- }
75
- // Compression function main loop, 64 rounds
76
- let { A, B, C, D, E, F, G, H } = this;
77
- for (let i = 0; i < 64; i++) {
78
- const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
79
- const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
80
- const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
81
- const T2 = (sigma0 + Maj(A, B, C)) | 0;
82
- H = G;
83
- G = F;
84
- F = E;
85
- E = (D + T1) | 0;
86
- D = C;
87
- C = B;
88
- B = A;
89
- A = (T1 + T2) | 0;
90
- }
91
- // Add the compressed chunk to the current hash value
92
- A = (A + this.A) | 0;
93
- B = (B + this.B) | 0;
94
- C = (C + this.C) | 0;
95
- D = (D + this.D) | 0;
96
- E = (E + this.E) | 0;
97
- F = (F + this.F) | 0;
98
- G = (G + this.G) | 0;
99
- H = (H + this.H) | 0;
100
- this.set(A, B, C, D, E, F, G, H);
101
- }
102
- roundClean() {
103
- SHA256_W.fill(0);
104
- }
105
- destroy() {
106
- this.set(0, 0, 0, 0, 0, 0, 0, 0);
107
- this.buffer.fill(0);
108
- }
109
- }
110
- /** SHA2-256 hash function */
111
- const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
13
+ /** @deprecated Use import from `noble/hashes/sha2` module */
14
+ const sha256 = sha256$1;
112
15
 
113
- export { SHA256, sha256 };
16
+ export { sha256 };