@leofcoin/peernet 0.10.6 → 0.11.0
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/LICENSE +1 -1
- package/README.md +49 -49
- package/dist/browser/peernet.js +101814 -92947
- package/dist/commonjs/client-bd0caeb7.js +324 -0
- package/dist/commonjs/{codec-6367213c.js → codec-4a768e5e.js} +99 -89
- package/dist/commonjs/codec-format-interface.js +41 -24
- package/dist/commonjs/codec.js +3 -3
- package/dist/commonjs/dht-response.js +3 -3
- package/dist/commonjs/dht.js +23 -23
- package/dist/commonjs/hash.js +17 -7
- package/dist/commonjs/{http-42a6e555.js → http-2b0735ef.js} +19 -15
- package/dist/commonjs/peernet-message.js +12 -12
- package/dist/commonjs/peernet.js +1164 -966
- package/dist/commonjs/request.js +12 -12
- package/dist/commonjs/response.js +12 -12
- package/dist/module/peernet.js +1325 -1120
- package/index.html +4 -6
- package/package.json +31 -7
- package/rollup.config.js +33 -5
- package/rollup0.config.js +7 -0
- package/src/codec/codec-format-interface.js +40 -23
- package/src/codec/codec.js +21 -11
- package/src/codec/codecs.js +79 -79
- package/src/handlers/message.js +52 -52
- package/src/hash/hash.js +16 -6
- package/src/http/client/http-client.js +1 -1
- package/src/messages/chat-message.js +14 -14
- package/src/messages/data-response.js +14 -14
- package/src/messages/data.js +18 -18
- package/src/messages/dht-response.js +0 -1
- package/src/messages/dht.js +25 -25
- package/src/messages/peer-response.js +14 -14
- package/src/messages/peer.js +14 -14
- package/src/messages/peernet-message.js +14 -14
- package/src/messages/ps.js +14 -14
- package/src/messages/request.js +14 -14
- package/src/messages/response.js +14 -14
- package/src/peernet.js +31 -116
- package/test/codec.js +3 -2
- package/test/messages.js +7 -4
- package/test.js +11 -4
- package/webpack.config.js +35 -0
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -79
- package/coverage/lcov-report/codec-format-interface.js.html +0 -533
- package/coverage/lcov-report/codec.js.html +0 -677
- package/coverage/lcov-report/dht-response.js.html +0 -188
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/hash.js.html +0 -551
- package/coverage/lcov-report/index.html +0 -156
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -170
- package/coverage/lcov.info +0 -459
- package/debug.log +0 -3
- package/dist/browser/peernet.js.tmp-browserify-14074318104595318069 +0 -0
- package/dist/browser/peernet.js.tmp-browserify-45407634493269122267 +0 -0
- package/dist/browser/peernet.js.tmp-browserify-53722389064799025427 +0 -0
- package/dist/browser/peernet.js.tmp-browserify-96323030449218949300 +0 -0
- package/dist/codec/codec-format-interface.js +0 -433
- package/dist/codec/codec.js +0 -199
- package/dist/commonjs/codec-73adfc0f.js +0 -205
- package/dist/commonjs/http-2c603501.js +0 -324
- package/dist/commonjs/http-43f4fafe.js +0 -324
- package/dist/commonjs/http-a94c5a81.js +0 -324
- package/dist/commonjs/peernet-message-b6925673.js +0 -32
- package/dist/hash/hash.js +0 -340
- package/dist/messages/dht-response.js +0 -454
- package/dist/messages/dht.js +0 -453
- package/dist/messages/peernet.js +0 -456
- package/dist/module/http-273664bd.js +0 -317
- package/dist/module/http-8fe3c0d7.js +0 -317
- package/dist/module/http-c780c991.js +0 -317
- package/dist/module/http-f13e0d77.js +0 -317
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/* socket-request-client version 1.6.1 */
|
|
4
|
+
|
|
5
|
+
class LittlePubSub {
|
|
6
|
+
constructor(verbose = true) {
|
|
7
|
+
this.subscribers = {};
|
|
8
|
+
this.verbose = verbose;
|
|
9
|
+
}
|
|
10
|
+
subscribe(event, handler, context) {
|
|
11
|
+
if (typeof context === 'undefined') {
|
|
12
|
+
context = handler;
|
|
13
|
+
}
|
|
14
|
+
this.subscribers[event] = this.subscribers[event] || { handlers: [], value: null};
|
|
15
|
+
this.subscribers[event].handlers.push(handler.bind(context));
|
|
16
|
+
}
|
|
17
|
+
unsubscribe(event, handler, context) {
|
|
18
|
+
if (typeof context === 'undefined') {
|
|
19
|
+
context = handler;
|
|
20
|
+
}
|
|
21
|
+
if (this.subscribers[event]) {
|
|
22
|
+
const index = this.subscribers[event].handlers.indexOf(handler.bind(context));
|
|
23
|
+
this.subscribers[event].handlers.splice(index);
|
|
24
|
+
if (this.subscribers[event].handlers.length === 0) delete this.subscribers[event];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
publish(event, change) {
|
|
28
|
+
if (this.subscribers[event]) {
|
|
29
|
+
if (this.verbose || this.subscribers[event].value !== change) {
|
|
30
|
+
this.subscribers[event].value = change;
|
|
31
|
+
this.subscribers[event].handlers.forEach(handler => {
|
|
32
|
+
handler(change, this.subscribers[event].value);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
var clientApi = _pubsub => {
|
|
40
|
+
const subscribe = (topic, cb) => {
|
|
41
|
+
_pubsub.subscribe(topic, cb);
|
|
42
|
+
};
|
|
43
|
+
const unsubscribe = (topic, cb) => {
|
|
44
|
+
_pubsub.unsubscribe(topic, cb);
|
|
45
|
+
};
|
|
46
|
+
const publish = (topic, value) => {
|
|
47
|
+
_pubsub.publish(topic, value);
|
|
48
|
+
};
|
|
49
|
+
const _connectionState = (state) => {
|
|
50
|
+
switch (state) {
|
|
51
|
+
case 0:
|
|
52
|
+
return 'connecting'
|
|
53
|
+
case 1:
|
|
54
|
+
return 'open'
|
|
55
|
+
case 2:
|
|
56
|
+
return 'closing'
|
|
57
|
+
case 3:
|
|
58
|
+
return 'closed'
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const request = (client, request) => {
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
const state = _connectionState(client.readyState);
|
|
64
|
+
if (state !== 'open') return reject(`coudn't send request to ${client.id}, no open connection found.`)
|
|
65
|
+
request.id = Math.random().toString(36).slice(-12);
|
|
66
|
+
const handler = result => {
|
|
67
|
+
if (result && result.error) return reject(result.error)
|
|
68
|
+
resolve({result, id: request.id, handler});
|
|
69
|
+
unsubscribe(request.id, handler);
|
|
70
|
+
};
|
|
71
|
+
subscribe(request.id, handler);
|
|
72
|
+
send(client, request);
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
const send = async (client, request) => {
|
|
76
|
+
return client.send(JSON.stringify(request))
|
|
77
|
+
};
|
|
78
|
+
const pubsub = client => {
|
|
79
|
+
return {
|
|
80
|
+
publish: (topic = 'pubsub', value) => {
|
|
81
|
+
return send(client, {url: 'pubsub', params: { topic, value }})
|
|
82
|
+
},
|
|
83
|
+
subscribe: (topic = 'pubsub', cb) => {
|
|
84
|
+
subscribe(topic, cb);
|
|
85
|
+
return send(client, {url: 'pubsub', params: { topic, subscribe: true }})
|
|
86
|
+
},
|
|
87
|
+
unsubscribe: (topic = 'pubsub', cb) => {
|
|
88
|
+
unsubscribe(topic, cb);
|
|
89
|
+
return send(client, {url: 'pubsub', params: { topic, unsubscribe: true }})
|
|
90
|
+
},
|
|
91
|
+
subscribers: _pubsub.subscribers
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const server = (client) => {
|
|
95
|
+
return {
|
|
96
|
+
uptime: async () => {
|
|
97
|
+
try {
|
|
98
|
+
const { result, id, handler } = await request(client, {url: 'uptime'});
|
|
99
|
+
unsubscribe(id, handler);
|
|
100
|
+
return result
|
|
101
|
+
} catch (e) {
|
|
102
|
+
throw e
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
ping: async () => {
|
|
106
|
+
try {
|
|
107
|
+
const now = new Date().getTime();
|
|
108
|
+
const { result, id, handler } = await request(client, {url: 'ping'});
|
|
109
|
+
unsubscribe(id, handler);
|
|
110
|
+
return (Number(result) - now)
|
|
111
|
+
} catch (e) {
|
|
112
|
+
throw e
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const peernet = (client) => {
|
|
118
|
+
return {
|
|
119
|
+
join: async (params) => {
|
|
120
|
+
try {
|
|
121
|
+
params.join = true;
|
|
122
|
+
const requested = { url: 'peernet', params };
|
|
123
|
+
const { result, id, handler } = await request(client, requested);
|
|
124
|
+
unsubscribe(id, handler);
|
|
125
|
+
return result
|
|
126
|
+
} catch (e) {
|
|
127
|
+
throw e
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
leave: async (params) => {
|
|
131
|
+
try {
|
|
132
|
+
params.join = false;
|
|
133
|
+
const requested = { url: 'peernet', params };
|
|
134
|
+
const { result, id, handler } = await request(client, requested);
|
|
135
|
+
unsubscribe(id, handler);
|
|
136
|
+
return result
|
|
137
|
+
} catch (e) {
|
|
138
|
+
throw e
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
return { send, request, pubsub, server, subscribe, unsubscribe, publish, peernet }
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
if (!globalThis.PubSub) globalThis.PubSub = LittlePubSub;
|
|
147
|
+
if (!globalThis.pubsub) globalThis.pubsub = new LittlePubSub({verbose: false});
|
|
148
|
+
const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry: false }) => {
|
|
149
|
+
const { retry } = options;
|
|
150
|
+
const api = clientApi(pubsub);
|
|
151
|
+
let tries = 0;
|
|
152
|
+
const onerror = error => {
|
|
153
|
+
if (pubsub.subscribers['error']) {
|
|
154
|
+
pubsub.publish('error', error);
|
|
155
|
+
} else {
|
|
156
|
+
console.error(error);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
const onmessage = message => {
|
|
160
|
+
const {value, url, status, id} = JSON.parse(message.data.toString());
|
|
161
|
+
const publisher = id ? id : url;
|
|
162
|
+
if (status === 200) {
|
|
163
|
+
pubsub.publish(publisher, value);
|
|
164
|
+
} else {
|
|
165
|
+
pubsub.publish(publisher, {error: value});
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
const clientConnection = client => {
|
|
169
|
+
const startTime = new Date().getTime();
|
|
170
|
+
return {
|
|
171
|
+
client,
|
|
172
|
+
request: async req => {
|
|
173
|
+
const { result, id, handler } = await api.request(client, req);
|
|
174
|
+
pubsub.unsubscribe(id, handler);
|
|
175
|
+
return result
|
|
176
|
+
},
|
|
177
|
+
send: req => api.send(client, req),
|
|
178
|
+
subscribe: api.subscribe,
|
|
179
|
+
unsubscribe: api.unsubscribe,
|
|
180
|
+
subscribers: api.subscribers,
|
|
181
|
+
publish: api.publish,
|
|
182
|
+
pubsub: api.pubsub(client),
|
|
183
|
+
uptime: () => {
|
|
184
|
+
const now = new Date().getTime();
|
|
185
|
+
return (now - startTime)
|
|
186
|
+
},
|
|
187
|
+
peernet: api.peernet(client),
|
|
188
|
+
server: api.server(client),
|
|
189
|
+
close: exit => {
|
|
190
|
+
client.onclose = message => {
|
|
191
|
+
if (exit) process.exit();
|
|
192
|
+
};
|
|
193
|
+
client.close();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
return new Promise((resolve, reject) => {
|
|
198
|
+
const init = () => {
|
|
199
|
+
let ws;
|
|
200
|
+
if (typeof process === 'object' && !globalThis.WebSocket) {
|
|
201
|
+
ws = require('websocket').w3cwebsocket;
|
|
202
|
+
} else {
|
|
203
|
+
ws = WebSocket;
|
|
204
|
+
}
|
|
205
|
+
const client = new ws(url, protocols);
|
|
206
|
+
client.onmessage = onmessage;
|
|
207
|
+
client.onerror = onerror;
|
|
208
|
+
client.onopen = () => {
|
|
209
|
+
tries = 0;
|
|
210
|
+
resolve(clientConnection(client));
|
|
211
|
+
};
|
|
212
|
+
client.onclose = message => {
|
|
213
|
+
tries++;
|
|
214
|
+
if (!retry) return reject(options)
|
|
215
|
+
if (tries > 5) {
|
|
216
|
+
console.log(`${protocols} Client Closed`);
|
|
217
|
+
console.error(`could not connect to - ${url}/`);
|
|
218
|
+
return resolve(clientConnection(client))
|
|
219
|
+
}
|
|
220
|
+
if (message.code === 1006) {
|
|
221
|
+
console.log('Retrying in 10 seconds');
|
|
222
|
+
setTimeout(() => {
|
|
223
|
+
return init();
|
|
224
|
+
}, retry);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
return init();
|
|
229
|
+
});
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
class HttpClientApi$1 {
|
|
233
|
+
constructor(config = {}) {
|
|
234
|
+
if (!config.apiPath) config.apiPath = 'api';
|
|
235
|
+
|
|
236
|
+
const address = `ws://${config.host}:${config.port}`;
|
|
237
|
+
|
|
238
|
+
this.apiUrl = (url) => `${address}/${url}`;
|
|
239
|
+
return (async () => {
|
|
240
|
+
this.client = await socketRequestClient(address, config.protocol, {pubsub: config.pubsub, retry: 3000});
|
|
241
|
+
return this
|
|
242
|
+
})()
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async get(url, obj) {
|
|
246
|
+
const headers = {};
|
|
247
|
+
let body = null;
|
|
248
|
+
let method = 'GET';
|
|
249
|
+
if (obj) {
|
|
250
|
+
method = 'POST';
|
|
251
|
+
headers['Content-Type'] = 'application/json';
|
|
252
|
+
body = JSON.stringify(obj);
|
|
253
|
+
}
|
|
254
|
+
let response = await this.client.request(url, {headers, body, method});
|
|
255
|
+
const type = response.headers.get('content-type').split(';')[0];
|
|
256
|
+
if (type==='application/json') response = await response.json();
|
|
257
|
+
return response
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async put(url, obj) {
|
|
261
|
+
const headers = {};
|
|
262
|
+
let body = {};
|
|
263
|
+
if (obj) {
|
|
264
|
+
headers['Content-Type'] = 'application/json';
|
|
265
|
+
body = JSON.stringify(obj);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
let response = await fetch(this.apiUrl(url), {method: 'PUT', headers, body});
|
|
269
|
+
const type = response.headers.get('content-type').split(';')[0];
|
|
270
|
+
if (type==='application/json') response = await response.json();
|
|
271
|
+
return response
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
class HttpClientApi extends HttpClientApi$1 {
|
|
276
|
+
constructor(config = {}) {
|
|
277
|
+
config.apiPath = 'api';
|
|
278
|
+
return (async () => {
|
|
279
|
+
await super(config);
|
|
280
|
+
|
|
281
|
+
this.properties = {
|
|
282
|
+
wallet: 'get',
|
|
283
|
+
version: 'get',
|
|
284
|
+
addresses: 'get',
|
|
285
|
+
config: 'get',
|
|
286
|
+
account: 'get',
|
|
287
|
+
accounts: 'get',
|
|
288
|
+
transaction: 'any',
|
|
289
|
+
transactions: 'get',
|
|
290
|
+
block: 'get',
|
|
291
|
+
blocks: 'get',
|
|
292
|
+
};
|
|
293
|
+
this.keys = Object.keys(this.properties);
|
|
294
|
+
return this
|
|
295
|
+
})()
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
async request(url, data) {
|
|
299
|
+
return await this.client.request({url, params: data})
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async ready() {
|
|
303
|
+
return await this.request('ready')
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
async version() {
|
|
307
|
+
return await this.request('version')
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
async account(index) {
|
|
311
|
+
return await this.request('account', {index})
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
var client = (config = {}) => {
|
|
316
|
+
if (typeof config !== 'object') config = {};
|
|
317
|
+
if (!config.protocol) config.protocol = 'peernet-v0.1.0';
|
|
318
|
+
if (!config.port) config.port = 1000;
|
|
319
|
+
if (!config.host) config.host = '127.0.0.1';
|
|
320
|
+
|
|
321
|
+
return new HttpClientApi(config)
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
exports["default"] = client;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var varint = require('varint');
|
|
4
|
-
var bs32 = require('
|
|
5
|
-
var bs58 = require('
|
|
4
|
+
var bs32 = require('@vandeurenglenn/base32');
|
|
5
|
+
var bs58 = require('@vandeurenglenn/base58');
|
|
6
6
|
var isHex = require('is-hex');
|
|
7
7
|
|
|
8
8
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
@@ -12,84 +12,84 @@ var bs32__default = /*#__PURE__*/_interopDefaultLegacy(bs32);
|
|
|
12
12
|
var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
|
|
13
13
|
var isHex__default = /*#__PURE__*/_interopDefaultLegacy(isHex);
|
|
14
14
|
|
|
15
|
-
var codecs = {
|
|
16
|
-
// just a hash
|
|
17
|
-
'disco-hash': {
|
|
18
|
-
codec: '30',
|
|
19
|
-
hashAlg: 'dbl-keccak-512', // ,
|
|
20
|
-
// testnet: 'olivia'
|
|
21
|
-
},
|
|
22
|
-
'peernet-peer-response': {
|
|
23
|
-
codec: '707072',
|
|
24
|
-
hashAlg: 'keccak-256',
|
|
25
|
-
},
|
|
26
|
-
'peernet-peer': {
|
|
27
|
-
codec: '7070',
|
|
28
|
-
hashAlg: 'keccak-256',
|
|
29
|
-
},
|
|
30
|
-
'peernet-dht': {
|
|
31
|
-
codec: '706468',
|
|
32
|
-
hashAlg: 'keccak-256',
|
|
33
|
-
},
|
|
34
|
-
'peernet-dht-response': {
|
|
35
|
-
codec: '706472',
|
|
36
|
-
hashAlg: 'keccak-256',
|
|
37
|
-
},
|
|
38
|
-
// data
|
|
39
|
-
'peernet-data': {
|
|
40
|
-
codec: '706461',
|
|
41
|
-
hashAlg: 'keccak-256',
|
|
42
|
-
},
|
|
43
|
-
'peernet-data-response': {
|
|
44
|
-
codec: '70646172',
|
|
45
|
-
hashAlg: 'keccak-256',
|
|
46
|
-
},
|
|
47
|
-
// message
|
|
48
|
-
'peernet-message': {
|
|
49
|
-
codec: '706d65',
|
|
50
|
-
hashAlg: 'keccak-512',
|
|
51
|
-
},
|
|
52
|
-
// pubsub
|
|
53
|
-
'peernet-ps': {
|
|
54
|
-
codec: '707073',
|
|
55
|
-
hashAlg: 'keccak-256',
|
|
56
|
-
},
|
|
57
|
-
'peernet-response': {
|
|
58
|
-
codec: '7072',
|
|
59
|
-
hashAlg: 'keccak-256',
|
|
60
|
-
},
|
|
61
|
-
'peernet-request': {
|
|
62
|
-
codec: '707271',
|
|
63
|
-
hashAlg: 'keccak-256',
|
|
64
|
-
},
|
|
65
|
-
// normal block
|
|
66
|
-
'leofcoin-block': {
|
|
67
|
-
codec: '6c62',
|
|
68
|
-
hashAlg: 'dbl-keccak-512', // ,
|
|
69
|
-
// testnet: 'olivia'
|
|
70
|
-
},
|
|
71
|
-
'leofcoin-tx': {
|
|
72
|
-
codec: '6c74',
|
|
73
|
-
hashAlg: 'dbl-keccak-512', // ,
|
|
74
|
-
// testnet: 'olivia'
|
|
75
|
-
},
|
|
76
|
-
// itx
|
|
77
|
-
'leofcoin-itx': {
|
|
78
|
-
codec: '6c69',
|
|
79
|
-
hashAlg: 'keccak-512', // ,
|
|
80
|
-
// testnet: 'olivia'
|
|
81
|
-
},
|
|
82
|
-
// peer reputation
|
|
83
|
-
'leofcoin-pr': {
|
|
84
|
-
codec: '6c70',
|
|
85
|
-
hashAlg: 'keccak-256', // ,
|
|
86
|
-
// testnet: 'olivia'
|
|
87
|
-
},
|
|
88
|
-
// chat message
|
|
89
|
-
'chat-message': {
|
|
90
|
-
codec: '636d',
|
|
91
|
-
hashAlg: 'dbl-keccak-512',
|
|
92
|
-
},
|
|
15
|
+
var codecs = {
|
|
16
|
+
// just a hash
|
|
17
|
+
'disco-hash': {
|
|
18
|
+
codec: parseInt('30', 16),
|
|
19
|
+
hashAlg: 'dbl-keccak-512', // ,
|
|
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-512',
|
|
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-512',
|
|
92
|
+
},
|
|
93
93
|
};
|
|
94
94
|
|
|
95
95
|
class PeernetCodec {
|
|
@@ -98,10 +98,9 @@ class PeernetCodec {
|
|
|
98
98
|
}
|
|
99
99
|
constructor(buffer) {
|
|
100
100
|
if (buffer) {
|
|
101
|
-
if (
|
|
101
|
+
if (buffer instanceof Uint8Array) {
|
|
102
102
|
const codec = varint__default["default"].decode(buffer);
|
|
103
103
|
const name = this.getCodecName(codec);
|
|
104
|
-
|
|
105
104
|
if (name) {
|
|
106
105
|
this.name = name;
|
|
107
106
|
this.encoded = buffer;
|
|
@@ -109,12 +108,23 @@ class PeernetCodec {
|
|
|
109
108
|
} else {
|
|
110
109
|
this.encode(buffer);
|
|
111
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
|
|
112
121
|
}
|
|
113
122
|
if (typeof buffer === 'string') {
|
|
114
123
|
if (this.codecs[buffer]) this.fromName(buffer);
|
|
115
124
|
else if (isHex__default["default"](buffer)) this.fromHex(buffer);
|
|
116
|
-
else if (bs32__default["default"].
|
|
117
|
-
else this.fromBs58(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}`)
|
|
118
128
|
}
|
|
119
129
|
if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer);
|
|
120
130
|
}
|
|
@@ -149,7 +159,8 @@ class PeernetCodec {
|
|
|
149
159
|
|
|
150
160
|
getCodecName(codec) {
|
|
151
161
|
return Object.keys(this.codecs).reduce((p, c) => {
|
|
152
|
-
|
|
162
|
+
const item = this.codecs[c];
|
|
163
|
+
if (item.codec === codec) return c;
|
|
153
164
|
else return p;
|
|
154
165
|
}, undefined)
|
|
155
166
|
}
|
|
@@ -163,7 +174,7 @@ class PeernetCodec {
|
|
|
163
174
|
this.hashAlg = this.getHashAlg(this.name);
|
|
164
175
|
|
|
165
176
|
this.codec = this.getCodec(this.name);
|
|
166
|
-
this.codecBuffer =
|
|
177
|
+
this.codecBuffer = varint__default["default"].encode(codec);
|
|
167
178
|
}
|
|
168
179
|
|
|
169
180
|
fromName(name) {
|
|
@@ -171,7 +182,7 @@ class PeernetCodec {
|
|
|
171
182
|
this.name = name;
|
|
172
183
|
this.codec = codec;
|
|
173
184
|
this.hashAlg = this.getHashAlg(name);
|
|
174
|
-
this.codecBuffer =
|
|
185
|
+
this.codecBuffer = varint__default["default"].encode(codec);
|
|
175
186
|
}
|
|
176
187
|
|
|
177
188
|
toBs32() {
|
|
@@ -191,11 +202,10 @@ class PeernetCodec {
|
|
|
191
202
|
decode() {
|
|
192
203
|
const codec = varint__default["default"].decode(this.encoded);
|
|
193
204
|
this.fromCodec(codec);
|
|
194
|
-
this.name = this.getCodecName(codec);
|
|
195
205
|
}
|
|
196
206
|
|
|
197
207
|
encode() {
|
|
198
|
-
const codec =
|
|
208
|
+
const codec = varint__default["default"].encode(this.decoded);
|
|
199
209
|
this.encoded = codec;
|
|
200
210
|
return this.encoded
|
|
201
211
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var bs32 = require('
|
|
4
|
-
var bs58 = require('
|
|
3
|
+
var bs32 = require('@vandeurenglenn/base32');
|
|
4
|
+
var bs58 = require('@vandeurenglenn/base58');
|
|
5
5
|
var isHex = require('is-hex');
|
|
6
|
-
var codec = require('./codec-
|
|
6
|
+
var codec = require('./codec-4a768e5e.js');
|
|
7
7
|
var hash = require('./hash.js');
|
|
8
8
|
require('varint');
|
|
9
9
|
require('keccak');
|
|
@@ -23,25 +23,18 @@ class FormatInterface {
|
|
|
23
23
|
constructor(buffer, proto, options = {}) {
|
|
24
24
|
this.protoEncode = proto.encode;
|
|
25
25
|
this.protoDecode = proto.decode;
|
|
26
|
-
if (options.name) this.name = options.name;
|
|
27
26
|
this.hashFormat = options.hashFormat || 'bs32';
|
|
28
|
-
if (
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
this.fromEncoded(buffer);
|
|
34
|
-
} else {
|
|
35
|
-
this.create(buffer);
|
|
36
|
-
}
|
|
37
|
-
} else {
|
|
38
|
-
if (typeof buffer === 'string') {
|
|
27
|
+
if (options.name) this.name = options.name;
|
|
28
|
+
if (buffer instanceof Uint8Array) return this.fromUint8Array(buffer)
|
|
29
|
+
else if (buffer instanceof ArrayBuffer) return this.fromArrayBuffer(buffer)
|
|
30
|
+
else if (buffer.name === options.name) return buffer
|
|
31
|
+
else if (typeof buffer === 'string') {
|
|
39
32
|
if (isHex__default["default"](buffer)) this.fromHex(buffer);
|
|
40
|
-
else if (bs32__default["default"].
|
|
41
|
-
else this.fromBs58(buffer);
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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);
|
|
45
38
|
}
|
|
46
39
|
}
|
|
47
40
|
|
|
@@ -66,7 +59,7 @@ class FormatInterface {
|
|
|
66
59
|
*/
|
|
67
60
|
decode() {
|
|
68
61
|
let encoded = this.encoded;
|
|
69
|
-
const discoCodec = new codec.PeernetCodec(this.encoded
|
|
62
|
+
const discoCodec = new codec.PeernetCodec(this.encoded);
|
|
70
63
|
encoded = encoded.slice(discoCodec.codecBuffer.length);
|
|
71
64
|
this.name = discoCodec.name;
|
|
72
65
|
this.decoded = this.protoDecode(encoded);
|
|
@@ -79,16 +72,40 @@ class FormatInterface {
|
|
|
79
72
|
encode(decoded) {
|
|
80
73
|
if (!decoded) decoded = this.decoded;
|
|
81
74
|
const codec$1 = new codec.PeernetCodec(this.name);
|
|
82
|
-
|
|
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;
|
|
83
80
|
return this.encoded
|
|
84
81
|
}
|
|
85
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
|
+
|
|
86
105
|
/**
|
|
87
106
|
* @param {Buffer} encoded
|
|
88
107
|
*/
|
|
89
108
|
fromEncoded(encoded) {
|
|
90
|
-
const codec$1 = new codec.PeernetCodec(encoded);
|
|
91
|
-
this.name = codec$1.name;
|
|
92
109
|
this.encoded = encoded;
|
|
93
110
|
this.decode();
|
|
94
111
|
}
|
package/dist/commonjs/codec.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
require('varint');
|
|
4
|
-
require('
|
|
5
|
-
require('
|
|
4
|
+
require('@vandeurenglenn/base32');
|
|
5
|
+
require('@vandeurenglenn/base58');
|
|
6
6
|
require('is-hex');
|
|
7
|
-
var codec = require('./codec-
|
|
7
|
+
var codec = require('./codec-4a768e5e.js');
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|