@leofcoin/chain 1.5.10 → 1.5.12

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.
Files changed (34) hide show
  1. package/exports/browser/browser-store.js +4 -3
  2. package/exports/browser/chain.js +55 -45
  3. package/exports/browser/{index-9c85cd32-ab3cf058.js → index-329e0324-67daaccb.js} +2 -2
  4. package/exports/browser/{index-b32f624f.js → index-e8f03811.js} +155 -144
  5. package/exports/browser/{messages-b66f5393-8ba2fe1a.js → messages-000b7f84-fd07a3bf.js} +2 -2
  6. package/exports/browser/{node-browser-3364f1f2.js → node-browser-505084de.js} +230 -252
  7. package/exports/browser/node-browser.js +2 -2
  8. package/exports/browser/workers/block-worker.js +159 -148
  9. package/exports/browser/workers/machine-worker.js +159 -148
  10. package/exports/chain.d.ts +72 -0
  11. package/exports/chain.js +41 -28
  12. package/exports/contract.d.ts +45 -0
  13. package/exports/{types/machine.d.ts → machine.d.ts} +3 -3
  14. package/exports/node.js +1 -0
  15. package/exports/{types/state.d.ts → state.d.ts} +2 -2
  16. package/exports/{types/transaction.d.ts → transaction.d.ts} +2 -1
  17. package/exports/{types/typer.d.ts → typer.d.ts} +1 -1
  18. package/exports/workers/block-worker.js +159 -148
  19. package/exports/workers/machine-worker.js +159 -148
  20. package/package.json +18 -3
  21. package/exports/types/contract.d.ts +0 -31
  22. /package/exports/{types/config → config}/config.d.ts +0 -0
  23. /package/exports/{types/config → config}/main.d.ts +0 -0
  24. /package/exports/{types/config → config}/protocol.d.ts +0 -0
  25. /package/exports/{types/consensus → consensus}/consensus.d.ts +0 -0
  26. /package/exports/{types/consensus → consensus}/helpers/sort-transactions.d.ts +0 -0
  27. /package/exports/{types/fee → fee}/config.d.ts +0 -0
  28. /package/exports/{types/jobs → jobs}/jobber.d.ts +0 -0
  29. /package/exports/{types/machine-state.d.ts → machine-state.d.ts} +0 -0
  30. /package/exports/{types/node-browser.d.ts → node-browser.d.ts} +0 -0
  31. /package/exports/{types/node.d.ts → node.d.ts} +0 -0
  32. /package/exports/{types/protocol.d.ts → protocol.d.ts} +0 -0
  33. /package/exports/{types/sync-controller.d.ts → sync-controller.d.ts} +0 -0
  34. /package/exports/{types/types.d.ts → types.d.ts} +0 -0
@@ -401,13 +401,14 @@ class BrowerStore {
401
401
  });
402
402
  }
403
403
  toKeyPath(key) {
404
- if (!key.isKeyPath())
404
+ if (key instanceof KeyPath)
405
405
  key = new KeyPath(key);
406
- return key.toString('base32');
406
+ return key.toString();
407
407
  }
408
408
  toKeyValue(value) {
409
- if (!value.isKeyValue())
409
+ if (value instanceof KeyValue)
410
410
  value = new KeyValue(value);
411
+ // @ts-ignore
411
412
  return value.uint8Array;
412
413
  }
