@waku/rln 0.1.8-e224c05.0 → 0.1.8-e800af3.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/bundle/_virtual/utils.js +2 -2
- package/bundle/_virtual/utils2.js +2 -2
- package/bundle/index.js +6 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/_sha2.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/hmac.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/pbkdf2.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/scrypt.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/sha256.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/sha512.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/utils.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +2 -2
- package/bundle/node_modules/@chainsafe/is-ip/lib/is-ip.js +12 -0
- package/bundle/node_modules/@chainsafe/is-ip/lib/parse.js +26 -0
- package/bundle/node_modules/@chainsafe/is-ip/lib/parser.js +202 -0
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/constants.js +43 -0
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/errors.js +17 -0
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/registry.js +245 -0
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/utils.js +191 -0
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/validation.js +30 -0
- package/bundle/node_modules/@noble/hashes/esm/hmac.js +88 -0
- package/bundle/node_modules/@noble/hashes/esm/sha3.js +1 -1
- package/bundle/node_modules/@noble/hashes/esm/utils.js +8 -1
- package/bundle/node_modules/@waku/zerokit-rln-wasm/rln_wasm.js +517 -255
- package/bundle/node_modules/it-length-prefixed/dist/src/decode.js +6 -0
- package/bundle/node_modules/multiformats/dist/src/bases/base10.js +3 -1
- package/bundle/node_modules/multiformats/dist/src/bases/base16.js +4 -2
- package/bundle/node_modules/multiformats/dist/src/bases/base2.js +3 -1
- package/bundle/node_modules/multiformats/dist/src/bases/base256emoji.js +3 -1
- package/bundle/node_modules/multiformats/dist/src/bases/base32.js +11 -9
- package/bundle/node_modules/multiformats/dist/src/bases/base36.js +4 -2
- package/bundle/node_modules/multiformats/dist/src/bases/base58.js +4 -2
- package/bundle/node_modules/multiformats/dist/src/bases/base64.js +6 -4
- package/bundle/node_modules/multiformats/dist/src/bases/base8.js +3 -1
- package/bundle/node_modules/multiformats/dist/src/bases/identity.js +3 -1
- package/bundle/node_modules/multiformats/dist/src/basics.js +15 -0
- package/bundle/node_modules/multiformats/dist/src/bytes.js +15 -1
- package/bundle/node_modules/multiformats/dist/src/cid.js +371 -0
- package/bundle/node_modules/multiformats/dist/src/hashes/digest.js +62 -0
- package/bundle/node_modules/multiformats/dist/src/varint.js +15 -0
- package/bundle/node_modules/multiformats/dist/src/vendor/varint.js +78 -0
- package/bundle/node_modules/protons-runtime/dist/src/codec.js +20 -0
- package/bundle/node_modules/protons-runtime/dist/src/codecs/enum.js +24 -0
- package/bundle/node_modules/protons-runtime/dist/src/codecs/message.js +7 -0
- package/bundle/node_modules/protons-runtime/dist/src/decode.js +8 -0
- package/bundle/node_modules/protons-runtime/dist/src/encode.js +11 -0
- package/bundle/node_modules/protons-runtime/dist/src/index.js +30 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/float.js +54 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/longbits.js +175 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/pool.js +28 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/reader.js +367 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/utf8.js +99 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/writer.js +438 -0
- package/bundle/node_modules/uint8-varint/dist/src/index.js +124 -0
- package/bundle/node_modules/uint8arrays/dist/src/alloc.js +17 -0
- package/bundle/node_modules/uint8arrays/dist/src/concat.js +20 -0
- package/bundle/node_modules/uint8arrays/dist/src/from-string.js +19 -0
- package/bundle/node_modules/uint8arrays/dist/src/to-string.js +19 -0
- package/bundle/node_modules/uint8arrays/dist/src/util/as-uint8array.js +9 -0
- package/bundle/node_modules/uint8arrays/dist/src/util/bases.js +49 -0
- package/bundle/packages/core/dist/lib/connection_manager/connection_limiter.js +18 -0
- package/bundle/packages/core/dist/lib/connection_manager/connection_manager.js +24 -0
- package/bundle/packages/core/dist/lib/connection_manager/dialer.js +14 -0
- package/bundle/packages/core/dist/lib/connection_manager/discovery_dialer.js +14 -0
- package/bundle/packages/core/dist/lib/connection_manager/keep_alive_manager.js +15 -0
- package/bundle/packages/core/dist/lib/connection_manager/shard_reader.js +14 -0
- package/bundle/packages/core/dist/lib/filter/filter.js +28 -0
- package/bundle/packages/core/dist/lib/light_push/light_push.js +28 -0
- package/bundle/packages/core/dist/lib/message/version_0.js +172 -0
- package/bundle/packages/core/dist/lib/metadata/metadata.js +28 -0
- package/bundle/packages/core/dist/lib/store/store.js +24 -0
- package/bundle/packages/interfaces/dist/connection_manager.js +9 -0
- package/bundle/packages/interfaces/dist/health_status.js +17 -0
- package/bundle/packages/interfaces/dist/protocols.js +92 -0
- package/bundle/packages/interfaces/dist/waku.js +7 -0
- package/bundle/packages/proto/dist/generated/filter.js +447 -0
- package/bundle/packages/proto/dist/generated/filter_v2.js +426 -0
- package/bundle/packages/proto/dist/generated/light_push.js +550 -0
- package/bundle/packages/proto/dist/generated/message.js +215 -0
- package/bundle/packages/proto/dist/generated/metadata.js +132 -0
- package/bundle/packages/proto/dist/generated/peer_exchange.js +211 -0
- package/bundle/packages/proto/dist/generated/sds_message.js +172 -0
- package/bundle/packages/proto/dist/generated/store_v3.js +492 -0
- package/bundle/packages/proto/dist/generated/topic_only_message.js +63 -0
- package/bundle/packages/rln/dist/codec.js +92 -0
- package/bundle/packages/rln/dist/contract/constants.js +14 -7
- package/bundle/packages/rln/dist/contract/rln_base_contract.js +2 -1
- package/bundle/packages/rln/dist/contract/rln_contract.js +109 -0
- package/bundle/packages/rln/dist/credentials_manager.js +45 -4
- package/bundle/packages/rln/dist/identity.js +2 -1
- package/bundle/packages/rln/dist/keystore/keystore.js +30 -9
- package/bundle/packages/rln/dist/message.js +59 -0
- package/bundle/packages/rln/dist/proof.js +54 -0
- package/bundle/packages/rln/dist/resources/verification_key.js +112 -0
- package/bundle/packages/rln/dist/resources/witness_calculator.js +1 -1
- package/bundle/packages/rln/dist/rln.js +36 -4
- package/bundle/packages/rln/dist/root_tracker.js +76 -0
- package/bundle/packages/rln/dist/utils/bytes.js +70 -31
- package/bundle/packages/rln/dist/utils/epoch.js +23 -1
- package/bundle/packages/rln/dist/utils/hash.js +10 -0
- package/bundle/packages/rln/dist/zerokit.js +99 -2
- package/bundle/packages/utils/dist/bytes/index.js +31 -0
- package/bundle/resources/rln.wasm +0 -0
- package/bundle/resources/rln_final.zkey +0 -0
- package/bundle/resources/verification_key.d.ts +13 -0
- package/bundle/resources/verification_key.js +112 -0
- package/bundle/resources/witness_calculator.d.ts +7 -21
- package/bundle/resources/witness_calculator.js +1 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/codec.d.ts +40 -0
- package/dist/codec.js +79 -0
- package/dist/codec.js.map +1 -0
- package/dist/codec.test-utils.d.ts +37 -0
- package/dist/codec.test-utils.js +61 -0
- package/dist/codec.test-utils.js.map +1 -0
- package/dist/contract/constants.d.ts +10 -3
- package/dist/contract/constants.js +13 -6
- package/dist/contract/constants.js.map +1 -1
- package/dist/contract/index.d.ts +1 -0
- package/dist/contract/index.js +1 -0
- package/dist/contract/index.js.map +1 -1
- package/dist/contract/rln_base_contract.js +2 -1
- package/dist/contract/rln_base_contract.js.map +1 -1
- package/dist/contract/rln_contract.d.ts +17 -0
- package/dist/contract/rln_contract.js +107 -0
- package/dist/contract/rln_contract.js.map +1 -0
- package/dist/contract/test_setup.d.ts +26 -0
- package/dist/contract/test_setup.js +56 -0
- package/dist/contract/test_setup.js.map +1 -0
- package/dist/contract/test_utils.d.ts +39 -0
- package/dist/contract/test_utils.js +118 -0
- package/dist/contract/test_utils.js.map +1 -0
- package/dist/credentials_manager.d.ts +14 -2
- package/dist/credentials_manager.js +45 -4
- package/dist/credentials_manager.js.map +1 -1
- package/dist/identity.js +2 -1
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/keystore/keystore.js +30 -9
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/message.d.ts +19 -0
- package/dist/message.js +51 -0
- package/dist/message.js.map +1 -0
- package/dist/proof.d.ts +21 -0
- package/dist/proof.js +50 -0
- package/dist/proof.js.map +1 -0
- package/dist/resources/rln.wasm +0 -0
- package/dist/resources/rln_final.zkey +0 -0
- package/dist/resources/verification_key.d.ts +13 -0
- package/dist/resources/verification_key.js +112 -0
- package/dist/resources/witness_calculator.d.ts +7 -21
- package/dist/resources/witness_calculator.js +1 -1
- package/dist/rln.d.ts +9 -0
- package/dist/rln.js +32 -4
- package/dist/rln.js.map +1 -1
- package/dist/root_tracker.d.ts +10 -0
- package/dist/root_tracker.js +75 -0
- package/dist/root_tracker.js.map +1 -0
- package/dist/utils/bytes.d.ts +31 -9
- package/dist/utils/bytes.js +70 -31
- package/dist/utils/bytes.js.map +1 -1
- package/dist/zerokit.d.ts +11 -0
- package/dist/zerokit.js +97 -1
- package/dist/zerokit.js.map +1 -1
- package/package.json +1 -1
- package/src/codec.test-utils.ts +88 -0
- package/src/codec.ts +138 -0
- package/src/contract/constants.ts +16 -6
- package/src/contract/index.ts +1 -0
- package/src/contract/rln_base_contract.ts +2 -1
- package/src/contract/rln_contract.ts +147 -0
- package/src/contract/test_setup.ts +86 -0
- package/src/contract/test_utils.ts +179 -0
- package/src/credentials_manager.ts +72 -8
- package/src/identity.ts +2 -1
- package/src/index.ts +11 -1
- package/src/keystore/keystore.ts +32 -9
- package/src/message.ts +73 -0
- package/src/proof.ts +69 -0
- package/src/resources/verification_key.d.ts +13 -0
- package/src/resources/witness_calculator.d.ts +7 -21
- package/src/rln.ts +65 -5
- package/src/root_tracker.ts +92 -0
- package/src/utils/bytes.ts +73 -36
- package/src/zerokit.ts +217 -1
|
@@ -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 };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { Hash, ahash, toBytes, clean, aexists, abytes } from './utils.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* HMAC: RFC2104 message authentication code.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
class HMAC extends Hash {
|
|
8
|
+
constructor(hash, _key) {
|
|
9
|
+
super();
|
|
10
|
+
this.finished = false;
|
|
11
|
+
this.destroyed = false;
|
|
12
|
+
ahash(hash);
|
|
13
|
+
const key = toBytes(_key);
|
|
14
|
+
this.iHash = hash.create();
|
|
15
|
+
if (typeof this.iHash.update !== 'function')
|
|
16
|
+
throw new Error('Expected instance of class which extends utils.Hash');
|
|
17
|
+
this.blockLen = this.iHash.blockLen;
|
|
18
|
+
this.outputLen = this.iHash.outputLen;
|
|
19
|
+
const blockLen = this.blockLen;
|
|
20
|
+
const pad = new Uint8Array(blockLen);
|
|
21
|
+
// blockLen can be bigger than outputLen
|
|
22
|
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
23
|
+
for (let i = 0; i < pad.length; i++)
|
|
24
|
+
pad[i] ^= 0x36;
|
|
25
|
+
this.iHash.update(pad);
|
|
26
|
+
// By doing update (processing of first block) of outer hash here we can re-use it between multiple calls via clone
|
|
27
|
+
this.oHash = hash.create();
|
|
28
|
+
// Undo internal XOR && apply outer XOR
|
|
29
|
+
for (let i = 0; i < pad.length; i++)
|
|
30
|
+
pad[i] ^= 0x36 ^ 0x5c;
|
|
31
|
+
this.oHash.update(pad);
|
|
32
|
+
clean(pad);
|
|
33
|
+
}
|
|
34
|
+
update(buf) {
|
|
35
|
+
aexists(this);
|
|
36
|
+
this.iHash.update(buf);
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
digestInto(out) {
|
|
40
|
+
aexists(this);
|
|
41
|
+
abytes(out, this.outputLen);
|
|
42
|
+
this.finished = true;
|
|
43
|
+
this.iHash.digestInto(out);
|
|
44
|
+
this.oHash.update(out);
|
|
45
|
+
this.oHash.digestInto(out);
|
|
46
|
+
this.destroy();
|
|
47
|
+
}
|
|
48
|
+
digest() {
|
|
49
|
+
const out = new Uint8Array(this.oHash.outputLen);
|
|
50
|
+
this.digestInto(out);
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
_cloneInto(to) {
|
|
54
|
+
// Create new instance without calling constructor since key already in state and we don't know it.
|
|
55
|
+
to || (to = Object.create(Object.getPrototypeOf(this), {}));
|
|
56
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
57
|
+
to = to;
|
|
58
|
+
to.finished = finished;
|
|
59
|
+
to.destroyed = destroyed;
|
|
60
|
+
to.blockLen = blockLen;
|
|
61
|
+
to.outputLen = outputLen;
|
|
62
|
+
to.oHash = oHash._cloneInto(to.oHash);
|
|
63
|
+
to.iHash = iHash._cloneInto(to.iHash);
|
|
64
|
+
return to;
|
|
65
|
+
}
|
|
66
|
+
clone() {
|
|
67
|
+
return this._cloneInto();
|
|
68
|
+
}
|
|
69
|
+
destroy() {
|
|
70
|
+
this.destroyed = true;
|
|
71
|
+
this.oHash.destroy();
|
|
72
|
+
this.iHash.destroy();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* HMAC: RFC2104 message authentication code.
|
|
77
|
+
* @param hash - function that would be used e.g. sha256
|
|
78
|
+
* @param key - message key
|
|
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');
|
|
84
|
+
*/
|
|
85
|
+
const hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
|
|
86
|
+
hmac.create = (hash, key) => new HMAC(hash, key);
|
|
87
|
+
|
|
88
|
+
export { HMAC, hmac };
|
|
@@ -25,6 +25,13 @@ function abytes(b, ...lengths) {
|
|
|
25
25
|
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
26
26
|
throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
|
|
27
27
|
}
|
|
28
|
+
/** Asserts something is hash */
|
|
29
|
+
function ahash(h) {
|
|
30
|
+
if (typeof h !== 'function' || typeof h.create !== 'function')
|
|
31
|
+
throw new Error('Hash should be wrapped by utils.createHasher');
|
|
32
|
+
anumber(h.outputLen);
|
|
33
|
+
anumber(h.blockLen);
|
|
34
|
+
}
|
|
28
35
|
/** Asserts a hash instance has not been destroyed / finished */
|
|
29
36
|
function aexists(instance, checkFinished = true) {
|
|
30
37
|
if (instance.destroyed)
|
|
@@ -185,4 +192,4 @@ function createHasher(hashCons) {
|
|
|
185
192
|
return hashC;
|
|
186
193
|
}
|
|
187
194
|
|
|
188
|
-
export { Hash, abytes, aexists, anumber, aoutput, byteSwap, byteSwap32, bytesToHex, clean, concatBytes, createHasher, createView, hexToBytes, isBytes, isLE, rotr, swap32IfBE, toBytes, u32, utf8ToBytes };
|
|
195
|
+
export { Hash, abytes, aexists, ahash, anumber, aoutput, byteSwap, byteSwap32, bytesToHex, clean, concatBytes, createHasher, createView, hexToBytes, isBytes, isLE, rotr, swap32IfBE, toBytes, u32, utf8ToBytes };
|