Skip to content

Commit

Permalink
Add initial support for mocked hardhat network
Browse files Browse the repository at this point in the history
  • Loading branch information
Cashmaney committed Aug 21, 2024
1 parent ec2a306 commit e9ad860
Show file tree
Hide file tree
Showing 7 changed files with 1,881 additions and 2,170 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fhenix-hardhat-plugin-root",
"version": "0.3.0-alpha.1",
"version": "0.3.0-alpha.2",
"license": "MIT",
"scripts": {
"preinstall": "npx only-allow pnpm",
Expand Down
2 changes: 1 addition & 1 deletion packages/fhenix-hardhat-docker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fhenix-hardhat-docker",
"version": "0.3.0-alpha.1",
"version": "0.3.0-alpha.2",
"description": "Hardhat plugin to run Fhenix node locally",
"repository": "github:FhenixProtocol/fhenix-hardhat-plugin",
"author": "Fhe Labs",
Expand Down
2 changes: 1 addition & 1 deletion packages/fhenix-hardhat-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fhenix-hardhat-plugin",
"version": "0.3.0-alpha.1",
"version": "0.3.0-alpha.2",
"description": "Hardhat TypeScript plugin boilerplate",
"repository": "github:FhenixProtocol/fhenix-hardhat-plugin",
"author": "Fhe Labs",
Expand Down
180 changes: 180 additions & 0 deletions packages/fhenix-hardhat-plugin/src/FhenixHardhatRuntimeEnvironment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import {
EncryptedBool,
EncryptedUint128,
EncryptedUint16,
EncryptedUint256,
EncryptedUint32,
EncryptedUint64,
EncryptedUint8,
FhenixClient,
getPermit,
InstanceParams,
Expand All @@ -18,6 +25,7 @@ export class FhenixHardhatRuntimeEnvironment extends FhenixClient {
// TODO remove config
// move the faucet to a task on the example repo
// there's no good way to discover the faucet port from here
public network: string;

public constructor(
public hre: HardhatRuntimeEnvironment,
Expand All @@ -35,6 +43,150 @@ export class FhenixHardhatRuntimeEnvironment extends FhenixClient {
}

super(superArgs);

this.network = hre?.network?.name;
console.log(`network: ${JSON.stringify(this.network)}`);
if (hre?.network?.name === "hardhat") {
return;
}
}

// public useHardhatNetwork() {
// this.network = "hardhat";
// }
//
// public useFhenix() {
// this.network = "fhenix";
// }
//
public async encrypt_uint8(
value: number,
securityZone?: number | undefined,
): Promise<EncryptedUint8> {
if (this.network === "hardhat") {
const data = bigintToUint8Array(BigInt(value));

return {
data,
securityZone: securityZone || 0,
};
} else {
return super.encrypt_uint8(value, securityZone);
}
}

public async encrypt_uint16(
value: number,
securityZone?: number | undefined,
): Promise<EncryptedUint16> {
if (this.network === "hardhat") {
const data = bigintToUint8Array(BigInt(value));

return {
data,
securityZone: securityZone || 0,
};
} else {
return super.encrypt_uint16(value, securityZone);
}
}

public async encrypt_uint32(
value: number,
securityZone?: number | undefined,
): Promise<EncryptedUint32> {
if (this.network === "hardhat") {
const data = bigintToUint8Array(BigInt(value));

return {
data,
securityZone: securityZone || 0,
};
} else {
return super.encrypt_uint32(value, securityZone);
}
}

public async encrypt_uint64(
value: string | bigint,
securityZone?: number | undefined,
): Promise<EncryptedUint64> {
if (this.network === "hardhat") {
const data = bigintToUint8Array(BigInt(value));

return {
data,
securityZone: securityZone || 0,
};
} else {
return super.encrypt_uint64(value, securityZone);
}
}

public async encrypt_uint128(
value: string | bigint,
securityZone?: number | undefined,
): Promise<EncryptedUint128> {
if (this.network === "hardhat") {
const data = bigintToUint8Array(BigInt(value));

return {
data,
securityZone: securityZone || 0,
};
} else {
return super.encrypt_uint128(value, securityZone);
}
}

public async encrypt_uint256(
value: string | bigint,
securityZone?: number | undefined,
): Promise<EncryptedUint256> {
if (this.network === "hardhat") {
const data = bigintToUint8Array(BigInt(value));

return {
data,
securityZone: securityZone || 0,
};
} else {
return super.encrypt_uint256(value, securityZone);
}
}

public async encrypt_bool(
value: boolean,
securityZone?: number | undefined,
): Promise<EncryptedBool> {
if (this.network === "hardhat") {
if (value) {
const data = bigintToUint8Array(BigInt(1));

return {
data,
securityZone: securityZone || 0,
};
} else {
const data = bigintToUint8Array(BigInt(0));

return {
data,
securityZone: securityZone || 0,
};
}
} else {
return super.encrypt_bool(value, securityZone);
}
}

public unseal(contractAddress: string, ciphertext: string): bigint {
// console.log(`ct: ${ciphertext}`);
if (this.network === "hardhat") {
return BigInt(ciphertext);
} else {
return super.unseal(contractAddress, ciphertext);
}
}

public async getFunds(address: string) {
Expand Down Expand Up @@ -77,3 +229,31 @@ export class MockProvider {
});
}
}
//
// function bigintToUint8Array(value: bigint): Uint8Array {
// const hex = value.toString(16);
// const len = Math.ceil(hex.length / 2);
// const u8 = new Uint8Array(len);
// for (let i = 0; i < len; i++) {
// u8[len - i - 1] = parseInt(hex.substr(i * 2, 2), 16);
// }
// return u8;
// }

function bigintToUint8Array(bigNum: bigint): Uint8Array {
const byteLength = 32;
const byteArray = new Uint8Array(byteLength);

// Create a BigInt mask for each byte
const mask = BigInt(0xff);

// Initialize an index to start from the end of the array
for (let i = 0; i < byteLength; i++) {
// Extract the last byte and assign it to the corresponding position in the array
byteArray[byteLength - 1 - i] = Number(bigNum & mask);
// Shift bigint right by 8 bits to process the next byte in the next iteration
bigNum >>= BigInt(8);
}

return byteArray;
}
16 changes: 6 additions & 10 deletions packages/fhenix-hardhat-plugin/test/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ describe("Test Fhenix Plugin", function () {
assert.equal(this.hre.fhenixjs.sayHello(), "hello");
});

it("checks that client doesn't work on a non-fhe chain", async function () {
let err: Error | EncryptedUint8;
try {
err = await this.hre.fhenixjs.encrypt_uint8(1);
} catch (e) {
// @ts-ignore
err = e;
}
expect(err).to.be.an("error");
expect(err.toString()).to.include("initializing fhenixjs");
it("checks that client works on hardhat with transparent operation", async function () {
expect(this.hre.fhenixjs.network).to.be.equal("hardhat");
const fakeEnc = await this.hre.fhenixjs.encrypt_uint8(1);
expect(fakeEnc).to.be.an("object");
expect(fakeEnc).to.have.property("data");
expect(fakeEnc).to.have.property("securityZone");
});
});

Expand Down
3 changes: 2 additions & 1 deletion packages/fhenix-hardhat-plugin/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
],
"no-floating-promises": true,
"prefer-conditional-expression": false,
"no-implicit-dependencies": true
"no-implicit-dependencies": true,
"no-bitwise": false
}
}
Loading

0 comments on commit e9ad860

Please sign in to comment.