@metamask/snaps-rpc-methods 11.13.1 → 12.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -1
- package/dist/endowments/index.cjs +1 -0
- package/dist/endowments/index.cjs.map +1 -1
- package/dist/endowments/index.d.cts.map +1 -1
- package/dist/endowments/index.d.mts.map +1 -1
- package/dist/endowments/index.mjs +1 -0
- package/dist/endowments/index.mjs.map +1 -1
- package/dist/permitted/createInterface.d.cts +44 -44
- package/dist/permitted/createInterface.d.mts +44 -44
- package/dist/permitted/experimentalProviderRequest.d.cts +3 -3
- package/dist/permitted/experimentalProviderRequest.d.mts +3 -3
- package/dist/permitted/getFile.d.cts +2 -2
- package/dist/permitted/getFile.d.mts +2 -2
- package/dist/permitted/getState.d.cts +1 -1
- package/dist/permitted/getState.d.mts +1 -1
- package/dist/permitted/handlers.d.cts +16 -0
- package/dist/permitted/handlers.d.cts.map +1 -1
- package/dist/permitted/handlers.d.mts +16 -0
- package/dist/permitted/handlers.d.mts.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.cjs +2 -19
- package/dist/permitted/scheduleBackgroundEvent.cjs.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.d.cts +4 -4
- package/dist/permitted/scheduleBackgroundEvent.d.cts.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.d.mts +4 -4
- package/dist/permitted/scheduleBackgroundEvent.d.mts.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.mjs +4 -21
- package/dist/permitted/scheduleBackgroundEvent.mjs.map +1 -1
- package/dist/permitted/setState.d.cts +1 -1
- package/dist/permitted/setState.d.mts +1 -1
- package/dist/permitted/updateInterface.d.cts +44 -44
- package/dist/permitted/updateInterface.d.mts +44 -44
- package/dist/restricted/dialog.d.cts +91 -147
- package/dist/restricted/dialog.d.cts.map +1 -1
- package/dist/restricted/dialog.d.mts +91 -147
- package/dist/restricted/dialog.d.mts.map +1 -1
- package/dist/restricted/getBip32Entropy.cjs +16 -3
- package/dist/restricted/getBip32Entropy.cjs.map +1 -1
- package/dist/restricted/getBip32Entropy.d.cts +11 -1
- package/dist/restricted/getBip32Entropy.d.cts.map +1 -1
- package/dist/restricted/getBip32Entropy.d.mts +11 -1
- package/dist/restricted/getBip32Entropy.d.mts.map +1 -1
- package/dist/restricted/getBip32Entropy.mjs +17 -4
- package/dist/restricted/getBip32Entropy.mjs.map +1 -1
- package/dist/restricted/getBip32PublicKey.cjs +19 -3
- package/dist/restricted/getBip32PublicKey.cjs.map +1 -1
- package/dist/restricted/getBip32PublicKey.d.cts +13 -3
- package/dist/restricted/getBip32PublicKey.d.cts.map +1 -1
- package/dist/restricted/getBip32PublicKey.d.mts +13 -3
- package/dist/restricted/getBip32PublicKey.d.mts.map +1 -1
- package/dist/restricted/getBip32PublicKey.mjs +20 -4
- package/dist/restricted/getBip32PublicKey.mjs.map +1 -1
- package/dist/restricted/getBip44Entropy.cjs +8 -5
- package/dist/restricted/getBip44Entropy.cjs.map +1 -1
- package/dist/restricted/getBip44Entropy.d.cts +6 -6
- package/dist/restricted/getBip44Entropy.d.cts.map +1 -1
- package/dist/restricted/getBip44Entropy.d.mts +6 -6
- package/dist/restricted/getBip44Entropy.d.mts.map +1 -1
- package/dist/restricted/getBip44Entropy.mjs +9 -6
- package/dist/restricted/getBip44Entropy.mjs.map +1 -1
- package/dist/restricted/getEntropy.cjs +7 -7
- package/dist/restricted/getEntropy.cjs.map +1 -1
- package/dist/restricted/getEntropy.d.cts +8 -8
- package/dist/restricted/getEntropy.d.cts.map +1 -1
- package/dist/restricted/getEntropy.d.mts +8 -8
- package/dist/restricted/getEntropy.d.mts.map +1 -1
- package/dist/restricted/getEntropy.mjs +8 -8
- package/dist/restricted/getEntropy.mjs.map +1 -1
- package/dist/restricted/manageAccounts.d.cts +1 -1
- package/dist/restricted/manageAccounts.d.mts +1 -1
- package/dist/restricted/manageState.cjs +4 -4
- package/dist/restricted/manageState.cjs.map +1 -1
- package/dist/restricted/manageState.d.cts +3 -3
- package/dist/restricted/manageState.d.cts.map +1 -1
- package/dist/restricted/manageState.d.mts +3 -3
- package/dist/restricted/manageState.d.mts.map +1 -1
- package/dist/restricted/manageState.mjs +5 -5
- package/dist/restricted/manageState.mjs.map +1 -1
- package/dist/restricted/notify.d.cts +74 -74
- package/dist/restricted/notify.d.mts +74 -74
- package/dist/utils.cjs +68 -23
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +40 -16
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +40 -16
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +63 -19
- package/dist/utils.mjs.map +1 -1
- package/package.json +7 -7
|
@@ -7,80 +7,81 @@ import type { NonEmptyArray } from "@metamask/utils";
|
|
|
7
7
|
import { type MethodHooksObject } from "../utils.mjs";
|
|
8
8
|
declare const methodName = "snap_notify";
|
|
9
9
|
declare const NotificationParametersStruct: import("@metamask/superstruct").Struct<{
|
|
10
|
-
type: "native";
|
|
11
10
|
message: string;
|
|
11
|
+
type: "native";
|
|
12
12
|
} | {
|
|
13
|
-
type: "inApp";
|
|
14
13
|
message: string;
|
|
15
|
-
} | {
|
|
16
14
|
type: "inApp";
|
|
15
|
+
} | {
|
|
17
16
|
message: string;
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
type: "inApp";
|
|
18
|
+
title: string;
|
|
19
|
+
content: import("@metamask/snaps-sdk").Panel | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AssetSelectorProps, "AssetSelector"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AddressInputProps, "AddressInput"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ButtonProps, "Button"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CheckboxProps, "Checkbox"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FormProps, "Form"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FieldProps, "Field"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FileInputProps, "FileInput"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").InputProps, "Input"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").DropdownProps, "Dropdown"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SelectorProps, "Selector"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BoldProps, "Bold"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ItalicProps, "Italic"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AvatarProps, "Avatar"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AddressProps, "Address"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BoxProps, "Box"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CardProps, "Card"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ContainerProps, "Container"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CopyableProps, "Copyable"> | import("@metamask/snaps-sdk/jsx").SnapElement<Record<string, never>, "Divider"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").IconProps, "Icon"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").LinkProps, "Link"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").RowProps, "Row"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SectionProps, "Section"> | import("@metamask/snaps-sdk/jsx").SnapElement<Record<string, never>, "Spinner"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").TextProps, "Text"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").TooltipProps, "Tooltip"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BannerProps, "Banner"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SkeletonProps, "Skeleton"> | {
|
|
20
20
|
value: string;
|
|
21
|
+
type: import("@metamask/snaps-sdk").NodeType.Copyable;
|
|
21
22
|
sensitive?: boolean | undefined;
|
|
22
23
|
} | {
|
|
23
24
|
type: import("@metamask/snaps-sdk").NodeType.Divider;
|
|
24
25
|
} | {
|
|
25
|
-
type: import("@metamask/snaps-sdk").NodeType.Heading;
|
|
26
26
|
value: string;
|
|
27
|
+
type: import("@metamask/snaps-sdk").NodeType.Heading;
|
|
27
28
|
} | {
|
|
28
|
-
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
29
29
|
value: string;
|
|
30
|
+
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
30
31
|
} | {
|
|
31
32
|
type: import("@metamask/snaps-sdk").NodeType.Spinner;
|
|
32
33
|
} | {
|
|
33
|
-
type: import("@metamask/snaps-sdk").NodeType.Text;
|
|
34
34
|
value: string;
|
|
35
|
+
type: import("@metamask/snaps-sdk").NodeType.Text;
|
|
35
36
|
markdown?: boolean | undefined;
|
|
36
37
|
} | {
|
|
37
|
-
type: import("@metamask/snaps-sdk").NodeType.Address;
|
|
38
38
|
value: `0x${string}`;
|
|
39
|
+
type: import("@metamask/snaps-sdk").NodeType.Address;
|
|
39
40
|
} | {
|
|
40
|
-
type: import("@metamask/snaps-sdk").NodeType.Row;
|
|
41
41
|
value: {
|
|
42
|
-
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
43
42
|
value: string;
|
|
43
|
+
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
44
44
|
} | {
|
|
45
|
-
type: import("@metamask/snaps-sdk").NodeType.Text;
|
|
46
45
|
value: string;
|
|
46
|
+
type: import("@metamask/snaps-sdk").NodeType.Text;
|
|
47
47
|
markdown?: boolean | undefined;
|
|
48
48
|
} | {
|
|
49
|
-
type: import("@metamask/snaps-sdk").NodeType.Address;
|
|
50
49
|
value: `0x${string}`;
|
|
50
|
+
type: import("@metamask/snaps-sdk").NodeType.Address;
|
|
51
51
|
};
|
|
52
|
+
type: import("@metamask/snaps-sdk").NodeType.Row;
|
|
52
53
|
label: string;
|
|
53
|
-
variant?: "default" | "
|
|
54
|
+
variant?: "default" | "critical" | "warning" | undefined;
|
|
54
55
|
} | {
|
|
55
|
-
type: import("@metamask/snaps-sdk").NodeType.Input;
|
|
56
56
|
name: string;
|
|
57
|
-
|
|
58
|
-
inputType?: "number" | "text" | "password" | undefined;
|
|
57
|
+
type: import("@metamask/snaps-sdk").NodeType.Input;
|
|
59
58
|
value?: string | undefined;
|
|
60
59
|
label?: string | undefined;
|
|
60
|
+
inputType?: "number" | "text" | "password" | undefined;
|
|
61
61
|
placeholder?: string | undefined;
|
|
62
|
+
error?: string | undefined;
|
|
62
63
|
} | {
|
|
63
|
-
type: import("@metamask/snaps-sdk").NodeType.Button;
|
|
64
64
|
value: string;
|
|
65
|
-
|
|
65
|
+
type: import("@metamask/snaps-sdk").NodeType.Button;
|
|
66
66
|
name?: string | undefined;
|
|
67
|
+
variant?: "primary" | "secondary" | undefined;
|
|
67
68
|
buttonType?: "button" | "submit" | undefined;
|
|
68
69
|
} | {
|
|
69
|
-
type: import("@metamask/snaps-sdk").NodeType.Form;
|
|
70
70
|
name: string;
|
|
71
|
+
type: import("@metamask/snaps-sdk").NodeType.Form;
|
|
71
72
|
children: ({
|
|
72
|
-
type: import("@metamask/snaps-sdk").NodeType.Input;
|
|
73
73
|
name: string;
|
|
74
|
-
|
|
75
|
-
inputType?: "number" | "text" | "password" | undefined;
|
|
74
|
+
type: import("@metamask/snaps-sdk").NodeType.Input;
|
|
76
75
|
value?: string | undefined;
|
|
77
76
|
label?: string | undefined;
|
|
77
|
+
inputType?: "number" | "text" | "password" | undefined;
|
|
78
78
|
placeholder?: string | undefined;
|
|
79
|
+
error?: string | undefined;
|
|
79
80
|
} | {
|
|
80
|
-
type: import("@metamask/snaps-sdk").NodeType.Button;
|
|
81
81
|
value: string;
|
|
82
|
-
|
|
82
|
+
type: import("@metamask/snaps-sdk").NodeType.Button;
|
|
83
83
|
name?: string | undefined;
|
|
84
|
+
variant?: "primary" | "secondary" | undefined;
|
|
84
85
|
buttonType?: "button" | "submit" | undefined;
|
|
85
86
|
})[];
|
|
86
87
|
} | import("@metamask/snaps-sdk/jsx").SnapElement<{
|
|
@@ -100,86 +101,86 @@ declare const NotificationParametersStruct: import("@metamask/superstruct").Stru
|
|
|
100
101
|
children: import("@metamask/snaps-sdk/jsx").StringElement;
|
|
101
102
|
size?: "sm" | "md" | "lg" | undefined;
|
|
102
103
|
}, "Heading">;
|
|
103
|
-
title: string;
|
|
104
104
|
footerLink?: {
|
|
105
105
|
text: string;
|
|
106
106
|
href: string;
|
|
107
107
|
} | undefined;
|
|
108
108
|
}, [head: import("@metamask/superstruct").Struct<{
|
|
109
|
-
type: "inApp";
|
|
110
109
|
message: string;
|
|
110
|
+
type: "inApp";
|
|
111
111
|
}, {
|
|
112
112
|
type: import("@metamask/superstruct").Struct<"inApp", null>;
|
|
113
113
|
message: import("@metamask/superstruct").Struct<string, null>;
|
|
114
114
|
}>, import("@metamask/superstruct").Struct<{
|
|
115
|
-
type: "inApp";
|
|
116
115
|
message: string;
|
|
117
|
-
|
|
118
|
-
|
|
116
|
+
type: "inApp";
|
|
117
|
+
title: string;
|
|
118
|
+
content: import("@metamask/snaps-sdk").Panel | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AssetSelectorProps, "AssetSelector"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AddressInputProps, "AddressInput"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ButtonProps, "Button"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CheckboxProps, "Checkbox"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FormProps, "Form"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FieldProps, "Field"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FileInputProps, "FileInput"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").InputProps, "Input"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").DropdownProps, "Dropdown"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SelectorProps, "Selector"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BoldProps, "Bold"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ItalicProps, "Italic"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AvatarProps, "Avatar"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AddressProps, "Address"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BoxProps, "Box"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CardProps, "Card"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ContainerProps, "Container"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CopyableProps, "Copyable"> | import("@metamask/snaps-sdk/jsx").SnapElement<Record<string, never>, "Divider"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").IconProps, "Icon"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").LinkProps, "Link"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").RowProps, "Row"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SectionProps, "Section"> | import("@metamask/snaps-sdk/jsx").SnapElement<Record<string, never>, "Spinner"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").TextProps, "Text"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").TooltipProps, "Tooltip"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BannerProps, "Banner"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SkeletonProps, "Skeleton"> | {
|
|
119
119
|
value: string;
|
|
120
|
+
type: import("@metamask/snaps-sdk").NodeType.Copyable;
|
|
120
121
|
sensitive?: boolean | undefined;
|
|
121
122
|
} | {
|
|
122
123
|
type: import("@metamask/snaps-sdk").NodeType.Divider;
|
|
123
124
|
} | {
|
|
124
|
-
type: import("@metamask/snaps-sdk").NodeType.Heading;
|
|
125
125
|
value: string;
|
|
126
|
+
type: import("@metamask/snaps-sdk").NodeType.Heading;
|
|
126
127
|
} | {
|
|
127
|
-
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
128
128
|
value: string;
|
|
129
|
+
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
129
130
|
} | {
|
|
130
131
|
type: import("@metamask/snaps-sdk").NodeType.Spinner;
|
|
131
132
|
} | {
|
|
132
|
-
type: import("@metamask/snaps-sdk").NodeType.Text;
|
|
133
133
|
value: string;
|
|
134
|
+
type: import("@metamask/snaps-sdk").NodeType.Text;
|
|
134
135
|
markdown?: boolean | undefined;
|
|
135
136
|
} | {
|
|
136
|
-
type: import("@metamask/snaps-sdk").NodeType.Address;
|
|
137
137
|
value: `0x${string}`;
|
|
138
|
+
type: import("@metamask/snaps-sdk").NodeType.Address;
|
|
138
139
|
} | {
|
|
139
|
-
type: import("@metamask/snaps-sdk").NodeType.Row;
|
|
140
140
|
value: {
|
|
141
|
-
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
142
141
|
value: string;
|
|
142
|
+
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
143
143
|
} | {
|
|
144
|
-
type: import("@metamask/snaps-sdk").NodeType.Text;
|
|
145
144
|
value: string;
|
|
145
|
+
type: import("@metamask/snaps-sdk").NodeType.Text;
|
|
146
146
|
markdown?: boolean | undefined;
|
|
147
147
|
} | {
|
|
148
|
-
type: import("@metamask/snaps-sdk").NodeType.Address;
|
|
149
148
|
value: `0x${string}`;
|
|
149
|
+
type: import("@metamask/snaps-sdk").NodeType.Address;
|
|
150
150
|
};
|
|
151
|
+
type: import("@metamask/snaps-sdk").NodeType.Row;
|
|
151
152
|
label: string;
|
|
152
|
-
variant?: "default" | "
|
|
153
|
+
variant?: "default" | "critical" | "warning" | undefined;
|
|
153
154
|
} | {
|
|
154
|
-
type: import("@metamask/snaps-sdk").NodeType.Input;
|
|
155
155
|
name: string;
|
|
156
|
-
|
|
157
|
-
inputType?: "number" | "text" | "password" | undefined;
|
|
156
|
+
type: import("@metamask/snaps-sdk").NodeType.Input;
|
|
158
157
|
value?: string | undefined;
|
|
159
158
|
label?: string | undefined;
|
|
159
|
+
inputType?: "number" | "text" | "password" | undefined;
|
|
160
160
|
placeholder?: string | undefined;
|
|
161
|
+
error?: string | undefined;
|
|
161
162
|
} | {
|
|
162
|
-
type: import("@metamask/snaps-sdk").NodeType.Button;
|
|
163
163
|
value: string;
|
|
164
|
-
|
|
164
|
+
type: import("@metamask/snaps-sdk").NodeType.Button;
|
|
165
165
|
name?: string | undefined;
|
|
166
|
+
variant?: "primary" | "secondary" | undefined;
|
|
166
167
|
buttonType?: "button" | "submit" | undefined;
|
|
167
168
|
} | {
|
|
168
|
-
type: import("@metamask/snaps-sdk").NodeType.Form;
|
|
169
169
|
name: string;
|
|
170
|
+
type: import("@metamask/snaps-sdk").NodeType.Form;
|
|
170
171
|
children: ({
|
|
171
|
-
type: import("@metamask/snaps-sdk").NodeType.Input;
|
|
172
172
|
name: string;
|
|
173
|
-
|
|
174
|
-
inputType?: "number" | "text" | "password" | undefined;
|
|
173
|
+
type: import("@metamask/snaps-sdk").NodeType.Input;
|
|
175
174
|
value?: string | undefined;
|
|
176
175
|
label?: string | undefined;
|
|
176
|
+
inputType?: "number" | "text" | "password" | undefined;
|
|
177
177
|
placeholder?: string | undefined;
|
|
178
|
+
error?: string | undefined;
|
|
178
179
|
} | {
|
|
179
|
-
type: import("@metamask/snaps-sdk").NodeType.Button;
|
|
180
180
|
value: string;
|
|
181
|
-
|
|
181
|
+
type: import("@metamask/snaps-sdk").NodeType.Button;
|
|
182
182
|
name?: string | undefined;
|
|
183
|
+
variant?: "primary" | "secondary" | undefined;
|
|
183
184
|
buttonType?: "button" | "submit" | undefined;
|
|
184
185
|
})[];
|
|
185
186
|
} | import("@metamask/snaps-sdk/jsx").SnapElement<{
|
|
@@ -199,7 +200,6 @@ declare const NotificationParametersStruct: import("@metamask/superstruct").Stru
|
|
|
199
200
|
children: import("@metamask/snaps-sdk/jsx").StringElement;
|
|
200
201
|
size?: "sm" | "md" | "lg" | undefined;
|
|
201
202
|
}, "Heading">;
|
|
202
|
-
title: string;
|
|
203
203
|
footerLink?: {
|
|
204
204
|
text: string;
|
|
205
205
|
href: string;
|
|
@@ -207,72 +207,72 @@ declare const NotificationParametersStruct: import("@metamask/superstruct").Stru
|
|
|
207
207
|
}, {
|
|
208
208
|
type: import("@metamask/superstruct").Struct<"inApp", null>;
|
|
209
209
|
message: import("@metamask/superstruct").Struct<string, null>;
|
|
210
|
-
content: import("@metamask/superstruct").Struct<import("@metamask/snaps-sdk").Panel | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ButtonProps, "Button"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CheckboxProps, "Checkbox"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FormProps, "Form"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FieldProps, "Field"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FileInputProps, "FileInput"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").InputProps, "Input"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").DropdownProps, "Dropdown"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SelectorProps, "Selector"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BoldProps, "Bold"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ItalicProps, "Italic"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AvatarProps, "Avatar"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AddressProps, "Address"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BoxProps, "Box"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CardProps, "Card"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ContainerProps, "Container"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CopyableProps, "Copyable"> | import("@metamask/snaps-sdk/jsx").SnapElement<Record<string, never>, "Divider"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").IconProps, "Icon"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").LinkProps, "Link"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").RowProps, "Row"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SectionProps, "Section"> | import("@metamask/snaps-sdk/jsx").SnapElement<Record<string, never>, "Spinner"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").TextProps, "Text"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").TooltipProps, "Tooltip"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BannerProps, "Banner"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SkeletonProps, "Skeleton"> | {
|
|
211
|
-
type: import("@metamask/snaps-sdk").NodeType.Copyable;
|
|
210
|
+
content: import("@metamask/superstruct").Struct<import("@metamask/snaps-sdk").Panel | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AssetSelectorProps, "AssetSelector"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AddressInputProps, "AddressInput"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ButtonProps, "Button"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CheckboxProps, "Checkbox"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FormProps, "Form"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FieldProps, "Field"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").FileInputProps, "FileInput"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").InputProps, "Input"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").DropdownProps, "Dropdown"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SelectorProps, "Selector"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BoldProps, "Bold"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ItalicProps, "Italic"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AvatarProps, "Avatar"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").AddressProps, "Address"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BoxProps, "Box"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CardProps, "Card"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").ContainerProps, "Container"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").CopyableProps, "Copyable"> | import("@metamask/snaps-sdk/jsx").SnapElement<Record<string, never>, "Divider"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").IconProps, "Icon"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").LinkProps, "Link"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").RowProps, "Row"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SectionProps, "Section"> | import("@metamask/snaps-sdk/jsx").SnapElement<Record<string, never>, "Spinner"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").TextProps, "Text"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").TooltipProps, "Tooltip"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").BannerProps, "Banner"> | import("@metamask/snaps-sdk/jsx").SnapElement<import("@metamask/snaps-sdk/jsx").SkeletonProps, "Skeleton"> | {
|
|
212
211
|
value: string;
|
|
212
|
+
type: import("@metamask/snaps-sdk").NodeType.Copyable;
|
|
213
213
|
sensitive?: boolean | undefined;
|
|
214
214
|
} | {
|
|
215
215
|
type: import("@metamask/snaps-sdk").NodeType.Divider;
|
|
216
216
|
} | {
|
|
217
|
-
type: import("@metamask/snaps-sdk").NodeType.Heading;
|
|
218
217
|
value: string;
|
|
218
|
+
type: import("@metamask/snaps-sdk").NodeType.Heading;
|
|
219
219
|
} | {
|
|
220
|
-
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
221
220
|
value: string;
|
|
221
|
+
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
222
222
|
} | {
|
|
223
223
|
type: import("@metamask/snaps-sdk").NodeType.Spinner;
|
|
224
224
|
} | {
|
|
225
|
-
type: import("@metamask/snaps-sdk").NodeType.Text;
|
|
226
225
|
value: string;
|
|
226
|
+
type: import("@metamask/snaps-sdk").NodeType.Text;
|
|
227
227
|
markdown?: boolean | undefined;
|
|
228
228
|
} | {
|
|
229
|
-
type: import("@metamask/snaps-sdk").NodeType.Address;
|
|
230
229
|
value: `0x${string}`;
|
|
230
|
+
type: import("@metamask/snaps-sdk").NodeType.Address;
|
|
231
231
|
} | {
|
|
232
|
-
type: import("@metamask/snaps-sdk").NodeType.Row;
|
|
233
232
|
value: {
|
|
234
|
-
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
235
233
|
value: string;
|
|
234
|
+
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
236
235
|
} | {
|
|
237
|
-
type: import("@metamask/snaps-sdk").NodeType.Text;
|
|
238
236
|
value: string;
|
|
237
|
+
type: import("@metamask/snaps-sdk").NodeType.Text;
|
|
239
238
|
markdown?: boolean | undefined;
|
|
240
239
|
} | {
|
|
241
|
-
type: import("@metamask/snaps-sdk").NodeType.Address;
|
|
242
240
|
value: `0x${string}`;
|
|
241
|
+
type: import("@metamask/snaps-sdk").NodeType.Address;
|
|
243
242
|
};
|
|
243
|
+
type: import("@metamask/snaps-sdk").NodeType.Row;
|
|
244
244
|
label: string;
|
|
245
|
-
variant?: "default" | "
|
|
245
|
+
variant?: "default" | "critical" | "warning" | undefined;
|
|
246
246
|
} | {
|
|
247
|
-
type: import("@metamask/snaps-sdk").NodeType.Input;
|
|
248
247
|
name: string;
|
|
249
|
-
|
|
250
|
-
inputType?: "number" | "text" | "password" | undefined;
|
|
248
|
+
type: import("@metamask/snaps-sdk").NodeType.Input;
|
|
251
249
|
value?: string | undefined;
|
|
252
250
|
label?: string | undefined;
|
|
251
|
+
inputType?: "number" | "text" | "password" | undefined;
|
|
253
252
|
placeholder?: string | undefined;
|
|
253
|
+
error?: string | undefined;
|
|
254
254
|
} | {
|
|
255
|
-
type: import("@metamask/snaps-sdk").NodeType.Button;
|
|
256
255
|
value: string;
|
|
257
|
-
|
|
256
|
+
type: import("@metamask/snaps-sdk").NodeType.Button;
|
|
258
257
|
name?: string | undefined;
|
|
258
|
+
variant?: "primary" | "secondary" | undefined;
|
|
259
259
|
buttonType?: "button" | "submit" | undefined;
|
|
260
260
|
} | {
|
|
261
|
-
type: import("@metamask/snaps-sdk").NodeType.Form;
|
|
262
261
|
name: string;
|
|
262
|
+
type: import("@metamask/snaps-sdk").NodeType.Form;
|
|
263
263
|
children: ({
|
|
264
|
-
type: import("@metamask/snaps-sdk").NodeType.Input;
|
|
265
264
|
name: string;
|
|
266
|
-
|
|
267
|
-
inputType?: "number" | "text" | "password" | undefined;
|
|
265
|
+
type: import("@metamask/snaps-sdk").NodeType.Input;
|
|
268
266
|
value?: string | undefined;
|
|
269
267
|
label?: string | undefined;
|
|
268
|
+
inputType?: "number" | "text" | "password" | undefined;
|
|
270
269
|
placeholder?: string | undefined;
|
|
270
|
+
error?: string | undefined;
|
|
271
271
|
} | {
|
|
272
|
-
type: import("@metamask/snaps-sdk").NodeType.Button;
|
|
273
272
|
value: string;
|
|
274
|
-
|
|
273
|
+
type: import("@metamask/snaps-sdk").NodeType.Button;
|
|
275
274
|
name?: string | undefined;
|
|
275
|
+
variant?: "primary" | "secondary" | undefined;
|
|
276
276
|
buttonType?: "button" | "submit" | undefined;
|
|
277
277
|
})[];
|
|
278
278
|
} | import("@metamask/snaps-sdk/jsx").SnapElement<{
|
|
@@ -301,8 +301,8 @@ declare const NotificationParametersStruct: import("@metamask/superstruct").Stru
|
|
|
301
301
|
text: import("@metamask/superstruct").Struct<string, null>;
|
|
302
302
|
}>;
|
|
303
303
|
}>, import("@metamask/superstruct").Struct<{
|
|
304
|
-
type: "native";
|
|
305
304
|
message: string;
|
|
305
|
+
type: "native";
|
|
306
306
|
}, {
|
|
307
307
|
type: import("@metamask/superstruct").Struct<"native", null>;
|
|
308
308
|
message: import("@metamask/superstruct").Struct<string, null>;
|
package/dist/utils.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getValueFromEntropySource = exports.StateKeyStruct = exports.isValidStateKey = exports.getNodeFromSeed = exports.getNodeFromMnemonic = exports.getPathPrefix = exports.deriveEntropyFromSeed = exports.selectHooks = exports.FORBIDDEN_KEYS = void 0;
|
|
4
4
|
const key_tree_1 = require("@metamask/key-tree");
|
|
5
5
|
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
6
6
|
const superstruct_1 = require("@metamask/superstruct");
|
|
@@ -53,7 +53,7 @@ function getDerivationPathArray(hash) {
|
|
|
53
53
|
return array;
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
56
|
-
*
|
|
56
|
+
* Get the derivation path to use for entropy derivation.
|
|
57
57
|
*
|
|
58
58
|
* This is based on the reference implementation of
|
|
59
59
|
* [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).
|
|
@@ -61,34 +61,51 @@ function getDerivationPathArray(hash) {
|
|
|
61
61
|
* @param options - The options for entropy derivation.
|
|
62
62
|
* @param options.input - The input value to derive entropy from.
|
|
63
63
|
* @param options.salt - An optional salt to use when deriving entropy.
|
|
64
|
-
* @param options.mnemonicPhrase - The mnemonic phrase to use for entropy
|
|
65
|
-
* derivation.
|
|
66
64
|
* @param options.magic - A hardened BIP-32 index, which is used to derive the
|
|
67
65
|
* root key from the mnemonic phrase.
|
|
68
|
-
* @
|
|
69
|
-
* for the derivation.
|
|
70
|
-
* @returns The derived entropy.
|
|
66
|
+
* @returns The derivation path to be used for entropy key derivation.
|
|
71
67
|
*/
|
|
72
|
-
|
|
68
|
+
function getEntropyDerivationPath({ input, salt, magic, }) {
|
|
73
69
|
const inputBytes = (0, utils_1.stringToBytes)(input);
|
|
74
70
|
const saltBytes = (0, utils_1.stringToBytes)(salt);
|
|
75
71
|
// Get the derivation path from the snap ID.
|
|
76
72
|
const hash = (0, sha3_1.keccak_256)((0, utils_1.concatBytes)([inputBytes, (0, sha3_1.keccak_256)(saltBytes)]));
|
|
77
73
|
const computedDerivationPath = getDerivationPathArray(hash);
|
|
74
|
+
return [`bip32:${magic}`, ...computedDerivationPath];
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Derive entropy from the given mnemonic seed and salt.
|
|
78
|
+
*
|
|
79
|
+
* This is based on the reference implementation of
|
|
80
|
+
* [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).
|
|
81
|
+
*
|
|
82
|
+
* @param options - The options for entropy derivation.
|
|
83
|
+
* @param options.input - The input value to derive entropy from.
|
|
84
|
+
* @param options.salt - An optional salt to use when deriving entropy.
|
|
85
|
+
* @param options.seed - The mnemonic seed to use for entropy
|
|
86
|
+
* derivation.
|
|
87
|
+
* @param options.magic - A hardened BIP-32 index, which is used to derive the
|
|
88
|
+
* root key from the mnemonic phrase.
|
|
89
|
+
* @param options.cryptographicFunctions - The cryptographic functions to use
|
|
90
|
+
* for the derivation.
|
|
91
|
+
* @returns The derived entropy.
|
|
92
|
+
*/
|
|
93
|
+
async function deriveEntropyFromSeed({ input, salt = '', seed, magic, cryptographicFunctions, }) {
|
|
94
|
+
const computedDerivationPath = getEntropyDerivationPath({
|
|
95
|
+
input,
|
|
96
|
+
salt,
|
|
97
|
+
magic,
|
|
98
|
+
});
|
|
78
99
|
// Derive the private key using BIP-32.
|
|
79
|
-
const { privateKey } = await key_tree_1.SLIP10Node.
|
|
80
|
-
derivationPath: [
|
|
81
|
-
mnemonicPhrase,
|
|
82
|
-
`bip32:${magic}`,
|
|
83
|
-
...computedDerivationPath,
|
|
84
|
-
],
|
|
100
|
+
const { privateKey } = await key_tree_1.SLIP10Node.fromSeed({
|
|
101
|
+
derivationPath: [seed, ...computedDerivationPath],
|
|
85
102
|
curve: 'secp256k1',
|
|
86
103
|
}, cryptographicFunctions);
|
|
87
104
|
// This should never happen, but this keeps TypeScript happy.
|
|
88
105
|
(0, utils_1.assert)(privateKey, 'Failed to derive the entropy.');
|
|
89
106
|
return (0, utils_1.add0x)(privateKey);
|
|
90
107
|
}
|
|
91
|
-
exports.
|
|
108
|
+
exports.deriveEntropyFromSeed = deriveEntropyFromSeed;
|
|
92
109
|
/**
|
|
93
110
|
* Get the path prefix to use for key derivation in `key-tree`. This assumes the
|
|
94
111
|
* following:
|
|
@@ -133,7 +150,7 @@ exports.getPathPrefix = getPathPrefix;
|
|
|
133
150
|
* for the node.
|
|
134
151
|
* @returns The `key-tree` SLIP-10 node.
|
|
135
152
|
*/
|
|
136
|
-
async function
|
|
153
|
+
async function getNodeFromMnemonic({ curve, secretRecoveryPhrase, path, cryptographicFunctions, }) {
|
|
137
154
|
const prefix = getPathPrefix(curve);
|
|
138
155
|
return await key_tree_1.SLIP10Node.fromDerivationPath({
|
|
139
156
|
curve,
|
|
@@ -143,7 +160,34 @@ async function getNode({ curve, secretRecoveryPhrase, path, cryptographicFunctio
|
|
|
143
160
|
],
|
|
144
161
|
}, cryptographicFunctions);
|
|
145
162
|
}
|
|
146
|
-
exports.
|
|
163
|
+
exports.getNodeFromMnemonic = getNodeFromMnemonic;
|
|
164
|
+
/**
|
|
165
|
+
* Get a `key-tree`-compatible node.
|
|
166
|
+
*
|
|
167
|
+
* Note: This function assumes that all the parameters have been validated
|
|
168
|
+
* beforehand.
|
|
169
|
+
*
|
|
170
|
+
* @param options - The derivation options.
|
|
171
|
+
* @param options.curve - The curve to use for derivation.
|
|
172
|
+
* @param options.seed - The BIP-39 to use for
|
|
173
|
+
* derivation.
|
|
174
|
+
* @param options.path - The derivation path to use as array, starting with an
|
|
175
|
+
* "m" as the first item.
|
|
176
|
+
* @param options.cryptographicFunctions - The cryptographic functions to use
|
|
177
|
+
* for the node.
|
|
178
|
+
* @returns The `key-tree` SLIP-10 node.
|
|
179
|
+
*/
|
|
180
|
+
async function getNodeFromSeed({ curve, seed, path, cryptographicFunctions, }) {
|
|
181
|
+
const prefix = getPathPrefix(curve);
|
|
182
|
+
return await key_tree_1.SLIP10Node.fromSeed({
|
|
183
|
+
curve,
|
|
184
|
+
derivationPath: [
|
|
185
|
+
seed,
|
|
186
|
+
...path.slice(1).map((index) => `${prefix}:${index}`),
|
|
187
|
+
],
|
|
188
|
+
}, cryptographicFunctions);
|
|
189
|
+
}
|
|
190
|
+
exports.getNodeFromSeed = getNodeFromSeed;
|
|
147
191
|
/**
|
|
148
192
|
* Validate the key of a state object.
|
|
149
193
|
*
|
|
@@ -164,16 +208,17 @@ exports.StateKeyStruct = (0, superstruct_1.refine)((0, superstruct_1.string)(),
|
|
|
164
208
|
return true;
|
|
165
209
|
});
|
|
166
210
|
/**
|
|
167
|
-
* Get
|
|
168
|
-
* and handles any errors that occur,
|
|
211
|
+
* Get a value using the entropy source hooks: getMnemonic or getMnemonicSeed.
|
|
212
|
+
* This function calls the passed hook and handles any errors that occur,
|
|
213
|
+
* throwing formatted JSON-RPC errors.
|
|
169
214
|
*
|
|
170
|
-
* @param
|
|
215
|
+
* @param hook - The hook.
|
|
171
216
|
* @param source - The entropy source to use.
|
|
172
217
|
* @returns The secret recovery phrase.
|
|
173
218
|
*/
|
|
174
|
-
async function
|
|
219
|
+
async function getValueFromEntropySource(hook, source) {
|
|
175
220
|
try {
|
|
176
|
-
return await
|
|
221
|
+
return await hook(source);
|
|
177
222
|
}
|
|
178
223
|
catch (error) {
|
|
179
224
|
if (error instanceof Error) {
|
|
@@ -189,5 +234,5 @@ async function getSecretRecoveryPhrase(getMnemonic, source) {
|
|
|
189
234
|
});
|
|
190
235
|
}
|
|
191
236
|
}
|
|
192
|
-
exports.
|
|
237
|
+
exports.getValueFromEntropySource = getValueFromEntropySource;
|
|
193
238
|
//# sourceMappingURL=utils.cjs.map
|
package/dist/utils.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAOA,iDAAgD;AAChD,qDAAiD;AAEjD,uDAAuD;AAEvD,2CAOyB;AACzB,6CAA6D;AAE7D,MAAM,cAAc,GAAG,UAAU,CAAC;AAErB,QAAA,cAAc,GAAG,CAAC,aAAa,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;AAWxE;;;;;;;;;;;GAWG;AACH,SAAgB,WAAW,CAIzB,KAAY,EACZ,SAAqC;IAErC,IAAI,SAAS,EAAE,CAAC;QACd,OAAO,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,MAAM,CAClC,CAAC,UAAU,EAAE,SAAS,EAAE,EAAE;YACxB,MAAM,QAAQ,GAAG,SAAqB,CAAC;YACvC,UAAU,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC;YACvC,OAAO,UAAU,CAAC;QACpB,CAAC,EACD,EAAE,CACsB,CAAC;IAC7B,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAlBD,kCAkBC;AAED;;;;;;GAMG;AACH,SAAS,sBAAsB,CAAC,IAAgB;IAC9C,MAAM,KAAK,GAAwB,EAAE,CAAC;IACtC,MAAM,IAAI,GAAG,IAAA,sBAAc,EAAC,IAAI,CAAC,CAAC;IAElC,KAAK,I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DAA4D,CAAC;IACtE,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC,CAAC,CAAC;AAEH;;;;;;;GAOG;AACI,KAAK,UAAU,uBAAuB,CAC3C,WAAiE,EACjE,MAA2B;IAE3B,IAAI,CAAC;QACH,OAAO,MAAM,WAAW,CAAC,MAAM,CAAC,CAAC;IACnC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;YAC3B,MAAM,sBAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,KAAK,CAAC,OAAO;aACvB,CAAC,CAAC;QACL,CAAC;QAED,MAAM,sBAAS,CAAC,QAAQ,CAAC;YACvB,OAAO,EAAE,4BAA4B;YACrC,IAAI,EAAE;gBACJ,KAAK,EAAE,KAAK,CAAC,QAAQ,EAAE;aACxB;SACF,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AApBD,0DAoBC","sourcesContent":["import type {\n HardenedBIP32Node,\n BIP32Node,\n SLIP10PathNode,\n SupportedCurve,\n CryptographicFunctions,\n} from '@metamask/key-tree';\nimport { SLIP10Node } from '@metamask/key-tree';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { MagicValue } from '@metamask/snaps-utils';\nimport { refine, string } from '@metamask/superstruct';\nimport type { Hex } from '@metamask/utils';\nimport {\n assertExhaustive,\n add0x,\n assert,\n concatBytes,\n createDataView,\n stringToBytes,\n} from '@metamask/utils';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\n\nconst HARDENED_VALUE = 0x80000000;\n\nexport const FORBIDDEN_KEYS = ['constructor', '__proto__', 'prototype'];\n\n/**\n * Maps an interface with method hooks to an object, using the keys of the\n * interface, and `true` as value. This ensures that the `methodHooks` object\n * has the same values as the interface.\n */\nexport type MethodHooksObject<HooksType extends Record<string, unknown>> = {\n [Key in keyof HooksType]: true;\n};\n\n/**\n * Returns the subset of the specified `hooks` that are included in the\n * `hookNames` object. This is a Principle of Least Authority (POLA) measure\n * to ensure that each RPC method implementation only has access to the\n * API \"hooks\" it needs to do its job.\n *\n * @param hooks - The hooks to select from.\n * @param hookNames - The names of the hooks to select.\n * @returns The selected hooks.\n * @template Hooks - The hooks to select from.\n * @template HookName - The names of the hooks to select.\n */\nexport function selectHooks<\n Hooks extends Record<string, unknown>,\n HookName extends keyof Hooks,\n>(\n hooks: Hooks,\n hookNames?: Record<HookName, boolean>,\n): Pick<Hooks, HookName> | undefined {\n if (hookNames) {\n return Object.keys(hookNames).reduce<Partial<Pick<Hooks, HookName>>>(\n (hookSubset, _hookName) => {\n const hookName = _hookName as HookName;\n hookSubset[hookName] = hooks[hookName];\n return hookSubset;\n },\n {},\n ) as Pick<Hooks, HookName>;\n }\n return undefined;\n}\n\n/**\n * Get a BIP-32 derivation path array from a hash, which is compatible with\n * `@metamask/key-tree`. The hash is assumed to be 32 bytes long.\n *\n * @param hash - The hash to derive indices from.\n * @returns The derived indices as a {@link HardenedBIP32Node} array.\n */\nfunction getDerivationPathArray(hash: Uint8Array): HardenedBIP32Node[] {\n const array: HardenedBIP32Node[] = [];\n const view = createDataView(hash);\n\n for (let index = 0; index < 8; index++) {\n const uint32 = view.getUint32(index * 4);\n\n // This is essentially `index | 0x80000000`. Because JavaScript numbers are\n // signed, we use the bitwise unsigned right shift operator to ensure that\n // the result is a positive number.\n // eslint-disable-next-line no-bitwise\n const pathIndex = (uint32 | HARDENED_VALUE) >>> 0;\n array.push(`bip32:${pathIndex - HARDENED_VALUE}'` as const);\n }\n\n return array;\n}\n\ntype DeriveEntropyOptions = {\n /**\n * The input value to derive entropy from.\n */\n input: string;\n\n /**\n * An optional salt to use when deriving entropy.\n */\n salt?: string;\n\n /**\n * The mnemonic phrase to use for entropy derivation.\n */\n mnemonicPhrase: Uint8Array;\n\n /**\n * A hardened BIP-32 index, which is used to derive the root key from the\n * mnemonic phrase.\n */\n magic: MagicValue;\n\n /**\n * The cryptographic functions to use for the derivation.\n */\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\n/**\n * Derive entropy from the given mnemonic phrase and salt.\n *\n * This is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param options - The options for entropy derivation.\n * @param options.input - The input value to derive entropy from.\n * @param options.salt - An optional salt to use when deriving entropy.\n * @param options.mnemonicPhrase - The mnemonic phrase to use for entropy\n * derivation.\n * @param options.magic - A hardened BIP-32 index, which is used to derive the\n * root key from the mnemonic phrase.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the derivation.\n * @returns The derived entropy.\n */\nexport async function deriveEntropy({\n input,\n salt = '',\n mnemonicPhrase,\n magic,\n cryptographicFunctions,\n}: DeriveEntropyOptions): Promise<Hex> {\n const inputBytes = stringToBytes(input);\n const saltBytes = stringToBytes(salt);\n\n // Get the derivation path from the snap ID.\n const hash = keccak256(concatBytes([inputBytes, keccak256(saltBytes)]));\n const computedDerivationPath = getDerivationPathArray(hash);\n\n // Derive the private key using BIP-32.\n const { privateKey } = await SLIP10Node.fromDerivationPath(\n {\n derivationPath: [\n mnemonicPhrase,\n `bip32:${magic}`,\n ...computedDerivationPath,\n ],\n curve: 'secp256k1',\n },\n cryptographicFunctions,\n );\n\n // This should never happen, but this keeps TypeScript happy.\n assert(privateKey, 'Failed to derive the entropy.');\n\n return add0x(privateKey);\n}\n\n/**\n * Get the path prefix to use for key derivation in `key-tree`. This assumes the\n * following:\n *\n * - The Secp256k1 curve always uses the BIP-32 specification.\n * - The Ed25519 curve always uses the SLIP-10 specification.\n * - The BIP-32-Ed25519 curve always uses the CIP-3 specification.\n *\n * While this does not matter in most situations (no known case at the time of\n * writing), `key-tree` requires a specific specification to be used.\n *\n * @param curve - The curve to get the path prefix for. The curve is NOT\n * validated by this function.\n * @returns The path prefix, i.e., `bip32` or `slip10`.\n */\nexport function getPathPrefix(\n curve: SupportedCurve,\n): 'bip32' | 'slip10' | 'cip3' {\n switch (curve) {\n case 'secp256k1':\n return 'bip32';\n case 'ed25519':\n return 'slip10';\n case 'ed25519Bip32':\n return 'cip3';\n default:\n return assertExhaustive(curve);\n }\n}\n\ntype GetNodeArgs = {\n curve: SupportedCurve;\n secretRecoveryPhrase: Uint8Array;\n path: string[];\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\n/**\n * Get a `key-tree`-compatible node.\n *\n * Note: This function assumes that all the parameters have been validated\n * beforehand.\n *\n * @param options - The derivation options.\n * @param options.curve - The curve to use for derivation.\n * @param options.secretRecoveryPhrase - The secret recovery phrase to use for\n * derivation.\n * @param options.path - The derivation path to use as array, starting with an\n * \"m\" as the first item.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the node.\n * @returns The `key-tree` SLIP-10 node.\n */\nexport async function getNode({\n curve,\n secretRecoveryPhrase,\n path,\n cryptographicFunctions,\n}: GetNodeArgs) {\n const prefix = getPathPrefix(curve);\n\n return await SLIP10Node.fromDerivationPath(\n {\n curve,\n derivationPath: [\n secretRecoveryPhrase,\n ...(path.slice(1).map((index) => `${prefix}:${index}`) as\n | BIP32Node[]\n | SLIP10PathNode[]),\n ],\n },\n cryptographicFunctions,\n );\n}\n\n/**\n * Validate the key of a state object.\n *\n * @param key - The key to validate.\n * @returns `true` if the key is valid, `false` otherwise.\n */\nexport function isValidStateKey(key: string | undefined) {\n if (key === undefined) {\n return true;\n }\n\n return key.split('.').every((part) => part.length > 0);\n}\n\nexport const StateKeyStruct = refine(string(), 'state key', (value) => {\n if (!isValidStateKey(value)) {\n return 'Invalid state key. Each part of the key must be non-empty.';\n }\n\n return true;\n});\n\n/**\n * Get the secret recovery phrase of the user. This calls the `getMnemonic` hook\n * and handles any errors that occur, throwing formatted JSON-RPC errors.\n *\n * @param getMnemonic - The `getMnemonic` hook.\n * @param source - The entropy source to use.\n * @returns The secret recovery phrase.\n */\nexport async function getSecretRecoveryPhrase(\n getMnemonic: (source?: string | undefined) => Promise<Uint8Array>,\n source?: string | undefined,\n): Promise<Uint8Array> {\n try {\n return await getMnemonic(source);\n } catch (error) {\n if (error instanceof Error) {\n throw rpcErrors.invalidParams({\n message: error.message,\n });\n }\n\n throw rpcErrors.internal({\n message: 'An unknown error occurred.',\n data: {\n error: error.toString(),\n },\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAOA,iDAAgD;AAChD,qDAAiD;AAEjD,uDAAuD;AACvD,2CAOyB;AACzB,6CAA6D;AAE7D,MAAM,cAAc,GAAG,UAAU,CAAC;AAErB,QAAA,cAAc,GAAG,CAAC,aAAa,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;AAWxE;;;;;;;;;;;GAWG;AACH,SAAgB,WAAW,CAIzB,KAAY,EACZ,SAAqC;IAErC,IAAI,SAAS,EAAE,CAAC;QACd,OAAO,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,MAAM,CAClC,CAAC,UAAU,EAAE,SAAS,EAAE,EAAE;YACxB,MAAM,QAAQ,GAAG,SAAqB,CAAC;YACvC,UAAU,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC;YACvC,OAAO,UAAU,CAAC;QACpB,CAAC,EACD,EAAE,CACsB,CAAC;IAC7B,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAlBD,kCAkBC;AAED;;;;;;GAMG;AACH,SAAS,sBAAsB,CAAC,IAAgB;IAC9C,MAAM,KAAK,GAAwB,EAAE,CAAC;IACtC,MAAM,IAAI,GAAG,IAAA,sBAAc,EAAC,IAAI,CAAC,CAAC;IAElC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC;QACvC,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;QAEzC,2EAA2E;QAC3E,0EAA0E;QAC1E,mCAAmC;QACnC,sCAAsC;QACtC,MAAM,SAAS,GAAG,CAAC,MAAM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AKG;AACH,SAAgB,eAAe,CAAC,GAAuB;IACrD,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACzD,CAAC;AAND,0CAMC;AAEY,QAAA,cAAc,GAAG,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE;IACpE,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,EAAE,CAAC;QAC5B,OAAO,4DAA4D,CAAC;IACtE,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC,CAAC,CAAC;AAEH;;;;;;;;GAQG;AACI,KAAK,UAAU,yBAAyB,CAC7C,IAA0D,EAC1D,MAA2B;IAE3B,IAAI,CAAC;QACH,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,CAAC;IAC5B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;YAC3B,MAAM,sBAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,KAAK,CAAC,OAAO;aACvB,CAAC,CAAC;QACL,CAAC;QAED,MAAM,sBAAS,CAAC,QAAQ,CAAC;YACvB,OAAO,EAAE,4BAA4B;YACrC,IAAI,EAAE;gBACJ,KAAK,EAAE,KAAK,CAAC,QAAQ,EAAE;aACxB;SACF,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AApBD,8DAoBC","sourcesContent":["import type {\n HardenedBIP32Node,\n BIP32Node,\n SLIP10PathNode,\n SupportedCurve,\n CryptographicFunctions,\n} from '@metamask/key-tree';\nimport { SLIP10Node } from '@metamask/key-tree';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { MagicValue } from '@metamask/snaps-utils';\nimport { refine, string } from '@metamask/superstruct';\nimport {\n assertExhaustive,\n add0x,\n assert,\n concatBytes,\n createDataView,\n stringToBytes,\n} from '@metamask/utils';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\n\nconst HARDENED_VALUE = 0x80000000;\n\nexport const FORBIDDEN_KEYS = ['constructor', '__proto__', 'prototype'];\n\n/**\n * Maps an interface with method hooks to an object, using the keys of the\n * interface, and `true` as value. This ensures that the `methodHooks` object\n * has the same values as the interface.\n */\nexport type MethodHooksObject<HooksType extends Record<string, unknown>> = {\n [Key in keyof HooksType]: true;\n};\n\n/**\n * Returns the subset of the specified `hooks` that are included in the\n * `hookNames` object. This is a Principle of Least Authority (POLA) measure\n * to ensure that each RPC method implementation only has access to the\n * API \"hooks\" it needs to do its job.\n *\n * @param hooks - The hooks to select from.\n * @param hookNames - The names of the hooks to select.\n * @returns The selected hooks.\n * @template Hooks - The hooks to select from.\n * @template HookName - The names of the hooks to select.\n */\nexport function selectHooks<\n Hooks extends Record<string, unknown>,\n HookName extends keyof Hooks,\n>(\n hooks: Hooks,\n hookNames?: Record<HookName, boolean>,\n): Pick<Hooks, HookName> | undefined {\n if (hookNames) {\n return Object.keys(hookNames).reduce<Partial<Pick<Hooks, HookName>>>(\n (hookSubset, _hookName) => {\n const hookName = _hookName as HookName;\n hookSubset[hookName] = hooks[hookName];\n return hookSubset;\n },\n {},\n ) as Pick<Hooks, HookName>;\n }\n return undefined;\n}\n\n/**\n * Get a BIP-32 derivation path array from a hash, which is compatible with\n * `@metamask/key-tree`. The hash is assumed to be 32 bytes long.\n *\n * @param hash - The hash to derive indices from.\n * @returns The derived indices as a {@link HardenedBIP32Node} array.\n */\nfunction getDerivationPathArray(hash: Uint8Array): HardenedBIP32Node[] {\n const array: HardenedBIP32Node[] = [];\n const view = createDataView(hash);\n\n for (let index = 0; index < 8; index++) {\n const uint32 = view.getUint32(index * 4);\n\n // This is essentially `index | 0x80000000`. Because JavaScript numbers are\n // signed, we use the bitwise unsigned right shift operator to ensure that\n // the result is a positive number.\n // eslint-disable-next-line no-bitwise\n const pathIndex = (uint32 | HARDENED_VALUE) >>> 0;\n array.push(`bip32:${pathIndex - HARDENED_VALUE}'` as const);\n }\n\n return array;\n}\n\ntype BaseDeriveEntropyOptions = {\n /**\n * The input value to derive entropy from.\n */\n input: string;\n\n /**\n * An optional salt to use when deriving entropy.\n */\n salt?: string;\n\n /**\n * A hardened BIP-32 index, which is used to derive the root key from the\n * mnemonic phrase.\n */\n magic: MagicValue;\n\n /**\n * The cryptographic functions to use for the derivation.\n */\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\ntype SeedDeriveEntropyOptions = BaseDeriveEntropyOptions & {\n /**\n * The mnemonic seed to use for entropy derivation.\n */\n seed: Uint8Array;\n};\n\n/**\n * Get the derivation path to use for entropy derivation.\n *\n * This is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param options - The options for entropy derivation.\n * @param options.input - The input value to derive entropy from.\n * @param options.salt - An optional salt to use when deriving entropy.\n * @param options.magic - A hardened BIP-32 index, which is used to derive the\n * root key from the mnemonic phrase.\n * @returns The derivation path to be used for entropy key derivation.\n */\nfunction getEntropyDerivationPath({\n input,\n salt,\n magic,\n}: Required<Omit<BaseDeriveEntropyOptions, 'cryptographicFunctions'>>):\n | BIP32Node[]\n | SLIP10PathNode[] {\n const inputBytes = stringToBytes(input);\n const saltBytes = stringToBytes(salt);\n\n // Get the derivation path from the snap ID.\n const hash = keccak256(concatBytes([inputBytes, keccak256(saltBytes)]));\n const computedDerivationPath = getDerivationPathArray(hash);\n\n return [`bip32:${magic}`, ...computedDerivationPath];\n}\n\n/**\n * Derive entropy from the given mnemonic seed and salt.\n *\n * This is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param options - The options for entropy derivation.\n * @param options.input - The input value to derive entropy from.\n * @param options.salt - An optional salt to use when deriving entropy.\n * @param options.seed - The mnemonic seed to use for entropy\n * derivation.\n * @param options.magic - A hardened BIP-32 index, which is used to derive the\n * root key from the mnemonic phrase.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the derivation.\n * @returns The derived entropy.\n */\nexport async function deriveEntropyFromSeed({\n input,\n salt = '',\n seed,\n magic,\n cryptographicFunctions,\n}: SeedDeriveEntropyOptions) {\n const computedDerivationPath = getEntropyDerivationPath({\n input,\n salt,\n magic,\n });\n\n // Derive the private key using BIP-32.\n const { privateKey } = await SLIP10Node.fromSeed(\n {\n derivationPath: [seed, ...computedDerivationPath],\n curve: 'secp256k1',\n },\n cryptographicFunctions,\n );\n\n // This should never happen, but this keeps TypeScript happy.\n assert(privateKey, 'Failed to derive the entropy.');\n\n return add0x(privateKey);\n}\n\n/**\n * Get the path prefix to use for key derivation in `key-tree`. This assumes the\n * following:\n *\n * - The Secp256k1 curve always uses the BIP-32 specification.\n * - The Ed25519 curve always uses the SLIP-10 specification.\n * - The BIP-32-Ed25519 curve always uses the CIP-3 specification.\n *\n * While this does not matter in most situations (no known case at the time of\n * writing), `key-tree` requires a specific specification to be used.\n *\n * @param curve - The curve to get the path prefix for. The curve is NOT\n * validated by this function.\n * @returns The path prefix, i.e., `bip32` or `slip10`.\n */\nexport function getPathPrefix(\n curve: SupportedCurve,\n): 'bip32' | 'slip10' | 'cip3' {\n switch (curve) {\n case 'secp256k1':\n return 'bip32';\n case 'ed25519':\n return 'slip10';\n case 'ed25519Bip32':\n return 'cip3';\n default:\n return assertExhaustive(curve);\n }\n}\n\ntype BaseGetNodeArgs = {\n curve: SupportedCurve;\n path: string[];\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\ntype GetNodeArgsMnemonic = BaseGetNodeArgs & {\n secretRecoveryPhrase: Uint8Array;\n};\n\ntype GetNodeArgsSeed = BaseGetNodeArgs & {\n seed: Uint8Array;\n};\n\n/**\n * Get a `key-tree`-compatible node.\n *\n * Note: This function assumes that all the parameters have been validated\n * beforehand.\n *\n * @param options - The derivation options.\n * @param options.curve - The curve to use for derivation.\n * @param options.secretRecoveryPhrase - The secret recovery phrase to use for\n * derivation.\n * @param options.path - The derivation path to use as array, starting with an\n * \"m\" as the first item.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the node.\n * @returns The `key-tree` SLIP-10 node.\n */\nexport async function getNodeFromMnemonic({\n curve,\n secretRecoveryPhrase,\n path,\n cryptographicFunctions,\n}: GetNodeArgsMnemonic) {\n const prefix = getPathPrefix(curve);\n\n return await SLIP10Node.fromDerivationPath(\n {\n curve,\n derivationPath: [\n secretRecoveryPhrase,\n ...(path.slice(1).map((index) => `${prefix}:${index}`) as\n | BIP32Node[]\n | SLIP10PathNode[]),\n ],\n },\n cryptographicFunctions,\n );\n}\n\n/**\n * Get a `key-tree`-compatible node.\n *\n * Note: This function assumes that all the parameters have been validated\n * beforehand.\n *\n * @param options - The derivation options.\n * @param options.curve - The curve to use for derivation.\n * @param options.seed - The BIP-39 to use for\n * derivation.\n * @param options.path - The derivation path to use as array, starting with an\n * \"m\" as the first item.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the node.\n * @returns The `key-tree` SLIP-10 node.\n */\nexport async function getNodeFromSeed({\n curve,\n seed,\n path,\n cryptographicFunctions,\n}: GetNodeArgsSeed) {\n const prefix = getPathPrefix(curve);\n\n return await SLIP10Node.fromSeed(\n {\n curve,\n derivationPath: [\n seed,\n ...(path.slice(1).map((index) => `${prefix}:${index}`) as\n | BIP32Node[]\n | SLIP10PathNode[]),\n ],\n },\n cryptographicFunctions,\n );\n}\n\n/**\n * Validate the key of a state object.\n *\n * @param key - The key to validate.\n * @returns `true` if the key is valid, `false` otherwise.\n */\nexport function isValidStateKey(key: string | undefined) {\n if (key === undefined) {\n return true;\n }\n\n return key.split('.').every((part) => part.length > 0);\n}\n\nexport const StateKeyStruct = refine(string(), 'state key', (value) => {\n if (!isValidStateKey(value)) {\n return 'Invalid state key. Each part of the key must be non-empty.';\n }\n\n return true;\n});\n\n/**\n * Get a value using the entropy source hooks: getMnemonic or getMnemonicSeed.\n * This function calls the passed hook and handles any errors that occur,\n * throwing formatted JSON-RPC errors.\n *\n * @param hook - The hook.\n * @param source - The entropy source to use.\n * @returns The secret recovery phrase.\n */\nexport async function getValueFromEntropySource(\n hook: (source?: string | undefined) => Promise<Uint8Array>,\n source?: string | undefined,\n): Promise<Uint8Array> {\n try {\n return await hook(source);\n } catch (error) {\n if (error instanceof Error) {\n throw rpcErrors.invalidParams({\n message: error.message,\n });\n }\n\n throw rpcErrors.internal({\n message: 'An unknown error occurred.',\n data: {\n error: error.toString(),\n },\n });\n }\n}\n"]}
|