@leofcoin/peernet 0.10.8 → 0.11.2

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