@leofcoin/peernet 1.2.22 → 1.2.23

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,4 +1,4 @@
1
- import { m as getDefaultExportFromCjs } from './identity-CjBX8cWv.js';
1
+ import { m as getDefaultExportFromCjs } from './identity-BS_eDrwh.js';
2
2
 
3
3
  function _mergeNamespaces(n, m) {
4
4
  m.forEach(function (e) {
@@ -1,4 +1,4 @@
1
- import { m as getDefaultExportFromCjs } from './identity-CjBX8cWv.js';
1
+ import { m as getDefaultExportFromCjs } from './identity-BS_eDrwh.js';
2
2
 
3
3
  function _mergeNamespaces(n, m) {
4
4
  m.forEach(function (e) {
@@ -1,5 +1,5 @@
1
- import { L as LittlePubSub, d as deflate_1, i as inflate_1, c as createDebugger } from './peernet-D3z5pEBD.js';
2
- import './identity-CjBX8cWv.js';
1
+ import { L as LittlePubSub, d as deflate_1, i as inflate_1, c as createDebugger } from './peernet-CgU-ZdIe.js';
2
+ import './identity-BS_eDrwh.js';
3
3
  import 'crypto';
4
4
  import './value-C3vAp-wb.js';
5
5
 
@@ -232,7 +232,7 @@ class SocketRequestClient {
232
232
  const init = async () => {
233
233
  // @ts-ignore
234
234
  if (!globalThis.WebSocket && !this.#experimentalWebsocket)
235
- globalThis.WebSocket = (await import('./browser-tK3uOLQV.js').then(function (n) { return n.b; })).default.w3cwebsocket;
235
+ globalThis.WebSocket = (await import('./browser-BKlRwB0A.js').then(function (n) { return n.b; })).default.w3cwebsocket;
236
236
  const client = new WebSocket(this.#url, this.#protocol);
237
237
  if (this.#experimentalWebsocket) {
238
238
  client.addEventListener('error', this.onerror);
@@ -1303,7 +1303,7 @@ class Client {
1303
1303
  }
1304
1304
  }
1305
1305
  async #loadNodeWebrtcImplementation() {
1306
- const koushWrtcModule = await import('./browser-DEjI2nFg.js').then(function (n) { return n.b; });
1306
+ const koushWrtcModule = await import('./browser-Dx3X9MHO.js').then(function (n) { return n.b; });
1307
1307
  const koushWrtc = koushWrtcModule.default;
1308
1308
  if (!isWrtcImplementation(koushWrtc)) {
1309
1309
  throw new Error('@koush/wrtc does not match required wrtc contract');
@@ -8261,6 +8261,7 @@ var qrcode = /*@__PURE__*/getDefaultExportFromCjs(browserExports);
8261
8261
 
8262
8262
  class Identity {
8263
8263
  #wallet;
8264
+ #accountWallets = new Map();
8264
8265
  network;
8265
8266
  id;
8266
8267
  selectedAccount;
@@ -8298,7 +8299,7 @@ class Identity {
8298
8299
  this.selectedAccount = new TextDecoder().decode(selected);
8299
8300
  }
8300
8301
  else {
8301
- const importee = await import(/* webpackChunkName: "generate-account" */ './index-DIhxISkU.js');
8302
+ const importee = await import(/* webpackChunkName: "generate-account" */ './index-B91mcwxm.js');
8302
8303
  const { identity, accounts } = await importee.default(password, this.network);
8303
8304
  await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
8304
8305
  await globalThis.walletStore.put('version', String(1));
@@ -8312,13 +8313,36 @@ class Identity {
8312
8313
  this.#wallet = new MultiWallet(this.network);
8313
8314
  const multiWIF = await decrypt(password, base58$1.decode(identity.multiWIF));
8314
8315
  await this.#wallet.fromMultiWif(multiWIF);
8316
+ await this.#loadAccountWallets();
8317
+ }
8318
+ async #loadAccountWallets() {
8319
+ this.#accountWallets.clear();
8320
+ const accounts = await this.getAccounts();
8321
+ for (const [index, [, externalAddress, internalAddress]] of accounts.entries()) {
8322
+ const account = this.#wallet.account(index + 1);
8323
+ const external = (await account.external(1));
8324
+ const internal = (await account.internal(1));
8325
+ if ((await external.address) !== externalAddress || (await internal.address) !== internalAddress) {
8326
+ throw new Error(`stored account ${index + 1} does not match this identity`);
8327
+ }
8328
+ this.#accountWallets.set(externalAddress, external);
8329
+ this.#accountWallets.set(internalAddress, internal);
8330
+ }
8331
+ if (!this.#accountWallets.has(this.selectedAccount)) {
8332
+ throw new Error(`selected account ${this.selectedAccount} is not part of this identity`);
8333
+ }
8315
8334
  }
8316
8335
  selectAccount(account) {
8336
+ if (!this.#accountWallets.has(account))
8337
+ throw new Error(`unknown identity account ${account}`);
8317
8338
  this.selectedAccount = account;
8318
8339
  return walletStore.put('selected-account', account);
8319
8340
  }
8320
8341
  sign(hash) {
8321
- return this.#wallet.sign(hash.subarray(0, 32));
8342
+ const wallet = this.#accountWallets.get(this.selectedAccount);
8343
+ if (!wallet)
8344
+ throw new Error(`no signer available for selected account ${this.selectedAccount}`);
8345
+ return wallet.sign(hash.subarray(0, 32));
8322
8346
  }
8323
8347
  lock(password) {
8324
8348
  this.#wallet.lock(password);
@@ -1,2 +1,2 @@
1
- export { I as default } from './identity-CjBX8cWv.js';
1
+ export { I as default } from './identity-BS_eDrwh.js';
2
2
  import 'crypto';
@@ -1,4 +1,4 @@
1
- import { M as MultiWallet, l as encrypt, a as base58$1 } from './identity-CjBX8cWv.js';
1
+ import { M as MultiWallet, l as encrypt, a as base58$1 } from './identity-BS_eDrwh.js';
2
2
  import 'crypto';
3
3
 
4
4
  /**
@@ -1,5 +1,5 @@
1
- import { F as FormatInterface } from './peernet-D3z5pEBD.js';
2
- import './identity-CjBX8cWv.js';
1
+ import { F as FormatInterface } from './peernet-CgU-ZdIe.js';
2
+ import './identity-BS_eDrwh.js';
3
3
  import 'crypto';
4
4
  import './value-C3vAp-wb.js';
5
5
 
@@ -1,4 +1,4 @@
1
- import { i as index$2, b as base, a as base58$1, c as index$3, d as index$4, e as index$5, f as createSHA512, g as createSHA384, h as createSHA256, j as createSHA224, k as createSHA1, I as Identity } from './identity-CjBX8cWv.js';
1
+ import { i as index$2, b as base, a as base58$1, c as index$3, d as index$4, e as index$5, f as createSHA512, g as createSHA384, h as createSHA256, j as createSHA224, k as createSHA1, I as Identity } from './identity-BS_eDrwh.js';
2
2
  import { K as KeyPath, a as KeyValue } from './value-C3vAp-wb.js';
3
3
 
4
4
  const getTargets = () => Array.from([]);
@@ -8615,7 +8615,7 @@ class Peernet {
8615
8615
  await getAddress();
8616
8616
  this.storePrefix = options.storePrefix;
8617
8617
  this.root = options.root;
8618
- const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-BX59GgBQ.js');
8618
+ const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-D0Goitr6.js');
8619
8619
  /**
8620
8620
  * proto Object containing protos
8621
8621
  * @type {Object}
@@ -8662,7 +8662,7 @@ class Peernet {
8662
8662
  if (this.#starting || this.#started)
8663
8663
  return;
8664
8664
  this.#starting = true;
8665
- const importee = await import('./client-Ba1cHcxF.js');
8665
+ const importee = await import('./client-eYBPUrVq.js');
8666
8666
  /**
8667
8667
  * @access public
8668
8668
  * @type {PeernetClient}
@@ -1,4 +1,4 @@
1
- export { P as default } from './peernet-D3z5pEBD.js';
2
- import './identity-CjBX8cWv.js';
1
+ export { P as default } from './peernet-CgU-ZdIe.js';
2
+ import './identity-BS_eDrwh.js';
3
3
  import './value-C3vAp-wb.js';
4
4
  import 'crypto';
@@ -6,6 +6,7 @@ import qrcode from 'qrcode';
6
6
 
7
7
  class Identity {
8
8
  #wallet;
9
+ #accountWallets = new Map();
9
10
  network;
10
11
  id;
11
12
  selectedAccount;
@@ -57,13 +58,36 @@ class Identity {
57
58
  this.#wallet = new MultiWallet(this.network);
58
59
  const multiWIF = await decrypt(password, base58.decode(identity.multiWIF));
59
60
  await this.#wallet.fromMultiWif(multiWIF);
61
+ await this.#loadAccountWallets();
62
+ }
63
+ async #loadAccountWallets() {
64
+ this.#accountWallets.clear();
65
+ const accounts = await this.getAccounts();
66
+ for (const [index, [, externalAddress, internalAddress]] of accounts.entries()) {
67
+ const account = this.#wallet.account(index + 1);
68
+ const external = (await account.external(1));
69
+ const internal = (await account.internal(1));
70
+ if ((await external.address) !== externalAddress || (await internal.address) !== internalAddress) {
71
+ throw new Error(`stored account ${index + 1} does not match this identity`);
72
+ }
73
+ this.#accountWallets.set(externalAddress, external);
74
+ this.#accountWallets.set(internalAddress, internal);
75
+ }
76
+ if (!this.#accountWallets.has(this.selectedAccount)) {
77
+ throw new Error(`selected account ${this.selectedAccount} is not part of this identity`);
78
+ }
60
79
  }
61
80
  selectAccount(account) {
81
+ if (!this.#accountWallets.has(account))
82
+ throw new Error(`unknown identity account ${account}`);
62
83
  this.selectedAccount = account;
63
84
  return walletStore.put('selected-account', account);
64
85
  }
65
86
  sign(hash) {
66
- return this.#wallet.sign(hash.subarray(0, 32));
87
+ const wallet = this.#accountWallets.get(this.selectedAccount);
88
+ if (!wallet)
89
+ throw new Error(`no signer available for selected account ${this.selectedAccount}`);
90
+ return wallet.sign(hash.subarray(0, 32));
67
91
  }
68
92
  lock(password) {
69
93
  this.#wallet.lock(password);
@@ -9,7 +9,7 @@ export default class Identity {
9
9
  getAccounts(): Promise<[[name: string, externalAddress: string, internalAddress: string]]>;
10
10
  load(password?: string): Promise<void>;
11
11
  selectAccount(account: string): Promise<unknown>;
12
- sign(hash: Uint8Array): any;
12
+ sign(hash: Uint8Array): Promise<Uint8Array<ArrayBufferLike>>;
13
13
  lock(password: string): void;
14
14
  unlock(password: string): void;
15
15
  export(password: string): Promise<any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "1.2.22",
3
+ "version": "1.2.23",
4
4
  "description": "",
5
5
  "browser": "./exports/browser/peernet.js",
6
6
  "exports": {
@@ -107,7 +107,7 @@
107
107
  "@leofcoin/multi-wallet": "^3.1.8",
108
108
  "@leofcoin/storage": "^3.5.38",
109
109
  "@mapbox/node-pre-gyp": "^2.0.3",
110
- "@netpeer/swarm": "^0.9.8",
110
+ "@netpeer/swarm": "^0.9.9",
111
111
  "@vandeurenglenn/base58": "^1.1.9",
112
112
  "@vandeurenglenn/debug": "^1.4.0",
113
113
  "@vandeurenglenn/little-pubsub": "^1.5.2",
package/src/identity.ts CHANGED
@@ -7,6 +7,7 @@ import qrcode from 'qrcode'
7
7
 
8
8
  export default class Identity {
9
9
  #wallet: MultiWallet
10
+ #accountWallets = new Map<string, { sign: (hash: Uint8Array) => Promise<Uint8Array> }>()
10
11
  network
11
12
  id: string
12
13
  selectedAccount: string
@@ -63,15 +64,43 @@ export default class Identity {
63
64
  this.#wallet = new MultiWallet(this.network)
64
65
  const multiWIF = await decrypt(password, base58.decode(identity.multiWIF))
65
66
  await this.#wallet.fromMultiWif(multiWIF)
67
+ await this.#loadAccountWallets()
68
+ }
69
+
70
+ async #loadAccountWallets() {
71
+ this.#accountWallets.clear()
72
+ const accounts = await this.getAccounts()
73
+ for (const [index, [, externalAddress, internalAddress]] of accounts.entries()) {
74
+ const account = this.#wallet.account(index + 1)
75
+ const external = (await account.external(1)) as unknown as {
76
+ address: Promise<string>
77
+ sign: (hash: Uint8Array) => Promise<Uint8Array>
78
+ }
79
+ const internal = (await account.internal(1)) as unknown as {
80
+ address: Promise<string>
81
+ sign: (hash: Uint8Array) => Promise<Uint8Array>
82
+ }
83
+ if ((await external.address) !== externalAddress || (await internal.address) !== internalAddress) {
84
+ throw new Error(`stored account ${index + 1} does not match this identity`)
85
+ }
86
+ this.#accountWallets.set(externalAddress, external)
87
+ this.#accountWallets.set(internalAddress, internal)
88
+ }
89
+ if (!this.#accountWallets.has(this.selectedAccount)) {
90
+ throw new Error(`selected account ${this.selectedAccount} is not part of this identity`)
91
+ }
66
92
  }
67
93
 
68
94
  selectAccount(account: string) {
95
+ if (!this.#accountWallets.has(account)) throw new Error(`unknown identity account ${account}`)
69
96
  this.selectedAccount = account
70
97
  return walletStore.put('selected-account', account)
71
98
  }
72
99
 
73
100
  sign(hash: Uint8Array) {
74
- return this.#wallet.sign(hash.subarray(0, 32))
101
+ const wallet = this.#accountWallets.get(this.selectedAccount)
102
+ if (!wallet) throw new Error(`no signer available for selected account ${this.selectedAccount}`)
103
+ return wallet.sign(hash.subarray(0, 32))
75
104
  }
76
105
 
77
106
  lock(password: string) {
@@ -3,11 +3,13 @@ import assert from 'node:assert/strict'
3
3
 
4
4
  import Peernet from '../exports/peernet.js'
5
5
  import Identity from '../exports/identity.js'
6
+ import MultiWallet from '@leofcoin/multi-wallet'
7
+ import { fromBase58, toBase58 } from '@vandeurenglenn/typed-array-utils'
6
8
 
7
9
  const options = {
8
10
  network: 'leofcoin:peach',
9
11
  stars: [],
10
- root: '.testnet',
12
+ root: `.testnet-${process.pid}`,
11
13
  version: '1.0.0',
12
14
  storePrefix: 'test',
13
15
  autoStart: false
@@ -234,6 +236,14 @@ test('selected account is set', () => {
234
236
  assert.ok(typeof peernet.identity.selectedAccount === 'string')
235
237
  })
236
238
 
239
+ test('identity signatures authenticate the selected account', async () => {
240
+ const payload = new Uint8Array(32).fill(7)
241
+ const signature = toBase58(await peernet.identity.sign(payload))
242
+ const verifier = new MultiWallet(peernet.network)
243
+ await verifier.fromAddress(peernet.selectedAccount, null, peernet.network)
244
+ assert.equal(await verifier.verify(fromBase58(signature), payload), true)
245
+ })
246
+
237
247
  test('error handling methods exist', () => {
238
248
  assert.equal(typeof peernet.handleDHT, 'function')
239
249
  assert.equal(typeof peernet.handleData, 'function')