fix: build uint256 instead of uint64 for commitment id

This commit is contained in:
Arseniy Klempner 2024-04-06 15:10:30 +03:00
parent 6b20df5257
commit 25607793e7
4 changed files with 18 additions and 5 deletions

View File

@ -1,4 +1,4 @@
import { buildBigIntFromUint8Array } from "./utils/index.js"; import { uint256FromBytes } from "./utils/bytes.js";
export class IdentityCredential { export class IdentityCredential {
constructor( constructor(
@ -14,7 +14,7 @@ export class IdentityCredential {
const idNullifier = memKeys.subarray(32, 64); const idNullifier = memKeys.subarray(32, 64);
const idSecretHash = memKeys.subarray(64, 96); const idSecretHash = memKeys.subarray(64, 96);
const idCommitment = memKeys.subarray(96); const idCommitment = memKeys.subarray(96);
const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment, 96); const idCommitmentBigInt = uint256FromBytes(idCommitment);
return new IdentityCredential( return new IdentityCredential(
idTrapdoor, idTrapdoor,

View File

@ -14,7 +14,7 @@ import {
import _ from "lodash"; import _ from "lodash";
import { v4 as uuidV4 } from "uuid"; import { v4 as uuidV4 } from "uuid";
import { buildBigIntFromUint8Array } from "../utils/bytes.js"; import { uint256FromBytes } from "../utils/bytes.js";
import { decryptEipKeystore, keccak256Checksum } from "./cipher.js"; import { decryptEipKeystore, keccak256Checksum } from "./cipher.js";
import { isCredentialValid, isKeystoreValid } from "./schema_validator.js"; import { isCredentialValid, isKeystoreValid } from "./schema_validator.js";
@ -262,7 +262,7 @@ export class Keystore {
IDNullifier: Keystore.fromArraylikeToBytes( IDNullifier: Keystore.fromArraylikeToBytes(
_.get(obj, "identityCredential.idNullifier", []) _.get(obj, "identityCredential.idNullifier", [])
), ),
IDCommitmentBigInt: buildBigIntFromUint8Array( IDCommitmentBigInt: uint256FromBytes(
Keystore.fromArraylikeToBytes( Keystore.fromArraylikeToBytes(
_.get(obj, "identityCredential.idCommitment", []) _.get(obj, "identityCredential.idCommitment", [])
) )

View File

@ -69,6 +69,19 @@ export function buildBigIntFromUint8Array(
return dataView.getBigUint64(byteOffset, true); return dataView.getBigUint64(byteOffset, true);
} }
export function uint256FromBytes(bytes: Uint8Array): bigint {
if (bytes.length !== 32) {
throw new Error("Invalid uint256 byte length");
}
let value = BigInt(0);
for (let i = 0; i < 32; i++) {
value |= BigInt(bytes[i]) << BigInt(i * 8);
}
return value as bigint;
}
/** /**
* Fills with zeros to set length * Fills with zeros to set length
* @param array little endian Uint8Array * @param array little endian Uint8Array

View File

@ -119,7 +119,7 @@ export class Zerokit {
"zerokit/rln/010203040506070809" "zerokit/rln/010203040506070809"
); );
const fetchUrl = `${process.env.MERKLE_PROOF_SERVICE_URL || "http://localhost:8645/debug/v1/merkleProof"}/${idCommitment}`; const fetchUrl = `http://localhost:8645/debug/v1/merkleProof/${idCommitment}`;
const response = await fetch(fetchUrl); const response = await fetch(fetchUrl);
const proofData = await response.json(); const proofData = await response.json();