UNPKG

@dgpub/prime-sdk

Version:

Etherspot Prime (Account Abstraction) SDK

104 lines (103 loc) 3.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WalletService = void 0; const operators_1 = require("rxjs/operators"); const ethers_1 = require("ethers"); const common_1 = require("../common"); const providers_1 = require("./providers"); class WalletService extends common_1.Service { constructor(providerLike, options, rpcUrl, chain) { super(); this.providerLike = providerLike; this.options = options; this.rpcUrl = rpcUrl; this.chain = chain; this.wallet$ = new common_1.ObjectSubject(); this.rpcBundlerUrl = rpcUrl; this.chainId = chain; this.EOAAddress$ = this.wallet$.observeKey('address'); } get wallet() { return this.wallet$.value; } get etherWallet() { return this.wallet$.value; } get EOAAddress() { return this.wallet ? this.wallet.address : null; } get walletProvider() { return this.provider ? this.provider : null; } getWalletProvider() { if (this.rpcUrl) return new ethers_1.ethers.providers.JsonRpcProvider(this.rpcUrl); return new ethers_1.ethers.providers.JsonRpcProvider(this.rpcBundlerUrl); } async signMessage(message) { return this.provider ? this.provider.signMessage(message) : null; } async signTypedData(types, message, accountAddress) { return this.provider ? this.provider.signTypedData(types, message, accountAddress) : null; } switchWalletProvider(providerLike) { var _a; let provider = null; if (providerLike) { switch (typeof providerLike) { case 'object': { const { privateKey } = providerLike; const walletLike = providerLike; const isNotJsonRpcProvider = ((_a = walletLike.provider) === null || _a === void 0 ? void 0 : _a.constructor.name) !== 'JsonRpcProvider'; if (privateKey && isNotJsonRpcProvider) { provider = new providers_1.KeyWalletProvider(privateKey); } else { provider = providerLike; } break; } case 'string': provider = new providers_1.KeyWalletProvider(providerLike); break; } } if (!provider) { this.wallet$.next(null); this.removeSubscriptions(); } else { const { networkService } = this.services; const { type: providerType } = provider; const subscriptions = []; const { address, address$ } = provider; if (typeof address$ !== 'undefined') { subscriptions.push(address$ .pipe((0, operators_1.map)((address) => ({ address, providerType, }))) .subscribe((wallet) => this.wallet$.next(wallet))); } else if (typeof address !== 'undefined') { this.wallet$.next({ address, providerType, }); } else { throw new Error('Invalid wallet address'); } networkService.useDefaultNetwork(); this.replaceSubscriptions(...subscriptions); } this.provider = provider; } onInit() { if (this.providerLike) { this.switchWalletProvider(this.providerLike); this.providerLike = null; } } } exports.WalletService = WalletService;