@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.
package/package.json CHANGED
@@ -1,9 +1,13 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.13.3",
3
+ "version": "0.14.1",
4
4
  "description": "",
5
+ "source": "src/peernet.js",
5
6
  "main": "dist/commonjs/peernet.js",
6
- "module": "src/module/peernet.js",
7
+ "module": "dist/module/peernet.js",
8
+ "targets": [
9
+ "browser"
10
+ ],
7
11
  "scripts": {
8
12
  "build": "npm run c && webpack",
9
13
  "test": "node test/index.js",
@@ -15,18 +19,20 @@
15
19
  "coveralls": "cat ./coverage/lcov.info | coveralls",
16
20
  "c": "rollup -c",
17
21
  "w": "rollup -c -w",
18
- "b": "browserify dist/browser/peernet.js -o dist/browser/peernet.js --standalone Peernet && rm browser.js"
22
+ "watch": "parcel watch",
23
+ "parcel-build": "parcel build",
24
+ "b": "browserify dist/module/peernet.js -o dist/module/peernet.js --standalone Peernet && rm browser.js"
19
25
  },
20
26
  "keywords": [],
21
27
  "author": "",
22
28
  "license": "MIT",
23
- "browserslist": "> 0.5%, last 2 versions, not dead",
29
+ "browserslist": "> 5%, last 2 versions, not dead",
24
30
  "dependencies": {
25
31
  "@leofcoin/codec-format-interface": "^1.2.5",
26
32
  "@leofcoin/generate-account": "^1.0.4",
27
33
  "@leofcoin/multi-wallet": "^2.1.2",
28
34
  "@leofcoin/peernet-swarm": "^0.3.3",
29
- "@leofcoin/storage": "^2.3.0",
35
+ "@leofcoin/storage": "^3.0.0",
30
36
  "@vandeurenglenn/base32": "^1.1.0",
31
37
  "@vandeurenglenn/base58": "^1.1.0",
32
38
  "@vandeurenglenn/debug": "^1.0.0",
package/rollup.config.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { execSync } from 'child_process';
2
2
  import json from '@rollup/plugin-json'
3
3
  import modify from 'rollup-plugin-modify'
4
+ import resolve from 'rollup-plugin-node-resolve'
4
5
 
5
6
  try {
6
7
  execSync('rm dist -r')
@@ -26,7 +27,8 @@ export default [{
26
27
  input: 'src/peernet.js',
27
28
  output: {
28
29
  dir: 'dist/module',
29
- format: 'es'
30
+ format: 'cjs',
31
+ externals: ["@leofcoin/storage"]
30
32
  },
31
33
  plugins: [
32
34
  json(),
@@ -1,4 +1,3 @@
1
- import MultiWallet from '@leofcoin/multi-wallet'
2
1
  import { CodecHash } from '@leofcoin/codec-format-interface'
3
2
 
4
3
  export default class MessageHandler {
@@ -18,7 +17,11 @@ export default class MessageHandler {
18
17
  async hashAndSignMessage(message) {
19
18
  const hasher = new CodecHash(message, {name: 'peernet-message'})
20
19
  let identity = await walletStore.get('identity')
21
- identity = JSON.parse(new TextDecoder().decode(identity))
20
+ identity = JSON.parse(identity)
21
+ if (!globalThis.MultiWallet) {
22
+ const importee = await import(/* webpackChunkName: "storage" */ '@leofcoin/multi-wallet')
23
+ globalThis.LeofcoinStorage = importee.default
24
+ }
22
25
  const wallet = new MultiWallet(this.network)
23
26
  wallet.recover(identity.mnemonic)
24
27
  return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
package/src/peer-info.js CHANGED
@@ -1,9 +1,9 @@
1
- import CodecFormat from './codec/codec-format-interface'
2
-
3
- export default class PeerInfo extends CodecFormat {
4
- constructor(data, options) {
5
- super(data, options)
6
-
7
- this.keys = ['id', 'address', 'family']
8
- }
9
- }
1
+ import CodecFormat from './codec/codec-format-interface'
2
+
3
+ export default class PeerInfo extends CodecFormat {
4
+ constructor(data, options) {
5
+ super(data, options)
6
+
7
+ this.keys = ['id', 'address', 'family']
8
+ }
9
+ }
package/src/peernet.js CHANGED
@@ -2,14 +2,13 @@ import '@vandeurenglenn/debug'
2
2
  import PubSub from '@vandeurenglenn/little-pubsub'
3
3
  import PeerDiscovery from './discovery/peer-discovery'
4
4
  import DHT from './dht/dht.js'
5
- import { CodecHash, codecs} from '@leofcoin/codec-format-interface'
5
+ import codecs from './../node_modules/@leofcoin/codec-format-interface/src/codecs'
6
6
  import { protoFor, target } from './utils/utils.js'
7
7
  import MessageHandler from './handlers/message.js'
8
8
  import dataHandler from './handlers/data.js'
9
9
  import { encapsulatedError, dhtError,
10
10
  nothingFoundError } from './errors/errors.js'
11
-
12
- import {parse} from 'path'
11
+
13
12
  globalThis.leofcoin = globalThis.leofcoin || {}
14
13
  globalThis.pubsub = globalThis.pubsub || new PubSub()
15
14
  globalThis.globalSub = globalThis.globalSub || new PubSub({verbose: true})
@@ -197,36 +196,33 @@ export default class Peernet {
197
196
  await this.addStore(store, options.storePrefix, options.root)
198
197
  }
199
198
 
200
- try {
199
+ const accountExists = await accountStore.has('public')
200
+ if (accountExists) {
201
201
  const pub = await accountStore.get('public')
202
- this.id = JSON.parse(new TextDecoder().decode(pub)).walletId
202
+ this.id = JSON.parse(pub).walletId
203
203
  let accounts = await walletStore.get('accounts')
204
- accounts = new TextDecoder().decode(accounts)
204
+
205
+
205
206
 
206
207
  // fixing account issue (string while needs to be a JSON)
207
208
  // TODO: remove when on mainnet
208
209
  try {
209
- this.accounts = JSON.parse(account)
210
+ this.accounts = JSON.parse(accounts)
210
211
  } catch (e) {
211
212
  this.accounts = [accounts.split(',')]
212
213
  }
213
- } catch (e) {
214
- if (e.code === 'ERR_NOT_FOUND') {
215
-
216
- const importee = await import(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account')
217
- const generateAccount = importee.default
218
- const wallet = {}
219
- const {identity, accounts, config} = await generateAccount(this.network)
220
- walletStore.put('version', new TextEncoder().encode(1))
221
- walletStore.put('accounts', new TextEncoder().encode(JSON.stringify(accounts)))
222
- walletStore.put('identity', new TextEncoder().encode(JSON.stringify(identity)))
223
- await accountStore.put('config', new TextEncoder().encode(JSON.stringify(config)));
224
- await accountStore.put('public', new TextEncoder().encode(JSON.stringify({walletId: identity.walletId})));
225
-
226
- this.id = identity.walletId
227
- } else {
228
- throw e
229
- }
214
+ } else {
215
+ const importee = await import(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account')
216
+ const generateAccount = importee.default
217
+ const {identity, accounts, config} = await generateAccount(this.network)
218
+ // await accountStore.put('config', JSON.stringify(config));
219
+ await accountStore.put('public', JSON.stringify({walletId: identity.walletId}));
220
+
221
+ await walletStore.put('version', String(1))
222
+ await walletStore.put('accounts', JSON.stringify(accounts))
223
+ await walletStore.put('identity', JSON.stringify(identity))
224
+
225
+ this.id = identity.walletId
230
226
  }
231
227
  this._peerHandler = new PeerDiscovery(this.id)
232
228
  this.peerId = this.id
@@ -570,7 +566,7 @@ export default class Peernet {
570
566
  return hash
571
567
  }
572
568
 
573
- async ls(hash) {
569
+ async ls(hash, options) {
574
570
  let data
575
571
  const has = await dataStore.has(hash)
576
572
  if (has) data = await dataStore.get(hash)
@@ -582,18 +578,19 @@ export default class Peernet {
582
578
  for (const {path, hash} of node.decoded.links) {
583
579
  paths.push({path, hash})
584
580
  }
585
-
581
+ if (options?.pin) await dataStore.put(hash, node.encoded)
586
582
  return paths
587
583
  }
588
584
 
589
- async cat(hash) {
585
+ async cat(hash, options) {
590
586
  let data
591
587
  const has = await dataStore.has(hash)
592
588
  if (has) data = await dataStore.get(hash)
593
589
  else data = await this.requestData(hash, 'data')
594
590
  const node = await new peernet.protos['peernet-file'](data)
595
- const paths = []
591
+
596
592
  if (node.decoded?.links.length > 0) throw new Error(`${hash} is a directory`)
593
+ if (options?.pin) await dataStore.put(hash, node.encoded)
597
594
  return node.decoded.content
598
595
  }
599
596
 
@@ -705,3 +702,4 @@ export default class Peernet {
705
702
  //
706
703
  // }
707
704
  }
705
+ globalThis.Peernet = Peernet
package/webpack.config.js CHANGED
@@ -1,7 +1,9 @@
1
1
  const path = require('path');
2
2
  const webpack = require('webpack');
3
3
  module.exports = [{
4
- entry: './src/peernet.js',
4
+ entry: {
5
+ peernet: './dist/module/peernet.js'
6
+ },
5
7
  mode: 'production',
6
8
  plugins: [
7
9
  // Work around for Buffer is undefined:
@@ -26,9 +28,9 @@ module.exports = [{
26
28
  optimization: {
27
29
  minimize: false
28
30
  },
29
- experiments: {
30
- outputModule: true
31
- },
31
+ // experiments: {
32
+ // outputModule: true
33
+ // },
32
34
  resolve: {
33
35
  extensions: [ '.ts', '.js' ],
34
36
  fallback: {
@@ -39,15 +41,19 @@ resolve: {
39
41
  "path": require.resolve("path-browserify"),
40
42
  "os": require.resolve("os-browserify"),
41
43
  "crypto": require.resolve("crypto-browserify"),
42
- "vm": require.resolve("vm-browserify")
44
+ "vm": require.resolve("vm-browserify"),
45
+ "@store:import": require.resolve("@leofcoin/storage/src/store-shim.js")
43
46
  }
44
47
  },
45
48
  output: {
49
+ // library: {
50
+ // type: 'module'
51
+ // },
46
52
  library: {
47
- type: 'module'
53
+ type: 'global'
48
54
  },
49
55
  chunkFilename: '[name].js',
50
- filename: 'peernet.js',
56
+ filename: '[name].js',
51
57
  path: path.resolve(__dirname, 'dist', 'browser'),
52
58
  },
53
59
  }];
@@ -1,50 +0,0 @@
1
- (self["webpackChunk_leofcoin_peernet"] = self["webpackChunk_leofcoin_peernet"] || []).push([[510],{
2
-
3
- /***/ 5162:
4
- /***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
5
-
6
- __webpack_require__.r(__webpack_exports__);
7
- /* harmony import */ var _leofcoin_multi_wallet__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(9755);
8
-
9
-
10
- /**
11
- * @params {String} network
12
- * @return {object} { identity, accounts, config }
13
- */
14
- /* harmony default export */ __webpack_exports__["default"] = (async network => {
15
- let wallet = new _leofcoin_multi_wallet__WEBPACK_IMPORTED_MODULE_0__/* ["default"] */ .Z(network);
16
- /**
17
- * @type {string}
18
- */
19
- const mnemonic = await wallet.generate();
20
-
21
- wallet = new _leofcoin_multi_wallet__WEBPACK_IMPORTED_MODULE_0__/* ["default"] */ .Z(network)
22
- await wallet.recover(mnemonic, network)
23
- /**
24
- * @type {object}
25
- */
26
- const account = wallet.account(0)
27
- /**
28
- * @type {object}
29
- */
30
- const external = account.external(0)
31
- const internal = account.internal(0)
32
-
33
- return {
34
- identity: {
35
- mnemonic,
36
- // multiWIF: wallet.export(),
37
- publicKey: external.publicKey,
38
- privateKey: external.privateKey,
39
- walletId: external.id
40
- },
41
- accounts: [['main account', external.address, internal.address]]
42
- // config: {
43
- // }
44
- }
45
- });
46
-
47
-
48
- /***/ })
49
-
50
- }])