@ethernity-cloud/sdk-js 1.6.1 → 1.7.0

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 (52) hide show
  1. package/README.md +44 -6
  2. package/cas/config.js +61 -3
  3. package/info.js +333 -324
  4. package/init.js +20 -4
  5. package/nodenithy/build/securelock/Dockerfile.tmpl +34 -32
  6. package/nodenithy/build/securelock/scripts/binary-fs-build.sh +7 -6
  7. package/nodenithy/build/securelock/src/securelock.js.tmpl +5 -2
  8. package/nodenithy/build.mjs +83 -71
  9. package/nodenithy/run/docker-compose-final.yml.tmpl +36 -31
  10. package/nodenithy/run/docker-compose.yml.tmpl +34 -45
  11. package/nodenithy/run/image_registry.js +396 -380
  12. package/nodenithy/run.js +42 -28
  13. package/package.json +5 -3
  14. package/publish.js +4 -2
  15. package/pynithy/build/securelock/Dockerfile.base.tpl +13 -0
  16. package/pynithy/build/securelock/Dockerfile.tpl +131 -0
  17. package/pynithy/build/securelock/scripts/binary-fs-build.sh +25 -34
  18. package/pynithy/build/securelock/scripts/pyfreeze.sh +32 -0
  19. package/pynithy/build/securelock/src/{pox.abi → abi/etnyImplementationV2.abi} +1489 -1489
  20. package/pynithy/build/securelock/src/abi/image_registry.abi +1 -0
  21. package/pynithy/build/securelock/src/ecld_state.py +906 -0
  22. package/pynithy/build/securelock/src/esr.abi +1 -0
  23. package/pynithy/build/securelock/src/esr_local.py +267 -0
  24. package/pynithy/build/securelock/src/esr_wallet.py +150 -0
  25. package/pynithy/build/securelock/src/etny_crypto.py +208 -167
  26. package/pynithy/build/securelock/src/etny_exec.py +385 -79
  27. package/pynithy/build/securelock/src/etny_exec_flask.py +535 -0
  28. package/pynithy/build/securelock/src/etny_exec_serv.py +353 -0
  29. package/pynithy/build/securelock/src/etny_session_exec.py +245 -0
  30. package/pynithy/build/securelock/src/get_sgx_report.so +0 -0
  31. package/pynithy/build/securelock/src/image_registry.abi +1 -1
  32. package/pynithy/build/securelock/src/key_generation.py +17 -17
  33. package/pynithy/build/securelock/src/local_api.py +189 -0
  34. package/pynithy/build/securelock/src/models.py +200 -176
  35. package/pynithy/build/securelock/src/securelock.py.tmpl +1037 -727
  36. package/pynithy/build/securelock/src/swift_stream_service.py +206 -186
  37. package/pynithy/build.mjs +142 -92
  38. package/pynithy/run/docker-compose-final.yml.tmpl +58 -51
  39. package/pynithy/run/docker-compose.yml.tmpl +62 -48
  40. package/pynithy/run/etny-securelock-test.yaml.tpl +4 -1
  41. package/pynithy/run/image_registry.js +396 -380
  42. package/pynithy/run.js +44 -28
  43. package/pynithy/src/serverless/Dockerfile.serverless +8 -0
  44. package/pynithy/src/serverless/requirements.txt +6 -0
  45. package/run.js +69 -28
  46. package/nodenithy/build/las/Dockerfile +0 -5
  47. package/pynithy/build/docker-compose.yml +0 -70
  48. package/pynithy/build/las/Dockerfile +0 -11
  49. package/pynithy/build/las/src/etny_entrypoint.sh +0 -6
  50. package/pynithy/build/las/src/las_entrypoint.sh +0 -26
  51. package/pynithy/build/scripts/binary-fs-build.sh +0 -28
  52. package/pynithy/build/securelock/Dockerfile.tmpl +0 -95
