UNPKG

atomicals-js

Version:
188 lines (187 loc) 9.01 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.customColorInteractiveCommand = void 0; const ecc = __importStar(require("tiny-secp256k1")); const readline = __importStar(require("readline")); const bitcoin = require('bitcoinjs-lib'); bitcoin.initEccLib(ecc); const bitcoinjs_lib_1 = require("bitcoinjs-lib"); const command_helpers_1 = require("./command-helpers"); const atomical_operation_builder_1 = require("../utils/atomical-operation-builder"); const get_atomicals_at_location_command_1 = require("./get-atomicals-at-location-command"); const address_helpers_1 = require("../utils/address-helpers"); const atomical_format_helpers_1 = require("../utils/atomical-format-helpers"); const tinysecp = require('tiny-secp256k1'); (0, bitcoinjs_lib_1.initEccLib)(tinysecp); class customColorInteractiveCommand { constructor(electrumApi, options, locationId, owner, funding) { this.electrumApi = electrumApi; this.options = options; this.locationId = locationId; this.owner = owner; this.funding = funding; } run() { return __awaiter(this, void 0, void 0, function* () { (0, command_helpers_1.logBanner)(`Custom color FTs Interactive`); const command = new get_atomicals_at_location_command_1.GetAtomicalsAtLocationCommand(this.electrumApi, this.locationId); const response = yield command.run(); if (!response || !response.success) { throw new Error(response); } const atomicals = response.data.atomicals; const hasNfts = (0, atomical_format_helpers_1.hasAtomicalType)('NFT', atomicals); if (hasNfts) { console.log('Found at least one NFT at the same location. The first output will contain the NFTs, and the second output, etc will contain the FTs split out. After you may use the splat command to separate multiple NFTs if they exist at the same location.'); } const inputUtxoPartial = (0, address_helpers_1.GetUtxoPartialFromLocation)(this.owner.address, response.data.location_info); const atomicalBuilder = new atomical_operation_builder_1.AtomicalOperationBuilder({ electrumApi: this.electrumApi, rbf: this.options.rbf, satsbyte: this.options.satsbyte, address: this.owner.address, disableMiningChalk: this.options.disableMiningChalk, opType: 'z', skipOptions: {}, meta: this.options.meta, ctx: this.options.ctx, init: this.options.init, }); // Add the owner of the atomicals at the location atomicalBuilder.addInputUtxo(inputUtxoPartial, this.owner.WIF); const atomicalsToColored = {}; let index = 0; for (const atomical of atomicals) { if (!atomical.atomical_id) { throw new Error('Critical error atomical_id not set for FT'); } if (!(0, atomical_format_helpers_1.isAtomicalId)(atomical.atomical_id)) { throw new Error('Critical error atomical_id is not valid for FT'); } console.log('-'); console.log(`current atomical_id: ${atomical.atomical_id}`); const outputs = yield this.promptCustomColored(index, atomical.value); // Make sure to make N outputs, for each atomical NFT atomicalsToColored[atomical.atomical_id] = outputs; for (const [key, value] of Object.entries(outputs)) { let atomical_value = value; if (atomical_value < 546 && atomical_value > 0) { atomical_value = 546; } if (atomical_value == 0) { continue; } atomicalBuilder.addOutput({ address: inputUtxoPartial.address, value: atomical_value }); index += 1; } } yield atomicalBuilder.setData(atomicalsToColored); console.log(atomicalBuilder); yield this.promptContinue(); const result = yield atomicalBuilder.start(this.funding.WIF); return { success: true, data: result }; }); } promptCustomColored(index, availableBalance) { return __awaiter(this, void 0, void 0, function* () { let remainingBalance = availableBalance; const atomicalColored = {}; const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); try { const prompt = (query) => new Promise((resolve) => rl.question(query, resolve)); while (remainingBalance > 0) { console.log('-'); console.log(`Remaining amount: ${remainingBalance}`); const prompt = (query) => new Promise((resolve) => rl.question(query, resolve)); let reply = yield prompt("Current outputs uxto index is " + index + ", Please enter amount you want to separated: "); console.log('--------'); if (reply === 'f') { break; } const valuePart = parseInt(reply, 10); if (valuePart < 0 || !valuePart) { console.log('Invalid value, minimum: 1'); continue; } if (remainingBalance - valuePart < 0) { console.log('Invalid value, maximum remaining: ' + remainingBalance); continue; } atomicalColored[index] = valuePart; remainingBalance -= valuePart; index += 1; } if (remainingBalance > 0) { throw new Error('Remaining balance was not 0'); } console.log('Successfully allocated entire available amounts to recipients...'); return atomicalColored; } finally { rl.close(); } }); } promptContinue() { return __awaiter(this, void 0, void 0, function* () { const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); try { let reply = ''; const prompt = (query) => new Promise((resolve) => rl.question(query, resolve)); reply = (yield prompt("Does everything look good above? To continue type 'y' or 'yes': ")); if (reply === 'y' || reply === 'yes') { return; } throw 'Aborted'; } finally { rl.close(); } }); } } exports.customColorInteractiveCommand = customColorInteractiveCommand;