atomicals-js
Version:
Atomicals JavaScript SDK and CLI
229 lines (228 loc) • 11.9 kB
JavaScript
"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.InitInteractiveFixedDftCommand = void 0;
const command_interface_1 = require("./command.interface");
const ecc = __importStar(require("tiny-secp256k1"));
const bitcoin = require('bitcoinjs-lib');
const readline = __importStar(require("readline"));
bitcoin.initEccLib(ecc);
const bitcoinjs_lib_1 = require("bitcoinjs-lib");
const get_by_ticker_command_1 = require("./get-by-ticker-command");
const atomical_operation_builder_1 = require("../utils/atomical-operation-builder");
const atomical_format_helpers_1 = require("../utils/atomical-format-helpers");
const command_helpers_1 = require("./command-helpers");
const tinysecp = require('tiny-secp256k1');
(0, bitcoinjs_lib_1.initEccLib)(tinysecp);
const promptContinue = () => __awaiter(void 0, 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));
while (reply !== 'q') {
console.log(`Are you sure you want to continue with the details above? (y/n)`);
console.log('-');
reply = (yield prompt("Enter your selection: "));
switch (reply) {
case 'y':
return true;
default:
throw new Error("user aborted");
}
}
}
finally {
rl.close();
}
});
class InitInteractiveFixedDftCommand {
constructor(electrumApi, options, file, address, requestTicker, mintAmount, maxMints, mintHeight, mintBitworkCommit, mintBitworkReveal, fundingWIF, noimage) {
this.electrumApi = electrumApi;
this.options = options;
this.file = file;
this.address = address;
this.requestTicker = requestTicker;
this.mintAmount = mintAmount;
this.maxMints = maxMints;
this.mintHeight = mintHeight;
this.mintBitworkCommit = mintBitworkCommit;
this.mintBitworkReveal = mintBitworkReveal;
this.fundingWIF = fundingWIF;
this.noimage = noimage;
this.options = (0, atomical_format_helpers_1.checkBaseRequestOptions)(this.options);
this.requestTicker = this.requestTicker.startsWith('$') ? this.requestTicker.substring(1) : this.requestTicker;
(0, atomical_format_helpers_1.isValidBitworkMinimum)(this.options.bitworkc);
if (this.maxMints > 10000000 || this.maxMints < 10000) {
throw new Error('Command line tool expects max mints to be between 10,000 and 10,000,000');
}
if (this.mintAmount > 100000000 || this.mintAmount < 546) {
throw new Error('mint amount must be between 546 and 100,000,000');
}
}
run() {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
let filesData;
if (this.file) {
filesData = yield (0, command_helpers_1.readJsonFileAsCompleteDataObjectEncodeAtomicalIds)(this.file, true);
console.log(filesData);
if (!filesData['name']) {
throw new Error('Please set a name in the files metadata. See examples in /templates/fungible-tokens');
}
if (!filesData['legal']) {
throw new Error('Please set legal in the files metadata. See examples in /templates/fungible-tokens');
}
if (!filesData['legal']['terms']) {
throw new Error('Please set legal terms in the files metadata. See examples in /templates/fungible-tokens');
}
if (!this.noimage && filesData['image']) {
// Ex: atom:btc:dat:<location of store-file data>/image.png
const re = /atom\:btc\:dat\:[a-f0-9]{64}i0\/.*\.(png|jpeg|svg|jpg|gif|webp)/;
if (!re.test(filesData['image'])) {
throw new Error('The image field in the metadata is invalid and must be in the format of atom:btc:dat:<locationId>/image.png - create the file with the store-file command and copy the urn into the image field');
}
}
}
else {
// Just default some metadata if a file was not provided
filesData = {
name: this.requestTicker.toUpperCase()
};
}
console.log('Initializing Fixed Decentralized FT Token');
console.log('-----------------------');
let supply = this.maxMints * this.mintAmount;
console.log('Total Supply: ', supply);
console.log('Total Supply (Satoshis): ', supply);
console.log('Total Supply (BTC): ', supply / 100000000);
console.log('Total Supply: ', supply);
console.log('Max mints (per Bitwork difficulty target): ', this.maxMints);
console.log('Mint Bitwork (commit): ', this.mintBitworkCommit);
console.log('Mint Bitwork (reveal): ', this.mintBitworkReveal);
console.log('Mint Height: ', this.mintHeight);
console.log('Mint Amount: ', this.mintAmount);
console.log('Data objects: ', filesData);
console.log('-----------------------');
if (((_a = this.mintBitworkCommit) === null || _a === void 0 ? void 0 : _a.length) < 5) {
console.log('WARNING: Mint Bitworkc is too easy to mine and can be mined in less than a minute or faster. Confirm if that is acceptable.', this.mintBitworkCommit);
}
if (this.mintBitworkReveal && ((_b = this.mintBitworkReveal) === null || _b === void 0 ? void 0 : _b.length) > 8) {
console.log('WARNING: Mint Bitworkc might be too hard to mine and could take a very long time. Confirm if that is acceptable.', this.mintBitworkReveal);
}
if (supply < 100000000) {
console.log('WARNING: Total supply is less than 1,000,000,000. Confirm if that is acceptable.', supply);
}
yield promptContinue();
const getExistingNameCommand = new get_by_ticker_command_1.GetByTickerCommand(this.electrumApi, this.requestTicker, command_interface_1.AtomicalsGetFetchType.GET);
try {
const getExistingNameResult = yield getExistingNameCommand.run();
if (getExistingNameResult.success && getExistingNameResult.data) {
if (getExistingNameResult.data.result && getExistingNameResult.data.result.atomical_id || getExistingNameResult.data.candidates.length) {
throw 'Already exists with that name. Try a different name.';
}
}
}
catch (err) {
console.log('err', err);
if (err.code !== 1) {
throw err; // Code 1 means call correctly returned that it was not found
}
}
const atomicalBuilder = new atomical_operation_builder_1.AtomicalOperationBuilder({
electrumApi: this.electrumApi,
rbf: this.options.rbf,
satsbyte: this.options.satsbyte,
address: this.address,
opType: 'dft',
dftOptions: {
mintAmount: Number(this.mintAmount),
maxMints: Number(this.maxMints),
mintHeight: Number(this.mintHeight),
ticker: this.requestTicker,
},
meta: this.options.meta,
ctx: this.options.ctx,
init: this.options.init,
});
// Attach any default data
yield atomicalBuilder.setData(filesData);
const args = {
mint_amount: Number(this.mintAmount),
mint_height: Number(this.mintHeight),
max_mints: Number(this.maxMints),
};
let mintBitworkCommitInfo = null;
if (this.mintBitworkCommit) {
mintBitworkCommitInfo = (0, atomical_format_helpers_1.isValidBitworkString)(this.mintBitworkCommit);
args['mint_bitworkc'] = mintBitworkCommitInfo === null || mintBitworkCommitInfo === void 0 ? void 0 : mintBitworkCommitInfo.hex_bitwork;
}
let mintBitworkRevealInfo = null;
if (this.mintBitworkReveal) {
mintBitworkRevealInfo = (0, atomical_format_helpers_1.isValidBitworkString)(this.mintBitworkReveal);
args['mint_bitworkr'] = mintBitworkRevealInfo === null || mintBitworkRevealInfo === void 0 ? void 0 : mintBitworkRevealInfo.hex_bitwork;
}
// Set mint mode = perpetual/infinite
atomicalBuilder.setArgs(args);
// Set to request a container
atomicalBuilder.setRequestTicker(this.requestTicker);
// Attach a container request
if (this.options.container)
atomicalBuilder.setContainerMembership(this.options.container);
// Attach any requested bitwork
if (this.options.bitworkc) {
atomicalBuilder.setBitworkCommit(this.options.bitworkc);
}
if (this.options.bitworkr) {
atomicalBuilder.setBitworkReveal(this.options.bitworkr);
}
if (this.options.parent) {
atomicalBuilder.setInputParent(yield atomical_operation_builder_1.AtomicalOperationBuilder.resolveInputParent(this.electrumApi, this.options.parent, this.options.parentOwner));
}
// The receiver output
atomicalBuilder.addOutput({
address: this.address,
value: this.options.satsoutput || 1000
});
const result = yield atomicalBuilder.start(this.fundingWIF);
return {
success: true,
data: result
};
});
}
}
exports.InitInteractiveFixedDftCommand = InitInteractiveFixedDftCommand;