@ethernity-cloud/sdk-js 1.5.2 → 1.5.4

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.
@@ -1,372 +1,372 @@
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://rpc-amoy.polygon.technology";
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
-
305
- const imageRegistry = new ImageRegistry();
306
- if (action === 'registerSecureLockImage') {
307
- const secureLock = fs.readFileSync("./registry/certificate.securelock.crt", 'utf8');
308
- // console.log("SECURELOCK:", secureLock);
309
- const ipfsHash = process.env.IPFS_HASH || "";
310
- console.log(`ipfsHash: ${ipfsHash}`);
311
- const ipfsDockerComposeHash = process.env.IPFS_DOCKER_COMPOSE_HASH || "";
312
- console.log(`ipfsDockerComposeHash: ${ipfsDockerComposeHash}`);
313
- const imageName = process.env.PROJECT_NAME || "";
314
- console.log(`imageName: ${imageName}`);
315
- const enclaveVersion = process.env.VERSION || "";
316
- const enclaveNameSecureLock = process.env.ENCLAVE_NAME_SECURELOCK || "";
317
- console.log(`enclaveNameSecureLock: ${enclaveNameSecureLock}`);
318
- const fee = process.env.DEVELOPER_FEE || "0";
319
- console.log(`fee: ${fee}`);
320
-
321
- // Register the SAME build under two registry version keys:
322
- // "v3" -> the moving "latest" pointer the runner resolves by
323
- // default (getLatestImageVersionPublicKey(name,"v3")).
324
- // This MUST be "v3" (the protocol version), NOT the
325
- // enclave/template VERSION -- registering under the
326
- // VERSION (e.g. "21") wrote the pointer where the
327
- // runner never looks, so it kept using a stale image.
328
- // <VERSION> -> an immutable per-version entry (e.g. "22") so this
329
- // exact build can be pinned/rolled back to later.
330
- // The contract stores each (name,version) channel append-only.
331
- const versionKeys = ["v3"];
332
- if (enclaveVersion && enclaveVersion !== "v3") versionKeys.push(enclaveVersion);
333
- for (const versionKey of versionKeys) {
334
- console.log(`Registering securelock under version '${versionKey}'`);
335
- await imageRegistry.addSecureLockImageCert(secureLock, ipfsHash, imageName, versionKey, ipfsDockerComposeHash, enclaveNameSecureLock, fee);
336
- }
337
- process.exit(0);
338
- }
339
- if (action === 'getImagePublicKey') {
340
- const ipfsHash = process.env.IPFS_HASH || "";
341
- await imageRegistry.getImagePublicKeyCert(ipfsHash);
342
- process.exit(0);
343
- }
344
- console.log(`Checking image: '${projectName}' on the ${networkName} blockchain...`);
345
- const imageHash = (await imageRegistry._getLatestImageVersionPublicKey(projectName, version))[0];
346
- console.log(`Image hash: ${imageHash}`);
347
- if (!imageHash) {
348
- console.log(`Image: '${projectName}' is available on the ${networkName} blockchain.`);
349
- process.exit(0);
350
- }
351
- const imageOwner = (await imageRegistry.getImageDetails(imageHash))[0];
352
-
353
- if (privateKey) {
354
- if (isStringPrivateKey(privateKey) === "OK") {
355
- const account = Account.fromPrivate(privateKey);
356
- if (imageOwner !== account.address) {
357
- 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`);
358
- process.exit(1);
359
- }
360
- }
361
- }
362
-
363
- if (imageOwner) {
364
- 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`);
365
- process.exit(0);
366
- }
367
-
368
- console.log(`Image: '${projectName}' is available on the ${networkName} blockchain.`);
369
- } catch (e) {
370
- process.exit(0);
371
- }
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
+
305
+ const imageRegistry = new ImageRegistry();
306
+ if (action === 'registerSecureLockImage') {
307
+ const secureLock = fs.readFileSync("./registry/certificate.securelock.crt", 'utf8');
308
+ // console.log("SECURELOCK:", secureLock);
309
+ const ipfsHash = process.env.IPFS_HASH || "";
310
+ console.log(`ipfsHash: ${ipfsHash}`);
311
+ const ipfsDockerComposeHash = process.env.IPFS_DOCKER_COMPOSE_HASH || "";
312
+ console.log(`ipfsDockerComposeHash: ${ipfsDockerComposeHash}`);
313
+ const imageName = process.env.PROJECT_NAME || "";
314
+ console.log(`imageName: ${imageName}`);
315
+ const enclaveVersion = process.env.VERSION || "";
316
+ const enclaveNameSecureLock = process.env.ENCLAVE_NAME_SECURELOCK || "";
317
+ console.log(`enclaveNameSecureLock: ${enclaveNameSecureLock}`);
318
+ const fee = process.env.DEVELOPER_FEE || "0";
319
+ console.log(`fee: ${fee}`);
320
+
321
+ // Register the SAME build under two registry version keys:
322
+ // "v3" -> the moving "latest" pointer the runner resolves by
323
+ // default (getLatestImageVersionPublicKey(name,"v3")).
324
+ // This MUST be "v3" (the protocol version), NOT the
325
+ // enclave/template VERSION -- registering under the
326
+ // VERSION (e.g. "21") wrote the pointer where the
327
+ // runner never looks, so it kept using a stale image.
328
+ // <VERSION> -> an immutable per-version entry (e.g. "22") so this
329
+ // exact build can be pinned/rolled back to later.
330
+ // The contract stores each (name,version) channel append-only.
331
+ const versionKeys = ["v3"];
332
+ if (enclaveVersion && enclaveVersion !== "v3") versionKeys.push(enclaveVersion);
333
+ for (const versionKey of versionKeys) {
334
+ console.log(`Registering securelock under version '${versionKey}'`);
335
+ await imageRegistry.addSecureLockImageCert(secureLock, ipfsHash, imageName, versionKey, ipfsDockerComposeHash, enclaveNameSecureLock, fee);
336
+ }
337
+ process.exit(0);
338
+ }
339
+ if (action === 'getImagePublicKey') {
340
+ const ipfsHash = process.env.IPFS_HASH || "";
341
+ await imageRegistry.getImagePublicKeyCert(ipfsHash);
342
+ process.exit(0);
343
+ }
344
+ console.log(`Checking image: '${projectName}' on the ${networkName} blockchain...`);
345
+ const imageHash = (await imageRegistry._getLatestImageVersionPublicKey(projectName, version))[0];
346
+ console.log(`Image hash: ${imageHash}`);
347
+ if (!imageHash) {
348
+ console.log(`Image: '${projectName}' is available on the ${networkName} blockchain.`);
349
+ process.exit(0);
350
+ }
351
+ const imageOwner = (await imageRegistry.getImageDetails(imageHash))[0];
352
+
353
+ if (privateKey) {
354
+ if (isStringPrivateKey(privateKey) === "OK") {
355
+ const account = Account.fromPrivate(privateKey);
356
+ if (imageOwner !== account.address) {
357
+ 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`);
358
+ process.exit(1);
359
+ }
360
+ }
361
+ }
362
+
363
+ if (imageOwner) {
364
+ 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`);
365
+ process.exit(0);
366
+ }
367
+
368
+ console.log(`Image: '${projectName}' is available on the ${networkName} blockchain.`);
369
+ } catch (e) {
370
+ process.exit(0);
371
+ }
372
372
  })();