mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-02 13:53:12 +00:00
fix: use viem for bytes32/bigint conversion
This commit is contained in:
parent
a20fac086e
commit
ed996117d8
@ -77,14 +77,14 @@ describe("RLN Proof Integration Tests", function () {
|
||||
BytesUtils.bytes32FromBigInt(element, "little")
|
||||
),
|
||||
proofElementIndexes.map((index) =>
|
||||
BytesUtils.writeUIntLE(new Uint8Array(1), index, 0, 1)
|
||||
BytesUtils.writeUintLE(new Uint8Array(1), index, 0, 1)
|
||||
),
|
||||
Number(rateLimit),
|
||||
0
|
||||
);
|
||||
|
||||
const isValid = rlnInstance.zerokit.verifyRLNProof(
|
||||
BytesUtils.writeUIntLE(new Uint8Array(8), testMessage.length, 0, 8),
|
||||
BytesUtils.writeUintLE(new Uint8Array(8), testMessage.length, 0, 8),
|
||||
testMessage,
|
||||
proof,
|
||||
[BytesUtils.bytes32FromBigInt(merkleRoot, "little")]
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import { bytesToBigInt, numberToBytes } from "viem";
|
||||
export class BytesUtils {
|
||||
/**
|
||||
* Concatenate Uint8Arrays
|
||||
@ -32,21 +33,13 @@ export class BytesUtils {
|
||||
return 0n;
|
||||
}
|
||||
|
||||
// Create a copy to avoid modifying the original array
|
||||
const workingBytes = new Uint8Array(bytes);
|
||||
|
||||
// Reverse bytes if input is little-endian to work with big-endian internally
|
||||
if (inputEndianness === "little") {
|
||||
workingBytes.reverse();
|
||||
}
|
||||
|
||||
// Convert to BigInt
|
||||
let result = 0n;
|
||||
for (let i = 0; i < workingBytes.length; i++) {
|
||||
result = (result << 8n) | BigInt(workingBytes[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
return bytesToBigInt(workingBytes, { size: 32 });
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,18 +62,7 @@ export class BytesUtils {
|
||||
);
|
||||
}
|
||||
|
||||
if (value === 0n) {
|
||||
return new Uint8Array(32);
|
||||
}
|
||||
|
||||
const result = new Uint8Array(32);
|
||||
let workingValue = value;
|
||||
|
||||
// Extract bytes in big-endian order
|
||||
for (let i = 31; i >= 0; i--) {
|
||||
result[i] = Number(workingValue & 0xffn);
|
||||
workingValue = workingValue >> 8n;
|
||||
}
|
||||
const result = numberToBytes(value, { size: 32 });
|
||||
|
||||
// If we need little-endian output, reverse the array
|
||||
if (outputEndianness === "little") {
|
||||
@ -93,7 +75,7 @@ export class BytesUtils {
|
||||
/**
|
||||
* Writes an unsigned integer to a buffer in little-endian format
|
||||
*/
|
||||
public static writeUIntLE(
|
||||
public static writeUintLE(
|
||||
buf: Uint8Array,
|
||||
value: number,
|
||||
offset: number,
|
||||
|
||||
@ -17,7 +17,7 @@ export function dateToEpoch(
|
||||
}
|
||||
|
||||
export function epochIntToBytes(epoch: number): Uint8Array {
|
||||
return BytesUtils.writeUIntLE(new Uint8Array(32), epoch, 0, 32);
|
||||
return BytesUtils.writeUintLE(new Uint8Array(32), epoch, 0, 32);
|
||||
}
|
||||
|
||||
export function epochBytesToInt(bytes: Uint8Array): number {
|
||||
|
||||
@ -3,7 +3,7 @@ import { hash, poseidonHash as poseidon } from "@waku/zerokit-rln-wasm-utils";
|
||||
import { BytesUtils } from "./bytes.js";
|
||||
|
||||
export function poseidonHash(...input: Array<Uint8Array>): Uint8Array {
|
||||
const inputLen = BytesUtils.writeUIntLE(
|
||||
const inputLen = BytesUtils.writeUintLE(
|
||||
new Uint8Array(8),
|
||||
input.length,
|
||||
0,
|
||||
|
||||
@ -52,7 +52,7 @@ export class Zerokit {
|
||||
sha256(this.rlnIdentifier)
|
||||
);
|
||||
const pathElementsBytes = new Uint8Array(8 + pathElements.length * 32);
|
||||
BytesUtils.writeUIntLE(pathElementsBytes, pathElements.length, 0, 8);
|
||||
BytesUtils.writeUintLE(pathElementsBytes, pathElements.length, 0, 8);
|
||||
for (let i = 0; i < pathElements.length; i++) {
|
||||
// We assume that the path elements are already in little-endian format
|
||||
pathElementsBytes.set(pathElements[i], 8 + i * 32);
|
||||
@ -60,7 +60,7 @@ export class Zerokit {
|
||||
const identityPathIndexBytes = new Uint8Array(
|
||||
8 + identityPathIndex.length * 1
|
||||
);
|
||||
BytesUtils.writeUIntLE(
|
||||
BytesUtils.writeUintLE(
|
||||
identityPathIndexBytes,
|
||||
identityPathIndex.length,
|
||||
0,
|
||||
@ -73,8 +73,8 @@ export class Zerokit {
|
||||
const x = sha256(msg);
|
||||
return BytesUtils.concatenate(
|
||||
idSecretHash,
|
||||
BytesUtils.writeUIntLE(new Uint8Array(32), rateLimit, 0, 32),
|
||||
BytesUtils.writeUIntLE(new Uint8Array(32), messageId, 0, 32),
|
||||
BytesUtils.writeUintLE(new Uint8Array(32), rateLimit, 0, 32),
|
||||
BytesUtils.writeUintLE(new Uint8Array(32), messageId, 0, 32),
|
||||
pathElementsBytes,
|
||||
identityPathIndexBytes,
|
||||
x,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user