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

feat: New Apis for Snap #60

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
"yaml@npm:^2.0.0": "^2.3.1"
},
"dependencies": {
"@metamask/snaps-cli": "^0.32.2",
"@metamask/snaps-ui": "^0.32.2",
"cross-env": "^7.0.3",
"crypto": "^1.0.1",
"gatsby-cli": "^5.13.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/site/src/utils/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const connectSnap = async (
await window.ethereum.request({
method: 'wallet_requestSnaps',
params: {
[snapId]: params,
'local:http://localhost:8080': {},
},
});
};
Expand Down
Binary file added packages/snap/images/insights-display.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions packages/snap/images/process-explanation.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 8 additions & 7 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@
"serve": "mm-snap serve",
"start": "mm-snap watch"
},
"dependencies": {
"@metamask/snaps-sdk": "^3.1.1"
},
"devDependencies": {
"@lavamoat/allow-scripts": "^2.5.1",
"@metamask/auto-changelog": "^3.4.4",
"@metamask/eslint-config": "^11.1.0",
"@metamask/eslint-config-jest": "^11.1.0",
"@metamask/eslint-config-nodejs": "^11.1.0",
"@metamask/eslint-config-typescript": "^11.1.0",
"@metamask/snaps-cli": "^0.32.2",
"@metamask/snaps-types": "^0.32.2",
"@metamask/snaps-ui": "^0.32.2",
"@metamask/snaps-cli": "^6.0.2",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"eslint": "^8.57.0",
Expand All @@ -55,16 +56,16 @@
"rimraf": "^3.0.2",
"typescript": "^4.9.5"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"packageManager": "[email protected]",
"engines": {
"node": ">=16.0.0"
},
"volta": {
"node": "16.20.0",
"yarn": "3.3.1"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
}
}
24 changes: 22 additions & 2 deletions packages/snap/snap.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
module.exports = {
cliOptions: {
src: './src/index.ts',
bundler: 'webpack',
input: 'src/index.ts',
output: {
filename: 'bundle.js',
path: 'dist',
},
server: {
port: 8080,
},
manifest: {
path: 'snap.manifest.json',
},
features: {
images: true,
},
environment: {
SNAP_ENV: process.env.SNAP_ENV,

Check failure on line 18 in packages/snap/snap.config.js

View workflow job for this annotation

GitHub Actions / Lint (16.x)

Unexpected use of process.env

Check failure on line 18 in packages/snap/snap.config.js

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Unexpected use of process.env
PUBLIC_KEY: process.env.PUBLIC_KEY,

Check failure on line 19 in packages/snap/snap.config.js

View workflow job for this annotation

GitHub Actions / Lint (16.x)

Unexpected use of process.env

Check failure on line 19 in packages/snap/snap.config.js

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Unexpected use of process.env
},
polyfills: {
buffer: true,
crypto: true,
path: true,
},
};
6 changes: 4 additions & 2 deletions 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/kleros/scout-snap.git"
},
"source": {
"shasum": "KCzo/QBHtC+GPdGeVJaIgLy+O5nucZ3jEY7PvNbBtH4=",
"shasum": "l+waekI2X/Lm43VZ8VljivOlJgdOCu5ne9wNGWHICAc=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand All @@ -21,7 +21,9 @@
"endowment:transaction-insight": {
"allowTransactionOrigin": true
},
"endowment:network-access": {}
"endowment:network-access": {},
"endowment:page-home": {},
"endowment:lifecycle-hooks": {}
},
"manifestVersion": "0.1"
}
78 changes: 75 additions & 3 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { OnTransactionHandler } from '@metamask/snaps-types';
import { panel, heading, text } from '@metamask/snaps-ui';
import {

Check failure on line 1 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint (16.x)

There should be at least one empty line between import groups

Check failure on line 1 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

There should be at least one empty line between import groups
OnHomePageHandler,
OnUpdateHandler,
OnTransactionHandler,
OnInstallHandler,
panel,
text,
heading,
divider,
image,
} from '@metamask/snaps-sdk';
import InsightsDisplayImage from '../images/insights-display.png';
import ProcessExplanationImage from '../images/process-explanation.svg';

Check failure on line 13 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint (16.x)

There should be at least one empty line between import groups

Check failure on line 13 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

There should be at least one empty line between import groups
// eslint-disable-next-line import/no-extraneous-dependencies
import mdEscape from 'markdown-escape';

Check failure on line 15 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint (16.x)

`markdown-escape` import should occur before import of `../images/insights-display.png`

Check failure on line 15 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

`markdown-escape` import should occur before import of `../images/insights-display.png`

// Define types
type AddressTag = {
caipAddress: string;
publicName: string;
Expand Down Expand Up @@ -216,13 +228,73 @@
'No insights available for this contract. Interact at your own risk.',
);
insights.push(
'Do you know this contract? Submit insights on curate.kleros.io in Gnosis Chain and earn rewards!',
'Do you know this contract? Submit insights on [curate.kleros.io](https://curate.kleros.io) in Gnosis Chain and earn rewards!',
);
}

return insights;
};

export const onInstall: OnInstallHandler = async () => {
await snap.request({
method: 'snap_dialog',
params: {
type: 'alert',
content: panel([
heading(
'Kleros Scout’s community curated contract insights secures your dApp browsing.',
),
text(
'Congrats on taking a crucial step towards safeguarding your wallet interactions!',
),
divider(),
heading('How to use the Snap?'),
text(
'The Kleros Scout Snap provides 3 points of insight on every transaction:',
),
text(
'**Project:** _Which project does the contract you’re interacting with belong to?_',
),
text(
'**Contract Tag:** _What is the function or tag associated with the smart contract?_',
),
text('**Domain:** _Which domain(s) is this contract interacted in?_'),
image(InsightsDisplayImage),
]),
},
});
};

export const onHomePage: OnHomePageHandler = async () => {
return {
content: panel([
heading(
'Kleros Scout’s community curated contract insights secures your dApp browsing.',
),
divider(),
heading('How does it work?'),
text(
'Anyone can submit contract insights & earn up to $15 per entry! [Head here to know more.](https://klerosscout.eth.limo)',
),
image(ProcessExplanationImage),
]),
};
};

export const onUpdate: OnUpdateHandler = async () => {
await snap.request({
method: 'snap_dialog',
params: {
type: 'alert',
content: panel([
heading('Update successful'),
text('New features added in this version.'),
text(''),
]),
},
});
};

export const onTransaction: OnTransactionHandler = async ({
transactionOrigin,
transaction,
Expand All @@ -237,7 +309,7 @@
}
}
const numericChainId = parseInt(chainId.split(':')[1], 16);
const caipAddress = `eip155:${numericChainId}:${transaction.to as string}`;

Check failure on line 312 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint (16.x)

This assertion is unnecessary since it does not change the type of the expression

Check failure on line 312 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

This assertion is unnecessary since it does not change the type of the expression
console.log(JSON.stringify(transaction));

const insights = await getInsights(caipAddress, domain);
Expand Down
Loading
Loading