@ethernity-cloud/sdk-js 1.5.0 → 1.5.2

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.
@@ -60,6 +60,7 @@ function configure({ identityPriv, swiftStreamService, bucket, contractAddress,
60
60
  _bucket = bucket;
61
61
  _contractAddress = contractAddress;
62
62
  _provider = provider;
63
+ _esrLedger = [];
63
64
  }
64
65
 
65
66
  function keccak256(buf) {
@@ -157,11 +158,20 @@ function keyHash(key) {
157
158
 
158
159
  const ESR_ABI = [
159
160
  'function commit(bytes32 key, string newCID, uint256 expectedVersion)',
161
+ 'function commitFor(address enclave, bytes32 key, string newCID, uint256 expectedVersion, uint256 relayNonce, bytes signature)',
162
+ 'function commitDigest(address enclave, bytes32 key, string newCID, uint256 expectedVersion, uint256 relayNonce) view returns (bytes32)',
163
+ 'function relayNonce(address enclave) view returns (uint256)',
160
164
  'function getState(address enclave, bytes32 key) view returns (string cid, uint256 version, uint64 updatedAt)',
161
165
  'function getVersion(address enclave, bytes32 key) view returns (uint256)',
162
166
  'function exists(address enclave, bytes32 key) view returns (bool)'
163
167
  ];
164
168
 
169
+ // Order-wide ledger of signed authorizations. ESR commits do NOT pay their own
170
+ // gas (see contracts/esr/RELAY-DESIGN.md): the enclave only SIGNS each commit
171
+ // (commitFor); the NODE relays it and pays, and does all gas accounting. The
172
+ // securelock does no gas math -- nothing it could claim about cost is trusted.
173
+ let _esrLedger = [];
174
+
165
175
  class StateRegistry {
166
176
  constructor() {
167
177
  if (!_contractAddress) {
@@ -290,9 +300,39 @@ class StateRegistry {
290
300
  }
291
301
 
292
302
  async _sendCommit(keyHashHex, cid, expectedVersion) {
293
- const contract = this._contract(this._signer());
294
- const tx = await contract.commit(keyHashHex, cid, expectedVersion);
295
- return tx.wait();
303
+ // The enclave never pays gas and does NO gas math: it SIGNS the commit
304
+ // (commitFor) and stages the signed authorization for the node, which
305
+ // relays it and pays. The trustedzone independently re-prices the whole
306
+ // ledger and adjudicates. Nothing the securelock could claim about cost is
307
+ // trusted, so it claims nothing.
308
+ const signer = this._signer();
309
+ const enclave = await signer.getAddress();
310
+ const contract = this._contract();
311
+
312
+ const relayNonce = await contract.relayNonce(enclave);
313
+ const digest = await contract.commitDigest(
314
+ enclave, keyHashHex, cid, expectedVersion, relayNonce);
315
+ // Sign the raw 32-byte digest as an eth_sign message (matches the
316
+ // contract's "\x19Ethereum Signed Message:\n32" recovery).
317
+ const signature = await signer.signMessage(ethers.utils.arrayify(digest));
318
+
319
+ const auth = {
320
+ enclave,
321
+ keyHash: keyHashHex,
322
+ cid,
323
+ expectedVersion: Number(expectedVersion),
324
+ relayNonce: Number(relayNonce),
325
+ signature
326
+ };
327
+
328
+ _esrLedger.push(auth);
329
+ await _swift.putFileContent(
330
+ _bucket, 'esr.authorizations.json', '',
331
+ Buffer.from(JSON.stringify(_esrLedger), 'utf8'));
332
+ await _swift.putFileContent(
333
+ _bucket, `esr.commit.${Number(relayNonce)}.json`, '',
334
+ Buffer.from(JSON.stringify(auth), 'utf8'));
335
+ return { relayed: true, relayNonce: Number(relayNonce) };
296
336
  }
297
337
  }
298
338
 
@@ -481,6 +481,40 @@ function initSwiftStreamClient() {
481
481
  return swiftStreamClient;
482
482
  }
483
483
 
484
+ // Set as soon as the enclave object exists so the top-level catch can report a
485
+ // failure result even when the throw happens deep inside execute().
486
+ let currentApp = null;
487
+
488
+ /**
489
+ * Best-effort: write a failure result so the trustedzone stops waiting.
490
+ *
491
+ * Securelock only publishes the result and its code; the trustedzone reads
492
+ * those and builds the signed transaction. Without them that wait runs to its
493
+ * timeout and the whole order stalls with no diagnosis.
494
+ *
495
+ * Guarded throughout: this runs while already handling a failure, so it must
496
+ * never throw or hang.
497
+ */
498
+ async function reportFailure(app, err) {
499
+ if (!app) {
500
+ console.log('cannot report failure: enclave died before initialization');
501
+ return;
502
+ }
503
+ try {
504
+ // Keep a specific code if one was already set (checksum mismatch, ...).
505
+ if (app.task_code === undefined || app.task_code === null || app.task_code === '') {
506
+ app.task_code = TaskStatus.SYSTEM_ERROR;
507
+ }
508
+ if (!app.task_result) {
509
+ app.task_result = `ENCLAVE ERROR: ${err && err.message ? err.message : err}`;
510
+ }
511
+ await app.saveResult();
512
+ console.log(`failure reported (task_code=${app.task_code})`);
513
+ } catch (reportErr) {
514
+ console.log('could not report the failure result:', reportErr);
515
+ }
516
+ }
517
+
484
518
  async function execute() {
485
519
  console.log('########################################');
486
520
  console.log('########################################');
@@ -494,6 +528,9 @@ async function execute() {
494
528
  const swiftStreamClient = initSwiftStreamClient();
495
529
 
496
530
  const app = new EtnySecureLock(swiftStreamClient);
531
+ // Exposed so the top-level handler can still publish a failure result if
532
+ // anything below throws (see reportFailure).
533
+ currentApp = app;
497
534
 
498
535
  // Testnet: materialize the self-signed cert/key from MR_ENCLAVE into the
499
536
  // file paths before anything reads them (harvest + decrypt). No-op on mainnet
@@ -543,5 +580,7 @@ async function execute() {
543
580
  await execute();
544
581
  } catch (e) {
545
582
  console.log(e);
583
+ await reportFailure(currentApp, e);
584
+ process.exitCode = 1;
546
585
  }
547
586
  })();
@@ -15,6 +15,8 @@ class TaskStatus {
15
15
  // address that was never baked into the sealed image). Reported eagerly
16
16
  // with the variable named, instead of a confusing downstream crash.
17
17
  static CONFIG_ERROR = 32;
18
+ static EXECUTION_TIMEOUT = 33; // Started but produced no result within the order duration.
19
+ static ESR_GAS_LIMIT_EXCEEDED = 34; // ESR commits would exceed the per-order relayed-gas budget.
18
20
  }
19
21
 
20
22
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ethernity-cloud/sdk-js",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
4
4
  "description": "A package to run ethernity cloud build scripts",
5
5
  "scripts": {
6
6
  "ecld-build": "cross-env node ./node_modules/@ethernity-cloud/sdk-js/build.js",
@@ -47,4 +47,4 @@
47
47
  },
48
48
  "author": "Alexandru Luca",
49
49
  "license": "AGPL-3.0"
50
- }
50
+ }