@leofcoin/chain 1.9.21 → 1.9.24

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,6 +1,6 @@
1
- import { g as getDefaultExportFromCjs } from './node-browser-CYsu-WiL.js';
1
+ import { g as getDefaultExportFromCjs } from './node-browser-Dc4HZiX0.js';
2
2
  import 'crypto';
3
- import './constants-BCaq9RBn.js';
3
+ import './constants-COpvMqFX.js';
4
4
 
5
5
  function _mergeNamespaces$1(n, m) {
6
6
  m.forEach(function (e) {
@@ -1,6 +1,6 @@
1
- import { g as getDefaultExportFromCjs } from './node-browser-CYsu-WiL.js';
1
+ import { g as getDefaultExportFromCjs } from './node-browser-Dc4HZiX0.js';
2
2
  import 'crypto';
3
- import './constants-BCaq9RBn.js';
3
+ import './constants-COpvMqFX.js';
4
4
 
5
5
  function _mergeNamespaces$1(n, m) {
6
6
  m.forEach(function (e) {
@@ -1,4 +1,4 @@
1
- import { t as toBase58, T as TransactionMessage, C as ContractMessage, R as RawTransactionMessage, B as BlockMessage, L as LastBlockMessage, P as PROTOCOL_VERSION, a as REACHED_ONE_ZERO_ZERO, b as PrevoteMessage, c as PrecommitMessage, d as ProposalMessage, e as BWMessage, S as StateMessage, f as BWRequestMessage } from './constants-BCaq9RBn.js';
1
+ import { t as toBase58, T as TransactionMessage, C as ContractMessage, R as RawTransactionMessage, B as BlockMessage, L as LastBlockMessage, P as PROTOCOL_VERSION, a as REACHED_ONE_ZERO_ZERO, b as PrevoteMessage, c as PrecommitMessage, d as ProposalMessage, e as BWMessage, S as StateMessage, f as Codec, g as BWRequestMessage } from './constants-COpvMqFX.js';
2
2
 
3
3
  const getTargets = () => Array.from([]);
4
4
  const isEnabled = (target) => {
@@ -4947,6 +4947,42 @@ class State extends Contract {
4947
4947
  get resolving() {
4948
4948
  return this.#resolving;
4949
4949
  }
4950
+ async #resolveLastBlockMessage(result) {
4951
+ if (result instanceof Uint8Array) {
4952
+ try {
4953
+ let payload = result;
4954
+ for (let i = 0; i < 3; i += 1) {
4955
+ try {
4956
+ const wrapped = await new globalThis.peernet.protos["peernet-response"](payload);
4957
+ if (wrapped?.decoded?.response === void 0) break;
4958
+ payload = wrapped.decoded.response;
4959
+ } catch {
4960
+ break;
4961
+ }
4962
+ }
4963
+ if (payload !== result) return this.#resolveLastBlockMessage(payload);
4964
+ return new LastBlockMessage(result);
4965
+ } catch {
4966
+ const candidate = new TextDecoder().decode(result);
4967
+ if (candidate) {
4968
+ const blockData = await globalThis.peernet.get(candidate, "block");
4969
+ if (blockData) return new BlockMessage(blockData);
4970
+ }
4971
+ }
4972
+ }
4973
+ if (typeof result === "string") {
4974
+ const blockData = await globalThis.peernet.get(result, "block");
4975
+ if (blockData) return new BlockMessage(blockData);
4976
+ }
4977
+ if (result && typeof result === "object") {
4978
+ const response = result.decoded?.response ?? result.response;
4979
+ if (response !== void 0) return this.#resolveLastBlockMessage(response);
4980
+ if ("hash" in result && "index" in result) {
4981
+ return new LastBlockMessage(result);
4982
+ }
4983
+ }
4984
+ throw new Error(`invalid lastBlock payload: ${typeof result}`);
4985
+ }
4950
4986
  get contracts() {
4951
4987
  return this.#machine.contracts;
4952
4988
  }
@@ -5386,7 +5422,7 @@ class State extends Contract {
5386
5422
  }
5387
5423
  }
5388
5424
  }
5389
- return { result: new LastBlockMessage(result), peer };
5425
+ return { result, peer };
5390
5426
  } catch (error) {
5391
5427
  const peerId = peer?.peerId || peer?.id || peer?.address || "unknown";
5392
5428
  debug$1(`lastBlock request failed: ${peerId}:`, error?.message ?? error);
@@ -5407,6 +5443,12 @@ class State extends Contract {
5407
5443
  throw new ResolveError("latestBlock: no responses from compatible peers");
5408
5444
  }
5409
5445
  console.log({ promises });
5446
+ promises = await Promise.all(
5447
+ promises.map(async (item) => ({
5448
+ value: (await this.#resolveLastBlockMessage(item.value)).decoded,
5449
+ peer: item.peer
5450
+ }))
5451
+ );
5410
5452
  let latest = { index: 0, hash: "0x0", previousHash: "0x0" };
5411
5453
  promises = promises.sort((a, b) => b.index - a.index);
5412
5454
  if (promises.length > 0) latest = promises[0].value;
@@ -5833,7 +5875,7 @@ class ConnectionMonitor {
5833
5875
  try {
5834
5876
  const networkName = globalThis.peernet?.network;
5835
5877
  if (networkName && typeof networkName === "string") {
5836
- const { default: networks } = await import('./constants-BCaq9RBn.js').then(function (n) { return n.n; });
5878
+ const { default: networks } = await import('./constants-COpvMqFX.js').then(function (n) { return n.n; });
5837
5879
  const [mainKey, subKey] = networkName.split(":");
5838
5880
  const networkConfig = networks?.[mainKey]?.[subKey];
5839
5881
  if (networkConfig?.stars && Array.isArray(networkConfig.stars)) {
@@ -6407,6 +6449,8 @@ class Chain extends VersionControl {
6407
6449
  let bytes = payload instanceof Uint8Array ? payload : new Uint8Array(payload);
6408
6450
  for (let i = 0; i < 3; i += 1) {
6409
6451
  try {
6452
+ const codec = new Codec(bytes);
6453
+ if (codec.name !== "peernet-response") break;
6410
6454
  const wrapped = await new globalThis.peernet.protos["peernet-response"](bytes);
6411
6455
  if (wrapped?.decoded?.response === void 0) break;
6412
6456
  const next = wrapped.decoded.response;
@@ -6451,6 +6495,42 @@ class Chain extends VersionControl {
6451
6495
  throw error;
6452
6496
  }
6453
6497
  }
6498
+ async #resolveLastBlockMessage(result) {
6499
+ if (result instanceof Uint8Array) {
6500
+ try {
6501
+ let payload = result;
6502
+ for (let i = 0; i < 3; i += 1) {
6503
+ try {
6504
+ const wrapped = await new globalThis.peernet.protos["peernet-response"](payload);
6505
+ if (wrapped?.decoded?.response === void 0) break;
6506
+ payload = wrapped.decoded.response;
6507
+ } catch {
6508
+ break;
6509
+ }
6510
+ }
6511
+ if (payload !== result) return this.#resolveLastBlockMessage(payload);
6512
+ return new LastBlockMessage(result);
6513
+ } catch {
6514
+ const candidate = new TextDecoder().decode(result);
6515
+ if (candidate) {
6516
+ const blockData = await globalThis.peernet.get(candidate, "block");
6517
+ if (blockData) return new BlockMessage(blockData);
6518
+ }
6519
+ }
6520
+ }
6521
+ if (typeof result === "string") {
6522
+ const blockData = await globalThis.peernet.get(result, "block");
6523
+ if (blockData) return new BlockMessage(blockData);
6524
+ }
6525
+ if (result && typeof result === "object") {
6526
+ const response = result.decoded?.response ?? result.response;
6527
+ if (response !== void 0) return this.#resolveLastBlockMessage(response);
6528
+ if ("hash" in result && "index" in result) {
6529
+ return new LastBlockMessage(result);
6530
+ }
6531
+ }
6532
+ throw new Error(`invalid lastBlock payload: ${typeof result}`);
6533
+ }
6454
6534
  async #decodeKnownBlocksResponse(response) {
6455
6535
  if (!response) return null;
6456
6536
  if (Array.isArray(response)) {
@@ -6576,11 +6656,12 @@ class Chain extends VersionControl {
6576
6656
  console.log(await this.lastBlock);
6577
6657
  const lastBlockRaw = await this.#makeRequest(peer, "lastBlock");
6578
6658
  console.log("raw last block response:", lastBlockRaw);
6579
- if (lastBlockRaw === void 0 || lastBlockRaw === null || typeof lastBlockRaw !== "object" && !(lastBlockRaw instanceof Uint8Array)) {
6659
+ if (lastBlockRaw === void 0 || lastBlockRaw === null) {
6580
6660
  throw new Error(`invalid lastBlock payload: ${typeof lastBlockRaw}`);
6581
6661
  }
6582
- console.log(new LastBlockMessage(lastBlockRaw));
6583
- lastBlock = new LastBlockMessage(lastBlockRaw).decoded;
6662
+ const lastBlockMessage = await this.#resolveLastBlockMessage(lastBlockRaw);
6663
+ console.log(lastBlockMessage);
6664
+ lastBlock = lastBlockMessage.decoded;
6584
6665
  } catch (error) {
6585
6666
  const peerName = peer?.peerId || peer?.id || peer?.address || peerId || "unknown";
6586
6667
  debug(`lastBlock request failed: ${peerName}:`, error?.message ?? error);
@@ -1,6 +1,6 @@
1
- import { i as inflate_1, c as createDebugger, d as deflate_1, L as LittlePubSub } from './node-browser-CYsu-WiL.js';
1
+ import { i as inflate_1, c as createDebugger, d as deflate_1, L as LittlePubSub } from './node-browser-Dc4HZiX0.js';
2
2
  import 'crypto';
3
- import './constants-BCaq9RBn.js';
3
+ import './constants-COpvMqFX.js';
4
4
 
5
5
  class Api {
6
6
  _pubsub;
@@ -231,7 +231,7 @@ class SocketRequestClient {
231
231
  const init = async () => {
232
232
  // @ts-ignore
233
233
  if (!globalThis.WebSocket && !this.#experimentalWebsocket)
234
- globalThis.WebSocket = (await import('./browser-8BFql6K9-Cfm2axSE.js').then(function (n) { return n.b; })).default.w3cwebsocket;
234
+ globalThis.WebSocket = (await import('./browser-8BFql6K9-BMXXABIY.js').then(function (n) { return n.b; })).default.w3cwebsocket;
235
235
  const client = new WebSocket(this.#url, this.#protocol);
236
236
  if (this.#experimentalWebsocket) {
237
237
  client.addEventListener('error', this.onerror);
@@ -1302,7 +1302,7 @@ class Client {
1302
1302
  }
1303
1303
  }
1304
1304
  async #loadNodeWebrtcImplementation() {
1305
- const koushWrtcModule = await import('./browser-FVp_QbaL-DShoEW2_.js').then(function (n) { return n.b; });
1305
+ const koushWrtcModule = await import('./browser-FVp_QbaL-C-GWIuv6.js').then(function (n) { return n.b; });
1306
1306
  const koushWrtc = koushWrtcModule.default;
1307
1307
  if (!isWrtcImplementation(koushWrtc)) {
1308
1308
  throw new Error('@koush/wrtc does not match required wrtc contract');
@@ -7802,7 +7802,7 @@ const utils = {
7802
7802
  codecs: registry
7803
7803
  };
7804
7804
 
7805
- class Codec extends BasicInterface {
7805
+ let Codec$1 = class Codec extends BasicInterface {
7806
7806
  codecBuffer;
7807
7807
  codec;
7808
7808
  hashAlg;
@@ -7902,7 +7902,7 @@ class Codec extends BasicInterface {
7902
7902
  this.encoded = index$4.encode(codec);
7903
7903
  return this.encoded;
7904
7904
  }
7905
- }
7905
+ };
7906
7906
 
7907
7907
  /*!
7908
7908
  * hash-wasm (https://www.npmjs.com/package/hash-wasm)
@@ -8517,7 +8517,7 @@ class CodecHash extends BasicInterface {
8517
8517
  // For large buffers, only check first bytes for codec prefix
8518
8518
  // Codec prefixes are small (varint encoded), so 100 bytes is more than enough
8519
8519
  const checkBuffer = uint8Array.length > 100 ? uint8Array.subarray(0, 100) : uint8Array;
8520
- this.discoCodec = new Codec(checkBuffer);
8520
+ this.discoCodec = new Codec$1(checkBuffer);
8521
8521
  const name = this.discoCodec.name;
8522
8522
  if (name) {
8523
8523
  this.name = name;
@@ -8550,7 +8550,7 @@ class CodecHash extends BasicInterface {
8550
8550
  this.name = name;
8551
8551
  if (!buffer)
8552
8552
  throw new Error('buffer is required for encoding');
8553
- this.discoCodec = new Codec(this.name);
8553
+ this.discoCodec = new Codec$1(this.name);
8554
8554
  let hashAlg = this.discoCodec.hashAlg;
8555
8555
  const hashVariant = Number(hashAlg.split('-')[hashAlg.split('-').length - 1]);
8556
8556
  if (hashAlg.includes('dbl')) {
@@ -8582,7 +8582,7 @@ class CodecHash extends BasicInterface {
8582
8582
  decode(buffer) {
8583
8583
  this.encoded = buffer;
8584
8584
  const codec = index$4.decode(buffer);
8585
- this.discoCodec = new Codec(codec, this.codecs);
8585
+ this.discoCodec = new Codec$1(codec, this.codecs);
8586
8586
  // TODO: validate codec
8587
8587
  buffer = buffer.slice(index$4.decode.bytes);
8588
8588
  this.size = index$4.decode(buffer);
@@ -8622,13 +8622,13 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
8622
8622
  hasCodec() {
8623
8623
  if (!this.encoded)
8624
8624
  return false;
8625
- const codec = new Codec(this.encoded);
8625
+ const codec = new Codec$1(this.encoded);
8626
8626
  if (codec.name)
8627
8627
  return true;
8628
8628
  }
8629
8629
  decode(encoded) {
8630
8630
  encoded = encoded || this.encoded;
8631
- const codec = new Codec(encoded);
8631
+ const codec = new Codec$1(encoded);
8632
8632
  if (codec.codecBuffer) {
8633
8633
  encoded = encoded.slice(codec.codecBuffer.length);
8634
8634
  this.name = codec.name;
@@ -8646,7 +8646,7 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
8646
8646
  encode(decoded) {
8647
8647
  let encoded;
8648
8648
  decoded = decoded || this.decoded;
8649
- const codec = new Codec(this.name);
8649
+ const codec = new Codec$1(this.name);
8650
8650
  if (decoded instanceof Uint8Array)
8651
8651
  encoded = decoded;
8652
8652
  else
@@ -8685,7 +8685,7 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
8685
8685
  get contentBytes() {
8686
8686
  if (!this.encoded)
8687
8687
  return new Uint8Array(0);
8688
- const codec = new Codec(this.encoded);
8688
+ const codec = new Codec$1(this.encoded);
8689
8689
  if (codec.codecBuffer) {
8690
8690
  return this.encoded.slice(codec.codecBuffer.length);
8691
8691
  }
@@ -8765,6 +8765,10 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
8765
8765
  * Interface for defining codec formats
8766
8766
  */
8767
8767
  const FormatInterface = FormatInterface$1;
8768
+ /**
8769
+ * Codec utility class
8770
+ */
8771
+ const Codec = Codec$1;
8768
8772
 
8769
8773
  var proto$b = {
8770
8774
  address: String(),
@@ -9040,4 +9044,4 @@ const DEFAULT_NODE_OPTIONS = {
9040
9044
  stars: networks.leofcoin.peach.stars
9041
9045
  };
9042
9046
 
9043
- export { BlockMessage as B, ContractMessage as C, DEFAULT_NODE_OPTIONS as D, LastBlockMessage as L, PROTOCOL_VERSION as P, RawTransactionMessage as R, StateMessage as S, TransactionMessage as T, ValidatorMessage as V, REACHED_ONE_ZERO_ZERO as a, PrevoteMessage as b, PrecommitMessage as c, ProposalMessage as d, BWMessage as e, BWRequestMessage as f, networks$1 as n, toBase58 as t };
9047
+ export { BlockMessage as B, ContractMessage as C, DEFAULT_NODE_OPTIONS as D, LastBlockMessage as L, PROTOCOL_VERSION as P, RawTransactionMessage as R, StateMessage as S, TransactionMessage as T, ValidatorMessage as V, REACHED_ONE_ZERO_ZERO as a, PrevoteMessage as b, PrecommitMessage as c, ProposalMessage as d, BWMessage as e, Codec as f, BWRequestMessage as g, networks$1 as n, toBase58 as t };
@@ -1,6 +1,6 @@
1
- import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-CYsu-WiL.js';
1
+ import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-Dc4HZiX0.js';
2
2
  import 'crypto';
3
- import './constants-BCaq9RBn.js';
3
+ import './constants-COpvMqFX.js';
4
4
 
5
5
  /**
6
6
  * @params {String} network
@@ -1,6 +1,6 @@
1
- import { F as FormatInterface } from './node-browser-CYsu-WiL.js';
1
+ import { F as FormatInterface } from './node-browser-Dc4HZiX0.js';
2
2
  import 'crypto';
3
- import './constants-BCaq9RBn.js';
3
+ import './constants-COpvMqFX.js';
4
4
 
5
5
  var proto$b = {
6
6
  data: new Uint8Array(),
@@ -1,5 +1,5 @@
1
1
  import { createHash as createHash$1 } from 'crypto';
2
- import { T as TransactionMessage, C as ContractMessage, B as BlockMessage, e as BWMessage, f as BWRequestMessage, V as ValidatorMessage, S as StateMessage, L as LastBlockMessage, D as DEFAULT_NODE_OPTIONS } from './constants-BCaq9RBn.js';
2
+ import { T as TransactionMessage, C as ContractMessage, B as BlockMessage, e as BWMessage, g as BWRequestMessage, V as ValidatorMessage, S as StateMessage, L as LastBlockMessage, D as DEFAULT_NODE_OPTIONS } from './constants-COpvMqFX.js';
3
3
 
4
4
  var nodeConfig = async (config = {
5
5
  network: "leofcoin:peach",
@@ -8337,7 +8337,7 @@ class Identity {
8337
8337
  this.selectedAccount = new TextDecoder().decode(selected);
8338
8338
  }
8339
8339
  else {
8340
- const importee = await import(/* webpackChunkName: "generate-account" */ './index-D6qd-AUn-q2EQ9wfH.js');
8340
+ const importee = await import(/* webpackChunkName: "generate-account" */ './index-D6qd-AUn-cJS9xh8w.js');
8341
8341
  const { identity, accounts } = await importee.default(password, this.network);
8342
8342
  await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
8343
8343
  await globalThis.walletStore.put('version', String(1));
@@ -16998,7 +16998,7 @@ class Peernet {
16998
16998
  await getAddress();
16999
16999
  this.storePrefix = options.storePrefix;
17000
17000
  this.root = options.root;
17001
- const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-BfDvXW-x-CNuQ6tVL.js');
17001
+ const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-BfDvXW-x-BaHit_or.js');
17002
17002
  /**
17003
17003
  * proto Object containing protos
17004
17004
  * @type {Object}
@@ -17045,7 +17045,7 @@ class Peernet {
17045
17045
  if (this.#starting || this.#started)
17046
17046
  return;
17047
17047
  this.#starting = true;
17048
- const importee = await import('./client-lPe0SUWx-Cm8Ph6Z5.js');
17048
+ const importee = await import('./client-lPe0SUWx-DwxXCP5p.js');
17049
17049
  /**
17050
17050
  * @access public
17051
17051
  * @type {PeernetClient}
@@ -1,3 +1,3 @@
1
- export { N as default } from './node-browser-CYsu-WiL.js';
1
+ export { N as default } from './node-browser-Dc4HZiX0.js';
2
2
  import 'crypto';
3
- import './constants-BCaq9RBn.js';
3
+ import './constants-COpvMqFX.js';
package/exports/chain.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { createDebugger } from '@vandeurenglenn/debug';
2
+ import { Codec } from '@leofcoin/codec-format-interface';
2
3
  import { jsonStringifyBigInt, jsonParseBigInt, formatBytes, parseUnits, formatUnits } from '@leofcoin/utils';
3
4
  import { TransactionMessage, BlockMessage, ContractMessage, LastBlockMessage, PrevoteMessage, PrecommitMessage, ProposalMessage, BWMessage, StateMessage, BWRequestMessage } from '@leofcoin/messages';
4
5
  import addresses, { contractFactory } from '@leofcoin/addresses';
@@ -1153,6 +1154,42 @@ class State extends Contract {
1153
1154
  get resolving() {
1154
1155
  return this.#resolving;
1155
1156
  }
1157
+ async #resolveLastBlockMessage(result) {
1158
+ if (result instanceof Uint8Array) {
1159
+ try {
1160
+ let payload = result;
1161
+ for (let i = 0; i < 3; i += 1) {
1162
+ try {
1163
+ const wrapped = await new globalThis.peernet.protos["peernet-response"](payload);
1164
+ if (wrapped?.decoded?.response === void 0) break;
1165
+ payload = wrapped.decoded.response;
1166
+ } catch {
1167
+ break;
1168
+ }
1169
+ }
1170
+ if (payload !== result) return this.#resolveLastBlockMessage(payload);
1171
+ return new LastBlockMessage(result);
1172
+ } catch {
1173
+ const candidate = new TextDecoder().decode(result);
1174
+ if (candidate) {
1175
+ const blockData = await globalThis.peernet.get(candidate, "block");
1176
+ if (blockData) return new BlockMessage(blockData);
1177
+ }
1178
+ }
1179
+ }
1180
+ if (typeof result === "string") {
1181
+ const blockData = await globalThis.peernet.get(result, "block");
1182
+ if (blockData) return new BlockMessage(blockData);
1183
+ }
1184
+ if (result && typeof result === "object") {
1185
+ const response = result.decoded?.response ?? result.response;
1186
+ if (response !== void 0) return this.#resolveLastBlockMessage(response);
1187
+ if ("hash" in result && "index" in result) {
1188
+ return new LastBlockMessage(result);
1189
+ }
1190
+ }
1191
+ throw new Error(`invalid lastBlock payload: ${typeof result}`);
1192
+ }
1156
1193
  get contracts() {
1157
1194
  return this.#machine.contracts;
1158
1195
  }
@@ -1592,7 +1629,7 @@ class State extends Contract {
1592
1629
  }
1593
1630
  }
1594
1631
  }
1595
- return { result: new LastBlockMessage(result), peer };
1632
+ return { result, peer };
1596
1633
  } catch (error) {
1597
1634
  const peerId = peer?.peerId || peer?.id || peer?.address || "unknown";
1598
1635
  debug$1(`lastBlock request failed: ${peerId}:`, error?.message ?? error);
@@ -1613,6 +1650,12 @@ class State extends Contract {
1613
1650
  throw new ResolveError("latestBlock: no responses from compatible peers");
1614
1651
  }
1615
1652
  console.log({ promises });
1653
+ promises = await Promise.all(
1654
+ promises.map(async (item) => ({
1655
+ value: (await this.#resolveLastBlockMessage(item.value)).decoded,
1656
+ peer: item.peer
1657
+ }))
1658
+ );
1616
1659
  let latest = { index: 0, hash: "0x0", previousHash: "0x0" };
1617
1660
  promises = promises.sort((a, b) => b.index - a.index);
1618
1661
  if (promises.length > 0) latest = promises[0].value;
@@ -2613,6 +2656,8 @@ class Chain extends VersionControl {
2613
2656
  let bytes = payload instanceof Uint8Array ? payload : new Uint8Array(payload);
2614
2657
  for (let i = 0; i < 3; i += 1) {
2615
2658
  try {
2659
+ const codec = new Codec(bytes);
2660
+ if (codec.name !== "peernet-response") break;
2616
2661
  const wrapped = await new globalThis.peernet.protos["peernet-response"](bytes);
2617
2662
  if (wrapped?.decoded?.response === void 0) break;
2618
2663
  const next = wrapped.decoded.response;
@@ -2657,6 +2702,42 @@ class Chain extends VersionControl {
2657
2702
  throw error;
2658
2703
  }
2659
2704
  }
2705
+ async #resolveLastBlockMessage(result) {
2706
+ if (result instanceof Uint8Array) {
2707
+ try {
2708
+ let payload = result;
2709
+ for (let i = 0; i < 3; i += 1) {
2710
+ try {
2711
+ const wrapped = await new globalThis.peernet.protos["peernet-response"](payload);
2712
+ if (wrapped?.decoded?.response === void 0) break;
2713
+ payload = wrapped.decoded.response;
2714
+ } catch {
2715
+ break;
2716
+ }
2717
+ }
2718
+ if (payload !== result) return this.#resolveLastBlockMessage(payload);
2719
+ return new LastBlockMessage(result);
2720
+ } catch {
2721
+ const candidate = new TextDecoder().decode(result);
2722
+ if (candidate) {
2723
+ const blockData = await globalThis.peernet.get(candidate, "block");
2724
+ if (blockData) return new BlockMessage(blockData);
2725
+ }
2726
+ }
2727
+ }
2728
+ if (typeof result === "string") {
2729
+ const blockData = await globalThis.peernet.get(result, "block");
2730
+ if (blockData) return new BlockMessage(blockData);
2731
+ }
2732
+ if (result && typeof result === "object") {
2733
+ const response = result.decoded?.response ?? result.response;
2734
+ if (response !== void 0) return this.#resolveLastBlockMessage(response);
2735
+ if ("hash" in result && "index" in result) {
2736
+ return new LastBlockMessage(result);
2737
+ }
2738
+ }
2739
+ throw new Error(`invalid lastBlock payload: ${typeof result}`);
2740
+ }
2660
2741
  async #decodeKnownBlocksResponse(response) {
2661
2742
  if (!response) return null;
2662
2743
  if (Array.isArray(response)) {
@@ -2782,11 +2863,12 @@ class Chain extends VersionControl {
2782
2863
  console.log(await this.lastBlock);
2783
2864
  const lastBlockRaw = await this.#makeRequest(peer, "lastBlock");
2784
2865
  console.log("raw last block response:", lastBlockRaw);
2785
- if (lastBlockRaw === void 0 || lastBlockRaw === null || typeof lastBlockRaw !== "object" && !(lastBlockRaw instanceof Uint8Array)) {
2866
+ if (lastBlockRaw === void 0 || lastBlockRaw === null) {
2786
2867
  throw new Error(`invalid lastBlock payload: ${typeof lastBlockRaw}`);
2787
2868
  }
2788
- console.log(new LastBlockMessage(lastBlockRaw));
2789
- lastBlock = new LastBlockMessage(lastBlockRaw).decoded;
2869
+ const lastBlockMessage = await this.#resolveLastBlockMessage(lastBlockRaw);
2870
+ console.log(lastBlockMessage);
2871
+ lastBlock = lastBlockMessage.decoded;
2790
2872
  } catch (error) {
2791
2873
  const peerName = peer?.peerId || peer?.id || peer?.address || peerId || "unknown";
2792
2874
  debug(`lastBlock request failed: ${peerName}:`, error?.message ?? error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.9.21",
3
+ "version": "1.9.24",
4
4
  "description": "Official javascript implementation",
5
5
  "private": false,
6
6
  "exports": {
@@ -62,19 +62,19 @@
62
62
  "tslib": "^2.8.1"
63
63
  },
64
64
  "dependencies": {
65
- "@leofcoin/addresses": "^1.0.58",
66
- "@leofcoin/codecs": "^1.1.2",
67
- "@leofcoin/contracts": "^0.1.19",
68
- "@leofcoin/crypto": "^0.2.39",
69
- "@leofcoin/errors": "^1.0.28",
70
- "@leofcoin/lib": "^1.2.77",
71
- "@leofcoin/messages": "^1.5.2",
65
+ "@leofcoin/addresses": "^1.0.59",
66
+ "@leofcoin/codecs": "^1.1.3",
67
+ "@leofcoin/contracts": "^0.1.20",
68
+ "@leofcoin/crypto": "^0.2.40",
69
+ "@leofcoin/errors": "^1.0.29",
70
+ "@leofcoin/lib": "^1.2.78",
71
+ "@leofcoin/messages": "^1.5.3",
72
72
  "@leofcoin/multi-wallet": "^3.1.9",
73
- "@leofcoin/networks": "^1.1.28",
73
+ "@leofcoin/networks": "^1.1.29",
74
74
  "@leofcoin/peernet": "^1.2.21",
75
75
  "@leofcoin/storage": "^3.5.38",
76
- "@leofcoin/utils": "^1.1.42",
77
- "@leofcoin/workers": "^1.5.30",
76
+ "@leofcoin/utils": "^1.1.43",
77
+ "@leofcoin/workers": "^1.5.31",
78
78
  "@vandeurenglenn/base58": "^1.1.9",
79
79
  "@vandeurenglenn/easy-worker": "^1.0.3",
80
80
  "semver": "^7.8.2"