UNPKG

@btc-vision/transaction

Version:

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

52 lines (51 loc) 1.78 kB
import { U256_BYTE_LENGTH } from './lengths.js'; export class BufferHelper { static bufferToUint8Array(buffer) { if (Buffer.isBuffer(buffer)) { const length = buffer.byteLength; const arrayBuffer = new ArrayBuffer(length); const view = new Uint8Array(arrayBuffer); for (let i = 0; i < length; ++i) { view[i] = buffer[i]; } return view; } return buffer; } static uint8ArrayToHex(input) { return Buffer.from(input.buffer, 0, input.byteLength).toString('hex'); } static hexToUint8Array(input) { if (input.startsWith('0x')) { input = input.substring(2); } if (input.length % 2 !== 0) { input = '0' + input; } 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; } static pointerToUint8Array(pointer) { const pointerHex = pointer.toString(16).padStart(64, '0'); return BufferHelper.hexToUint8Array(pointerHex); } static uint8ArrayToPointer(input) { const hex = BufferHelper.uint8ArrayToHex(input); return BigInt('0x' + hex); } static valueToUint8Array(value, length = U256_BYTE_LENGTH) { const valueHex = value.toString(16).padStart(length * 2, '0'); return BufferHelper.hexToUint8Array(valueHex); } static uint8ArrayToValue(input) { const hex = BufferHelper.uint8ArrayToHex(input); if (!hex) return BigInt(0); return BigInt('0x' + hex); } } BufferHelper.EXPECTED_BUFFER_LENGTH = 32;