From 25607793e7775b019db96fa4e09218c9405b3e12 Mon Sep 17 00:00:00 2001 From: Arseniy Klempner Date: Sat, 6 Apr 2024 15:10:30 +0300 Subject: [PATCH] fix: build uint256 instead of uint64 for commitment id --- src/identity.ts | 4 ++-- src/keystore/keystore.ts | 4 ++-- src/utils/bytes.ts | 13 +++++++++++++ src/zerokit.ts | 2 +- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/identity.ts b/src/identity.ts index ecd64ca..72956d0 100644 --- a/src/identity.ts +++ b/src/identity.ts @@ -1,4 +1,4 @@ -import { buildBigIntFromUint8Array } from "./utils/index.js"; +import { uint256FromBytes } from "./utils/bytes.js"; export class IdentityCredential { constructor( @@ -14,7 +14,7 @@ export class IdentityCredential { const idNullifier = memKeys.subarray(32, 64); const idSecretHash = memKeys.subarray(64, 96); const idCommitment = memKeys.subarray(96); - const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment, 96); + const idCommitmentBigInt = uint256FromBytes(idCommitment); return new IdentityCredential( idTrapdoor, diff --git a/src/keystore/keystore.ts b/src/keystore/keystore.ts index 7951398..abacd44 100644 --- a/src/keystore/keystore.ts +++ b/src/keystore/keystore.ts @@ -14,7 +14,7 @@ import { import _ from "lodash"; 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 { isCredentialValid, isKeystoreValid } from "./schema_validator.js"; @@ -262,7 +262,7 @@ export class Keystore { IDNullifier: Keystore.fromArraylikeToBytes( _.get(obj, "identityCredential.idNullifier", []) ), - IDCommitmentBigInt: buildBigIntFromUint8Array( + IDCommitmentBigInt: uint256FromBytes( Keystore.fromArraylikeToBytes( _.get(obj, "identityCredential.idCommitment", []) ) diff --git a/src/utils/bytes.ts b/src/utils/bytes.ts index 279a2b3..7aaf878 100644 --- a/src/utils/bytes.ts +++ b/src/utils/bytes.ts @@ -69,6 +69,19 @@ export function buildBigIntFromUint8Array( 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 * @param array little endian Uint8Array diff --git a/src/zerokit.ts b/src/zerokit.ts index da9b1d5..d167e82 100644 --- a/src/zerokit.ts +++ b/src/zerokit.ts @@ -119,7 +119,7 @@ export class Zerokit { "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 proofData = await response.json();