@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.
@@ -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-1908438f.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;
@@ -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 };
@@ -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 };
@@ -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$1 from '@leofcoin/multi-wallet';
6
7
 
7
8
  const BufferToUint8Array = data => {
8
9
  if (data.type === 'Buffer') {
@@ -272,7 +273,8 @@ class MessageHandler {
272
273
  }
273
274
  const wallet = new MultiWallet(this.network);
274
275
  wallet.recover(identity.mnemonic);
275
- message.decoded.signature = wallet.sign(Buffer.from(await message.hash).slice(0, 32));
276
+ const hash = await message.hash();
277
+ message.decoded.signature = wallet.sign(hash.subarray(0, 32));
276
278
  return message;
277
279
  }
278
280
  /**
@@ -305,6 +307,84 @@ const nothingFoundError = (hash) => {
305
307
  return new Error(`nothing found for ${hash}`);
306
308
  };
307
309
 
310
+ const walletStore$1 = globalThis.walletStore;
311
+ const accountStore = globalThis.accountStore;
312
+ class Identity {
313
+ #wallet;
314
+ network;
315
+ id;
316
+ constructor(network) {
317
+ this.network = network;
318
+ }
319
+ get accounts() {
320
+ return this.getAccounts();
321
+ }
322
+ async getAccounts() {
323
+ let accounts = await walletStore$1.get('accounts');
324
+ accounts = new TextDecoder().decode(accounts);
325
+ return JSON.parse(accounts);
326
+ }
327
+ async load(password) {
328
+ let importee;
329
+ if (!password) {
330
+ if (globalThis.navigator)
331
+ importee = await import('./src/prompts/password/browser.js');
332
+ else {
333
+ const path = await import('node:path');
334
+ const { fileURLToPath } = await import('node:url');
335
+ const __filename = fileURLToPath(import.meta.url);
336
+ const __dirname = path.dirname(__filename);
337
+ const url = path.join(__dirname, './prompts/password/node.js');
338
+ importee = await import(url);
339
+ }
340
+ password = await importee.default();
341
+ }
342
+ const accountExists = await accountStore.has('public');
343
+ if (accountExists) {
344
+ const pub = await accountStore.get('public');
345
+ this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
346
+ const selected = await walletStore$1.get('selected-account');
347
+ globalThis.peernet.selectedAccount = new TextDecoder().decode(selected);
348
+ }
349
+ else {
350
+ const importee = await import(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account');
351
+ const { identity, accounts } = await importee.default(password, this.network);
352
+ await accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
353
+ await walletStore$1.put('version', String(1));
354
+ await walletStore$1.put('accounts', JSON.stringify(accounts));
355
+ await walletStore$1.put('selected-account', accounts[0][1]);
356
+ await walletStore$1.put('identity', JSON.stringify(identity));
357
+ globalThis.peernet.selectedAccount = accounts[0][1];
358
+ this.id = identity.walletId;
359
+ }
360
+ const identity = JSON.parse(new TextDecoder().decode(await walletStore$1.get('identity')));
361
+ this.#wallet = new MultiWallet$1(this.network);
362
+ await this.#wallet.recover(identity.mnemonic, password, this.network);
363
+ }
364
+ async export(password) {
365
+ if (!password)
366
+ throw new Error('IdentityExportError: password required');
367
+ const identity = JSON.parse(new TextDecoder().decode(await walletStore$1.get('identity')));
368
+ this.#wallet = new MultiWallet$1(this.network);
369
+ await this.#wallet.recover(identity.mnemonic, password, this.network);
370
+ return this.#wallet.toMultiWif();
371
+ }
372
+ async import(multiWIF) {
373
+ this.#wallet = new MultiWallet$1(this.network);
374
+ await this.#wallet.fromMultiWif(multiWIF);
375
+ }
376
+ async unlock({ key, iv, cipher }) {
377
+ }
378
+ /**
379
+ * Lock current wallet or import wallet using a MultiWIF and lock
380
+ *
381
+ * @param password
382
+ * @param multiWIF
383
+ */
384
+ async lock(password, multiWIF) {
385
+ }
386
+ }
387
+
308
388
  globalThis.LeofcoinStorage = LeofcoinStorage;
309
389
  globalThis.leofcoin = globalThis.leofcoin || {};
310
390
  globalThis.pubsub = globalThis.pubsub || new PubSub();
@@ -315,6 +395,7 @@ globalThis.globalSub = globalThis.globalSub || new PubSub(true);
315
395
  * const peernet = new Peernet();
316
396
  */
317
397
  class Peernet {
398
+ identity;
318
399
  stores = [];
319
400
  /**
320
401
  * @type {Object}
@@ -362,6 +443,12 @@ class Peernet {
362
443
  };
363
444
  return this._init(options);
364
445
  }
446
+ get id() {
447
+ return this.identity.id;
448
+ }
449
+ get accounts() {
450
+ return this.identity.accounts;
451
+ }
365
452
  get defaultStores() {
366
453
  return ['account', 'wallet', 'block', 'transaction', 'chain', 'data', 'message'];
367
454
  }
@@ -456,36 +543,8 @@ class Peernet {
456
543
  for (const store of this.defaultStores) {
457
544
  await this.addStore(store, options.storePrefix, options.root);
458
545
  }
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
- }
546
+ this.identity = new Identity(this.network);
547
+ await this.identity.load();
489
548
  this._peerHandler = new PeerDiscovery(this.id);
490
549
  this.peerId = this.id;
491
550
  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;