@leofcoin/chain 1.0.22 → 1.0.23

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 (46) hide show
  1. package/dist/chain.browser.js +26563 -9162
  2. package/dist/chain.js +479 -118
  3. package/dist/node.browser.js +3603 -3912
  4. package/dist/node.js +1 -1
  5. package/dist/protons.browser.js +7818 -175
  6. package/dist/wrtc.browser.js +1 -1
  7. package/docs/@leofcoin/chain/1.0.22/chain.js.html +377 -223
  8. package/docs/@leofcoin/chain/1.0.22/contracts_factory.js.html +123 -123
  9. package/docs/@leofcoin/chain/1.0.22/contracts_nameService.js.html +123 -123
  10. package/docs/@leofcoin/chain/1.0.22/contracts_proxies_factoryProxy.js.html +123 -123
  11. package/docs/@leofcoin/chain/1.0.22/contracts_proxies_nameServiceProxy.js.html +123 -123
  12. package/docs/@leofcoin/chain/1.0.22/contracts_proxies_nativeTokenProxy.js.html +123 -123
  13. package/docs/@leofcoin/chain/1.0.22/contracts_proxies_validatorsProxy.js.html +123 -123
  14. package/docs/@leofcoin/chain/1.0.22/contracts_proxies_votingProxy.js.html +123 -123
  15. package/docs/@leofcoin/chain/1.0.22/contracts_validators.js.html +123 -123
  16. package/docs/@leofcoin/chain/1.0.22/fonts/OpenSans-Bold-webfont.svg +1829 -1829
  17. package/docs/@leofcoin/chain/1.0.22/fonts/OpenSans-BoldItalic-webfont.svg +1829 -1829
  18. package/docs/@leofcoin/chain/1.0.22/fonts/OpenSans-Italic-webfont.svg +1829 -1829
  19. package/docs/@leofcoin/chain/1.0.22/fonts/OpenSans-Light-webfont.svg +1830 -1830
  20. package/docs/@leofcoin/chain/1.0.22/fonts/OpenSans-LightItalic-webfont.svg +1834 -1834
  21. package/docs/@leofcoin/chain/1.0.22/fonts/OpenSans-Regular-webfont.svg +1830 -1830
  22. package/docs/@leofcoin/chain/1.0.22/fonts/OpenSans-Semibold-webfont.svg +1829 -1829
  23. package/docs/@leofcoin/chain/1.0.22/fonts/OpenSans-SemiboldItalic-webfont.svg +1829 -1829
  24. package/docs/@leofcoin/chain/1.0.22/global.html +1804 -1152
  25. package/docs/@leofcoin/chain/1.0.22/icons/home.svg +3 -3
  26. package/docs/@leofcoin/chain/1.0.22/icons/search.svg +3 -3
  27. package/docs/@leofcoin/chain/1.0.22/index.html +124 -124
  28. package/docs/@leofcoin/chain/1.0.22/machine.js.html +133 -129
  29. package/docs/@leofcoin/chain/1.0.22/scripts/linenumber.js +23 -23
  30. package/docs/@leofcoin/chain/1.0.22/scripts/pagelocation.js +89 -89
  31. package/docs/@leofcoin/chain/1.0.22/standards_roles.js.html +123 -123
  32. package/docs/@leofcoin/chain/1.0.22/standards_token.js.html +123 -123
  33. package/docs/@leofcoin/chain/1.0.22/styles/collapse.css +27 -27
  34. package/docs/@leofcoin/chain/1.0.22/styles/jsdoc-default.css +953 -953
  35. package/docs/@leofcoin/chain/1.0.22/styles/prettify-jsdoc.css +111 -111
  36. package/docs/@leofcoin/chain/1.0.22/styles/prettify-tomorrow.css +138 -138
  37. package/package.json +8 -5
  38. package/rollup.config.js +1 -2
  39. package/src/chain.js +254 -100
  40. package/src/config/config.js +6 -2
  41. package/src/machine.js +10 -6
  42. package/src/node.js +2 -34
  43. package/src/state.js +8 -3
  44. package/src/transactions/transaction.js +3 -0
  45. package/test/chain.js +39 -60
  46. package/webpack.config.js +3 -3
package/dist/chain.js CHANGED
@@ -2,8 +2,180 @@
2
2
 
3
3
  var utils = require('@leofcoin/utils');
4
4
  require('vm');
5
+ var codecFormatInterface = require('@leofcoin/codec-format-interface');
6
+ require('@ethersproject/bignumber');
7
+ require('@ethersproject/units');
5
8
  var messages = require('@leofcoin/messages');
