@leofcoin/chain 1.7.156 → 1.7.157

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.
@@ -5915,9 +5915,8 @@ class Chain extends VersionControl {
5915
5915
  }
5916
5916
  }
5917
5917
  async #addBlock(block) {
5918
- // Store the original received encoded bytes for validation
5919
- const receivedEncoded = block instanceof BlockMessage ? block.encoded : block;
5920
- const blockMessage = await new BlockMessage(block);
5918
+ const receivedEncoded = block instanceof BlockMessage ? block.encoded : this.#normalizeBytes(block);
5919
+ const blockMessage = await new BlockMessage(receivedEncoded);
5921
5920
  const hash = await blockMessage.hash();
5922
5921
  // Verify data integrity: re-encode should produce the same bytes
5923
5922
  const canonicalEncoded = blockMessage.encoded;
@@ -6157,7 +6156,7 @@ class Chain extends VersionControl {
6157
6156
  debug(`created block: ${hash} @${block.index}`);
6158
6157
  // Publish canonical encoded form via codec interface
6159
6158
  console.log(`[chain] 📤 Publishing block #${block.index} | hash: ${hash} | encoded bytes: ${blockMessage.encoded.length}`);
6160
- globalThis.peernet.publish('add-block', blockMessage.decoded);
6159
+ globalThis.peernet.publish('add-block', Array.from(blockMessage.encoded));
6161
6160
  globalThis.pubsub.publish('add-block', blockMessage.decoded);
6162
6161
  }
6163
6162
  catch (error) {
@@ -6298,6 +6297,26 @@ class Chain extends VersionControl {
6298
6297
  console.warn('Failed to reconnect to peers:', error.message);
6299
6298
  }
6300
6299
  }
6300
+ #normalizeBytes(input) {
6301
+ if (input instanceof Uint8Array)
6302
+ return input;
6303
+ if (Array.isArray(input))
6304
+ return Uint8Array.from(input);
6305
+ if (input && typeof input === 'object') {
6306
+ if (input.type === 'Buffer' && Array.isArray(input.data))
6307
+ return Uint8Array.from(input.data);
6308
+ const values = Object.values(input);
6309
+ if (values.length > 0 && values.every((v) => typeof v === 'number'))
6310
+ return Uint8Array.from(values);
6311
+ }
6312
+ try {
6313
+ // @ts-ignore
6314
+ return new Uint8Array(input);
6315
+ }
6316
+ catch {
6317
+ throw new Error('Unsupported block payload format for normalization');
6318
+ }
6319
+ }
6301
6320
  }
6302
6321
 
6303
6322
  export { Chain as default };
package/exports/chain.js CHANGED
@@ -2056,9 +2056,8 @@ class Chain extends VersionControl {
2056
2056
  }
2057
2057
  }
2058
2058
  async #addBlock(block) {
2059
- // Store the original received encoded bytes for validation
2060
- const receivedEncoded = block instanceof BlockMessage ? block.encoded : block;
2061
- const blockMessage = await new BlockMessage(block);
2059
+ const receivedEncoded = block instanceof BlockMessage ? block.encoded : this.#normalizeBytes(block);
2060
+ const blockMessage = await new BlockMessage(receivedEncoded);
2062
2061
  const hash = await blockMessage.hash();
2063
2062
  // Verify data integrity: re-encode should produce the same bytes
2064
2063
  const canonicalEncoded = blockMessage.encoded;
@@ -2298,7 +2297,7 @@ class Chain extends VersionControl {
2298
2297
  debug(`created block: ${hash} @${block.index}`);
2299
2298
  // Publish canonical encoded form via codec interface
2300
2299
  console.log(`[chain] 📤 Publishing block #${block.index} | hash: ${hash} | encoded bytes: ${blockMessage.encoded.length}`);
2301
- globalThis.peernet.publish('add-block', blockMessage.decoded);
2300
+ globalThis.peernet.publish('add-block', Array.from(blockMessage.encoded));
2302
2301
  globalThis.pubsub.publish('add-block', blockMessage.decoded);
2303
2302
  }
2304
2303
  catch (error) {
@@ -2439,6 +2438,26 @@ class Chain extends VersionControl {
2439
2438
  console.warn('Failed to reconnect to peers:', error.message);
2440
2439
  }
2441
2440
  }
2441
+ #normalizeBytes(input) {
2442
+ if (input instanceof Uint8Array)
2443
+ return input;
2444
+ if (Array.isArray(input))
2445
+ return Uint8Array.from(input);
2446
+ if (input && typeof input === 'object') {
2447
+ if (input.type === 'Buffer' && Array.isArray(input.data))
2448
+ return Uint8Array.from(input.data);
2449
+ const values = Object.values(input);
2450
+ if (values.length > 0 && values.every((v) => typeof v === 'number'))
2451
+ return Uint8Array.from(values);
2452
+ }
2453
+ try {
2454
+ // @ts-ignore
2455
+ return new Uint8Array(input);
2456
+ }
2457
+ catch {
2458
+ throw new Error('Unsupported block payload format for normalization');
2459
+ }
2460
+ }
2442
2461
  }
2443
2462
 
2444
2463
  export { Chain as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.7.156",
3
+ "version": "1.7.157",
4
4
  "description": "Official javascript implementation",
5
5
  "private": false,
6
6
  "exports": {