atomicals-js
Version:
Atomicals JavaScript SDK and CLI
248 lines (247 loc) • 13 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.InitInteractiveInfiniteDftCommand = 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 utils_1 = require("../utils/utils");
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 InitInteractiveInfiniteDftCommand {
constructor(electrumApi, options, file, address, requestTicker, mintAmount, maxMints, mintHeight, mintBitworkVector, mintBitworkCommitIncrement, mintBitworkRevealIncrement, mintBitworkCommitIncrementStart, mintBitworkRevealIncrementStart, maxGlobalMints, 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.mintBitworkVector = mintBitworkVector;
this.mintBitworkCommitIncrement = mintBitworkCommitIncrement;
this.mintBitworkRevealIncrement = mintBitworkRevealIncrement;
this.mintBitworkCommitIncrementStart = mintBitworkCommitIncrementStart;
this.mintBitworkRevealIncrementStart = mintBitworkRevealIncrementStart;
this.maxGlobalMints = maxGlobalMints;
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 > 100000 || this.maxMints < 1) {
throw new Error('Command line tool expects max mints to be between 1 and 100,000');
}
if (this.mintAmount > 100000000 || this.mintAmount < 546) {
throw new Error('mint amount must be between 546 and 100,000,000');
}
}
run() {
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 Infinite Decentralized FT Token');
console.log('-----------------------');
console.log('Total Supply (Satoshis): unbounded');
console.log('Total Supply (BTC): unbounded');
console.log('Total Supply: unbounded');
console.log('Max mints (per Bitwork difficulty target): ', this.maxMints);
console.log('Mint Bitwork Vector: ', this.mintBitworkVector);
console.log('Mint Bitwork (commit) Increment: ', this.mintBitworkCommitIncrement);
console.log('Mint Bitwork (commit) Increment Start: ', this.mintBitworkCommitIncrementStart);
console.log('Mint Bitwork (reveal) Increment: ', this.mintBitworkRevealIncrement);
console.log('Mint Bitwork (reveal) Increment Start: ', this.mintBitworkRevealIncrementStart);
console.log('Max mints in global:', this.maxGlobalMints);
console.log('Mint Height: ', this.mintHeight);
console.log('Mint Amount: ', this.mintAmount);
console.log('Data objects: ', filesData);
console.log('-----------------------');
if (this.mintBitworkCommitIncrement < 1 || this.mintBitworkCommitIncrement > 64) {
throw new Error('Error: Mint Bitwork Commit Increment must be between 1 and 64');
}
if (this.mintBitworkRevealIncrement && (this.mintBitworkRevealIncrement < 1 || this.mintBitworkRevealIncrement > 64)) {
throw new Error('Error: Mint Bitwork Reveal Increment must be between 1 and 64');
}
if (this.mintBitworkVector.length < 4 || !(0, utils_1.isHex)(this.mintBitworkVector)) {
throw new Error('Error: Mint Bitwork Vector must be a hex string at least length 4');
}
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),
};
// Set mint mode = perpetual/infinite
args['md'] = 1;
args['bv'] = this.mintBitworkVector;
if (this.mintBitworkCommitIncrement) {
args['bci'] = this.mintBitworkCommitIncrement;
if (this.mintBitworkCommitIncrementStart) {
if (this.mintBitworkCommitIncrementStart < 0 || this.mintBitworkCommitIncrementStart > 192) {
throw new Error('mintBitworkCommitIncrementStart is not a number in range 0 to 192');
}
args['bcs'] = this.mintBitworkCommitIncrementStart;
}
}
if (this.mintBitworkRevealIncrement) {
args['bri'] = this.mintBitworkRevealIncrement;
if (this.mintBitworkRevealIncrementStart) {
if (this.mintBitworkRevealIncrementStart < 0 || this.mintBitworkRevealIncrementStart > 192) {
throw new Error('mintBitworkRevealIncrementStart is not a number in range 0 to 192');
}
args['brs'] = this.mintBitworkRevealIncrementStart;
}
}
if (this.maxGlobalMints) {
args['maxg'] = this.maxGlobalMints;
}
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.InitInteractiveInfiniteDftCommand = InitInteractiveInfiniteDftCommand;