Skip to content

Commit

Permalink
enhancement: displaying link to metamask when snap is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Aug 22, 2024
1 parent 08ec3bb commit 9b36d26
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,20 @@ export type GetSnapsResponse = Record<string, Snap>

export * from './metamask'

interface WalletPluginMetaMaskConfig {
snapOrigin?: string
setupPageUrl?: string
}

const defaultSnapOrigin = 'local:http://localhost:8080'
// const defaultSnapOrigin = 'npm:@greymass/test-snap'
const defaultSetupPageUrl = 'https://unicove.com/eos/metamask'

export class WalletPluginMetaMask extends AbstractWalletPlugin implements WalletPlugin {
public id = 'wallet-plugin-metamask'

snapOrigin: string
setupPageUrl: string

readonly config: WalletPluginConfig = {
requiresChainSelect: true,
requiresPermissionSelect: true,
Expand All @@ -39,6 +48,13 @@ export class WalletPluginMetaMask extends AbstractWalletPlugin implements Wallet
download: '',
})

constructor(walletPluginMetaMaskConfig?: WalletPluginMetaMaskConfig) {
super()

this.snapOrigin = walletPluginMetaMaskConfig?.snapOrigin || defaultSnapOrigin
this.setupPageUrl = walletPluginMetaMaskConfig?.setupPageUrl || defaultSetupPageUrl
}

login(context: LoginContext): Cancelable<WalletPluginLoginResponse> {
const promise = this.metamaskLogin(context)
return cancelable(promise, (canceled) => {
Expand All @@ -47,7 +63,7 @@ export class WalletPluginMetaMask extends AbstractWalletPlugin implements Wallet
}

async metamaskLogin(context: LoginContext): Promise<WalletPluginLoginResponse> {
await this.initialize()
await this.initialize(context)
if (!this.provider) {
throw new Error('Metamask not found')
}
Expand Down Expand Up @@ -120,18 +136,19 @@ export class WalletPluginMetaMask extends AbstractWalletPlugin implements Wallet
return data
}

async initialize() {
async initialize(context?: LoginContext) {
if (!this.provider) {
this.provider = await getSnapsProvider()
}
if (this.provider && !this.installedSnap) {
this.isFlask = await checkIsFlask(this.provider)
await this.setSnap()
if (!this.installedSnap) {
await this.requestSnap()
if (this.installedSnap) {
await this.initialize()
}
context?.ui?.prompt({
title: 'Snap not found',
body: `Plese visit ${this.setupPageUrl} for help setting up the snap.`,
elements: [],
})
}
}
}
Expand Down

0 comments on commit 9b36d26

Please sign in to comment.