Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MathWallet extension #63

Merged
merged 13 commits into from Mar 12, 2021
7 changes: 7 additions & 0 deletions src/utils/wallet.tsx
Expand Up @@ -16,6 +16,7 @@ import {
LedgerWalletAdapter,
SolongWalletAdapter,
PhantomWalletAdapter,
MathWalletAdapter,
} from '../wallet-adapters';

const ASSET_URL =
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/wallet-adapters/index.tsx
@@ -1,4 +1,5 @@
export * from './ledger';
export * from './solong';
export * from './phantom';
export * from './math';
export * from './types';
91 changes: 91 additions & 0 deletions src/wallet-adapters/math/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 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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting an error when calling getAccount, is this supposed to be getAccounts()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
The getAccount method is correct, update MathWallet extension to v2.6.4.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed v2.6.4 works

.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');
}
}
}