Skip to content

Commit

Permalink
Ag 52 dec refactor (#45)
Browse files Browse the repository at this point in the history
* feat(lib): AG-52 get hash function

implemented utils function that get an hash based on an input string

* feat(contracts): AG-52 added sha field

added sha field to ecrypted structs in the DEC contract

* feat(contracts): AG-52 removed getters

removed getters from the DEC smart contract
  • Loading branch information
g3k0 authored Apr 22, 2024
1 parent bfc0439 commit 99343a8
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 45 deletions.
27 changes: 5 additions & 22 deletions contracts/DEC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ pragma solidity ^0.8.24;
/// @custom:experimental This is an experimental contract.
contract DEC {
address public owner;
Encrypted taxCode;
Encrypted municipality;
Encrypted region;
Encrypted country;
Encrypted public taxCode;
Encrypted public municipality;
Encrypted public region;
Encrypted public country;

struct Encrypted {
string sha;
string chiper;
string nonce;
}
Expand Down Expand Up @@ -39,33 +40,15 @@ contract DEC {
taxCode = _taxCode;
}

function getTaxCode() external view returns (Encrypted memory) {
return taxCode;
}

function setMunicipality(Encrypted memory _municipality) external onlyOwner {
municipality = _municipality;
}

function getMunicipality() external view returns (Encrypted memory) {
return municipality;
}

function setRegion(Encrypted memory _region) external onlyOwner {
region = _region;
}

function getRegion() external view returns (Encrypted memory) {
return region;
}

function setCountry(Encrypted memory _country) external onlyOwner {
country = _country;
}

function getCountry() external view returns (Encrypted memory) {
return country;
}


}
12 changes: 11 additions & 1 deletion lib/crypto-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encryptString, decryptString } from "./crypto-utils";
import { encryptString, decryptString, getHash } from "./crypto-utils";
import { mockEOAs } from "./__mocks__";

describe("Crypto Utils", () => {
Expand Down Expand Up @@ -30,4 +30,14 @@ describe("Crypto Utils", () => {
expect(e.message).toBe("Error decrypting string");
}
});

it("should return a SHA hash for a given message", () => {
const message = "Hello, world!";
const expectedHash =
"f345a219da005ebe9c1a1eaad97bbf38a10c8473e41d0af7fb617caa0c6aa722";

const result = getHash(message);

expect(result).toEqual({ sha: expectedHash });
});
});
23 changes: 22 additions & 1 deletion lib/crypto-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* is because If we send the data and then encrypt it in solidity, the data will be visible in the transaction that
* in the first place was used to send the data to the contract. Also, solidity doesn't have a function to encrypt.
*/
import { Encrypted, Decrypted } from "./types";
import { Encrypted, Decrypted, Sha } from "./types";
import { execSync, ExecSyncOptionsWithStringEncoding } from "child_process";
import * as path from "path";

Expand Down Expand Up @@ -40,6 +40,7 @@ export function encryptString(
);

const response: Encrypted = JSON.parse(encrypted.toString());
response.sha = getHash(decryptedString).sha;

return response;
} catch (e) {
Expand Down Expand Up @@ -84,6 +85,26 @@ export function decryptString(
}
}

export function getHash(message: string): Sha {
try {
const execSyncOptions = {
stdio: "pipe",
} as ExecSyncOptionsWithStringEncoding;

const cryptoPyPath = getCryptoPyPath();
const hashed = execSync(
`cd ${cryptoPyPath} && python3 Crypto.py sha3_256 --input="${message}"`,
execSyncOptions,
);

const response: Sha = JSON.parse(hashed.toString());

return response;
} catch (e: any) {
throw new Error("Error hashing the message");
}
}

