dotbit
Version:
A complete .bit SDK and utilities in TypeScript
308 lines • 13.8 kB
JavaScript
"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 _BitAccount_instances, _BitAccount_changeOwnerManager, _BitAccount_addrs;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BitAccount = void 0;
const RecordsEditor_1 = require("./builders/RecordsEditor");
const const_1 = require("./const");
const RegisterAPI_1 = require("./fetchers/RegisterAPI");
const slip44_1 = require("./slip44/slip44");
const account_1 = require("./tools/account");
const DotbitError_1 = require("./errors/DotbitError");
const common_1 = require("./tools/common");
class BitAccount {
constructor(options) {
_BitAccount_instances.add(this);
if (!(0, account_1.isSupportedAccount)(options.account)) {
throw new DotbitError_1.DotbitError(`${options.account} is not a valid .bit account`, DotbitError_1.BitIndexerErrorCode.AccountFormatInvalid);
}
this.account = options.account;
if (options.bitIndexer) {
this.bitIndexer = options.bitIndexer;
}
if (options.bitBuilder) {
this.bitBuilder = options.bitBuilder;
}
if (options.signer) {
this.signer = options.signer;
}
}
requireSigner() {
if (!this.signer) {
throw new DotbitError_1.DotbitError('signer is required', DotbitError_1.BitErrorCode.SignerRequired);
}
}
requireBitBuilder() {
if (!this.bitBuilder) {
throw new DotbitError_1.DotbitError('bitBuilder is required', DotbitError_1.BitErrorCode.BitBuilderRequired);
}
}
setReverseRecord() {
this.requireBitBuilder();
}
subAccounts(params = { page: 1, size: 100, keyword: '' }) {
return this.bitBuilder.subAccountAPI.subAccountList({
account: this.account,
page: params.page,
size: params.size,
keyword: params.keyword,
});
}
checkSubAccounts(subAccounts) {
return __awaiter(this, void 0, void 0, function* () {
const address = yield this.signer.getAddress();
const coinType = yield this.signer.getCoinType();
return yield this.bitBuilder.subAccountAPI.checkSubAccounts({
account: this.account,
type: 'blockchain',
key_info: {
key: address,
coin_type: coinType
},
sub_account_list: subAccounts
});
});
}
mintSubAccounts(params) {
return __awaiter(this, void 0, void 0, function* () {
this.requireBitBuilder();
this.requireSigner();
const address = yield this.signer.getAddress();
const coinType = yield this.signer.getCoinType();
const mintSubAccountsParams = {
account: this.account,
type: 'blockchain',
key_info: {
key: address,
coin_type: coinType
},
sub_account_list: params.map(param => {
const account = (0, account_1.toDottedStyle)(param.account);
if (param.mintForAccount) {
return {
account,
mint_for_account: param.mintForAccount,
register_years: param.registerYears,
account_char_str: (0, account_1.graphemesAccount)(account.split('.')[0]),
};
}
else {
return {
account,
type: 'blockchain',
key_info: param.keyInfo,
register_years: param.registerYears,
account_char_str: (0, account_1.graphemesAccount)(account.split('.')[0])
};
}
})
};
const checkResults = yield this.checkSubAccounts(mintSubAccountsParams.sub_account_list);
checkResults.result.forEach(result => {
if (result.status !== const_1.CheckSubAccountStatus.ok) {
throw new DotbitError_1.DotbitError(`Second-level DID ${result.account} can not be registered, reason: ${result.message}, status ${result.status}`, DotbitError_1.BitErrorCode.SubAccountStatusInvalid);
}
});
const txs = yield this.bitBuilder.mintSubAccounts(mintSubAccountsParams);
const signatureList = yield this.signer.signTxList(txs);
return yield this.bitBuilder.subAccountAPI.sendTransaction(signatureList);
});
}
mintSubAccount(params) {
return this.mintSubAccounts([params]);
}
changeOwner(keyInfo) {
return __classPrivateFieldGet(this, _BitAccount_instances, "m", _BitAccount_changeOwnerManager).call(this, keyInfo, true);
}
changeManager(keyInfo) {
return __classPrivateFieldGet(this, _BitAccount_instances, "m", _BitAccount_changeOwnerManager).call(this, keyInfo, false);
}
updateRecords(records) {
return __awaiter(this, void 0, void 0, function* () {
this.requireBitBuilder();
this.requireSigner();
const signerAddress = yield this.signer.getAddress();
const signerCoinType = yield this.signer.getCoinType();
const signerChainId = yield this.signer.getChainId();
const txs = yield this.bitBuilder.editRecords({
keyInfo: {
coin_type: signerCoinType,
key: signerAddress
},
evm_chain_id: signerChainId,
account: this.account,
raw_param: {
records: records.map(RegisterAPI_1.toEditingRecord)
},
});
const signatureList = yield this.signer.signTxList(txs);
return yield this.bitBuilder.registerAPI.sendTransaction(signatureList);
});
}
editRecords() {
return __awaiter(this, void 0, void 0, function* () {
const records = yield this.records();
return new RecordsEditor_1.RecordsEditor(records, this);
});
}
info() {
return __awaiter(this, void 0, void 0, function* () {
if (!this._info) {
const info = (yield this.bitIndexer.accountInfo(this.account)).account_info;
this._info = info;
}
return this._info;
});
}
owner() {
return __awaiter(this, void 0, void 0, function* () {
const info = yield this.info();
return {
key: info.owner_key,
coin_type: const_1.SignType2CoinType[info.owner_algorithm_id],
algorithm_id: info.owner_algorithm_id,
};
});
}
manager() {
return __awaiter(this, void 0, void 0, function* () {
const info = yield this.info();
return {
key: info.manager_key,
coin_type: const_1.SignType2CoinType[info.manager_algorithm_id],
algorithm_id: info.manager_algorithm_id,
};
});
}
records(key) {
return __awaiter(this, void 0, void 0, function* () {
if (!this._records) {
const records = (yield this.bitIndexer.accountRecords(this.account));
this._records = records.map(account_1.toRecordExtended);
}
key = key === null || key === void 0 ? void 0 : key.toLowerCase();
return key ? this._records.filter(record => record.key === key) : this._records;
});
}
addresses(chain) {
return __classPrivateFieldGet(this, _BitAccount_instances, "m", _BitAccount_addrs).call(this, chain);
}
addrs(chain) {
return __classPrivateFieldGet(this, _BitAccount_instances, "m", _BitAccount_addrs).call(this, chain);
}
dwebs(protocol) {
return __awaiter(this, void 0, void 0, function* () {
const records = yield this.records();
let dwebs = records.filter(record => record.type === const_1.RecordType.dweb);
dwebs = dwebs.map((record) => {
const matched = (0, common_1.matchDWebProtocol)(record.value);
if (matched) {
record.value = matched[2];
}
return record;
});
return protocol ? dwebs.filter(record => record.subtype === protocol.toLowerCase()) : dwebs;
});
}
dweb() {
return __awaiter(this, void 0, void 0, function* () {
const dwebs = yield this.dwebs();
if (!dwebs.length) {
return undefined;
}
else if (dwebs.length === 1) {
return dwebs[0];
}
else {
const protocol = [const_1.DWebProtocol.ipns, const_1.DWebProtocol.ipfs, const_1.DWebProtocol.arweave, const_1.DWebProtocol.skynet, const_1.DWebProtocol.resilio].find(protocol => dwebs.find(dweb => dweb.subtype === protocol));
return dwebs.find(dweb => dweb.subtype === protocol);
}
});
}
profiles(subtype) {
return __awaiter(this, void 0, void 0, function* () {
const records = yield this.records();
const profiles = records.filter(record => record.type === const_1.RecordType.profile);
return subtype ? profiles.filter(record => record.subtype === subtype.toLowerCase()) : profiles;
});
}
avatar() {
throw new DotbitError_1.DotbitError('Please install @dotbit/plugin-avatar to get users avatar', DotbitError_1.BitErrorCode.PluginRequired);
}
register(param) {
throw new DotbitError_1.DotbitError('Please install plugin @dotbit/plugin-register', DotbitError_1.BitErrorCode.PluginRequired);
}
renew(param) {
throw new DotbitError_1.DotbitError('Please install plugin @dotbit/plugin-register', DotbitError_1.BitErrorCode.PluginRequired);
}
}
exports.BitAccount = BitAccount;
_BitAccount_instances = new WeakSet(), _BitAccount_changeOwnerManager = function _BitAccount_changeOwnerManager(keyInfo_1) {
return __awaiter(this, arguments, void 0, function* (keyInfo, isOwner = true) {
const signerAddress = yield this.signer.getAddress();
const signerCoinType = yield this.signer.getCoinType();
const signerChainId = yield this.signer.getChainId();
let mmJsonTxs;
if (isOwner) {
mmJsonTxs = yield this.bitBuilder.changeOwner({
keyInfo: {
coin_type: signerCoinType,
key: signerAddress
},
evm_chain_id: signerChainId,
account: this.account,
raw_param: {
receiver_address: keyInfo.key,
receiver_coin_type: keyInfo.coin_type,
},
});
}
else {
mmJsonTxs = yield this.bitBuilder.changeManager({
keyInfo: {
coin_type: signerCoinType,
key: signerAddress
},
evm_chain_id: signerChainId,
account: this.account,
raw_param: {
manager_address: keyInfo.key,
manager_coin_type: keyInfo.coin_type,
},
});
}
const signatureList = yield this.signer.signTxList(mmJsonTxs);
return yield this.bitBuilder.registerAPI.sendTransaction(signatureList);
});
}, _BitAccount_addrs = function _BitAccount_addrs(chain) {
return __awaiter(this, void 0, void 0, function* () {
const records = yield this.records();
const addresses = records
.filter(record => record.type === const_1.RecordType.address)
.map(record => {
return (Object.assign(Object.assign({}, record), { coin_type: (0, slip44_1.mapSymbolToCoinType)(record.subtype) }));
});
if (chain) {
const coinType = (0, slip44_1.mapSymbolToCoinType)(chain);
const symbol = (0, slip44_1.mapCoinTypeToSymbol)(chain).toLowerCase();
return addresses.filter(record => record.coin_type === coinType || record.subtype === symbol || record.subtype === coinType);
}
else {
return addresses;
}
});
};
//# sourceMappingURL=BitAccount.js.map