@leofcoin/chain 1.5.11 → 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 +54 -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 +40 -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
package/exports/chain.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { formatBytes, BigNumber, formatUnits, parseUnits } from '@leofcoin/utils';
2
2
  import { TransactionMessage, BlockMessage, ContractMessage, BWMessage, BWRequestMessage } from '@leofcoin/messages';
3
3
  import addresses, { contractFactory, nativeToken, validators, nameService } from '@leofcoin/addresses';
4
- import { calculateFee, createContractMessage, contractFactoryMessage, nativeTokenMessage, validatorsMessage, nameServiceMessage, signTransaction } from '@leofcoin/lib';
4
+ import { calculateFee, createContractMessage, signTransaction, contractFactoryMessage, nativeTokenMessage, validatorsMessage, nameServiceMessage } from '@leofcoin/lib';
5
5
  import { randombytes } from '@leofcoin/crypto';
6
6
  import EasyWorker from '@vandeurenglenn/easy-worker';
7
7
  import { isResolveError, ResolveError } from '@leofcoin/errors';
@@ -83,7 +83,9 @@ class Transaction extends Protocol {
83
83
  transactions = await this.promiseTransactions(transactions);
84
84
  transactions = transactions.filter(tx => tx.decoded.from === address);
85
85
  transactions = await this.promiseTransactionsContent(transactions);
86
+ // @ts-ignore
86
87
  if (this.lastBlock?.hash && transactions.length === 0 && this.lastBlock.hash !== '0x0') {
88
+ // @ts-ignore
87
89
  let block = await peernet.get(this.lastBlock.hash, 'block');
88
90
  block = await new BlockMessage(block);
89
91
  // for (let tx of block.decoded?.transactions) {
@@ -147,6 +149,9 @@ class Transaction extends Protocol {
147
149
  return true;
148
150
  return false;
149
151
  }
152
+ async createTransactionMessage(transaction, signature) {
153
+ return new TransactionMessage({ ...transaction, signature });
154
+ }
150
155
  async createTransaction(transaction) {
151
156
  return {
152
157
  from: transaction.from,
@@ -239,17 +244,25 @@ class Contract extends Transaction {
239
244
  * @param {Array} parameters
240
245
  * @returns
241
246
  */
242
- async deployContract(contract, constructorParameters = []) {
243
- const message = await createContractMessage(peernet.selectedAccount, contract, constructorParameters);
247
+ async deployContract(signer, contract, constructorParameters = []) {
248
+ const message = await createContractMessage(await signer.address, contract, constructorParameters);
249
+ return this.deployContractMessage(signer, message);
250
+ }
251
+ async deployContractMessage(signer, message) {
244
252
  try {
245
- await contractStore.put(await message.hash(), message.encoded);
253
+ await globalThis.contractStore.put(await message.hash(), message.encoded);
246
254
  }
247
255
  catch (error) {
248
256
  throw error;
249
257
  }
250
- return this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'registerContract', [await message.hash()]);
251
- }
252
- async deployContractMessage(message) {
258
+ let transaction = {
259
+ from: await signer.address,
260
+ to: addresses.contractFactory,
261
+ method: 'registerContract',
262
+ params: [await message.hash()]
263
+ };
264
+ transaction = await signTransaction(await this.createTransaction(transaction), signer);
265
+ return this.sendTransaction(transaction);
253
266
  }
254
267
  }
255
268
 
@@ -264,8 +277,7 @@ class Machine {
264
277
  // @ts-ignore
265
278
  return this.#init(blocks);
266
279
  }
267
- // @ts-ignore
268
- #createMessage(sender = globalThis.peernet.selectedAccount) {
280
+ #createMessage(sender = peernet.selectedAccount) {
269
281
  return {
270
282
  sender,
271
283
  call: this.execute,
@@ -467,7 +479,7 @@ class Machine {
467
479
  * @returns Promise
468
480
  */
469
481
  async deleteAll() {
470
- let hashes = await globalThis.contractStore.get();
482
+ let hashes = await globalThis.contractStore.keys();
471
483
  hashes = Object.keys(hashes).map(hash => this.delete(hash));
472
484
  return Promise.all(hashes);
473
485
  }
@@ -687,6 +699,7 @@ class State extends Contract {
687
699
  return new globalThis.peernet.protos['peernet-response']({ response: { blocks: this.#blocks.map((block) => block.hash) } });
688
700
  }
689
701
  getLatestBlock() {
702
+ // @ts-ignore
690
703
  return this.#getLatestBlock();
691
704
  }
692
705
  async getAndPutBlock(hash) {
@@ -697,7 +710,7 @@ class State extends Contract {
697
710
  const { index } = block.decoded;
698
711
  if (this.#blocks[index - 1] && this.#blocks[index - 1].hash !== block.hash)
699
712
  throw `invalid block ${hash} @${index}`;
700
- if (!await globalThis.peernet.has(hash, 'block'))
713
+ if (!await globalThis.peernet.has(hash))
701
714
  await globalThis.peernet.put(hash, block.encoded, 'block');
702
715
  }
703
716
  return block;
@@ -840,7 +853,7 @@ class State extends Contract {
840
853
  if (this.knownBlocks?.length === Number(lastBlock.index) + 1) {
841
854
  let promises = [];
842
855
  promises = await Promise.allSettled(this.knownBlocks.map(async (address) => {
843
- const has = await globalThis.peernet.has(address, 'block');
856
+ const has = await globalThis.peernet.has(address);
844
857
  return { has, address };
845
858
  }));
846
859
  promises = promises.filter(({ status, value }) => status === 'fulfilled' && !value.has);
@@ -870,6 +883,7 @@ class State extends Contract {
870
883
  let data = await new globalThis.peernet.protos['peernet-request']({ request: 'lastBlock' });
871
884
  let node = await globalThis.peernet.prepareMessage(data);
872
885
  for (const peer of globalThis.peernet?.connections) {
886
+ // @ts-ignore
873
887
  if (peer.connected && peer.version === this.version) {
874
888
  const task = async () => {
875
889
  try {
@@ -883,6 +897,7 @@ class State extends Contract {
883
897
  promises.push(task());
884
898
  }
885
899
  }
900
+ // @ts-ignore
886
901
  promises = await this.promiseRequests(promises);
887
902
  let latest = { index: 0, hash: '0x0', previousHash: '0x0' };
888
903
  promises = promises.sort((a, b) => b.index - a.index);
@@ -953,6 +968,7 @@ class State extends Contract {
953
968
  }
954
969
  }
955
970
  this.#blocks[block.index - 1].loaded = true;
971
+ // @ts-ignore
956
972
  globalThis.debug(`loaded block: ${block.hash} @${block.index}`);
957
973
  globalThis.pubsub.publish('block-loaded', { ...block });
958
974
  }
@@ -1000,7 +1016,7 @@ class State extends Contract {
1000
1016
  }
1001
1017
 
1002
1018
  globalThis.BigNumber = BigNumber;
1003
- const ignorelist = ['BA5XUACBBBAT653LT3GHP2Z5SUHVCA42BP6IBFBJACHOZIHHR4DUPG2XMB', 'BA5XUACK6K5XA5P4BHRZ4SZT6FCLO6GLGCLUAD62WBPVLFK73RHZZUFLEG'];
1019
+ const ignorelist = [];
1004
1020
  // check if browser or local
1005
1021
  class Chain extends State {
1006
1022
  #state;
@@ -1074,7 +1090,9 @@ class Chain extends State {
1074
1090
  message: nameServiceMessage
1075
1091
  }];
1076
1092
  await Promise.all(contracts.map(async ({ address, message }) => {
1093
+ // @ts-ignore
1077
1094
  message = await new ContractMessage(Uint8Array.from(message.split(',').map(string => Number(string))));
1095
+ // @ts-ignore
1078
1096
  await globalThis.contractStore.put(address, message.encoded);
1079
1097
  }));
1080
1098
  console.log('handle native contracts');
@@ -1206,20 +1224,24 @@ class Chain extends State {
1206
1224
  async #addBlock(block) {
1207
1225
  const blockMessage = await new BlockMessage(block);
1208
1226
  await Promise.all(blockMessage.decoded.transactions
1209
- .map(async (transaction) => globalThis.transactionPoolStore.delete(transaction.hash)));
1227
+ // @ts-ignore
1228
+ .map(async (transaction) => transactionPoolStore.delete(transaction.hash)));
1210
1229
  const hash = await blockMessage.hash();
1211
1230
  await globalThis.blockStore.put(hash, blockMessage.encoded);
1212
- if (this.lastBlock.index < blockMessage.decoded.index)
1231
+ if (this.lastBlock.index < Number(blockMessage.decoded.index))
1213
1232
  await this.updateState(blockMessage);
1214
1233
  globalThis.debug(`added block: ${hash}`);
1215
1234
  let promises = [];
1216
1235
  let contracts = [];
1217
1236
  for (let transaction of blockMessage.decoded.transactions) {
1218
1237
  // await transactionStore.put(transaction.hash, transaction.encoded)
1238
+ // @ts-ignore
1219
1239
  const index = contracts.indexOf(transaction.to);
1240
+ // @ts-ignore
1220
1241
  if (index === -1)
1221
1242
  contracts.push(transaction.to);
1222
- // Todo: go trough all accounts
1243
+ // Todo: go trough all accounts
1244
+ // @ts-ignore
1223
1245
  promises.push(this.#executeTransaction(transaction));
1224
1246
  }
1225
1247
  try {
@@ -1345,7 +1367,7 @@ class Chain extends State {
1345
1367
  const peer = peers[validator];
1346
1368
  if (peer && peer.connected && peer.version === this.version) {
1347
1369
  let data = await new BWRequestMessage();
1348
- const node = await globalThis.peernet.prepareMessage(validator, data.encoded);
1370
+ const node = await globalThis.peernet.prepareMessage(data.encoded);
1349
1371
  try {
1350
1372
  const bw = await peer.request(node.encoded);
1351
1373
  block.validators.push({
@@ -1450,9 +1472,7 @@ class Chain extends State {
1450
1472
  return {
1451
1473
  sender,
1452
1474
  call: this.call,
1453
- staticCall: this.staticCall,
1454
- delegate: this.delegate,
1455
- staticDelegate: this.staticDelegate
1475
+ staticCall: this.staticCall
1456
1476
  };
1457
1477
  }
1458
1478
  /**
@@ -1482,14 +1502,6 @@ class Chain extends State {
1482
1502
  globalThis.msg = this.#createMessage();
1483
1503
  return this.machine.get(contract, method, parameters);
1484
1504
  }
1485
- delegate(contract, method, parameters) {
1486
- globalThis.msg = this.#createMessage();
1487
- return this.machine.execute(contract, method, parameters);
1488
- }
1489
- staticDelegate(contract, method, parameters) {
1490
- globalThis.msg = this.#createMessage();
1491
- return this.machine.get(contract, method, parameters);
1492
- }
1493
1505
  mint(to, amount) {
1494
1506
  return this.call(addresses.nativeToken, 'mint', [to, amount]);
1495
1507
  }
@@ -0,0 +1,45 @@
1
+ import Transaction from "./transaction.js";
2
+ import type MultiWallet from '@leofcoin/multi-wallet';
3
+ /**
4
+ * @extends {Transaction}
5
+ */
6
+ export default class Contract extends Transaction {
7
+ constructor();
8
+ init(): Promise<void>;
9
+ /**
10
+ *
11
+ * @param {Address} creator
12
+ * @param {String} contract
13
+ * @param {Array} constructorParameters
14
+ * @returns lib.createContractMessage
15
+ */
16
+ createContractMessage(creator: any, contract: any, constructorParameters?: any[]): Promise<import("@leofcoin/messages").ContractMessage>;
17
+ /**
18
+ *
19
+ * @param {Address} creator
20
+ * @param {String} contract
21
+ * @param {Array} constructorParameters
22
+ * @returns {Address}
23
+ */
24
+ createContractAddress(creator: any, contract: any, constructorParameters?: any[]): Promise<any>;
25
+ /**
26
+ *
27
+ * @param {String} contract
28
+ * @param {Array} parameters
29
+ * @returns
30
+ */
31
+ deployContract(signer: MultiWallet, contract: any, constructorParameters?: any[]): Promise<{
32
+ hash: any;
33
+ data: any;
34
+ fee: string | 0 | import("@ethersproject/bignumber").BigNumber;
35
+ wait: Promise<unknown>;
36
+ message: any;
37
+ }>;
38
+ deployContractMessage(signer: any, message: any): Promise<{
39
+ hash: any;
40
+ data: any;
41
+ fee: string | 0 | import("@ethersproject/bignumber").BigNumber;
42
+ wait: Promise<unknown>;
43
+ message: any;
44
+ }>;
45
+ }
@@ -15,10 +15,10 @@ export default class Machine {
15
15
  * @param {Array} parameters
16
16
  * @returns Promise<message>
17
17
  */
18
- execute(contract: any, method: any, parameters: any): Promise<unknown>;
19
- get(contract: any, method: any, parameters?: any): Promise<unknown>;
18
+ execute(contract: any, method: any, parameters: any): Promise<any>;
19
+ get(contract: any, method: any, parameters?: any): Promise<any>;
20
20
  has(address: any): Promise<unknown>;
21
- delete(hash: any): Promise<any>;
21
+ delete(hash: any): Promise<void>;
22
22
  /**
23
23
  *
24
24
  * @returns Promise
package/exports/node.js CHANGED
@@ -6,6 +6,7 @@ import networks from '@leofcoin/networks';
6
6
  class Node {
7
7
  #node;
8
8
  constructor(config, password) {
9
+ // @ts-ignore
9
10
  return this._init(config, password);
10
11
  }
11
12
  async _init(config = {
@@ -27,8 +27,8 @@ export default class State extends Contract {
27
27
  constructor();
28
28
  init(): Promise<void>;
29
29
  updateState(message: any): Promise<void>;
30
- getLatestBlock(): Promise<BlockMessage.decoded>;
31
- getAndPutBlock(hash: string): BlockMessage;
30
+ getLatestBlock(): Promise<BlockMessage['decoded']>;
31
+ getAndPutBlock(hash: string): Promise<BlockMessage>;
32
32
  resolveBlock(hash: any): any;
33
33
  resolveBlocks(): Promise<void>;
34
34
  restoreChain(): Promise<void>;
@@ -29,6 +29,7 @@ export default class Transaction extends Protocol {
29
29
  getNonce(address: any): Promise<number>;
30
30
  validateNonce(address: any, nonce: any): Promise<void>;
31
31
  isTransactionMessage(message: any): boolean;
32
+ createTransactionMessage(transaction: any, signature: any): Promise<TransactionMessage>;
32
33
  createTransaction(transaction: any): Promise<{
33
34
  from: any;
34
35
  to: any;
@@ -40,7 +41,7 @@ export default class Transaction extends Protocol {
40
41
  sendTransaction(message: any): Promise<{
41
42
  hash: any;
42
43
  data: any;
43
- fee: any;
44
+ fee: string | 0 | import("@leofcoin/utils").BigNumber;
44
45
  wait: Promise<unknown>;
45
46
  message: any;
46
47
  }>;
@@ -1,5 +1,5 @@
1
1
  declare namespace _default {
2
- export function isType(type: any, value: any): any;
2
+ export function isType(type: any, value: any): boolean;
3
3
  export { isAddress };
4
4
  }
5
5
  export default _default;