@mobile-wallet-protocol/client
Version:
Client SDK for the Mobile Wallet Protocol
54 lines (53 loc) • 2.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EIP1193Provider = void 0;
const MWPClient_1 = require("../../MWPClient");
const error_1 = require("../../core/error");
const serialize_1 = require("../../core/error/serialize");
const interface_1 = require("../../core/provider/interface");
class EIP1193Provider extends interface_1.ProviderEventEmitter {
constructor(options) {
super();
this.client = null;
this.initPromise = this.initialize(options);
}
async initialize(options) {
this.client = await MWPClient_1.MWPClient.createInstance(options);
}
async request(args) {
await this.ensureInitialized();
try {
if (!this.client)
throw error_1.standardErrors.rpc.internal('MWPClient not initialized');
if (args.method === 'eth_requestAccounts') {
// TODO: emit connect
const accounts = await this.client.handshake();
return accounts;
}
return await this.client.request(args);
}
catch (error) {
const { code } = error;
if (code === error_1.standardErrorCodes.provider.unauthorized)
this.disconnect();
return Promise.reject((0, serialize_1.serializeError)(error));
}
}
/** @deprecated Use `.request({ method: 'eth_requestAccounts' })` instead. */
async enable() {
console.warn(`.enable() has been deprecated. Please use .request({ method: "eth_requestAccounts" }) instead.`);
return await this.request({
method: 'eth_requestAccounts',
});
}
async disconnect() {
var _a;
await this.ensureInitialized();
await ((_a = this.client) === null || _a === void 0 ? void 0 : _a.reset());
this.emit('disconnect', error_1.standardErrors.provider.disconnected('User initiated disconnection'));
}
async ensureInitialized() {
await this.initPromise; // resolves immediately if already initialized
}
}
exports.EIP1193Provider = EIP1193Provider;