413
414
  async get(key) {
@@ -1,4 +1,4 @@
1
- import { B as BigNumber, L as Logger, v as version$1, h as hexZeroPad, i as isBigNumberish, a as arrayify, b as isBytes, C as ContractMessage, T as TransactionMessage, t as toBase58, R as RawTransactionMessage, c as BlockMessage, d as BWMessage, e as BWRequestMessage } from './index-b32f624f.js';
1
+ import { B as BigNumber, L as Logger, v as version$1, h as hexZeroPad, i as isBigNumberish, a as arrayify, b as isBytes, C as ContractMessage, T as TransactionMessage, t as toBase58, R as RawTransactionMessage, c as BlockMessage, d as BWMessage, e as BWRequestMessage } from './index-e8f03811.js';
2
2
 
3
3
  const logger$1 = new Logger(version$1);
4
4
  const _constructorGuard = {};
@@ -389,15 +389,14 @@ function parseUnits(value, unitName) {
389
389
  }
390
390
 
391
391
  const byteFormats = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
392
-
393
392
  const formatBytes = (bytes, decimals = 2) => {
394
- if (bytes === 0) return '0 Bytes';
395
- if (decimals < 0) decimals = 0;
396
-
397
- const k = 1024;
398
- const i = Math.floor(Math.log(bytes) / Math.log(k));
399
-
400
- return `${parseFloat((bytes / Math.pow(k, i)).toFixed(decimals))} ${byteFormats[i]}`
393
+ if (bytes === 0)
394
+ return '0 Bytes';
395
+ if (decimals < 0)
396
+ decimals = 0;
397
+ const k = 1024;
398
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
399
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(decimals))} ${byteFormats[i]}`;
401
400
  };
402
401
 
403
402
  var contractFactory$1 = "IHNY2GQHKQLQTSDQ5CRDWDDILC34U5LGP4ONJESURDTOZ3NZWJZ3KEUGCVU";
@@ -457,18 +456,16 @@ const calculateFee = async (transaction, format = false) => {
457
456
  };
458
457
  const createTransactionHash = async (transaction) => {
459
458
  const isRawTransactionMessage = transaction instanceof RawTransactionMessage;
460
- if (!isRawTransactionMessage) {
461
- if (transaction.decoded && transaction instanceof TransactionMessage)
462
- transaction = await new RawTransactionMessage(transaction.decoded);
463
- else
464
- transaction = await new RawTransactionMessage(transaction);
465
- }
466
- return (await transaction.peernetHash).digest;
459
+ let message;
460
+ if (!isRawTransactionMessage)
461
+ message = await new RawTransactionMessage(transaction instanceof TransactionMessage ? transaction.decoded : transaction);
462
+ else
463
+ message = transaction;
464
+ return (await message.peernetHash).digest;
467
465
  };
468
466
  const signTransaction = async (transaction, wallet) => {
469
467
  const signature = toBase58(await wallet.sign(await createTransactionHash(transaction)));
470
- const signedTransaction = { ...transaction, signature };
471
- return signedTransaction;
468
+ return { ...transaction, signature };
472
469
  };
473
470
 
474
471
  const limit = 1800;
@@ -548,7 +545,9 @@ class Transaction extends Protocol {
548
545
  transactions = await this.promiseTransactions(transactions);
549
546
  transactions = transactions.filter(tx => tx.decoded.from === address);
550
547
  transactions = await this.promiseTransactionsContent(transactions);
548
+ // @ts-ignore
551
549
  if (this.lastBlock?.hash && transactions.length === 0 && this.lastBlock.hash !== '0x0') {
550
+ // @ts-ignore
552
551
  let block = await peernet.get(this.lastBlock.hash, 'block');
553
552
  block = await new BlockMessage(block);
554
553
  // for (let tx of block.decoded?.transactions) {
@@ -612,6 +611,9 @@ class Transaction extends Protocol {
612
611
  return true;
613
612
  return false;
614
613
  }
614
+ async createTransactionMessage(transaction, signature) {
615
+ return new TransactionMessage({ ...transaction, signature });
616
+ }
615
617
  async createTransaction(transaction) {
616
618
  return {
617
619
  from: transaction.from,
@@ -704,17 +706,25 @@ class Contract extends Transaction {
704
706
  * @param {Array} parameters
705
707
  * @returns
706
708
  */
707
- async deployContract(contract, constructorParameters = []) {
708
- const message = await createContractMessage(peernet.selectedAccount, contract, constructorParameters);
709
+ async deployContract(signer, contract, constructorParameters = []) {
710
+ const message = await createContractMessage(await signer.address, contract, constructorParameters);
711
+ return this.deployContractMessage(signer, message);
712
+ }
713
+ async deployContractMessage(signer, message) {
709
714
  try {
710
- await contractStore.put(await message.hash(), message.encoded);
715
+ await globalThis.contractStore.put(await message.hash(), message.encoded);
711
716
  }
712
717
  catch (error) {
713
718
  throw error;
714
719
  }
715
- return this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'registerContract', [await message.hash()]);
716
- }
717
- async deployContractMessage(message) {
720
+ let transaction = {
721
+ from: await signer.address,
722
+ to: addresses.contractFactory,
723
+ method: 'registerContract',
724
+ params: [await message.hash()]
725
+ };
726
+ transaction = await signTransaction(await this.createTransaction(transaction), signer);
727
+ return this.sendTransaction(transaction);
718
728
  }
719
729
  }
720
730
 
@@ -809,8 +819,7 @@ class Machine {
809
819
  // @ts-ignore
810
820
  return this.#init(blocks);
811
821
  }
812
- // @ts-ignore
813
- #createMessage(sender = globalThis.peernet.selectedAccount) {
822
+ #createMessage(sender = peernet.selectedAccount) {
814
823
  return {
815
824
  sender,
816
825
  call: this.execute,
@@ -1012,7 +1021,7 @@ class Machine {
1012
1021
  * @returns Promise
1013
1022
  */
1014
1023
  async deleteAll() {
1015
- let hashes = await globalThis.contractStore.get();
1024
+ let hashes = await globalThis.contractStore.keys();
1016
1025
  hashes = Object.keys(hashes).map(hash => this.delete(hash));
1017
1026
  return Promise.all(hashes);
1018
1027
  }
@@ -1238,6 +1247,7 @@ class State extends Contract {
1238
1247
  this.#lastBlock = { hash, ...message.decoded };
1239
1248
  // await this.state.updateState(message)
1240
1249
  await globalThis.chainStore.put('lastBlock', hash);
1250
+ globalThis.pubsub.publish('lastBlock', this.#lastBlock);
1241
1251
  }
1242
1252
  async #lastBlockHandler() {
1243
1253
  return new globalThis.peernet.protos['peernet-response']({ response: { hash: this.#lastBlock?.hash, index: this.#lastBlock?.index } });
@@ -1246,6 +1256,7 @@ class State extends Contract {
1246
1256
  return new globalThis.peernet.protos['peernet-response']({ response: { blocks: this.#blocks.map((block) => block.hash) } });
1247
1257
  }
1248
1258
  getLatestBlock() {
1259
+ // @ts-ignore
1249
1260
  return this.#getLatestBlock();
1250
1261
  }
1251
1262
  async getAndPutBlock(hash) {
@@ -1256,7 +1267,7 @@ class State extends Contract {
1256
1267
  const { index } = block.decoded;
1257
1268
  if (this.#blocks[index - 1] && this.#blocks[index - 1].hash !== block.hash)
1258
1269
  throw `invalid block ${hash} @${index}`;
1259
- if (!await globalThis.peernet.has(hash, 'block'))
1270
+ if (!await globalThis.peernet.has(hash))
1260
1271
  await globalThis.peernet.put(hash, block.encoded, 'block');
1261
1272
  }
1262
1273
  return block;
@@ -1399,7 +1410,7 @@ class State extends Contract {
1399
1410
  if (this.knownBlocks?.length === Number(lastBlock.index) + 1) {
1400
1411
  let promises = [];
1401
1412
  promises = await Promise.allSettled(this.knownBlocks.map(async (address) => {
1402
- const has = await globalThis.peernet.has(address, 'block');
1413
+ const has = await globalThis.peernet.has(address);
1403
1414
  return { has, address };
1404
1415
  }));
1405
1416
  promises = promises.filter(({ status, value }) => status === 'fulfilled' && !value.has);
@@ -1429,6 +1440,7 @@ class State extends Contract {
1429
1440
  let data = await new globalThis.peernet.protos['peernet-request']({ request: 'lastBlock' });
1430
1441
  let node = await globalThis.peernet.prepareMessage(data);
1431
1442
  for (const peer of globalThis.peernet?.connections) {
1443
+ // @ts-ignore
1432
1444
  if (peer.connected && peer.version === this.version) {
1433
1445
  const task = async () => {
1434
1446
  try {
@@ -1442,6 +1454,7 @@ class State extends Contract {
1442
1454
  promises.push(task());
1443
1455
  }
1444
1456
  }
1457
+ // @ts-ignore
1445
1458
  promises = await this.promiseRequests(promises);
1446
1459
  let latest = { index: 0, hash: '0x0', previousHash: '0x0' };
1447
1460
  promises = promises.sort((a, b) => b.index - a.index);
@@ -1512,6 +1525,7 @@ class State extends Contract {
1512
1525
  }
1513
1526
  }
1514
1527
  this.#blocks[block.index - 1].loaded = true;
1528
+ // @ts-ignore
1515
1529
  globalThis.debug(`loaded block: ${block.hash} @${block.index}`);
1516
1530
  globalThis.pubsub.publish('block-loaded', { ...block });
1517
1531
  }
@@ -1559,7 +1573,7 @@ class State extends Contract {
1559
1573
  }
1560
1574
 
1561
1575
  globalThis.BigNumber = BigNumber;
1562
- const ignorelist = ['BA5XUACBBBAT653LT3GHP2Z5SUHVCA42BP6IBFBJACHOZIHHR4DUPG2XMB', 'BA5XUACK6K5XA5P4BHRZ4SZT6FCLO6GLGCLUAD62WBPVLFK73RHZZUFLEG'];
1576
+ const ignorelist = [];
1563
1577
  // check if browser or local
1564
1578
  class Chain extends State {
1565
1579
  #state;
@@ -1633,7 +1647,9 @@ class Chain extends State {
1633
1647
  message: nameServiceMessage
1634
1648
  }];
1635
1649
  await Promise.all(contracts.map(async ({ address, message }) => {
1650
+ // @ts-ignore
1636
1651
  message = await new ContractMessage(Uint8Array.from(message.split(',').map(string => Number(string))));
1652
+ // @ts-ignore
1637
1653
  await globalThis.contractStore.put(address, message.encoded);
1638
1654
  }));
1639
1655
  console.log('handle native contracts');
@@ -1765,20 +1781,24 @@ class Chain extends State {
1765
1781
  async #addBlock(block) {
1766
1782
  const blockMessage = await new BlockMessage(block);
1767
1783
  await Promise.all(blockMessage.decoded.transactions
1768
- .map(async (transaction) => globalThis.transactionPoolStore.delete(transaction.hash)));
1784
+ // @ts-ignore
1785
+ .map(async (transaction) => transactionPoolStore.delete(transaction.hash)));
1769
1786
  const hash = await blockMessage.hash();
1770
1787
  await globalThis.blockStore.put(hash, blockMessage.encoded);
1771
- if (this.lastBlock.index < blockMessage.decoded.index)
1788
+ if (this.lastBlock.index < Number(blockMessage.decoded.index))
1772
1789
  await this.updateState(blockMessage);
1773
1790
  globalThis.debug(`added block: ${hash}`);
1774
1791
  let promises = [];
1775
1792
  let contracts = [];
1776
1793
  for (let transaction of blockMessage.decoded.transactions) {
1777
1794
  // await transactionStore.put(transaction.hash, transaction.encoded)
1795
+ // @ts-ignore
1778
1796
  const index = contracts.indexOf(transaction.to);
1797
+ // @ts-ignore
1779
1798
  if (index === -1)
1780
1799
  contracts.push(transaction.to);
1781
- // Todo: go trough all accounts
1800
+ // Todo: go trough all accounts
1801
+ // @ts-ignore
1782
1802
  promises.push(this.#executeTransaction(transaction));
1783
1803
  }
1784
1804
  try {
@@ -1904,7 +1924,7 @@ class Chain extends State {
1904
1924
  const peer = peers[validator];
1905
1925
  if (peer && peer.connected && peer.version === this.version) {
1906
1926
  let data = await new BWRequestMessage();
1907
- const node = await globalThis.peernet.prepareMessage(validator, data.encoded);
1927
+ const node = await globalThis.peernet.prepareMessage(data.encoded);
1908
1928
  try {
1909
1929
  const bw = await peer.request(node.encoded);
1910
1930
  block.validators.push({
@@ -2009,9 +2029,7 @@ class Chain extends State {
2009
2029
  return {
2010
2030
  sender,
2011
2031
  call: this.call,
2012
- staticCall: this.staticCall,
2013
- delegate: this.delegate,
2014
- staticDelegate: this.staticDelegate
2032
+ staticCall: this.staticCall
2015
2033
  };
2016
2034
  }
2017
2035
  /**
@@ -2041,14 +2059,6 @@ class Chain extends State {
2041
2059
  globalThis.msg = this.#createMessage();
2042
2060
  return this.machine.get(contract, method, parameters);
2043
2061
  }
2044
- delegate(contract, method, parameters) {
2045
- globalThis.msg = this.#createMessage();
2046
- return this.machine.execute(contract, method, parameters);
2047
- }
2048
- staticDelegate(contract, method, parameters) {
2049
- globalThis.msg = this.#createMessage();
2050
- return this.machine.get(contract, method, parameters);
2051
- }
2052
2062
  mint(to, amount) {
2053
2063
  return this.call(addresses.nativeToken, 'mint', [to, amount]);
2054
2064
  }
@@ -1,5 +1,5 @@
1
- import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-3364f1f2.js';
2
- import './index-b32f624f.js';
1
+ import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-505084de.js';
2
+ import './index-e8f03811.js';
3
3
 
4
4
  /**
5
5
  * @params {String} network