Skip to content

Commit 82ea67b

Browse files
authored
Add displayAvatar prop to AddressInput component (#3264)
The `displayAvatar` prop is being added to the `AddressInput` component to allow a snap to indicate that it has validated the value of the component itself. Updated the send flow example snap as well, removed dead code that was missed previously.
1 parent dc26f07 commit 82ea67b

File tree

11 files changed

+42
-24
lines changed

11 files changed

+42
-24
lines changed

packages/examples/packages/browserify-plugin/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": "PNtdcFc5HFBGHnv3e/8CUhm16s6+Ksmmc38dF34lsSw=",
10+
"shasum": "1V2d/+iUgdA+QeXEVHKxmle1VvQJaVC6KaHqSevpWBQ=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/browserify/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": "1V+pdRmIER1rMRFWU02Fzc+LXLUcCoCHAXsbu2JXgSI=",
10+
"shasum": "/i4ajCNv/9R1NuYAfSTnq8j8czxSymcZhcjZgnhj3Pg=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
},
4545
"dependencies": {
4646
"@metamask/rpc-errors": "^7.0.2",
47-
"@metamask/snaps-sdk": "workspace:^"
47+
"@metamask/snaps-sdk": "workspace:^",
48+
"@metamask/superstruct": "^3.1.0",
49+
"@metamask/utils": "^11.2.0"
4850
},
4951
"devDependencies": {
5052
"@jest/globals": "^29.5.0",

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": "wE+wX6shyn8Q1Tu8SrUCOdSjARmQrq2jbCKiRqFz5EY=",
10+
"shasum": "5qERUafayyDWQ12V/+htfEs2wzmQwhSn0EcQolI7quo=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/send-flow/src/components/SendFlow.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ import type { Account, Currency } from '../types';
1515
* @property selectedCurrency - The selected currency to display.
1616
* @property total - The total cost of the transaction.
1717
* @property fees - The fees for the transaction.
18-
* @property flushToAddress - Whether to flush the address field or not.
1918
* @property errors - The form errors.
19+
* @property displayAvatar - Whether to display the avatar of the address.
2020
*/
2121
export type SendFlowProps = {
2222
accounts: Account[];
2323
selectedAccount: string;
2424
selectedCurrency: 'BTC' | '$';
2525
total: Currency;
2626
fees: Currency;
27-
flushToAddress?: boolean;
2827
errors?: {
2928
amount?: string;
3029
to?: string;
3130
};
31+
displayAvatar?: boolean | undefined;
3232
};
3333

3434
/**
@@ -41,7 +41,7 @@ export type SendFlowProps = {
4141
* @param props.total - The total cost of the transaction.
4242
* @param props.errors - The form errors.
4343
* @param props.fees - The fees for the transaction.
44-
* @param props.flushToAddress - Whether to flush the address field or not.
44+
* @param props.displayAvatar - Whether to display the avatar of the address.
4545
* @returns The SendFlow component.
4646
*/
4747
export const SendFlow: SnapComponent<SendFlowProps> = ({
@@ -50,8 +50,8 @@ export const SendFlow: SnapComponent<SendFlowProps> = ({
5050
selectedCurrency,
5151
total,
5252
fees,
53-
flushToAddress,
5453
errors,
54+
displayAvatar,
5555
}) => {
5656
return (
5757
<Container>
@@ -61,8 +61,8 @@ export const SendFlow: SnapComponent<SendFlowProps> = ({
6161
selectedAccount={selectedAccount}
6262
accounts={accounts}
6363
selectedCurrency={selectedCurrency}
64-
flushToAddress={flushToAddress}
6564
errors={errors}
65+
displayAvatar={displayAvatar}
6666
/>
6767
<TransactionSummary fees={fees} total={total} />
6868
</Box>

packages/examples/packages/send-flow/src/components/SendForm.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import type { Account, SendFormErrors } from '../types';
2222
* @property accounts - The available accounts.
2323
* @property errors - The form errors.
2424
* @property selectedCurrency - The selected currency to display.
25-
* @property flushToAddress - Whether to flush the address field or not.
25+
* @property displayAvatar - Whether to display the avatar of the address.
2626
*/
2727
export type SendFormProps = {
2828
selectedAccount: string;
2929
accounts: Account[];
3030
errors?: SendFormErrors;
3131
selectedCurrency: 'BTC' | '$';
32-
flushToAddress?: boolean;
32+
displayAvatar?: boolean | undefined;
3333
};
3434

3535
/**
@@ -40,15 +40,15 @@ export type SendFormProps = {
4040
* @param props.accounts - The available accounts.
4141
* @param props.errors - The form errors.
4242
* @param props.selectedCurrency - The selected currency to display.
43-
* @param props.flushToAddress - Whether to flush the address field or not.
43+
* @param props.displayAvatar - Whether to display the avatar of the address.
4444
* @returns The SendForm component.
4545
*/
4646
export const SendForm: SnapComponent<SendFormProps> = ({
4747
selectedAccount,
4848
accounts,
4949
errors,
5050
selectedCurrency,
51-
flushToAddress,
51+
displayAvatar,
5252
}) => (
5353
<Form name="sendForm">
5454
<AccountSelector selectedAccount={selectedAccount} accounts={accounts} />
@@ -69,7 +69,7 @@ export const SendForm: SnapComponent<SendFormProps> = ({
6969
name="to"
7070
chainId="eip155:0"
7171
placeholder="Enter receiving address"
72-
value={flushToAddress ? '' : undefined}
72+
displayAvatar={displayAvatar}
7373
/>
7474
</Field>
7575
</Form>

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type {
55
OnRpcRequestHandler,
66
} from '@metamask/snaps-sdk';
77
import { UserInputEventType } from '@metamask/snaps-sdk';
8+
import { is } from '@metamask/superstruct';
9+
import { HexChecksumAddressStruct } from '@metamask/utils';
810

911
import { SendFlow } from './components';
1012
import { accountsArray, accounts } from './data';
@@ -98,8 +100,8 @@ export const onUserInput: OnUserInputHandler = async ({
98100
if (event.type === UserInputEventType.InputChangeEvent) {
99101
switch (event.name) {
100102
case 'amount':
101-
case 'to':
102-
case 'accountSelector': {
103+
case 'to': {
104+
// For testing purposes, we display the avatar if the address is a valid hex checksum address.
103105
await snap.request({
104106
method: 'snap_updateInterface',
105107
params: {
@@ -112,19 +114,14 @@ export const onUserInput: OnUserInputHandler = async ({
112114
total={total}
113115
fees={fees}
114116
errors={formErrors}
117+
displayAvatar={is(event.value, HexChecksumAddressStruct)}
115118
/>
116119
),
117120
},
118121
});
119-
120122
break;
121123
}
122-
default:
123-
break;
124-
}
125-
} else if (event.type === UserInputEventType.ButtonClickEvent) {
126-
switch (event.name) {
127-
case 'clear':
124+
case 'accountSelector': {
128125
await snap.request({
129126
method: 'snap_updateInterface',
130127
params: {
@@ -136,13 +133,14 @@ export const onUserInput: OnUserInputHandler = async ({
136133
selectedCurrency={selectedCurrency}
137134
total={total}
138135
fees={fees}
139-
flushToAddress={true}
140136
errors={formErrors}
141137
/>
142138
),
143139
},
144140
});
141+
145142
break;
143+
}
146144
default:
147145
break;
148146
}

packages/snaps-sdk/src/jsx/components/form/AddressInput.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type AddressInputProps = {
88
chainId: CaipChainId;
99
placeholder?: string | undefined;
1010
disabled?: boolean | undefined;
11+
displayAvatar?: boolean | undefined;
1112
};
1213

1314
const TYPE = 'AddressInput';
@@ -21,6 +22,7 @@ const TYPE = 'AddressInput';
2122
* @param props.chainId - The CAIP-2 chain ID of the address.
2223
* @param props.placeholder - The placeholder text of the input field.
2324
* @param props.disabled - Whether the input field is disabled.
25+
* @param props.displayAvatar - Whether to display the avatar of the address.
2426
* @returns An input element.
2527
* @example
2628
* <AddressInput name="address" value="0x1234567890123456789012345678901234567890" chainId="eip155:1" />

packages/snaps-sdk/src/jsx/validation.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ describe('AddressInputStruct', () => {
221221
/>,
222222
<AddressInput name="address" chainId="eip155:1" placeholder="foo" />,
223223
<AddressInput name="address" chainId="eip155:1" disabled={true} />,
224+
<AddressInput
225+
name="address"
226+
chainId="eip155:1"
227+
displayAvatar={true}
228+
value="0x1234567890abcdef1234567890abcdef12345678"
229+
/>,
224230
])('validates an address input element', (value) => {
225231
expect(is(value, AddressInputStruct)).toBe(true);
226232
});
@@ -242,6 +248,13 @@ describe('AddressInputStruct', () => {
242248
<AddressInput name="foo" chainId="eip155:1" placeholder={32} />,
243249
// @ts-expect-error - Invalid props.
244250
<AddressInput name="foo" chainId="eip155:1" disabled="bar" />,
251+
<AddressInput
252+
name="foo"
253+
chainId="eip155:1"
254+
// @ts-expect-error - Invalid props.
255+
displayAvatar={123}
256+
value="0x1234567890abcdef1234567890abcdef12345678"
257+
/>,
245258
])('does not validate "%p"', (value) => {
246259
expect(is(value, AddressInputStruct)).toBe(false);
247260
});

packages/snaps-sdk/src/jsx/validation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ export const AddressInputStruct: Describe<AddressInputElement> = element(
358358
value: optional(string()),
359359
placeholder: optional(string()),
360360
disabled: optional(boolean()),
361+
displayAvatar: optional(boolean()),
361362
},
362363
);
363364

0 commit comments

Comments
 (0)