UNPKG

@btc-vision/transaction

Version:

OPNet transaction library allows you to create and sign transactions for the OPNet network.

72 lines (51 loc) 2.2 kB
import { U256_BYTE_LENGTH } from './lengths.js'; import { MemorySlotPointer } from './types.js'; export class BufferHelper { public static readonly EXPECTED_BUFFER_LENGTH: number = 32; public static bufferToUint8Array(buffer: Buffer | Uint8Array): Uint8Array { if (Buffer.isBuffer(buffer)) { const length: number = buffer.byteLength; const arrayBuffer: ArrayBuffer = new ArrayBuffer(length); const view: Uint8Array = new Uint8Array(arrayBuffer); for (let i = 0; i < length; ++i) { view[i] = buffer[i]; } return view; } return buffer; } public static uint8ArrayToHex(input: Uint8Array): string { return Buffer.from(input.buffer, 0, input.byteLength).toString('hex'); } public static hexToUint8Array(input: string): Uint8Array { if (input.startsWith('0x')) { input = input.substring(2); // Remove the '0x' prefix } if (input.length % 2 !== 0) { input = '0' + input; // Pad with a leading zero if the length is odd } const lengthS = input.length / 2; const buffer = new Uint8Array(lengthS); for (let i = 0; i < lengthS; i++) { buffer[i] = parseInt(input.substring(i * 2, i * 2 + 2), 16); } return buffer; } public static pointerToUint8Array(pointer: MemorySlotPointer): Uint8Array { const pointerHex = pointer.toString(16).padStart(64, '0'); return BufferHelper.hexToUint8Array(pointerHex); } public static uint8ArrayToPointer(input: Uint8Array): MemorySlotPointer { const hex = BufferHelper.uint8ArrayToHex(input); return BigInt('0x' + hex); } public static valueToUint8Array(value: bigint, length: number = U256_BYTE_LENGTH): Uint8Array { const valueHex = value.toString(16).padStart(length * 2, '0'); return BufferHelper.hexToUint8Array(valueHex); } public static uint8ArrayToValue(input: Uint8Array): bigint { const hex = BufferHelper.uint8ArrayToHex(input); if (!hex) return BigInt(0); return BigInt('0x' + hex); } }