@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.11.22",
3
+ "version": "0.11.25",
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.2.0",
29
+ "@leofcoin/peernet-swarm": "^0.3.0",
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/codec/codec.js', 'src/hash/hash.js', 'src/messages/dht.js',
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', 'src/codec/codec-format-interface.js'],
14
+ 'src/messages/response.js'],
15
15
  output: {
16
16
  dir: 'dist/commonjs',
17
17
  format: 'cjs'
@@ -1,5 +1,5 @@
1
1
  import MultiWallet from './../../node_modules/@leofcoin/multi-wallet/src/index'
2
- import Hash from './../hash/hash.js'
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 CodecFormat from './../codec/codec-format-interface.js'
3
+ import { FormatInterface } from '@leofcoin/codec-format-interface'
4
4
 
5
- export default class ChatMessage extends CodecFormat {
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 CodecFormat from './../codec/codec-format-interface.js'
3
+ import { FormatInterface } from '@leofcoin/codec-format-interface'
4
4
 
5
- export default class DataMessageResponse extends CodecFormat {
5
+ export default class DataMessageResponse extends FormatInterface {
6
6
  get keys() {
7
7
  return ['hash', 'data']
8
8
  }
@@ -1,11 +1,11 @@
1
1
  import protons from 'protons'
2
2
  import proto from './../proto/data.proto.js'
3
- import CodecFormat from './../codec/codec-format-interface.js'
3
+ import { FormatInterface } from '@leofcoin/codec-format-interface'
4
4
 
5
5
  /**
6
6
  * @extends {CodecFormat}
7
7
  */
8
- export default class DataMessage extends CodecFormat {
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 CodecFormat from './../codec/codec-format-interface.js'
3
+ import { FormatInterface } from '@leofcoin/codec-format-interface'
4
4
 
5
- export default class DHTMessageResponse extends CodecFormat {
5
+ export default class DHTMessageResponse extends FormatInterface {
6
6
  get keys() {
7
7
  return ['hash', 'has']
8
8
  }
@@ -1,6 +1,6 @@
1
1
  import protons from 'protons'
2
2
  import proto from './../proto/dht.proto.js'
3
- import CodecFormat from './../codec/codec-format-interface.js'
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 CodecFormat {
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 CodecFormat from './../codec/codec-format-interface.js'
3
+ import { FormatInterface } from '@leofcoin/codec-format-interface'
4
4
 
5
- export default class PeerMessageResponse extends CodecFormat {
5
+ export default class PeerMessageResponse extends FormatInterface {
6
6
  get keys() {
7
7
  return ['id']
8
8
  }
@@ -1,8 +1,8 @@
1
1
  import protons from 'protons'
2
2
  import proto from './../proto/peer.proto.js'
3
- import CodecFormat from './../codec/codec-format-interface.js'
3
+ import { FormatInterface } from '@leofcoin/codec-format-interface'
4
4
 
5
- export default class PeerMessage extends CodecFormat {
5
+ export default class PeerMessage extends FormatInterface {
6
6
  get keys() {
7
7
  return ['id']
8
8
  }
@@ -1,8 +1,8 @@
1
1
  import protons from 'protons'
2
2
  import proto from './../proto/peernet.proto.js'
3
- import CodecFormat from './../codec/codec-format-interface.js'
3
+ import { FormatInterface } from '@leofcoin/codec-format-interface'
4
4
 
5
- export default class PeernetMessage extends CodecFormat {
5
+ export default class PeernetMessage extends FormatInterface {
6
6
  get keys() {
7
7
  return ['data', 'signature', 'from', 'to', 'id']
8
8
  }
@@ -1,8 +1,8 @@
1
1
  import protons from 'protons'
2
2
  import proto from './../proto/ps.proto.js'
3
- import CodecFormat from './../codec/codec-format-interface.js'
3
+ import { FormatInterface } from '@leofcoin/codec-format-interface'
4
4
 
5
- export default class PsMessage extends CodecFormat {
5
+ export default class PsMessage extends FormatInterface {
6
6
  get keys() {
7
7
  return ['data', 'topic']
8
8
  }
@@ -1,8 +1,8 @@
1
1
  import protons from 'protons'
2
2
  import proto from './../proto/request.proto.js'
3
- import CodecFormat from './../codec/codec-format-interface.js'
3
+ import { FormatInterface } from '@leofcoin/codec-format-interface'
4
4
 
5
- export default class RequestMessage extends CodecFormat {
5
+ export default class RequestMessage extends FormatInterface {
6
6
  get keys() {
7
7
  return ['request']
8
8
  }
@@ -1,8 +1,8 @@
1
1
  import protons from 'protons'
2
2
  import proto from './../proto/response.proto.js'
3
- import CodecFormat from './../codec/codec-format-interface.js'
3
+ import { FormatInterface } from '@leofcoin/codec-format-interface'
4
4
 
5
- export default class ResponseMessage extends CodecFormat {
5
+ export default class ResponseMessage extends FormatInterface {
6
6
  get keys() {
7
7
  return ['response']
8
8
  }
package/src/peernet.js CHANGED
@@ -15,8 +15,7 @@ import DataMessageResponse from './messages/data-response.js'
15
15
  import ChatMessage from './messages/chat-message.js'
16
16
  import PeerDiscovery from './discovery/peer-discovery'
17
17
  import DHT from './dht/dht.js'
18
- import Hash from './hash/hash'
19
- import codecs from './codec/codecs'
18
+ import { CodecHash as Hash, codecs} from '@leofcoin/codec-format-interface'
20
19
  import { protoFor, target } from './utils/utils.js'
21
20
  import generateAccount from './../node_modules/@leofcoin/generate-account/dist/module/generate-account'
22
21
  import MessageHandler from './handlers/message.js'
@@ -1,5 +1,6 @@
1
1
  import fetch from 'node-fetch'
2
- import Codec from './../codec/codec.js'
2
+
3
+ import { Codec } from '@leofcoin/codec-format-interface'
3
4
 
4
5
  export const expected = (expected, actual) => {
5
6
  const rule = (entry) => {
@@ -1,215 +0,0 @@
1
- 'use strict';
2
-
3
- var varint = require('varint');
4
- var bs32 = require('@vandeurenglenn/base32');
5
- var bs58 = require('@vandeurenglenn/base58');
6
- var isHex = require('@vandeurenglenn/is-hex');
7
-
8
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
-
10
- var varint__default = /*#__PURE__*/_interopDefaultLegacy(varint);
11
- var bs32__default = /*#__PURE__*/_interopDefaultLegacy(bs32);
12
- var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
13
- var isHex__default = /*#__PURE__*/_interopDefaultLegacy(isHex);
14
-
15
- var codecs = {
16
- // just a hash
17
- 'disco-hash': {
18
- codec: parseInt('30', 16),
19
- hashAlg: 'dbl-keccak-256', // ,
20
- // testnet: 'olivia'
21
- },
22
- 'peernet-peer-response': {
23
- codec: parseInt('707072', 16),
24
- hashAlg: 'keccak-256',
25
- },
26
- 'peernet-peer': {
27
- codec: parseInt('7070', 16),
28
- hashAlg: 'keccak-256',
29
- },
30
- 'peernet-dht': {
31
- codec: parseInt('706468', 16),
32
- hashAlg: 'keccak-256',
33
- },
34
- 'peernet-dht-response': {
35
- codec: parseInt('706472', 16),
36
- hashAlg: 'keccak-256',
37
- },
38
- // data
39
- 'peernet-data': {
40
- codec: parseInt('706461', 16),
41
- hashAlg: 'keccak-256',
42
- },
43
- 'peernet-data-response': {
44
- codec: parseInt('70646172', 16),
45
- hashAlg: 'keccak-256',
46
- },
47
- // message
48
- 'peernet-message': {
49
- codec: parseInt('706d65', 16),
50
- hashAlg: 'keccak-256',
51
- },
52
- // pubsub
53
- 'peernet-ps': {
54
- codec: parseInt('707073', 16),
55
- hashAlg: 'keccak-256',
56
- },
57
- 'peernet-response': {
58
- codec: parseInt('7072', 16),
59
- hashAlg: 'keccak-256',
60
- },
61
- 'peernet-request': {
62
- codec: parseInt('707271', 16),
63
- hashAlg: 'keccak-256',
64
- },
65
- // normal block
66
- 'leofcoin-block': {
67
- codec: parseInt('6c62', 16),
68
- hashAlg: 'dbl-keccak-512', // ,
69
- // testnet: 'olivia'
70
- },
71
- 'leofcoin-tx': {
72
- codec: parseInt('6c74', 16),
73
- hashAlg: 'dbl-keccak-512', // ,
74
- // testnet: 'olivia'
75
- },
76
- // itx
77
- 'leofcoin-itx': {
78
- codec: parseInt('6c69', 16),
79
- hashAlg: 'keccak-512', // ,
80
- // testnet: 'olivia'
81
- },
82
- // peer reputation
83
- 'leofcoin-pr': {
84
- codec: parseInt('6c70', 16),
85
- hashAlg: 'keccak-256', // ,
86
- // testnet: 'olivia'
87
- },
88
- // chat message
89
- 'chat-message': {
90
- codec: parseInt('636d', 16),
91
- hashAlg: 'dbl-keccak-256',
92
- },
93
- };
94
-
95
- class PeernetCodec {
96
- get codecs() {
97
- return {...globalThis.peernet.codecs, ...codecs}
98
- }
99
- constructor(buffer) {
100
- if (buffer) {
101
- if (buffer instanceof Uint8Array) {
102
- const codec = varint__default["default"].decode(buffer);
103
- const name = this.getCodecName(codec);
104
- if (name) {
105
- this.name = name;
106
- this.encoded = buffer;
107
- this.decode(buffer);
108
- } else {
109
- this.encode(buffer);
110
- }
111
- } else if (buffer instanceof ArrayBuffer) {
112
- const encoded = new Uint8Array(buffer.byteLength);
113
-
114
- for (let i = 0; i < buffer.byteLength; i++) {
115
- encoded[i] = buffer[i];
116
- }
117
- this.encoded = encoded;
118
- // this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
119
- this.decode(buffer);
120
- return
121
- }
122
- if (typeof buffer === 'string') {
123
- if (this.codecs[buffer]) this.fromName(buffer);
124
- else if (isHex__default["default"](buffer)) this.fromHex(buffer);
125
- else if (bs32__default["default"].isBase32(buffer)) this.fromBs32(buffer);
126
- else if (bs58__default["default"].isBase58(buffer)) this.fromBs58(buffer);
127
- else throw new Error(`unsupported string ${buffer}`)
128
- }
129
- if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer);
130
- }
131
- }
132
-
133
- fromEncoded(encoded) {
134
- const codec = varint__default["default"].decode(encoded);
135
- const name = this.getCodecName(codec);
136
- this.name = name;
137
- this.encoded = encoded;
138
- this.decode(encoded);
139
- }
140
-
141
- fromHex(hex) {
142
- this.encoded = Buffer.from(hex, 'hex');
143
- this.decode();
144
- }
145
-
146
- fromBs32(input) {
147
- this.encoded = bs32__default["default"].decode(input);
148
- this.decode();
149
- }
150
-
151
- fromBs58(input) {
152
- this.encoded = bs58__default["default"].decode(input);
153
- this.decode();
154
- }
155
-
156
- getCodec(name) {
157
- return this.codecs[name].codec
158
- }
159
-
160
- getCodecName(codec) {
161
- return Object.keys(this.codecs).reduce((p, c) => {
162
- const item = this.codecs[c];
163
- if (item.codec === codec) return c;
164
- else return p;
165
- }, undefined)
166
- }
167
-
168
- getHashAlg(name) {
169
- return this.codecs[name].hashAlg
170
- }
171
-
172
- fromCodec(codec) {
173
- this.name = this.getCodecName(codec);
174
- this.hashAlg = this.getHashAlg(this.name);
175
-
176
- this.codec = this.getCodec(this.name);
177
- this.codecBuffer = varint__default["default"].encode(codec);
178
- }
179
-
180
- fromName(name) {
181
- const codec = this.getCodec(name);
182
- this.name = name;
183
- this.codec = codec;
184
- this.hashAlg = this.getHashAlg(name);
185
- this.codecBuffer = varint__default["default"].encode(codec);
186
- }
187
-
188
- toBs32() {
189
- this.encode();
190
- return bs32__default["default"].encode(this.encoded)
191
- }
192
-
193
- toBs58() {
194
- this.encode();
195
- return bs58__default["default"].encode(this.encoded)
196
- }
197
-
198
- toHex() {
199
- return this.encoded.toString('hex')
200
- }
201
-
202
- decode() {
203
- const codec = varint__default["default"].decode(this.encoded);
204
- this.fromCodec(codec);
205
- }
206
-
207
- encode() {
208
- const codec = varint__default["default"].encode(this.decoded);
209
- this.encoded = codec;
210
- return this.encoded
211
- }
212
- }
213
-
214
- exports.PeernetCodec = PeernetCodec;
215
- exports.codecs = codecs;
@@ -1,206 +0,0 @@
1
- 'use strict';
2
-
3
- var bs32 = require('@vandeurenglenn/base32');
4
- var bs58 = require('@vandeurenglenn/base58');
5
- var isHex = require('@vandeurenglenn/is-hex');
6
- var codec = require('./codec-45796010.js');
7
- var hash = require('./hash.js');
8
- require('varint');
9
- require('keccak');
10
-
11
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
-
13
- var bs32__default = /*#__PURE__*/_interopDefaultLegacy(bs32);
14
- var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
15
- var isHex__default = /*#__PURE__*/_interopDefaultLegacy(isHex);
16
-
17
- class FormatInterface {
18
- /**
19
- * @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
20
- * @param {Object} proto - {encode, decode}
21
- * @param {Object} options - {hashFormat, name}
22
- */
23
- constructor(buffer, proto, options = {}) {
24
- this.protoEncode = proto.encode;
25
- this.protoDecode = proto.decode;
26
- this.hashFormat = options.hashFormat || 'bs32';
27
- if (options.name) this.name = options.name;
28
- if (buffer instanceof Uint8Array) this.fromUint8Array(buffer);
29
- else if (buffer instanceof ArrayBuffer) this.fromArrayBuffer(buffer);
30
- else if (buffer.name === options.name) return buffer
31
- else if (buffer instanceof String) {
32
- if (isHex__default["default"](buffer)) this.fromHex(buffer);
33
- else if (bs32__default["default"].isBase32(buffer)) this.fromBs32(buffer);
34
- else if (bs58__default["default"].isBase58(buffer)) this.fromBs58(buffer);
35
- else throw new Error(`unsupported string ${buffer}`)
36
- } else {
37
- this.create(buffer);
38
- }
39
- }
40
-
41
- /**
42
- * @return {PeernetHash}
43
- */
44
- get peernetHash() {
45
- return new hash(this.decoded, {name: this.name})
46
- }
47
-
48
- /**
49
- * @return {peernetHash}
50
- */
51
- get hash() {
52
- const upper = this.hashFormat.charAt(0).toUpperCase();
53
- const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
54
- return this.peernetHash[`to${format}`]()
55
- }
56
-
57
- /**
58
- * @return {Object}
59
- */
60
- decode() {
61
- let encoded = this.encoded;
62
- const discoCodec = new codec.PeernetCodec(this.encoded);
63
- encoded = encoded.slice(discoCodec.codecBuffer.length);
64
- this.name = discoCodec.name;
65
- this.decoded = this.protoDecode(encoded);
66
- return this.decoded
67
- }
68
-
69
- /**
70
- * @return {Buffer}
71
- */
72
- encode(decoded) {
73
- if (!decoded) decoded = this.decoded;
74
- const codec$1 = new codec.PeernetCodec(this.name);
75
- const encoded = this.protoEncode(decoded);
76
- const uint8Array = new Uint8Array(encoded.length + codec$1.codecBuffer.length);
77
- uint8Array.set(codec$1.codecBuffer);
78
- uint8Array.set(encoded, codec$1.codecBuffer.length);
79
- this.encoded = uint8Array;
80
- return this.encoded
81
- }
82
-
83
- hasCodec() {
84
- if (!this.encoded) return false
85
- const codec$1 = new codec.PeernetCodec(this.encoded);
86
- if (codec$1.name) return true
87
- }
88
-
89
- fromUint8Array(buffer) {
90
- this.encoded = buffer;
91
- if (!this.hasCodec()) this.create(
92
- JSON.parse(new TextDecoder().decode(this.encoded))
93
- );
94
- else this.decode();
95
- }
96
-
97
- fromArrayBuffer(buffer) {
98
- this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
99
- if (!this.hasCodec()) this.create(
100
- JSON.parse(new TextDecoder().decode(this.encoded))
101
- );
102
- else this.decode();
103
- }
104
-
105
- toString() {
106
- return this.encoded.toString()
107
- }
108
-
109
- async toArray() {
110
- const array = [];
111
- for await (const value of this.encoded.values()) {
112
- array.push(value);
113
- }
114
- return array
115
- }
116
-
117
- fromString(string) {
118
- this.encoded = new Uint8Array(string.split(','));
119
- this.decode();
120
- }
121
-
122
- fromArray(array) {
123
- this.encoded = new Uint8Array([...array]);
124
- this.decode();
125
- }
126
-
127
- /**
128
- * @param {Buffer} encoded
129
- */
130
- fromEncoded(encoded) {
131
- this.encoded = encoded;
132
- this.decode();
133
- }
134
-
135
- /**
136
- * @param {String} encoded
137
- */
138
- fromHex(encoded) {
139
- this.encoded = Buffer.from(encoded, 'hex');
140
- this.decode();
141
- }
142
-
143
- /**
144
- * @param {String} encoded
145
- */
146
- fromBs32(encoded) {
147
- this.encoded = bs32__default["default"].decode(encoded);
148
- this.decode();
149
- }
150
-
151
- /**
152
- * @param {String} encoded
153
- */
154
- fromBs58(encoded) {
155
- this.encoded = bs58__default["default"].decode(encoded);
156
- this.decode();
157
- }
158
-
159
- /**
160
- * @return {String} encoded
161
- */
162
- toHex() {
163
- if (!this.encoded) this.encode();
164
- return this.encoded.toString('hex')
165
- }
166
-
167
- /**
168
- * @return {String} encoded
169
- */
170
- toBs32() {
171
- if (!this.encoded) this.encode();
172
- return bs32__default["default"].encode(this.encoded)
173
- }
174
-
175
- /**
176
- * @return {String} encoded
177
- */
178
- toBs58() {
179
- if (!this.encoded) this.encode();
180
- return bs58__default["default"].encode(this.encoded)
181
- }
182
-
183
- /**
184
- * @param {Object} data
185
- */
186
- create(data) {
187
- const decoded = {};
188
- if (this.keys?.length > 0) {
189
- for (const key of this.keys) {
190
- Object.defineProperties(decoded, {
191
- [key]: {
192
- enumerable: true,
193
- configurable: true,
194
- set: (val) => value = data[key],
195
- get: () => data[key]
196
- }
197
- });
198
- }
199
-
200
- this.decoded = decoded;
201
- this.encode();
202
- }
203
- }
204
- }
205
-
206
- module.exports = FormatInterface;
@@ -1,11 +0,0 @@
1
- 'use strict';
2
-
3
- require('varint');
4
- require('@vandeurenglenn/base32');
5
- require('@vandeurenglenn/base58');
6
- require('@vandeurenglenn/is-hex');
7
- var codec = require('./codec-45796010.js');
8
-
9
-
10
-
11
- module.exports = codec.PeernetCodec;