UNPKG

bch-slpjs

Version:

Simple Ledger Protocol (SLP) JavaScript Library

98 lines 3.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const bchaddr = require("bchaddrjs-slp"); const bignumber_js_1 = require("bignumber.js"); class Utils { static toCashAddress(address) { return bchaddr.toCashAddress(address); } static toSlpAddress(address) { return bchaddr.toSlpAddress(address); } static isSlpAddress(address) { return bchaddr.isSlpAddress(address); } static isMainnet(address) { if (bchaddr.decodeAddress(address).network === 'mainnet') return true; return false; } static txnBuilderString(address) { return Utils.isMainnet(address) ? 'bitcoincash' : 'bchtest'; } static mapToSlpAddressUtxoResultArray(bitboxResult) { return bitboxResult.utxos.map(txo => { return { satoshis: txo.satoshis, txid: txo.txid, amount: txo.amount, confirmations: txo.confirmations, height: txo.height, vout: txo.vout, cashAddress: bitboxResult.cashAddress, legacyAddress: bitboxResult.legacyAddress, scriptPubKey: bitboxResult.scriptPubKey }; }); } static mapToUtxoArray(utxos) { return utxos.map(txo => { return { satoshis: new bignumber_js_1.default(txo.satoshis), wif: txo.wif, txid: txo.txid, vout: txo.vout, slpTransactionDetails: txo.slpTransactionDetails, slpUtxoJudgement: txo.slpUtxoJudgement, slpUtxoJudgementAmount: txo.slpUtxoJudgementAmount }; }); } static getPushDataOpcode(data) { let length = data.length; if (length === 0) return [0x4c, 0x00]; else if (length < 76) return length; else if (length < 256) return [0x4c, length]; throw Error("Pushdata too large"); } static int2FixedBuffer(amount) { try { amount.absoluteValue(); } catch (_) { throw Error("Amount must be an instance of BigNumber"); } let hex = amount.toString(16); hex = hex.padStart(16, '0'); return Buffer.from(hex, 'hex'); } // This is for encoding Script in scriptPubKey OP_RETURN scripts, where BIP62.3 does not apply static encodeScript(script) { const bufferSize = script.reduce((acc, cur) => { if (Array.isArray(cur)) return acc + cur.length; else return acc + 1; }, 0); const buffer = Buffer.allocUnsafe(bufferSize); let offset = 0; script.forEach((scriptItem) => { if (Array.isArray(scriptItem)) { scriptItem.forEach((item) => { buffer.writeUInt8(item, offset); offset += 1; }); } else { buffer.writeUInt8(scriptItem, offset); offset += 1; } }); return buffer; } } exports.Utils = Utils; //# sourceMappingURL=utils.js.map