@leofcoin/peernet 1.2.25 → 1.2.27

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-DWchlbmt.js';
1
+ import { m as getDefaultExportFromCjs } from './identity-ChyyZSve.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-DWchlbmt.js';
1
+ import { m as getDefaultExportFromCjs } from './identity-ChyyZSve.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-Coi40mR_.js';
2
- import './identity-DWchlbmt.js';
1
+ import { L as LittlePubSub, d as deflate_1, i as inflate_1, c as createDebugger } from './peernet-r_9IyzTe.js';
2
+ import './identity-ChyyZSve.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-BuaFyOQj.js').then(function (n) { return n.b; })).default.w3cwebsocket;
235
+ globalThis.WebSocket = (await import('./browser-_hiyXwPp.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);
@@ -1104,6 +1104,27 @@ class CircuitPeer {
1104
1104
  }
1105
1105
  }
1106
1106
 
1107
+ /** Normalize current star announcements and the legacy peer-id-only format. */
1108
+ const normalizePeerAnnouncement = (value) => {
1109
+ if (typeof value === 'string') {
1110
+ const peerId = value.trim();
1111
+ return peerId && peerId !== 'undefined' ? { peerId } : null;
1112
+ }
1113
+ if (!value || typeof value !== 'object')
1114
+ return null;
1115
+ const candidate = value;
1116
+ if (typeof candidate.peerId !== 'string')
1117
+ return null;
1118
+ const peerId = candidate.peerId.trim();
1119
+ if (!peerId || peerId === 'undefined')
1120
+ return null;
1121
+ return {
1122
+ peerId,
1123
+ version: typeof candidate.version === 'string' ? candidate.version : undefined,
1124
+ transport: candidate.transport
1125
+ };
1126
+ };
1127
+
1107
1128
  // Simple CRC32 implementation
1108
1129
  const crc32 = (data) => {
1109
1130
  let crc = 0xffffffff;
@@ -1303,7 +1324,7 @@ class Client {
1303
1324
  }
1304
1325
  }
1305
1326
  async #loadNodeWebrtcImplementation() {
1306
- const koushWrtcModule = await import('./browser-BOiH47em.js').then(function (n) { return n.b; });
1327
+ const koushWrtcModule = await import('./browser-D-r0O9Qn.js').then(function (n) { return n.b; });
1307
1328
  const koushWrtc = koushWrtcModule.default;
1308
1329
  if (!isWrtcImplementation(koushWrtc)) {
1309
1330
  throw new Error('@koush/wrtc does not match required wrtc contract');
@@ -1853,7 +1874,16 @@ class Client {
1853
1874
  this.#peerTransportKinds.set(peerId, 'webrtc');
1854
1875
  return peer;
1855
1876
  };
1856
- #peerJoined = async ({ peerId, version, transport }, star) => {
1877
+ #peerJoined = async (announcement, star) => {
1878
+ const normalized = normalizePeerAnnouncement(announcement);
1879
+ if (!normalized) {
1880
+ debug('ignored malformed peer announcement');
1881
+ return;
1882
+ }
1883
+ const { peerId, transport } = normalized;
1884
+ // Legacy stars announce only the peer id. This value is used for transport
1885
+ // negotiation; applications still perform their own version handshake.
1886
+ const version = normalized.version ?? this.version;
1857
1887
  if (this.peerId === peerId)
1858
1888
  return;
1859
1889
  // Stars can announce the same peer more than once while its transport is
@@ -8308,7 +8308,7 @@ class Identity {
8308
8308
  accounts = new TextDecoder().decode(accounts);
8309
8309
  return JSON.parse(accounts);
8310
8310
  }
8311
- async load(password) {
8311
+ async load(password, fresh = false) {
8312
8312
  if (password && password.includes('.txt')) {
8313
8313
  const { readFile } = await import('fs/promises');
8314
8314
  try {
@@ -8324,15 +8324,19 @@ class Identity {
8324
8324
  password = await importee.default();
8325
8325
  }
8326
8326
  const accountExists = await globalThis.accountStore.has('public');
8327
- if (accountExists) {
8327
+ if (accountExists && !fresh) {
8328
8328
  const pub = await globalThis.accountStore.get('public');
8329
8329
  this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
8330
8330
  const selected = await globalThis.walletStore.get('selected-account');
8331
8331
  this.selectedAccount = new TextDecoder().decode(selected);
8332
8332
  }
8333
8333
  else {
8334
- const importee = await import(/* webpackChunkName: "generate-account" */ './index-s61gSKAd.js');
8334
+ const importee = await import(/* webpackChunkName: "generate-account" */ './index-BD0Anx7_.js');
8335
8335
  const { identity, accounts } = await importee.default(password, this.network);
8336
+ if (fresh) {
8337
+ await globalThis.accountStore.clear();
8338
+ await globalThis.walletStore.clear();
8339
+ }
8336
8340
  await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
8337
8341
  await globalThis.walletStore.put('version', String(1));
8338
8342
  await globalThis.walletStore.put('accounts', JSON.stringify(accounts));
@@ -1,2 +1,2 @@
1
- export { I as default, r as resolveAccountWallets } from './identity-DWchlbmt.js';
1
+ export { I as default, r as resolveAccountWallets } from './identity-ChyyZSve.js';
2
2
  import 'crypto';
@@ -1,4 +1,4 @@
1
- import { M as MultiWallet, l as encrypt, a as base58$1 } from './identity-DWchlbmt.js';
1
+ import { M as MultiWallet, l as encrypt, a as base58$1 } from './identity-ChyyZSve.js';
2
2
  import 'crypto';
3
3
 
4
4
  /**
@@ -1,5 +1,5 @@
1
- import { F as FormatInterface } from './peernet-Coi40mR_.js';
2
- import './identity-DWchlbmt.js';
1
+ import { F as FormatInterface } from './peernet-r_9IyzTe.js';
2
+ import './identity-ChyyZSve.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-DWchlbmt.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-ChyyZSve.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-CZ-3jxiD.js');
8618
+ const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-UKnuelZ7.js');
8619
8619
  /**
8620
8620
  * proto Object containing protos
8621
8621
  * @type {Object}
@@ -8646,7 +8646,7 @@ class Peernet {
8646
8646
  await this.addStore(store, options.storePrefix, options.root);
8647
8647
  }
8648
8648
  this.identity = new Identity(this.network);
8649
- await this.identity.load(password);
8649
+ await this.identity.load(password, options.freshIdentity === true);
8650
8650
  this._peerHandler = new PeerDiscovery(this.id);
8651
8651
  this.peerId = this.id;
8652
8652
  /**
@@ -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-B2wkkpij.js');
8665
+ const importee = await import('./client-BVhUamQG.js');
8666
8666
  /**
8667
8667
  * @access public
8668
8668
  * @type {PeernetClient}
@@ -1,4 +1,4 @@
1
- export { P as default } from './peernet-Coi40mR_.js';
2
- import './identity-DWchlbmt.js';
1
+ export { P as default } from './peernet-r_9IyzTe.js';
2
+ import './identity-ChyyZSve.js';
3
3
  import './value-C3vAp-wb.js';
4
4
  import 'crypto';
@@ -53,7 +53,7 @@ class Identity {
53
53
  accounts = new TextDecoder().decode(accounts);
54
54
  return JSON.parse(accounts);
55
55
  }
56
- async load(password) {
56
+ async load(password, fresh = false) {
57
57
  if (password && password.includes('.txt')) {
58
58
  const { readFile } = await import('fs/promises');
59
59
  try {
@@ -69,7 +69,7 @@ class Identity {
69
69
  password = await importee.default();
70
70
  }
71
71
  const accountExists = await globalThis.accountStore.has('public');
72
- if (accountExists) {
72
+ if (accountExists && !fresh) {
73
73
  const pub = await globalThis.accountStore.get('public');
74
74
  this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
75
75
  const selected = await globalThis.walletStore.get('selected-account');
@@ -78,6 +78,10 @@ class Identity {
78
78
  else {
79
79
  const importee = await import(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account');
80
80
  const { identity, accounts } = await importee.default(password, this.network);
81
+ if (fresh) {
82
+ await globalThis.accountStore.clear();
83
+ await globalThis.walletStore.clear();
84
+ }
81
85
  await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
82
86
  await globalThis.walletStore.put('version', String(1));
83
87
  await globalThis.walletStore.put('accounts', JSON.stringify(accounts));
@@ -487,7 +487,7 @@ class Peernet {
487
487
  await this.addStore(store, options.storePrefix, options.root);
488
488
  }
489
489
  this.identity = new Identity(this.network);
490
- await this.identity.load(password);
490
+ await this.identity.load(password, options.freshIdentity === true);
491
491
  this._peerHandler = new PeerDiscovery(this.id);
492
492
  this.peerId = this.id;
493
493
  /**
@@ -14,7 +14,7 @@ export default class Identity {
14
14
  constructor(network: string);
15
15
  get accounts(): Promise<[[name: string, externalAddress: string, internalAddress: string]]>;
16
16
  getAccounts(): Promise<[[name: string, externalAddress: string, internalAddress: string]]>;
17
- load(password?: string): Promise<void>;
17
+ load(password?: string, fresh?: boolean): Promise<void>;
18
18
  selectAccount(account: string): Promise<unknown>;
19
19
  sign(hash: Uint8Array): Promise<Uint8Array<ArrayBufferLike>>;
20
20
  lock(password: string): void;
@@ -86,14 +86,47 @@ export default class Peernet {
86
86
  *
87
87
  * @return {Array} peerId
88
88
  */
89
- get peers(): [string, import("@netpeer/swarm/peer").default][];
89
+ get peers(): [string, {
90
+ connected: boolean;
91
+ initiator: boolean;
92
+ peerId: string;
93
+ channelName: string;
94
+ on: (event: string, handler: (...args: unknown[]) => void) => unknown;
95
+ off: (event: string, handler: (...args: unknown[]) => void) => unknown;
96
+ signal: (signalData: unknown) => void;
97
+ destroy: () => void;
98
+ send: (data: Uint8Array, id?: string) => void;
99
+ request: (data: Uint8Array, id?: string) => Promise<Uint8Array>;
100
+ }][];
90
101
  get connections(): {
91
- [index: string]: import("@netpeer/swarm/peer").default;
102
+ [index: string]: {
103
+ connected: boolean;
104
+ initiator: boolean;
105
+ peerId: string;
106
+ channelName: string;
107
+ on: (event: string, handler: (...args: unknown[]) => void) => unknown;
108
+ off: (event: string, handler: (...args: unknown[]) => void) => unknown;
109
+ signal: (signalData: unknown) => void;
110
+ destroy: () => void;
111
+ send: (data: Uint8Array, id?: string) => void;
112
+ request: (data: Uint8Array, id?: string) => Promise<Uint8Array>;
113
+ };
92
114
  };
93
115
  /**
94
116
  * @return {String} id - peerId
95
117
  */
96
- getConnection(id: any): import("@netpeer/swarm/peer").default;
118
+ getConnection(id: any): {
119
+ connected: boolean;
120
+ initiator: boolean;
121
+ peerId: string;
122
+ channelName: string;
123
+ on: (event: string, handler: (...args: unknown[]) => void) => unknown;
124
+ off: (event: string, handler: (...args: unknown[]) => void) => unknown;
125
+ signal: (signalData: unknown) => void;
126
+ destroy: () => void;
127
+ send: (data: Uint8Array, id?: string) => void;
128
+ request: (data: Uint8Array, id?: string) => Promise<Uint8Array>;
129
+ };
97
130
  /**
98
131
  * @private
99
132
  *
@@ -105,6 +138,7 @@ export default class Peernet {
105
138
  _init(options: {
106
139
  storePrefix?: string;
107
140
  root?: string;
141
+ freshIdentity?: boolean;
108
142
  }, password: string): Promise<Peernet>;
109
143
  start(): Promise<void>;
110
144
  addRequestHandler(name: any, method: any): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "1.2.25",
3
+ "version": "1.2.27",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",
@@ -112,7 +112,7 @@
112
112
  "@leofcoin/multi-wallet": "^3.1.8",
113
113
  "@leofcoin/storage": "^3.5.38",
114
114
  "@mapbox/node-pre-gyp": "^2.0.3",
115
- "@netpeer/swarm": "^0.9.9",
115
+ "@netpeer/swarm": "^0.9.11",
116
116
  "@vandeurenglenn/base58": "^1.1.9",
117
117
  "@vandeurenglenn/debug": "^1.4.0",
118
118
  "@vandeurenglenn/little-pubsub": "^1.5.2",
package/src/identity.ts CHANGED
@@ -72,7 +72,7 @@ export default class Identity {
72
72
  return JSON.parse(accounts)
73
73
  }
74
74
 
75
- async load(password?: string): Promise<void> {
75
+ async load(password?: string, fresh = false): Promise<void> {
76
76
  if (password && password.includes('.txt')) {
77
77
  const { readFile } = await import('fs/promises')
78
78
  try {
@@ -88,7 +88,7 @@ export default class Identity {
88
88
  }
89
89
 
90
90
  const accountExists = await globalThis.accountStore.has('public')
91
- if (accountExists) {
91
+ if (accountExists && !fresh) {
92
92
  const pub = await globalThis.accountStore.get('public')
93
93
  this.id = JSON.parse(new TextDecoder().decode(pub)).walletId
94
94
  const selected = await globalThis.walletStore.get('selected-account')
@@ -96,6 +96,10 @@ export default class Identity {
96
96
  } else {
97
97
  const importee = await import(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account')
98
98
  const { identity, accounts } = await importee.default(password, this.network)
99
+ if (fresh) {
100
+ await globalThis.accountStore.clear()
101
+ await globalThis.walletStore.clear()
102
+ }
99
103
  await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }))
100
104
 
101
105
  await globalThis.walletStore.put('version', String(1))
package/src/peernet.ts CHANGED
@@ -199,7 +199,10 @@ export default class Peernet {
199
199
  *
200
200
  * @return {Promise} instance of Peernet
201
201
  */
202
- async _init(options: { storePrefix?: string; root?: string }, password: string): Promise<Peernet> {
202
+ async _init(
203
+ options: { storePrefix?: string; root?: string; freshIdentity?: boolean },
204
+ password: string
205
+ ): Promise<Peernet> {
203
206
  await getAddress()
204
207
  this.storePrefix = options.storePrefix
205
208
  this.root = options.root
@@ -254,7 +257,7 @@ export default class Peernet {
254
257
  }
255
258
 
256
259
  this.identity = new Identity(this.network)
257
- await this.identity.load(password)
260
+ await this.identity.load(password, options.freshIdentity === true)
258
261
 
259
262
  this._peerHandler = new PeerDiscovery(this.id)
260
263
  this.peerId = this.id
@@ -35,6 +35,16 @@ test('exposes identity instance', () => {
35
35
  assert.ok(peernet.identity instanceof Identity)
36
36
  })
37
37
 
38
+ test('freshIdentity replaces a persisted identity and signer', async () => {
39
+ const originalId = peernet.id
40
+ const originalAccount = peernet.selectedAccount
41
+ const replacement = await new Peernet({ ...options, freshIdentity: true }, password)
42
+
43
+ assert.notEqual(replacement.id, originalId)
44
+ assert.notEqual(replacement.selectedAccount, originalAccount)
45
+ assert.equal(new TextDecoder().decode(await globalThis.walletStore.get('selected-account')), replacement.selectedAccount)
46
+ })
47
+
38
48
  test('has a DHT instance', () => {
39
49
  assert.ok(peernet.dht)
40
50
  })