@leofcoin/peernet 0.11.24 → 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 +1861 -1847
- package/dist/commonjs/dht-response.js +2 -9
- package/dist/commonjs/dht.js +2 -9
- package/dist/commonjs/{http-b45d1330.js → http-78686629.js} +1 -1
- package/dist/commonjs/peernet-message.js +2 -9
- package/dist/commonjs/peernet.js +15 -20
- package/dist/commonjs/request.js +2 -9
- package/dist/commonjs/response.js +2 -9
- package/dist/module/peernet.js +6 -542
- package/package.json +2 -1
- 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
|
@@ -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 */
|
|
@@ -724,540 +722,6 @@ message PeernetMessage {
|
|
|
724
722
|
optional string id = 5;
|
|
725
723
|
}`;
|
|
726
724
|
|
|
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
725
|
class PeernetMessage extends FormatInterface {
|
|
1262
726
|
get keys() {
|
|
1263
727
|
return ['data', 'signature', 'from', 'to', 'id']
|
|
@@ -1471,7 +935,7 @@ class ChatMessage extends FormatInterface {
|
|
|
1471
935
|
|
|
1472
936
|
const protoFor = (data) => {
|
|
1473
937
|
if (!Buffer.isBuffer(data)) data = Buffer.from(data);
|
|
1474
|
-
const codec = new
|
|
938
|
+
const codec = new Codec(data);
|
|
1475
939
|
if (!codec.name) throw new Error('proto not found')
|
|
1476
940
|
const Proto = globalThis.peernet.protos[codec.name];
|
|
1477
941
|
if (!Proto) throw (new Error(`No proto defined for ${codec.name}`))
|
|
@@ -2256,7 +1720,7 @@ class MessageHandler {
|
|
|
2256
1720
|
* @return signature
|
|
2257
1721
|
*/
|
|
2258
1722
|
async hashAndSignMessage(message) {
|
|
2259
|
-
const hasher = new
|
|
1723
|
+
const hasher = new CodecHash(message, {name: 'peernet-message'});
|
|
2260
1724
|
let identity = await walletStore.get('identity');
|
|
2261
1725
|
identity = JSON.parse(new TextDecoder().decode(identity));
|
|
2262
1726
|
const wallet = new MultiWallet(this.network);
|
|
@@ -2866,7 +2330,7 @@ class Peernet {
|
|
|
2866
2330
|
}
|
|
2867
2331
|
|
|
2868
2332
|
createHash(data, name) {
|
|
2869
|
-
return new
|
|
2333
|
+
return new CodecHash(data, {name})
|
|
2870
2334
|
}
|
|
2871
2335
|
|
|
2872
2336
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.25",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/commonjs/peernet.js",
|
|
6
6
|
"module": "dist/module/peernet.js",
|
|
@@ -23,6 +23,7 @@
|
|
|
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
29
|
"@leofcoin/peernet-swarm": "^0.3.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
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import protons from 'protons'
|
|
2
2
|
import proto from './../proto/dht-response.proto.js'
|
|
3
|
-
import
|
|
3
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
4
|
|
|
5
|
-
export default class DHTMessageResponse extends
|
|
5
|
+
export default class DHTMessageResponse extends FormatInterface {
|
|
6
6
|
get keys() {
|
|
7
7
|
return ['hash', 'has']
|
|
8
8
|
}
|
package/src/messages/dht.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import protons from 'protons'
|
|
2
2
|
import proto from './../proto/dht.proto.js'
|
|
3
|
-
import
|
|
3
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @example `
|
|
@@ -10,7 +10,7 @@ import CodecFormat from './../codec/codec-format-interface.js'
|
|
|
10
10
|
message = new DHTMessage('hashmvbs124xcfd...', 'block')
|
|
11
11
|
`
|
|
12
12
|
*/
|
|
13
|
-
export default class DHTMessage extends
|
|
13
|
+
export default class DHTMessage extends FormatInterface {
|
|
14
14
|
/**
|
|
15
15
|
*
|
|
16
16
|
*/
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import protons from 'protons'
|
|
2
2
|
import proto from './../proto/peer-response.proto.js'
|
|
3
|
-
import
|
|
3
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
4
|
|
|
5
|
-
export default class PeerMessageResponse extends
|
|
5
|
+
export default class PeerMessageResponse extends FormatInterface {
|
|
6
6
|
get keys() {
|
|
7
7
|
return ['id']
|
|
8
8
|
}
|