@tronweb3/tronwallet-adapter-bitkeep
Version:
Wallet adapter for Bitget(BitKeep) Wallet extension and app.
330 lines • 13.4 kB
JavaScript
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());
});
};
import { Adapter, AdapterState, isInBrowser, WalletReadyState, WalletSignMessageError, WalletNotFoundError, WalletDisconnectedError, WalletSignTransactionError, WalletConnectionError, WalletGetNetworkError, isInMobileBrowser, } from '@tronweb3/tronwallet-abstract-adapter';
import { getNetworkInfoByTronWeb } from '@tronweb3/tronwallet-adapter-tronlink';
import { WalletInfo, TronLinkAdapter } from '@bitget-wallet/web3-sdk';
import { openBitgetWallet, supportBitgetWallet } from './utils.js';
export const BitgetWalletAdapterName = WalletInfo === null || WalletInfo === void 0 ? void 0 : WalletInfo.name;
export class BitKeepAdapter extends Adapter {
constructor(config = {}) {
var _a;
super();
this.name = BitgetWalletAdapterName;
this.url = WalletInfo === null || WalletInfo === void 0 ? void 0 : WalletInfo.homepage;
this.icon = (_a = WalletInfo === null || WalletInfo === void 0 ? void 0 : WalletInfo.logolist) === null || _a === void 0 ? void 0 : _a.base64;
this._readyState = WalletReadyState.Loading;
this._state = AdapterState.Loading;
this.checkReadyInterval = null;
this._checkPromise = null;
this._updateWallet = () => __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e;
let state = this.state;
let address = this.address;
if (supportBitgetWallet()) {
if (isInMobileBrowser()) {
const adapter = new TronLinkAdapter();
const tron = (yield (adapter === null || adapter === void 0 ? void 0 : adapter.getProvider().tronLink)) ||
((_a = window.bitkeep) === null || _a === void 0 ? void 0 : _a.tronLink);
this._wallet = {
tron,
tronWeb: tron === null || tron === void 0 ? void 0 : tron.tronWeb,
};
}
else {
const tronWeb = (_b = window.bitkeep) === null || _b === void 0 ? void 0 : _b.tronWeb;
this._wallet = {
tron: window.bitkeep.tronLink,
tronWeb,
};
}
address = ((_c = this._wallet.tronWeb.defaultAddress) === null || _c === void 0 ? void 0 : _c.base58) || null;
state = ((_d = this._wallet.tron) === null || _d === void 0 ? void 0 : _d.ready) ? AdapterState.Connected : AdapterState.Disconnect;
if (!((_e = this._wallet.tron) === null || _e === void 0 ? void 0 : _e.ready)) {
this.checkForWalletReady();
}
}
else {
this._wallet = null;
address = null;
state = AdapterState.NotFound;
}
this.setAddress(address);
this.setState(state);
});
const { checkTimeout = 2 * 1000, openUrlWhenWalletNotFound = true, openAppWithDeeplink = true } = config;
if (typeof checkTimeout !== 'number') {
throw new Error('[BitKeepAdapter] config.checkTimeout should be a number');
}
this.config = {
checkTimeout,
openUrlWhenWalletNotFound,
openAppWithDeeplink,
};
this._connecting = false;
this._wallet = null;
this._address = null;
if (!isInBrowser()) {
this._readyState = WalletReadyState.NotFound;
this.setState(AdapterState.NotFound);
return;
}
if (supportBitgetWallet()) {
this._readyState = WalletReadyState.Found;
this._updateWallet();
}
else {
this._checkWallet().then(() => {
if (this.connected) {
this.emit('connect', this.address || '');
}
});
}
}
get address() {
return this._address;
}
get state() {
return this._state;
}
get readyState() {
return this._readyState;
}
get connecting() {
return this._connecting;
}
/**
* Get network information.
* @returns {Network} Current network information.
*/
network() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this._checkWallet();
if (this.state !== AdapterState.Connected)
throw new WalletDisconnectedError();
const wallet = this._wallet;
if (!wallet || !wallet.tronWeb)
throw new WalletDisconnectedError();
try {
return yield getNetworkInfoByTronWeb(wallet.tronWeb);
}
catch (e) {
throw new WalletGetNetworkError(e === null || e === void 0 ? void 0 : e.message, e);
}
}
catch (e) {
this.emit('error', e);
throw e;
}
});
}
connect() {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
try {
this.checkIfOpenApp();
if (this.connected || this.connecting)
return;
yield this._checkWallet();
if (this.readyState === WalletReadyState.NotFound) {
if (this.config.openUrlWhenWalletNotFound !== false && isInBrowser()) {
window.open(this.url, '_blank');
}
throw new WalletNotFoundError();
}
const wallet = this._wallet;
if (!isInMobileBrowser()) {
if (!wallet)
return;
this._connecting = true;
try {
yield wallet.tron.request({ method: 'tron_requestAccounts' });
}
catch (e) {
throw new WalletConnectionError(e.message);
}
}
const address = ((_a = wallet === null || wallet === void 0 ? void 0 : wallet.tronWeb.defaultAddress) === null || _a === void 0 ? void 0 : _a.base58) || ((_d = (_c = (_b = window.bitkeep) === null || _b === void 0 ? void 0 : _b.tronWeb) === null || _c === void 0 ? void 0 : _c.defaultAddress) === null || _d === void 0 ? void 0 : _d.base58) || '';
this.setAddress(address);
this.setState(AdapterState.Connected);
this.emit('connect', this.address || '');
}
catch (error) {
this.emit('error', error);
throw error;
}
finally {
this._connecting = false;
}
});
}
disconnect() {
return __awaiter(this, void 0, void 0, function* () {
if (this.state !== AdapterState.Connected) {
return;
}
this.setAddress(null);
this.setState(AdapterState.Disconnect);
this.emit('disconnect');
});
}
signTransaction(transaction, privateKey) {
return __awaiter(this, void 0, void 0, function* () {
try {
const wallet = yield this.checkAndGetWallet();
try {
return yield wallet.tronWeb.trx.sign(transaction, privateKey);
}
catch (error) {
if (error instanceof Error) {
throw new WalletSignTransactionError(error.message, error);
}
else {
throw new WalletSignTransactionError(error, new Error(error));
}
}
}
catch (error) {
this.emit('error', error);
throw error;
}
});
}
multiSign(transaction, privateKey, permissionId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const wallet = yield this.checkAndGetWallet();
try {
return yield wallet.tronWeb.trx.multiSign(transaction, privateKey, permissionId);
}
catch (error) {
if (error instanceof Error) {
throw new WalletSignTransactionError(error.message, error);
}
else {
throw new WalletSignTransactionError(error, new Error(error));
}
}
}
catch (error) {
this.emit('error', error);
throw error;
}
});
}
signMessage(message, privateKey) {
return __awaiter(this, void 0, void 0, function* () {
try {
const wallet = yield this.checkAndGetWallet();
try {
return yield wallet.tronWeb.trx.signMessageV2(message, privateKey);
}
catch (error) {
if (error instanceof Error) {
throw new WalletSignMessageError(error.message, error);
}
else {
throw new WalletSignMessageError(error, new Error(error));
}
}
}
catch (error) {
this.emit('error', error);
throw error;
}
});
}
checkAndGetWallet() {
return __awaiter(this, void 0, void 0, function* () {
this.checkIfOpenApp();
yield this._checkWallet();
if (!this.connected)
throw new WalletDisconnectedError();
const wallet = this._wallet;
if (!wallet || !wallet.tronWeb)
throw new WalletDisconnectedError();
return wallet;
});
}
checkForWalletReady() {
if (this.checkReadyInterval) {
return;
}
let times = 0;
const maxTimes = Math.floor(this.config.checkTimeout / 200);
const check = () => __awaiter(this, void 0, void 0, function* () {
var _a;
if (this._wallet && ((_a = this._wallet) === null || _a === void 0 ? void 0 : _a.tron.ready)) {
this.checkReadyInterval && clearInterval(this.checkReadyInterval);
this.checkReadyInterval = null;
yield this._updateWallet();
this.emit('connect', this.address || '');
}
else if (times > maxTimes) {
this.checkReadyInterval && clearInterval(this.checkReadyInterval);
this.checkReadyInterval = null;
}
else {
times++;
}
});
this.checkReadyInterval = setInterval(check, 200);
}
/**
* check if wallet exists by interval, the promise only resolve when wallet detected or timeout
* @returns if wallet exists
*/
_checkWallet() {
if (this.readyState === WalletReadyState.Found) {
return Promise.resolve(true);
}
if (this._checkPromise) {
return this._checkPromise;
}
const interval = 100;
const maxTimes = Math.floor(this.config.checkTimeout / interval);
let times = 0, timer;
this._checkPromise = new Promise((resolve) => {
const check = () => {
times++;
const isSupport = supportBitgetWallet();
if (isSupport || times > maxTimes) {
timer && clearInterval(timer);
this._readyState = isSupport ? WalletReadyState.Found : WalletReadyState.NotFound;
this._updateWallet();
this.emit('readyStateChanged', this.readyState);
resolve(isSupport);
}
};
timer = setInterval(check, interval);
check();
});
return this._checkPromise;
}
checkIfOpenApp() {
if (this.config.openAppWithDeeplink === false) {
return;
}
if (openBitgetWallet()) {
throw new WalletNotFoundError();
}
}
setAddress(address) {
this._address = address;
}
setState(state) {
const preState = this.state;
if (state !== preState) {
this._state = state;
this.emit('stateChanged', state);
}
}
}
//# sourceMappingURL=adapter.js.map