Skip to content

Commit a557815

Browse files
Merge pull request #5 from AndriianChestnykh/feature/diffie-hellman
add tests for encrypt and decrypt message Owner-Oracle-Heir
2 parents f4cb184 + dcebe23 commit a557815

File tree

7 files changed

+82
-9
lines changed

7 files changed

+82
-9
lines changed

contracts/Hub.sol

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ contract Hub {
4040
}
4141

4242
function registerPubKeyOracle(bytes calldata publicKey) public {
43-
require(registeredKeys[_address].length == 32, "This user did not register the key");
4443
registeredKeys[msg.sender] = publicKey;
4544
emit PublicKeyRegistered(msg.sender, publicKey);
4645
}

dapps/utils/decrypt-im/decrypt-im.js renamed to dapps/utils/decrypt-im.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const compressKey = require("./../compress-key/compress-key.js");
1+
const compressKey = require('./compress-key.js');
22
const sodium = require('sodium').api;
33
const crypto = require('crypto');
4-
const cryptoConfigConstant = require("./../crypto-config-constant.js");
4+
const cryptoConfigConstant = require("./crypto-config-constant.js");
55

66
async function decryptIM(encryptedMessage, recipientPrivateKey, senderPublicKey) {
77
return new Promise((resolve, reject) => {

dapps/utils/encryp-im/encrypt-im.js renamed to dapps/utils/encrypt-im.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const compressKey = require("./../compress-key/compress-key.js");
1+
const compressKey = require("./compress-key.js");
22
const sodium = require('sodium').api;
33
const crypto = require('crypto');
4-
const cryptoConfigConstant = require("./../crypto-config-constant.js");
4+
const cryptoConfigConstant = require("./crypto-config-constant.js");
55

66
async function encryptIM(message, senderPrivateKey, recipientPublicKey) {
77
return new Promise((resolve, reject) => {

test/encrypt-decrypt.test.js

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const { expect } = require("chai");
2-
const encryptIM = require("./../dapps/utils/encryp-im/encrypt-im.js");
3-
const decryptIM = require("./../dapps/utils/decrypt-im/decrypt-im.js");
2+
const { ethers } = require("hardhat");
3+
const { utils } = require("ethers");
4+
const encryptIM = require("./../dapps/utils/encrypt-im.js");
5+
const decryptIM = require("./../dapps/utils/decrypt-im.js");
46

57
const accounts = {
68
Owner: {
@@ -33,4 +35,37 @@ describe("Encryption and Decryption", function () {
3335
// Check if the decrypted message matches the original message
3436
expect(decryptedMessage).to.equal(message);
3537
});
38+
39+
it("should encrypt the message, send it to Oracle, Oracle should decrypt it", async function () {
40+
const message = "Hello, world!";
41+
42+
// Encrypting a message from Owner to Heir
43+
const encryptedMessage = await encryptIM(message, accounts.Owner.privateKey, accounts.Heir.publicKey);
44+
45+
// Owner encrypting message for Oracle and send it
46+
const encryptedMessageForOracle = await encryptIM(encryptedMessage, accounts.Owner.privateKey, accounts.Oracle.publicKey);
47+
48+
// Oracle gets encrypted message from Owner and decrypted it
49+
const decryptedMessageFromOwnerToOracle = await decryptIM(encryptedMessageForOracle, accounts.Oracle.privateKey, accounts.Owner.publicKey)
50+
51+
expect(decryptedMessageFromOwnerToOracle).to.equal(encryptedMessage)
52+
})
53+
54+
it("create new account using ether.js and private key", async function () {
55+
const message = "Hello, world!";
56+
57+
// Encrypting a message from Owner to Heir
58+
const encryptedMessage = await encryptIM(message, accounts.Owner.privateKey, accounts.Heir.publicKey);
59+
60+
// Owner encrypting message for Oracle and send it
61+
const encryptedMessageForOracle = await encryptIM(encryptedMessage, accounts.Owner.privateKey, accounts.Oracle.publicKey);
62+
63+
// Oracle gets encrypted message from Owner and decrypted it
64+
const decryptedMessageFromOwnerToOracle = await decryptIM(encryptedMessageForOracle, accounts.Oracle.privateKey, accounts.Owner.publicKey)
65+
66+
// Heir gets encrypt message from Oracle
67+
const decryptedMessageFromOwnetToHeir = await decryptIM(decryptedMessageFromOwnerToOracle, accounts.Heir.privateKey, accounts.Owner.publicKey)
68+
69+
expect(decryptedMessageFromOwnetToHeir).to.equal(message)
70+
})
3671
});

test/hub/hub.test.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe("Hub Contract", function () {
5454

5555
//1. Owner encrypts (DH with Owner + Heir): DH_O_H(IM) - EIM
5656

57-
alice.privateKey = Buffer.from('ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80', 'hex');
57+
// alice.privateKey = Buffer.from('ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80', 'hex');
5858
//2. Owner gets Oracle public key
5959
//3. Owner encrypts (DH with Owner + Oracle): DH_O_Or(DH_O_H(IM)) - EIM
6060
//2. Owner sends DH_O_Or(DH_O_H(IM)) to Oracle
@@ -72,8 +72,15 @@ describe("Hub Contract", function () {
7272
// //3. Owner encrypts (DH with Owner + Oracle): DH_O_Or(DH_O_H(IM)) - EIM
7373

7474
// TODO finalize the encryption process
75-
const publicKey = sodium.crypto_scalarmult_base(alice.privateKey)
75+
// const publicKey = sodium.crypto_scalarmult_base(alice.privateKey)
7676

77+
// await hubContract.connect(owner).registerPubKeyOwner(publicKey); // Register owner's public key
78+
79+
// const heirAddress = heir.address;
80+
// const encryptedData = utils.toUtf8Bytes("0xencrypteddata");
81+
// await hubContract.connect(owner).sendEIMtoOracle(heirAddress, encryptedData);
82+
83+
const publicKey = "0x0123456789ABCDEF"; // Example public key
7784
await hubContract.connect(owner).registerPubKeyOwner(publicKey); // Register owner's public key
7885

7986
const heirAddress = heir.address;

test/new-encrypt-decrypt.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// const { expect } = require("chai");
2+
// const {ethers} = require("hardhat");
3+
// const encryptIM = require("./../dapps/utils/encrypt-im.js");
4+
// const decryptIM = require("./../dapps/utils/decrypt-im.js");
5+
// const { describe } = require("mocha");
6+
7+
8+
// describe("New encrypr decrypt tests functions", function() {
9+
// let Hub;
10+
// let hubContract;
11+
// let owner;
12+
// let heir;
13+
// let oracle;
14+
15+
// before(async function() {
16+
// Hub = await ethers.getContractFactory("Hub");
17+
// [owner, heir, oracle] = await ethers.getSigners();
18+
// ethers.ge
19+
20+
// hubContract = await Hub.deploy();
21+
// await hubContract.deployed();
22+
// })
23+
24+
// it("should encrypt and decrypt the message in Hub Smart Contract", async function() {
25+
// const ownerPublicKey = await owner.getPublicKey();
26+
27+
// await hubContract.connect(owner).registerPubKeyOwner(ownerPublicKey);
28+
// const registerPubKeyHeirByOwner = await hubContract.registeredKeys(owner.address);
29+
30+
// expect(registerPubKeyHeirByOwner).to.equal(ownerPublicKey);
31+
// })
32+
// })

0 commit comments

Comments
 (0)