dotbit
Version:
A complete .bit SDK and utilities in TypeScript
203 lines • 8.08 kB
JavaScript
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;
import { BitAccount } from './BitAccount';
import { BitSubAccount } from './BitSubAccount';
import { isSubAccount } from './tools/account';
import { BitErrorCode, BitIndexerErrorCode, DotbitError } from './errors/DotbitError';
import { isEmptyAddress } from './tools/common';
import { version } from './version';
export class DotBit {
constructor(config = {}) {
_DotBit_instances.add(this);
this.version = version;
this.plugins = [];
if (config.network) {
this.network = config.network;
}
if (config.cacheProvider) {
this.cacheProvider = config.cacheProvider;
}
if (config.bitIndexer) {
this.bitIndexer = config.bitIndexer;
}
if (config.bitBuilder) {
this.bitBuilder = config.bitBuilder;
}
if (config.signer) {
this.signer = config.signer;
}
}
installPlugin(plugin) {
var _a;
if (plugin.onInstall) {
plugin.onInstall(this);
this.plugins.push(plugin);
}
else {
console.warn(`Plugin '${(_a = plugin === null || plugin === void 0 ? void 0 : plugin.name) !== null && _a !== void 0 ? _a : ''}' 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 (isSubAccount(account)) {
bitAccount = new BitSubAccount({
account,
bitIndexer: this.bitIndexer,
bitBuilder: this.bitBuilder,
signer: this.signer,
});
}
else {
bitAccount = new 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;
});
}
accountsOfManager(keyInfo) {
return __awaiter(this, void 0, void 0, function* () {
const accounts = yield this.bitIndexer.accountList(keyInfo, 'manager');
return accounts;
});
}
account(account) {
return this.getAccount(account);
}
exist(account) {
const bitAccount = this.getAccount(account);
return bitAccount.info()
.then(() => true)
.catch((err) => {
if (err.code === BitIndexerErrorCode.AccountNotExist) {
return false;
}
throw err;
});
}
accountById(accountId) {
return __awaiter(this, void 0, void 0, function* () {
if (isEmptyAddress(accountId)) {
throw new DotbitError('Please provide a valid account id, current: ' + accountId, 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();
});
}
verifyAddrsByAccount(address, mainAccount, subAccount, verifyType) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.bitIndexer.subAccountVerify(address, mainAccount, subAccount, verifyType);
});
}
validDotbitAliasAddresses(account) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.bitIndexer.validDotbitAliasAddresses(account);
});
}
batchAccountInfo(accounts) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.bitIndexer.batchAccountInfo(accounts);
});
}
dobList(keyInfo_1) {
return __awaiter(this, arguments, void 0, function* (keyInfo, didType = 1, page = 1, size = 100) {
return yield this.bitIndexer.dobList({
keyInfo,
didType,
page,
size
});
});
}
}
_DotBit_instances = new WeakSet(), _DotBit_addrs = function _DotBit_addrs(account, chain) {
const bitAccount = this.getAccount(account);
return bitAccount.addrs(chain);
};
//# sourceMappingURL=DotBit.js.map