UNPKG

chaingate

Version:

A complete TypeScript library for connecting to and making transactions on different blockchains

81 lines 3.65 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LegacyUtxoPreparedTransaction = void 0; const Utils_1 = require("../../../../../Utils/Utils"); const decimal_js_1 = __importDefault(require("decimal.js")); const btc = __importStar(require("@scure/btc-signer")); const btc_signer_1 = require("@scure/btc-signer"); const UtxoPreparedTransaction_1 = require("../Utxo/UtxoPreparedTransaction"); const UtxoUtils_1 = require("../Utxo/UtxoUtils"); class LegacyUtxoPreparedTransaction extends UtxoPreparedTransaction_1.UtxoPreparedTransaction { constructor(api, currencyProviders, currencyInfo, fromAddress, toAddress, amount, networkParams, privateKeyProvider) { super(api, currencyProviders, currencyInfo, fromAddress, toAddress, amount, networkParams, privateKeyProvider); } toLegacyAddress(address) { return address; } async sign(inputs, outputs) { const txVins = inputs.map(vin => ({ txid: (0, Utils_1.hexToBytes)(vin.txid), index: vin.n, witnessUtxo: { script: vin.script, amount: BigInt((0, UtxoUtils_1.toSatoshi)(new decimal_js_1.default(vin.amount.toString())).toString()) } })); const txVouts = outputs.map(vout => ({ script: btc_signer_1.OutScript.encode(btc.Address(this.networkParams).decode(vout.address)), amount: BigInt((0, UtxoUtils_1.toSatoshi)(vout.amount).toString()) })); const transaction = new btc.Transaction({ allowLegacyWitnessUtxo: true }); for (const txVin of txVins) transaction.addInput(txVin); for (const txVout of txVouts) transaction.addOutput(txVout); const privateKeyRaw = (await this.privateKeyProvider()).raw; for (let i = 0; i < transaction.inputsLength; i++) transaction.signIdx(privateKeyRaw, i); transaction.finalize(); return (0, Utils_1.hexToBytes)(transaction.hex); } } exports.LegacyUtxoPreparedTransaction = LegacyUtxoPreparedTransaction; //# sourceMappingURL=LegacyUtxoPreparedTransaction.js.map