@@ -1,381 +1,397 @@
1
- const Web3 = require('web3');
2
- const { ethers } = require('ethers');
3
- const { Account } = require('eth-lib/lib/account');
4
- const { config } = require('dotenv');
5
- const fs = require('fs');
6
- const path = require('path');
7
-
8
- config();
9
-
10
- const PRIVATE_KEY = process.env.PRIVATE_KEY || "";
11
- let BLOCKCHAIN_NETWORK = process.env.BLOCKCHAIN_NETWORK || "Bloxberg_Testnet";
12
- let NETWORK_RPC = "https://bloxberg.ethernity.cloud";
13
- let IMAGE_REGISTRY_ADDRESS = "0x15D73a742529C3fb11f3FA32EF7f0CC3870ACA31"; // bloxberg testnet
14
- let CHAIN_ID = 8995;
15
- let GAS = 9000000;
16
- let GAS_PRICE = 1;
17
-
18
- function setVars(network = "") {
19
- if (BLOCKCHAIN_NETWORK.includes("Bloxberg")) {
20
- IMAGE_REGISTRY_ADDRESS = "0x15D73a742529C3fb11f3FA32EF7f0CC3870ACA31";
21
- } else if (BLOCKCHAIN_NETWORK.includes("Polygon")) {
22
- if (BLOCKCHAIN_NETWORK.includes("Mainnet")) {
23
- NETWORK_RPC = "https://polygon-rpc.com";
24
- IMAGE_REGISTRY_ADDRESS = "0x689f3806874d3c8A973f419a4eB24e6fBA7E830F";
25
- CHAIN_ID = 137;
26
- GAS = 20000000;
27
- GAS_PRICE = 40500500010;
28
- } else {
29
- NETWORK_RPC = "https://polygon-amoy-bor-rpc.publicnode.com";
30
- IMAGE_REGISTRY_ADDRESS = "0xF7F4eEb3d9a64387F4AcEb6d521b948E6E2fB049";
31
- CHAIN_ID = 80002;
32
- GAS = 20000000;
33
- GAS_PRICE = 1300000010;
34
- }
35
- }
36
- }
37
-
38
- setVars();
39
-
40
- function isStringPrivateKey(privateKey) {
41
- try {
42
- let key = privateKey;
43
- if (!key.startsWith("0x")) {
44
- key = `0x${privateKey}`;
45
- }
46
-
47
- Web3.eth.accounts.privateKeyToAccount(key);
48
- return "OK";
49
- } catch (e) {
50
- return e.toString();
51
- }
52
- }
53
-
54
- async function checkAccountBalance() {
55
- try {
56
- const web3 = new ethers.providers.JsonRpcProvider(NETWORK_RPC);
57
- const account = new ethers.Wallet(PRIVATE_KEY, this.provider);
58
- const balance = await web3.getBalance(account.address);
59
- return Web3.utils.fromWei(balance, 'ether');
60
- } catch (e) {
61
- console.error(e);
62
- return 0;
63
- }
64
- }
65
-
66
- class ImageRegistry {
67
- constructor() {
68
- try {
69
- this.imageRegistryAbi = this.readContractAbi('image_registry.abi');
70
- this.imageRegistryAddress = IMAGE_REGISTRY_ADDRESS;
71
- console.log("imageRegistryAddress: ", this.imageRegistryAddress);
72
- this.provider = new ethers.providers.JsonRpcProvider(NETWORK_RPC);
73
-
74
- if (PRIVATE_KEY) {
75
- this.acct = new ethers.Wallet(PRIVATE_KEY, this.provider);
76
- } else {
77
- const _privateKey = new ethers.Wallet.createRandom().privateKey;
78
- this.acct = new ethers.Wallet(_privateKey, this.provider);
79
- }
80
- this.imageRegistryContract = new ethers.Contract(
81
- this.imageRegistryAddress,
82
- this.imageRegistryAbi,
83
- this.acct
84
- );
85
- } catch (e) {
86
- console.error(e);
87
- }
88
- }
89
-
90
- readContractAbi(contractName) {
91
- const filePath = path.join(__dirname, contractName);
92
- return JSON.parse(fs.readFileSync(filePath, 'utf8'));
93
- }
94
-
95
- async addTrustedZoneCert(certContent, ipfsHash, imageName, dockerComposeHash, enclaveNameTrustedZone, fee) {
96
- console.log("Adding trusted zone cert to image registry");
97
- const nonce = await this.provider.getTransactionCount(this.acct.address);
98
-
99
- const unicornTxn = this.imageRegistryContract.addTrustedZoneImage(
100
- ipfsHash, certContent, "v3", imageName, dockerComposeHash, enclaveNameTrustedZone, fee
101
- ).send({
102
- gas: GAS,
103
- chainId: CHAIN_ID,
104
- nonce: nonce,
105
- gasPrice: GAS_PRICE === 1 ? ethers.utils.parseUnits('1', 'mwei') : GAS_PRICE
106
- });
107
-
108
- console.log("transaction status: ", receipt.status);
109
- console.log("transaction receipt: ", receipt);
110
- if (receipt.status === 1) {
111
- console.log("Adding trusted zone cert transaction was successful!");
112
- } else {
113
- console.log("Adding trusted zone cert transaction was UNSUCCESSFUL!");
114
- }
115
- const signedTxn = await this.acct.signTransaction(unicornTxn);
116
- const receipt = await this.provider.sendTransaction(signedTxn.rawTransaction);
117
- }
118
-
119
- async addSecureLockImageCert(certContent, ipfsHash, imageName, version, dockerComposeHash, enclaveNameSecureLock, fee) {
120
- try {
121
- console.log("Adding secure lock image cert to image registry");
122
- // Fetch current nonce
123
- if (BLOCKCHAIN_NETWORK.includes("Polygon")) {
124
- console.log("Polygon Mainnet");
125
- let nonce = await this.provider.getTransactionCount(this.acct.address, 'pending');
126
- // Fetch current gas price and increase it
127
- let gasPrice = await this.provider.getGasPrice();
128
- gasPrice = gasPrice.mul(ethers.BigNumber.from(110)).div(ethers.BigNumber.from(100));
129
- const unicornTxn = await this.imageRegistryContract.addImage(
130
- ipfsHash, certContent, version, imageName, dockerComposeHash, enclaveNameSecureLock, fee,
131
- {
132
- nonce: nonce,
133
- gasPrice: gasPrice,
134
- });
135
- // console.log("Transaction: ", unicornTxn);
136
- const receipt = await this.imageRegistryContract.provider.waitForTransaction(unicornTxn.hash);
137
-
138
- // console.log("transaction status: ", receipt.status);
139
- console.log("transaction receipt: ", unicornTxn.hash);
140
- if (receipt.status === 1) {
141
- console.log("Adding secure lock image cert transaction was successful!");
142
- } else {
143
- // console.log("receipt.status", receipt.status)
144
- console.log("Image certificates already exist for this image!");
145
- }
146
- } else {
147
- const unicornTxn = await this.imageRegistryContract.addImage(
148
- ipfsHash, certContent, version, imageName, dockerComposeHash, enclaveNameSecureLock, fee);
149
- // console.log("Transaction: ", unicornTxn);
150
- const receipt = await this.imageRegistryContract.provider.waitForTransaction(unicornTxn.hash);
151
-
152
- // console.log("transaction status: ", receipt.status);
153
- console.log("transaction receipt: ", unicornTxn.hash);
154
- if (receipt.status === 1) {
155
- console.log("Adding secure lock image cert transaction was successful!");
156
- } else {
157
- // console.log("receipt.status", receipt.status)
158
- console.log("Image certificates already exist for this image!");
159
- }
160
- }
161
-
162
- // console.log("this.acct.address: ", this.acct.address);
163
- // console.log("private key", PRIVATE_KEY);
164
- // console.log('Getting nonce');
165
- // const nonce = await this.imageRegistryContract.provider.getTransactionCount(this.acct.address);
166
-
167
-
168
- // const signedTxn = await this.acct.signTransaction(unicornTxn);
169
- // const receipt = await this.provider.sendTransaction(signedTxn.rawTransaction);
170
- } catch (e) {
171
- // console.error(e);
172
- console.log("Image certificates already exist for this image!");
173
- }
174
- }
175
-
176
- async getImagePublicKeyCert(ipfsHash) {
177
- try {
178
- console.log("Getting image cert from image registry");
179
- // console.log("this.acct.address: ", this.acct.address);
180
- // console.log("private key", PRIVATE_KEY);
181
- // console.log('Getting nonce');
182
- // const nonce = await this.imageRegistryContract.provider.getTransactionCount(this.acct.address);
183
- const unicornTxn = await this.imageRegistryContract.getImageCertPublicKey(ipfsHash);
184
- console.log("unicornTxn: ", unicornTxn);
185
- // const receipt = await this.imageRegistryContract.provider.waitForTransaction(unicornTxn.hash);
186
-
187
- // console.log("transaction status: ", receipt.status);
188
- // // console.log("transaction receipt: ", unicornTxn);
189
- // if (receipt.status === 1) {
190
- // console.log("");
191
- // } else {
192
- // console.log("Adding secure lock image cert transaction was UNSUCCESSFUL!");
193
- // }
194
- // const signedTxn = await this.acct.signTransaction(unicornTxn);
195
- // const receipt = await this.provider.sendTransaction(signedTxn.rawTransaction);
196
- } catch (e) {
197
- console.error(e);
198
- console.log("Getting image cert transaction was UNSUCCESSFUL!");
199
- }
200
- }
201
-
202
- async validateSecureLockImageCert(certContent, ipfsHash, imageName, dockerComposeHash, enclaveNameSecureLock, fee) {
203
- console.log("Validating secure lock image cert in image registry");
204
- const nonce = await this.provider.getTransactionCount(this.acct.address);
205
- const unicornTxn = this.imageRegistryContract.validateSecureLockImage(
206
- ipfsHash, certContent, "v3", imageName, dockerComposeHash, enclaveNameSecureLock, fee
207
- ).send({
208
- gas: GAS,
209
- chainId: CHAIN_ID,
210
- nonce: nonce,
211
- gasPrice: GAS_PRICE === 1 ? ethers.utils.parseUnits('1', 'mwei') : GAS_PRICE
212
- });
213
-
214
- console.log("transaction status: ", receipt.status);
215
- console.log("transaction receipt: ", receipt);
216
- if (receipt.status === 1) {
217
- console.log("Validating secure lock image cert transaction was successful!");
218
- } else {
219
- console.log("Validating secure lock image cert transaction was UNSUCCESSFUL!");
220
- }
221
- const signedTxn = await this.acct.signTransaction(unicornTxn);
222
- const receipt = await this.provider.sendTransaction(signedTxn.rawTransaction);
223
- }
224
-
225
- async addSecureLockAndValidateImageCert(certContent, ipfsHash, imageName, dockerComposeHash, enclaveNameSecureLock, fee) {
226
- console.log("Adding and validating secure lock image cert in image registry");
227
- const nonce = await this.provider.getTransactionCount(this.acct.address);
228
- const unicornTxn = this.imageRegistryContract.addSecureLockAndValidateImage(
229
- ipfsHash, certContent, "v3", imageName, dockerComposeHash, enclaveNameSecureLock, fee
230
- ).send({
231
- gas: GAS,
232
- chainId: CHAIN_ID,
233
- nonce: nonce,
234
- gasPrice: GAS_PRICE === 1 ? ethers.utils.parseUnits('1', 'mwei') : GAS_PRICE
235
- });
236
-
237
- console.log("transaction status: ", receipt.status);
238
- console.log("transaction receipt: ", receipt);
239
- if (receipt.status === 1) {
240
- console.log("Adding and validating secure lock image cert transaction was successful!");
241
- } else {
242
- console.log("Adding and validating secure lock image cert transaction was UNSUCCESSFUL!");
243
- }
244
- const signedTxn = await this.acct.signTransaction(unicornTxn);
245
- const receipt = await this.provider.sendTransaction(signedTxn.rawTransaction);
246
- }
247
-
248
- async getTrustedZoneCert(ipfsHash) {
249
- const cert = await this.imageRegistryContract.getTrustedZoneCert(ipfsHash);
250
- return cert;
251
- }
252
-
253
- async getSecureLockCert(ipfsHash) {
254
- const cert = await this.imageRegistryContract.getSecureLockCert(ipfsHash);
255
- return cert;
256
- }
257
-
258
- async getImageDetails(ipfsHash) {
259
- try {
260
- const details = await this.imageRegistryContract.imageDetails(ipfsHash);
261
- return details;
262
- } catch (e) {
263
- return ['', '', ''];
264
- }
265
- }
266
-
267
- async _getLatestImageVersionPublicKey(projectName, version) {
268
- try {
269
- // console.log("Getting latest image version public key");
270
- const publicKey = await this.imageRegistryContract.getLatestImageVersionPublicKey(projectName, version);
271
- // console.log("public key: ", publicKey);
272
- return publicKey;
273
- } catch (e) {
274
- // console.error(e);
275
- return ['', '', ''];
276
- }
277
- }
278
- }
279
-
280
- (async () => {
281
- try {
282
- const [networkName, projectName, version, privateKey, action] = process.argv.slice(2);
283
- if (action === "validateAddress") {
284
- if (privateKey) {
285
- console.log(isStringPrivateKey(privateKey));
286
- }
287
- process.exit(0);
288
- }
289
- if (networkName && projectName) {
290
- BLOCKCHAIN_NETWORK = networkName;
291
- }
292
- setVars(networkName);
293
- if (action === "checkBalance") {
294
- const balance = await checkAccountBalance();
295
- console.log(`${balance} gas`);
296
- process.exit(0);
297
- }
298
- if (action === 'getTrustedZoneCert') {
299
- const imageRegistry = new ImageRegistry();
300
- const public = (await imageRegistry._getLatestImageVersionPublicKey(projectName, version))[1]
301
- console.log(public);
302
- return public;
303
- }
304
- // The CAS session name recorded for the trustedzone template's latest
305
- // image: what the compose's SCONE_CONFIG_ID for the trustedzone must name.
306
- if (action === 'getTrustedZoneSession') {
307
- const imageRegistry = new ImageRegistry();
308
- const latest = await imageRegistry.imageRegistryContract.getLatestTrustedZoneImageCertPublicKey(projectName, version);
309
- const session = await imageRegistry.imageRegistryContract.getTrustedZoneImageSession(latest[0]);
310
- console.log(session);
311
- return session;
312
- }
313
-
314
- const imageRegistry = new ImageRegistry();
315
- if (action === 'registerSecureLockImage') {
316
- const secureLock = fs.readFileSync("./registry/certificate.securelock.crt", 'utf8');
317
- // console.log("SECURELOCK:", secureLock);
318
- const ipfsHash = process.env.IPFS_HASH || "";
319
- console.log(`ipfsHash: ${ipfsHash}`);
320
- const ipfsDockerComposeHash = process.env.IPFS_DOCKER_COMPOSE_HASH || "";
321
- console.log(`ipfsDockerComposeHash: ${ipfsDockerComposeHash}`);
322
- const imageName = process.env.PROJECT_NAME || "";
323
- console.log(`imageName: ${imageName}`);
324
- const enclaveVersion = process.env.VERSION || "";
325
- const enclaveNameSecureLock = process.env.ENCLAVE_NAME_SECURELOCK || "";
326
- console.log(`enclaveNameSecureLock: ${enclaveNameSecureLock}`);
327
- const fee = process.env.DEVELOPER_FEE || "0";
328
- console.log(`fee: ${fee}`);
329
-
330
- // Register the SAME build under two registry version keys:
331
- // "v3" -> the moving "latest" pointer the runner resolves by
332
- // default (getLatestImageVersionPublicKey(name,"v3")).
333
- // This MUST be "v3" (the protocol version), NOT the
334
- // enclave/template VERSION -- registering under the
335
- // VERSION (e.g. "21") wrote the pointer where the
336
- // runner never looks, so it kept using a stale image.
337
- // <VERSION> -> an immutable per-version entry (e.g. "22") so this
338
- // exact build can be pinned/rolled back to later.
339
- // The contract stores each (name,version) channel append-only.
340
- const versionKeys = ["v3"];
341
- if (enclaveVersion && enclaveVersion !== "v3") versionKeys.push(enclaveVersion);
342
- for (const versionKey of versionKeys) {
343
- console.log(`Registering securelock under version '${versionKey}'`);
344
- await imageRegistry.addSecureLockImageCert(secureLock, ipfsHash, imageName, versionKey, ipfsDockerComposeHash, enclaveNameSecureLock, fee);
345
- }
346
- process.exit(0);
347
- }
348
- if (action === 'getImagePublicKey') {
349
- const ipfsHash = process.env.IPFS_HASH || "";
350
- await imageRegistry.getImagePublicKeyCert(ipfsHash);
351
- process.exit(0);
352
- }
353
- console.log(`Checking image: '${projectName}' on the ${networkName} blockchain...`);
354
- const imageHash = (await imageRegistry._getLatestImageVersionPublicKey(projectName, version))[0];
355
- console.log(`Image hash: ${imageHash}`);
356
- if (!imageHash) {
357
- console.log(`Image: '${projectName}' is available on the ${networkName} blockchain.`);
358
- process.exit(0);
359
- }
360
- const imageOwner = (await imageRegistry.getImageDetails(imageHash))[0];
361
-
362
- if (privateKey) {
363
- if (isStringPrivateKey(privateKey) === "OK") {
364
- const account = Account.fromPrivate(privateKey);
365
- if (imageOwner !== account.address) {
366
- console.log(`!!! Image: '${projectName}' is owned by '${imageOwner}'.\nYou are not the account holder of the image.\nPlease change the project name and try again.\n`);
367
- process.exit(1);
368
- }
369
- }
370
- }
371
-
372
- if (imageOwner) {
373
- console.log(`Image: '${projectName}' is owned by '${imageOwner}'.\nIf you are not the account holder, you will not be able to publish your project with the current name. Please change the project name and try again.\n`);
374
- process.exit(0);
375
- }
376
-
377
- console.log(`Image: '${projectName}' is available on the ${networkName} blockchain.`);
378
- } catch (e) {
379
- process.exit(0);
380
- }
1
+ const Web3 = require('web3');
2
+ const { ethers } = require('ethers');
3
+ const { Account } = require('eth-lib/lib/account');
4
+ const { config } = require('dotenv');
5
+ const fs = require('fs');
6
+ const path = require('path');
7
+
8
+ config();
9
+
10
+ const PRIVATE_KEY = process.env.PRIVATE_KEY || "";
11
+ let BLOCKCHAIN_NETWORK = process.env.BLOCKCHAIN_NETWORK || "Bloxberg_Testnet";
12
+ let NETWORK_RPC = "https://bloxberg.ethernity.cloud";
13
+ let IMAGE_REGISTRY_ADDRESS = "0x99A84C624C028bdf0a855A1E9E3f2fcf7275B3D8"; // bloxberg testnet (ECImageRegistryV2)
14
+ let CHAIN_ID = 8995;
15
+ let GAS = 9000000;
16
+ let GAS_PRICE = 1;
17
+
18
+ function setVars(network = "") {
19
+ if (BLOCKCHAIN_NETWORK.includes("Bloxberg")) {
20
+ // The bloxberg testnet reads ECImageRegistryV2; mainnet the original registry.
21
+ IMAGE_REGISTRY_ADDRESS = BLOCKCHAIN_NETWORK.includes("Testnet")
22
+ ? "0x99A84C624C028bdf0a855A1E9E3f2fcf7275B3D8"
23
+ : "0x15D73a742529C3fb11f3FA32EF7f0CC3870ACA31";
24
+ } else if (BLOCKCHAIN_NETWORK.includes("Polygon")) {
25
+ if (BLOCKCHAIN_NETWORK.includes("Mainnet")) {
26
+ NETWORK_RPC = "https://polygon-rpc.com";
27
+ IMAGE_REGISTRY_ADDRESS = "0x689f3806874d3c8A973f419a4eB24e6fBA7E830F";
28
+ CHAIN_ID = 137;
29
+ GAS = 20000000;
30
+ GAS_PRICE = 40500500010;
31
+ } else {
32
+ NETWORK_RPC = "https://polygon-amoy-bor-rpc.publicnode.com";
33
+ IMAGE_REGISTRY_ADDRESS = "0xF7F4eEb3d9a64387F4AcEb6d521b948E6E2fB049";
34
+ CHAIN_ID = 80002;
35
+ GAS = 20000000;
36
+ GAS_PRICE = 1300000010;
37
+ }
38
+ } else if (BLOCKCHAIN_NETWORK.includes("LitVM")) {
39
+ NETWORK_RPC = "https://liteforge.rpc.caldera.xyz/infra-partner-http";
40
+ IMAGE_REGISTRY_ADDRESS = "0x55e0ad455Be85162b71a790f00Fc305680E3CE53";
41
+ CHAIN_ID = 4441;
42
+ }
43
+ }
44
+
45
+ setVars();
46
+
47
+ function isStringPrivateKey(privateKey) {
48
+ try {
49
+ let key = privateKey;
50
+ if (!key.startsWith("0x")) {
51
+ key = `0x${privateKey}`;
52
+ }
53
+
54
+ Web3.eth.accounts.privateKeyToAccount(key);
55
+ return "OK";
56
+ } catch (e) {
57
+ return e.toString();
58
+ }
59
+ }
60
+
61
+ async function checkAccountBalance() {
62
+ try {
63
+ const web3 = new ethers.providers.JsonRpcProvider(NETWORK_RPC);
64
+ const account = new ethers.Wallet(PRIVATE_KEY, this.provider);
65
+ const balance = await web3.getBalance(account.address);
66
+ return Web3.utils.fromWei(balance, 'ether');
67
+ } catch (e) {
68
+ console.error(e);
69
+ return 0;
70
+ }
71
+ }
72
+
73
+ class ImageRegistry {
74
+ constructor() {
75
+ try {
76
+ this.imageRegistryAbi = this.readContractAbi('image_registry.abi');
77
+ this.imageRegistryAddress = IMAGE_REGISTRY_ADDRESS;
78
+ // stderr: callers read an action's result from stdout.
79
+ console.error("imageRegistryAddress: ", this.imageRegistryAddress);
80
+ this.provider = new ethers.providers.JsonRpcProvider(NETWORK_RPC);
81
+
82
+ if (PRIVATE_KEY) {
83
+ this.acct = new ethers.Wallet(PRIVATE_KEY, this.provider);
84
+ } else {
85
+ const _privateKey = new ethers.Wallet.createRandom().privateKey;
86
+ this.acct = new ethers.Wallet(_privateKey, this.provider);
87
+ }
88
+ this.imageRegistryContract = new ethers.Contract(
89
+ this.imageRegistryAddress,
90
+ this.imageRegistryAbi,
91
+ this.acct
92
+ );
93
+ } catch (e) {
94
+ console.error(e);
95
+ }
96
+ }
97
+
98
+ readContractAbi(contractName) {
99
+ const filePath = path.join(__dirname, contractName);
100
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'));
101
+ }
102
+
103
+ async addTrustedZoneCert(certContent, ipfsHash, imageName, dockerComposeHash, enclaveNameTrustedZone, fee) {
104
+ console.log("Adding trusted zone cert to image registry");
105
+ const nonce = await this.provider.getTransactionCount(this.acct.address);
106
+
107
+ const unicornTxn = this.imageRegistryContract.addTrustedZoneImage(
108
+ ipfsHash, certContent, "v3", imageName, dockerComposeHash, enclaveNameTrustedZone, fee
109
+ ).send({
110
+ gas: GAS,
111
+ chainId: CHAIN_ID,
112
+ nonce: nonce,
113
+ gasPrice: GAS_PRICE === 1 ? ethers.utils.parseUnits('1', 'mwei') : GAS_PRICE
114
+ });
115
+
116
+ console.log("transaction status: ", receipt.status);
117
+ console.log("transaction receipt: ", receipt);
118
+ if (receipt.status === 1) {
119
+ console.log("Adding trusted zone cert transaction was successful!");
120
+ } else {
121
+ console.log("Adding trusted zone cert transaction was UNSUCCESSFUL!");
122
+ }
123
+ const signedTxn = await this.acct.signTransaction(unicornTxn);
124
+ const receipt = await this.provider.sendTransaction(signedTxn.rawTransaction);
125
+ }
126
+
127
+ async addSecureLockImageCert(certContent, ipfsHash, imageName, version, dockerComposeHash, enclaveNameSecureLock, fee) {
128
+ try {
129
+ console.log("Adding secure lock image cert to image registry");
130
+ // Fetch current nonce
131
+ if (BLOCKCHAIN_NETWORK.includes("Polygon")) {
132
+ console.log("Polygon Mainnet");
133
+ let nonce = await this.provider.getTransactionCount(this.acct.address, 'pending');
134
+ // Fetch current gas price and increase it
135
+ let gasPrice = await this.provider.getGasPrice();
136
+ gasPrice = gasPrice.mul(ethers.BigNumber.from(110)).div(ethers.BigNumber.from(100));
137
+ const unicornTxn = await this.imageRegistryContract.addImage(
138
+ ipfsHash, certContent, version, imageName, dockerComposeHash, enclaveNameSecureLock, fee,
139
+ {
140
+ nonce: nonce,
141
+ gasPrice: gasPrice,
142
+ });
143
+ // console.log("Transaction: ", unicornTxn);
144
+ const receipt = await this.imageRegistryContract.provider.waitForTransaction(unicornTxn.hash);
145
+
146
+ // console.log("transaction status: ", receipt.status);
147
+ console.log("transaction receipt: ", unicornTxn.hash);
148
+ if (receipt.status === 1) {
149
+ console.log("Adding secure lock image cert transaction was successful!");
150
+ } else {
151
+ // console.log("receipt.status", receipt.status)
152
+ console.log("Image certificates already exist for this image!");
153
+ }
154
+ } else {
155
+ const unicornTxn = await this.imageRegistryContract.addImage(
156
+ ipfsHash, certContent, version, imageName, dockerComposeHash, enclaveNameSecureLock, fee);
157
+ // console.log("Transaction: ", unicornTxn);
158
+ const receipt = await this.imageRegistryContract.provider.waitForTransaction(unicornTxn.hash);
159
+
160
+ // console.log("transaction status: ", receipt.status);
161
+ console.log("transaction receipt: ", unicornTxn.hash);
162
+ if (receipt.status === 1) {
163
+ console.log("Adding secure lock image cert transaction was successful!");
164
+ } else {
165
+ // console.log("receipt.status", receipt.status)
166
+ console.log("Image certificates already exist for this image!");
167
+ }
168
+ }
169
+
170
+ // console.log("this.acct.address: ", this.acct.address);
171
+ // console.log("private key", PRIVATE_KEY);
172
+ // console.log('Getting nonce');
173
+ // const nonce = await this.imageRegistryContract.provider.getTransactionCount(this.acct.address);
174
+
175
+
176
+ // const signedTxn = await this.acct.signTransaction(unicornTxn);
177
+ // const receipt = await this.provider.sendTransaction(signedTxn.rawTransaction);
178
+ } catch (e) {
179
+ // console.error(e);
180
+ console.log("Image certificates already exist for this image!");
181
+ }
182
+ }
183
+
184
+ async getImagePublicKeyCert(ipfsHash) {
185
+ try {
186
+ console.log("Getting image cert from image registry");
187
+ // console.log("this.acct.address: ", this.acct.address);
188
+ // console.log("private key", PRIVATE_KEY);
189
+ // console.log('Getting nonce');
190
+ // const nonce = await this.imageRegistryContract.provider.getTransactionCount(this.acct.address);
191
+ const unicornTxn = await this.imageRegistryContract.getImageCertPublicKey(ipfsHash);
192
+ console.log("unicornTxn: ", unicornTxn);
193
+ // const receipt = await this.imageRegistryContract.provider.waitForTransaction(unicornTxn.hash);
194
+
195
+ // console.log("transaction status: ", receipt.status);
196
+ // // console.log("transaction receipt: ", unicornTxn);
197
+ // if (receipt.status === 1) {
198
+ // console.log("");
199
+ // } else {
200
+ // console.log("Adding secure lock image cert transaction was UNSUCCESSFUL!");
201
+ // }
202
+ // const signedTxn = await this.acct.signTransaction(unicornTxn);
203
+ // const receipt = await this.provider.sendTransaction(signedTxn.rawTransaction);
204
+ } catch (e) {
205
+ console.error(e);
206
+ console.log("Getting image cert transaction was UNSUCCESSFUL!");
207
+ }
208
+ }
209
+
210
+ async validateSecureLockImageCert(certContent, ipfsHash, imageName, dockerComposeHash, enclaveNameSecureLock, fee) {
211
+ console.log("Validating secure lock image cert in image registry");
212
+ const nonce = await this.provider.getTransactionCount(this.acct.address);
213
+ const unicornTxn = this.imageRegistryContract.validateSecureLockImage(
214
+ ipfsHash, certContent, "v3", imageName, dockerComposeHash, enclaveNameSecureLock, fee
215
+ ).send({
216
+ gas: GAS,
217
+ chainId: CHAIN_ID,
218
+ nonce: nonce,
219
+ gasPrice: GAS_PRICE === 1 ? ethers.utils.parseUnits('1', 'mwei') : GAS_PRICE
220
+ });
221
+
222
+ console.log("transaction status: ", receipt.status);
223
+ console.log("transaction receipt: ", receipt);
224
+ if (receipt.status === 1) {
225
+ console.log("Validating secure lock image cert transaction was successful!");
226
+ } else {
227
+ console.log("Validating secure lock image cert transaction was UNSUCCESSFUL!");
228
+ }
229
+ const signedTxn = await this.acct.signTransaction(unicornTxn);
230
+ const receipt = await this.provider.sendTransaction(signedTxn.rawTransaction);
231
+ }
232
+
233
+ async addSecureLockAndValidateImageCert(certContent, ipfsHash, imageName, dockerComposeHash, enclaveNameSecureLock, fee) {
234
+ console.log("Adding and validating secure lock image cert in image registry");
235
+ const nonce = await this.provider.getTransactionCount(this.acct.address);
236
+ const unicornTxn = this.imageRegistryContract.addSecureLockAndValidateImage(
237
+ ipfsHash, certContent, "v3", imageName, dockerComposeHash, enclaveNameSecureLock, fee
238
+ ).send({
239
+ gas: GAS,
240
+ chainId: CHAIN_ID,
241
+ nonce: nonce,
242
+ gasPrice: GAS_PRICE === 1 ? ethers.utils.parseUnits('1', 'mwei') : GAS_PRICE
243
+ });
244
+
245
+ console.log("transaction status: ", receipt.status);
246
+ console.log("transaction receipt: ", receipt);
247
+ if (receipt.status === 1) {
248
+ console.log("Adding and validating secure lock image cert transaction was successful!");
249
+ } else {
250
+ console.log("Adding and validating secure lock image cert transaction was UNSUCCESSFUL!");
251
+ }
252
+ const signedTxn = await this.acct.signTransaction(unicornTxn);
253
+ const receipt = await this.provider.sendTransaction(signedTxn.rawTransaction);
254
+ }
255
+
256
+ async getTrustedZoneCert(ipfsHash) {
257
+ const cert = await this.imageRegistryContract.getTrustedZoneCert(ipfsHash);
258
+ return cert;
259
+ }
260
+
261
+ async getSecureLockCert(ipfsHash) {
262
+ const cert = await this.imageRegistryContract.getSecureLockCert(ipfsHash);
263
+ return cert;
264
+ }
265
+
266
+ async getImageDetails(ipfsHash) {
267
+ try {
268
+ const details = await this.imageRegistryContract.imageDetails(ipfsHash);
269
+ return details;
270
+ } catch (e) {
271
+ return ['', '', ''];
272
+ }
273
+ }
274
+
275
+ async _getLatestImageVersionPublicKey(projectName, version) {
276
+ try {
277
+ // console.log("Getting latest image version public key");
278
+ const publicKey = await this.imageRegistryContract.getLatestImageVersionPublicKey(projectName, version);
279
+ // console.log("public key: ", publicKey);
280
+ return publicKey;
281
+ } catch (e) {
282
+ // console.error(e);
283
+ return ['', '', ''];
284
+ }
285
+ }
286
+ }
287
+
288
+ (async () => {
289
+ try {
290
+ const [networkName, projectName, version, privateKey, action] = process.argv.slice(2);
291
+ if (action === "validateAddress") {
292
+ if (privateKey) {
293
+ console.log(isStringPrivateKey(privateKey));
294
+ }
295
+ process.exit(0);
296
+ }
297
+ if (networkName && projectName) {
298
+ BLOCKCHAIN_NETWORK = networkName;
299
+ }
300
+ setVars(networkName);
301
+ if (action === "checkBalance") {
302
+ const balance = await checkAccountBalance();
303
+ console.log(`${balance} gas`);
304
+ process.exit(0);
305
+ }
306
+ // The certificate of the trustedzone's own record. getLatestImageVersionPublicKey
307
+ // reads the securelock records instead: ECImageRegistryV2 reverts it for
308
+ // a trustedzone, and the bloxberg mainnet and LitVM registries return a
309
+ // different certificate for the same name.
310
+ if (action === 'getTrustedZoneCert') {
311
+ const imageRegistry = new ImageRegistry();
312
+ try {
313
+ const latest = await imageRegistry.imageRegistryContract.getLatestTrustedZoneImageCertPublicKey(projectName, version);
314
+ console.log(latest[1]);
315
+ } catch (e) {
316
+ console.error(`No trustedzone ${projectName} (${version}) in image registry ${IMAGE_REGISTRY_ADDRESS}: ${e.reason || e.message}`);
317
+ }
318
+ return;
319
+ }
320
+ // The CAS session name recorded for the trustedzone template's latest
321
+ // image: what the compose's SCONE_CONFIG_ID for the trustedzone must name.
322
+ if (action === 'getTrustedZoneSession') {
323
+ const imageRegistry = new ImageRegistry();
324
+ const latest = await imageRegistry.imageRegistryContract.getLatestTrustedZoneImageCertPublicKey(projectName, version);
325
+ const session = await imageRegistry.imageRegistryContract.getTrustedZoneImageSession(latest[0]);
326
+ console.log(session);
327
+ return session;
328
+ }
329
+
330
+ const imageRegistry = new ImageRegistry();
331
+ if (action === 'registerSecureLockImage') {
332
+ const secureLock = fs.readFileSync("./registry/certificate.securelock.crt", 'utf8');
333
+ // console.log("SECURELOCK:", secureLock);
334
+ const ipfsHash = process.env.IPFS_HASH || "";
335
+ console.log(`ipfsHash: ${ipfsHash}`);
336
+ const ipfsDockerComposeHash = process.env.IPFS_DOCKER_COMPOSE_HASH || "";
337
+ console.log(`ipfsDockerComposeHash: ${ipfsDockerComposeHash}`);
338
+ const imageName = process.env.PROJECT_NAME || "";
339
+ console.log(`imageName: ${imageName}`);
340
+ const enclaveVersion = process.env.VERSION || "";
341
+ const enclaveNameSecureLock = process.env.ENCLAVE_NAME_SECURELOCK || "";
342
+ console.log(`enclaveNameSecureLock: ${enclaveNameSecureLock}`);
343
+ const fee = process.env.DEVELOPER_FEE || "0";
344
+ console.log(`fee: ${fee}`);
345
+
346
+ // Register the SAME build under two registry version keys:
347
+ // "v3" -> the moving "latest" pointer the runner resolves by
348
+ // default (getLatestImageVersionPublicKey(name,"v3")).
349
+ // This MUST be "v3" (the protocol version), NOT the
350
+ // enclave/template VERSION -- registering under the
351
+ // VERSION (e.g. "21") wrote the pointer where the
352
+ // runner never looks, so it kept using a stale image.
353
+ // <VERSION> -> an immutable per-version entry (e.g. "22") so this
354
+ // exact build can be pinned/rolled back to later.
355
+ // The contract stores each (name,version) channel append-only.
356
+ const versionKeys = ["v3"];
357
+ if (enclaveVersion && enclaveVersion !== "v3") versionKeys.push(enclaveVersion);
358
+ for (const versionKey of versionKeys) {
359
+ console.log(`Registering securelock under version '${versionKey}'`);
360
+ await imageRegistry.addSecureLockImageCert(secureLock, ipfsHash, imageName, versionKey, ipfsDockerComposeHash, enclaveNameSecureLock, fee);
361
+ }
362
+ process.exit(0);
363
+ }
364
+ if (action === 'getImagePublicKey') {
365
+ const ipfsHash = process.env.IPFS_HASH || "";
366
+ await imageRegistry.getImagePublicKeyCert(ipfsHash);
367
+ process.exit(0);
368
+ }
369
+ console.log(`Checking image: '${projectName}' on the ${networkName} blockchain...`);
370
+ const imageHash = (await imageRegistry._getLatestImageVersionPublicKey(projectName, version))[0];
371
+ console.log(`Image hash: ${imageHash}`);
372
+ if (!imageHash) {
373
+ console.log(`Image: '${projectName}' is available on the ${networkName} blockchain.`);
374
+ process.exit(0);
375
+ }
376
+ const imageOwner = (await imageRegistry.getImageDetails(imageHash))[0];
377
+
378
+ if (privateKey) {
379
+ if (isStringPrivateKey(privateKey) === "OK") {
380
+ const account = Account.fromPrivate(privateKey);
381
+ if (imageOwner !== account.address) {
382
+ console.log(`!!! Image: '${projectName}' is owned by '${imageOwner}'.\nYou are not the account holder of the image.\nPlease change the project name and try again.\n`);
383
+ process.exit(1);
384
+ }
385
+ }
386
+ }
387
+
388
+ if (imageOwner) {
389
+ console.log(`Image: '${projectName}' is owned by '${imageOwner}'.\nIf you are not the account holder, you will not be able to publish your project with the current name. Please change the project name and try again.\n`);
390
+ process.exit(0);
391
+ }
392
+
393
+ console.log(`Image: '${projectName}' is available on the ${networkName} blockchain.`);
394
+ } catch (e) {
395
+ process.exit(0);
396
+ }
381
397
  })();