Skip to content

Commit

Permalink
Use customError in ConfidentialERC1155
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Bln committed Apr 4, 2024
1 parent 2b6c20d commit dd9865f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 53 deletions.
26 changes: 15 additions & 11 deletions contracts/ConfidentialERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ struct TokenData {
}

contract ConfidentialERC1155 is ERC1155, Ownable, Reencrypt {
error DataAlreadySet(uint256 tokenId);
error RequirePositiveBalance(uint256 tokenId);
// Mapping from token ID to additional data
mapping(uint256 tokenId => TokenData tokenData) private _tokenData;
mapping(uint256 tokenId => TokenData tokenData) private _tokenDatas;

constructor(string memory uri) ERC1155(uri) Ownable(msg.sender) {}

Expand All @@ -25,28 +27,30 @@ contract ConfidentialERC1155 is ERC1155, Ownable, Reencrypt {
uint256 tokenId,
uint256 amount,
bytes calldata confidentialData,
bytes memory data
bytes memory metaData
) external onlyOwner {
//require(!_tokenData[tokenId].alreadySet, "Confidential data already set for this tokenId");
if (!_tokenData[tokenId].alreadySet) {
super._mint(account, tokenId, amount, data);
_tokenData[tokenId] = TokenData(TFHE.asEuint32(confidentialData), true);
if (_tokenDatas[tokenId].alreadySet) {
revert DataAlreadySet(tokenId);
}
// require(!_tokenDatas[tokenId].alreadySet, "Data Already Set");
super._mint(account, tokenId, amount, metaData);
_tokenDatas[tokenId] = TokenData(TFHE.asEuint32(confidentialData), true);
}

function getConfidentialData(
uint256 tokenId,
bytes32 publicKey,
bytes calldata signature
) public view virtual onlySignedPublicKey(publicKey, signature) returns (bytes memory) {
if (balanceOf(msg.sender, tokenId) > 0) {
return TFHE.reencrypt(_tokenData[tokenId].x, publicKey, 0);
} else {
return TFHE.reencrypt(TFHE.asEuint32(0), publicKey, 0);
if (balanceOf(msg.sender, tokenId) <= 0) {
revert RequirePositiveBalance(tokenId);
}
//require(balanceOf(msg.sender, tokenId) > 0, "Require positive balance to access confidential data");
return TFHE.reencrypt(_tokenDatas[tokenId].x, publicKey, 0);
}

// Function to get the additional data associated with a token
function getTokenData(uint256 tokenId) external view returns (TokenData memory) {
return _tokenData[tokenId];
return _tokenDatas[tokenId];
}
}
46 changes: 23 additions & 23 deletions deploy/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
//import { DeployFunction } from "hardhat-deploy/types";
//import { HardhatRuntimeEnvironment } from "hardhat/types";
//
//const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
// const { deployer } = await hre.getNamedAccounts();
// const { deploy } = hre.deployments;
//
// const deployed = await deploy("ConfidentialERC20", {
// from: deployer,
// args: ["Naraggara", "NARA"],
// log: true,
// });
//
// console.log(`ConfidentialERC20 contract: `, deployed.address);
//};
//export default func;
//func.id = "deploy_confidentialERC20"; // id required to prevent reexecution
//func.tags = ["ConfidentialERC20"];
import { DeployFunction } from "hardhat-deploy/types";
import { HardhatRuntimeEnvironment } from "hardhat/types";

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { deployer } = await hre.getNamedAccounts();
const { deploy } = hre.deployments;

const deployed = await deploy("ConfidentialERC1155", {
const deployed = await deploy("ConfidentialERC20", {
from: deployer,
args: ["Uri"],
args: ["Naraggara", "NARA"],
log: true,
});

console.log(`ConfidentialERC1155 contract: `, deployed.address);
console.log(`ConfidentialERC20 contract: `, deployed.address);
};
export default func;
func.id = "deploy_confidentialERC1155"; // id required to prevent reexecution
func.tags = ["ConfidentialERC1155"];
func.id = "deploy_confidentialERC20"; // id required to prevent reexecution
func.tags = ["ConfidentialERC20"];
// import { DeployFunction } from "hardhat-deploy/types";
// import { HardhatRuntimeEnvironment } from "hardhat/types";

// const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
// const { deployer } = await hre.getNamedAccounts();
// const { deploy } = hre.deployments;

// const deployed = await deploy("ConfidentialERC1155", {
// from: deployer,
// args: ["Uri"],
// log: true,
// });

// console.log(`ConfidentialERC1155 contract: `, deployed.address);
// };
// export default func;
// func.id = "deploy_confidentialERC1155"; // id required to prevent reexecution
// func.tags = ["ConfidentialERC1155"];
2 changes: 1 addition & 1 deletion test/confidentialERC1155/ConfidentialERC1155.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function deployEncryptedERC1155Fixture(): Promise<ConfidentialERC11
const signers = await getSigners();

const contractFactory = await ethers.getContractFactory("ConfidentialERC1155");
const contract = await contractFactory.connect(signers.alice).deploy("contractURI"); // City of Zama's battle
const contract = await contractFactory.connect(signers.alice).deploy("contractURI");
await contract.waitForDeployment();

return contract;
Expand Down
66 changes: 48 additions & 18 deletions test/confidentialERC1155/ConfidentialERC1155.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { expect } from "chai";
import { ethers } from "hardhat";

import { ConfidentialERC1155 } from "../../types";
import { createInstances } from "../instance";
import { getSigners, initSigners } from "../signers";
import { deployEncryptedERC1155Fixture } from "./ConfidentialERC1155.fixture";

describe("ConfidentialERC1155", function () {
let contractInstance: ConfidentialERC1155;
before(async function () {
await initSigners();
this.signers = await getSigners();
Expand All @@ -15,6 +17,7 @@ describe("ConfidentialERC1155", function () {
const contract = await deployEncryptedERC1155Fixture();
this.contractAddress = await contract.getAddress();
this.erc1155 = contract;
contractInstance = contract;
this.instances = await createInstances(this.contractAddress, ethers, this.signers);
});

Expand Down Expand Up @@ -63,41 +66,68 @@ describe("ConfidentialERC1155", function () {

const bobErc1155 = this.erc1155.connect(this.signers.bob);
const tokenBob = this.instances.bob.getPublicKey(this.contractAddress)!;

const returnedEncryptedData = await bobErc1155.getConfidentialData(0, tokenBob.publicKey, tokenBob.signature);
const data = this.instances.bob.decrypt(this.contractAddress, returnedEncryptedData);
expect(data).to.equal(0);
await expect(bobErc1155.getConfidentialData(0, tokenBob.publicKey, tokenBob.signature))
.to.be.revertedWithCustomError(bobErc1155, "RequirePositiveBalance")
.withArgs(0);
//const returnedEncryptedData = await bobErc1155.getConfidentialData(0, tokenBob.publicKey, tokenBob.signature);
//const data = this.instances.bob.decrypt(this.contractAddress, returnedEncryptedData);
//expect(data).to.equal(0);
});

it("should not re-mint already set confidential data", async function () {
it.only("should not re-mint already set confidential data", async function () {
const encryptedData1 = this.instances.alice.encrypt32(787);
const transaction1 = await this.erc1155.mintWithConfidentialData(
const transaction1 = await contractInstance.mintWithConfidentialData(
this.signers.alice.address,
0,
1000,
encryptedData1,
encryptedData1,
);
console.log("yoyo");
await transaction1.wait();
console.log("yoyo");

const encryptedData2 = this.instances.alice.encrypt32(42);
const transaction2 = await this.erc1155.mintWithConfidentialData(
this.signers.alice.address,
0,
500,
encryptedData2,
encryptedData2,
);
await transaction2.wait();

try {
await contractInstance.mintWithConfidentialData(
this.signers.alice.address,
0,
500,
encryptedData2,
encryptedData2,
);
} catch (e) {
expect(
(e as Error).toString().substring(0, 67) ===
"ProviderError: rpc error: code = Unknown desc = execution reverted",
).to.be.true;
}
// await expect(
// this.erc1155.mintWithConfidentialData(this.signers.alice.address, 0, 500, encryptedData2, encryptedData2),
// ).to.be.revertedWith("Data Already Set");

// await expect(
// contractInstance.mintWithConfidentialData(this.signers.alice.address, 0, 500, encryptedData2, encryptedData2),
// )
// .to.be.revertedWithCustomError(contractInstance, "DataAlreadySet")
// .withArgs(0);

// await expect(transaction2).to.be.revertedWith("Confidential data already set for this tokenId");
// await (await transaction2).wait();
// await expect(
// this.erc1155.mintWithConfidentialData(this.signers.alice.address, 0, 500, encryptedData2, encryptedData2),
// ).to.be.revertedWith("Confidential data already set for this tokenId");
//await transaction2.wait();

const balance = await this.erc1155.balanceOf(this.signers.alice.address, 0);
expect(balance).to.equal(1000);

const token = this.instances.alice.getPublicKey(this.contractAddress)!;
// const token = this.instances.alice.getPublicKey(this.contractAddress)!;

const returnedEncryptedData = await this.erc1155.getConfidentialData(0, token.publicKey, token.signature);
const data = this.instances.alice.decrypt(this.contractAddress, returnedEncryptedData);
expect(data).to.equal(787);
// const returnedEncryptedData = await this.erc1155.getConfidentialData(0, token.publicKey, token.signature);
// const data = this.instances.alice.decrypt(this.contractAddress, returnedEncryptedData);
// expect(data).to.equal(787);
});

it("should transfer tokens between two users", async function () {
Expand Down

0 comments on commit dd9865f

Please sign in to comment.