@leofcoin/peernet 0.13.2 → 0.14.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.
@@ -156,7 +156,7 @@ message PeernetFileLink {
156
156
 
157
157
  message PeernetFile {
158
158
  required string path = 1;
159
- optional bytes content = 2;
159
+ optional string content = 2;
160
160
  repeated PeernetFileLink links = 3;
161
161
  }
162
162
  `;
@@ -5,7 +5,6 @@ var PubSub = require('@vandeurenglenn/little-pubsub');
5
5
  var fetch = require('node-fetch');
6
6
  var codecFormatInterface = require('@leofcoin/codec-format-interface');
7
7
  var MultiWallet = require('@leofcoin/multi-wallet');
8
- require('path');
9
8
 
10
9
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
10
 
@@ -282,6 +281,94 @@ class DhtEarth {
282
281
  }
283
282
  }
284
283
 
284
+ var codecs = {
285
+ // just a hash
286
+ 'disco-hash': {
287
+ codec: parseInt('30', 16),
288
+ hashAlg: 'dbl-keccak-256', // ,
289
+ // testnet: 'olivia'
290
+ },
291
+ 'peernet-peer-response': {
292
+ codec: parseInt('707072', 16),
293
+ hashAlg: 'keccak-256',
294
+ },
295
+ 'peernet-peer': {
296
+ codec: parseInt('7070', 16),
297
+ hashAlg: 'keccak-256',
298
+ },
299
+ 'peernet-dht': {
300
+ codec: parseInt('706468', 16),
301
+ hashAlg: 'keccak-256',
302
+ },
303
+ 'peernet-dht-response': {
304
+ codec: parseInt('706472', 16),
305
+ hashAlg: 'keccak-256',
306
+ },
307
+ // data
308
+ 'peernet-data': {
309
+ codec: parseInt('706461', 16),
310
+ hashAlg: 'keccak-256',
311
+ },
312
+ 'peernet-data-response': {
313
+ codec: parseInt('70646172', 16),
314
+ hashAlg: 'keccak-256',
315
+ },
316
+ // message
317
+ 'peernet-message': {
318
+ codec: parseInt('706d65', 16),
319
+ hashAlg: 'keccak-256',
320
+ },
321
+ // pubsub
322
+ 'peernet-ps': {
323
+ codec: parseInt('707073', 16),
324
+ hashAlg: 'keccak-256',
325
+ },
326
+ 'peernet-response': {
327
+ codec: parseInt('7072', 16),
328
+ hashAlg: 'keccak-256',
329
+ },
330
+ 'peernet-request': {
331
+ codec: parseInt('707271', 16),
332
+ hashAlg: 'keccak-256',
333
+ },
334
+ // normal block
335
+ 'leofcoin-block': {
336
+ codec: parseInt('6c62', 16),
337
+ hashAlg: 'dbl-keccak-512', // ,
338
+ // testnet: 'olivia'
339
+ },
340
+ 'leofcoin-tx': {
341
+ codec: parseInt('6c74', 16),
342
+ hashAlg: 'dbl-keccak-512', // ,
343
+ // testnet: 'olivia'
344
+ },
345
+ // itx
346
+ 'leofcoin-itx': {
347
+ codec: parseInt('6c69', 16),
348
+ hashAlg: 'keccak-512', // ,
349
+ // testnet: 'olivia'
350
+ },
351
+ // peer reputation
352
+ 'leofcoin-pr': {
353
+ codec: parseInt('6c70', 16),
354
+ hashAlg: 'keccak-256', // ,
355
+ // testnet: 'olivia'
356
+ },
357
+ // chat message
358
+ 'chat-message': {
359
+ codec: parseInt('636d', 16),
360
+ hashAlg: 'dbl-keccak-256',
361
+ },
362
+ 'peernet-file' : {
363
+ codec: parseInt('7066', 16),
364
+ hashAlg: 'keccak-256',
365
+ },
366
+ 'peernet-file-response' : {
367
+ codec: parseInt('706672', 16),
368
+ hashAlg: 'keccak-256',
369
+ }
370
+ };
371
+
285
372
  class MessageHandler {
286
373
  constructor(network) {
287
374
  this.network = network;
@@ -403,7 +490,7 @@ class Peernet {
403
490
  }
404
491
 
405
492
  get codecs() {
406
- return codecFormatInterface.codecs
493
+ return codecs
407
494
  }
408
495
 
409
496
  addProto(name, proto) {
@@ -505,7 +592,7 @@ class Peernet {
505
592
  ChatMessage,
506
593
  PeernetFile
507
594
  // FolderMessageResponse
508
- } = await Promise.resolve().then(function () { return require(/* webpackChunkName: "messages" */ './messages-7c05afbc.js'); });
595
+ } = await Promise.resolve().then(function () { return require(/* webpackChunkName: "messages" */ './messages-f46a3ca1.js'); });
509
596
 
510
597
  /**
511
598
  * proto Object containing protos
@@ -541,35 +628,33 @@ class Peernet {
541
628
  await this.addStore(store, options.storePrefix, options.root);
542
629
  }
543
630
 
544
- try {
631
+ const accountExists = await accountStore.has('public');
632
+ if (accountExists) {
545
633
  const pub = await accountStore.get('public');
546
- this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
634
+ this.id = JSON.parse(pub).walletId;
547
635
  let accounts = await walletStore.get('accounts');
548
- accounts = new TextDecoder().decode(accounts);
636
+
637
+
549
638
 
550
639
  // fixing account issue (string while needs to be a JSON)
551
640
  // TODO: remove when on mainnet
552
641
  try {
553
- this.accounts = JSON.parse(account);
642
+ this.accounts = JSON.parse(accounts);
554
643
  } catch (e) {
555
644
  this.accounts = [accounts.split(',')];
556
645
  }
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
- }
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;
573
658
  }
574
659
  this._peerHandler = new PeerDiscovery(this.id);
575
660
  this.peerId = this.id;
@@ -909,7 +994,7 @@ class Peernet {
909
994
  return hash
910
995
  }
911
996
 
912
- async ls(hash) {
997
+ async ls(hash, options) {
913
998
  let data;
914
999
  const has = await dataStore.has(hash);
915
1000
  if (has) data = await dataStore.get(hash);
@@ -921,17 +1006,19 @@ class Peernet {
921
1006
  for (const {path, hash} of node.decoded.links) {
922
1007
  paths.push({path, hash});
923
1008
  }
924
-
1009
+ if (options?.pin) await dataStore.put(hash, node.encoded);
925
1010
  return paths
926
1011
  }
927
1012
 
928
- async cat(hash) {
1013
+ async cat(hash, options) {
929
1014
  let data;
930
1015
  const has = await dataStore.has(hash);
931
1016
  if (has) data = await dataStore.get(hash);
932
1017
  else data = await this.requestData(hash, 'data');
933
1018
  const node = await new peernet.protos['peernet-file'](data);
1019
+
934
1020
  if (node.decoded?.links.length > 0) throw new Error(`${hash} is a directory`)
1021
+ if (options?.pin) await dataStore.put(hash, node.encoded);
935
1022
  return node.decoded.content
936
1023
  }
937
1024
 
@@ -1041,6 +1128,7 @@ class Peernet {
1041
1128
  // }
1042
1129
  //
1043
1130
  // }
1044
- }
1131
+ }
1132
+ globalThis.Peernet = Peernet;
1045
1133
 
1046
1134
  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
  }
@@ -275,7 +277,7 @@ message PeernetFileLink {
275
277
 
276
278
  message PeernetFile {
277
279
  required string path = 1;
278
- optional bytes content = 2;
280
+ optional string content = 2;
279
281
  repeated PeernetFileLink links = 3;
280
282
  }
281
283
  `;
@@ -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,36 @@
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
+ var MultiWallet = require('@leofcoin/multi-wallet');
7
+
8
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
+
10
+ function _interopNamespace(e) {
11
+ if (e && e.__esModule) return e;
12
+ var n = Object.create(null);
13
+ if (e) {
14
+ Object.keys(e).forEach(function (k) {
15
+ if (k !== 'default') {
16
+ var d = Object.getOwnPropertyDescriptor(e, k);
17
+ Object.defineProperty(n, k, d.get ? d : {
18
+ enumerable: true,
19
+ get: function () { return e[k]; }
20
+ });
21
+ }
22
+ });
23
+ }
24
+ n["default"] = e;
25
+ return Object.freeze(n);
26
+ }
27
+
28
+ var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
29
+ var MultiWallet__default = /*#__PURE__*/_interopDefaultLegacy(MultiWallet);
6
30
 
7
31
  const protoFor = (data) => {
8
32
  if (!Buffer.isBuffer(data)) data = Buffer.from(data);
9
- const codec = new Codec(data);
33
+ const codec = new codecFormatInterface.Codec(data);
10
34
  if (!codec.name) throw new Error('proto not found')
11
35
  const Proto = globalThis.peernet.protos[codec.name];
12
36
  if (!Proto) throw (new Error(`No proto defined for ${codec.name}`))
@@ -255,6 +279,94 @@ class DhtEarth {
255
279
  }
256
280
  }
257
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
+
258
370
  class MessageHandler {
259
371
  constructor(network) {
260
372
  this.network = network;
@@ -270,10 +382,10 @@ class MessageHandler {
270
382
  * @return signature
271
383
  */
272
384
  async hashAndSignMessage(message) {
273
- const hasher = new CodecHash(message, {name: 'peernet-message'});
385
+ const hasher = new codecFormatInterface.CodecHash(message, {name: 'peernet-message'});
274
386
  let identity = await walletStore.get('identity');
275
387
  identity = JSON.parse(new TextDecoder().decode(identity));
276
- const wallet = new MultiWallet(this.network);
388
+ const wallet = new MultiWallet__default["default"](this.network);
277
389
  wallet.recover(identity.mnemonic);
278
390
  return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
279
391
  }
@@ -328,8 +440,8 @@ const nothingFoundError = (hash) => {
328
440
  };
329
441
 
330
442
  globalThis.leofcoin = globalThis.leofcoin || {};
331
- globalThis.pubsub = globalThis.pubsub || new PubSub();
332
- globalThis.globalSub = globalThis.globalSub || new PubSub({verbose: true});
443
+ globalThis.pubsub = globalThis.pubsub || new PubSub__default["default"]();
444
+ globalThis.globalSub = globalThis.globalSub || new PubSub__default["default"]({verbose: true});
333
445
 
334
446
  /**
335
447
  * @access public
@@ -389,7 +501,7 @@ class Peernet {
389
501
 
390
502
  async addStore(name, prefix, root, isPrivate = true) {
391
503
  if (!globalThis.LeofcoinStorage) {
392
- const importee = await import(/* webpackChunkName: "storage" */ '@leofcoin/storage');
504
+ const importee = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(/* webpackChunkName: "storage" */ '@leofcoin/storage')); });
393
505
  globalThis.LeofcoinStorage = importee.default;
