diff --git a/src/rln.ts b/src/rln.ts index ce31e86..abc249d 100644 --- a/src/rln.ts +++ b/src/rln.ts @@ -26,6 +26,20 @@ function concatenate(...input: Uint8Array[]): Uint8Array { return result; } +/** + * Transforms Uint8Array into BigInt + * @param array: Uint8Array + * @returns BigInt + */ +function buildBigIntFromUint8Array(array: Uint8Array): bigint { + const bigEndianArray = array.reverse(); + // hack for Uint8Array.map that has Uint8Array -> Uint8Array definition that prevents from mapping to other types + const hexString = (bigEndianArray as unknown as number[]) + .map((b) => b.toString(16).padStart(2, "0")) + .join(); + return BigInt(`0x${hexString}`); +} + const stringEncoder = new TextEncoder(); const DEPTH = 20; @@ -59,13 +73,15 @@ export async function create(): Promise { export class MembershipKey { constructor( public readonly IDKey: Uint8Array, - public readonly IDCommitment: Uint8Array + public readonly IDCommitment: Uint8Array, + public readonly IDCommitmentBigInt: bigint ) {} static fromBytes(memKeys: Uint8Array): MembershipKey { const idKey = memKeys.subarray(0, 32); const idCommitment = memKeys.subarray(32); - return new MembershipKey(idKey, idCommitment); + const idCommitmentBitInt = buildBigIntFromUint8Array(idCommitment); + return new MembershipKey(idKey, idCommitment, idCommitmentBitInt); } } diff --git a/src/rln_contract.ts b/src/rln_contract.ts index b9dc36c..7ef0b9c 100644 --- a/src/rln_contract.ts +++ b/src/rln_contract.ts @@ -91,7 +91,7 @@ export class RLNContract { const membershipKey = await rlnInstance.generateSeededMembershipKey( signature ); - const pubkey = ethers.BigNumber.from(membershipKey.IDCommitment); + const pubkey = ethers.BigNumber.from(membershipKey.IDCommitmentBigInt); const depositValue = await this.contract.MEMBERSHIP_DEPOSIT(); const txRegisterResponse: ethers.ContractTransaction =