From fc63064670d841a7c7b24c6620fc9b36cb221ed9 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 1 Dec 2023 13:49:40 +0100 Subject: [PATCH] fix problem with zeroPad --- example/package-lock.json | 1 + src/byte_utils.ts | 14 ++++++++++++++ src/rln_contract.ts | 3 ++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/example/package-lock.json b/example/package-lock.json index 19f2037..2566290 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -19,6 +19,7 @@ } }, "..": { + "name": "@waku/rln", "version": "0.1.1", "license": "MIT OR Apache-2.0", "dependencies": { diff --git a/src/byte_utils.ts b/src/byte_utils.ts index 67cbb75..2e7e52b 100644 --- a/src/byte_utils.ts +++ b/src/byte_utils.ts @@ -47,3 +47,17 @@ export function buildBigIntFromUint8Array(array: Uint8Array): bigint { const dataView = new DataView(array.buffer); return dataView.getBigUint64(0, true); } + +/** + * Fills with zeros to set length + * @param array little endian Uint8Array + * @param length amount to pad + * @returns little endian Uint8Array padded with zeros to set length + */ +export function zeroPadLE(array: Uint8Array, length: number): Uint8Array { + const result = new Uint8Array(length); + for (let i = 0; i < length; i++) { + result[i] = array[i] || 0; + } + return result; +} diff --git a/src/rln_contract.ts b/src/rln_contract.ts index 798f370..6a53fa9 100644 --- a/src/rln_contract.ts +++ b/src/rln_contract.ts @@ -1,6 +1,7 @@ import { hexToBytes } from "@waku/utils/bytes"; import { ethers } from "ethers"; +import { zeroPadLE } from "./byte_utils.js"; import { RLN_REGISTRY_ABI, RLN_STORAGE_ABI } from "./constants.js"; import { IdentityCredential, RLNInstance } from "./rln.js"; import { MerkleRootTracker } from "./root_tracker.js"; @@ -170,7 +171,7 @@ export class RLNContract { return; } - const idCommitment = hexToBytes(_idCommitment?._hex); + const idCommitment = zeroPadLE(hexToBytes(_idCommitment?._hex), 32); rlnInstance.insertMember(idCommitment); this._members.set(index.toNumber(), { index,