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: add context field to snap_createInterface #2413

Merged
merged 5 commits into from
May 23, 2024
Merged
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: 1 addition & 1 deletion packages/examples/packages/bip32/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "4dOCGaoX1j+OZg4EVUWrwtuGZval0hf0uSTDUpL6OGM=",
"shasum": "9wUdqSqbVJe+0aQ6y/LxGbJK0h4bVGVG8Y6gIAP5zyU=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/bip44/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "jfMlNdZdqqc0W97lsuGx3nyUopRIjp67WXvqW1mnEmc=",
"shasum": "9Ysf5NAslS75tHeB5y4T8SkAEChEx6m2Bf4bJdU6z84=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "5c+KLOVBHhIzMeMdFPYQZNIHX0OI/dgi1mYv2D/5WB8=",
"shasum": "d8uw9NVVT/AWUsXjh+nwUOLaAtifQXT+sCJ0gTqPVjc=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/browserify/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "lov9UzWhC5bjKvVqBzZbdwgkJVkSdVLMoPkjpg3vw2c=",
"shasum": "xWwHeCztUjPa8YIFoCtTKf77qJczDgWw3v89S6ZJXwI=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "3XKHfOmSygIhzrlybA+rvrLpFIBuRofaz9IiLHlGzuk=",
"shasum": "tgk38f1RctSTY3valbyPvAWMltiFhlInsHywZH/Bq+A=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/cronjobs/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "AFBXBxUqzJe2Y/m52DFypWqSS9Y4uRFU1Fc/1RDZXGY=",
"shasum": "dF33PfyAEyIG+0x/1ggNeCScROxGheANb3U4k63pz0I=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/dialogs/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "t5YI0US9RhMAsjOfzqGNburzaQagNg2j2q4HamDP76I=",
"shasum": "7eaW1wlK1vuLEQoOyGfl4s6j7HvYwfN8lvhTim/ofUk=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "cTXs5WSTWF3EcAyY17TGxXnj7pHnEvm73S2Ggjy0vuo=",
"shasum": "DYAfBoGmsxtJAY/473htgzk0a262nFgmdkhFcbfVQlA=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/ethers-js/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "ds7zQq2RfNWr5c8g8AIoj/i6UM81HP4dnaLunR8Dbs8=",
"shasum": "6aVCE+EKxHD9mcS5asPqN4ejs6mU3+rxd6qre4Hx4v8=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "+ro+OLp+QUy/JuDWOhgKLyBHkABQV3WwbcWXTrrZnsI=",
"shasum": "P+vBqlJolHAqbfdM9F7nGXjF/YR1bs+lthGTX4lwNX8=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/get-file/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "uNy3t2aMQs11xHTYN44qP+EJgcd+D/A+HLjKLA/YA4o=",
"shasum": "HOT/KmUzzXrhnAI5PNYLapCB8kKoi9cE2ztzc8saaM4=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/home-page/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "eXviiGCf27NXpa/drwVnzm7giRRKt3ewoRpzbembkR0=",
"shasum": "rWABOhiFONdtEPQ6iFVw7XBVDswURwCNb2efK8oaBmE=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/images/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "qirq7wCYvPIlH1ZccqWsmmiRbCJq1mEUegPG3AJWQBY=",
"shasum": "7dHD4QcAJrRiowUw2LKyJDvNxz/2WN9jDPajCuHLtcg=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "WCcrRcQpQpNuWEmyN5TQuX1ckpHF/5JIsdkgMLCvjO0=",
"shasum": "qncrmZIAEJwYtq/76wIPvUCiTJ4uY1v+kpmJroamns8=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
22 changes: 10 additions & 12 deletions packages/examples/packages/interactive-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,11 @@ export const onHomePage: OnHomePageHandler = async () => {
* @returns The transaction insights.
*/
export const onTransaction: OnTransactionHandler = async ({ transaction }) => {
await snap.request({
method: 'snap_manageState',
params: {
operation: ManageStateOperation.UpdateState,
newState: { transaction },
},
});

const interfaceId = await snap.request({
method: 'snap_createInterface',
params: {
ui: await getInsightContent(),
ui: await getInsightContent(transaction),
context: { transaction },
},
});

Expand All @@ -153,25 +146,30 @@ export const onTransaction: OnTransactionHandler = async ({ transaction }) => {
* @param params - The event parameters.
* @param params.id - The Snap interface ID where the event was fired.
* @param params.event - The event object containing the event type, name and value.
* @param params.context - The Snap interface context.
* @see https://docs.metamask.io/snaps/reference/exports/#onuserinput
*/
export const onUserInput: OnUserInputHandler = async ({ id, event }) => {
export const onUserInput: OnUserInputHandler = async ({
id,
event,
context,
}) => {
if (event.type === UserInputEventType.ButtonClickEvent) {
switch (event.name) {
case 'update':
await showForm(id);
break;

case 'transaction-type':
await displayTransactionType(id);
await displayTransactionType(id, context?.transaction);
break;

case 'go-back':
await snap.request({
method: 'snap_updateInterface',
params: {
id,
ui: await getInsightContent(),
ui: await getInsightContent(context?.transaction),
},
});
break;
Expand Down
39 changes: 14 additions & 25 deletions packages/examples/packages/interactive-ui/src/ui.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
ButtonType,
ManageStateOperation,
address,
button,
copyable,
Expand Down Expand Up @@ -36,19 +35,15 @@ export async function createInterface(): Promise<string> {
/**
* Create the transaction insights components to display.
*
* @param transaction - The transaction object.
* @returns The transaction insight content.
*/
export async function getInsightContent(): Promise<Component> {
const snapState = await snap.request({
method: 'snap_manageState',
params: {
operation: ManageStateOperation.GetState,
},
});
export async function getInsightContent(
transaction?: Transaction,
): Promise<Component> {
assert(transaction, 'No transaction found in Snap context.');

assert(snapState?.transaction, 'No transaction found in Snap state.');

const { from, to } = snapState.transaction as Transaction;
const { from, to } = transaction as Transaction;

return panel([
row('From', address(from)),
Expand All @@ -58,22 +53,16 @@ export async function getInsightContent(): Promise<Component> {
}

/**
* Update a Snap interface to display the transaction type after fetching
* the transaction from state.
* Update a Snap interface to display the transaction type.
*
* @param id - The interface ID to update.
* @param id - The interface ID to update.
* @param transaction - The transaction object.
*/
export async function displayTransactionType(id: string) {
const snapState = await snap.request({
method: 'snap_manageState',
params: {
operation: ManageStateOperation.GetState,
},
});

assert(snapState?.transaction, 'No transaction found in Snap state.');

const transaction = snapState.transaction as Transaction;
export async function displayTransactionType(
id: string,
transaction?: Transaction,
) {
assert(transaction, 'No transaction found in Snap context.');

const type = decodeData(transaction.data);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "+RiAww9tB7ZbSNnhFuaN3UO9VIYAICxwIT4X9c+XY5o=",
"shasum": "GgSToaZ1ZQ2EAUsER9Ipz7JjXiP34tAh8yHlSeZnrIw=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "FgE3bkyOtLM+Xh3yWPkbfSUU1T8OFYSu4ViHzTr3ttg=",
"shasum": "qRjdWlOFuznynW12exjTado0bOvmHuyD9tJ6EYllTfI=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/json-rpc/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "nGoSEdNAKM/q9mtRCfLyOaq9axv3zMNItVcHHK2t/5Q=",
"shasum": "ecGcLmxHbfbcggo+xXwKiKqn0jwvZpJBy9jzwbuhx14=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/jsx/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "LUIVyk3+RKCfl4/rLeAWrMsEeiByKUBHLpLJrG9ne4Q=",
"shasum": "HoevSNqYDSD1M1rbAEDLdad/KnqdeJCR/qVQpXoRrfI=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "IYn+DHmfYp5W8YSsGSj/Tp9jwde8pZPV3QMKBu3eyUg=",
"shasum": "hoY7FWJwVVeWRRz7u0VKyQTnMoLGLSa5aRfyDXPj9AM=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "PFeFk5Pj8mT2DfmeAvXHX465zgYipTbi0seGEsV59iU=",
"shasum": "y+DckJzrDa6DAdy9g1K7sWK98XLi3b6byj6/SW+mvp0=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "vs6lQZrLO6I/0cFIiXpYG+9UcGhNUVuvMQP1Krd8i44=",
"shasum": "QRgU12P+k4MbET92wLU6RpueK9KYZb3HGItOH8pdXqo=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "10nPm2qcs2YypKXV4y7yUGYsG74blyVBCt+DLZBhxpc=",
"shasum": "idx+4WZjLaansFM6HrxdUpXkhcr3EDknwrRF5xcDPL0=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "gZi+6wk3FJAQtOxmcdqYGwZB0p+JRpzvkBzD7bbyEIQ=",
"shasum": "OozOdoPDn3miL7PzQHBhVDjbA8QULaNurUjXe8pEO5E=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "TAFDZQTBk0VXOnyvQou54DLLd5Yo80X/gTE4FB30Oe8=",
"shasum": "3NBcGdsi/8Tw7mYaRS+hhHcLMDbfxZmz6AUJHSPEJ1k=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "fsMOq+VpuKAGx56RLOMYMYkDMOrjJQYf9FRkQz2M53M=",
"shasum": "DqHMyRiHljEYgKJZXJaYhFNCeXZAlc2sCjyI8+7Vgkg=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "y8qxSYdffr4P2Buu/VVM2ZBzB5HCOl6CX67LqKCcJMs=",
"shasum": "oo7KkCxPI9OXMaS+Ik9qdj4AulwiwTrSNCHtMe0WPP4=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/wasm/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "NpCJVa8aJ70ZlKLmdhtjJ4g4h39pgTEU2NTK031mnRk=",
"shasum": "p6jcfAtN/r6hg314wA7Z9ihSAVP7+zm4KuZbB/Cu6+k=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "0iiw8G58ip4VE7EQ1lxQePIWwPzZKZJimH99qA8xIkc=",
"shasum": "zgVFc93FtIDmzDPyP80bN1gTPgIE+uhPrABgpoP83Gs=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
8 changes: 4 additions & 4 deletions packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 91.44,
"functions": 96.73,
"lines": 97.86,
"statements": 97.53
"branches": 91.5,
"functions": 96.74,
"lines": 97.87,
"statements": 97.54
}
Loading
Loading