/**
* The crypto-py library should be manually copy-pasted (or git cloned) inside the lib folder.
* Linux and MacOS users can in alternative create a symbolic link.
Expand Down
5 changes: 5 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
export type Encrypted = {
sha: string;
chiper: string;
nonce: string;
};

export type Decrypted = {
message: string;
};

export type Sha = {
sha: string;
};
61 changes: 40 additions & 21 deletions test/DEC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ describe("DEC Contract", () => {
const encryptedDataFactory = function (
chiper: string,
nonce: string,
sha: string,
): Encrypted {
return {
chiper,
nonce,
sha,
};
};

Expand Down Expand Up @@ -45,29 +47,33 @@ describe("DEC Contract", () => {
await (await ethers.provider.getSigner(0)).getAddress(),
);

const registeredTaxCode = await dec.getTaxCode();
const registeredMunicipality = await dec.getMunicipality();
const registeredRegion = await dec.getRegion();
const registeredCountry = await dec.getCountry();
const registeredTaxCode: Encrypted = await dec.taxCode();
const registeredMunicipality: Encrypted = await dec.municipality();
const registeredRegion: Encrypted = await dec.region();
const registeredCountry: Encrypted = await dec.country();

const enTaxCode = encryptedDataFactory(
registeredTaxCode[0],
registeredTaxCode[1],
registeredTaxCode.chiper,
registeredTaxCode.nonce,
registeredTaxCode.sha,
);

const enMunicipality = encryptedDataFactory(
registeredMunicipality[0],
registeredMunicipality[1],
registeredMunicipality.chiper,
registeredMunicipality.nonce,
registeredMunicipality.sha,
);

const enRegion = encryptedDataFactory(
registeredRegion[0],
registeredRegion[1],
registeredRegion.chiper,
registeredRegion.nonce,
registeredRegion.sha,
);

const enCountry = encryptedDataFactory(
registeredCountry[0],
registeredCountry[1],
registeredCountry.chiper,
registeredCountry.nonce,
registeredCountry.sha,
);

const decodedTaxCode = decryptString(
Expand Down Expand Up @@ -100,21 +106,26 @@ describe("DEC Contract", () => {
it("Should set and get tax code correctly", async () => {
await dec.setTaxCode(eTaxCode);

const getTaxCode = await dec.getTaxCode();
const getTaxCode = await dec.taxCode();

const gTaxCode = encryptedDataFactory(getTaxCode[0], getTaxCode[1]);
const gTaxCode = encryptedDataFactory(
getTaxCode.chiper,
getTaxCode.nonce,
getTaxCode.sha,
);

assert.equal(JSON.stringify(eTaxCode), JSON.stringify(gTaxCode));
});

it("Should set and get municipality correctly", async () => {
await dec.setMunicipality(eMunicipality);

const getMunicipality = await dec.getMunicipality();
const getMunicipality = await dec.municipality();

const gMunicipality = encryptedDataFactory(
getMunicipality[0],
getMunicipality[1],
getMunicipality.chiper,
getMunicipality.nonce,
getMunicipality.sha,
);

assert.equal(JSON.stringify(eMunicipality), JSON.stringify(gMunicipality));
Expand All @@ -123,19 +134,27 @@ describe("DEC Contract", () => {
it("Should set and get region correctly", async () => {
await dec.setRegion(eRegion);

const getRegion = await dec.getRegion();
const getRegion = await dec.region();

const gRegion = encryptedDataFactory(getRegion[0], getRegion[1]);
const gRegion = encryptedDataFactory(
getRegion.chiper,
getRegion.nonce,
getRegion.sha,
);

assert.equal(JSON.stringify(eRegion), JSON.stringify(gRegion));
});

it("Should set and get country correctly", async () => {
await dec.setCountry(eCountry);

const getCountry = await dec.getCountry();
const getCountry = await dec.country();

const gCountry = encryptedDataFactory(getCountry[0], getCountry[1]);
const gCountry = encryptedDataFactory(
getCountry.chiper,
getCountry.nonce,
getCountry.sha,
);

assert.equal(JSON.stringify(eCountry), JSON.stringify(gCountry));
});
Expand Down

0 comments on commit 99343a8

Please sign in to comment.