@leofcoin/peernet 0.13.3 → 0.14.1

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.
@@ -4,8 +4,6 @@ require('@vandeurenglenn/debug');
4
4
  var PubSub = require('@vandeurenglenn/little-pubsub');
5
5
  var fetch = require('node-fetch');
6
6
  var codecFormatInterface = require('@leofcoin/codec-format-interface');
7
- var MultiWallet = require('@leofcoin/multi-wallet');
8
- require('path');
9
7
 
10
8
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
9
 
@@ -29,7 +27,6 @@ function _interopNamespace(e) {
29
27
 
30
28
  var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
31
29
  var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
32
- var MultiWallet__default = /*#__PURE__*/_interopDefaultLegacy(MultiWallet);
33
30
 
34
31
  const protoFor = (data) => {
35
32
  if (!Buffer.isBuffer(data)) data = Buffer.from(data);
@@ -282,6 +279,94 @@ class DhtEarth {
282
279
  }
283
280
  }
284
281
 
282
+ var codecs = {
283
+ // just a hash
284
+ 'disco-hash': {
285
+ codec: parseInt('30', 16),
286
+ hashAlg: 'dbl-keccak-256', // ,
287
+ // testnet: 'olivia'
288
+ },
289
+ 'peernet-peer-response': {
290
+ codec: parseInt('707072', 16),
291
+ hashAlg: 'keccak-256',
292
+ },
293
+ 'peernet-peer': {
294
+ codec: parseInt('7070', 16),
295
+ hashAlg: 'keccak-256',
296
+ },
297
+ 'peernet-dht': {
298
+ codec: parseInt('706468', 16),
299
+ hashAlg: 'keccak-256',
300
+ },
301
+ 'peernet-dht-response': {
302
+ codec: parseInt('706472', 16),
303
+ hashAlg: 'keccak-256',
304
+ },
305
+ // data
306
+ 'peernet-data': {
307
+ codec: parseInt('706461', 16),
308
+ hashAlg: 'keccak-256',
309
+ },
310
+ 'peernet-data-response': {
311
+ codec: parseInt('70646172', 16),
312
+ hashAlg: 'keccak-256',
313
+ },
314
+ // message
315
+ 'peernet-message': {
316
+ codec: parseInt('706d65', 16),
317
+ hashAlg: 'keccak-256',
318
+ },
319
+ // pubsub
320
+ 'peernet-ps': {
321
+ codec: parseInt('707073', 16),
322
+ hashAlg: 'keccak-256',
323
+ },
324
+ 'peernet-response': {
325
+ codec: parseInt('7072', 16),
326
+ hashAlg: 'keccak-256',
327
+ },
328
+ 'peernet-request': {
329
+ codec: parseInt('707271', 16),
330
+ hashAlg: 'keccak-256',
331
+ },
332
+ // normal block
333
+ 'leofcoin-block': {
334
+ codec: parseInt('6c62', 16),
335
+ hashAlg: 'dbl-keccak-512', // ,
336
+ // testnet: 'olivia'
337
+ },
338
+ 'leofcoin-tx': {
339
+ codec: parseInt('6c74', 16),
340
+ hashAlg: 'dbl-keccak-512', // ,
341
+ // testnet: 'olivia'
342
+ },
343
+ // itx
344
+ 'leofcoin-itx': {
345
+ codec: parseInt('6c69', 16),
346
+ hashAlg: 'keccak-512', // ,
347
+ // testnet: 'olivia'
348
+ },
349
+ // peer reputation
350
+ 'leofcoin-pr': {
351
+ codec: parseInt('6c70', 16),
352
+ hashAlg: 'keccak-256', // ,
353
+ // testnet: 'olivia'
354
+ },
355
+ // chat message
356
+ 'chat-message': {
357
+ codec: parseInt('636d', 16),
358
+ hashAlg: 'dbl-keccak-256',
359
+ },
360
+ 'peernet-file' : {
361
+ codec: parseInt('7066', 16),
362
+ hashAlg: 'keccak-256',
363
+ },
364
+ 'peernet-file-response' : {
365
+ codec: parseInt('706672', 16),
366
+ hashAlg: 'keccak-256',
367
+ }
368
+ };
369
+
285
370
  class MessageHandler {
286
371
  constructor(network) {
287
372
  this.network = network;
@@ -299,8 +384,12 @@ class MessageHandler {
299
384
  async hashAndSignMessage(message) {
300
385
  const hasher = new codecFormatInterface.CodecHash(message, {name: 'peernet-message'});
301
386
  let identity = await walletStore.get('identity');
302
- identity = JSON.parse(new TextDecoder().decode(identity));
303
- const wallet = new MultiWallet__default["default"](this.network);
387
+ identity = JSON.parse(identity);
388
+ if (!globalThis.MultiWallet) {
389
+ const importee = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(/* webpackChunkName: "storage" */ '@leofcoin/multi-wallet')); });
390
+ globalThis.LeofcoinStorage = importee.default;
391
+ }
392
+ const wallet = new MultiWallet(this.network);
304
393
  wallet.recover(identity.mnemonic);
305
394
  return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
306
395
  }
@@ -403,7 +492,7 @@ class Peernet {
403
492
  }
404
493
 
405
494
  get codecs() {
406
- return codecFormatInterface.codecs
495
+ return codecs
407
496
  }
408
497
 
409
498
  addProto(name, proto) {
@@ -541,35 +630,33 @@ class Peernet {
541
630
  await this.addStore(store, options.storePrefix, options.root);
542
631
  }
543
632
 
544
- try {
633
+ const accountExists = await accountStore.has('public');
634
+ if (accountExists) {
545
635
  const pub = await accountStore.get('public');
546
- this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
636
+ this.id = JSON.parse(pub).walletId;
547
637
  let accounts = await walletStore.get('accounts');
548
- accounts = new TextDecoder().decode(accounts);
638
+
639
+
549
640
 
550
641
  // fixing account issue (string while needs to be a JSON)
551
642
  // TODO: remove when on mainnet
552
643
  try {
553
- this.accounts = JSON.parse(account);
644
+ this.accounts = JSON.parse(accounts);
554
645
  } catch (e) {
555
646
  this.accounts = [accounts.split(',')];
556
647
  }
557
- } catch (e) {
558
- if (e.code === 'ERR_NOT_FOUND') {
559
-
560
- const importee = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account')); });
561
- const generateAccount = importee.default;
562
- const {identity, accounts, config} = await generateAccount(this.network);
563
- walletStore.put('version', new TextEncoder().encode(1));
564
- walletStore.put('accounts', new TextEncoder().encode(JSON.stringify(accounts)));
565
- walletStore.put('identity', new TextEncoder().encode(JSON.stringify(identity)));
566
- await accountStore.put('config', new TextEncoder().encode(JSON.stringify(config)));
567
- await accountStore.put('public', new TextEncoder().encode(JSON.stringify({walletId: identity.walletId})));
568
-
569
- this.id = identity.walletId;
570
- } else {
571
- throw e
572
- }
648
+ } else {
649
+ const importee = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account')); });
650
+ const generateAccount = importee.default;
651
+ const {identity, accounts, config} = await generateAccount(this.network);
652
+ // await accountStore.put('config', JSON.stringify(config));
653
+ await accountStore.put('public', JSON.stringify({walletId: identity.walletId}));
654
+
655
+ await walletStore.put('version', String(1));
656
+ await walletStore.put('accounts', JSON.stringify(accounts));
657
+ await walletStore.put('identity', JSON.stringify(identity));
658
+
659
+ this.id = identity.walletId;
573
660
  }
574
661
  this._peerHandler = new PeerDiscovery(this.id);
575
662
  this.peerId = this.id;
@@ -909,7 +996,7 @@ class Peernet {
909
996
  return hash
910
997
  }
911
998
 
912
- async ls(hash) {
999
+ async ls(hash, options) {
913
1000
  let data;
914
1001
  const has = await dataStore.has(hash);
915
1002
  if (has) data = await dataStore.get(hash);
@@ -921,17 +1008,19 @@ class Peernet {
921
1008
  for (const {path, hash} of node.decoded.links) {
922
1009
  paths.push({path, hash});
923
1010
  }
924
-
1011
+ if (options?.pin) await dataStore.put(hash, node.encoded);
925
1012
  return paths
926
1013
  }
927
1014
 
928
- async cat(hash) {
1015
+ async cat(hash, options) {
929
1016
  let data;
930
1017
  const has = await dataStore.has(hash);
931
1018
  if (has) data = await dataStore.get(hash);
932
1019
  else data = await this.requestData(hash, 'data');
933
1020
  const node = await new peernet.protos['peernet-file'](data);
1021
+
934
1022
  if (node.decoded?.links.length > 0) throw new Error(`${hash} is a directory`)
1023
+ if (options?.pin) await dataStore.put(hash, node.encoded);
935
1024
  return node.decoded.content
936
1025
  }
937
1026
 
@@ -1041,6 +1130,7 @@ class Peernet {
1041
1130
  // }
1042
1131
  //
1043
1132
  // }
1044
- }
1133
+ }
1134
+ globalThis.Peernet = Peernet;
1045
1135
 
1046
1136
  module.exports = Peernet;
@@ -1,4 +1,6 @@
1
- import { FormatInterface } from '@leofcoin/codec-format-interface';
1
+ 'use strict';
2
+
3
+ var codecFormatInterface = require('@leofcoin/codec-format-interface');
2
4
 
3
5
  var proto$b = `
4
6
  // PeernetMessage
@@ -10,7 +12,7 @@ message PeernetMessage {
10
12
  optional string id = 5;
11
13
  }`;
12
14
 
13
- class PeernetMessage extends FormatInterface {
15
+ class PeernetMessage extends codecFormatInterface.FormatInterface {
14
16
  get keys() {
15
17
  return ['data', 'signature', 'from', 'to', 'id']
16
18
  }
@@ -41,7 +43,7 @@ message PeernetDHTMessage {
41
43
  message = new DHTMessage('hashmvbs124xcfd...', 'block')
42
44
  `
43
45
  */
44
- class DHTMessage extends FormatInterface {
46
+ class DHTMessage extends codecFormatInterface.FormatInterface {
45
47
  /**
46
48
  *
47
49
  */
@@ -67,7 +69,7 @@ message PeernetDHTMessageResponse {
67
69
  }
68
70
  `;
69
71
 
70
- class DHTMessageResponse extends FormatInterface {
72
+ class DHTMessageResponse extends codecFormatInterface.FormatInterface {
71
73
  get keys() {
72
74
  return ['hash', 'has']
73
75
  }
@@ -93,7 +95,7 @@ message PeernetDataMessage {
93
95
  /**
94
96
  * @extends {CodecFormat}
95
97
  */
96
- class DataMessage extends FormatInterface {
98
+ class DataMessage extends codecFormatInterface.FormatInterface {
97
99
  get keys() {
98
100
  return ['hash', 'store']
99
101
  }
@@ -116,7 +118,7 @@ message PsMessage {
116
118
  required bytes topic = 2;
117
119
  }`;
118
120
 
119
- class PsMessage extends FormatInterface {
121
+ class PsMessage extends codecFormatInterface.FormatInterface {
120
122
  get keys() {
121
123
  return ['data', 'topic']
122
124
  }
@@ -138,7 +140,7 @@ message PeernetPeerMessage {
138
140
  }
139
141
  `;
140
142
 
141
- class PeerMessage extends FormatInterface {
143
+ class PeerMessage extends codecFormatInterface.FormatInterface {
142
144
  get keys() {
143
145
  return ['id']
144
146
  }
@@ -160,7 +162,7 @@ message PeernetRequestMessage {
160
162
  }
161
163
  `;
162
164
 
163
- class RequestMessage extends FormatInterface {
165
+ class RequestMessage extends codecFormatInterface.FormatInterface {
164
166
  get keys() {
165
167
  return ['request']
166
168
  }
@@ -182,7 +184,7 @@ message PeernetResponseMessage {
182
184
  }
183
185
  `;
184
186
 
185
- class ResponseMessage extends FormatInterface {
187
+ class ResponseMessage extends codecFormatInterface.FormatInterface {
186
188
  get keys() {
187
189
  return ['response']
188
190
  }
@@ -204,7 +206,7 @@ message PeernetPeerMessageResponse {
204
206
  }
205
207
  `;
206
208
 
207
- class PeerMessageResponse extends FormatInterface {
209
+ class PeerMessageResponse extends codecFormatInterface.FormatInterface {
208
210
  get keys() {
209
211
  return ['id']
210
212
  }
@@ -227,7 +229,7 @@ message PeernetDataMessageResponse {
227
229
  }
228
230
  `;
229
231
 
230
- class DataMessageResponse extends FormatInterface {
232
+ class DataMessageResponse extends codecFormatInterface.FormatInterface {
231
233
  get keys() {
232
234
  return ['hash', 'data']
233
235
  }
@@ -250,7 +252,7 @@ message ChatMessage {
250
252
  repeated string files = 4;
251
253
  }`;
252
254
 
253
- class ChatMessage extends FormatInterface {
255
+ class ChatMessage extends codecFormatInterface.FormatInterface {
254
256
  get keys() {
255
257
  return ['author', 'value', 'timestamp', 'files']
256
258
  }
@@ -283,7 +285,7 @@ message PeernetFile {
283
285
  /**
284
286
  * @extends {CodecFormat}
285
287
  */
286
- class PeernetFile extends FormatInterface {
288
+ class PeernetFile extends codecFormatInterface.FormatInterface {
287
289
  get keys() {
288
290
  return ['path', 'content', 'links']
289
291
  }
@@ -299,4 +301,15 @@ class PeernetFile extends FormatInterface {
299
301
  }
300
302
  }
301
303
 
302
- export { ChatMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PeerMessage, PeerMessageResponse, PeernetFile, PeernetMessage, PsMessage, RequestMessage, ResponseMessage };
304
+ exports.ChatMessage = ChatMessage;
305
+ exports.DHTMessage = DHTMessage;
306
+ exports.DHTMessageResponse = DHTMessageResponse;
307
+ exports.DataMessage = DataMessage;
308
+ exports.DataMessageResponse = DataMessageResponse;
309
+ exports.PeerMessage = PeerMessage;
310
+ exports.PeerMessageResponse = PeerMessageResponse;
311
+ exports.PeernetFile = PeernetFile;
312
+ exports.PeernetMessage = PeernetMessage;
313
+ exports.PsMessage = PsMessage;
314
+ exports.RequestMessage = RequestMessage;
315
+ exports.ResponseMessage = ResponseMessage;
@@ -1,12 +1,34 @@
1
- import '@vandeurenglenn/debug';
2
- import PubSub from '@vandeurenglenn/little-pubsub';
3
- import { Codec, CodecHash, codecs } from '@leofcoin/codec-format-interface';
4
- import MultiWallet from '@leofcoin/multi-wallet';
5
- import 'path';
1
+ 'use strict';
2
+
3
+ require('@vandeurenglenn/debug');
4
+ var PubSub = require('@vandeurenglenn/little-pubsub');
5
+ var codecFormatInterface = require('@leofcoin/codec-format-interface');
6
+
7
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
+
9
+ function _interopNamespace(e) {
10
+ if (e && e.__esModule) return e;
11
+ var n = Object.create(null);
12
+ if (e) {
13
+ Object.keys(e).forEach(function (k) {
14
+ if (k !== 'default') {
15
+ var d = Object.getOwnPropertyDescriptor(e, k);
16
+ Object.defineProperty(n, k, d.get ? d : {
17
+ enumerable: true,
18
+ get: function () { return e[k]; }
19
+ });
20
+ }
21
+ });
22
+ }
23
+ n["default"] = e;
24
+ return Object.freeze(n);
25
+ }
26
+
27
+ var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
6
28
 
7
29
  const protoFor = (data) => {
8
30
  if (!Buffer.isBuffer(data)) data = Buffer.from(data);
9
- const codec = new Codec(data);
31
+ const codec = new codecFormatInterface.Codec(data);
10
32
  if (!codec.name) throw new Error('proto not found')
11
33
  const Proto = globalThis.peernet.protos[codec.name];
12
34
  if (!Proto) throw (new Error(`No proto defined for ${codec.name}`))
@@ -255,6 +277,94 @@ class DhtEarth {
255
277
  }
256
278
  }
257
279
 
280
+ var codecs = {
281
+ // just a hash
282
+ 'disco-hash': {
283
+ codec: parseInt('30', 16),
284
+ hashAlg: 'dbl-keccak-256', // ,
285
+ // testnet: 'olivia'
286
+ },
287
+ 'peernet-peer-response': {
288
+ codec: parseInt('707072', 16),
289
+ hashAlg: 'keccak-256',
290
+ },
291
+ 'peernet-peer': {
292
+ codec: parseInt('7070', 16),
293
+ hashAlg: 'keccak-256',
294
+ },
295
+ 'peernet-dht': {
296
+ codec: parseInt('706468', 16),
297
+ hashAlg: 'keccak-256',
298
+ },
299
+ 'peernet-dht-response': {
300
+ codec: parseInt('706472', 16),
301
+ hashAlg: 'keccak-256',
302
+ },
303
+ // data
304
+ 'peernet-data': {
305
+ codec: parseInt('706461', 16),
306
+ hashAlg: 'keccak-256',
307
+ },
308
+ 'peernet-data-response': {
309
+ codec: parseInt('70646172', 16),
310
+ hashAlg: 'keccak-256',
311
+ },
312
+ // message
313
+ 'peernet-message': {
314
+ codec: parseInt('706d65', 16),
315
+ hashAlg: 'keccak-256',
316
+ },
317
+ // pubsub
318
+ 'peernet-ps': {
319
+ codec: parseInt('707073', 16),
320
+ hashAlg: 'keccak-256',
321
+ },
322
+ 'peernet-response': {
323
+ codec: parseInt('7072', 16),
324
+ hashAlg: 'keccak-256',
325
+ },
326
+ 'peernet-request': {
327
+ codec: parseInt('707271', 16),
328
+ hashAlg: 'keccak-256',
329
+ },
330
+ // normal block
331
+ 'leofcoin-block': {
332
+ codec: parseInt('6c62', 16),
333
+ hashAlg: 'dbl-keccak-512', // ,
334
+ // testnet: 'olivia'
335
+ },
336
+ 'leofcoin-tx': {
337
+ codec: parseInt('6c74', 16),
338
+ hashAlg: 'dbl-keccak-512', // ,
339
+ // testnet: 'olivia'
340
+ },
341
+ // itx
342
+ 'leofcoin-itx': {
343
+ codec: parseInt('6c69', 16),
344
+ hashAlg: 'keccak-512', // ,
345
+ // testnet: 'olivia'
346
+ },
347
+ // peer reputation
348
+ 'leofcoin-pr': {
349
+ codec: parseInt('6c70', 16),
350
+ hashAlg: 'keccak-256', // ,
351
+ // testnet: 'olivia'
352
+ },
353
+ // chat message
354
+ 'chat-message': {
355
+ codec: parseInt('636d', 16),
356
+ hashAlg: 'dbl-keccak-256',
357
+ },
358
+ 'peernet-file' : {
359
+ codec: parseInt('7066', 16),
360
+ hashAlg: 'keccak-256',
361
+ },
362
+ 'peernet-file-response' : {
363
+ codec: parseInt('706672', 16),
364
+ hashAlg: 'keccak-256',
365
+ }
366
+ };
367
+
258
368
  class MessageHandler {
259
369
  constructor(network) {
260
370
  this.network = network;
@@ -270,9 +380,13 @@ class MessageHandler {
270
380
  * @return signature
271
381
  */
272
382
  async hashAndSignMessage(message) {
273
- const hasher = new CodecHash(message, {name: 'peernet-message'});
383
+ const hasher = new codecFormatInterface.CodecHash(message, {name: 'peernet-message'});
274
384
  let identity = await walletStore.get('identity');
275
- identity = JSON.parse(new TextDecoder().decode(identity));
385
+ identity = JSON.parse(identity);
386
+ if (!globalThis.MultiWallet) {
387
+ const importee = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(/* webpackChunkName: "storage" */ '@leofcoin/multi-wallet')); });
388
+ globalThis.LeofcoinStorage = importee.default;
389
+ }
276
390
  const wallet = new MultiWallet(this.network);
277
391
  wallet.recover(identity.mnemonic);
278
392
  return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
@@ -328,8 +442,8 @@ const nothingFoundError = (hash) => {
328
442
  };
329
443
 
330
444
  globalThis.leofcoin = globalThis.leofcoin || {};
331
- globalThis.pubsub = globalThis.pubsub || new PubSub();
332
- globalThis.globalSub = globalThis.globalSub || new PubSub({verbose: true});
445
+ globalThis.pubsub = globalThis.pubsub || new PubSub__default["default"]();
446
+ globalThis.globalSub = globalThis.globalSub || new PubSub__default["default"]({verbose: true});
333
447
 
334
448
  /**
335
449
  * @access public
@@ -389,7 +503,7 @@ class Peernet {
389
503
 
390
504
  async addStore(name, prefix, root, isPrivate = true) {
391
505
  if (!globalThis.LeofcoinStorage) {
392
- const importee = await import(/* webpackChunkName: "storage" */ '@leofcoin/storage');
506
+ const importee = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(/* webpackChunkName: "storage" */ '@leofcoin/storage')); });
393
507
  globalThis.LeofcoinStorage = importee.default;
394
508
  }
395
509
  if (name === 'block' || name === 'transaction' || name === 'chain' ||
@@ -478,7 +592,7 @@ class Peernet {
478
592
  ChatMessage,
479
593
  PeernetFile
480
594
  // FolderMessageResponse
481
- } = await import(/* webpackChunkName: "messages" */ './messages-bce1b91d.js');
595
+ } = await Promise.resolve().then(function () { return require(/* webpackChunkName: "messages" */ './messages-1b12e93b.js'); });
482
596
 
483
597
  /**
484
598
  * proto Object containing protos
@@ -514,35 +628,33 @@ class Peernet {
514
628
  await this.addStore(store, options.storePrefix, options.root);
515
629
  }
516
630
 
517
- try {
631
+ const accountExists = await accountStore.has('public');
632
+ if (accountExists) {
518
633
  const pub = await accountStore.get('public');
519
- this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
634
+ this.id = JSON.parse(pub).walletId;
520
635
  let accounts = await walletStore.get('accounts');
521
- accounts = new TextDecoder().decode(accounts);
636
+
637
+
522
638
 
523
639
  // fixing account issue (string while needs to be a JSON)
524
640
  // TODO: remove when on mainnet
525
641
  try {
526
- this.accounts = JSON.parse(account);
642
+ this.accounts = JSON.parse(accounts);
527
643
  } catch (e) {
528
644
  this.accounts = [accounts.split(',')];
529
645
  }
530
- } catch (e) {
531
- if (e.code === 'ERR_NOT_FOUND') {
532
-
533
- const importee = await import(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account');
534
- const generateAccount = importee.default;
535
- const {identity, accounts, config} = await generateAccount(this.network);
536
- walletStore.put('version', new TextEncoder().encode(1));
537
- walletStore.put('accounts', new TextEncoder().encode(JSON.stringify(accounts)));
538
- walletStore.put('identity', new TextEncoder().encode(JSON.stringify(identity)));
539
- await accountStore.put('config', new TextEncoder().encode(JSON.stringify(config)));
540
- await accountStore.put('public', new TextEncoder().encode(JSON.stringify({walletId: identity.walletId})));
541
-
542
- this.id = identity.walletId;
543
- } else {
544
- throw e
545
- }
646
+ } else {
647
+ const importee = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account')); });
648
+ const generateAccount = importee.default;
649
+ const {identity, accounts, config} = await generateAccount(this.network);
650
+ // await accountStore.put('config', JSON.stringify(config));
651
+ await accountStore.put('public', JSON.stringify({walletId: identity.walletId}));
652
+
653
+ await walletStore.put('version', String(1));
654
+ await walletStore.put('accounts', JSON.stringify(accounts));
655
+ await walletStore.put('identity', JSON.stringify(identity));
656
+
657
+ this.id = identity.walletId;
546
658
  }
547
659
  this._peerHandler = new PeerDiscovery(this.id);
548
660
  this.peerId = this.id;
@@ -565,7 +677,7 @@ class Peernet {
565
677
  pubsub.subscribe('peer:data', dataHandler);
566
678
 
567
679
 
568
- const importee = await import(/* webpackChunkName: "peernet-swarm" */ '@leofcoin/peernet-swarm');
680
+ const importee = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(/* webpackChunkName: "peernet-swarm" */ '@leofcoin/peernet-swarm')); });
569
681
  /**
570
682
  * @access public
571
683
  * @type {PeernetClient}
@@ -882,7 +994,7 @@ class Peernet {
882
994
  return hash
883
995
  }
884
996
 
885
- async ls(hash) {
997
+ async ls(hash, options) {
886
998
  let data;
887
999
  const has = await dataStore.has(hash);
888
1000
  if (has) data = await dataStore.get(hash);
@@ -894,17 +1006,19 @@ class Peernet {
894
1006
  for (const {path, hash} of node.decoded.links) {
895
1007
  paths.push({path, hash});
896
1008
  }
897
-
1009
+ if (options?.pin) await dataStore.put(hash, node.encoded);
898
1010
  return paths
899
1011
  }
900
1012
 
901
- async cat(hash) {
1013
+ async cat(hash, options) {
902
1014
  let data;
903
1015
  const has = await dataStore.has(hash);
904
1016
  if (has) data = await dataStore.get(hash);
905
1017
  else data = await this.requestData(hash, 'data');
906
1018
  const node = await new peernet.protos['peernet-file'](data);
1019
+
907
1020
  if (node.decoded?.links.length > 0) throw new Error(`${hash} is a directory`)
1021
+ if (options?.pin) await dataStore.put(hash, node.encoded);
908
1022
  return node.decoded.content
909
1023
  }
910
1024
 
@@ -1014,6 +1128,7 @@ class Peernet {
1014
1128
  // }
1015
1129
  //
1016
1130
  // }
1017
- }
1131
+ }
1132
+ globalThis.Peernet = Peernet;
1018
1133
 
1019
- export { Peernet as default };
1134
+ module.exports = Peernet;
package/index.html CHANGED
@@ -8,8 +8,8 @@
8
8
  <script src="./dist/browser/peernet.js">
9
9
  </script>
10
10
 
11
- <script>
12
-
11
+ <script >
12
+ // import Peernet from './dist/browser/peernet.js';
13
13
  (async () => {
14
14
  const peernet = await new Peernet()
15
15
  peernet.addRequestHandler('lastBlock', () => new peernet.protos['peernet-response']({response: new TextEncoder().encode(100)}))