add test when you successfuly mint sbt

This commit is contained in:
motemotech
2024-10-08 21:22:17 +09:00
parent 6e8d134a70
commit ca2d7bcfab
3 changed files with 91 additions and 73 deletions

View File

@@ -100,10 +100,23 @@ contract OneTimeSBT is ERC721Enumerable {
attributes.values[4] = AttributeLibrary.getDateOfBirth(charcodes);
attributes.values[5] = AttributeLibrary.getGender(charcodes);
attributes.values[6] = AttributeLibrary.getExpiryDate(charcodes);
attributes.values[7] = AttributeLibrary.getOlderThan(charcodes);
uint[] memory olderThanAscii = new uint[](2);
olderThanAscii[0] = p_proof.pubSignals[PROVE_RSA_OLDER_THAN_INDEX];
olderThanAscii[1] = p_proof.pubSignals[PROVE_RSA_OLDER_THAN_INDEX + 1];
attributes.values[7] = convertUintArrayToString(olderThanAscii);
sbtExpiration[newTokenId] = block.timestamp + 90 days;
}
function convertUintArrayToString(uint[] memory input) internal pure returns (string memory) {
bytes memory bytesArray = new bytes(input.length);
for (uint i = 0; i < input.length; i++) {
// 確実に uint8 に収まるようにキャスト
bytesArray[i] = bytes1(uint8(input[i]));
}
return string(bytesArray);
}
function fieldElementsToBytes(
uint256[3] memory publicSignals

View File

@@ -93,15 +93,6 @@ library AttributeLibrary {
return extractAttribute(charcodes, EXPIRY_DATE_START, EXPIRY_DATE_END);
}
/**
* @notice Extracts the "older than" attribute from the charcodes.
* @param charcodes The bytes array containing packed attribute data.
* @return The "older than" value as a string.
*/
function getOlderThan(bytes memory charcodes) internal pure returns (string memory) {
return extractAttribute(charcodes, OLDER_THAN_START, OLDER_THAN_END);
}
/**
* @notice Extracts a substring from the charcodes based on start and end indices.
* @param charcodes The bytes array containing packed attribute data.

View File

@@ -4,11 +4,12 @@ import { BigNumberish, Block, dataLength } from "ethers";
import { genMockPassportData } from "../../common/src/utils/genMockPassportData";
import {
generateCircuitInputsProve,
generateCircuitInputsDisclose
generateCircuitInputsDisclose,
} from "../../common/src/utils/generateInputs";
import {
generateMockRSAProveVerifierInputs,
generateMockDSCVerifierInputs
generateMockDSCVerifierInputs,
convertProofTypeIntoInput
} from "../../common/src/utils/unitTest/generateMockProof";
import { getCSCAModulusMerkleTree } from "../../common/src/utils/csca";
import { formatRoot } from "../../common/src/utils/utils";
@@ -49,6 +50,22 @@ describe("Unit test for OneTimeSBT.sol", function() {
console.log('\x1b[34m%s\x1b[0m', `sbt deployed to ${oneTimeSBT.target}`);
});
async function initializeOneTimeSBT() {
console.log('\x1b[34m%s\x1b[0m', `Initializing OneTimeSBT contract`);
const sbtFactory = await ethers.getContractFactory("OneTimeSBT");
oneTimeSBT = await sbtFactory.deploy(
verifiersManager,
formatter
);
await oneTimeSBT.waitForDeployment();
console.log('\x1b[34m%s\x1b[0m', `sbt deployed to ${oneTimeSBT.target}`);
}
function removeNullCharacters(str: string): string {
return str.replace(/\u0000/g, ''); // ヌル文字を削除
}
describe("Test mint function", async function () {
it("Should revert if current time is +2 dyas", async function() {
@@ -57,20 +74,10 @@ describe("Unit test for OneTimeSBT.sol", function() {
let mockRSAProveVerifierProof = generateMockRSAProveVerifierInputs({
current_date: twoDaysPassed
});
let p_proof = {
a: mockRSAProveVerifierProof.proof.a,
b: mockRSAProveVerifierProof.proof.b,
c: mockRSAProveVerifierProof.proof.c,
pubSignals: mockRSAProveVerifierProof.pub_signals
}
let p_proof = convertProofTypeIntoInput(mockRSAProveVerifierProof);
let mockDscVerifierProof = generateMockDSCVerifierInputs({});
let d_proof = {
a: mockDscVerifierProof.proof.a,
b: mockDscVerifierProof.proof.b,
c: mockDscVerifierProof.proof.c,
pubSignals: mockDscVerifierProof.pub_signals
}
let d_proof = convertProofTypeIntoInput(mockDscVerifierProof);
await expect(
oneTimeSBT.mint(
@@ -90,20 +97,10 @@ describe("Unit test for OneTimeSBT.sol", function() {
let mockRSAProveVerifierProof = generateMockRSAProveVerifierInputs({
current_date: twoDaysBefore
});
let p_proof = {
a: mockRSAProveVerifierProof.proof.a,
b: mockRSAProveVerifierProof.proof.b,
c: mockRSAProveVerifierProof.proof.c,
pubSignals: mockRSAProveVerifierProof.pub_signals
}
let p_proof = convertProofTypeIntoInput(mockRSAProveVerifierProof);
let mockDscVerifierProof = generateMockDSCVerifierInputs({});
let d_proof = {
a: mockDscVerifierProof.proof.a,
b: mockDscVerifierProof.proof.b,
c: mockDscVerifierProof.proof.c,
pubSignals: mockDscVerifierProof.pub_signals
}
let d_proof = convertProofTypeIntoInput(mockDscVerifierProof);
await expect(
oneTimeSBT.mint(
@@ -122,22 +119,12 @@ describe("Unit test for OneTimeSBT.sol", function() {
let mockRSAProveVerifierProof = generateMockRSAProveVerifierInputs({
blinded_dsc_commitment: "42"
});
let p_proof = {
a: mockRSAProveVerifierProof.proof.a,
b: mockRSAProveVerifierProof.proof.b,
c: mockRSAProveVerifierProof.proof.c,
pubSignals: mockRSAProveVerifierProof.pub_signals
}
let p_proof = convertProofTypeIntoInput(mockRSAProveVerifierProof);
let mockDscVerifierProof = generateMockDSCVerifierInputs({
blinded_dsc_commitment: "43"
});
let d_proof = {
a: mockDscVerifierProof.proof.a,
b: mockDscVerifierProof.proof.b,
c: mockDscVerifierProof.proof.c,
pubSignals: mockDscVerifierProof.pub_signals
}
let d_proof = convertProofTypeIntoInput(mockDscVerifierProof);
await expect(
oneTimeSBT.mint(
@@ -154,20 +141,10 @@ describe("Unit test for OneTimeSBT.sol", function() {
it("Should revert if prove verifier returns false", async function() {
let mockRSAProveVerifierProof = generateMockRSAProveVerifierInputs({});
let p_proof = {
a: mockRSAProveVerifierProof.proof.a,
b: mockRSAProveVerifierProof.proof.b,
c: mockRSAProveVerifierProof.proof.c,
pubSignals: mockRSAProveVerifierProof.pub_signals
}
let p_proof = convertProofTypeIntoInput(mockRSAProveVerifierProof);
let mockDscVerifierProof = generateMockDSCVerifierInputs({});
let d_proof = {
a: mockDscVerifierProof.proof.a,
b: mockDscVerifierProof.proof.b,
c: mockDscVerifierProof.proof.c,
pubSignals: mockDscVerifierProof.pub_signals
}
let d_proof = convertProofTypeIntoInput(mockDscVerifierProof);
await expect(
oneTimeSBT.mint(
@@ -184,20 +161,10 @@ describe("Unit test for OneTimeSBT.sol", function() {
it("Should revert if dsc veriifier returns false", async function() {
let mockRSAProveVerifierProof = generateMockRSAProveVerifierInputs({});
let p_proof = {
a: mockRSAProveVerifierProof.proof.a,
b: mockRSAProveVerifierProof.proof.b,
c: mockRSAProveVerifierProof.proof.c,
pubSignals: mockRSAProveVerifierProof.pub_signals
}
let p_proof = convertProofTypeIntoInput(mockRSAProveVerifierProof);
let mockDscVerifierProof = generateMockDSCVerifierInputs({});
let d_proof = {
a: mockDscVerifierProof.proof.a,
b: mockDscVerifierProof.proof.b,
c: mockDscVerifierProof.proof.c,
pubSignals: mockDscVerifierProof.pub_signals
}
let d_proof = convertProofTypeIntoInput(mockDscVerifierProof);
await expect(
oneTimeSBT.mint(
@@ -211,6 +178,53 @@ describe("Unit test for OneTimeSBT.sol", function() {
"INVALID_DSC_PROOF"
);
});
it("Should be able to mint after passed all validations and parameters are set correctly", async function() {
let mockRSAProveVerifierProof = generateMockRSAProveVerifierInputs({
revealedData_packed: [
"114847304187799599204038932047563106716216648531307757482805904045144161360",
"90441810240571260447108696195211715124097440720966537976235069762603139660",
"80649680061362283084530183871589323447324161052952025077985585"
],
user_identifier: addr1.address
});
let p_proof = convertProofTypeIntoInput(mockRSAProveVerifierProof);
let mockDscVerifierProof = generateMockDSCVerifierInputs({});
let d_proof = convertProofTypeIntoInput(mockDscVerifierProof);
let beforeTotalSupply = await oneTimeSBT.totalSupply();
const tx = await oneTimeSBT.mint(
TRUE_VERIFIER_ID,
TRUE_VERIFIER_ID,
p_proof,
d_proof
);
const receipt = await tx.wait();
let totalSupply = await oneTimeSBT.totalSupply();
let thisTokenId = totalSupply - 1n;
expect(totalSupply).to.be.equal(beforeTotalSupply + 1n);
expect(await oneTimeSBT.ownerOf(thisTokenId)).to.be.equal(addr1.address);
expect(await oneTimeSBT.getIssuingStateOf(thisTokenId)).to.be.equal("FRA");
expect(removeNullCharacters(await oneTimeSBT.getNameOf(thisTokenId))).to.be.equal("DUPONTALPHONSEHUGHUESALBERT");
expect(await oneTimeSBT.getPassportNumberOf(thisTokenId)).to.be.equal("15AA81234");
expect(await oneTimeSBT.getNationalityOf(thisTokenId)).to.be.equal("FRA");
expect(await oneTimeSBT.getDateOfBirthOf(thisTokenId)).to.be.equal("001031");
expect(await oneTimeSBT.getGenderOf(thisTokenId)).to.be.equal("M");
expect(await oneTimeSBT.getExpiryDateOf(thisTokenId)).to.be.equal("401010");
expect(await oneTimeSBT.getOlderThanOf(thisTokenId)).to.be.equal("18");
const mintBlockNumber = receipt.blockNumber;
const mintBlock = await ethers.provider.getBlock(mintBlockNumber) as Block;
const mintTimestamp = mintBlock.timestamp;
const expectedExpiration = mintTimestamp + 90 * 24 * 60 * 60; // 90 days = 90 * 24 * 60 * 60 seconds
expect(await oneTimeSBT.sbtExpiration(thisTokenId)).to.equal(expectedExpiration);
});
});
describe("Test util functions", async function () {
@@ -228,4 +242,4 @@ describe("Unit test for OneTimeSBT.sol", function() {
describe("Test attrs are correctly registerd", async function () {
})
});
});