update function name and function to extract forbidden countries

This commit is contained in:
motemotech
2024-10-21 00:29:55 +09:00
parent 19722c38cb
commit c895d2ee77
3 changed files with 46 additions and 14 deletions

View File

@@ -244,7 +244,7 @@ contract OpenPassportVerifier is IOpenPassportVerifier {
revert INVALID_SIGNATURE_TYPE();
}
}
bytes memory charcodes = OpenPassportFormatter.fieldElementsToBytes(
bytes memory charcodes = OpenPassportFormatter.revealedDataPackedToBytes(
revealedData_packed
);

View File

@@ -8,17 +8,16 @@ library OpenPassportFormatter {
uint256 constant FORBIDDEN_COUNTRIES_LIST_LENGTH = 20;
// TODO: Handle country code in bytes3 data type
function extractForbiddenCountries(
bytes memory charcodes
) internal pure returns (string[] memory) {
string[] memory forbiddenCountries = new string[](20);
) internal pure returns (bytes3[] memory) {
bytes3[] memory forbiddenCountries = new string[](FORBIDDEN_COUNTRIES_LIST_LENGTH);
for (uint256 i = 0; i < FORBIDDEN_COUNTRIES_LIST_LENGTH; i++) {
bytes memory attributeBytes = new bytes(3);
bytes3 countryCode;
for (uint256 j = 0; j < 3; j++) {
attributeBytes[j] = charcodes[i * 3 + j];
countryCode |= bytes1(charcodes[i * 3 + j]) << (j * 8);
}
forbiddenCountries[i] = string(attributeBytes);
forbiddenCountries[i] = countryCode;
}
return forbiddenCountries;
}
@@ -71,16 +70,16 @@ library OpenPassportFormatter {
}
function numAsciiToUint(uint256 numAscii) internal pure returns (uint256) {
if (numAscii < 48 && numAscii > 57) {
if (numAscii < 48 || numAscii > 57) {
revert InvalidAsciiCode();
}
return (numAscii - 48);
}
function forbiddenCountriesfieldElementsToBytes(
function forbiddenCountriesPackedToBytes(
uint256[2] memory publicSignals
) internal pure returns (bytes memory) {
uint8[3] memory bytesCount = [31, 29];
uint8[2] memory bytesCount = [31, 29];
bytes memory bytesArray = new bytes(60);
uint256 index = 0;
@@ -95,8 +94,7 @@ library OpenPassportFormatter {
return bytesArray;
}
// TODO: This function is only work for revealedData_packed, need to cahnge name and define similar function for forbidden coutries
function fieldElementsToBytes(
function revealedDataPackedToBytes(
uint256[3] memory publicSignals
) internal pure returns (bytes memory) {
uint8[3] memory bytesCount = [31, 31, 28];

View File

@@ -17,8 +17,10 @@ import {
} from "../../../common/src/utils/csca";
import { mock_dsc_sha256_rsa_4096 } from "../../../common/src/constants/mockCertificates";
import { generateCircuitInputsProve } from "../../../common/src/utils/generateInputs";
import { buildSMT, importSMTFromJsonFile, exportSMTToJsonFile } from "../../../common/src/utils/smtTree";
import { SMT } from "@ashpect/smt";
import { buildSMT } from "../../../common/src/utils/smtTree";
import { SMT, ChildNodes } from "@ashpect/smt";
import path from "path";
import { poseidon3, poseidon2 } from "poseidon-lite"
type CircuitArtifacts = {
[key: string]: {
@@ -448,3 +450,35 @@ describe("Test one time verification flow", async function () {
}
});
export function exportSMTToJsonFile(count: number, time: number, smt: SMT, outputPath?: string) {
const serializedSMT = smt.export();
const data = {
count: count,
time: time,
smt: serializedSMT
};
const jsonString = JSON.stringify(data, null, 2);
const defaultPath = path.join(process.cwd(), 'smt.json');
const finalPath = outputPath ? path.resolve(process.cwd(), outputPath) : defaultPath;
fs.writeFileSync(finalPath, jsonString, 'utf8');
}
export function importSMTFromJsonFile(filePath?: string): SMT | null {
try {
const jsonString = fs.readFileSync(path.resolve(process.cwd(), filePath as string), 'utf8');
const data = JSON.parse(jsonString);
const hash2 = (childNodes: ChildNodes) => (childNodes.length === 2 ? poseidon2(childNodes) : poseidon3(childNodes));
const smt = new SMT(hash2, true);
smt.import(data.smt);
console.log('Successfully imported SMT from JSON file');
return smt;
} catch (error) {
console.error('Failed to import SMT from JSON file:', error);
return null;
}
}