@pontem/aptos-wallet-adapter
Version:
Wallet adapter with supporting Vue and React
267 lines • 12.3 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RiseWalletAdapter = exports.RiseWalletName = void 0;
const WalletProviders_1 = require("../WalletProviders");
const BaseAdapter_1 = require("./BaseAdapter");
exports.RiseWalletName = 'Rise Wallet';
class RiseWalletAdapter extends BaseAdapter_1.BaseWalletAdapter {
constructor({
// provider,
// network = WalletAdapterNetwork.Testnet,
timeout = 10000 } = {}) {
super();
this.name = exports.RiseWalletName;
this.url = 'https://risewallet.io';
this.icon = 'https://static.risewallet.io/logo.png';
this._readyState = typeof window === 'undefined' || typeof document === 'undefined'
? BaseAdapter_1.WalletReadyState.Unsupported
: BaseAdapter_1.WalletReadyState.NotDetected;
this.handleDisconnect = () => {
try {
const provider = this._provider || window.rise;
provider === null || provider === void 0 ? void 0 : provider.off('disconnect', this.handleDisconnect);
this._wallet = null;
}
finally {
this.emit('disconnect');
}
};
this._provider = typeof window !== 'undefined' ? window.rise : undefined;
this._network = undefined;
this._timeout = timeout;
this._connecting = false;
this._wallet = null;
if (typeof window !== 'undefined' && this._readyState !== BaseAdapter_1.WalletReadyState.Unsupported) {
(0, BaseAdapter_1.scopePollingDetectionStrategy)(() => {
if (window.rise) {
this._readyState = BaseAdapter_1.WalletReadyState.Installed;
this.emit('readyStateChange', this._readyState);
return true;
}
return false;
});
}
}
get publicAccount() {
var _a, _b, _c;
return {
publicKey: ((_a = this._wallet) === null || _a === void 0 ? void 0 : _a.publicKey) || null,
address: ((_b = this._wallet) === null || _b === void 0 ? void 0 : _b.address) || null,
authKey: ((_c = this._wallet) === null || _c === void 0 ? void 0 : _c.authKey) || null
};
}
get network() {
return {
name: this._network,
api: this._api,
chainId: this._chainId
};
}
get connecting() {
return this._connecting;
}
get connected() {
var _a;
return !!((_a = this._wallet) === null || _a === void 0 ? void 0 : _a.isConnected);
}
get readyState() {
return this._readyState;
}
connect() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
if (this.connected || this.connecting)
return;
if (!(this._readyState === BaseAdapter_1.WalletReadyState.Loadable ||
this._readyState === BaseAdapter_1.WalletReadyState.Installed))
throw new WalletProviders_1.WalletNotReadyError();
this._connecting = true;
const provider = this._provider || window.rise;
const isConnected = yield ((_a = this._provider) === null || _a === void 0 ? void 0 : _a.isConnected());
if (isConnected === true) {
yield (provider === null || provider === void 0 ? void 0 : provider.disconnect());
}
const response = yield (provider === null || provider === void 0 ? void 0 : provider.connect());
if (!response) {
throw new WalletProviders_1.WalletNotConnectedError('User has rejected the request');
}
try {
const { chainId, api, name } = yield provider.network();
this._network = name;
this._chainId = chainId;
this._api = api;
}
catch (error) {
const errMsg = error.message;
this.emit('error', new WalletProviders_1.WalletGetNetworkError(errMsg));
throw error;
}
provider === null || provider === void 0 ? void 0 : provider.on('disconnect', this.handleDisconnect);
const account = yield (provider === null || provider === void 0 ? void 0 : provider.account());
if (account) {
const { publicKey, address, authKey } = account;
this._wallet = {
publicKey,
address,
authKey,
isConnected: true
};
this.emit('connect', this._wallet.publicKey);
}
}
catch (error) {
this.emit('error', error);
throw error;
}
finally {
this._connecting = false;
}
});
}
disconnect() {
return __awaiter(this, void 0, void 0, function* () {
const wallet = this._wallet;
if (wallet) {
this._wallet = null;
try {
const provider = this._provider || window.rise;
provider === null || provider === void 0 ? void 0 : provider.off('disconnect', this.handleDisconnect);
yield (provider === null || provider === void 0 ? void 0 : provider.disconnect());
}
catch (error) {
this.emit('error', new WalletProviders_1.WalletDisconnectionError(error === null || error === void 0 ? void 0 : error.message, error));
}
}
this.emit('disconnect');
});
}
signTransaction(transaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
const wallet = this._wallet;
const provider = this._provider || window.rise;
if (!wallet || !provider)
throw new WalletProviders_1.WalletNotConnectedError();
const response = yield (provider === null || provider === void 0 ? void 0 : provider.signTransaction(transaction));
if (response) {
return response;
}
else {
throw new Error('Sign Transaction failed');
}
}
catch (error) {
const errMsg = error.message;
this.emit('error', new WalletProviders_1.WalletSignTransactionError(errMsg));
throw error;
}
});
}
signAndSubmitTransaction(transaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
const wallet = this._wallet;
const provider = this._provider || window.rise;
if (!wallet || !provider)
throw new WalletProviders_1.WalletNotConnectedError();
const response = yield (provider === null || provider === void 0 ? void 0 : provider.signAndSubmitTransaction(transaction));
if (response) {
return response;
}
else {
throw new Error('Transaction failed');
}
}
catch (error) {
const errMsg = error.message;
this.emit('error', new WalletProviders_1.WalletSignTransactionError(errMsg));
throw error;
}
});
}
signMessage(msgPayload) {
return __awaiter(this, void 0, void 0, function* () {
try {
const wallet = this._wallet;
const provider = this._provider || window.rise;
if (!wallet || !provider)
throw new WalletProviders_1.WalletNotConnectedError();
if (typeof msgPayload !== 'object' || !msgPayload.nonce) {
throw new WalletProviders_1.WalletSignMessageError('Invalid signMessage Payload');
}
const response = yield (provider === null || provider === void 0 ? void 0 : provider.signMessage(msgPayload));
if (response) {
return response;
}
else {
throw new Error('Sign Message failed');
}
}
catch (error) {
const errMsg = error.message;
this.emit('error', new WalletProviders_1.WalletSignMessageError(errMsg));
throw error;
}
});
}
onAccountChange() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
const wallet = this._wallet;
const provider = this._provider || window.rise;
if (!wallet || !provider)
throw new WalletProviders_1.WalletNotConnectedError();
const handleAccountChange = (newAccount) => __awaiter(this, void 0, void 0, function* () {
var _b, _c, _d;
if (newAccount === null || newAccount === void 0 ? void 0 : newAccount.publicKey) {
this._wallet = Object.assign(Object.assign({}, this._wallet), { publicKey: newAccount.publicKey || ((_b = this._wallet) === null || _b === void 0 ? void 0 : _b.publicKey), authKey: newAccount.authKey || ((_c = this._wallet) === null || _c === void 0 ? void 0 : _c.authKey), address: newAccount.address || ((_d = this._wallet) === null || _d === void 0 ? void 0 : _d.address) });
this.emit('accountChange', newAccount === null || newAccount === void 0 ? void 0 : newAccount.publicKey);
}
});
yield ((_a = provider === null || provider === void 0 ? void 0 : provider.onAccountChange) === null || _a === void 0 ? void 0 : _a.call(provider, handleAccountChange));
}
catch (error) {
const errMsg = error.message;
this.emit('error', new WalletProviders_1.WalletAccountChangeError(errMsg));
throw error;
}
});
}
onNetworkChange() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
const wallet = this._wallet;
const provider = this._provider || window.rise;
if (!wallet || !provider)
throw new WalletProviders_1.WalletNotConnectedError();
const handleNetworkChange = (newNetwork) => __awaiter(this, void 0, void 0, function* () {
const { chainId, api, name } = newNetwork;
this._network = name;
this._chainId = chainId;
this._api = api;
this.emit('networkChange', this._network);
});
yield ((_a = provider === null || provider === void 0 ? void 0 : provider.onNetworkChange) === null || _a === void 0 ? void 0 : _a.call(provider, handleNetworkChange));
}
catch (error) {
const errMsg = error.message;
this.emit('error', new WalletProviders_1.WalletNetworkChangeError(errMsg));
throw error;
}
});
}
}
exports.RiseWalletAdapter = RiseWalletAdapter;
//# sourceMappingURL=RiseWallet.js.map