UNPKG

@btc-vision/transaction

Version:

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

39 lines (38 loc) 1.48 kB
import { createHash } from 'crypto'; const hexPattern = /^[0-9a-fA-F]+$/; export class BitcoinUtils { static btcToSatoshi(btc) { return BigInt(btc * 100000000); } static rndBytes() { const buf = BitcoinUtils.getSafeRandomValues(64); return Buffer.from(buf); } static getSafeRandomValues(length) { if (typeof globalThis.window !== 'undefined' && globalThis.window.crypto && typeof globalThis.window.crypto.getRandomValues === 'function') { const array = new Uint8Array(length); window.crypto.getRandomValues(array); return Buffer.from(array); } else if (globalThis.crypto && typeof globalThis.crypto.getRandomValues === 'function') { const array = new Uint8Array(length); globalThis.crypto.getRandomValues(array); return Buffer.from(array); } else { console.log(`No secure random number generator available. Please upgrade your environment.`, globalThis.window.crypto, globalThis.crypto); throw new Error('No secure random number generator available. Please upgrade your environment.'); } } static isValidHex(hex) { return hexPattern.test(hex); } static opnetHash(data) { const hashed = createHash('sha512'); hashed.update(data); const hash = hashed.digest(); return `0x${Buffer.from(hash).toString('hex')}`; } }