UNPKG

dotbit

Version:

A complete .bit SDK and utilities in TypeScript

304 lines 13.4 kB
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; import { RecordsEditor } from './builders/RecordsEditor'; import { SignType2CoinType, CheckSubAccountStatus, DWebProtocol, RecordType } from './const'; import { toEditingRecord } from './fetchers/RegisterAPI'; import { mapCoinTypeToSymbol, mapSymbolToCoinType } from './slip44/slip44'; import { isSupportedAccount, graphemesAccount, toDottedStyle, toRecordExtended, } from './tools/account'; import { BitErrorCode, BitIndexerErrorCode, DotbitError } from './errors/DotbitError'; import { matchDWebProtocol } from './tools/common'; export class BitAccount { constructor(options) { _BitAccount_instances.add(this); if (!isSupportedAccount(options.account)) { throw new DotbitError(`${options.account} is not a valid .bit account`, 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('signer is required', BitErrorCode.SignerRequired); } } requireBitBuilder() { if (!this.bitBuilder) { throw new DotbitError('bitBuilder is required', 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 = toDottedStyle(param.account); if (param.mintForAccount) { return { account, mint_for_account: param.mintForAccount, register_years: param.registerYears, account_char_str: graphemesAccount(account.split('.')[0]), }; } else { return { account, type: 'blockchain', key_info: param.keyInfo, register_years: param.registerYears, account_char_str: graphemesAccount(account.split('.')[0]) }; } }) }; const checkResults = yield this.checkSubAccounts(mintSubAccountsParams.sub_account_list); checkResults.result.forEach(result => { if (result.status !== CheckSubAccountStatus.ok) { throw new DotbitError(`Second-level DID ${result.account} can not be registered, reason: ${result.message}, status ${result.status}`, 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(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(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: 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: 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(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 === RecordType.dweb); dwebs = dwebs.map((record) => { const matched = 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 = [DWebProtocol.ipns, DWebProtocol.ipfs, DWebProtocol.arweave, DWebProtocol.skynet, 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 === RecordType.profile); return subtype ? profiles.filter(record => record.subtype === subtype.toLowerCase()) : profiles; }); } avatar() { throw new DotbitError('Please install @dotbit/plugin-avatar to get users avatar', BitErrorCode.PluginRequired); } register(param) { throw new DotbitError('Please install plugin @dotbit/plugin-register', BitErrorCode.PluginRequired); } renew(param) { throw new DotbitError('Please install plugin @dotbit/plugin-register', BitErrorCode.PluginRequired); } } _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 === RecordType.address) .map(record => { return (Object.assign(Object.assign({}, record), { coin_type: mapSymbolToCoinType(record.subtype) })); }); if (chain) { const coinType = mapSymbolToCoinType(chain); const symbol = mapCoinTypeToSymbol(chain).toLowerCase(); return addresses.filter(record => record.coin_type === coinType || record.subtype === symbol || record.subtype === coinType); } else { return addresses; } }); }; //# sourceMappingURL=BitAccount.js.map