394
506
  }
395
507
  if (name === 'block' || name === 'transaction' || name === 'chain' ||
@@ -478,7 +590,7 @@ class Peernet {
478
590
  ChatMessage,
479
591
  PeernetFile
480
592
  // FolderMessageResponse
481
- } = await import(/* webpackChunkName: "messages" */ './messages-d7278178.js');
593
+ } = await Promise.resolve().then(function () { return require(/* webpackChunkName: "messages" */ './messages-1b12e93b.js'); });
482
594
 
483
595
  /**
484
596
  * proto Object containing protos
@@ -514,35 +626,33 @@ class Peernet {
514
626
  await this.addStore(store, options.storePrefix, options.root);
515
627
  }
516
628
 
517
- try {
629
+ const accountExists = await accountStore.has('public');
630
+ if (accountExists) {
518
631
  const pub = await accountStore.get('public');
519
- this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
632
+ this.id = JSON.parse(pub).walletId;
520
633
  let accounts = await walletStore.get('accounts');
521
- accounts = new TextDecoder().decode(accounts);
634
+
635
+
522
636
 
523
637
  // fixing account issue (string while needs to be a JSON)
524
638
  // TODO: remove when on mainnet
525
639
  try {
526
- this.accounts = JSON.parse(account);
640
+ this.accounts = JSON.parse(accounts);
527
641
  } catch (e) {
528
642
  this.accounts = [accounts.split(',')];
529
643
  }
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
- }
644
+ } else {
645
+ const importee = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account')); });
646
+ const generateAccount = importee.default;
647
+ const {identity, accounts, config} = await generateAccount(this.network);
648
+ // await accountStore.put('config', JSON.stringify(config));
649
+ await accountStore.put('public', JSON.stringify({walletId: identity.walletId}));
650
+
651
+ await walletStore.put('version', String(1));
652
+ await walletStore.put('accounts', JSON.stringify(accounts));
653
+ await walletStore.put('identity', JSON.stringify(identity));
654
+
655
+ this.id = identity.walletId;
546
656
  }
547
657
  this._peerHandler = new PeerDiscovery(this.id);
548
658
  this.peerId = this.id;
@@ -565,7 +675,7 @@ class Peernet {
565
675
  pubsub.subscribe('peer:data', dataHandler);
566
676
 
567
677
 
568
- const importee = await import(/* webpackChunkName: "peernet-swarm" */ '@leofcoin/peernet-swarm');
678
+ const importee = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(/* webpackChunkName: "peernet-swarm" */ '@leofcoin/peernet-swarm')); });
569
679
  /**
570
680
  * @access public
571
681
  * @type {PeernetClient}
@@ -882,7 +992,7 @@ class Peernet {
882
992
  return hash
883
993
  }
884
994
 
885
- async ls(hash) {
995
+ async ls(hash, options) {
886
996
  let data;
887
997
  const has = await dataStore.has(hash);
888
998
  if (has) data = await dataStore.get(hash);
@@ -894,17 +1004,19 @@ class Peernet {
894
1004
  for (const {path, hash} of node.decoded.links) {
895
1005
  paths.push({path, hash});
896
1006
  }
897
-
1007
+ if (options?.pin) await dataStore.put(hash, node.encoded);
898
1008
  return paths
899
1009
  }
900
1010
 
901
- async cat(hash) {
1011
+ async cat(hash, options) {
902
1012
  let data;
903
1013
  const has = await dataStore.has(hash);
904
1014
  if (has) data = await dataStore.get(hash);
905
1015
  else data = await this.requestData(hash, 'data');
906
1016
  const node = await new peernet.protos['peernet-file'](data);
1017
+
907
1018
  if (node.decoded?.links.length > 0) throw new Error(`${hash} is a directory`)
1019
+ if (options?.pin) await dataStore.put(hash, node.encoded);
908
1020
  return node.decoded.content
909
1021
  }
910
1022
 
@@ -1014,6 +1126,7 @@ class Peernet {
1014
1126
  // }
1015
1127
  //
1016
1128
  // }
1017
- }
1129
+ }
1130
+ globalThis.Peernet = Peernet;
1018
1131
 
1019
- export { Peernet as default };
1132
+ 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 type="module">
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)}))