@leofcoin/peernet 0.10.7 → 0.11.1

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 (76) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +49 -49
  3. package/coverage/lcov-report/block-navigation.js +8 -0
  4. package/coverage/lcov-report/codec-format-interface.js.html +224 -120
  5. package/coverage/lcov-report/dht-response.js.html +44 -39
  6. package/coverage/lcov-report/index.html +39 -64
  7. package/coverage/lcov-report/sorter.js +26 -0
  8. package/coverage/lcov.info +164 -424
  9. package/dist/browser/peernet.js +101788 -93018
  10. package/dist/commonjs/client-1a1f75e6.js +324 -0
  11. package/dist/commonjs/{codec-6367213c.js → codec-8c8c652f.js} +198 -188
  12. package/dist/commonjs/codec-format-interface.js +169 -152
  13. package/dist/commonjs/codec.js +4 -4
  14. package/dist/commonjs/dht-response.js +13 -13
  15. package/dist/commonjs/dht.js +24 -24
  16. package/dist/commonjs/hash.js +151 -141
  17. package/dist/commonjs/{http-a94c5a81.js → http-7bbac90a.js} +19 -15
  18. package/dist/commonjs/peernet-message.js +13 -13
  19. package/dist/commonjs/peernet.js +1901 -1794
  20. package/dist/commonjs/request.js +13 -13
  21. package/dist/commonjs/response.js +13 -13
  22. package/dist/module/peernet.js +2460 -2346
  23. package/index.html +4 -6
  24. package/package.json +22 -14
  25. package/rollup.config.js +33 -5
  26. package/rollup0.config.js +7 -0
  27. package/src/client.js +75 -75
  28. package/src/codec/codec-format-interface.js +172 -155
  29. package/src/codec/codec.js +124 -114
  30. package/src/codec/codecs.js +79 -79
  31. package/src/dht/dht.js +121 -121
  32. package/src/discovery/peer-discovery.js +75 -75
  33. package/src/handlers/message.js +52 -52
  34. package/src/hash/hash.js +155 -145
  35. package/src/http/client/http-client.js +44 -44
  36. package/src/messages/chat-message.js +14 -14
  37. package/src/messages/data-response.js +14 -14
  38. package/src/messages/data.js +18 -18
  39. package/src/messages/dht-response.js +14 -15
  40. package/src/messages/dht.js +25 -25
  41. package/src/messages/peer-response.js +14 -14
  42. package/src/messages/peer.js +14 -14
  43. package/src/messages/peernet-message.js +14 -14
  44. package/src/messages/ps.js +14 -14
  45. package/src/messages/request.js +14 -14
  46. package/src/messages/response.js +14 -14
  47. package/src/peer.js +67 -67
  48. package/src/peernet.js +612 -697
  49. package/src/proto/chat-message.proto.js +7 -7
  50. package/src/utils/utils.js +78 -78
  51. package/test/codec.js +3 -2
  52. package/test/messages.js +7 -4
  53. package/test.js +11 -4
  54. package/webpack.config.js +35 -0
  55. package/coverage/lcov-report/codec.js.html +0 -677
  56. package/coverage/lcov-report/hash.js.html +0 -551
  57. package/debug.log +0 -3
  58. package/dist/browser/peernet.js.tmp-browserify-14074318104595318069 +0 -0
  59. package/dist/browser/peernet.js.tmp-browserify-45407634493269122267 +0 -0
  60. package/dist/browser/peernet.js.tmp-browserify-53722389064799025427 +0 -0
  61. package/dist/browser/peernet.js.tmp-browserify-96323030449218949300 +0 -0
  62. package/dist/codec/codec-format-interface.js +0 -433
  63. package/dist/codec/codec.js +0 -199
  64. package/dist/commonjs/codec-73adfc0f.js +0 -205
  65. package/dist/commonjs/http-2c603501.js +0 -324
  66. package/dist/commonjs/http-42a6e555.js +0 -324
  67. package/dist/commonjs/http-43f4fafe.js +0 -324
  68. package/dist/commonjs/peernet-message-b6925673.js +0 -32
  69. package/dist/hash/hash.js +0 -340
  70. package/dist/messages/dht-response.js +0 -454
  71. package/dist/messages/dht.js +0 -453
  72. package/dist/messages/peernet.js +0 -456
  73. package/dist/module/http-273664bd.js +0 -317
  74. package/dist/module/http-8fe3c0d7.js +0 -317
  75. package/dist/module/http-c780c991.js +0 -317
  76. package/dist/module/http-f13e0d77.js +0 -317
