@ethernity-cloud/sdk-js 1.6.1 → 1.6.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.
@@ -1,381 +1,384 @@
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
+ }
39
+ }
40
+
41
+ setVars();
42
+
43
+ function isStringPrivateKey(privateKey) {
44
+ try {
45
+ let key = privateKey;
46
+ if (!key.startsWith("0x")) {
47
+ key = `0x${privateKey}`;
48
+ }
49
+
50
+ Web3.eth.accounts.privateKeyToAccount(key);
51
+ return "OK";
52
+ } catch (e) {
53
+ return e.toString();
54
+ }
55
+ }
56
+
57
+ async function checkAccountBalance() {
58
+ try {
59
+ const web3 = new ethers.providers.JsonRpcProvider(NETWORK_RPC);
60
+ const account = new ethers.Wallet(PRIVATE_KEY, this.provider);
61
+ const balance = await web3.getBalance(account.address);
62
+ return Web3.utils.fromWei(balance, 'ether');
63
+ } catch (e) {
64
+ console.error(e);
65
+ return 0;
66
+ }
67
+ }
68
+
69
+ class ImageRegistry {
70
+ constructor() {
71
+ try {
72
+ this.imageRegistryAbi = this.readContractAbi('image_registry.abi');
73
+ this.imageRegistryAddress = IMAGE_REGISTRY_ADDRESS;
74
+ console.log("imageRegistryAddress: ", this.imageRegistryAddress);
75
+ this.provider = new ethers.providers.JsonRpcProvider(NETWORK_RPC);
76
+
77
+ if (PRIVATE_KEY) {
78
+ this.acct = new ethers.Wallet(PRIVATE_KEY, this.provider);
79
+ } else {
80
+ const _privateKey = new ethers.Wallet.createRandom().privateKey;
81
+ this.acct = new ethers.Wallet(_privateKey, this.provider);
82
+ }
83
+ this.imageRegistryContract = new ethers.Contract(
84
+ this.imageRegistryAddress,
85
+ this.imageRegistryAbi,
86
+ this.acct
87
+ );
88
+ } catch (e) {
89
+ console.error(e);
90
+ }
91
+ }
92
+
93
+ readContractAbi(contractName) {
94
+ const filePath = path.join(__dirname, contractName);
95
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'));
96
+ }
97
+
98
+ async addTrustedZoneCert(certContent, ipfsHash, imageName, dockerComposeHash, enclaveNameTrustedZone, fee) {
99
+ console.log("Adding trusted zone cert to image registry");
100
+ const nonce = await this.provider.getTransactionCount(this.acct.address);
101
+
102
+ const unicornTxn = this.imageRegistryContract.addTrustedZoneImage(
103
+ ipfsHash, certContent, "v3", imageName, dockerComposeHash, enclaveNameTrustedZone, fee
104
+ ).send({
105
+ gas: GAS,
106
+ chainId: CHAIN_ID,
107
+ nonce: nonce,
108
+ gasPrice: GAS_PRICE === 1 ? ethers.utils.parseUnits('1', 'mwei') : GAS_PRICE
109
+ });
110
+
111
+ console.log("transaction status: ", receipt.status);
112
+ console.log("transaction receipt: ", receipt);
113
+ if (receipt.status === 1) {
114
+ console.log("Adding trusted zone cert transaction was successful!");
115
+ } else {
116
+ console.log("Adding trusted zone cert transaction was UNSUCCESSFUL!");
117
+ }
118
+ const signedTxn = await this.acct.signTransaction(unicornTxn);
119
+ const receipt = await this.provider.sendTransaction(signedTxn.rawTransaction);
120
+ }
121
+
122
+ async addSecureLockImageCert(certContent, ipfsHash, imageName, version, dockerComposeHash, enclaveNameSecureLock, fee) {
123
+ try {
124
+ console.log("Adding secure lock image cert to image registry");
125
+ // Fetch current nonce
126
+ if (BLOCKCHAIN_NETWORK.includes("Polygon")) {
127
+ console.log("Polygon Mainnet");
128
+ let nonce = await this.provider.getTransactionCount(this.acct.address, 'pending');
129
+ // Fetch current gas price and increase it
130
+ let gasPrice = await this.provider.getGasPrice();
131
+ gasPrice = gasPrice.mul(ethers.BigNumber.from(110)).div(ethers.BigNumber.from(100));
132
+ const unicornTxn = await this.imageRegistryContract.addImage(
133
+ ipfsHash, certContent, version, imageName, dockerComposeHash, enclaveNameSecureLock, fee,
134
+ {
135
+ nonce: nonce,
136
+ gasPrice: gasPrice,
137
+ });
138
+ // console.log("Transaction: ", unicornTxn);
139
+ const receipt = await this.imageRegistryContract.provider.waitForTransaction(unicornTxn.hash);
140
+
141
+ // console.log("transaction status: ", receipt.status);
142
+ console.log("transaction receipt: ", unicornTxn.hash);
143
+ if (receipt.status === 1) {
144
+ console.log("Adding secure lock image cert transaction was successful!");
145
+ } else {
146
+ // console.log("receipt.status", receipt.status)
147
+ console.log("Image certificates already exist for this image!");
148
+ }
149
+ } else {
150
+ const unicornTxn = await this.imageRegistryContract.addImage(
151
+ ipfsHash, certContent, version, imageName, dockerComposeHash, enclaveNameSecureLock, fee);
152
+ // console.log("Transaction: ", unicornTxn);
153
+ const receipt = await this.imageRegistryContract.provider.waitForTransaction(unicornTxn.hash);
154
+
155
+ // console.log("transaction status: ", receipt.status);
156
+ console.log("transaction receipt: ", unicornTxn.hash);
157
+ if (receipt.status === 1) {
158
+ console.log("Adding secure lock image cert transaction was successful!");
159
+ } else {
160
+ // console.log("receipt.status", receipt.status)
161
+ console.log("Image certificates already exist for this image!");
162
+ }
163
+ }
164
+
165
+ // console.log("this.acct.address: ", this.acct.address);
166
+ // console.log("private key", PRIVATE_KEY);
167
+ // console.log('Getting nonce');
168
+ // const nonce = await this.imageRegistryContract.provider.getTransactionCount(this.acct.address);
169
+
170
+
171
+ // const signedTxn = await this.acct.signTransaction(unicornTxn);
172
+ // const receipt = await this.provider.sendTransaction(signedTxn.rawTransaction);
173
+ } catch (e) {
174
+ // console.error(e);
175
+ console.log("Image certificates already exist for this image!");
176
+ }
177
+ }
178
+
179
+ async getImagePublicKeyCert(ipfsHash) {
180
+ try {
181
+ console.log("Getting image cert from image registry");
182
+ // console.log("this.acct.address: ", this.acct.address);
183
+ // console.log("private key", PRIVATE_KEY);
184
+ // console.log('Getting nonce');
185
+ // const nonce = await this.imageRegistryContract.provider.getTransactionCount(this.acct.address);
186
+ const unicornTxn = await this.imageRegistryContract.getImageCertPublicKey(ipfsHash);
187
+ console.log("unicornTxn: ", unicornTxn);
188
+ // const receipt = await this.imageRegistryContract.provider.waitForTransaction(unicornTxn.hash);
189
+
190
+ // console.log("transaction status: ", receipt.status);
191
+ // // console.log("transaction receipt: ", unicornTxn);
192
+ // if (receipt.status === 1) {
193
+ // console.log("");
194
+ // } else {
195
+ // console.log("Adding secure lock image cert transaction was UNSUCCESSFUL!");
196
+ // }
197
+ // const signedTxn = await this.acct.signTransaction(unicornTxn);
198
+ // const receipt = await this.provider.sendTransaction(signedTxn.rawTransaction);
199
+ } catch (e) {
200
+ console.error(e);
201
+ console.log("Getting image cert transaction was UNSUCCESSFUL!");
202
+ }
203
+ }
204
+
205
+ async validateSecureLockImageCert(certContent, ipfsHash, imageName, dockerComposeHash, enclaveNameSecureLock, fee) {
206
+ console.log("Validating secure lock image cert in image registry");
207
+ const nonce = await this.provider.getTransactionCount(this.acct.address);
208
+ const unicornTxn = this.imageRegistryContract.validateSecureLockImage(
209
+ ipfsHash, certContent, "v3", imageName, dockerComposeHash, enclaveNameSecureLock, fee
210
+ ).send({
211
+ gas: GAS,
212
+ chainId: CHAIN_ID,
213
+ nonce: nonce,
214
+ gasPrice: GAS_PRICE === 1 ? ethers.utils.parseUnits('1', 'mwei') : GAS_PRICE
215
+ });
216
+
217
+ console.log("transaction status: ", receipt.status);
218
+ console.log("transaction receipt: ", receipt);
219
+ if (receipt.status === 1) {
220
+ console.log("Validating secure lock image cert transaction was successful!");
221
+ } else {
222
+ console.log("Validating secure lock image cert transaction was UNSUCCESSFUL!");
223
+ }
224
+ const signedTxn = await this.acct.signTransaction(unicornTxn);
225
+ const receipt = await this.provider.sendTransaction(signedTxn.rawTransaction);
226
+ }
227
+
228
+ async addSecureLockAndValidateImageCert(certContent, ipfsHash, imageName, dockerComposeHash, enclaveNameSecureLock, fee) {
229
+ console.log("Adding and validating secure lock image cert in image registry");
230
+ const nonce = await this.provider.getTransactionCount(this.acct.address);
231
+ const unicornTxn = this.imageRegistryContract.addSecureLockAndValidateImage(
232
+ ipfsHash, certContent, "v3", imageName, dockerComposeHash, enclaveNameSecureLock, fee
233
+ ).send({
234
+ gas: GAS,
235
+ chainId: CHAIN_ID,
236
+ nonce: nonce,
237
+ gasPrice: GAS_PRICE === 1 ? ethers.utils.parseUnits('1', 'mwei') : GAS_PRICE
238
+ });
239
+
240
+ console.log("transaction status: ", receipt.status);
241
+ console.log("transaction receipt: ", receipt);
242
+ if (receipt.status === 1) {
243
+ console.log("Adding and validating secure lock image cert transaction was successful!");
244
+ } else {
245
+ console.log("Adding and validating secure lock image cert transaction was UNSUCCESSFUL!");
246
+ }
247
+ const signedTxn = await this.acct.signTransaction(unicornTxn);
248
+ const receipt = await this.provider.sendTransaction(signedTxn.rawTransaction);
249
+ }
250
+
251
+ async getTrustedZoneCert(ipfsHash) {
252
+ const cert = await this.imageRegistryContract.getTrustedZoneCert(ipfsHash);
253
+ return cert;
254
+ }
255
+
256
+ async getSecureLockCert(ipfsHash) {
257
+ const cert = await this.imageRegistryContract.getSecureLockCert(ipfsHash);
258
+ return cert;
259
+ }
260
+
261
+ async getImageDetails(ipfsHash) {
262
+ try {
263
+ const details = await this.imageRegistryContract.imageDetails(ipfsHash);
264
+ return details;
265
+ } catch (e) {
266
+ return ['', '', ''];
267
+ }
268
+ }
269
+
270
+ async _getLatestImageVersionPublicKey(projectName, version) {
271
+ try {
272
+ // console.log("Getting latest image version public key");
273
+ const publicKey = await this.imageRegistryContract.getLatestImageVersionPublicKey(projectName, version);
274
+ // console.log("public key: ", publicKey);
275
+ return publicKey;
276
+ } catch (e) {
277
+ // console.error(e);
278
+ return ['', '', ''];
279
+ }
280
+ }
281
+ }
282
+
283
+ (async () => {
284
+ try {
285
+ const [networkName, projectName, version, privateKey, action] = process.argv.slice(2);
286
+ if (action === "validateAddress") {
287
+ if (privateKey) {
288
+ console.log(isStringPrivateKey(privateKey));
289
+ }
290
+ process.exit(0);
291
+ }
292
+ if (networkName && projectName) {
293
+ BLOCKCHAIN_NETWORK = networkName;
294
+ }
295
+ setVars(networkName);
296
+ if (action === "checkBalance") {
297
+ const balance = await checkAccountBalance();
298
+ console.log(`${balance} gas`);
299
+ process.exit(0);
300
+ }
301
+ if (action === 'getTrustedZoneCert') {
302
+ const imageRegistry = new ImageRegistry();
303
+ const public = (await imageRegistry._getLatestImageVersionPublicKey(projectName, version))[1]
304
+ console.log(public);
305
+ return public;
306
+ }
307
+ // The CAS session name recorded for the trustedzone template's latest
308
+ // image: what the compose's SCONE_CONFIG_ID for the trustedzone must name.
309
+ if (action === 'getTrustedZoneSession') {
310
+ const imageRegistry = new ImageRegistry();
311
+ const latest = await imageRegistry.imageRegistryContract.getLatestTrustedZoneImageCertPublicKey(projectName, version);
312
+ const session = await imageRegistry.imageRegistryContract.getTrustedZoneImageSession(latest[0]);
313
+ console.log(session);
314
+ return session;
315
+ }
316
+
317
+ const imageRegistry = new ImageRegistry();
318
+ if (action === 'registerSecureLockImage') {
319
+ const secureLock = fs.readFileSync("./registry/certificate.securelock.crt", 'utf8');
320
+ // console.log("SECURELOCK:", secureLock);
321
+ const ipfsHash = process.env.IPFS_HASH || "";
322
+ console.log(`ipfsHash: ${ipfsHash}`);
323
+ const ipfsDockerComposeHash = process.env.IPFS_DOCKER_COMPOSE_HASH || "";
324
+ console.log(`ipfsDockerComposeHash: ${ipfsDockerComposeHash}`);
325
+ const imageName = process.env.PROJECT_NAME || "";
326
+ console.log(`imageName: ${imageName}`);
327
+ const enclaveVersion = process.env.VERSION || "";
328
+ const enclaveNameSecureLock = process.env.ENCLAVE_NAME_SECURELOCK || "";
329
+ console.log(`enclaveNameSecureLock: ${enclaveNameSecureLock}`);
330
+ const fee = process.env.DEVELOPER_FEE || "0";
331
+ console.log(`fee: ${fee}`);
332
+
333
+ // Register the SAME build under two registry version keys:
334
+ // "v3" -> the moving "latest" pointer the runner resolves by
335
+ // default (getLatestImageVersionPublicKey(name,"v3")).
336
+ // This MUST be "v3" (the protocol version), NOT the
337
+ // enclave/template VERSION -- registering under the
338
+ // VERSION (e.g. "21") wrote the pointer where the
339
+ // runner never looks, so it kept using a stale image.
340
+ // <VERSION> -> an immutable per-version entry (e.g. "22") so this
341
+ // exact build can be pinned/rolled back to later.
342
+ // The contract stores each (name,version) channel append-only.
343
+ const versionKeys = ["v3"];
344
+ if (enclaveVersion && enclaveVersion !== "v3") versionKeys.push(enclaveVersion);
345
+ for (const versionKey of versionKeys) {
346
+ console.log(`Registering securelock under version '${versionKey}'`);
347
+ await imageRegistry.addSecureLockImageCert(secureLock, ipfsHash, imageName, versionKey, ipfsDockerComposeHash, enclaveNameSecureLock, fee);
348
+ }
349
+ process.exit(0);
350
+ }
351
+ if (action === 'getImagePublicKey') {
352
+ const ipfsHash = process.env.IPFS_HASH || "";
353
+ await imageRegistry.getImagePublicKeyCert(ipfsHash);
354
+ process.exit(0);
355
+ }
356
+ console.log(`Checking image: '${projectName}' on the ${networkName} blockchain...`);
357
+ const imageHash = (await imageRegistry._getLatestImageVersionPublicKey(projectName, version))[0];
358
+ console.log(`Image hash: ${imageHash}`);
359
+ if (!imageHash) {
360
+ console.log(`Image: '${projectName}' is available on the ${networkName} blockchain.`);
361
+ process.exit(0);
362
+ }
363
+ const imageOwner = (await imageRegistry.getImageDetails(imageHash))[0];
364
+
365
+ if (privateKey) {
366
+ if (isStringPrivateKey(privateKey) === "OK") {
367
+ const account = Account.fromPrivate(privateKey);
368
+ if (imageOwner !== account.address) {
369
+ 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`);
370
+ process.exit(1);
371
+ }
372
+ }
373
+ }
374
+
375
+ if (imageOwner) {
376
+ 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`);
377
+ process.exit(0);
378
+ }
379
+
380
+ console.log(`Image: '${projectName}' is available on the ${networkName} blockchain.`);
381
+ } catch (e) {
382
+ process.exit(0);
383
+ }
381
384
  })();