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