mirror of
https://github.com/selfxyz/self.git
synced 2026-01-13 00:28:17 -05:00
* Add Prettier configuration and ignore files for code formatting - Created .prettierignore to exclude specific directories and files from formatting. - Added .prettierrc.yml with custom settings for print width and trailing commas. - Updated package.json to include Prettier and its Solidity plugin as dependencies, along with scripts for formatting and checking code. * Run prettier formatting
35 lines
1.0 KiB
TypeScript
35 lines
1.0 KiB
TypeScript
import { randomBytes, toBigInt, toBeHex, zeroPadValue } from "ethers";
|
|
|
|
export function generateRandomFieldElement(): string {
|
|
const FIELD_PRIME = BigInt("21888242871839275222246405745257275088696311157297823662689037894645226208583");
|
|
|
|
const fieldElement = zeroPadValue(toBeHex(toBigInt(randomBytes(32)) % FIELD_PRIME), 32);
|
|
|
|
return fieldElement;
|
|
}
|
|
|
|
export function getStartOfDayTimestamp(timestamp: number): number {
|
|
const dayInSeconds = 86400;
|
|
return timestamp - (timestamp % dayInSeconds);
|
|
}
|
|
|
|
export function splitHexFromBack(hexString: string, bytesPerChunk: number = 31): string[] {
|
|
if (hexString.startsWith("0x")) {
|
|
hexString = hexString.slice(2);
|
|
}
|
|
|
|
const chunkSizeHex = bytesPerChunk * 2;
|
|
const chunks: string[] = [];
|
|
|
|
let remaining = hexString;
|
|
while (remaining.length > 0) {
|
|
const chunk = remaining.slice(-chunkSizeHex);
|
|
remaining = remaining.slice(0, -chunkSizeHex);
|
|
|
|
const paddedChunk = chunk.padStart(64, "0");
|
|
chunks.push("0x" + paddedChunk);
|
|
}
|
|
|
|
return chunks;
|
|
}
|