Skip to content

Commit b1969a7

Browse files
authored
Slightly refactor send flow example (#3268)
1 parent 4fcdda6 commit b1969a7

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

packages/examples/packages/send-flow/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
"dependencies": {
4646
"@metamask/rpc-errors": "^7.0.2",
4747
"@metamask/snaps-sdk": "workspace:^",
48-
"@metamask/superstruct": "^3.1.0",
4948
"@metamask/utils": "^11.2.0"
5049
},
5150
"devDependencies": {

packages/examples/packages/send-flow/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "INn+njbWhdgWBpvU4y5opvJpDWSyT14xl7DQEvwY0JM=",
10+
"shasum": "fwMVpG2q/9ej3M3mAf5A/gfeXhZI7W2a4veQmVHKlME=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/send-flow/src/index.tsx

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ import type {
33
OnHomePageHandler,
44
OnUserInputHandler,
55
OnRpcRequestHandler,
6-
CaipAccountId,
76
} from '@metamask/snaps-sdk';
87
import { UserInputEventType } from '@metamask/snaps-sdk';
9-
import { is } from '@metamask/superstruct';
10-
import { HexChecksumAddressStruct, parseCaipAccountId } from '@metamask/utils';
118

129
import { SendFlow } from './components';
1310
import { accountsArray, accounts } from './data';
1411
import type { SendFormState, SendFlowContext } from './types';
15-
import { formValidation, generateSendFlow } from './utils';
12+
import { formValidation, generateSendFlow, isCaipHexAddress } from './utils';
1613

1714
/**
1815
* Handle incoming JSON-RPC requests from the dapp, sent through the
@@ -102,15 +99,6 @@ export const onUserInput: OnUserInputHandler = async ({
10299
switch (event.name) {
103100
case 'amount':
104101
case 'to': {
105-
// For testing purposes, we display the avatar if the address is a valid hex checksum address.
106-
let parsedAddress;
107-
try {
108-
parsedAddress = parseCaipAccountId(
109-
event.value as CaipAccountId,
110-
).address;
111-
} catch {
112-
/** noop */
113-
}
114102
await snap.request({
115103
method: 'snap_updateInterface',
116104
params: {
@@ -123,13 +111,16 @@ export const onUserInput: OnUserInputHandler = async ({
123111
total={total}
124112
fees={fees}
125113
errors={formErrors}
126-
displayAvatar={is(parsedAddress, HexChecksumAddressStruct)}
114+
// For testing purposes, we display the avatar if the address is
115+
// a valid hex checksum address.
116+
displayAvatar={isCaipHexAddress(event.value)}
127117
/>
128118
),
129119
},
130120
});
131121
break;
132122
}
123+
133124
case 'accountSelector': {
134125
await snap.request({
135126
method: 'snap_updateInterface',
@@ -150,6 +141,7 @@ export const onUserInput: OnUserInputHandler = async ({
150141

151142
break;
152143
}
144+
153145
default:
154146
break;
155147
}

packages/examples/packages/send-flow/src/utils.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
import type { Hex } from '@metamask/utils';
2+
import {
3+
isStrictHexString,
4+
isCaipAccountId,
5+
parseCaipAccountId,
6+
isValidHexAddress,
7+
} from '@metamask/utils';
8+
19
import { SendFlow } from './components';
210
import type {
311
Account,
@@ -88,3 +96,18 @@ export function truncate(str: string, length: number): string {
8896
? `${str.slice(0, 5)}...${str.slice(str.length - 5, str.length)}`
8997
: str;
9098
}
99+
100+
/**
101+
* Check if a string is a valid hex address.
102+
*
103+
* @param value - The value to check.
104+
* @returns Whether the value is a valid hex address.
105+
*/
106+
export function isCaipHexAddress(value: unknown): value is Hex {
107+
if (isCaipAccountId(value)) {
108+
const { address } = parseCaipAccountId(value);
109+
return isStrictHexString(address) && isValidHexAddress(address);
110+
}
111+
112+
return false;
113+
}

yarn.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5425,7 +5425,6 @@ __metadata:
54255425
"@metamask/snaps-cli": "workspace:^"
54265426
"@metamask/snaps-jest": "workspace:^"
54275427
"@metamask/snaps-sdk": "workspace:^"
5428-
"@metamask/superstruct": "npm:^3.1.0"
54295428
"@metamask/utils": "npm:^11.2.0"
54305429
"@swc/core": "npm:1.3.78"
54315430
"@swc/jest": "npm:^0.2.26"

0 commit comments

Comments
 (0)