atomicals-js
Version:
Atomicals JavaScript SDK and CLI
100 lines (99 loc) • 4.53 kB
JavaScript
;
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WalletImportCommand = void 0;
const create_key_pair_1 = require("../utils/create-key-pair");
const file_utils_1 = require("../utils/file-utils");
const bitcoin = require('bitcoinjs-lib');
const ecpair_1 = __importDefault(require("ecpair"));
const ecc = __importStar(require("tiny-secp256k1"));
const wallet_path_resolver_1 = require("../utils/wallet-path-resolver");
const command_helpers_1 = require("./command-helpers");
bitcoin.initEccLib(ecc);
const ECPair = (0, ecpair_1.default)(ecc);
const walletPath = (0, wallet_path_resolver_1.walletPathResolver)();
class WalletImportCommand {
constructor(wif, alias) {
this.wif = wif;
this.alias = alias;
}
run() {
return __awaiter(this, void 0, void 0, function* () {
if (!(yield this.walletExists())) {
throw "wallet.json does NOT exist, please create one first with wallet-init";
}
const walletFileData = (yield (0, file_utils_1.jsonFileReader)(walletPath));
if (!walletFileData.imported) {
walletFileData.imported = {};
}
if (walletFileData.imported.hasOwnProperty(this.alias)) {
throw `Wallet alias ${this.alias} already exists!`;
}
// Just make a backup for now to be safe
yield (0, file_utils_1.jsonFileWriter)(walletPath + '.' + (new Date()).getTime() + '.walletbackup', walletFileData);
// Get the wif and the address and ensure they match
const importedKeypair = ECPair.fromWIF(this.wif);
const { address, output } = bitcoin.payments.p2tr({
internalPubkey: (0, create_key_pair_1.toXOnly)(importedKeypair.publicKey),
network: command_helpers_1.NETWORK
});
const walletImportedField = Object.assign({}, walletFileData.imported, {
[this.alias]: {
address,
WIF: this.wif
}
});
walletFileData['imported'] = walletImportedField;
yield (0, file_utils_1.jsonFileWriter)(walletPath, walletFileData);
return {
success: true,
data: {
address,
alias: this.alias
}
};
});
}
walletExists() {
return __awaiter(this, void 0, void 0, function* () {
if (yield (0, file_utils_1.jsonFileExists)(walletPath)) {
return true;
}
});
}
}
exports.WalletImportCommand = WalletImportCommand;