diff --git a/src/utils/wallet.tsx b/src/utils/wallet.tsx index bd9afbb80..28b33c798 100644 --- a/src/utils/wallet.tsx +++ b/src/utils/wallet.tsx @@ -17,6 +17,7 @@ import { PhantomWalletAdapter, SolletExtensionAdapter, MathWalletAdapter, + BitKeepWalletAdapter, SolflareExtensionWalletAdapter, } from '../wallet-adapters'; @@ -63,6 +64,12 @@ export const WALLET_PROVIDERS = [ icon: `${ASSET_URL}/mathwallet.svg`, adapter: MathWalletAdapter, }, + { + name: 'BitKeep', + url: 'https://bitkeep.com', + icon: `${ASSET_URL}/bitkeep.svg`, + adapter: BitKeepWalletAdapter, + }, ]; const WalletContext = React.createContext(null); diff --git a/src/wallet-adapters/bitkeep/index.tsx b/src/wallet-adapters/bitkeep/index.tsx new file mode 100644 index 000000000..b808ed2cc --- /dev/null +++ b/src/wallet-adapters/bitkeep/index.tsx @@ -0,0 +1,91 @@ +import EventEmitter from 'eventemitter3'; +import { PublicKey, Transaction } from '@solana/web3.js'; +import { notify } from '../../utils/notifications'; +import { DEFAULT_PUBLIC_KEY, WalletAdapter } from '../types'; + +export class BitKeepWalletAdapter extends EventEmitter implements WalletAdapter { + _publicKey?: PublicKey; + _onProcess: boolean; + _connected: boolean; + constructor() { + super(); + this._onProcess = false; + this._connected = false; + this.connect = this.connect.bind(this); + } + + get connected() { + return this._connected; + } + + get autoApprove() { + return false; + } + + public async signAllTransactions( + transactions: Transaction[], + ): Promise { + if (!this._provider) { + return transactions; + } + + return this._provider.signAllTransactions(transactions); + } + + private get _provider() { + if ((window as any)?.solana?.isBitKeep) { + return (window as any).solana; + } + return undefined; + } + + get publicKey() { + return this._publicKey || DEFAULT_PUBLIC_KEY; + } + + async signTransaction(transaction: Transaction) { + if (!this._provider) { + return transaction; + } + + return this._provider.signTransaction(transaction); + } + + connect() { + if (this._onProcess) { + return; + } + + if (!this._provider) { + window.open('https://bitkeep.com/', '_blank'); + notify({ + message: 'BitKeep Wallet Error', + description: 'Please install BitKeep Wallet', + }); + return; + } + + this._onProcess = true; + this._provider + .getAccount() + .then((account: any) => { + this._publicKey = new PublicKey(account); + this._connected = true; + this.emit('connect', this._publicKey); + }) + .catch(() => { + this.disconnect(); + }) + .finally(() => { + this._onProcess = false; + }); + } + + disconnect() { + if (this._publicKey) { + this._publicKey = undefined; + this._connected = false; + this.emit('disconnect'); + } + } +} diff --git a/src/wallet-adapters/index.tsx b/src/wallet-adapters/index.tsx index ce93c1e5e..02b9c7800 100644 --- a/src/wallet-adapters/index.tsx +++ b/src/wallet-adapters/index.tsx @@ -2,5 +2,6 @@ export * from './ledger'; export * from './phantom'; export * from './sollet-extension'; export * from './math'; +export * from './bitkeep'; export * from './types'; export * from './solflare-extension';