UNPKG

@tmawallet/sdk

Version:
85 lines 2.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TMAWAlletAbstractWallet = void 0; const WalletBundleController_1 = require("../client/WalletBundleController"); class TMAWAlletAbstractWallet { client; type; walletBundleController; lastReportedWalletAddress = null; _walletAddress = null; constructor(client, type) { this.client = client; this.type = type; this.walletBundleController = new WalletBundleController_1.WalletBundleController(type, client.storage); } async _init() { if (this.client.bundle) { this._walletAddress = await this.walletBundleController.getWalletAddress(this.client.bundle.clientPublicKey); } else { this._walletAddress = null; } } async _initWallet() { if (!this.client.bundle) { throw new Error('Client bundle does not exist'); } const privateKey = await this.client.accessPrivateKey(); const walletAddress = await this.getAddressFromPrivateKey(privateKey); await this.walletBundleController.storeWalletAddress(this.client.bundle.clientPublicKey, walletAddress); this._walletAddress = walletAddress; try { await this.client._storeWalletAddress(this.type, walletAddress); } catch (error) { console.warn('Error reporting wallet address: ', error); } } _checkStateChanged() { if (this.lastReportedWalletAddress !== this._walletAddress) { this.lastReportedWalletAddress = this._walletAddress; this.client.emit('walletChanged', this._walletAddress); } } get state() { if (!this.client.bundle) { return { registered: false, walletAddress: null }; } else { console.log('state.walletAddress: ', this._walletAddress); return { registered: true, walletAddress: this._walletAddress }; } } get walletAddress() { return this._walletAddress; } async init() { await this.client._init(); await this._init(); this._checkStateChanged(); return this.state; } async authenticate() { await this.client._authenticate(); await this._init(); if (!this._walletAddress) { await this._initWallet(); } this._checkStateChanged(); return this.state; } async initWallet() { await this._initWallet(); this._checkStateChanged(); return this.state; } async clearLocalWalletAddress() { await this.walletBundleController.clearWalletAddress(); this._walletAddress = null; this._checkStateChanged(); return this.state; } } exports.TMAWAlletAbstractWallet = TMAWAlletAbstractWallet; //# sourceMappingURL=TMAWallet.AbstractWallet.js.map