6
- var lib$1 = require('@leofcoin/lib');
9
+ var addresses$1 = require('@leofcoin/addresses');
10
+ var MultiWallet = require('@leofcoin/multi-wallet');
11
+ var index = require('@leofcoin/codec-format-interface/dist/index');
12
+ var bs32 = require('@vandeurenglenn/base32');
13
+ var util = require('util');
14
+ var fs = require('fs');
15
+
16
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
17
+
18
+ var MultiWallet__default = /*#__PURE__*/_interopDefaultLegacy(MultiWallet);
19
+ var bs32__default = /*#__PURE__*/_interopDefaultLegacy(bs32);
20
+
21
+ var proto$4 = `
22
+ message ContractMessage {
23
+ required string creator = 1;
24
+ required bytes contract = 2;
25
+ repeated string constructorParameters = 3;
26
+ }
27
+ `;
28
+
29
+ class ContractMessage extends codecFormatInterface.FormatInterface {
30
+ get keys() {
31
+ return ['creator', 'contract', 'constructorParameters']
32
+ }
33
+
34
+ get messageName() {
35
+ return 'ContractMessage'
36
+ }
37
+
38
+ constructor(buffer) {
39
+ super(buffer, proto$4, {name: 'contract-message'});
40
+ }
41
+ }
42
+
43
+ var proto$3 = `
44
+
45
+ message TransactionMessage {
46
+ required uint64 timestamp = 1;
47
+ required string from = 2;
48
+ required string to = 3;
49
+ required uint64 nonce = 4;
50
+ required string method = 5;
51
+ repeated string params = 6;
52
+ required string signature = 7;
53
+ }
54
+ `;
55
+
56
+ class TransactionMessage extends codecFormatInterface.FormatInterface {
57
+ get keys() {
58
+ return ['timestamp', 'from', 'to', 'nonce', 'method', 'params', 'signature']
59
+ }
60
+
61
+ get messageName() {
62
+ return 'TransactionMessage'
63
+ }
64
+
65
+ constructor(buffer) {
66
+ const name = 'transaction-message';
67
+ super(buffer, proto$3, {name});
68
+ }
69
+ }
70
+
71
+ var proto$2 = `
72
+ message ValidatorMessage {
73
+ required string address = 1;
74
+ required string reward = 2;
75
+ }
76
+
77
+ message Transaction {
78
+ required string hash = 1;
79
+ required uint64 timestamp = 2;
80
+ required string from = 3;
81
+ required string to = 4;
82
+ required uint64 nonce = 5;
83
+ required string method = 6;
84
+ repeated string params = 7;
85
+ }
86
+
87
+ message BlockMessage {
88
+ required uint64 index = 1;
89
+ required string previousHash = 3;
90
+ required uint64 timestamp = 4;
91
+ required uint64 reward = 5;
92
+ required string fees = 6;
93
+ repeated Transaction transactions = 7;
94
+ repeated ValidatorMessage validators = 8;
95
+ }
96
+ `;
97
+
98
+ class BlockMessage extends codecFormatInterface.FormatInterface {
99
+ get keys() {
100
+ return ['index', 'previousHash', 'timestamp', 'reward', 'fees', 'transactions', 'validators']
101
+ }
102
+
103
+ get messageName() {
104
+ return 'BlockMessage'
105
+ }
106
+
107
+ constructor(buffer) {
108
+ const name = 'block-message';
109
+ super(buffer, proto$2, {name});
110
+ }
111
+ }
112
+
113
+ var proto$1 = `
114
+
115
+ message BWMessage {
116
+ required uint64 up = 1;
117
+ required uint64 down = 2;
118
+ }
119
+ `;
120
+
121
+ class BWMessage extends codecFormatInterface.FormatInterface {
122
+ get keys() {
123
+ return ['up', 'down']
124
+ }
125
+
126
+ get messageName() {
127
+ return 'BWMessage'
128
+ }
129
+
130
+ constructor(buffer) {
131
+ const name = 'bw-message';
132
+ super(buffer, proto$1, {name});
133
+ }
134
+ }
135
+
136
+ var proto = `
137
+
138
+ message BWRequestMessage {
139
+ }
140
+ `;
141
+
142
+ class BWRequestMessage extends codecFormatInterface.FormatInterface {
143
+ get keys() {
144
+ return []
145
+ }
146
+
147
+ get messageName() {
148
+ return 'BWRequestMessage'
149
+ }
150
+
151
+ constructor(buffer) {
152
+ const name = 'bw-request-message';
153
+ super(buffer, proto, {name});
154
+ }
155
+ }
156
+
157
+ var contractFactory$1 = "ba5xdac4ybttixammtscsptfsvtqwnzuaw5p6eghn2sh7tf5dskqtdghjc";
158
+ var nativeToken$1 = "ba5xdad2t57fpdadkrumwht32uwcdubjppq6z2vkwomgowfjpzmjbn2wo2";
159
+ var nameService$1 = "ba5xdadb3f2jptnup2j6ev6usz4vs6p5vo7vst5hj7r4ddkhdxpnvebxvh";
160
+ var validators$1 = "ba5xdacicpruzh4lu6je5mfn42f6s4y7tboxosyrspxmner5wjma5bvntn";
161
+ var addresses = {
162
+ contractFactory: contractFactory$1,
163
+ nativeToken: nativeToken$1,
164
+ nameService: nameService$1,
165
+ validators: validators$1
166
+ };
167
+
168
+ const byteFormats = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
169
+
170
+ const formatBytes = (bytes, decimals = 2) => {
171
+ if (bytes === 0) return '0 Bytes';
172
+ if (decimals < 0) decimals = 0;
173
+
174
+ const k = 1024;
175
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
176
+
177
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(decimals))} ${byteFormats[i]}`
178
+ };
7
179
 
8
180
  // import State from './state'
9
181
 
@@ -26,15 +198,18 @@ class Machine {
26
198
  // return
27
199
  try {
28
200
  let contracts = [
29
- contractStore.get(lib$1.contractFactory),
30
- contractStore.get(lib$1.nativeToken),
31
- contractStore.get(lib$1.validators),
32
- contractStore.get(lib$1.nameService)
201
+ contractStore.get(contractFactory$1),
202
+ contractStore.get(nativeToken$1),
203
+ contractStore.get(validators$1),
204
+ contractStore.get(nameService$1)
33
205
  ];
34
206
 
35
207
  contracts = await Promise.all(contracts);
208
+ console.log(contracts);
36
209
  for (const contract of contracts) {
37
- const message = await new messages.ContractMessage(new Uint8Array(contract.buffer, contract.buffer.byteOffset, contract.buffer.byteLength));
210
+
211
+ const message = await new ContractMessage(new Uint8Array(contract.buffer, contract.buffer.byteOffset, contract.buffer.byteLength));
212
+ console.log(message);
38
213
  await this.#runContract(message);
39
214
  }
40
215
  } catch (e) {
@@ -55,6 +230,7 @@ console.log(e);
55
230
  }
56
231
 
57
232
  async #runContract(contractMessage) {
233
+ console.log(contractMessage);
58
234
  const params = contractMessage.decoded.constructorParameters;
59
235
  try {
60
236
 
@@ -63,16 +239,15 @@ console.log(e);
63
239
 
64
240
  globalThis.msg = this.#createMessage(contractMessage.decoded.creator);
65
241
  // globalThis.msg = {sender: contractMessage.decoded.creator}
66
- this.#contracts[contractMessage.hash] = new Contract(...params);
67
- debug(`loaded contract: ${contractMessage.hash}`);
68
- debug(`size: ${Math.round((contractMessage.encoded.length / 1024) * 100) / 100} kb`);
242
+ this.#contracts[await contractMessage.hash] = await new Contract(...params);
243
+ debug(`loaded contract: ${await contractMessage.hash}`);
244
+ debug(`size: ${formatBytes(contractMessage.encoded.length)}`);
69
245
  } catch (e) {
70
246
  console.log(e);
71
247
  console.warn(`removing contract ${contractMessage.hash}`);
72
248
  await contractStore.delete(contractMessage.hash, contractMessage.encoded);
73
249
  }
74
250
  }
75
-
76
251
  /**
77
252
  * @params {ContractMessage} - contractMessage
78
253
  */
@@ -131,6 +306,45 @@ console.log(e);
131
306
  }
132
307
  }
133
308
 
309
+ var contractFactory = "237,198,1,10,54,51,67,122,86,50,109,72,55,122,115,71,66,119,54,122,122,71,99,53,69,102,111,109,116,76,78,114,116,75,78,51,103,78,90,77,98,69,99,112,89,118,83,82,75,115,52,106,85,90,68,116,118,71,121,18,146,8,114,101,116,117,114,110,32,99,108,97,115,115,32,70,97,99,116,111,114,121,123,35,110,97,109,101,61,34,65,114,116,79,110,108,105,110,101,67,111,110,116,114,97,99,116,70,97,99,116,111,114,121,34,59,35,116,111,116,97,108,67,111,110,116,114,97,99,116,115,61,48,59,35,99,111,110,116,114,97,99,116,115,61,91,93,59,99,111,110,115,116,114,117,99,116,111,114,40,115,116,97,116,101,41,123,115,116,97,116,101,38,38,40,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,61,115,116,97,116,101,46,99,111,110,116,114,97,99,116,115,44,116,104,105,115,46,35,116,111,116,97,108,67,111,110,116,114,97,99,116,115,61,115,116,97,116,101,46,116,111,116,97,108,67,111,110,116,114,97,99,116,115,41,125,103,101,116,32,115,116,97,116,101,40,41,123,114,101,116,117,114,110,123,116,111,116,97,108,67,111,110,116,114,97,99,116,115,58,116,104,105,115,46,35,116,111,116,97,108,67,111,110,116,114,97,99,116,115,44,99,111,110,116,114,97,99,116,115,58,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,125,125,103,101,116,32,110,97,109,101,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,110,97,109,101,125,103,101,116,32,99,111,110,116,114,97,99,116,115,40,41,123,114,101,116,117,114,110,91,46,46,46,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,93,125,103,101,116,32,116,111,116,97,108,67,111,110,116,114,97,99,116,115,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,116,111,116,97,108,67,111,110,116,114,97,99,116,115,125,105,115,118,97,108,105,100,40,104,97,115,104,44,99,114,101,97,116,111,114,44,99,111,110,116,114,97,99,116,44,99,111,110,115,116,114,117,99,116,111,114,80,97,114,97,109,101,116,101,114,115,61,91,93,41,123,99,111,110,115,116,32,109,101,115,115,97,103,101,61,110,101,119,32,67,111,110,116,114,97,99,116,77,101,115,115,97,103,101,40,123,99,114,101,97,116,111,114,58,99,114,101,97,116,111,114,44,99,111,110,116,114,97,99,116,58,99,111,110,116,114,97,99,116,44,99,111,110,115,116,114,117,99,116,111,114,80,97,114,97,109,101,116,101,114,115,58,99,111,110,115,116,114,117,99,116,111,114,80,97,114,97,109,101,116,101,114,115,125,41,59,114,101,116,117,114,110,32,66,111,111,108,101,97,110,40,109,101,115,115,97,103,101,46,104,97,115,104,61,61,61,104,97,115,104,41,125,97,115,121,110,99,32,100,101,112,108,111,121,67,111,110,116,114,97,99,116,40,99,111,110,116,114,97,99,116,72,97,115,104,44,99,114,101,97,116,111,114,44,99,111,110,116,114,97,99,116,44,99,111,110,115,116,114,117,99,116,111,114,80,97,114,97,109,101,116,101,114,115,61,91,93,41,123,105,102,40,99,111,110,116,114,97,99,116,46,99,114,101,97,116,111,114,33,61,61,109,115,103,46,115,101,110,100,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,111,110,108,121,32,97,32,99,111,110,116,114,97,99,116,32,99,114,101,97,116,111,114,32,99,97,110,32,100,101,112,108,111,121,32,97,32,99,111,110,116,114,97,99,116,34,41,59,105,102,40,97,119,97,105,116,32,99,111,110,116,114,97,99,116,83,116,111,114,101,46,104,97,115,40,104,97,115,104,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,100,117,112,108,105,99,97,116,101,32,99,111,110,116,114,97,99,116,34,41,59,105,102,40,33,116,104,105,115,46,105,115,86,97,108,105,100,40,99,111,110,116,114,97,99,116,72,97,115,104,44,99,114,101,97,116,111,114,44,99,111,110,116,114,97,99,116,44,99,111,110,115,116,114,117,99,116,111,114,80,97,114,97,109,101,116,101,114,115,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,105,110,118,97,108,105,100,32,99,111,110,116,114,97,99,116,34,41,59,97,119,97,105,116,32,99,111,110,116,114,97,99,116,83,116,111,114,101,46,112,117,116,40,104,97,115,104,44,101,110,99,111,100,101,100,41,44,116,104,105,115,46,35,116,111,116,97,108,67,111,110,116,114,97,99,116,115,43,61,49,44,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,46,112,117,115,104,40,104,97,115,104,41,125,125,59,10";
310
+ var nativeToken = "237,198,1,10,54,51,67,122,86,50,109,72,55,122,115,71,66,119,54,122,122,71,99,53,69,102,111,109,116,76,78,114,116,75,78,51,103,78,90,77,98,69,99,112,89,118,83,82,75,115,52,106,85,90,68,116,118,71,121,18,152,26,114,101,116,117,114,110,32,99,108,97,115,115,32,65,114,116,79,110,108,105,110,101,32,101,120,116,101,110,100,115,32,99,108,97,115,115,32,84,111,107,101,110,32,101,120,116,101,110,100,115,32,99,108,97,115,115,32,82,111,108,101,115,123,35,114,111,108,101,115,61,123,79,87,78,69,82,58,91,93,44,77,73,78,84,58,91,93,44,66,85,82,78,58,91,93,125,59,99,111,110,115,116,114,117,99,116,111,114,40,114,111,108,101,115,41,123,105,102,40,114,111,108,101,115,41,123,105,102,40,33,40,114,111,108,101,115,32,105,110,115,116,97,110,99,101,111,102,32,79,98,106,101,99,116,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,101,120,112,101,99,116,101,100,32,114,111,108,101,115,32,116,111,32,98,101,32,97,110,32,111,98,106,101,99,116,34,41,59,116,104,105,115,46,35,114,111,108,101,115,61,123,46,46,46,114,111,108,101,115,44,46,46,46,116,104,105,115,46,35,114,111,108,101,115,125,125,101,108,115,101,32,116,104,105,115,46,35,103,114,97,110,116,82,111,108,101,40,109,115,103,46,115,101,110,100,101,114,44,34,79,87,78,69,82,34,41,125,103,101,116,32,115,116,97,116,101,40,41,123,114,101,116,117,114,110,123,114,111,108,101,115,58,116,104,105,115,46,114,111,108,101,115,125,125,103,101,116,32,114,111,108,101,115,40,41,123,114,101,116,117,114,110,123,46,46,46,116,104,105,115,46,35,114,111,108,101,115,125,125,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,114,101,116,117,114,110,33,33,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,38,38,45,49,33,61,61,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,105,110,100,101,120,79,102,40,97,100,100,114,101,115,115,41,125,35,103,114,97,110,116,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,96,36,123,114,111,108,101,125,32,114,111,108,101,32,97,108,114,101,97,100,121,32,103,114,97,110,116,101,100,32,102,111,114,32,36,123,97,100,100,114,101,115,115,125,96,41,59,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,112,117,115,104,40,97,100,100,114,101,115,115,41,125,35,114,101,118,111,107,101,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,96,36,123,114,111,108,101,125,32,114,111,108,101,32,97,108,114,101,97,100,121,32,114,101,118,111,107,101,100,32,102,111,114,32,36,123,97,100,100,114,101,115,115,125,96,41,59,105,102,40,34,79,87,78,69,82,34,61,61,61,114,111,108,101,38,38,49,61,61,61,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,108,101,110,103,116,104,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,97,116,108,101,97,115,116,32,111,110,101,32,111,119,110,101,114,32,105,115,32,110,101,101,100,101,100,33,34,41,59,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,115,112,108,105,99,101,40,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,105,110,100,101,120,79,102,40,97,100,100,114,101,115,115,41,41,125,103,114,97,110,116,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,34,79,87,78,69,82,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,78,111,116,32,97,108,108,111,119,101,100,34,41,59,116,104,105,115,46,35,103,114,97,110,116,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,125,114,101,118,111,107,101,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,34,79,87,78,69,82,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,78,111,116,32,97,108,108,111,119,101,100,34,41,59,116,104,105,115,46,35,114,101,118,111,107,101,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,125,125,123,35,110,97,109,101,59,35,115,121,109,98,111,108,59,35,104,111,108,100,101,114,115,61,48,59,35,98,97,108,97,110,99,101,115,61,123,125,59,35,97,112,112,114,111,118,97,108,115,61,123,125,59,35,100,101,99,105,109,97,108,115,61,49,56,59,35,116,111,116,97,108,83,117,112,112,108,121,61,66,105,103,78,117,109,98,101,114,46,102,114,111,109,40,48,41,59,99,111,110,115,116,114,117,99,116,111,114,40,110,97,109,101,44,115,121,109,98,111,108,44,100,101,99,105,109,97,108,115,61,49,56,44,115,116,97,116,101,41,123,105,102,40,33,110,97,109,101,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,97,109,101,32,117,110,100,101,102,105,110,101,100,34,41,59,105,102,40,33,115,121,109,98,111,108,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,115,121,109,98,111,108,32,117,110,100,101,102,105,110,101,100,34,41,59,115,117,112,101,114,40,115,116,97,116,101,63,46,114,111,108,101,115,41,44,116,104,105,115,46,35,110,97,109,101,61,110,97,109,101,44,116,104,105,115,46,35,115,121,109,98,111,108,61,115,121,109,98,111,108,44,116,104,105,115,46,35,100,101,99,105,109,97,108,115,61,100,101,99,105,109,97,108,115,125,103,101,116,32,115,116,97,116,101,40,41,123,114,101,116,117,114,110,123,46,46,46,115,117,112,101,114,46,115,116,97,116,101,44,104,111,108,100,101,114,115,58,116,104,105,115,46,104,111,108,100,101,114,115,44,98,97,108,97,110,99,101,115,58,116,104,105,115,46,98,97,108,97,110,99,101,115,44,97,112,112,114,111,118,97,108,115,58,123,46,46,46,116,104,105,115,46,35,97,112,112,114,111,118,97,108,115,125,44,116,111,116,97,108,83,117,112,112,108,121,58,116,104,105,115,46,116,111,116,97,108,83,117,112,112,108,121,125,125,103,101,116,32,116,111,116,97,108,83,117,112,112,108,121,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,116,111,116,97,108,83,117,112,112,108,121,125,103,101,116,32,110,97,109,101,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,110,97,109,101,125,103,101,116,32,115,121,109,98,111,108,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,115,121,109,98,111,108,125,103,101,116,32,104,111,108,100,101,114,115,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,104,111,108,100,101,114,115,125,103,101,116,32,98,97,108,97,110,99,101,115,40,41,123,114,101,116,117,114,110,123,46,46,46,116,104,105,115,46,35,98,97,108,97,110,99,101,115,125,125,109,105,110,116,40,116,111,44,97,109,111,117,110,116,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,109,115,103,46,115,101,110,100,101,114,44,34,77,73,78,84,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,116,32,97,108,108,111,119,101,100,34,41,59,116,104,105,115,46,35,116,111,116,97,108,83,117,112,112,108,121,61,116,104,105,115,46,35,116,111,116,97,108,83,117,112,112,108,121,46,97,100,100,40,97,109,111,117,110,116,41,44,116,104,105,115,46,35,105,110,99,114,101,97,115,101,66,97,108,97,110,99,101,40,116,111,44,97,109,111,117,110,116,41,125,98,117,114,110,40,116,111,44,97,109,111,117,110,116,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,109,115,103,46,115,101,110,100,101,114,44,34,66,85,82,78,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,116,32,97,108,108,111,119,101,100,34,41,59,116,104,105,115,46,35,116,111,116,97,108,83,117,112,112,108,121,61,116,104,105,115,46,35,116,111,116,97,108,83,117,112,112,108,121,46,115,117,98,40,97,109,111,117,110,116,41,44,116,104,105,115,46,35,100,101,99,114,101,97,115,101,66,97,108,97,110,99,101,40,116,111,44,97,109,111,117,110,116,41,125,35,98,101,102,111,114,101,84,114,97,110,115,102,101,114,40,102,114,111,109,44,116,111,44,97,109,111,117,110,116,41,123,105,102,40,33,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,102,114,111,109,93,124,124,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,102,114,111,109,93,60,97,109,111,117,110,116,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,97,109,111,117,110,116,32,101,120,99,101,101,100,115,32,98,97,108,97,110,99,101,34,41,125,35,117,112,100,97,116,101,72,111,108,100,101,114,115,40,97,100,100,114,101,115,115,44,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,41,123,34,48,120,48,48,34,61,61,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,46,116,111,72,101,120,83,116,114,105,110,103,40,41,63,116,104,105,115,46,35,104,111,108,100,101,114,115,45,61,49,58,34,48,120,48,48,34,33,61,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,46,116,111,72,101,120,83,116,114,105,110,103,40,41,38,38,34,48,120,48,48,34,61,61,61,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,46,116,111,72,101,120,83,116,114,105,110,103,40,41,38,38,40,116,104,105,115,46,35,104,111,108,100,101,114,115,43,61,49,41,125,35,105,110,99,114,101,97,115,101,66,97,108,97,110,99,101,40,97,100,100,114,101,115,115,44,97,109,111,117,110,116,41,123,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,124,124,40,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,61,66,105,103,78,117,109,98,101,114,46,102,114,111,109,40,48,41,41,59,99,111,110,115,116,32,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,59,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,46,97,100,100,40,97,109,111,117,110,116,41,44,116,104,105,115,46,35,117,112,100,97,116,101,72,111,108,100,101,114,115,40,97,100,100,114,101,115,115,44,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,41,125,35,100,101,99,114,101,97,115,101,66,97,108,97,110,99,101,40,97,100,100,114,101,115,115,44,97,109,111,117,110,116,41,123,99,111,110,115,116,32,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,59,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,46,115,117,98,40,97,109,111,117,110,116,41,44,116,104,105,115,46,35,117,112,100,97,116,101,72,111,108,100,101,114,115,40,97,100,100,114,101,115,115,44,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,41,125,98,97,108,97,110,99,101,79,102,40,97,100,100,114,101,115,115,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,125,115,101,116,65,112,112,114,111,118,97,108,40,111,112,101,114,97,116,111,114,44,97,109,111,117,110,116,41,123,99,111,110,115,116,32,111,119,110,101,114,61,103,108,111,98,97,108,84,104,105,115,46,109,115,103,46,115,101,110,100,101,114,59,116,104,105,115,46,35,97,112,112,114,111,118,97,108,115,91,111,119,110,101,114,93,124,124,40,116,104,105,115,46,35,97,112,112,114,111,118,97,108,115,91,111,119,110,101,114,93,61,123,125,41,44,116,104,105,115,46,35,97,112,112,114,111,118,97,108,115,91,111,119,110,101,114,93,91,111,112,101,114,97,116,111,114,93,61,97,109,111,117,110,116,125,97,112,112,114,111,118,101,100,40,111,119,110,101,114,44,111,112,101,114,97,116,111,114,44,97,109,111,117,110,116,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,97,112,112,114,111,118,97,108,115,91,111,119,110,101,114,93,91,111,112,101,114,97,116,111,114,93,61,61,61,97,109,111,117,110,116,125,116,114,97,110,115,102,101,114,40,102,114,111,109,44,116,111,44,97,109,111,117,110,116,41,123,97,109,111,117,110,116,61,66,105,103,78,117,109,98,101,114,46,102,114,111,109,40,97,109,111,117,110,116,41,44,116,104,105,115,46,35,98,101,102,111,114,101,84,114,97,110,115,102,101,114,40,102,114,111,109,44,116,111,44,97,109,111,117,110,116,41,44,116,104,105,115,46,35,100,101,99,114,101,97,115,101,66,97,108,97,110,99,101,40,102,114,111,109,44,97,109,111,117,110,116,41,44,116,104,105,115,46,35,105,110,99,114,101,97,115,101,66,97,108,97,110,99,101,40,116,111,44,97,109,111,117,110,116,41,125,125,123,99,111,110,115,116,114,117,99,116,111,114,40,115,116,97,116,101,41,123,115,117,112,101,114,40,34,65,114,116,79,110,108,105,110,101,34,44,34,65,82,84,34,44,49,56,44,115,116,97,116,101,41,125,125,59,10";
311
+ var nameService = "237,198,1,10,54,51,67,122,86,50,109,72,55,122,115,71,66,119,54,122,122,71,99,53,69,102,111,109,116,76,78,114,116,75,78,51,103,78,90,77,98,69,99,112,89,118,83,82,75,115,52,106,85,90,68,116,118,71,121,18,138,12,114,101,116,117,114,110,32,99,108,97,115,115,32,78,97,109,101,83,101,114,118,105,99,101,123,35,110,97,109,101,61,34,65,114,116,79,110,108,105,110,101,78,97,109,101,83,101,114,118,105,99,101,34,59,35,111,119,110,101,114,59,35,112,114,105,99,101,61,48,59,35,114,101,103,105,115,116,114,121,61,123,125,59,35,99,117,114,114,101,110,99,121,59,103,101,116,32,110,97,109,101,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,110,97,109,101,125,103,101,116,32,114,101,103,105,115,116,114,121,40,41,123,114,101,116,117,114,110,123,46,46,46,116,104,105,115,46,35,114,101,103,105,115,116,114,121,125,125,103,101,116,32,115,116,97,116,101,40,41,123,125,99,111,110,115,116,114,117,99,116,111,114,40,102,97,99,116,111,114,121,65,100,100,114,101,115,115,44,99,117,114,114,101,110,99,121,44,118,97,108,105,100,97,116,111,114,65,100,100,114,101,115,115,44,112,114,105,99,101,44,115,116,97,116,101,41,123,115,116,97,116,101,63,40,116,104,105,115,46,35,111,119,110,101,114,61,115,116,97,116,101,46,111,119,110,101,114,44,116,104,105,115,46,35,114,101,103,105,115,116,114,121,61,115,116,97,116,101,46,114,101,103,105,115,116,114,121,44,116,104,105,115,46,35,99,117,114,114,101,110,99,121,61,115,116,97,116,101,46,99,117,114,114,101,110,99,121,44,116,104,105,115,46,35,112,114,105,99,101,61,115,116,97,116,101,46,112,114,105,99,101,41,58,40,116,104,105,115,46,35,111,119,110,101,114,61,109,115,103,46,115,101,110,100,101,114,44,116,104,105,115,46,35,112,114,105,99,101,61,112,114,105,99,101,44,116,104,105,115,46,35,114,101,103,105,115,116,114,121,46,65,114,116,79,110,108,105,110,101,67,111,110,116,114,97,99,116,70,97,99,116,111,114,121,61,123,111,119,110,101,114,58,109,115,103,46,115,101,110,100,101,114,44,97,100,100,114,101,115,115,58,102,97,99,116,111,114,121,65,100,100,114,101,115,115,125,44,116,104,105,115,46,35,114,101,103,105,115,116,114,121,46,65,114,116,79,110,108,105,110,101,84,111,107,101,110,61,123,111,119,110,101,114,58,109,115,103,46,115,101,110,100,101,114,44,97,100,100,114,101,115,115,58,99,117,114,114,101,110,99,121,125,44,116,104,105,115,46,35,114,101,103,105,115,116,114,121,46,65,114,116,79,110,108,105,110,101,86,97,108,105,100,97,116,111,114,115,61,123,111,119,110,101,114,58,109,115,103,46,115,101,110,100,101,114,44,97,100,100,114,101,115,115,58,118,97,108,105,100,97,116,111,114,65,100,100,114,101,115,115,125,44,116,104,105,115,46,35,99,117,114,114,101,110,99,121,61,99,117,114,114,101,110,99,121,41,125,99,104,97,110,103,101,79,119,110,101,114,40,111,119,110,101,114,41,123,105,102,40,109,115,103,46,115,101,110,100,101,114,33,61,61,116,104,105,115,46,35,111,119,110,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,32,111,119,110,101,114,34,41,59,116,104,105,115,46,35,111,119,110,101,114,61,111,119,110,101,114,125,99,104,97,110,103,101,80,114,105,99,101,40,112,114,105,99,101,41,123,105,102,40,109,115,103,46,115,101,110,100,101,114,33,61,61,116,104,105,115,46,35,111,119,110,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,32,111,119,110,101,114,34,41,59,116,104,105,115,46,35,112,114,105,99,101,61,112,114,105,99,101,125,99,104,97,110,103,101,67,117,114,114,101,110,99,121,40,99,117,114,114,101,110,99,121,41,123,105,102,40,109,115,103,46,115,101,110,100,101,114,33,61,61,116,104,105,115,46,35,111,119,110,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,32,111,119,110,101,114,34,41,59,116,104,105,115,46,35,99,117,114,114,101,110,99,121,61,99,117,114,114,101,110,99,121,125,97,115,121,110,99,32,112,117,114,99,104,97,115,101,78,97,109,101,40,110,97,109,101,44,97,100,100,114,101,115,115,41,123,105,102,40,97,119,97,105,116,32,109,115,103,46,99,97,108,108,40,116,104,105,115,46,35,99,117,114,114,101,110,99,121,44,34,98,97,108,97,110,99,101,79,102,34,44,91,109,115,103,46,115,101,110,100,101,114,93,41,60,116,104,105,115,46,35,112,114,105,99,101,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,112,114,105,99,101,32,101,120,99,101,101,100,115,32,98,97,108,97,110,99,101,34,41,59,116,114,121,123,97,119,97,105,116,32,109,115,103,46,99,97,108,108,40,116,104,105,115,46,35,99,117,114,114,101,110,99,121,44,34,116,114,97,110,115,102,101,114,34,44,91,109,115,103,46,115,101,110,100,101,114,44,116,104,105,115,46,35,111,119,110,101,114,44,116,104,105,115,46,35,112,114,105,99,101,93,41,125,99,97,116,99,104,40,101,41,123,116,104,114,111,119,32,101,125,116,104,105,115,46,35,114,101,103,105,115,116,114,121,91,110,97,109,101,93,61,123,111,119,110,101,114,58,109,115,103,46,115,101,110,100,101,114,44,97,100,100,114,101,115,115,58,97,100,100,114,101,115,115,125,125,108,111,111,107,117,112,40,110,97,109,101,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,114,101,103,105,115,116,114,121,91,110,97,109,101,93,125,116,114,97,110,115,102,101,114,79,119,110,101,114,115,104,105,112,40,110,97,109,101,44,116,111,41,123,105,102,40,109,115,103,46,115,101,110,100,101,114,33,61,61,116,104,105,115,46,35,114,101,103,105,115,116,114,121,46,111,119,110,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,116,32,97,32,111,119,110,101,114,34,41,59,116,104,105,115,46,35,114,101,103,105,115,116,114,121,91,110,97,109,101,93,46,111,119,110,101,114,61,116,111,125,99,104,97,110,103,101,65,100,100,114,101,115,115,40,110,97,109,101,44,97,100,100,114,101,115,115,41,123,105,102,40,109,115,103,46,115,101,110,100,101,114,33,61,61,116,104,105,115,46,35,114,101,103,105,115,116,114,121,46,111,119,110,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,116,32,97,32,111,119,110,101,114,34,41,59,116,104,105,115,46,35,114,101,103,105,115,116,114,121,91,110,97,109,101,93,46,97,100,100,114,101,115,115,61,97,100,100,114,101,115,115,125,125,59,10,26,58,98,97,53,120,100,97,99,52,121,98,116,116,105,120,97,109,109,116,115,99,115,112,116,102,115,118,116,113,119,110,122,117,97,119,53,112,54,101,103,104,110,50,115,104,55,116,102,53,100,115,107,113,116,100,103,104,106,99,26,58,98,97,53,120,100,97,100,50,116,53,55,102,112,100,97,100,107,114,117,109,119,104,116,51,50,117,119,99,100,117,98,106,112,112,113,54,122,50,118,107,119,111,109,103,111,119,102,106,112,122,109,106,98,110,50,119,111,50,26,58,98,97,53,120,100,97,99,105,99,112,114,117,122,104,52,108,117,54,106,101,53,109,102,110,52,50,102,54,115,52,121,55,116,98,111,120,111,115,121,114,115,112,120,109,110,101,114,53,119,106,109,97,53,98,118,110,116,110,26,17,48,46,48,48,48,48,48,48,48,48,48,48,48,48,48,48,49";
312
+ var validators = "237,198,1,10,54,51,67,122,86,50,109,72,55,122,115,71,66,119,54,122,122,71,99,53,69,102,111,109,116,76,78,114,116,75,78,51,103,78,90,77,98,69,99,112,89,118,83,82,75,115,52,106,85,90,68,116,118,71,121,18,215,16,114,101,116,117,114,110,32,99,108,97,115,115,32,86,97,108,105,100,97,116,111,114,115,123,35,110,97,109,101,61,34,65,114,116,79,110,108,105,110,101,86,97,108,105,100,97,116,111,114,115,34,59,35,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,61,48,59,35,118,97,108,105,100,97,116,111,114,115,61,123,125,59,35,111,119,110,101,114,59,35,99,117,114,114,101,110,99,121,59,35,109,105,110,105,109,117,109,66,97,108,97,110,99,101,59,103,101,116,32,115,116,97,116,101,40,41,123,114,101,116,117,114,110,123,111,119,110,101,114,58,116,104,105,115,46,35,111,119,110,101,114,44,109,105,110,105,109,117,109,66,97,108,97,110,99,101,58,116,104,105,115,46,35,109,105,110,105,109,117,109,66,97,108,97,110,99,101,44,99,117,114,114,101,110,99,121,58,116,104,105,115,46,35,99,117,114,114,101,110,99,121,44,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,58,116,104,105,115,46,35,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,44,118,97,108,105,100,97,116,111,114,115,58,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,125,125,99,111,110,115,116,114,117,99,116,111,114,40,116,111,107,101,110,65,100,100,114,101,115,115,44,115,116,97,116,101,41,123,115,116,97,116,101,63,40,116,104,105,115,46,35,111,119,110,101,114,61,115,116,97,116,101,46,111,119,110,101,114,44,116,104,105,115,46,35,109,105,110,105,109,117,109,66,97,108,97,110,99,101,61,115,116,97,116,101,46,109,105,110,105,109,117,109,66,97,108,97,110,99,101,44,116,104,105,115,46,35,99,117,114,114,101,110,99,121,61,115,116,97,116,101,46,99,117,114,114,101,110,99,121,44,116,104,105,115,46,35,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,61,115,116,97,116,101,46,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,44,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,61,115,116,97,116,101,46,118,97,108,105,100,97,116,111,114,115,41,58,40,116,104,105,115,46,35,111,119,110,101,114,61,109,115,103,46,115,101,110,100,101,114,44,116,104,105,115,46,35,109,105,110,105,109,117,109,66,97,108,97,110,99,101,61,53,101,52,44,116,104,105,115,46,35,99,117,114,114,101,110,99,121,61,116,111,107,101,110,65,100,100,114,101,115,115,44,116,104,105,115,46,35,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,43,61,49,44,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,91,109,115,103,46,115,101,110,100,101,114,93,61,123,102,105,114,115,116,83,101,101,110,58,40,110,101,119,32,68,97,116,101,41,46,103,101,116,84,105,109,101,40,41,44,97,99,116,105,118,101,58,33,48,125,41,125,103,101,116,32,110,97,109,101,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,110,97,109,101,125,103,101,116,32,111,119,110,101,114,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,111,119,110,101,114,125,103,101,116,32,99,117,114,114,101,110,99,121,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,99,117,114,114,101,110,99,121,125,103,101,116,32,118,97,108,105,100,97,116,111,114,115,40,41,123,114,101,116,117,114,110,123,46,46,46,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,125,125,103,101,116,32,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,125,103,101,116,32,109,105,110,105,109,117,109,66,97,108,97,110,99,101,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,109,105,110,105,109,117,109,66,97,108,97,110,99,101,125,99,104,97,110,103,101,79,119,110,101,114,40,111,119,110,101,114,41,123,105,102,40,109,115,103,46,115,101,110,100,101,114,33,61,61,116,104,105,115,46,35,111,119,110,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,116,32,97,110,32,111,119,110,101,114,34,41,125,99,104,97,110,103,101,67,117,114,114,101,110,99,121,40,99,117,114,114,101,110,99,121,41,123,105,102,40,109,115,103,46,115,101,110,100,101,114,33,61,61,116,104,105,115,46,35,111,119,110,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,116,32,97,110,32,111,119,110,101,114,34,41,59,116,104,105,115,46,35,99,117,114,114,101,110,99,121,61,99,117,114,114,101,110,99,121,125,104,97,115,40,118,97,108,105,100,97,116,111,114,41,123,114,101,116,117,114,110,32,66,111,111,108,101,97,110,40,118,111,105,100,32,48,33,61,61,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,91,118,97,108,105,100,97,116,111,114,93,41,125,97,115,121,110,99,32,97,100,100,86,97,108,105,100,97,116,111,114,40,118,97,108,105,100,97,116,111,114,41,123,105,102,40,116,104,105,115,46,104,97,115,40,118,97,108,105,100,97,116,111,114,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,97,108,114,101,97,100,121,32,97,32,118,97,108,105,100,97,116,111,114,34,41,59,99,111,110,115,116,32,98,97,108,97,110,99,101,61,97,119,97,105,116,32,109,115,103,46,115,116,97,116,105,99,67,97,108,108,40,116,104,105,115,46,99,117,114,114,101,110,99,121,44,34,98,97,108,97,110,99,101,79,102,34,44,91,109,115,103,46,115,101,110,100,101,114,93,41,59,105,102,40,98,97,108,97,110,99,101,60,116,104,105,115,46,109,105,110,105,109,117,109,66,97,108,97,110,99,101,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,96,98,97,108,97,110,99,101,32,116,111,32,108,111,119,33,32,103,111,116,58,32,36,123,98,97,108,97,110,99,101,125,32,110,101,101,100,58,32,36,123,116,104,105,115,46,35,109,105,110,105,109,117,109,66,97,108,97,110,99,101,125,96,41,59,116,104,105,115,46,35,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,43,61,49,44,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,91,118,97,108,105,100,97,116,111,114,93,61,123,102,105,114,115,116,83,101,101,110,58,40,110,101,119,32,68,97,116,101,41,46,103,101,116,84,105,109,101,40,41,44,97,99,116,105,118,101,58,33,48,125,125,114,101,109,111,118,101,86,97,108,105,100,97,116,111,114,40,118,97,108,105,100,97,116,111,114,41,123,105,102,40,33,116,104,105,115,46,104,97,115,40,118,97,108,105,100,97,116,111,114,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,118,97,108,105,100,97,116,111,114,32,110,111,116,32,102,111,117,110,100,34,41,59,116,104,105,115,46,35,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,45,61,49,44,100,101,108,101,116,101,32,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,91,118,97,108,105,100,97,116,111,114,93,125,97,115,121,110,99,32,117,112,100,97,116,101,86,97,108,105,100,97,116,111,114,40,118,97,108,105,100,97,116,111,114,44,97,99,116,105,118,101,41,123,105,102,40,33,116,104,105,115,46,104,97,115,40,118,97,108,105,100,97,116,111,114,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,118,97,108,105,100,97,116,111,114,32,110,111,116,32,102,111,117,110,100,34,41,59,99,111,110,115,116,32,98,97,108,97,110,99,101,61,97,119,97,105,116,32,109,115,103,46,115,116,97,116,105,99,67,97,108,108,40,116,104,105,115,46,99,117,114,114,101,110,99,121,44,34,98,97,108,97,110,99,101,79,102,34,44,91,109,115,103,46,115,101,110,100,101,114,93,41,59,105,102,40,98,97,108,97,110,99,101,60,116,104,105,115,46,109,105,110,105,109,117,109,66,97,108,97,110,99,101,38,38,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,91,118,97,108,105,100,97,116,111,114,93,46,97,99,116,105,118,101,38,38,40,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,91,118,97,108,105,100,97,116,111,114,93,46,97,99,116,105,118,101,61,33,49,41,44,98,97,108,97,110,99,101,60,116,104,105,115,46,109,105,110,105,109,117,109,66,97,108,97,110,99,101,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,96,98,97,108,97,110,99,101,32,116,111,32,108,111,119,33,32,103,111,116,58,32,36,123,98,97,108,97,110,99,101,125,32,110,101,101,100,58,32,36,123,116,104,105,115,46,35,109,105,110,105,109,117,109,66,97,108,97,110,99,101,125,96,41,59,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,91,118,97,108,105,100,97,116,111,114,93,46,97,99,116,105,118,101,61,97,99,116,105,118,101,125,125,59,10,26,58,98,97,53,120,100,97,100,50,116,53,55,102,112,100,97,100,107,114,117,109,119,104,116,51,50,117,119,99,100,117,98,106,112,112,113,54,122,50,118,107,119,111,109,103,111,119,102,106,112,122,109,106,98,110,50,119,111,50";
313
+ var bytecodes = {
314
+ contractFactory: contractFactory,
315
+ nativeToken: nativeToken,
316
+ nameService: nameService,
317
+ validators: validators
318
+ };
319
+
320
+ const contractFactoryMessage = new Uint8Array(bytecodes.contractFactory.split(','));
321
+ const nativeTokenMessage = new Uint8Array(bytecodes.nativeToken.split(','));
322
+
323
+ const nameServiceMessage = new Uint8Array(bytecodes.nameService.split(','));
324
+ const validatorsMessage = new Uint8Array(bytecodes.validators.split(','));
325
+
326
+ const calculateFee = async transaction => {
327
+ // excluded from fees
328
+ if (transaction.to === addresses$1.validators) return 0
329
+ // fee per gb
330
+ transaction = await new messages.TransactionMessage(transaction);
331
+ let fee = transaction.encoded.length;
332
+ fee = fee / 1024;
333
+ fee = fee / 1000000;
334
+ const parts = String(fee).split('.');
335
+ let decimals = 0;
336
+ if (parts[1]) {
337
+ const potentional = parts[1].split('e');
338
+ parts[1] = potentional[0];
339
+ decimals = Number(potentional[1].replace(/\-|\+/g, '')) + Number(potentional[0].length);
340
+ }
341
+
342
+ return Number.parseFloat(fee.toString()).toFixed(decimals)
343
+ };
344
+
345
+ util.promisify(fs.readFile);
346
+ util.promisify(fs.writeFile);
347
+
134
348
  globalThis.BigNumber = utils.BigNumber;
135
349
 
136
350
  // check if browser or local
@@ -154,7 +368,7 @@ class Chain {
154
368
  }
155
369
 
156
370
  get nativeToken() {
157
- return lib.nativeToken
371
+ return addresses.nativeToken
158
372
  }
159
373
 
160
374
  get validators() {
@@ -167,28 +381,39 @@ class Chain {
167
381
 
168
382
  async hasTransactionToHandle() {
169
383
  const size = await transactionPoolStore.size();
384
+ console.log({size});
170
385
  if (size > 0) return true
171
386
  return false
172
387
  }
173
388
 
174
389
  async #runEpoch() {
175
- const validators = await this.staticCall(lib.validators, 'validators');
390
+ this.#runningEpoch = true;
391
+ console.log('epoch');
392
+ const validators = await this.staticCall(addresses.validators, 'validators');
176
393
  if (!validators[peernet.id]?.active) return
394
+ console.log('active');
177
395
 
178
- this.#runningEpoch = true;
396
+
179
397
  const start = new Date().getTime();
180
- await this.#createBlock();
398
+ try {
399
+ await this.#createBlock();
400
+ } catch (e) {
401
+ console.error(e);
402
+ }
403
+
181
404
  const end = new Date().getTime();
182
- console.log((end - start) / 1000 + ' s');
405
+ console.log(((end - start) / 1000) + ' s');
406
+
183
407
  if (await this.hasTransactionToHandle()) return this.#runEpoch()
184
408
  this.#runningEpoch = false;
409
+ // if (await this.hasTransactionToHandle() && !this.#runningEpoch) return this.#runEpoch()
185
410
  }
186
411
 
187
412
  async #setup() {
188
- await contractStore.put(lib.contractFactory, lib.contractFactoryMessage);
189
- await contractStore.put(lib.nativeToken, lib.nativeTokenMessage);
190
- await contractStore.put(lib.validators, lib.validatorsMessage);
191
- await contractStore.put(lib.nameService, lib.nameServiceMessage);
413
+ await contractStore.put(addresses.contractFactory, contractFactoryMessage);
414
+ await contractStore.put(addresses.nativeToken, nativeTokenMessage);
415
+ await contractStore.put(addresses.validators, validatorsMessage);
416
+ await contractStore.put(addresses.nameService, nameServiceMessage);
192
417
  console.log('handle native contracts');
193
418
  // handle native contracts
194
419
  }
@@ -207,7 +432,6 @@ class Chain {
207
432
 
208
433
  promises = await Promise.allSettled(promises);
209
434
  promises = promises.reduce((set, c) => {
210
- console.log({c});
211
435
  if (c.index > set.index) {
212
436
  set.index = c.index;
213
437
  set.hash = c.hash;
@@ -220,7 +444,8 @@ class Chain {
220
444
  async #init() {
221
445
  // this.node = await new Node()
222
446
  this.participants = [];
223
- const initialized = await contractStore.has(lib.contractFactory);
447
+ this.participating = false;
448
+ const initialized = await contractStore.has(addresses.contractFactory);
224
449
  if (!initialized) await this.#setup();
225
450
 
226
451
  this.#machine = await new Machine();
@@ -229,8 +454,8 @@ class Chain {
229
454
  try {
230
455
  let localBlock = await chainStore.get('lastBlock');
231
456
  localBlock = await peernet.get(new TextDecoder().decode(localBlock));
232
- localBlock = await await new lib$1.BlockMessage(localBlock);
233
- this.#lastBlock = {...localBlock.decoded, hash: localBlock.hash};
457
+ localBlock = await await new BlockMessage(localBlock);
458
+ this.#lastBlock = {...localBlock.decoded, hash: await localBlock.hash};
234
459
  // console.log(this.lastBlock.decoded.transactions);
235
460
  } catch (e) {
236
461
  await this.#sync();
@@ -239,22 +464,14 @@ class Chain {
239
464
 
240
465
  await peernet.addRequestHandler('bw-request-message', () => {
241
466
 
242
- return new lib$1.BWMessage(peernet.client.bw) || { up: 0, down: 0 }
467
+ return new BWMessage(peernet.client.bw) || { up: 0, down: 0 }
243
468
  });
244
469
 
245
470
  await peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
246
471
 
247
472
  peernet.subscribe('add-block', this.#addBlock.bind(this));
248
473
 
249
- peernet.subscribe('add-transaction', async transaction => {
250
- try {
251
- transaction = await new lib$1.TransactionMessage(transaction);
252
- await transactionPoolStore.put(transaction.hash, transaction.encoded);
253
- if (await this.hasTransactionToHandle() && !this.#runningEpoch) return this.#runEpoch()
254
- } catch (e) {
255
- throw Error('invalid transaction')
256
- }
257
- });
474
+ peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
258
475
 
259
476
  pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
260
477
 
@@ -287,28 +504,41 @@ class Chain {
287
504
  this.#blocks.length;
288
505
  (this.#blocks.length) - blocksSynced;
289
506
  await this.#loadBlocks(this.#blocks);
290
- const message = await new lib$1.BlockMessage(this.lastBlock);
291
- await blockStore.put(message.hash, message.encoded);
507
+ const message = await new BlockMessage(this.lastBlock);
508
+ await blockStore.put(await message.hash, message.encoded);
292
509
  await chainStore.put('lastBlock', new TextEncoder().encode(this.lastBlock.hash));
293
510
  }
294
511
  }
295
512
 
296
- async #lastBlockHandler() {
297
- return new peernet.protos['peernet-response']({response: new TextEncoder().encode(JSON.stringify({ hash: this.lastBlock?.hash, index: this.lastBlock?.index }))})
298
- }
513
+ #epochTimeout
299
514
 
300
- async resolveBlock(hash) {
301
- let block = await peernet.get(hash, 'block');
302
- if (!await peernet.has(hash, 'block')) await peernet.put(hash, block, 'block');
303
- block = await new lib$1.BlockMessage(block);
304
- const size = block.encoded.length || block.encoded.byteLength;
305
- block = {...block.decoded, hash};
306
- this.#blocks[block.index] = block;
307
- console.log(`loaded block: ${hash} @${block.index} ${Math.round((size / 1024 / 1024) * 100) / 100} mb`);
308
- if (block.index !== 0) {
309
- return this.resolveBlock(block.previousHash)
310
- }
515
+ async #addTransaction(transaction) {
516
+ try {
517
+ transaction = await new TransactionMessage(transaction);
518
+ const has = await transactionPoolStore.has(await transaction.hash);
519
+ if (!has) await transactionPoolStore.put(await transaction.hash, transaction.encoded);
520
+ if (this.participating && !this.#runningEpoch) this.#runEpoch();
521
+ } catch (e) {
522
+ throw Error('invalid transaction')
311
523
  }
524
+ }
525
+
526
+ async #lastBlockHandler() {
527
+ return new peernet.protos['peernet-response']({response: new TextEncoder().encode(JSON.stringify({ hash: this.lastBlock?.hash, index: this.lastBlock?.index }))})
528
+ }
529
+
530
+ async resolveBlock(hash) {
531
+ let block = await peernet.get(hash, 'block');
532
+ if (!await peernet.has(hash, 'block')) await peernet.put(hash, block, 'block');
533
+ block = await new BlockMessage(block);
534
+ const size = block.encoded.length || block.encoded.byteLength;
535
+ block = {...block.decoded, hash};
536
+ this.#blocks[block.index] = block;
537
+ console.log(`loaded block: ${hash} @${block.index} ${formatBytes(size)}`);
538
+ if (block.index !== 0) {
539
+ return this.resolveBlock(block.previousHash)
540
+ }
541
+ }
312
542
 
313
543
  async resolveBlocks() {
314
544
  try {
@@ -373,7 +603,7 @@ class Chain {
373
603
  }
374
604
 
375
605
  async #addBlock(block) {
376
- const blockMessage = await new lib$1.BlockMessage(block);
606
+ const blockMessage = await new BlockMessage(block);
377
607
  // if (!Buffer.isBuffer(block)) block = Buffer.from(block, 'hex')
378
608
  // const transactionJob = async transaction => {
379
609
  // try {
@@ -389,14 +619,14 @@ class Chain {
389
619
  // return transaction
390
620
  // }
391
621
  await Promise.all(blockMessage.decoded.transactions
392
- .map(transaction => transactionPoolStore.delete(transaction.hash)));
393
-
622
+ .map(async transaction => transactionPoolStore.delete(await transaction.hash)));
623
+ const hash = await blockMessage.hash;
394
624
  // let transactions = blockMessage.decoded.transactions.map(tx => transactionJob(tx))
395
625
  // transactions = await Promise.all(transactions)
396
- this.#lastBlock = {hash: blockMessage.hash, ...blockMessage.decoded};
397
- await blockStore.put(blockMessage.hash, blockMessage.encoded);
398
- await chainStore.put('lastBlock', new TextEncoder().encode(blockMessage.hash));
399
- debug(`added block: ${blockMessage.hash}`);
626
+ this.#lastBlock = { hash, ...blockMessage.decoded };
627
+ await blockStore.put(hash, blockMessage.encoded);
628
+ await chainStore.put('lastBlock', new TextEncoder().encode(hash));
629
+ debug(`added block: ${hash}`);
400
630
  let promises = [];
401
631
  let contracts = [];
402
632
  for (let transaction of blockMessage.decoded.transactions) {
@@ -407,7 +637,10 @@ class Chain {
407
637
  }
408
638
  try {
409
639
  promises = await Promise.allSettled(promises);
410
-
640
+ for (let transaction of blockMessage.decoded.transactions) {
641
+ await accountsStore.put(transaction.from, new TextEncoder().encode(String(transaction.nonce)));
642
+ }
643
+
411
644
  // todo finish state
412
645
  // for (const contract of contracts) {
413
646
  // const state = await this.#machine.get(contract, 'state')
@@ -421,6 +654,10 @@ class Chain {
421
654
 
422
655
  }
423
656
 
657
+ async #updateState() {
658
+
659
+ }
660
+
424
661
 
425
662
 
426
663
  async participate() {
@@ -430,56 +667,72 @@ class Chain {
430
667
  // introduce peer-reputation
431
668
  // peerReputation(peerId)
432
669
  // {bandwith: {up, down}, uptime}
433
- if (!await this.staticCall(lib.validators, 'has', [peernet.id])) await this.createTransactionFrom(peernet.id, lib.validators, 'addValidator', [peernet.id]);
434
- if (await this.hasTransactionToHandle() && !this.#runningEpoch) return this.#runEpoch()
670
+ this.participating = true;
671
+ if (!await this.staticCall(addresses.validators, 'has', [peernet.id])) await this.createTransactionFrom(peernet.id, addresses.validators, 'addValidator', [peernet.id]);
672
+ if (await this.hasTransactionToHandle() && !this.#runningEpoch) await this.#runEpoch();
673
+
674
+ // const runEpoch = () => setTimeout(async () => {
675
+ // if (await this.hasTransactionToHandle() && !this.#runningEpoch) await this.#runEpoch()
676
+ // runEpoch()
677
+ // }, 5000)
678
+ // runEpoch()
435
679
  }
436
680
 
437
681
  calculateFee(transaction) {
438
682
  // excluded from fees
439
- if (transaction.decoded.to === lib.validators) return 0
683
+ // if (transaction.decoded.to === addresses.validators) return 0
440
684
  // fee per gb
441
685
  return (transaction.encoded.length / 1024) / 1e-6
442
686
  }
443
687
 
688
+ async getTransactions (transactions) {
689
+ return new Promise(async (resolve, reject) => {
690
+ let size = 0;
691
+ const _transactions = [];
692
+ const promises = await Promise.all(Object.values(transactions)
693
+ .map(tx => new TransactionMessage(new Uint8Array(tx, tx.byteOffset, tx.byteLength))));
694
+
695
+ for (let transaction of promises) {
696
+ size += transaction.encoded.length;
697
+ if ((size / 1024) / 1024 <= 1.15 ) _transactions.push({...transaction.decoded, hash: await transaction.hash });
698
+ else return resolve(_transactions)
699
+
700
+ }
701
+ return resolve(_transactions)
702
+ })
703
+
704
+ }
444
705
  async #createBlock() {
445
706
  let transactions = await transactionPoolStore.get();
446
707
 
447
708
  if (Object.keys(transactions)?.length === 0 ) return
709
+
448
710
  let block = {
449
711
  transactions: [],
450
712
  validators: [],
451
713
  fees: 0
452
714
  };
453
715
 
454
- let _transactions = [];
455
-
456
716
  // exclude failing tx
457
- for (let transaction of Object.values(transactions)) {
458
- transaction = await new lib$1.TransactionMessage(new Uint8Array(transaction, transaction.byteOffset, transaction.byteLength));
717
+ transactions = await this.getTransactions(transactions);
459
718
 
460
- block.fees += Number(lib.calculateFee(transaction));
461
- _transactions.push({...transaction.decoded, hash: transaction.hash });
462
- }
463
-
464
- transactions = _transactions.sort((a, b) => a.nonce - b.nonce);
465
- _transactions = [];
466
- for (let transaction of transactions) {
719
+ transactions = transactions.sort((a, b) => a.nonce - b.nonce);
720
+ for (let transaction of transactions) {
467
721
  try {
468
722
  await this.#executeTransaction(transaction);
469
- _transactions.push(transaction);
723
+ block.transactions.push(transaction);
724
+ block.fees += Number(calculateFee(transaction));
725
+ await accountsStore.put(transaction.from, new TextEncoder().encode(String(transaction.nonce)));
470
726
  } catch (e) {
471
- transaction = await new lib$1.TransactionMessage(transaction);
472
- block.fees -= Number(lib.calculateFee(transaction));
473
- await transactionPoolStore.delete(transaction.hash);
474
-
727
+ console.error(e);
728
+ transaction = await new TransactionMessage(transaction);
729
+ await transactionPoolStore.delete(await transaction.hash);
730
+ console.log(e);
475
731
  }
476
732
  }
477
- transactions = _transactions;
478
733
  // don't add empty block
479
- if (transactions.length === 0) return
480
-
481
- block.transactions = transactions;
482
- const validators = await this.staticCall(lib.validators, 'validators');
734
+ if (block.transactions.length === 0) return
735
+ const validators = await this.staticCall(addresses.validators, 'validators');
483
736
  console.log({validators});
484
737
  // block.validators = Object.keys(block.validators).reduce((set, key) => {
485
738
  // if (block.validators[key].active) {
@@ -496,7 +749,7 @@ class Chain {
496
749
  if (validators[validator].active) {
497
750
  const peer = peers[validator];
498
751
  if (peer && peer.connected) {
499
- let data = await new lib$1.BWRequestMessage();
752
+ let data = await new BWRequestMessage();
500
753
  const node = await peernet.prepareMessage(validator, data.encoded);
501
754
  try {
502
755
  const bw = await peer.request(node.encoded);
@@ -520,7 +773,7 @@ class Chain {
520
773
 
521
774
  }
522
775
 
523
- console.log(block.validators);
776
+ console.log({validators: block.validators});
524
777
 
525
778
  block.reward = 150;
526
779
  block.validators = block.validators.map(validator => {
@@ -528,7 +781,7 @@ class Chain {
528
781
  delete validator.bw;
529
782
  return validator
530
783
  });
531
- // block.validators = lib.calculateValidatorReward(block.validators, block.fees)
784
+ // block.validators = calculateValidatorReward(block.validators, block.fees)
532
785
 
533
786
  block.index = this.lastBlock?.index;
534
787
  if (block.index === undefined) block.index = 0;
@@ -552,25 +805,48 @@ class Chain {
552
805
  block.fees = Number.parseFloat(String(block.fees)).toFixed(decimals);
553
806
 
554
807
  try {
555
- let blockMessage = await new lib$1.BlockMessage(block);
556
- this.#lastBlock = {...block, hash: blockMessage.hash};
808
+ let blockMessage = await new BlockMessage(block);
809
+ const hash = await blockMessage.hash;
810
+ await Promise.all(blockMessage.decoded.transactions
811
+ .map(async transaction => transactionPoolStore.delete(await transaction.hash)));
812
+
813
+ this.#lastBlock = { hash, ...blockMessage.decoded };
814
+ await blockStore.put(hash, blockMessage.encoded);
815
+ await chainStore.put('lastBlock', new TextEncoder().encode(hash));
816
+ debug(`created block: ${hash}`);
817
+
557
818
  peernet.publish('add-block', blockMessage.encoded);
558
- this.#addBlock(blockMessage.encoded);
819
+ this.#updateState(blockMessage);
559
820
  } catch (e) {
821
+ console.log(e);
560
822
  throw Error(`invalid block ${block}`)
561
823
  }
562
824
  // data = await this.#machine.execute(to, method, params)
563
825
  // transactionStore.put(message.hash, message.encoded)
564
826
  }
565
827
 
566
- async getNonce(address) {
828
+ async promiseTransactions(transactions) {
829
+ transactions = await Promise.all(transactions.map(tx => new TransactionMessage(tx)));
830
+ return transactions
831
+ }
832
+
833
+ async promiseTransactionsContent(transactions) {
834
+ transactions = await Promise.all(transactions.map(tx => new Promise(async (resolve, reject) => {
835
+ resolve({ ...tx.decoded, hash: await tx.hash });
836
+ })));
837
+
838
+ return transactions
839
+ }
840
+
841
+ async #getNonceFallback(address) {
567
842
  let transactions = await transactionPoolStore.get();
568
- transactions = Object.keys(transactions).map(async tx => await new lib$1.TransactionMessage(transactions[tx]));
843
+ transactions = await this.promiseTransactions(Object.values(transactions));
569
844
  transactions = transactions.filter(tx => tx.decoded.from === address);
570
- transactions = transactions.map(tx => { return {...tx.decoded, hash: tx.hash} });
845
+ transactions = await this.promiseTransactionsContent(transactions);
846
+
571
847
  if (this.lastBlock && transactions.length === 0) {
572
848
  let block = await peernet.get(this.lastBlock.hash);
573
- block = await new lib$1.BlockMessage(block);
849
+ block = await new BlockMessage(block);
574
850
 
575
851
  // for (let tx of block.decoded?.transactions) {
576
852
  // tx = await peernet.get(tx, 'transaction')
@@ -579,7 +855,7 @@ class Chain {
579
855
  transactions = transactions.filter(tx => tx.from === address);
580
856
  while (transactions.length === 0 && block.decoded.index !== 0) {
581
857
  block = await blockStore.get(block.decoded.previousHash);
582
- block = await new lib$1.BlockMessage(block);
858
+ block = await new BlockMessage(block);
583
859
  transactions = block.decoded.transactions.filter(tx => tx.from === address);
584
860
  }
585
861
 
@@ -590,6 +866,16 @@ class Chain {
590
866
  return transactions[transactions.length - 1].nonce
591
867
  }
592
868
 
869
+ async getNonce(address) {
870
+ if (!await accountsStore.has(address)) {
871
+ const nonce = await this.#getNonceFallback(address);
872
+ await accountsStore.put(address, new TextEncoder().encode(String(nonce)));
873
+ }
874
+ let nonce = await accountsStore.get(address);
875
+ nonce = new TextDecoder().decode(nonce);
876
+ return Number(nonce)
877
+ }
878
+
593
879
  /**
594
880
  * whenever method = createContract params should hold the contract hash
595
881
  *
@@ -601,9 +887,74 @@ class Chain {
601
887
  * @param {Array} params - array of paramters to apply to the contract method
602
888
  * @param {Number} nonce - total transaction count [optional]
603
889
  */
604
- createTransaction(to, method, params, nonce) {
890
+ async createTransaction(to, method, params, nonce, signature) {
605
891
  return this.createTransactionFrom(peernet.id, to, method, params, nonce)
606
892
  }
893
+
894
+
895
+
896
+ /**
897
+ *
898
+ * @param {Object} transaction {}
899
+ * @param {String} transaction.from address
900
+ * @param {String} transaction.to address
901
+ * @param {Object} transaction.params {}
902
+ * @param {String} transaction.params.method get, call
903
+ * @param {Buffer} transaction.params.data
904
+ * @returns
905
+ */
906
+ async createTransactionHash(transaction) {
907
+ // todo: validate
908
+ const peernetHash = await new index.CodecHash(transaction, {name: 'transaction-message'});
909
+ return peernetHash.digest
910
+ }
911
+
912
+ /**
913
+ * @params {object} transaction -
914
+ * @params {object} wallet - any wallet/signer that supports sign(RAWtransaction)
915
+ */
916
+ async #signTransaction (transaction, wallet) {
917
+ return wallet.sign(await this.createTransactionHash(transaction))
918
+ }
919
+
920
+ async signTransaction(transaction, signer) {
921
+ let identity = await walletStore.get('identity');
922
+ identity = JSON.parse(new TextDecoder().decode(identity));
923
+ const wallet = new MultiWallet__default["default"](peernet.network);
924
+ wallet.recover(identity.mnemonic);
925
+ wallet.account(0).external(0);
926
+ transaction.signature = await this.#signTransaction(transaction, wallet);
927
+ transaction.signature = bs32__default["default"].encode(transaction.signature);
928
+ return transaction
929
+ }
930
+
931
+ /**
932
+ *
933
+ * @param {Object} transaction
934
+ * @param {Address} transaction.from
935
+ * @param {Address} transaction.to
936
+ * @param {String} transaction.method
937
+ * @param {Array} transaction.params
938
+ * @param {Number} transaction.nonce
939
+ *
940
+ * @returns {Object} transaction
941
+ */
942
+ async createRawTransaction(transaction) {
943
+ if (!transaction.from) transaction.from = peernet.id;
944
+ transaction.timestamp = Date.now();
945
+
946
+ if (transaction.nonce === undefined) {
947
+ transaction.nonce = await this.getNonce(transaction.from);
948
+ transaction.nonce += 1;
949
+ await accountsStore.put(transaction.from, new TextEncoder().encode(String(transaction.nonce)));
950
+ } else {
951
+ let nonce = await accountsStore.get(transaction.from);
952
+ nonce = new TextDecoder().decode(nonce);
953
+ if (transaction.nonce < nonce) throw Error(`a transaction with a higher nonce already exists`)
954
+ if (transaction.nonce === nonce) throw Error(`a transaction with the same nonce already exists`)
955
+ }
956
+ return transaction
957
+ }
607
958
  /**
608
959
  * every tx done is trough contracts so no need for amount
609
960
  * data is undefined when nothing is returned
@@ -616,35 +967,45 @@ class Chain {
616
967
  * @param {Number} nonce - total transaction count [optional]
617
968
  */
618
969
  async createTransactionFrom(from, to, method, params, nonce) {
619
- if (nonce === undefined) {
620
- nonce = await this.getNonce(from);
621
- nonce += 1;
622
- }
623
-
624
- let data;
625
- let message = await new lib$1.TransactionMessage({timestamp: new Date().getTime(), from, to, nonce, method, params});
626
970
  try {
971
+
972
+ const rawTransaction = await this.createRawTransaction({from, to, nonce, method, params});
973
+ const transaction = await this.signTransaction(rawTransaction, from);
974
+ const message = await new TransactionMessage(transaction);
975
+
976
+ let data;
627
977
  // await transactionPoolStore.put(message.hash, new TextEncoder().encode(JSON.stringify({signature, message: message.encoded})))
628
- await transactionPoolStore.put(message.hash, message.encoded);
978
+ const wait = () => new Promise(async (resolve, reject) => {
979
+ if (pubsub.subscribers[`transaction.completed.${await message.hash}`]) {
980
+ const result = pubsub.subscribers[`transaction.completed.${await message.hash}`].value;
981
+ result.status === 'fulfilled' ? resolve(await result.hash) : reject({hash: await result.hash, error: result.error});
982
+ } else {
983
+ const completed = async result => {
984
+ result.status === 'fulfilled' ? resolve(await result.hash) : reject({hash: await result.hash, error: result.error});
985
+
986
+ setTimeout(async () => {
987
+ pubsub.unsubscribe(`transaction.completed.${await message.hash}`, completed);
988
+ }, 10000);
989
+ };
990
+ pubsub.subscribe(`transaction.completed.${await message.hash}`, completed);
991
+ }
992
+
993
+
994
+ });
995
+
996
+ await transactionPoolStore.put(await message.hash, message.encoded);
629
997
  peernet.publish('add-transaction', message.encoded);
998
+ this.#addTransaction(message.encoded);
999
+ return {hash: await message.hash, data, fee: await calculateFee(message.decoded), wait}
630
1000
  } catch (e) {
1001
+ console.log(e);
631
1002
  throw e
632
1003
  }
633
- const wait = () => new Promise((resolve, reject) => {
634
- const completed = result => {
635
- result.status === 'fulfilled' ? resolve(result.hash) : reject({hash: result.hash, error: result.error});
636
-
637
- pubsub.unsubscribe(`transaction.completed.${message.hash}`, completed);
638
- };
639
- pubsub.subscribe(`transaction.completed.${message.hash}`, completed);
640
- });
641
-
642
- if (await this.hasTransactionToHandle() && !this.#runningEpoch) this.#runEpoch();
643
- return {hash: message.hash, data, fee: lib.calculateFee(message), wait}
1004
+
644
1005
  }
645
1006
 
646
1007
  async createContractMessage(creator, contract, constructorParameters = []) {
647
- return new lib$1.ContractMessage({
1008
+ return new ContractMessage({
648
1009
  creator,
649
1010
  contract,
650
1011
  constructorParameters
@@ -666,7 +1027,7 @@ class Chain {
666
1027
  const hash = await this.createContractAddress(creator, contract, params);
667
1028
 
668
1029
  try {
669
- const tx = await this.createTransactionFrom(peernet.id, lib.contractFactory, 'deployContract', [hash, creator, contract, constructorParameters]);
1030
+ const tx = await this.createTransactionFrom(peernet.id, addresses.contractFactory, 'deployContract', [hash, creator, contract, constructorParameters]);
670
1031
  } catch (e) {
671
1032
  throw e
672
1033
  }
@@ -713,15 +1074,15 @@ class Chain {
713
1074
  }
714
1075
 
715
1076
  mint(to, amount) {
716
- return this.call(lib.nativeToken, 'mint', [to, amount])
1077
+ return this.call(addresses.nativeToken, 'mint', [to, amount])
717
1078
  }
718
1079
 
719
1080
  transfer(from, to, amount) {
720
- return this.call(lib.nativeToken, 'transfer', [from, to, amount])
1081
+ return this.call(addresses.nativeToken, 'transfer', [from, to, amount])
721
1082
  }
722
1083
 
723
1084
  get balances() {
724
- return this.staticCall(lib.nativeToken, 'balances')
1085
+ return this.staticCall(addresses.nativeToken, 'balances')
725
1086
  }
726
1087
 
727
1088
  deleteAll() {
@@ -738,7 +1099,7 @@ class Chain {
738
1099
  * @example chain.lookup('myCoolContractName') // qmqsfddfdgfg...
739
1100
  */
740
1101
  lookup(name) {
741
- return this.call(lib.nameService, 'lookup', [name])
1102
+ return this.call(addresses.nameService, 'lookup', [name])
742
1103
  }
743
1104
  }
744
1105