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

Added optional config values for origin + service #3

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import {checkIsFlask, getSnapsProvider, InvokeSnapParams, Snap} from './metamask

export type GetSnapsResponse = Record<string, Snap>

const DEFAULT_SNAP_ORIGIN = 'npm:@greymass/eos-wallet'
const ACCOUNT_CREATION_SERVICE_URL = 'https://eos.account.unicove.com/buy'
const defaultSetupPageUrl = 'https://unicove.com/eos/metamask'
const DEFAULT_SETUP_PAGE = 'https://unicove.com/eos/metamask'
const DEFAULT_SNAP_ORIGIN = 'npm:@greymass/eos-wallet'

export interface AccountCreationPluginMetaMaskConfig {
accountCreationServiceUrl?: string
snapOrigin?: string
setupPageUrl?: string
}

Expand All @@ -29,7 +31,10 @@ export class AccountCreationPluginMetamask
public installedSnap: Snap | null = null
public provider: MetaMaskInpageProvider | null = null
public isFlask = false
public setupPageUrl

public accountCreationServiceUrl: string
public snapOrigin: string
public setupPageUrl: string

readonly config: AccountCreationPluginConfig = {
requiresChainSelect: true,
Expand All @@ -44,7 +49,10 @@ export class AccountCreationPluginMetamask
constructor(walletPluginMetaMaskConfig?: AccountCreationPluginMetaMaskConfig) {
super()

this.setupPageUrl = walletPluginMetaMaskConfig?.setupPageUrl || defaultSetupPageUrl
this.accountCreationServiceUrl =
walletPluginMetaMaskConfig?.accountCreationServiceUrl || ACCOUNT_CREATION_SERVICE_URL
this.snapOrigin = walletPluginMetaMaskConfig?.snapOrigin || DEFAULT_SNAP_ORIGIN
this.setupPageUrl = walletPluginMetaMaskConfig?.setupPageUrl || DEFAULT_SETUP_PAGE
}

get id(): string {
Expand Down Expand Up @@ -81,7 +89,7 @@ export class AccountCreationPluginMetamask
qs.set('active_key', String(publicKey))
const accountCreator = new AccountCreator({
supportedChains: [String(currentChain.id)],
fullCreationServiceUrl: `${ACCOUNT_CREATION_SERVICE_URL}?${qs.toString()}`,
fullCreationServiceUrl: `${this.accountCreationServiceUrl}?${qs.toString()}`,
scope: context.appName || 'Antelope App',
})
const accountCreationResponse = await accountCreator.createAccount()
Expand Down Expand Up @@ -156,11 +164,11 @@ export class AccountCreationPluginMetamask
method: 'wallet_getSnaps',
params: {},
})) as GetSnapsResponse
this.installedSnap = snaps[DEFAULT_SNAP_ORIGIN] ?? null
this.installedSnap = snaps[this.snapOrigin] ?? null
}

async requestSnap(id?: string, version?: string) {
const snapId = id || DEFAULT_SNAP_ORIGIN
const snapId = id || this.snapOrigin
const snaps = (await this.request({
method: 'wallet_requestSnaps',
params: {
Expand All @@ -171,7 +179,7 @@ export class AccountCreationPluginMetamask
}

async invokeSnap({method, params}: InvokeSnapParams, id?: string) {
const snapId = id || DEFAULT_SNAP_ORIGIN
const snapId = id || this.snapOrigin
return this.request({
method: 'wallet_invokeSnap',
params: {snapId, request: {method, params}},
Expand Down