Skip to content

Commit

Permalink
fix: remove flask detection code
Browse files Browse the repository at this point in the history
closes #62
  • Loading branch information
hugomrdias committed Sep 12, 2023
1 parent f5c4b83 commit 776c41b
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 108 deletions.
10 changes: 0 additions & 10 deletions examples/demo/src/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,6 @@ export function App() {
>
{import.meta.env.GIT_COMMIT_HASH.slice(0, 7)}
</a>{' '}
<a
title="Release tag"
target="_blank"
href={`https://github.com/filecoin-project/filsnap/releases/tag/${
import.meta.env.GIT_TAG
}`}
rel="noreferrer"
>
{import.meta.env.GIT_TAG}
</a>{' '}
{import.meta.env.GIT_DATE}
</code>
</li>
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-react/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function FilsnapProvider({ snapId, snapVersion, config, children }) {
if (mounted) {
setError(undefined)
try {
const hasFlask = await FilsnapAdapter.hasFlask()
const hasFlask = await FilsnapAdapter.hasSnaps()
setHasFlask(hasFlask)
} catch (error) {
const err = /** @type {Error} */ (error)
Expand Down
10 changes: 5 additions & 5 deletions packages/adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ pnpm install filsnap-adapter

## Usage

This adapter interacts directly with the snap, so Metamask Flask needs to be installed and unlocked in the browser.
This adapter interacts directly with the snap, so Metamask with support for Snaps needs to be installed and unlocked in the browser.

```js
import { FilsnapAdapter } from 'filsnap-adapter'

const hasFlask = await FilsnapAdapter.hasFlask()
if (!hasFlask) {
console.error('Flask not installed')
const hasSnaps = await FilsnapAdapter.hasSnaps()
if (!hasSnaps) {
console.error('Metamask with Snaps support is not installed')
return
}

Expand All @@ -38,7 +38,7 @@ if (error) {
// t1d2xrzcslx7xlbbylc5c3d5lvandqw4iwl6epxba
}

const isConnected = await FilsnapAdapter.isConnected()
const isAvailable = await FilsnapAdapter.isAvailable()
// true
```

Expand Down
3 changes: 2 additions & 1 deletion packages/adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
}
},
"dependencies": {
"filsnap": "workspace:^"
"filsnap": "workspace:^",
"iso-filecoin": "^3.0.0"
},
"devDependencies": {
"@playwright/test": "^1.37.1",
Expand Down
90 changes: 44 additions & 46 deletions packages/adapter/src/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
RequestWithFilSnap,
SnapConfig,
} from 'filsnap'
import { RPC } from 'iso-filecoin/rpc'

type SnapsResult = Record<
string,
Expand All @@ -24,22 +25,21 @@ export class FilsnapAdapter {
}

/**
* Check if Metamask flask is installed and enabled
* Check if Metamask has Snaps API
*/
static async hasFlask(): Promise<boolean> {
static async hasSnaps(): Promise<boolean> {
if (window.ethereum == null || !window.ethereum.isMetaMask) {
return false
}

const version = await window.ethereum.request<string>({
method: 'web3_clientVersion',
})

if (version == null || (version.length > 0 && !version.includes('flask'))) {
try {
await window.ethereum.request<SnapsResult>({
method: 'wallet_getSnaps',
})
return true
} catch {
return false
}

return true
}

/**
Expand All @@ -48,12 +48,12 @@ export class FilsnapAdapter {
* @param snapId - Snap ID to check for. Defaults to `npm:filsnap` which is the default ID for the Filsnap snap.
* @param snapVersion - Snap version to check for. Defaults to `*` which matches any version.
*/
static async isConnected(
static async isAvailable(
snapId: string = 'npm:filsnap',
snapVersion: string = '*'
): Promise<boolean> {
const hasFlask = await FilsnapAdapter.hasFlask()
if (!hasFlask) {
const hasSnaps = await FilsnapAdapter.hasSnaps()
if (!hasSnaps) {
return false
}

Expand All @@ -80,40 +80,10 @@ export class FilsnapAdapter {
return true
}

/**
* Create and configure a new Filsnap adapter
*
* This will check if Filsnap is installed and enabled, and if not, throw an error.
*
* @param config - Snap config
* @param snapId - Snap ID to check for. Defaults to `npm:filsnap` which is the default ID for the Filsnap snap.
* @param snapVersion - Snap version to check for. Defaults to `*` which matches any version.
*/
static async create(
config: Parameters<FilSnapMethods['fil_configure']>[1],
snapId: string = 'npm:filsnap',
snapVersion: string = '*'
): Promise<FilsnapAdapter> {
const hasFlask = await FilsnapAdapter.hasFlask()
if (!hasFlask) {
throw new Error('Flask is not installed.')
}

const isConnected = await FilsnapAdapter.isConnected(snapId, snapVersion)

if (!isConnected) {
throw new Error('Filsnap is not connected.')
}

const adapter = new FilsnapAdapter(snapId, snapVersion)
await adapter.configure(config)
return adapter
}

/**
* Installs and connects to Filsnap
*
* @throws Error if Metamask flask is not installed
* @throws Error if Metamask is not installed
*
* @param config - Snap config
* @param snapId - Snap ID to check for. Defaults to `npm:filsnap` which is the default ID for the Filsnap snap.
Expand All @@ -124,9 +94,11 @@ export class FilsnapAdapter {
snapId: string = 'npm:filsnap',
snapVersion: string = '*'
): Promise<FilsnapAdapter> {
const hasFlask = await FilsnapAdapter.hasFlask()
if (!hasFlask) {
throw new Error('Flask is not installed')
const hasSnaps = await FilsnapAdapter.hasSnaps()
if (!hasSnaps) {
throw new Error(
'Metamask does not have the Snaps API. Please update to the latest version.'
)
}

const snaps = await window.ethereum.request<SnapsResult>({
Expand All @@ -153,6 +125,32 @@ export class FilsnapAdapter {
return adapter
}

/**
* Check dapp is connected to Filsnap
*
* @returns `true` if connected to Filsnap, `false` otherwise
*/
isConnected(): boolean {
return this.config != null
}

/**
* Get the RPC instance configured by Filsnap
*
* @returns RPC instance
*/
rpc(): RPC {
if (this.config == null) {
throw new Error('Not connected to Filsnap')
}

return new RPC({
token: this.config.rpc.token,
api: this.config.rpc.url,
network: this.config.network,
})
}

/**
* Configure the snap
*
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/filecoin-project/filsnap.git"
},
"source": {
"shasum": "oZQy5ubCvJcJ7+6ozV35DHSc9mZPpIEvzfAGdFSR4H4=",
"shasum": "GaHeQ59kzC3GGcDTg0ehuWTs0toIsuv2ug6vAK8DaI0=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
36 changes: 36 additions & 0 deletions packages/snap/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,40 @@ export class State {

return config
}

async delete(origin: string): Promise<boolean> {
const state = (await this.snap.request({
method: 'snap_manageState',
params: { operation: 'get' },
})) as unknown as Record<string, SnapConfig> | null

if (state == null || state[origin] == null) {
return false
}

// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete state[origin]
await this.snap.request({
method: 'snap_manageState',
params: {
newState: state,
operation: 'update',
},
})

return true
}

async has(origin: string): Promise<boolean> {
const state = (await this.snap.request({
method: 'snap_manageState',
params: { operation: 'get' },
})) as unknown as Record<string, SnapConfig> | null

if (state == null || state[origin] == null) {
return false
}

return true
}
}
2 changes: 1 addition & 1 deletion packages/snap/src/transaction-insight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function contractAddressMatches(transactionTo: string | undefined): boolean {
)
}

// Note: currently MetaMask Flask shows the transaction insight tab by default even if we don't display any information
// Note: currently MetaMask shows the transaction insight tab by default even if we don't display any information
// in it. This is a bug that the MetaMask team is addressing in this PR:
// https://github.com/MetaMask/metamask-extension/pull/20267
export const onTransaction: OnTransactionHandler = async ({
Expand Down

0 comments on commit 776c41b

Please sign in to comment.