Add support for MathWallet extension (#63)

* mathwallet providerUrl

* mathwallet providerUrl

* mathwallet

* fix

* fix

* fix

* fix

* Delete yarn.lock

* Revert "Delete yarn.lock"

This reverts commit 21104038d8.

* Update yarn.lock

* Update yarn.lock
This commit is contained in:
Forrest 2021-03-12 17:27:30 +08:00 committed by GitHub
parent b07166de47
commit c96c5971fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 99 additions and 0 deletions

View File

@ -16,6 +16,7 @@ import {
LedgerWalletAdapter,
SolongWalletAdapter,
PhantomWalletAdapter,
MathWalletAdapter,
} from '../wallet-adapters';
const ASSET_URL =
@ -44,6 +45,12 @@ export const WALLET_PROVIDERS = [
icon: `https://www.phantom.app/img/logo.png`,
adapter: PhantomWalletAdapter,
},
{
name: 'MathWallet',
url: 'https://www.mathwallet.org',
icon: `${ASSET_URL}/mathwallet.svg`,
adapter: MathWalletAdapter,
}
];
const WalletContext = React.createContext<null | WalletContextValues>(null);

View File

@ -1,4 +1,5 @@
export * from './ledger';
export * from './solong';
export * from './phantom';
export * from './math';
export * from './types';

View File

@ -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 MathWalletAdapter 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<Transaction[]> {
if (!this._provider) {
return transactions;
}
return this._provider.signAllTransactions(transactions);
}
private get _provider() {
if ((window as any)?.solana?.isMathWallet) {
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://mathwallet.org/', '_blank');
notify({
message: 'Math Wallet Error',
description: 'Please install mathwallet',
});
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');
}
}
}