UNPKG

dotbit-sdk-allin

Version:

A complete .bit SDK and utilities in TypeScript

169 lines 7.16 kB
"use strict"; 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 __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _DotBit_instances, _DotBit_addrs; Object.defineProperty(exports, "__esModule", { value: true }); exports.DotBit = void 0; const BitAccount_1 = require("./BitAccount"); const BitSubAccount_1 = require("./BitSubAccount"); const account_1 = require("./tools/account"); const DotbitError_1 = require("./errors/DotbitError"); const common_1 = require("./tools/common"); class DotBit { constructor(config = {}) { _DotBit_instances.add(this); this.plugins = []; this.network = config.network; this.cacheProvider = config.cacheProvider; this.bitIndexer = config.bitIndexer; this.bitBuilder = config.bitBuilder; this.signer = config.signer; } installPlugin(plugin) { if (plugin.onInstall) { plugin.onInstall(this); this.plugins.push(plugin); } else { console.warn(`Plugin '${plugin.name}' does not have 'onInstall' method, please check your plugin`); } } uninstallPlugin(plugin) { var _a; const index = this.plugins.indexOf(plugin); this.plugins.splice(index, 1); (_a = plugin.onUninstall) === null || _a === void 0 ? void 0 : _a.call(plugin, this); } getAccount(account) { var _a, _b; let bitAccount = (_a = this.cacheProvider) === null || _a === void 0 ? void 0 : _a.get(`account:${account}`); if (bitAccount) return bitAccount; if ((0, account_1.isSubAccount)(account)) { bitAccount = new BitSubAccount_1.BitSubAccount({ account, bitIndexer: this.bitIndexer, bitBuilder: this.bitBuilder, signer: this.signer, }); } else { bitAccount = new BitAccount_1.BitAccount({ account, bitIndexer: this.bitIndexer, bitBuilder: this.bitBuilder, signer: this.signer, }); } this.plugins.forEach(plugin => { var _a; return (_a = plugin.onInitAccount) === null || _a === void 0 ? void 0 : _a.call(plugin, bitAccount); }); (_b = this.cacheProvider) === null || _b === void 0 ? void 0 : _b.set(`account:${account}`, bitAccount); return bitAccount; } serverInfo() { return __awaiter(this, void 0, void 0, function* () { return yield this.bitIndexer.serverInfo(); }); } reverse(keyInfo) { return __awaiter(this, void 0, void 0, function* () { const { account } = yield this.bitIndexer.reverseRecord(keyInfo); if (account) { return this.getAccount(account); } }); } alias(keyInfo) { return this.reverse(keyInfo); } accountsOfOwner(keyInfo) { return __awaiter(this, void 0, void 0, function* () { const accounts = yield this.bitIndexer.accountList(keyInfo); return accounts.map(account => this.getAccount(account)); }); } accountsOfManager(keyInfo) { return __awaiter(this, void 0, void 0, function* () { const accounts = yield this.bitIndexer.accountList(keyInfo, 'manager'); return accounts.map(account => this.getAccount(account)); }); } account(account) { return this.getAccount(account); } exist(account) { const bitAccount = this.getAccount(account); return bitAccount.info() .then(() => true) .catch((err) => { if (err.code === DotbitError_1.BitIndexerErrorCode.AccountNotExist) { return false; } throw err; }); } accountById(accountId) { return __awaiter(this, void 0, void 0, function* () { if ((0, common_1.isEmptyAddress)(accountId)) { throw new DotbitError_1.DotbitError('Please provide a valid account id, current: ' + accountId, DotbitError_1.BitErrorCode.InvalidAccountId); } const bitAccount = yield this.bitIndexer.accountInfoById(accountId); return this.getAccount(bitAccount.account_info.account); }); } records(account, key) { const bitAccount = this.getAccount(account); return bitAccount.records(key); } accountInfo(account) { const bitAccount = this.getAccount(account); return bitAccount.info(); } addresses(account, chain) { return __classPrivateFieldGet(this, _DotBit_instances, "m", _DotBit_addrs).call(this, account, chain); } addrs(account, chain) { return __classPrivateFieldGet(this, _DotBit_instances, "m", _DotBit_addrs).call(this, account, chain); } dwebs(account, key) { return __awaiter(this, void 0, void 0, function* () { const bitAccount = this.getAccount(account); return yield bitAccount.dwebs(key); }); } dweb(account) { return __awaiter(this, void 0, void 0, function* () { const bitAccount = this.getAccount(account); return yield bitAccount.dweb(); }); } profiles(account, key) { return __awaiter(this, void 0, void 0, function* () { const bitAccount = this.getAccount(account); return yield bitAccount.profiles(key); }); } avatar(account) { return __awaiter(this, void 0, void 0, function* () { const bitAccount = this.getAccount(account); return yield bitAccount.avatar(); }); } } exports.DotBit = DotBit; _DotBit_instances = new WeakSet(), _DotBit_addrs = function _DotBit_addrs(account, chain) { const bitAccount = this.getAccount(account); return bitAccount.addrs(chain); }; //# sourceMappingURL=DotBit.js.map