Skip to content
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
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": "zIDqJzXMi7kdr4+TOIKO1lOK4o3r60fEDRMO4Q6lXzk=",
"shasum": "2WLcpc3RXg2Up0zeSMxWS/2rsVI1bC1iHCHAhzc3DOA=",
"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": "coTp9c/ZNMaIGqnTbDMfSpt867Gyq6kQCbmgsZzTARg=",
"shasum": "LQYiFCEu2fQXCuzfYHVpFfqffCbzzUjaqH7kPwgBfNc=",
"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": "9/KE/yjYAfOTNAgd0D0SYVY8JBFf+NQuiEAQFw0NOC8=",
"shasum": "cvn98L15eJyKHx8ILZMXdWnldchVezqFyEEBPbzPphA=",
"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": "fCeqfbW1isFwXBPuM3RaAEtcR3J0Lv31fQ57rFNRwfs=",
"shasum": "3EvoIyaDBktcyCt1ctHZuF8NyLWcvwudTHutInzJLh8=",
"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": "V+BzJMukP8qstaXg0Wad4UP9+TEASCC3BI1aWh1x88I=",
"shasum": "tiSVGL8yltFcza2kKeinRFy78EpJjAOmNq7VdjUuZCA=",
"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": "lkNwxRYoirSS7KXzcQq+yRWJuQybHll6JMFFwCiNzJM=",
"shasum": "3BZDz8Bb5rS3LZtzyJzWf256FQyPUpau6fSJrdnb6z4=",
"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": "Of48MD2Ff6x699xG59ZcPr/sUJ3Eh2/L5jGI5m/oG0I=",
"shasum": "z7oshK7wm/kA4Oqm4je3l4jzoceecqc6YKLvzKqhD/g=",
"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": "JtC90PiqLl5E6zsaCbNijJztIbFuRTrAb/iFZqVWn58=",
"shasum": "ksDM4Au4/toUxZEMdG1827cC9nJo/ZOHUGOA/kmQF/Q=",
"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": "yPO8YJN8Obq9TqQ3yZaPjurJPu+f2Ohlwmup0Rrwn9o=",
"shasum": "7bsB6l4qPdrB12apSpFqD0i4DNZ65OOrgvyqu0dj0Mc=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
36 changes: 24 additions & 12 deletions packages/snaps-sdk/src/internals/structs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import type { AnyStruct, Infer, InferStructTuple } from '@metamask/superstruct';
import {
Struct,
define,
is,
literal as superstructLiteral,
union as superstructUnion,
} from '@metamask/superstruct';
import type { PlainObject } from '@metamask/utils';
import { hasProperty, isPlainObject } from '@metamask/utils';
import { hasProperty, isObject } from '@metamask/utils';

import type { EnumToUnion } from './helpers';

