resolves #42 Co-authored-by: johba <johba@harb.eth> Reviewed-on: https://codeberg.org/johba/harb/pulls/49
31 lines
1 KiB
TypeScript
31 lines
1 KiB
TypeScript
export function bytesToUint256LittleEndian(bytes: Uint8Array): bigint {
|
|
// console.log(hexString);
|
|
// const cleanHexString = hexString.startsWith('0x') ? hexString.substring(2) : hexString;
|
|
// const bytes = new Uint8Array(Math.ceil(cleanHexString.length / 2));
|
|
// for (let i = 0, j = 0; i < cleanHexString.length; i += 2, j++) {
|
|
// bytes[j] = parseInt(cleanHexString.slice(i, i + 2), 16);
|
|
// }
|
|
let value: bigint = 0n;
|
|
|
|
for (let i = bytes.length - 1; i >= 0; i--) {
|
|
value = (value << 8n) | BigInt(bytes[i]);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
export function uint256ToBytesLittleEndian(value: bigint): Uint8Array {
|
|
const bytes = new Uint8Array(4);
|
|
for (let i = 0; i < 4; i++) {
|
|
bytes[i] = Number((value >> (8n * BigInt(i))) & 0xffn);
|
|
}
|
|
return bytes;
|
|
// let hexString = '0x';
|
|
|
|
// for (let i = 0; i < bytes.length; i++) {
|
|
// // Convert each byte to a hexadecimal string and pad with zero if needed
|
|
// hexString += bytes[i].toString(16).padStart(2, '0');
|
|
// }
|
|
|
|
// return hexString;
|
|
}
|