From fd504b23d39699b4e29c2809b3c5c52feb86c93f Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Wed, 26 Mar 2025 20:48:09 +0100 Subject: [PATCH 1/2] Slightly refactor send flow example --- .../packages/send-flow/snap.manifest.json | 2 +- .../examples/packages/send-flow/src/index.tsx | 20 +++++----------- .../examples/packages/send-flow/src/utils.tsx | 23 +++++++++++++++++++ 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/packages/examples/packages/send-flow/snap.manifest.json b/packages/examples/packages/send-flow/snap.manifest.json index e17bc029a6..04a3ca802f 100644 --- a/packages/examples/packages/send-flow/snap.manifest.json +++ b/packages/examples/packages/send-flow/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "INn+njbWhdgWBpvU4y5opvJpDWSyT14xl7DQEvwY0JM=", + "shasum": "fwMVpG2q/9ej3M3mAf5A/gfeXhZI7W2a4veQmVHKlME=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/send-flow/src/index.tsx b/packages/examples/packages/send-flow/src/index.tsx index 87240c7cdd..103036dd8f 100644 --- a/packages/examples/packages/send-flow/src/index.tsx +++ b/packages/examples/packages/send-flow/src/index.tsx @@ -3,16 +3,13 @@ import type { OnHomePageHandler, OnUserInputHandler, OnRpcRequestHandler, - CaipAccountId, } from '@metamask/snaps-sdk'; import { UserInputEventType } from '@metamask/snaps-sdk'; -import { is } from '@metamask/superstruct'; -import { HexChecksumAddressStruct, parseCaipAccountId } from '@metamask/utils'; import { SendFlow } from './components'; import { accountsArray, accounts } from './data'; import type { SendFormState, SendFlowContext } from './types'; -import { formValidation, generateSendFlow } from './utils'; +import { formValidation, generateSendFlow, isCaipHexAddress } from './utils'; /** * Handle incoming JSON-RPC requests from the dapp, sent through the @@ -102,15 +99,6 @@ export const onUserInput: OnUserInputHandler = async ({ switch (event.name) { case 'amount': case 'to': { - // For testing purposes, we display the avatar if the address is a valid hex checksum address. - let parsedAddress; - try { - parsedAddress = parseCaipAccountId( - event.value as CaipAccountId, - ).address; - } catch { - /** noop */ - } await snap.request({ method: 'snap_updateInterface', params: { @@ -123,13 +111,16 @@ export const onUserInput: OnUserInputHandler = async ({ total={total} fees={fees} errors={formErrors} - displayAvatar={is(parsedAddress, HexChecksumAddressStruct)} + // For testing purposes, we display the avatar if the address is + // a valid hex checksum address. + displayAvatar={isCaipHexAddress(event.value)} /> ), }, }); break; } + case 'accountSelector': { await snap.request({ method: 'snap_updateInterface', @@ -150,6 +141,7 @@ export const onUserInput: OnUserInputHandler = async ({ break; } + default: break; } diff --git a/packages/examples/packages/send-flow/src/utils.tsx b/packages/examples/packages/send-flow/src/utils.tsx index 9219ed5c2e..e62f42dad8 100644 --- a/packages/examples/packages/send-flow/src/utils.tsx +++ b/packages/examples/packages/send-flow/src/utils.tsx @@ -1,3 +1,11 @@ +import type { Hex } from '@metamask/utils'; +import { + isStrictHexString, + isCaipAccountId, + parseCaipAccountId, + isValidHexAddress, +} from '@metamask/utils'; + import { SendFlow } from './components'; import type { Account, @@ -88,3 +96,18 @@ export function truncate(str: string, length: number): string { ? `${str.slice(0, 5)}...${str.slice(str.length - 5, str.length)}` : str; } + +/** + * Check if a string is a valid hex address. + * + * @param value - The value to check. + * @returns Whether the value is a valid hex address. + */ +export function isCaipHexAddress(value: unknown): value is Hex { + if (isCaipAccountId(value)) { + const { address } = parseCaipAccountId(value); + return isStrictHexString(address) && isValidHexAddress(address); + } + + return false; +} From 901bc92f8fa9c6373d20744c0fa77ce56c3f1a79 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Wed, 26 Mar 2025 20:57:23 +0100 Subject: [PATCH 2/2] Remove unused dependency --- packages/examples/packages/send-flow/package.json | 1 - yarn.lock | 1 - 2 files changed, 2 deletions(-) diff --git a/packages/examples/packages/send-flow/package.json b/packages/examples/packages/send-flow/package.json index 3794901feb..30b4243b66 100644 --- a/packages/examples/packages/send-flow/package.json +++ b/packages/examples/packages/send-flow/package.json @@ -45,7 +45,6 @@ "dependencies": { "@metamask/rpc-errors": "^7.0.2", "@metamask/snaps-sdk": "workspace:^", - "@metamask/superstruct": "^3.1.0", "@metamask/utils": "^11.2.0" }, "devDependencies": { diff --git a/yarn.lock b/yarn.lock index 87f5d50eaf..ddb484ec72 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5425,7 +5425,6 @@ __metadata: "@metamask/snaps-cli": "workspace:^" "@metamask/snaps-jest": "workspace:^" "@metamask/snaps-sdk": "workspace:^" - "@metamask/superstruct": "npm:^3.1.0" "@metamask/utils": "npm:^11.2.0" "@swc/core": "npm:1.3.78" "@swc/jest": "npm:^0.2.26"