@leofcoin/peernet 0.18.8 → 1.0.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.
@@ -1,2 +1,2 @@
1
- export { P as default } from './peernet-34ea081d.js';
2
- import './value-40634404.js';
1
+ export { P as default } from './peernet-e74b2091.js';
2
+ import './value-157ab062.js';
@@ -0,0 +1,2 @@
1
+ declare function _default(): Promise<string>;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare function _default(): Promise<any>;
2
+ export default _default;
@@ -0,0 +1,3 @@
1
+ var browser = async () => prompt('enter password');
2
+
3
+ export { browser as default };
@@ -1,64 +1,64 @@
1
- // import base32 from '@vandeurenglenn/base32'
2
- // import base58 from '@vandeurenglenn/base58'
3
-
4
- // export const encodings = {
5
- // base58,
6
- // base32
7
- // }
8
-
9
- const encode = (string, encoding = 'utf-8') => {
10
- if (typeof string === 'string') {
11
- let encoded;
12
-
13
- // if (encodings[encoding]) encoded = encodings[encoding].encode(encoded)
14
- encoded = new TextEncoder().encode(string);
15
- return encoded
16
- }
17
- throw Error(`expected typeof String instead got ${string}`)
18
- };
19
-
20
- const decode = (uint8Array, encoding) => {
21
- if (uint8Array instanceof Uint8Array) {
22
- let decoded;
23
- // if (encodings[encoding]) decoded = encodings[encoding].decode(decoded)
24
- decoded = new TextDecoder().decode(uint8Array);
25
-
26
- return decoded
27
- }
28
- throw Error(`expected typeof uint8Array instead got ${uint8Array}`)
1
+ // import base32 from '@vandeurenglenn/base32'
2
+ // import base58 from '@vandeurenglenn/base58'
3
+
4
+ // export const encodings = {
5
+ // base58,
6
+ // base32
7
+ // }
8
+
9
+ const encode = (string, encoding = 'utf-8') => {
10
+ if (typeof string === 'string') {
11
+ let encoded;
12
+
13
+ // if (encodings[encoding]) encoded = encodings[encoding].encode(encoded)
14
+ encoded = new TextEncoder().encode(string);
15
+ return encoded
16
+ }
17
+ throw Error(`expected typeof String instead got ${string}`)
18
+ };
19
+
20
+ const decode = (uint8Array, encoding) => {
21
+ if (uint8Array instanceof Uint8Array) {
22
+ let decoded;
23
+ // if (encodings[encoding]) decoded = encodings[encoding].decode(decoded)
24
+ decoded = new TextDecoder().decode(uint8Array);
25
+
26
+ return decoded
27
+ }
28
+ throw Error(`expected typeof uint8Array instead got ${uint8Array}`)
29
29
  };
30
30
 
31
- class KeyValue {
32
-
33
- /**
34
- * @param {string | Uint8Array} input
35
- */
36
- constructor(input) {
37
- if (typeof input === 'string') {
38
- this.uint8Array = encode(input);
39
- } else if (input instanceof Uint8Array) {
40
- this.uint8Array = input;
41
- } else if (input instanceof KeyValue) {
42
- this.uint8Array = input.uint8Array;
43
- } else {
44
- throw new Error('Invalid KeyValue, should be a String, Uint8Array or KeyValue')
45
- }
46
- }
47
-
48
- isKeyValue() {
49
- return true
50
- }
51
-
52
- /**
53
- * Convert to the string representation
54
- *
55
- * @param {import('uint8arrays/to-string').SupportedEncodings} [encoding='utf8'] - The encoding to use.
56
- * @returns {string}
57
- */
58
- toString(encoding = 'utf8') {
59
- return decode(this.uint8Array)
60
- }
61
-
31
+ class KeyValue {
32
+
33
+ /**
34
+ * @param {string | Uint8Array} input
35
+ */
36
+ constructor(input) {
37
+ if (typeof input === 'string') {
38
+ this.uint8Array = encode(input);
39
+ } else if (input instanceof Uint8Array) {
40
+ this.uint8Array = input;
41
+ } else if (input instanceof KeyValue) {
42
+ this.uint8Array = input.uint8Array;
43
+ } else {
44
+ throw new Error('Invalid KeyValue, should be a String, Uint8Array or KeyValue')
45
+ }
46
+ }
47
+
48
+ isKeyValue() {
49
+ return true
50
+ }
51
+
52
+ /**
53
+ * Convert to the string representation
54
+ *
55
+ * @param {import('uint8arrays/to-string').SupportedEncodings} [encoding='utf8'] - The encoding to use.
56
+ * @returns {string}
57
+ */
58
+ toString(encoding = 'utf8') {
59
+ return decode(this.uint8Array)
60
+ }
61
+
62
62
  }
63
63
 
64
64
  export { KeyValue as K };
@@ -3,6 +3,7 @@ import PubSub from '@vandeurenglenn/little-pubsub';
3
3
  import { Codec } from '@leofcoin/codec-format-interface';
4
4
  import LeofcoinStorage from '@leofcoin/storage';
5
5
  import { utils } from '@leofcoin/codecs';
6
+ import MultiWallet from '@leofcoin/multi-wallet';
6
7
 
7
8
  const BufferToUint8Array = data => {
8
9
  if (data.type === 'Buffer') {
@@ -264,15 +265,8 @@ class MessageHandler {
264
265
  * @return message
265
266
  */
266
267
  async hashAndSignMessage(message) {
267
- let identity = await walletStore.get('identity');
268
- identity = JSON.parse(identity);
269
- if (!globalThis.MultiWallet) {
270
- const importee = await import(/* webpackChunkName: "multi-wallet" */ '@leofcoin/multi-wallet');
271
- globalThis.MultiWallet = importee.default;
272
- }
273
- const wallet = new MultiWallet(this.network);
274
- wallet.recover(identity.mnemonic);
275
- message.decoded.signature = wallet.sign(Buffer.from(await message.hash).slice(0, 32));
268
+ const hash = await message.peernetHash;
269
+ message.decoded.signature = globalThis.identity.sign(hash.buffer);
276
270
  return message;
277
271
  }
278
272
  /**
@@ -305,6 +299,75 @@ const nothingFoundError = (hash) => {
305
299
  return new Error(`nothing found for ${hash}`);
306
300
  };
307
301
 
302
+ class Identity {
303
+ #wallet;
304
+ network;
305
+ id;
306
+ constructor(network) {
307
+ this.network = network;
308
+ }
309
+ get accounts() {
310
+ return this.getAccounts();
311
+ }
312
+ async getAccounts() {
313
+ let accounts = await globalThis.walletStore.get('accounts');
314
+ accounts = new TextDecoder().decode(accounts);
315
+ return JSON.parse(accounts);
316
+ }
317
+ async load(password) {
318
+ if (!password) {
319
+ const importee = await import('./src/prompts/password.js');
320
+ password = await importee.default();
321
+ }
322
+ const accountExists = await globalThis.accountStore.has('public');
323
+ if (accountExists) {
324
+ const pub = await globalThis.accountStore.get('public');
325
+ this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
326
+ const selected = await globalThis.walletStore.get('selected-account');
327
+ globalThis.peernet.selectedAccount = new TextDecoder().decode(selected);
328
+ }
329
+ else {
330
+ const importee = await import(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account');
331
+ const { identity, accounts } = await importee.default(password, this.network);
332
+ await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
333
+ await globalThis.walletStore.put('version', String(1));
334
+ await globalThis.walletStore.put('accounts', JSON.stringify(accounts));
335
+ await globalThis.walletStore.put('selected-account', accounts[0][1]);
336
+ await globalThis.walletStore.put('identity', JSON.stringify(identity));
337
+ globalThis.peernet.selectedAccount = accounts[0][1];
338
+ this.id = identity.walletId;
339
+ }
340
+ const identity = JSON.parse(new TextDecoder().decode(await globalThis.walletStore.get('identity')));
341
+ this.#wallet = new MultiWallet(this.network);
342
+ await this.#wallet.recover(identity.mnemonic, password, this.network);
343
+ }
344
+ sign(hash) {
345
+ return this.#wallet.sign(hash.subarray(0, 32));
346
+ }
347
+ async export(password) {
348
+ if (!password)
349
+ throw new Error('IdentityExportError: password required');
350
+ const identity = JSON.parse(new TextDecoder().decode(await globalThis.walletStore.get('identity')));
351
+ this.#wallet = new MultiWallet(this.network);
352
+ await this.#wallet.recover(identity.mnemonic, password, this.network);
353
+ return this.#wallet.toMultiWif();
354
+ }
355
+ async import(multiWIF) {
356
+ this.#wallet = new MultiWallet(this.network);
357
+ await this.#wallet.fromMultiWif(multiWIF);
358
+ }
359
+ async unlock({ key, iv, cipher }) {
360
+ }
361
+ /**
362
+ * Lock current wallet or import wallet using a MultiWIF and lock
363
+ *
364
+ * @param password
365
+ * @param multiWIF
366
+ */
367
+ async lock(password, multiWIF) {
368
+ }
369
+ }
370
+
308
371
  globalThis.LeofcoinStorage = LeofcoinStorage;
309
372
  globalThis.leofcoin = globalThis.leofcoin || {};
310
373
  globalThis.pubsub = globalThis.pubsub || new PubSub();
@@ -315,6 +378,7 @@ globalThis.globalSub = globalThis.globalSub || new PubSub(true);
315
378
  * const peernet = new Peernet();
316
379
  */
317
380
  class Peernet {
381
+ identity;
318
382
  stores = [];
319
383
  /**
320
384
  * @type {Object}
@@ -340,7 +404,7 @@ class Peernet {
340
404
  * @example
341
405
  * const peernet = new Peernet({network: 'leofcoin', root: '.leofcoin'});
342
406
  */
343
- constructor(options) {
407
+ constructor(options, password) {
344
408
  /**
345
409
  * @property {String} network - current network
346
410
  */
@@ -360,7 +424,13 @@ class Peernet {
360
424
  up: 0,
361
425
  down: 0,
362
426
  };
363
- return this._init(options);
427
+ return this._init(options, password);
428
+ }
429
+ get id() {
430
+ return this.identity.id;
431
+ }
432
+ get accounts() {
433
+ return this.identity.accounts;
364
434
  }
365
435
  get defaultStores() {
366
436
  return ['account', 'wallet', 'block', 'transaction', 'chain', 'data', 'message'];
@@ -420,7 +490,7 @@ class Peernet {
420
490
  *
421
491
  * @return {Promise} instance of Peernet
422
492
  */
423
- async _init(options) {
493
+ async _init(options, password) {
424
494
  this.requestProtos = {};
425
495
  this.storePrefix = options.storePrefix;
426
496
  this.root = options.root;
@@ -456,36 +526,8 @@ class Peernet {
456
526
  for (const store of this.defaultStores) {
457
527
  await this.addStore(store, options.storePrefix, options.root);
458
528
  }
459
- const accountExists = await accountStore.has('public');
460
- if (accountExists) {
461
- const pub = await accountStore.get('public');
462
- this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
463
- let accounts = await walletStore.get('accounts');
464
- accounts = new TextDecoder().decode(accounts);
465
- const selected = await walletStore.get('selected-account');
466
- globalThis.peernet.selectedAccount = new TextDecoder().decode(selected);
467
- // fixing account issue (string while needs to be a JSON)
468
- // TODO: remove when on mainnet
469
- try {
470
- this.accounts = JSON.parse(accounts);
471
- }
472
- catch {
473
- this.accounts = [accounts.split(',')];
474
- }
475
- }
476
- else {
477
- const importee = await import(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account');
478
- const generateAccount = importee.default;
479
- const { identity, accounts, config } = await generateAccount(this.network);
480
- // await accountStore.put('config', JSON.stringify(config));
481
- await accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
482
- await walletStore.put('version', String(1));
483
- await walletStore.put('accounts', JSON.stringify(accounts));
484
- await walletStore.put('selected-account', accounts[0][1]);
485
- await walletStore.put('identity', JSON.stringify(identity));
486
- globalThis.peernet.selectedAccount = accounts[0][1];
487
- this.id = identity.walletId;
488
- }
529
+ this.identity = new Identity(this.network);
530
+ await this.identity.load(password);
489
531
  this._peerHandler = new PeerDiscovery(this.id);
490
532
  this.peerId = this.id;
491
533
  pubsub.subscribe('peer:connected', async (peer) => {
@@ -0,0 +1,2 @@
1
+ declare function _default(): Promise<string>;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare function _default(): Promise<any>;
2
+ export default _default;
@@ -0,0 +1,8 @@
1
+ import inquirer from 'inquirer';
2
+
3
+ var node = async () => {
4
+ const answers = await inquirer.prompt({type: 'password', name: 'password', message: 'Enter password'});
5
+ return answers.password
6
+ };
7
+
8
+ export { node as default };