@leofcoin/peernet 0.18.8 → 1.0.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/src/peernet.ts CHANGED
@@ -10,6 +10,7 @@ import { encapsulatedError, dhtError,
10
10
 
11
11
  import LeofcoinStorage from '@leofcoin/storage'
12
12
  import { utils as codecUtils } from '@leofcoin/codecs'
13
+ import Identity from './identity.js'
13
14
  globalThis.LeofcoinStorage = LeofcoinStorage
14
15
 
15
16
  globalThis.leofcoin = globalThis.leofcoin || {}
@@ -21,6 +22,7 @@ globalThis.globalSub = globalThis.globalSub || new PubSub(true)
21
22
  * const peernet = new Peernet();
22
23
  */
23
24
  export default class Peernet {
25
+ identity: Identity
24
26
  stores: [] = []
25
27
  /**
26
28
  * @type {Object}
@@ -54,7 +56,7 @@ export default class Peernet {
54
56
  * @example
55
57
  * const peernet = new Peernet({network: 'leofcoin', root: '.leofcoin'});
56
58
  */
57
- constructor(options: options) {
59
+ constructor(options: options, password) {
58
60
  /**
59
61
  * @property {String} network - current network
60
62
  */
@@ -74,7 +76,15 @@ export default class Peernet {
74
76
  up: 0,
75
77
  down: 0,
76
78
  }
77
- return this._init(options)
79
+ return this._init(options, password)
80
+ }
81
+
82
+ get id() {
83
+ return this.identity.id
84
+ }
85
+
86
+ get accounts(): Promise<[[name: string, externalAddress: string, internalAddress: string]]> {
87
+ return this.identity.accounts
78
88
  }
79
89
 
80
90
  get defaultStores() {
@@ -146,7 +156,7 @@ export default class Peernet {
146
156
  *
147
157
  * @return {Promise} instance of Peernet
148
158
  */
149
- async _init(options) {
159
+ async _init(options, password) {
150
160
  this.requestProtos = {}
151
161
  this.storePrefix = options.storePrefix
152
162
  this.root = options.root
@@ -201,37 +211,10 @@ export default class Peernet {
201
211
  await this.addStore(store, options.storePrefix, options.root)
202
212
  }
203
213
 
204
- const accountExists = await accountStore.has('public')
205
- if (accountExists) {
206
- const pub = await accountStore.get('public')
207
- this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
208
- let accounts = await walletStore.get('accounts')
209
- accounts = new TextDecoder().decode(accounts)
210
- const selected = await walletStore.get('selected-account')
211
- globalThis.peernet.selectedAccount = new TextDecoder().decode(selected)
212
-
213
- // fixing account issue (string while needs to be a JSON)
214
- // TODO: remove when on mainnet
215
- try {
216
- this.accounts = JSON.parse(accounts)
217
- } catch {
218
- this.accounts = [accounts.split(',')]
219
- }
220
- } else {
221
- const importee = await import(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account')
222
- const generateAccount = importee.default
223
- const {identity, accounts, config} = await generateAccount(this.network)
224
- // await accountStore.put('config', JSON.stringify(config));
225
- await accountStore.put('public', JSON.stringify({walletId: identity.walletId}));
226
-
227
- await walletStore.put('version', String(1))
228
- await walletStore.put('accounts', JSON.stringify(accounts))
229
- await walletStore.put('selected-account', accounts[0][1])
230
- await walletStore.put('identity', JSON.stringify(identity))
231
-
232
- globalThis.peernet.selectedAccount = accounts[0][1]
233
- this.id = identity.walletId
234
- }
214
+ this.identity = new Identity(this.network)
215
+ await this.identity.load(password)
216
+
217
+
235
218
  this._peerHandler = new PeerDiscovery(this.id)
236
219
  this.peerId = this.id
237
220
 
@@ -0,0 +1 @@
1
+ export default async () => prompt('enter password')
@@ -0,0 +1,6 @@
1
+ import inquirer from 'inquirer'
2
+
3
+ export default async () => {
4
+ const answers = await inquirer.prompt({type: 'password', name: 'password', message: 'Enter password'})
5
+ return answers.password
6
+ }