@leofcoin/peernet 0.11.23 → 0.11.26
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 +12339 -5515
- package/dist/commonjs/dht-response.js +2 -9
- package/dist/commonjs/dht.js +2 -9
- package/dist/commonjs/{http-547d8100.js → http-997ec597.js} +1 -1
- package/dist/commonjs/peernet-message.js +2 -9
- package/dist/commonjs/peernet.js +32 -23
- package/dist/commonjs/request.js +2 -9
- package/dist/commonjs/response.js +2 -9
- package/dist/module/peernet.js +22 -545
- 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 */
|
|
@@ -316,6 +315,7 @@ class Peer {
|
|
|
316
315
|
|
|
317
316
|
splitMessage(message) {
|
|
318
317
|
const chunks = [];
|
|
318
|
+
message = pako.deflate(message);
|
|
319
319
|
const size = message.byteLength || message.length;
|
|
320
320
|
let offset = 0;
|
|
321
321
|
return new Promise((resolve, reject) => {
|
|
@@ -405,10 +405,20 @@ class Peer {
|
|
|
405
405
|
async #init() {
|
|
406
406
|
try {
|
|
407
407
|
const iceServers = [{
|
|
408
|
-
|
|
408
|
+
urls: 'stun:stun.l.google.com:19302' // Google's public STUN server
|
|
409
|
+
}, {
|
|
410
|
+
urls: "stun:openrelay.metered.ca:80",
|
|
411
|
+
}, {
|
|
412
|
+
urls: "turn:openrelay.metered.ca:443",
|
|
413
|
+
username: "openrelayproject",
|
|
414
|
+
credential: "openrelayproject",
|
|
415
|
+
}, {
|
|
416
|
+
urls: "turn:openrelay.metered.ca:443?transport=tcp",
|
|
417
|
+
username: "openrelayproject",
|
|
418
|
+
credential: "openrelayproject",
|
|
409
419
|
}];
|
|
410
420
|
|
|
411
|
-
this.#connection = new wrtc.RTCPeerConnection();
|
|
421
|
+
this.#connection = new wrtc.RTCPeerConnection({iceServers});
|
|
412
422
|
|
|
413
423
|
this.#connection.onicecandidate = ({ candidate }) => {
|
|
414
424
|
if (candidate) {
|
|
@@ -474,8 +484,9 @@ class Peer {
|
|
|
474
484
|
}
|
|
475
485
|
|
|
476
486
|
if (message.size === this.#chunksQue[id].length) {
|
|
477
|
-
|
|
487
|
+
let data = new Uint8Array(Object.values(this.#chunksQue[id]));
|
|
478
488
|
delete this.#chunksQue[id];
|
|
489
|
+
data = pako.inflate(data);
|
|
479
490
|
pubsub.publish('peer:data', { id, data });
|
|
480
491
|
}
|
|
481
492
|
this.bw.down += message.byteLength || message.length;
|
|
@@ -721,540 +732,6 @@ message PeernetMessage {
|
|
|
721
732
|
optional string id = 5;
|
|
722
733
|
}`;
|
|
723
734
|
|
|
724
|
-
var codecs = {
|
|
725
|
-
// just a hash
|
|
726
|
-
'disco-hash': {
|
|
727
|
-
codec: parseInt('30', 16),
|
|
728
|
-
hashAlg: 'dbl-keccak-256', // ,
|
|
729
|
-
// testnet: 'olivia'
|
|
730
|
-
},
|
|
731
|
-
'peernet-peer-response': {
|
|
732
|
-
codec: parseInt('707072', 16),
|
|
733
|
-
hashAlg: 'keccak-256',
|
|
734
|
-
},
|
|
735
|
-
'peernet-peer': {
|
|
736
|
-
codec: parseInt('7070', 16),
|
|
737
|
-
hashAlg: 'keccak-256',
|
|
738
|
-
},
|
|
739
|
-
'peernet-dht': {
|
|
740
|
-
codec: parseInt('706468', 16),
|
|
741
|
-
hashAlg: 'keccak-256',
|
|
742
|
-
},
|
|
743
|
-
'peernet-dht-response': {
|
|
744
|
-
codec: parseInt('706472', 16),
|
|
745
|
-
hashAlg: 'keccak-256',
|
|
746
|
-
},
|
|
747
|
-
// data
|
|
748
|
-
'peernet-data': {
|
|
749
|
-
codec: parseInt('706461', 16),
|
|
750
|
-
hashAlg: 'keccak-256',
|
|
751
|
-
},
|
|
752
|
-
'peernet-data-response': {
|
|
753
|
-
codec: parseInt('70646172', 16),
|
|
754
|
-
hashAlg: 'keccak-256',
|
|
755
|
-
},
|
|
756
|
-
// message
|
|
757
|
-
'peernet-message': {
|
|
758
|
-
codec: parseInt('706d65', 16),
|
|
759
|
-
hashAlg: 'keccak-256',
|
|
760
|
-
},
|
|
761
|
-
// pubsub
|
|
762
|
-
'peernet-ps': {
|
|
763
|
-
codec: parseInt('707073', 16),
|
|
764
|
-
hashAlg: 'keccak-256',
|
|
765
|
-
},
|
|
766
|
-
'peernet-response': {
|
|
767
|
-
codec: parseInt('7072', 16),
|
|
768
|
-
hashAlg: 'keccak-256',
|
|
769
|
-
},
|
|
770
|
-
'peernet-request': {
|
|
771
|
-
codec: parseInt('707271', 16),
|
|
772
|
-
hashAlg: 'keccak-256',
|
|
773
|
-
},
|
|
774
|
-
// normal block
|
|
775
|
-
'leofcoin-block': {
|
|
776
|
-
codec: parseInt('6c62', 16),
|
|
777
|
-
hashAlg: 'dbl-keccak-512', // ,
|
|
778
|
-
// testnet: 'olivia'
|
|
779
|
-
},
|
|
780
|
-
'leofcoin-tx': {
|
|
781
|
-
codec: parseInt('6c74', 16),
|
|
782
|
-
hashAlg: 'dbl-keccak-512', // ,
|
|
783
|
-
// testnet: 'olivia'
|
|
784
|
-
},
|
|
785
|
-
// itx
|
|
786
|
-
'leofcoin-itx': {
|
|
787
|
-
codec: parseInt('6c69', 16),
|
|
788
|
-
hashAlg: 'keccak-512', // ,
|
|
789
|
-
// testnet: 'olivia'
|
|
790
|
-
},
|
|
791
|
-
// peer reputation
|
|
792
|
-
'leofcoin-pr': {
|
|
793
|
-
codec: parseInt('6c70', 16),
|
|
794
|
-
hashAlg: 'keccak-256', // ,
|
|
795
|
-
// testnet: 'olivia'
|
|
796
|
-
},
|
|
797
|
-
// chat message
|
|
798
|
-
'chat-message': {
|
|
799
|
-
codec: parseInt('636d', 16),
|
|
800
|
-
hashAlg: 'dbl-keccak-256',
|
|
801
|
-
},
|
|
802
|
-
};
|
|
803
|
-
|
|
804
|
-
class PeernetCodec {
|
|
805
|
-
get codecs() {
|
|
806
|
-
return {...globalThis.peernet.codecs, ...codecs}
|
|
807
|
-
}
|
|
808
|
-
constructor(buffer) {
|
|
809
|
-
if (buffer) {
|
|
810
|
-
if (buffer instanceof Uint8Array) {
|
|
811
|
-
const codec = varint.decode(buffer);
|
|
812
|
-
const name = this.getCodecName(codec);
|
|
813
|
-
if (name) {
|
|
814
|
-
this.name = name;
|
|
815
|
-
this.encoded = buffer;
|
|
816
|
-
this.decode(buffer);
|
|
817
|
-
} else {
|
|
818
|
-
this.encode(buffer);
|
|
819
|
-
}
|
|
820
|
-
} else if (buffer instanceof ArrayBuffer) {
|
|
821
|
-
const encoded = new Uint8Array(buffer.byteLength);
|
|
822
|
-
|
|
823
|
-
for (let i = 0; i < buffer.byteLength; i++) {
|
|
824
|
-
encoded[i] = buffer[i];
|
|
825
|
-
}
|
|
826
|
-
this.encoded = encoded;
|
|
827
|
-
// this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
|
|
828
|
-
this.decode(buffer);
|
|
829
|
-
return
|
|
830
|
-
}
|
|
831
|
-
if (typeof buffer === 'string') {
|
|
832
|
-
if (this.codecs[buffer]) this.fromName(buffer);
|
|
833
|
-
else if (isHex(buffer)) this.fromHex(buffer);
|
|
834
|
-
else if (bs32.isBase32(buffer)) this.fromBs32(buffer);
|
|
835
|
-
else if (bs58.isBase58(buffer)) this.fromBs58(buffer);
|
|
836
|
-
else throw new Error(`unsupported string ${buffer}`)
|
|
837
|
-
}
|
|
838
|
-
if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer);
|
|
839
|
-
}
|
|
840
|
-
}
|
|
841
|
-
|
|
842
|
-
fromEncoded(encoded) {
|
|
843
|
-
const codec = varint.decode(encoded);
|
|
844
|
-
const name = this.getCodecName(codec);
|
|
845
|
-
this.name = name;
|
|
846
|
-
this.encoded = encoded;
|
|
847
|
-
this.decode(encoded);
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
fromHex(hex) {
|
|
851
|
-
this.encoded = Buffer.from(hex, 'hex');
|
|
852
|
-
this.decode();
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
fromBs32(input) {
|
|
856
|
-
this.encoded = bs32.decode(input);
|
|
857
|
-
this.decode();
|
|
858
|
-
}
|
|
859
|
-
|
|
860
|
-
fromBs58(input) {
|
|
861
|
-
this.encoded = bs58.decode(input);
|
|
862
|
-
this.decode();
|
|
863
|
-
}
|
|
864
|
-
|
|
865
|
-
getCodec(name) {
|
|
866
|
-
return this.codecs[name].codec
|
|
867
|
-
}
|
|
868
|
-
|
|
869
|
-
getCodecName(codec) {
|
|
870
|
-
return Object.keys(this.codecs).reduce((p, c) => {
|
|
871
|
-
const item = this.codecs[c];
|
|
872
|
-
if (item.codec === codec) return c;
|
|
873
|
-
else return p;
|
|
874
|
-
}, undefined)
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
getHashAlg(name) {
|
|
878
|
-
return this.codecs[name].hashAlg
|
|
879
|
-
}
|
|
880
|
-
|
|
881
|
-
fromCodec(codec) {
|
|
882
|
-
this.name = this.getCodecName(codec);
|
|
883
|
-
this.hashAlg = this.getHashAlg(this.name);
|
|
884
|
-
|
|
885
|
-
this.codec = this.getCodec(this.name);
|
|
886
|
-
this.codecBuffer = varint.encode(codec);
|
|
887
|
-
}
|
|
888
|
-
|
|
889
|
-
fromName(name) {
|
|
890
|
-
const codec = this.getCodec(name);
|
|
891
|
-
this.name = name;
|
|
892
|
-
this.codec = codec;
|
|
893
|
-
this.hashAlg = this.getHashAlg(name);
|
|
894
|
-
this.codecBuffer = varint.encode(codec);
|
|
895
|
-
}
|
|
896
|
-
|
|
897
|
-
toBs32() {
|
|
898
|
-
this.encode();
|
|
899
|
-
return bs32.encode(this.encoded)
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
toBs58() {
|
|
903
|
-
this.encode();
|
|
904
|
-
return bs58.encode(this.encoded)
|
|
905
|
-
}
|
|
906
|
-
|
|
907
|
-
toHex() {
|
|
908
|
-
return this.encoded.toString('hex')
|
|
909
|
-
}
|
|
910
|
-
|
|
911
|
-
decode() {
|
|
912
|
-
const codec = varint.decode(this.encoded);
|
|
913
|
-
this.fromCodec(codec);
|
|
914
|
-
}
|
|
915
|
-
|
|
916
|
-
encode() {
|
|
917
|
-
const codec = varint.encode(this.decoded);
|
|
918
|
-
this.encoded = codec;
|
|
919
|
-
return this.encoded
|
|
920
|
-
}
|
|
921
|
-
}
|
|
922
|
-
|
|
923
|
-
class PeernetHash {
|
|
924
|
-
constructor(buffer, options = {}) {
|
|
925
|
-
if (options.name) this.name = options.name;
|
|
926
|
-
else this.name = 'disco-hash';
|
|
927
|
-
if (options.codecs) this.codecs = options.codecs;
|
|
928
|
-
if (buffer) {
|
|
929
|
-
if (buffer instanceof Uint8Array) {
|
|
930
|
-
this.discoCodec = new PeernetCodec(buffer, this.codecs);
|
|
931
|
-
const name = this.discoCodec.name;
|
|
932
|
-
|
|
933
|
-
if (name) {
|
|
934
|
-
this.name = name;
|
|
935
|
-
this.decode(buffer);
|
|
936
|
-
} else {
|
|
937
|
-
this.encode(buffer);
|
|
938
|
-
}
|
|
939
|
-
}
|
|
940
|
-
|
|
941
|
-
if (typeof buffer === 'string') {
|
|
942
|
-
if (isHex(buffer)) this.fromHex(buffer);
|
|
943
|
-
if (bs32.isBase32(buffer)) this.fromBs32(buffer);
|
|
944
|
-
else if (bs58.isBase58(buffer)) this.fromBs58(buffer);
|
|
945
|
-
else throw new Error(`unsupported string ${buffer}`)
|
|
946
|
-
} else if (typeof buffer === 'object') this.fromJSON(buffer);
|
|
947
|
-
}
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
get prefix() {
|
|
951
|
-
const length = this.length;
|
|
952
|
-
const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
|
|
953
|
-
uint8Array.set(length);
|
|
954
|
-
uint8Array.set(this.discoCodec.codecBuffer, length.length);
|
|
955
|
-
|
|
956
|
-
return uint8Array
|
|
957
|
-
}
|
|
958
|
-
|
|
959
|
-
get length() {
|
|
960
|
-
return varint.encode(this.size)
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
get buffer() {
|
|
964
|
-
return this.hash
|
|
965
|
-
}
|
|
966
|
-
|
|
967
|
-
toHex() {
|
|
968
|
-
return this.hash.toString('hex')
|
|
969
|
-
}
|
|
970
|
-
|
|
971
|
-
fromHex(hex) {
|
|
972
|
-
return this.decode(Buffer.from(hex, 'hex'))
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
fromJSON(json) {
|
|
976
|
-
return this.encode(Buffer.from(JSON.stringify(json)))
|
|
977
|
-
}
|
|
978
|
-
|
|
979
|
-
toBs32() {
|
|
980
|
-
return bs32.encode(this.hash)
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
fromBs32(bs) {
|
|
984
|
-
return this.decode(bs32.decode(bs))
|
|
985
|
-
}
|
|
986
|
-
|
|
987
|
-
toBs58() {
|
|
988
|
-
return bs58.encode(this.hash)
|
|
989
|
-
}
|
|
990
|
-
|
|
991
|
-
fromBs58(bs) {
|
|
992
|
-
return this.decode(bs58.decode(bs))
|
|
993
|
-
}
|
|
994
|
-
|
|
995
|
-
toString(encoding = 'utf8') {
|
|
996
|
-
return this.hash.toString(encoding)
|
|
997
|
-
}
|
|
998
|
-
|
|
999
|
-
encode(buffer, name) {
|
|
1000
|
-
if (!this.name && name) this.name = name;
|
|
1001
|
-
if (!buffer) buffer = this.buffer;
|
|
1002
|
-
this.discoCodec = new PeernetCodec(this.name, this.codecs);
|
|
1003
|
-
this.discoCodec.fromName(this.name);
|
|
1004
|
-
let hashAlg = this.discoCodec.hashAlg;
|
|
1005
|
-
if (hashAlg.includes('dbl')) {
|
|
1006
|
-
hashAlg = hashAlg.replace('dbl-', '');
|
|
1007
|
-
buffer = createKeccakHash(hashAlg.replace('-', '')).update(buffer).digest();
|
|
1008
|
-
}
|
|
1009
|
-
this.digest = createKeccakHash(hashAlg.replace('-', '')).update(buffer).digest();
|
|
1010
|
-
this.size = this.digest.length;
|
|
1011
|
-
|
|
1012
|
-
this.codec = this.discoCodec.encode();
|
|
1013
|
-
this.codec = this.discoCodec.codecBuffer;
|
|
1014
|
-
const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
|
|
1015
|
-
uint8Array.set(this.prefix);
|
|
1016
|
-
uint8Array.set(this.digest, this.prefix.length);
|
|
1017
|
-
|
|
1018
|
-
this.hash = uint8Array;
|
|
1019
|
-
|
|
1020
|
-
return this.hash
|
|
1021
|
-
}
|
|
1022
|
-
|
|
1023
|
-
validate(buffer) {
|
|
1024
|
-
if (Buffer.isBuffer(buffer)) {
|
|
1025
|
-
const codec = varint.decode(buffer);
|
|
1026
|
-
if (this.codecs[codec]) {
|
|
1027
|
-
this.decode(buffer);
|
|
1028
|
-
} else {
|
|
1029
|
-
this.encode(buffer);
|
|
1030
|
-
}
|
|
1031
|
-
}
|
|
1032
|
-
if (typeof buffer === 'string') {
|
|
1033
|
-
if (isHex(buffer)) this.fromHex(buffer);
|
|
1034
|
-
if (bs32.test(buffer)) this.fromBs32(buffer);
|
|
1035
|
-
}
|
|
1036
|
-
if (typeof buffer === 'object') this.fromJSON(buffer);
|
|
1037
|
-
}
|
|
1038
|
-
|
|
1039
|
-
decode(buffer) {
|
|
1040
|
-
this.hash = buffer;
|
|
1041
|
-
const codec = varint.decode(buffer);
|
|
1042
|
-
|
|
1043
|
-
this.discoCodec = new PeernetCodec(codec, this.codecs);
|
|
1044
|
-
// TODO: validate codec
|
|
1045
|
-
buffer = buffer.slice(varint.decode.bytes);
|
|
1046
|
-
this.size = varint.decode(buffer);
|
|
1047
|
-
this.digest = buffer.slice(varint.decode.bytes);
|
|
1048
|
-
if (this.digest.length !== this.size) {
|
|
1049
|
-
throw new Error(`hash length inconsistent: 0x${this.hash.toString('hex')}`)
|
|
1050
|
-
}
|
|
1051
|
-
|
|
1052
|
-
// const discoCodec = new Codec(codec, this.codecs)
|
|
1053
|
-
|
|
1054
|
-
this.name = this.discoCodec.name;
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
this.size = this.digest.length;
|
|
1058
|
-
|
|
1059
|
-
return {
|
|
1060
|
-
codec: this.codec,
|
|
1061
|
-
name: this.name,
|
|
1062
|
-
size: this.size,
|
|
1063
|
-
length: this.length,
|
|
1064
|
-
digest: this.digest,
|
|
1065
|
-
}
|
|
1066
|
-
}
|
|
1067
|
-
}
|
|
1068
|
-
|
|
1069
|
-
class FormatInterface {
|
|
1070
|
-
/**
|
|
1071
|
-
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
1072
|
-
* @param {Object} proto - {encode, decode}
|
|
1073
|
-
* @param {Object} options - {hashFormat, name}
|
|
1074
|
-
*/
|
|
1075
|
-
constructor(buffer, proto, options = {}) {
|
|
1076
|
-
this.protoEncode = proto.encode;
|
|
1077
|
-
this.protoDecode = proto.decode;
|
|
1078
|
-
this.hashFormat = options.hashFormat || 'bs32';
|
|
1079
|
-
if (options.name) this.name = options.name;
|
|
1080
|
-
if (buffer instanceof Uint8Array) this.fromUint8Array(buffer);
|
|
1081
|
-
else if (buffer instanceof ArrayBuffer) this.fromArrayBuffer(buffer);
|
|
1082
|
-
else if (buffer.name === options.name) return buffer
|
|
1083
|
-
else if (buffer instanceof String) {
|
|
1084
|
-
if (isHex(buffer)) this.fromHex(buffer);
|
|
1085
|
-
else if (bs32.isBase32(buffer)) this.fromBs32(buffer);
|
|
1086
|
-
else if (bs58.isBase58(buffer)) this.fromBs58(buffer);
|
|
1087
|
-
else throw new Error(`unsupported string ${buffer}`)
|
|
1088
|
-
} else {
|
|
1089
|
-
this.create(buffer);
|
|
1090
|
-
}
|
|
1091
|
-
}
|
|
1092
|
-
|
|
1093
|
-
/**
|
|
1094
|
-
* @return {PeernetHash}
|
|
1095
|
-
*/
|
|
1096
|
-
get peernetHash() {
|
|
1097
|
-
return new PeernetHash(this.decoded, {name: this.name})
|
|
1098
|
-
}
|
|
1099
|
-
|
|
1100
|
-
/**
|
|
1101
|
-
* @return {peernetHash}
|
|
1102
|
-
*/
|
|
1103
|
-
get hash() {
|
|
1104
|
-
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
1105
|
-
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
1106
|
-
return this.peernetHash[`to${format}`]()
|
|
1107
|
-
}
|
|
1108
|
-
|
|
1109
|
-
/**
|
|
1110
|
-
* @return {Object}
|
|
1111
|
-
*/
|
|
1112
|
-
decode() {
|
|
1113
|
-
let encoded = this.encoded;
|
|
1114
|
-
const discoCodec = new PeernetCodec(this.encoded);
|
|
1115
|
-
encoded = encoded.slice(discoCodec.codecBuffer.length);
|
|
1116
|
-
this.name = discoCodec.name;
|
|
1117
|
-
this.decoded = this.protoDecode(encoded);
|
|
1118
|
-
return this.decoded
|
|
1119
|
-
}
|
|
1120
|
-
|
|
1121
|
-
/**
|
|
1122
|
-
* @return {Buffer}
|
|
1123
|
-
*/
|
|
1124
|
-
encode(decoded) {
|
|
1125
|
-
if (!decoded) decoded = this.decoded;
|
|
1126
|
-
const codec = new PeernetCodec(this.name);
|
|
1127
|
-
const encoded = this.protoEncode(decoded);
|
|
1128
|
-
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
|
|
1129
|
-
uint8Array.set(codec.codecBuffer);
|
|
1130
|
-
uint8Array.set(encoded, codec.codecBuffer.length);
|
|
1131
|
-
this.encoded = uint8Array;
|
|
1132
|
-
return this.encoded
|
|
1133
|
-
}
|
|
1134
|
-
|
|
1135
|
-
hasCodec() {
|
|
1136
|
-
if (!this.encoded) return false
|
|
1137
|
-
const codec = new PeernetCodec(this.encoded);
|
|
1138
|
-
if (codec.name) return true
|
|
1139
|
-
}
|
|
1140
|
-
|
|
1141
|
-
fromUint8Array(buffer) {
|
|
1142
|
-
this.encoded = buffer;
|
|
1143
|
-
if (!this.hasCodec()) this.create(
|
|
1144
|
-
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
1145
|
-
);
|
|
1146
|
-
else this.decode();
|
|
1147
|
-
}
|
|
1148
|
-
|
|
1149
|
-
fromArrayBuffer(buffer) {
|
|
1150
|
-
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
1151
|
-
if (!this.hasCodec()) this.create(
|
|
1152
|
-
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
1153
|
-
);
|
|
1154
|
-
else this.decode();
|
|
1155
|
-
}
|
|
1156
|
-
|
|
1157
|
-
toString() {
|
|
1158
|
-
return this.encoded.toString()
|
|
1159
|
-
}
|
|
1160
|
-
|
|
1161
|
-
async toArray() {
|
|
1162
|
-
const array = [];
|
|
1163
|
-
for await (const value of this.encoded.values()) {
|
|
1164
|
-
array.push(value);
|
|
1165
|
-
}
|
|
1166
|
-
return array
|
|
1167
|
-
}
|
|
1168
|
-
|
|
1169
|
-
fromString(string) {
|
|
1170
|
-
this.encoded = new Uint8Array(string.split(','));
|
|
1171
|
-
this.decode();
|
|
1172
|
-
}
|
|
1173
|
-
|
|
1174
|
-
fromArray(array) {
|
|
1175
|
-
this.encoded = new Uint8Array([...array]);
|
|
1176
|
-
this.decode();
|
|
1177
|
-
}
|
|
1178
|
-
|
|
1179
|
-
/**
|
|
1180
|
-
* @param {Buffer} encoded
|
|
1181
|
-
*/
|
|
1182
|
-
fromEncoded(encoded) {
|
|
1183
|
-
this.encoded = encoded;
|
|
1184
|
-
this.decode();
|
|
1185
|
-
}
|
|
1186
|
-
|
|
1187
|
-
/**
|
|
1188
|
-
* @param {String} encoded
|
|
1189
|
-
*/
|
|
1190
|
-
fromHex(encoded) {
|
|
1191
|
-
this.encoded = Buffer.from(encoded, 'hex');
|
|
1192
|
-
this.decode();
|
|
1193
|
-
}
|
|
1194
|
-
|
|
1195
|
-
/**
|
|
1196
|
-
* @param {String} encoded
|
|
1197
|
-
*/
|
|
1198
|
-
fromBs32(encoded) {
|
|
1199
|
-
this.encoded = bs32.decode(encoded);
|
|
1200
|
-
this.decode();
|
|
1201
|
-
}
|
|
1202
|
-
|
|
1203
|
-
/**
|
|
1204
|
-
* @param {String} encoded
|
|
1205
|
-
*/
|
|
1206
|
-
fromBs58(encoded) {
|
|
1207
|
-
this.encoded = bs58.decode(encoded);
|
|
1208
|
-
this.decode();
|
|
1209
|
-
}
|
|
1210
|
-
|
|
1211
|
-
/**
|
|
1212
|
-
* @return {String} encoded
|
|
1213
|
-
*/
|
|
1214
|
-
toHex() {
|
|
1215
|
-
if (!this.encoded) this.encode();
|
|
1216
|
-
return this.encoded.toString('hex')
|
|
1217
|
-
}
|
|
1218
|
-
|
|
1219
|
-
/**
|
|
1220
|
-
* @return {String} encoded
|
|
1221
|
-
*/
|
|
1222
|
-
toBs32() {
|
|
1223
|
-
if (!this.encoded) this.encode();
|
|
1224
|
-
return bs32.encode(this.encoded)
|
|
1225
|
-
}
|
|
1226
|
-
|
|
1227
|
-
/**
|
|
1228
|
-
* @return {String} encoded
|
|
1229
|
-
*/
|
|
1230
|
-
toBs58() {
|
|
1231
|
-
if (!this.encoded) this.encode();
|
|
1232
|
-
return bs58.encode(this.encoded)
|
|
1233
|
-
}
|
|
1234
|
-
|
|
1235
|
-
/**
|
|
1236
|
-
* @param {Object} data
|
|
1237
|
-
*/
|
|
1238
|
-
create(data) {
|
|
1239
|
-
const decoded = {};
|
|
1240
|
-
if (this.keys?.length > 0) {
|
|
1241
|
-
for (const key of this.keys) {
|
|
1242
|
-
Object.defineProperties(decoded, {
|
|
1243
|
-
[key]: {
|
|
1244
|
-
enumerable: true,
|
|
1245
|
-
configurable: true,
|
|
1246
|
-
set: (val) => value = data[key],
|
|
1247
|
-
get: () => data[key]
|
|
1248
|
-
}
|
|
1249
|
-
});
|
|
1250
|
-
}
|
|
1251
|
-
|
|
1252
|
-
this.decoded = decoded;
|
|
1253
|
-
this.encode();
|
|
1254
|
-
}
|
|
1255
|
-
}
|
|
1256
|
-
}
|
|
1257
|
-
|
|
1258
735
|
class PeernetMessage extends FormatInterface {
|
|
1259
736
|
get keys() {
|
|
1260
737
|
return ['data', 'signature', 'from', 'to', 'id']
|
|
@@ -1468,7 +945,7 @@ class ChatMessage extends FormatInterface {
|
|
|
1468
945
|
|
|
1469
946
|
const protoFor = (data) => {
|
|
1470
947
|
if (!Buffer.isBuffer(data)) data = Buffer.from(data);
|
|
1471
|
-
const codec = new
|
|
948
|
+
const codec = new Codec(data);
|
|
1472
949
|
if (!codec.name) throw new Error('proto not found')
|
|
1473
950
|
const Proto = globalThis.peernet.protos[codec.name];
|
|
1474
951
|
if (!Proto) throw (new Error(`No proto defined for ${codec.name}`))
|
|
@@ -2253,7 +1730,7 @@ class MessageHandler {
|
|
|
2253
1730
|
* @return signature
|
|
2254
1731
|
*/
|
|
2255
1732
|
async hashAndSignMessage(message) {
|
|
2256
|
-
const hasher = new
|
|
1733
|
+
const hasher = new CodecHash(message, {name: 'peernet-message'});
|
|
2257
1734
|
let identity = await walletStore.get('identity');
|
|
2258
1735
|
identity = JSON.parse(new TextDecoder().decode(identity));
|
|
2259
1736
|
const wallet = new MultiWallet(this.network);
|
|
@@ -2863,7 +2340,7 @@ class Peernet {
|
|
|
2863
2340
|
}
|
|
2864
2341
|
|
|
2865
2342
|
createHash(data, name) {
|
|
2866
|
-
return new
|
|
2343
|
+
return new CodecHash(data, {name})
|
|
2867
2344
|
}
|
|
2868
2345
|
|
|
2869
2346
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.26",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/commonjs/peernet.js",
|
|
6
6
|
"module": "dist/module/peernet.js",
|
|
@@ -23,9 +23,10 @@
|
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"browserslist": "> 0.5%, last 2 versions, not dead",
|
|
25
25
|
"dependencies": {
|
|
26
|
+
"@leofcoin/codec-format-interface": "^1.0.1",
|
|
26
27
|
"@leofcoin/generate-account": "^1.0.2",
|
|
27
28
|
"@leofcoin/multi-wallet": "^2.1.2",
|
|
28
|
-
"@leofcoin/peernet-swarm": "^0.
|
|
29
|
+
"@leofcoin/peernet-swarm": "^0.3.1",
|
|
29
30
|
"@leofcoin/storage": "^2.3.0",
|
|
30
31
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
31
32
|
"@vandeurenglenn/base58": "^1.1.0",
|
package/rollup.config.js
CHANGED
|
@@ -9,9 +9,9 @@ try {
|
|
|
9
9
|
|
|
10
10
|
}
|
|
11
11
|
export default [{
|
|
12
|
-
input: ['src/peernet.js', 'src/
|
|
12
|
+
input: ['src/peernet.js', 'src/messages/dht.js',
|
|
13
13
|
'src/messages/peernet-message.js', 'src/messages/dht-response.js', 'src/messages/request.js',
|
|
14
|
-
'src/messages/response.js'
|
|
14
|
+
'src/messages/response.js'],
|
|
15
15
|
output: {
|
|
16
16
|
dir: 'dist/commonjs',
|
|
17
17
|
format: 'cjs'
|
package/src/handlers/message.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import MultiWallet from './../../node_modules/@leofcoin/multi-wallet/src/index'
|
|
2
|
-
import Hash from '
|
|
2
|
+
import { CodecHash as Hash } from '@leofcoin/codec-format-interface'
|
|
3
3
|
import PeernetMessage from './../messages/peernet-message.js'
|
|
4
4
|
|
|
5
5
|
export default class MessageHandler {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import protons from 'protons'
|
|
2
2
|
import proto from './../proto/chat-message.proto.js'
|
|
3
|
-
import
|
|
3
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
4
|
|
|
5
|
-
export default class ChatMessage extends
|
|
5
|
+
export default class ChatMessage extends FormatInterface {
|
|
6
6
|
get keys() {
|
|
7
7
|
return ['author', 'value', 'timestamp', 'files']
|
|
8
8
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import protons from 'protons'
|
|
2
2
|
import proto from './../proto/data-response.proto.js'
|
|
3
|
-
import
|
|
3
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
4
|
|
|
5
|
-
export default class DataMessageResponse extends
|
|
5
|
+
export default class DataMessageResponse extends FormatInterface {
|
|
6
6
|
get keys() {
|
|
7
7
|
return ['hash', 'data']
|
|
8
8
|
}
|
package/src/messages/data.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import protons from 'protons'
|
|
2
2
|
import proto from './../proto/data.proto.js'
|
|
3
|
-
import
|
|
3
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @extends {CodecFormat}
|
|
7
7
|
*/
|
|
8
|
-
export default class DataMessage extends
|
|
8
|
+
export default class DataMessage extends FormatInterface {
|
|
9
9
|
get keys() {
|
|
10
10
|
return ['hash', 'store']
|
|
11
11
|
}
|