Expand Down Expand Up @@ -99,16 +98,27 @@ export function typedUnion<Head extends AnyStruct, Tail extends AnyStruct[]>(
)
.flat(Infinity);
const types = flatStructs.map(({ schema }) => schema.type.type);
const structMap = flatStructs.reduce<Record<string, Struct>>(
(accumulator, struct) => {
accumulator[JSON.parse(struct.schema.type.type)] = struct;
return accumulator;
},
{},
);
return new Struct({
type: 'union',
schema: flatStructs,
*entries(value, context) {
if (!isPlainObject(value) || !hasProperty(value, 'type')) {
if (
!isObject(value) ||
!hasProperty(value, 'type') ||
typeof value.type !== 'string'
) {
return;
}

const { type } = value;
const struct = flatStructs.find(({ schema }) => is(type, schema.type));
const struct = structMap[type];

if (!struct) {
return;
Expand All @@ -119,29 +129,31 @@ export function typedUnion<Head extends AnyStruct, Tail extends AnyStruct[]>(
}
},
coercer(value, context) {
if (!isPlainObject(value) || !hasProperty(value, 'type')) {
if (
!isObject(value) ||
!hasProperty(value, 'type') ||
typeof value.type !== 'string'
) {
return value;
}

const { type } = value;
const struct = flatStructs.find(({ schema }) => is(type, schema.type));
const struct = structMap[type];
if (struct) {
return struct.coercer(value, context);
}

return value;
},
// At this point we know the value to be an object.
*refiner(value: PlainObject, context) {
const struct = flatStructs.find(({ schema }) =>
is(value.type, schema.type),
);
*refiner(value: PlainObject & { type: string }, context) {
const struct = structMap[value.type];

yield* struct.refiner(value, context);
},
validator(value, context) {
if (
!isPlainObject(value) ||
!isObject(value) ||
!hasProperty(value, 'type') ||
typeof value.type !== 'string'
) {
Expand All @@ -152,7 +164,7 @@ export function typedUnion<Head extends AnyStruct, Tail extends AnyStruct[]>(

const { type } = value;

const struct = flatStructs.find(({ schema }) => is(type, schema.type));
const struct = structMap[type];

if (struct) {
// This only validates the root of the struct, entries does the rest of the work.
Expand Down
6 changes: 2 additions & 4 deletions packages/snaps-sdk/src/jsx/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,14 @@ function children<Head extends AnyStruct, Tail extends AnyStruct[]>(
Nestable<Infer<Head> | InferStructTuple<Tail>[number] | boolean | null>,
null
> {
const potentialUnion = structs.length === 1 ? structs[0] : nullUnion(structs);
return nestable(
nullable(
selectiveUnion((value) => {
if (typeof value === 'boolean') {
return boolean();
}
if (structs.length === 1) {
return structs[0];
}
return nullUnion(structs);
return potentialUnion;
}),
),
) as unknown as Struct<
Expand Down
3 changes: 2 additions & 1 deletion packages/snaps-sdk/src/ui/components/address.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Infer } from '@metamask/superstruct';
import { assign, literal, object } from '@metamask/superstruct';
import { assign, object } from '@metamask/superstruct';
import { HexChecksumAddressStruct } from '@metamask/utils';

import { literal } from '../../internals';
import { createBuilder } from '../builder';
import { LiteralStruct, NodeType } from '../nodes';

Expand Down
11 changes: 2 additions & 9 deletions packages/snaps-sdk/src/ui/components/button.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import type { Infer } from '@metamask/superstruct';
import {
assign,
literal,
object,
optional,
string,
union,
} from '@metamask/superstruct';
import { assign, object, optional, string, union } from '@metamask/superstruct';

import { enumValue } from '../../internals';
import { enumValue, literal } from '../../internals';
import { createBuilder } from '../builder';
import { LiteralStruct, NodeType } from '../nodes';

Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-sdk/src/ui/components/copyable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import type { Infer } from '@metamask/superstruct';
import {
assign,
boolean,
literal,
object,
optional,
string,
} from '@metamask/superstruct';

import { literal } from '../../internals';
import { createBuilder } from '../builder';
import { LiteralStruct, NodeType } from '../nodes';

Expand Down
3 changes: 2 additions & 1 deletion packages/snaps-sdk/src/ui/components/divider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Infer } from '@metamask/superstruct';
import { assign, literal, object } from '@metamask/superstruct';
import { assign, object } from '@metamask/superstruct';

import { literal } from '../../internals';
import { createBuilder } from '../builder';
import { NodeStruct, NodeType } from '../nodes';

Expand Down
10 changes: 2 additions & 8 deletions packages/snaps-sdk/src/ui/components/form.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import type { Infer } from '@metamask/superstruct';
import {
array,
assign,
literal,
object,
string,
union,
} from '@metamask/superstruct';
import { array, assign, object, string, union } from '@metamask/superstruct';

import { ButtonStruct } from './button';
import { InputStruct } from './input';
import { literal } from '../../internals';
import { createBuilder } from '../builder';
import { NodeStruct, NodeType } from '../nodes';

Expand Down
3 changes: 2 additions & 1 deletion packages/snaps-sdk/src/ui/components/heading.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Infer } from '@metamask/superstruct';
import { assign, literal, object, string } from '@metamask/superstruct';
import { assign, object, string } from '@metamask/superstruct';

import { literal } from '../../internals';
import { createBuilder } from '../builder';
import { LiteralStruct, NodeType } from '../nodes';

Expand Down
4 changes: 2 additions & 2 deletions packages/snaps-sdk/src/ui/components/image.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Infer } from '@metamask/superstruct';
import { assign, literal, object } from '@metamask/superstruct';
import { assign, object } from '@metamask/superstruct';

import { svg } from '../../internals';
import { svg, literal } from '../../internals';
import { createBuilder } from '../builder';
import { NodeStruct, NodeType } from '../nodes';

Expand Down
11 changes: 2 additions & 9 deletions packages/snaps-sdk/src/ui/components/input.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import type { Infer } from '@metamask/superstruct';
import {
assign,
literal,
object,
optional,
string,
union,
} from '@metamask/superstruct';
import { assign, object, optional, string, union } from '@metamask/superstruct';

import { enumValue } from '../../internals';
import { enumValue, literal } from '../../internals';
import { createBuilder } from '../builder';
import { LiteralStruct, NodeType } from '../nodes';

Expand Down
4 changes: 2 additions & 2 deletions packages/snaps-sdk/src/ui/components/panel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Infer, Struct } from '@metamask/superstruct';
import { array, assign, lazy, literal, object } from '@metamask/superstruct';
import { array, assign, lazy, object } from '@metamask/superstruct';

import { AddressStruct } from './address';
import { ButtonStruct } from './button';
Expand All @@ -12,7 +12,7 @@ import { InputStruct } from './input';
import { RowStruct } from './row';
import { SpinnerStruct } from './spinner';
import { TextStruct } from './text';
import { typedUnion } from '../../internals';
import { typedUnion, literal } from '../../internals';
import { createBuilder } from '../builder';
import { NodeStruct, NodeType } from '../nodes';

Expand Down
11 changes: 2 additions & 9 deletions packages/snaps-sdk/src/ui/components/row.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import type { Infer } from '@metamask/superstruct';
import {
assign,
literal,
object,
string,
optional,
union,
} from '@metamask/superstruct';
import { assign, object, string, optional, union } from '@metamask/superstruct';

import { AddressStruct } from './address';
import { ImageStruct } from './image';
import { TextStruct } from './text';
import { enumValue } from '../../internals';
import { enumValue, literal } from '../../internals';
import { createBuilder } from '../builder';
import { LiteralStruct, NodeType } from '../nodes';

Expand Down
3 changes: 2 additions & 1 deletion packages/snaps-sdk/src/ui/components/spinner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Infer } from '@metamask/superstruct';
import { assign, literal, object } from '@metamask/superstruct';
import { assign, object } from '@metamask/superstruct';

import { literal } from '../../internals';
import { createBuilder } from '../builder';
import { NodeStruct, NodeType } from '../nodes';

Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-sdk/src/ui/components/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import type { Infer } from '@metamask/superstruct';
import {
assign,
boolean,
literal,
object,
optional,
string,
} from '@metamask/superstruct';

import { literal } from '../../internals';
import { createBuilder } from '../builder';
import { LiteralStruct, NodeType } from '../nodes';

Expand Down
Loading