@@ -1,456 +0,0 @@
1
- 'use strict';
2
-
3
- var protons = require('protons');
4
- var bs32 = require('bs32');
5
- var bs58 = require('bs58');
6
- var isHex = require('is-hex');
7
- var varint = require('varint');
8
- var createKeccakHash = require('keccak');
9
-
10
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
-
12
- var protons__default = /*#__PURE__*/_interopDefaultLegacy(protons);
13
- var bs32__default = /*#__PURE__*/_interopDefaultLegacy(bs32);
14
- var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
15
- var isHex__default = /*#__PURE__*/_interopDefaultLegacy(isHex);
16
- var varint__default = /*#__PURE__*/_interopDefaultLegacy(varint);
17
- var createKeccakHash__default = /*#__PURE__*/_interopDefaultLegacy(createKeccakHash);
18
-
19
- var proto = `
20
- // PeernetMessage
21
- message PeernetMessage {
22
- required bytes data = 1;
23
- required bytes signature = 2;
24
- optional bytes from = 3;
25
- optional bytes to = 4;
26
- optional string id = 5;
27
- }`;
28
-
29
- var codecs = {
30
- // just a hash
31
- 'disco-hash': {
32
- codec: '30',
33
- hashAlg: 'dbl-keccak-512', // ,
34
- // testnet: 'olivia'
35
- },
36
- 'peernet-peer-response': {
37
- codec: '707072',
38
- hashAlg: 'keccak-256',
39
- },
40
- 'peernet-peer': {
41
- codec: '7070',
42
- hashAlg: 'keccak-256',
43
- },
44
- 'peernet-dht': {
45
- codec: '706468',
46
- hashAlg: 'keccak-256',
47
- },
48
- 'peernet-dht-response': {
49
- codec: '706472',
50
- hashAlg: 'keccak-256',
51
- },
52
- // data
53
- 'peernet-data': {
54
- codec: '706461',
55
- hashAlg: 'keccak-256',
56
- },
57
- 'peernet-data-response': {
58
- codec: '70646172',
59
- hashAlg: 'keccak-256',
60
- },
61
- // message
62
- 'peernet-message': {
63
- codec: '706d65',
64
- hashAlg: 'keccak-512',
65
- },
66
- // pubsub
67
- 'peernet-ps': {
68
- codec: '707073',
69
- hashAlg: 'keccak-256',
70
- },
71
- 'peernet-response': {
72
- codec: '7072',
73
- hashAlg: 'keccak-256',
74
- },
75
- 'peernet-request': {
76
- codec: '707271',
77
- hashAlg: 'keccak-256',
78
- },
79
- // normal block
80
- 'leofcoin-block': {
81
- codec: '6c62',
82
- hashAlg: 'dbl-keccak-512', // ,
83
- // testnet: 'olivia'
84
- },
85
- 'leofcoin-tx': {
86
- codec: '6c74',
87
- hashAlg: 'dbl-keccak-512', // ,
88
- // testnet: 'olivia'
89
- },
90
- // itx
91
- 'leofcoin-itx': {
92
- codec: '6c69',
93
- hashAlg: 'keccak-512', // ,
94
- // testnet: 'olivia'
95
- },
96
- // peer reputation
97
- 'leofcoin-pr': {
98
- codec: '6c70',
99
- hashAlg: 'keccak-256', // ,
100
- // testnet: 'olivia'
101
- },
102
- };
103
-
104
- class PeernetCodec {
105
- get codecs() {
106
- return {...globalThis.peernet.codecs, ...codecs}
107
- }
108
- constructor(buffer) {
109
- if (buffer) {
110
- if (Buffer.isBuffer(buffer)) {
111
- const codec = varint__default['default'].decode(buffer);
112
- const name = this.getCodecName(codec);
113
-
114
- if (name) {
115
- this.name = name;
116
- this.encoded = buffer;
117
- this.decode(buffer);
118
- } else {
119
- this.encode(buffer);
120
- }
121
- }
122
- if (typeof buffer === 'string') {
123
- if (this.codecs[buffer]) this.fromName(buffer);
124
- else if (isHex__default['default'](buffer)) this.fromHex(buffer);
125
- else if (bs32__default['default'].test(buffer)) this.fromBs32(buffer);
126
- else this.fromBs58(buffer);
127
- }
128
- if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer);
129
- }
130
- }
131
-
132
- fromEncoded(encoded) {
133
- const codec = varint__default['default'].decode(encoded);
134
- const name = this.getCodecName(codec);
135
- this.name = name;
136
- this.encoded = encoded;
137
- this.decode(encoded);
138
- }
139
-
140
- fromHex(hex) {
141
- this.encoded = Buffer.from(hex, 'hex');
142
- this.decode();
143
- }
144
-
145
- fromBs32(input) {
146
- this.encoded = bs32__default['default'].decode(input);
147
- this.decode();
148
- }
149
-
150
- fromBs58(input) {
151
- this.encoded = bs58__default['default'].decode(input);
152
- this.decode();
153
- }
154
-
155
- getCodec(name) {
156
- return this.codecs[name].codec
157
- }
158
-
159
- getCodecName(codec) {
160
- return Object.keys(this.codecs).reduce((p, c) => {
161
- if (parseInt(Buffer.from(`${this.getCodec(c)}`, 'hex').toString('hex'), 16) === codec) return c;
162
- else return p;
163
- }, undefined)
164
- }
165
-
166
- getHashAlg(name) {
167
- return this.codecs[name].hashAlg
168
- }
169
-
170
- fromCodec(codec) {
171
- this.name = this.getCodecName(codec);
172
- this.hashAlg = this.getHashAlg(this.name);
173
-
174
- this.codec = this.getCodec(this.name);
175
- this.codecBuffer = Buffer.from(varint__default['default'].encode(parseInt(Buffer.from(`${this.codec}`, 'hex').toString('hex'), 16)), 'hex');
176
- }
177
-
178
- fromName(name) {
179
- const codec = this.getCodec(name);
180
- this.name = name;
181
- this.codec = codec;
182
- this.hashAlg = this.getHashAlg(name);
183
- this.codecBuffer = Buffer.from(varint__default['default'].encode(parseInt(Buffer.from(`${codec}`, 'hex').toString('hex'), 16)), 'hex');
184
- }
185
-
186
- toBs32() {
187
- this.encode();
188
- return bs32__default['default'].encode(this.encoded)
189
- }
190
-
191
- toBs58() {
192
- this.encode();
193
- return bs58__default['default'].encode(this.encoded)
194
- }
195
-
196
- toHex() {
197
- return this.encoded.toString('hex')
198
- }
199
-
200
- decode() {
201
- const codec = varint__default['default'].decode(this.encoded);
202
- this.fromCodec(codec);
203
- this.name = this.getCodecName(codec);
204
- }
205
-
206
- encode() {
207
- const codec = Buffer.from(varint__default['default'].encode(parseInt(Buffer.from(`${this.codec}`, 'hex').toString('hex'), 16)), 'hex');
208
- this.encoded = codec;
209
- return this.encoded
210
- }
211
- }
212
-
213
- class DiscoHash {
214
- constructor(buffer, options = {}) {
215
- if (options.name) this.name = options.name;
216
- else this.name = 'disco-hash';
217
- if (options.codecs) this.codecs = options.codecs;
218
- if (buffer) {
219
- if (Buffer.isBuffer(buffer)) {
220
- this.discoCodec = new PeernetCodec(buffer, this.codecs);
221
- const name = this.discoCodec.name;
222
-
223
- if (name) {
224
- this.name = name;
225
- this.decode(buffer);
226
- } else {
227
- this.encode(buffer);
228
- }
229
- }
230
-
231
- if (typeof buffer === 'string') {
232
- if (isHex__default['default'](buffer)) this.fromHex(buffer);
233
- if (bs32__default['default'].test(buffer)) this.fromBs32(buffer);
234
- else this.fromBs58(buffer);
235
- } else if (typeof buffer === 'object') this.fromJSON(buffer);
236
- }
237
- }
238
-
239
- get prefix() {
240
- return Buffer.concat([this.discoCodec.codecBuffer, this.length])
241
- }
242
-
243
- get length() {
244
- return Buffer.from(varint__default['default'].encode(this.size))
245
- }
246
-
247
- get buffer() {
248
- return this.hash
249
- }
250
-
251
- toHex() {
252
- return this.hash.toString('hex')
253
- }
254
-
255
- fromHex(hex) {
256
- return this.decode(Buffer.from(hex, 'hex'))
257
- }
258
-
259
- fromJSON(json) {
260
- return this.encode(Buffer.from(JSON.stringify(json)))
261
- }
262
-
263
- toBs32() {
264
- return bs32__default['default'].encode(this.hash)
265
- }
266
-
267
- fromBs32(bs) {
268
- return this.decode(bs32__default['default'].decode(bs))
269
- }
270
-
271
- toBs58() {
272
- return bs58__default['default'].encode(this.hash)
273
- }
274
-
275
- fromBs58(bs) {
276
- return this.decode(bs58__default['default'].decode(bs))
277
- }
278
-
279
- toString(encoding = 'utf8') {
280
- return this.hash.toString(encoding)
281
- }
282
-
283
- encode(buffer, name) {
284
- if (!this.name && name) this.name = name;
285
- if (!buffer) buffer = this.buffer;
286
- this.discoCodec = new PeernetCodec(this.name, this.codecs);
287
- this.discoCodec.fromName(this.name);
288
- let hashAlg = this.discoCodec.hashAlg;
289
- if (hashAlg.includes('dbl')) {
290
- hashAlg = hashAlg.replace('dbl-', '');
291
- buffer = createKeccakHash__default['default'](hashAlg.replace('-', '')).update(buffer).digest();
292
- }
293
- this.digest = createKeccakHash__default['default'](hashAlg.replace('-', '')).update(buffer).digest();
294
- this.size = this.digest.length;
295
-
296
- this.codec = this.discoCodec.encode();
297
- this.codec = this.discoCodec.codecBuffer;
298
- this.hash = Buffer.concat([
299
- this.prefix,
300
- this.digest,
301
- ]);
302
-
303
- return this.hash
304
- }
305
-
306
- validate(buffer) {
307
- if (Buffer.isBuffer(buffer)) {
308
- const codec = varint__default['default'].decode(buffer);
309
- if (this.codecs[codec]) {
310
- this.decode(buffer);
311
- } else {
312
- this.encode(buffer);
313
- }
314
- }
315
- if (typeof buffer === 'string') {
316
- if (isHex__default['default'](buffer)) this.fromHex(buffer);
317
- if (bs32__default['default'].test(buffer)) this.fromBs32(buffer);
318
- }
319
- if (typeof buffer === 'object') this.fromJSON(buffer);
320
- }
321
-
322
- decode(buffer) {
323
- this.hash = buffer;
324
- const codec = varint__default['default'].decode(buffer);
325
-
326
- this.discoCodec = new PeernetCodec(codec, this.codecs);
327
- // TODO: validate codec
328
- buffer = buffer.slice(varint__default['default'].decode.bytes);
329
- this.size = varint__default['default'].decode(buffer);
330
- this.digest = buffer.slice(varint__default['default'].decode.bytes);
331
- if (this.digest.length !== this.size) {
332
- throw new Error(`hash length inconsistent: 0x${this.hash.toString('hex')}`)
333
- }
334
-
335
- // const discoCodec = new DiscoCodec(codec, this.codecs)
336
-
337
- this.name = this.discoCodec.name;
338
-
339
-
340
- this.size = this.digest.length;
341
-
342
- return {
343
- codec: this.codec,
344
- name: this.name,
345
- size: this.size,
346
- length: this.length,
347
- digest: this.digest,
348
- }
349
- }
350
- }
351
-
352
- class FormatInterface {
353
- constructor(buffer, proto, options = {}) {
354
- this.protoEncode = proto.encode;
355
- this.protoDecode = proto.decode;
356
- if (options.name) this.name = options.name;
357
- this.hashFormat = options.hashFormat || 'bs32';
358
-
359
- if (Buffer.isBuffer(buffer)) {
360
- const codec = new PeernetCodec(buffer);
361
-
362
- if (codec.name) {
363
- this.fromEncoded(buffer);
364
- } else {
365
- this.create(buffer);
366
- }
367
- } else if (typeof buffer === 'string') {
368
- if (isHex__default['default'](buffer)) this.fromHex(buffer);
369
- else if (bs32__default['default'].test(buffer)) this.fromBs32(buffer);
370
- else this.fromBs58(buffer);
371
- } else if (typeof buffer === 'object' && !Array.isArray(buffer)) {
372
- this.create(buffer);
373
- }
374
- }
375
-
376
- get peernetHash() {
377
- return new DiscoHash(this.decoded, {name: this.name})
378
- }
379
-
380
- get hash() {
381
- const upper = this.hashFormat.charAt(0).toUpperCase();
382
- const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
383
- return this.peernetHash[`to${format}`]()
384
- }
385
-
386
- decode() {
387
- let encoded = this.encoded;
388
- const discoCodec = new PeernetCodec(this.encoded.toString('hex'));
389
- encoded = encoded.slice(discoCodec.codecBuffer.length);
390
- this.name = discoCodec.name;
391
- this.decoded = this.protoDecode(encoded);
392
- return this.decoded
393
- }
394
-
395
- encode(decoded) {
396
- if (!decoded) decoded = this.decoded;
397
- const codec = new PeernetCodec(this.name);
398
- this.encoded = Buffer.concat([codec.codecBuffer, this.protoEncode(decoded)]);
399
- return this.encoded
400
- }
401
-
402
- fromEncoded(encoded) {
403
- const codec = new PeernetCodec(encoded);
404
- this.name = codec.name;
405
- this.encoded = encoded;
406
- this.decode();
407
- }
408
-
409
- fromHex(hex) {
410
- this.encoded = Buffer.from(hex, 'hex');
411
- this.decode();
412
- }
413
-
414
- fromBs32(input) {
415
- this.encoded = bs32__default['default'].decode(input);
416
- this.decode();
417
- }
418
-
419
- fromBs58(input) {
420
- this.encoded = bs58__default['default'].decode(input);
421
- this.decode();
422
- }
423
-
424
- toHex() {
425
- if (!this.encoded) this.encode();
426
- return this.encoded.toString('hex')
427
- }
428
-
429
- toBs32() {
430
- if (!this.encoded) this.encode();
431
- return bs32__default['default'].encode(this.encoded)
432
- }
433
-
434
- toBs58() {
435
- if (!this.encoded) this.encode();
436
- return bs58__default['default'].encode(this.encoded)
437
- }
438
-
439
- create(data) {
440
- this.decoded = data;
441
- this.encode(data);
442
- }
443
- }
444
-
445
- class PeernetMessage extends FormatInterface {
446
- get keys() {
447
- return ['data', 'signature', 'from', 'to', 'id']
448
- }
449
-
450
- constructor(buffer) {
451
- const name = 'peernet-message';
452
- super(buffer, protons__default['default'](proto).PeernetMessage, {name});
453
- }
454
- }
455
-
456
- module.exports = PeernetMessage;