@leofcoin/peernet 0.11.22 → 0.11.25
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/dist/browser/peernet.js +12700 -5856
- package/dist/commonjs/dht-response.js +2 -9
- package/dist/commonjs/dht.js +2 -9
- package/dist/commonjs/{http-4e34d4a5.js → http-78686629.js} +1 -1
- package/dist/commonjs/peernet-message.js +2 -9
- package/dist/commonjs/peernet.js +78 -49
- package/dist/commonjs/request.js +2 -9
- package/dist/commonjs/response.js +2 -9
- package/dist/module/peernet.js +68 -571
- package/package.json +3 -2
- package/rollup.config.js +2 -2
- package/src/handlers/message.js +1 -1
- package/src/messages/chat-message.js +2 -2
- package/src/messages/data-response.js +2 -2
- package/src/messages/data.js +2 -2
- package/src/messages/dht-response.js +2 -2
- package/src/messages/dht.js +2 -2
- package/src/messages/peer-response.js +2 -2
- package/src/messages/peer.js +2 -2
- package/src/messages/peernet-message.js +2 -2
- package/src/messages/ps.js +2 -2
- package/src/messages/request.js +2 -2
- package/src/messages/response.js +2 -2
- package/src/peernet.js +1 -2
- package/src/utils/utils.js +2 -1
- package/dist/commonjs/codec-45796010.js +0 -215
- package/dist/commonjs/codec-format-interface.js +0 -206
- package/dist/commonjs/codec.js +0 -11
- package/dist/commonjs/hash.js +0 -164
- package/src/codec/codec-format-interface.js +0 -194
- package/src/codec/codec.js +0 -124
- package/src/codec/codecs.js +0 -79
- package/src/hash/hash.js +0 -152
package/dist/module/peernet.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
import '@vandeurenglenn/debug';
|
|
2
|
+
import pako from 'pako';
|
|
2
3
|
import LeofcoinStorage from '@leofcoin/storage';
|
|
3
4
|
import protons from 'protons';
|
|
4
|
-
import
|
|
5
|
-
import bs58 from '@vandeurenglenn/base58';
|
|
6
|
-
import isHex from '@vandeurenglenn/is-hex';
|
|
7
|
-
import varint from 'varint';
|
|
8
|
-
import createKeccakHash from 'keccak';
|
|
5
|
+
import { FormatInterface, Codec, CodecHash, codecs } from '@leofcoin/codec-format-interface';
|
|
9
6
|
import MultiWallet$1 from '@leofcoin/multi-wallet';
|
|
10
7
|
import * as bs58check from 'bs58check';
|
|
11
8
|
import bs58check__default from 'bs58check';
|
|
12
9
|
import * as bip32 from 'bip32';
|
|
10
|
+
import createKeccakHash from 'keccak';
|
|
13
11
|
import ecc from 'tiny-secp256k1';
|
|
14
12
|
import Mnemonic from '@leofcoin/mnemonic';
|
|
15
13
|
import MultiSignature from 'multi-signature';
|
|
14
|
+
import varint from 'varint';
|
|
16
15
|
import randombytes from 'randombytes';
|
|
17
16
|
|
|
18
17
|
/* socket-request-client version 1.6.3 */
|
|
@@ -241,11 +240,8 @@ const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry
|
|
|
241
240
|
});
|
|
242
241
|
};
|
|
243
242
|
|
|
244
|
-
const messageQue = {};
|
|
245
|
-
|
|
246
243
|
class Peer {
|
|
247
244
|
#connection
|
|
248
|
-
#ready = false
|
|
249
245
|
#connecting = false
|
|
250
246
|
#connected = false
|
|
251
247
|
#channelReady = false
|
|
@@ -258,10 +254,14 @@ class Peer {
|
|
|
258
254
|
#remoteStreams = []
|
|
259
255
|
#pendingCandidates = []
|
|
260
256
|
#senderMap = new Map()
|
|
257
|
+
#messageQue = []
|
|
258
|
+
#chunksQue = {}
|
|
261
259
|
#iceCompleteTimer
|
|
262
260
|
#channel
|
|
263
261
|
#peerId
|
|
264
|
-
#chunkSize = 16384
|
|
262
|
+
#chunkSize = 16 * 1024 // 16384
|
|
263
|
+
#queRunning = false
|
|
264
|
+
#MAX_BUFFERED_AMOUNT = 16 * 1024 * 1024
|
|
265
265
|
|
|
266
266
|
get connection() {
|
|
267
267
|
return this.#connection
|
|
@@ -315,6 +315,7 @@ class Peer {
|
|
|
315
315
|
|
|
316
316
|
splitMessage(message) {
|
|
317
317
|
const chunks = [];
|
|
318
|
+
message = pako.deflate(message);
|
|
318
319
|
const size = message.byteLength || message.length;
|
|
319
320
|
let offset = 0;
|
|
320
321
|
return new Promise((resolve, reject) => {
|
|
@@ -330,27 +331,53 @@ class Peer {
|
|
|
330
331
|
})
|
|
331
332
|
}
|
|
332
333
|
|
|
333
|
-
async
|
|
334
|
-
|
|
334
|
+
async #runQue() {
|
|
335
|
+
this.#queRunning = true;
|
|
336
|
+
if (this.#messageQue.length > 0 && this.channel.bufferedAmount + this.#messageQue[0]?.length < this.#MAX_BUFFERED_AMOUNT) {
|
|
337
|
+
const message = this.#messageQue.shift();
|
|
338
|
+
|
|
339
|
+
switch (this.channel?.readyState) {
|
|
340
|
+
case 'open':
|
|
341
|
+
await this.channel.send(message);
|
|
342
|
+
if (this.#messageQue.length > 0) return this.#runQue()
|
|
343
|
+
else this.#queRunning = false;
|
|
344
|
+
break;
|
|
345
|
+
case 'closed':
|
|
346
|
+
case 'closing':
|
|
347
|
+
this.#messageQue = [];
|
|
348
|
+
this.#queRunning = false;
|
|
349
|
+
debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
|
|
350
|
+
break;
|
|
351
|
+
case undefined:
|
|
352
|
+
this.#messageQue = [];
|
|
353
|
+
this.#queRunning = false;
|
|
354
|
+
debug(`trying to send before a channel is created`);
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
} else {
|
|
360
|
+
return setTimeout(() => this.#runQue(), 50)
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
#trySend({ size, id, chunks }) {
|
|
335
365
|
let offset = 0;
|
|
366
|
+
|
|
336
367
|
for (const chunk of chunks) {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
debug(`trying to send before a channel is created`);
|
|
351
|
-
break;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
368
|
+
const start = offset;
|
|
369
|
+
const end = offset + chunk.length;
|
|
370
|
+
|
|
371
|
+
const message = new TextEncoder().encode(JSON.stringify({ size, id, chunk, start, end }));
|
|
372
|
+
this.#messageQue.push(message);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (!this.queRunning) return this.#runQue()
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
async send(message, id) {
|
|
379
|
+
const { chunks, size } = await this.splitMessage(message);
|
|
380
|
+
return this.#trySend({ size, id, chunks })
|
|
354
381
|
}
|
|
355
382
|
|
|
356
383
|
request(data) {
|
|
@@ -436,17 +463,21 @@ class Peer {
|
|
|
436
463
|
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
437
464
|
// allow sharding (multiple peers share data)
|
|
438
465
|
pubsub.publish('peernet:shard', message);
|
|
439
|
-
|
|
466
|
+
const { id } = message;
|
|
467
|
+
|
|
468
|
+
if (!this.#chunksQue[id]) this.#chunksQue[id] = [];
|
|
440
469
|
|
|
441
|
-
if (message.size >
|
|
470
|
+
if (message.size > this.#chunksQue[id].length || message.size === this.#chunksQue[id].length) {
|
|
442
471
|
for (const value of Object.values(message.chunk)) {
|
|
443
|
-
|
|
472
|
+
this.#chunksQue[id].push(value);
|
|
444
473
|
}
|
|
445
474
|
}
|
|
446
475
|
|
|
447
|
-
if (message.size ===
|
|
448
|
-
|
|
449
|
-
delete
|
|
476
|
+
if (message.size === this.#chunksQue[id].length) {
|
|
477
|
+
let data = new Uint8Array(Object.values(this.#chunksQue[id]));
|
|
478
|
+
delete this.#chunksQue[id];
|
|
479
|
+
data = pako.inflate(data);
|
|
480
|
+
pubsub.publish('peer:data', { id, data });
|
|
450
481
|
}
|
|
451
482
|
this.bw.down += message.byteLength || message.length;
|
|
452
483
|
}
|
|
@@ -691,540 +722,6 @@ message PeernetMessage {
|
|
|
691
722
|
optional string id = 5;
|
|
692
723
|
}`;
|
|
693
724
|
|
|
694
|
-
var codecs = {
|
|
695
|
-
// just a hash
|
|
696
|
-
'disco-hash': {
|
|
697
|
-
codec: parseInt('30', 16),
|
|
698
|
-
hashAlg: 'dbl-keccak-256', // ,
|
|
699
|
-
// testnet: 'olivia'
|
|
700
|
-
},
|
|
701
|
-
'peernet-peer-response': {
|
|
702
|
-
codec: parseInt('707072', 16),
|
|
703
|
-
hashAlg: 'keccak-256',
|
|
704
|
-
},
|
|
705
|
-
'peernet-peer': {
|
|
706
|
-
codec: parseInt('7070', 16),
|
|
707
|
-
hashAlg: 'keccak-256',
|
|
708
|
-
},
|
|
709
|
-
'peernet-dht': {
|
|
710
|
-
codec: parseInt('706468', 16),
|
|
711
|
-
hashAlg: 'keccak-256',
|
|
712
|
-
},
|
|
713
|
-
'peernet-dht-response': {
|
|
714
|
-
codec: parseInt('706472', 16),
|
|
715
|
-
hashAlg: 'keccak-256',
|
|
716
|
-
},
|
|
717
|
-
// data
|
|
718
|
-
'peernet-data': {
|
|
719
|
-
codec: parseInt('706461', 16),
|
|
720
|
-
hashAlg: 'keccak-256',
|
|
721
|
-
},
|
|
722
|
-
'peernet-data-response': {
|
|
723
|
-
codec: parseInt('70646172', 16),
|
|
724
|
-
hashAlg: 'keccak-256',
|
|
725
|
-
},
|
|
726
|
-
// message
|
|
727
|
-
'peernet-message': {
|
|
728
|
-
codec: parseInt('706d65', 16),
|
|
729
|
-
hashAlg: 'keccak-256',
|
|
730
|
-
},
|
|
731
|
-
// pubsub
|
|
732
|
-
'peernet-ps': {
|
|
733
|
-
codec: parseInt('707073', 16),
|
|
734
|
-
hashAlg: 'keccak-256',
|
|
735
|
-
},
|
|
736
|
-
'peernet-response': {
|
|
737
|
-
codec: parseInt('7072', 16),
|
|
738
|
-
hashAlg: 'keccak-256',
|
|
739
|
-
},
|
|
740
|
-
'peernet-request': {
|
|
741
|
-
codec: parseInt('707271', 16),
|
|
742
|
-
hashAlg: 'keccak-256',
|
|
743
|
-
},
|
|
744
|
-
// normal block
|
|
745
|
-
'leofcoin-block': {
|
|
746
|
-
codec: parseInt('6c62', 16),
|
|
747
|
-
hashAlg: 'dbl-keccak-512', // ,
|
|
748
|
-
// testnet: 'olivia'
|
|
749
|
-
},
|
|
750
|
-
'leofcoin-tx': {
|
|
751
|
-
codec: parseInt('6c74', 16),
|
|
752
|
-
hashAlg: 'dbl-keccak-512', // ,
|
|
753
|
-
// testnet: 'olivia'
|
|
754
|
-
},
|
|
755
|
-
// itx
|
|
756
|
-
'leofcoin-itx': {
|
|
757
|
-
codec: parseInt('6c69', 16),
|
|
758
|
-
hashAlg: 'keccak-512', // ,
|
|
759
|
-
// testnet: 'olivia'
|
|
760
|
-
},
|
|
761
|
-
// peer reputation
|
|
762
|
-
'leofcoin-pr': {
|
|
763
|
-
codec: parseInt('6c70', 16),
|
|
764
|
-
hashAlg: 'keccak-256', // ,
|
|
765
|
-
// testnet: 'olivia'
|
|
766
|
-
},
|
|
767
|
-
// chat message
|
|
768
|
-
'chat-message': {
|
|
769
|
-
codec: parseInt('636d', 16),
|
|
770
|
-
hashAlg: 'dbl-keccak-256',
|
|
771
|
-
},
|
|
772
|
-
};
|
|
773
|
-
|
|
774
|
-
class PeernetCodec {
|
|
775
|
-
get codecs() {
|
|
776
|
-
return {...globalThis.peernet.codecs, ...codecs}
|
|
777
|
-
}
|
|
778
|
-
constructor(buffer) {
|
|
779
|
-
if (buffer) {
|
|
780
|
-
if (buffer instanceof Uint8Array) {
|
|
781
|
-
const codec = varint.decode(buffer);
|
|
782
|
-
const name = this.getCodecName(codec);
|
|
783
|
-
if (name) {
|
|
784
|
-
this.name = name;
|
|
785
|
-
this.encoded = buffer;
|
|
786
|
-
this.decode(buffer);
|
|
787
|
-
} else {
|
|
788
|
-
this.encode(buffer);
|
|
789
|
-
}
|
|
790
|
-
} else if (buffer instanceof ArrayBuffer) {
|
|
791
|
-
const encoded = new Uint8Array(buffer.byteLength);
|
|
792
|
-
|
|
793
|
-
for (let i = 0; i < buffer.byteLength; i++) {
|
|
794
|
-
encoded[i] = buffer[i];
|
|
795
|
-
}
|
|
796
|
-
this.encoded = encoded;
|
|
797
|
-
// this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
|
|
798
|
-
this.decode(buffer);
|
|
799
|
-
return
|
|
800
|
-
}
|
|
801
|
-
if (typeof buffer === 'string') {
|
|
802
|
-
if (this.codecs[buffer]) this.fromName(buffer);
|
|
803
|
-
else if (isHex(buffer)) this.fromHex(buffer);
|
|
804
|
-
else if (bs32.isBase32(buffer)) this.fromBs32(buffer);
|
|
805
|
-
else if (bs58.isBase58(buffer)) this.fromBs58(buffer);
|
|
806
|
-
else throw new Error(`unsupported string ${buffer}`)
|
|
807
|
-
}
|
|
808
|
-
if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer);
|
|
809
|
-
}
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
fromEncoded(encoded) {
|
|
813
|
-
const codec = varint.decode(encoded);
|
|
814
|
-
const name = this.getCodecName(codec);
|
|
815
|
-
this.name = name;
|
|
816
|
-
this.encoded = encoded;
|
|
817
|
-
this.decode(encoded);
|
|
818
|
-
}
|
|
819
|
-
|
|
820
|
-
fromHex(hex) {
|
|
821
|
-
this.encoded = Buffer.from(hex, 'hex');
|
|
822
|
-
this.decode();
|
|
823
|
-
}
|
|
824
|
-
|
|
825
|
-
fromBs32(input) {
|
|
826
|
-
this.encoded = bs32.decode(input);
|
|
827
|
-
this.decode();
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
fromBs58(input) {
|
|
831
|
-
this.encoded = bs58.decode(input);
|
|
832
|
-
this.decode();
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
getCodec(name) {
|
|
836
|
-
return this.codecs[name].codec
|
|
837
|
-
}
|
|
838
|
-
|
|
839
|
-
getCodecName(codec) {
|
|
840
|
-
return Object.keys(this.codecs).reduce((p, c) => {
|
|
841
|
-
const item = this.codecs[c];
|
|
842
|
-
if (item.codec === codec) return c;
|
|
843
|
-
else return p;
|
|
844
|
-
}, undefined)
|
|
845
|
-
}
|
|
846
|
-
|
|
847
|
-
getHashAlg(name) {
|
|
848
|
-
return this.codecs[name].hashAlg
|
|
849
|
-
}
|
|
850
|
-
|
|
851
|
-
fromCodec(codec) {
|
|
852
|
-
this.name = this.getCodecName(codec);
|
|
853
|
-
this.hashAlg = this.getHashAlg(this.name);
|
|
854
|
-
|
|
855
|
-
this.codec = this.getCodec(this.name);
|
|
856
|
-
this.codecBuffer = varint.encode(codec);
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
fromName(name) {
|
|
860
|
-
const codec = this.getCodec(name);
|
|
861
|
-
this.name = name;
|
|
862
|
-
this.codec = codec;
|
|
863
|
-
this.hashAlg = this.getHashAlg(name);
|
|
864
|
-
this.codecBuffer = varint.encode(codec);
|
|
865
|
-
}
|
|
866
|
-
|
|
867
|
-
toBs32() {
|
|
868
|
-
this.encode();
|
|
869
|
-
return bs32.encode(this.encoded)
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
toBs58() {
|
|
873
|
-
this.encode();
|
|
874
|
-
return bs58.encode(this.encoded)
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
toHex() {
|
|
878
|
-
return this.encoded.toString('hex')
|
|
879
|
-
}
|
|
880
|
-
|
|
881
|
-
decode() {
|
|
882
|
-
const codec = varint.decode(this.encoded);
|
|
883
|
-
this.fromCodec(codec);
|
|
884
|
-
}
|
|
885
|
-
|
|
886
|
-
encode() {
|
|
887
|
-
const codec = varint.encode(this.decoded);
|
|
888
|
-
this.encoded = codec;
|
|
889
|
-
return this.encoded
|
|
890
|
-
}
|
|
891
|
-
}
|
|
892
|
-
|
|
893
|
-
class PeernetHash {
|
|
894
|
-
constructor(buffer, options = {}) {
|
|
895
|
-
if (options.name) this.name = options.name;
|
|
896
|
-
else this.name = 'disco-hash';
|
|
897
|
-
if (options.codecs) this.codecs = options.codecs;
|
|
898
|
-
if (buffer) {
|
|
899
|
-
if (buffer instanceof Uint8Array) {
|
|
900
|
-
this.discoCodec = new PeernetCodec(buffer, this.codecs);
|
|
901
|
-
const name = this.discoCodec.name;
|
|
902
|
-
|
|
903
|
-
if (name) {
|
|
904
|
-
this.name = name;
|
|
905
|
-
this.decode(buffer);
|
|
906
|
-
} else {
|
|
907
|
-
this.encode(buffer);
|
|
908
|
-
}
|
|
909
|
-
}
|
|
910
|
-
|
|
911
|
-
if (typeof buffer === 'string') {
|
|
912
|
-
if (isHex(buffer)) this.fromHex(buffer);
|
|
913
|
-
if (bs32.isBase32(buffer)) this.fromBs32(buffer);
|
|
914
|
-
else if (bs58.isBase58(buffer)) this.fromBs58(buffer);
|
|
915
|
-
else throw new Error(`unsupported string ${buffer}`)
|
|
916
|
-
} else if (typeof buffer === 'object') this.fromJSON(buffer);
|
|
917
|
-
}
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
get prefix() {
|
|
921
|
-
const length = this.length;
|
|
922
|
-
const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
|
|
923
|
-
uint8Array.set(length);
|
|
924
|
-
uint8Array.set(this.discoCodec.codecBuffer, length.length);
|
|
925
|
-
|
|
926
|
-
return uint8Array
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
get length() {
|
|
930
|
-
return varint.encode(this.size)
|
|
931
|
-
}
|
|
932
|
-
|
|
933
|
-
get buffer() {
|
|
934
|
-
return this.hash
|
|
935
|
-
}
|
|
936
|
-
|
|
937
|
-
toHex() {
|
|
938
|
-
return this.hash.toString('hex')
|
|
939
|
-
}
|
|
940
|
-
|
|
941
|
-
fromHex(hex) {
|
|
942
|
-
return this.decode(Buffer.from(hex, 'hex'))
|
|
943
|
-
}
|
|
944
|
-
|
|
945
|
-
fromJSON(json) {
|
|
946
|
-
return this.encode(Buffer.from(JSON.stringify(json)))
|
|
947
|
-
}
|
|
948
|
-
|
|
949
|
-
toBs32() {
|
|
950
|
-
return bs32.encode(this.hash)
|
|
951
|
-
}
|
|
952
|
-
|
|
953
|
-
fromBs32(bs) {
|
|
954
|
-
return this.decode(bs32.decode(bs))
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
toBs58() {
|
|
958
|
-
return bs58.encode(this.hash)
|
|
959
|
-
}
|
|
960
|
-
|
|
961
|
-
fromBs58(bs) {
|
|
962
|
-
return this.decode(bs58.decode(bs))
|
|
963
|
-
}
|
|
964
|
-
|
|
965
|
-
toString(encoding = 'utf8') {
|
|
966
|
-
return this.hash.toString(encoding)
|
|
967
|
-
}
|
|
968
|
-
|
|
969
|
-
encode(buffer, name) {
|
|
970
|
-
if (!this.name && name) this.name = name;
|
|
971
|
-
if (!buffer) buffer = this.buffer;
|
|
972
|
-
this.discoCodec = new PeernetCodec(this.name, this.codecs);
|
|
973
|
-
this.discoCodec.fromName(this.name);
|
|
974
|
-
let hashAlg = this.discoCodec.hashAlg;
|
|
975
|
-
if (hashAlg.includes('dbl')) {
|
|
976
|
-
hashAlg = hashAlg.replace('dbl-', '');
|
|
977
|
-
buffer = createKeccakHash(hashAlg.replace('-', '')).update(buffer).digest();
|
|
978
|
-
}
|
|
979
|
-
this.digest = createKeccakHash(hashAlg.replace('-', '')).update(buffer).digest();
|
|
980
|
-
this.size = this.digest.length;
|
|
981
|
-
|
|
982
|
-
this.codec = this.discoCodec.encode();
|
|
983
|
-
this.codec = this.discoCodec.codecBuffer;
|
|
984
|
-
const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
|
|
985
|
-
uint8Array.set(this.prefix);
|
|
986
|
-
uint8Array.set(this.digest, this.prefix.length);
|
|
987
|
-
|
|
988
|
-
this.hash = uint8Array;
|
|
989
|
-
|
|
990
|
-
return this.hash
|
|
991
|
-
}
|
|
992
|
-
|
|
993
|
-
validate(buffer) {
|
|
994
|
-
if (Buffer.isBuffer(buffer)) {
|
|
995
|
-
const codec = varint.decode(buffer);
|
|
996
|
-
if (this.codecs[codec]) {
|
|
997
|
-
this.decode(buffer);
|
|
998
|
-
} else {
|
|
999
|
-
this.encode(buffer);
|
|
1000
|
-
}
|
|
1001
|
-
}
|
|
1002
|
-
if (typeof buffer === 'string') {
|
|
1003
|
-
if (isHex(buffer)) this.fromHex(buffer);
|
|
1004
|
-
if (bs32.test(buffer)) this.fromBs32(buffer);
|
|
1005
|
-
}
|
|
1006
|
-
if (typeof buffer === 'object') this.fromJSON(buffer);
|
|
1007
|
-
}
|
|
1008
|
-
|
|
1009
|
-
decode(buffer) {
|
|
1010
|
-
this.hash = buffer;
|
|
1011
|
-
const codec = varint.decode(buffer);
|
|
1012
|
-
|
|
1013
|
-
this.discoCodec = new PeernetCodec(codec, this.codecs);
|
|
1014
|
-
// TODO: validate codec
|
|
1015
|
-
buffer = buffer.slice(varint.decode.bytes);
|
|
1016
|
-
this.size = varint.decode(buffer);
|
|
1017
|
-
this.digest = buffer.slice(varint.decode.bytes);
|
|
1018
|
-
if (this.digest.length !== this.size) {
|
|
1019
|
-
throw new Error(`hash length inconsistent: 0x${this.hash.toString('hex')}`)
|
|
1020
|
-
}
|
|
1021
|
-
|
|
1022
|
-
// const discoCodec = new Codec(codec, this.codecs)
|
|
1023
|
-
|
|
1024
|
-
this.name = this.discoCodec.name;
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
this.size = this.digest.length;
|
|
1028
|
-
|
|
1029
|
-
return {
|
|
1030
|
-
codec: this.codec,
|
|
1031
|
-
name: this.name,
|
|
1032
|
-
size: this.size,
|
|
1033
|
-
length: this.length,
|
|
1034
|
-
digest: this.digest,
|
|
1035
|
-
}
|
|
1036
|
-
}
|
|
1037
|
-
}
|
|
1038
|
-
|
|
1039
|
-
class FormatInterface {
|
|
1040
|
-
/**
|
|
1041
|
-
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
1042
|
-
* @param {Object} proto - {encode, decode}
|
|
1043
|
-
* @param {Object} options - {hashFormat, name}
|
|
1044
|
-
*/
|
|
1045
|
-
constructor(buffer, proto, options = {}) {
|
|
1046
|
-
this.protoEncode = proto.encode;
|
|
1047
|
-
this.protoDecode = proto.decode;
|
|
1048
|
-
this.hashFormat = options.hashFormat || 'bs32';
|
|
1049
|
-
if (options.name) this.name = options.name;
|
|
1050
|
-
if (buffer instanceof Uint8Array) this.fromUint8Array(buffer);
|
|
1051
|
-
else if (buffer instanceof ArrayBuffer) this.fromArrayBuffer(buffer);
|
|
1052
|
-
else if (buffer.name === options.name) return buffer
|
|
1053
|
-
else if (buffer instanceof String) {
|
|
1054
|
-
if (isHex(buffer)) this.fromHex(buffer);
|
|
1055
|
-
else if (bs32.isBase32(buffer)) this.fromBs32(buffer);
|
|
1056
|
-
else if (bs58.isBase58(buffer)) this.fromBs58(buffer);
|
|
1057
|
-
else throw new Error(`unsupported string ${buffer}`)
|
|
1058
|
-
} else {
|
|
1059
|
-
this.create(buffer);
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
|
|
1063
|
-
/**
|
|
1064
|
-
* @return {PeernetHash}
|
|
1065
|
-
*/
|
|
1066
|
-
get peernetHash() {
|
|
1067
|
-
return new PeernetHash(this.decoded, {name: this.name})
|
|
1068
|
-
}
|
|
1069
|
-
|
|
1070
|
-
/**
|
|
1071
|
-
* @return {peernetHash}
|
|
1072
|
-
*/
|
|
1073
|
-
get hash() {
|
|
1074
|
-
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
1075
|
-
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
1076
|
-
return this.peernetHash[`to${format}`]()
|
|
1077
|
-
}
|
|
1078
|
-
|
|
1079
|
-
/**
|
|
1080
|
-
* @return {Object}
|
|
1081
|
-
*/
|
|
1082
|
-
decode() {
|
|
1083
|
-
let encoded = this.encoded;
|
|
1084
|
-
const discoCodec = new PeernetCodec(this.encoded);
|
|
1085
|
-
encoded = encoded.slice(discoCodec.codecBuffer.length);
|
|
1086
|
-
this.name = discoCodec.name;
|
|
1087
|
-
this.decoded = this.protoDecode(encoded);
|
|
1088
|
-
return this.decoded
|
|
1089
|
-
}
|
|
1090
|
-
|
|
1091
|
-
/**
|
|
1092
|
-
* @return {Buffer}
|
|
1093
|
-
*/
|
|
1094
|
-
encode(decoded) {
|
|
1095
|
-
if (!decoded) decoded = this.decoded;
|
|
1096
|
-
const codec = new PeernetCodec(this.name);
|
|
1097
|
-
const encoded = this.protoEncode(decoded);
|
|
1098
|
-
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
|
|
1099
|
-
uint8Array.set(codec.codecBuffer);
|
|
1100
|
-
uint8Array.set(encoded, codec.codecBuffer.length);
|
|
1101
|
-
this.encoded = uint8Array;
|
|
1102
|
-
return this.encoded
|
|
1103
|
-
}
|
|
1104
|
-
|
|
1105
|
-
hasCodec() {
|
|
1106
|
-
if (!this.encoded) return false
|
|
1107
|
-
const codec = new PeernetCodec(this.encoded);
|
|
1108
|
-
if (codec.name) return true
|
|
1109
|
-
}
|
|
1110
|
-
|
|
1111
|
-
fromUint8Array(buffer) {
|
|
1112
|
-
this.encoded = buffer;
|
|
1113
|
-
if (!this.hasCodec()) this.create(
|
|
1114
|
-
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
1115
|
-
);
|
|
1116
|
-
else this.decode();
|
|
1117
|
-
}
|
|
1118
|
-
|
|
1119
|
-
fromArrayBuffer(buffer) {
|
|
1120
|
-
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
1121
|
-
if (!this.hasCodec()) this.create(
|
|
1122
|
-
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
1123
|
-
);
|
|
1124
|
-
else this.decode();
|
|
1125
|
-
}
|
|
1126
|
-
|
|
1127
|
-
toString() {
|
|
1128
|
-
return this.encoded.toString()
|
|
1129
|
-
}
|
|
1130
|
-
|
|
1131
|
-
async toArray() {
|
|
1132
|
-
const array = [];
|
|
1133
|
-
for await (const value of this.encoded.values()) {
|
|
1134
|
-
array.push(value);
|
|
1135
|
-
}
|
|
1136
|
-
return array
|
|
1137
|
-
}
|
|
1138
|
-
|
|
1139
|
-
fromString(string) {
|
|
1140
|
-
this.encoded = new Uint8Array(string.split(','));
|
|
1141
|
-
this.decode();
|
|
1142
|
-
}
|
|
1143
|
-
|
|
1144
|
-
fromArray(array) {
|
|
1145
|
-
this.encoded = new Uint8Array([...array]);
|
|
1146
|
-
this.decode();
|
|
1147
|
-
}
|
|
1148
|
-
|
|
1149
|
-
/**
|
|
1150
|
-
* @param {Buffer} encoded
|
|
1151
|
-
*/
|
|
1152
|
-
fromEncoded(encoded) {
|
|
1153
|
-
this.encoded = encoded;
|
|
1154
|
-
this.decode();
|
|
1155
|
-
}
|
|
1156
|
-
|
|
1157
|
-
/**
|
|
1158
|
-
* @param {String} encoded
|
|
1159
|
-
*/
|
|
1160
|
-
fromHex(encoded) {
|
|
1161
|
-
this.encoded = Buffer.from(encoded, 'hex');
|
|
1162
|
-
this.decode();
|
|
1163
|
-
}
|
|
1164
|
-
|
|
1165
|
-
/**
|
|
1166
|
-
* @param {String} encoded
|
|
1167
|
-
*/
|
|
1168
|
-
fromBs32(encoded) {
|
|
1169
|
-
this.encoded = bs32.decode(encoded);
|
|
1170
|
-
this.decode();
|
|
1171
|
-
}
|
|
1172
|
-
|
|
1173
|
-
/**
|
|
1174
|
-
* @param {String} encoded
|
|
1175
|
-
*/
|
|
1176
|
-
fromBs58(encoded) {
|
|
1177
|
-
this.encoded = bs58.decode(encoded);
|
|
1178
|
-
this.decode();
|
|
1179
|
-
}
|
|
1180
|
-
|
|
1181
|
-
/**
|
|
1182
|
-
* @return {String} encoded
|
|
1183
|
-
*/
|
|
1184
|
-
toHex() {
|
|
1185
|
-
if (!this.encoded) this.encode();
|
|
1186
|
-
return this.encoded.toString('hex')
|
|
1187
|
-
}
|
|
1188
|
-
|
|
1189
|
-
/**
|
|
1190
|
-
* @return {String} encoded
|
|
1191
|
-
*/
|
|
1192
|
-
toBs32() {
|
|
1193
|
-
if (!this.encoded) this.encode();
|
|
1194
|
-
return bs32.encode(this.encoded)
|
|
1195
|
-
}
|
|
1196
|
-
|
|
1197
|
-
/**
|
|
1198
|
-
* @return {String} encoded
|
|
1199
|
-
*/
|
|
1200
|
-
toBs58() {
|
|
1201
|
-
if (!this.encoded) this.encode();
|
|
1202
|
-
return bs58.encode(this.encoded)
|
|
1203
|
-
}
|
|
1204
|
-
|
|
1205
|
-
/**
|
|
1206
|
-
* @param {Object} data
|
|
1207
|
-
*/
|
|
1208
|
-
create(data) {
|
|
1209
|
-
const decoded = {};
|
|
1210
|
-
if (this.keys?.length > 0) {
|
|
1211
|
-
for (const key of this.keys) {
|
|
1212
|
-
Object.defineProperties(decoded, {
|
|
1213
|
-
[key]: {
|
|
1214
|
-
enumerable: true,
|
|
1215
|
-
configurable: true,
|
|
1216
|
-
set: (val) => value = data[key],
|
|
1217
|
-
get: () => data[key]
|
|
1218
|
-
}
|
|
1219
|
-
});
|
|
1220
|
-
}
|
|
1221
|
-
|
|
1222
|
-
this.decoded = decoded;
|
|
1223
|
-
this.encode();
|
|
1224
|
-
}
|
|
1225
|
-
}
|
|
1226
|
-
}
|
|
1227
|
-
|
|
1228
725
|
class PeernetMessage extends FormatInterface {
|
|
1229
726
|
get keys() {
|
|
1230
727
|
return ['data', 'signature', 'from', 'to', 'id']
|
|
@@ -1438,7 +935,7 @@ class ChatMessage extends FormatInterface {
|
|
|
1438
935
|
|
|
1439
936
|
const protoFor = (data) => {
|
|
1440
937
|
if (!Buffer.isBuffer(data)) data = Buffer.from(data);
|
|
1441
|
-
const codec = new
|
|
938
|
+
const codec = new Codec(data);
|
|
1442
939
|
if (!codec.name) throw new Error('proto not found')
|
|
1443
940
|
const Proto = globalThis.peernet.protos[codec.name];
|
|
1444
941
|
if (!Proto) throw (new Error(`No proto defined for ${codec.name}`))
|
|
@@ -2223,7 +1720,7 @@ class MessageHandler {
|
|
|
2223
1720
|
* @return signature
|
|
2224
1721
|
*/
|
|
2225
1722
|
async hashAndSignMessage(message) {
|
|
2226
|
-
const hasher = new
|
|
1723
|
+
const hasher = new CodecHash(message, {name: 'peernet-message'});
|
|
2227
1724
|
let identity = await walletStore.get('identity');
|
|
2228
1725
|
identity = JSON.parse(new TextDecoder().decode(identity));
|
|
2229
1726
|
const wallet = new MultiWallet(this.network);
|
|
@@ -2833,7 +2330,7 @@ class Peernet {
|
|
|
2833
2330
|
}
|
|
2834
2331
|
|
|
2835
2332
|
createHash(data, name) {
|
|
2836
|
-
return new
|
|
2333
|
+
return new CodecHash(data, {name})
|
|
2837
2334
|
}
|
|
2838
2335
|
|
|
2839
2336
|
/**
|