Skip to content

Commit

Permalink
Merge pull request #15 from FhenixProtocol/fix/detect-hardhat-with-ch…
Browse files Browse the repository at this point in the history
…ainid

Check if network is hardhat using chainId instead of network name
  • Loading branch information
eshelB authored Oct 9, 2024
2 parents f23815f + c147716 commit 935f6c1
Show file tree
Hide file tree
Showing 3 changed files with 2,958 additions and 2,371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class FhenixHardhatRuntimeEnvironment extends FhenixClient {
// 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 isHardhat: boolean;

public constructor(
public hre: HardhatRuntimeEnvironment,
Expand All @@ -46,17 +47,17 @@ 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;
}
this.isHardhat = hre?.network?.config?.chainId === 31337;
console.log(
`network: ${JSON.stringify(this.network)}, isHardhat?: ${this.isHardhat}`,
);
}

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

return {
Expand All @@ -72,7 +73,7 @@ export class FhenixHardhatRuntimeEnvironment extends FhenixClient {
value: number,
securityZone?: number | undefined,
): Promise<EncryptedUint16> {
if (this.network === "hardhat") {
if (this.isHardhat) {
const data = bigintToUint8Array(BigInt(value));

return {
Expand All @@ -88,7 +89,7 @@ export class FhenixHardhatRuntimeEnvironment extends FhenixClient {
value: number,
securityZone?: number | undefined,
): Promise<EncryptedUint32> {
if (this.network === "hardhat") {
if (this.isHardhat) {
const data = bigintToUint8Array(BigInt(value));

return {
Expand All @@ -104,7 +105,7 @@ export class FhenixHardhatRuntimeEnvironment extends FhenixClient {
value: string | bigint,
securityZone?: number | undefined,
): Promise<EncryptedUint64> {
if (this.network === "hardhat") {
if (this.isHardhat) {
const data = bigintToUint8Array(BigInt(value));

return {
Expand All @@ -120,7 +121,7 @@ export class FhenixHardhatRuntimeEnvironment extends FhenixClient {
value: string | bigint,
securityZone?: number | undefined,
): Promise<EncryptedUint128> {
if (this.network === "hardhat") {
if (this.isHardhat) {
const data = bigintToUint8Array(BigInt(value));

return {
Expand All @@ -136,7 +137,7 @@ export class FhenixHardhatRuntimeEnvironment extends FhenixClient {
value: string | bigint,
securityZone?: number | undefined,
): Promise<EncryptedUint256> {
if (this.network === "hardhat") {
if (this.isHardhat) {
const data = bigintToUint8Array(BigInt(value));

return {
Expand All @@ -152,7 +153,7 @@ export class FhenixHardhatRuntimeEnvironment extends FhenixClient {
value: boolean,
securityZone?: number | undefined,
): Promise<EncryptedBool> {
if (this.network === "hardhat") {
if (this.isHardhat) {
if (value) {
const data = bigintToUint8Array(BigInt(1));

Expand All @@ -178,8 +179,7 @@ export class FhenixHardhatRuntimeEnvironment extends FhenixClient {
ciphertext: string,
account: string,
): bigint {
// console.log(`ct: ${ciphertext}`);
if (this.network === "hardhat") {
if (this.isHardhat) {
return uint8ArrayToBigint(ciphertext);
} else {
return super.unseal(contractAddress, ciphertext, account);
Expand Down Expand Up @@ -220,16 +220,6 @@ 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 uint8ArrayToBigint(uint8ArrayStr: string): bigint {
const byteArray = new Uint8Array(
Expand Down
1 change: 1 addition & 0 deletions packages/fhenix-hardhat-plugin/test/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe("Test Fhenix Plugin", function () {

it("checks that client works on hardhat with transparent operation", async function () {
expect(this.hre.fhenixjs.network).to.be.equal("hardhat");
expect(this.hre.fhenixjs.isHardhat).to.be.equal(true);
const fakeEnc = await this.hre.fhenixjs.encrypt_uint8(1);
expect(fakeEnc).to.be.an("object");
expect(fakeEnc).to.have.property("data");
Expand Down
Loading

0 comments on commit 935f6c1

Please sign in to comment.