-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathindex.tsx
More file actions
256 lines (247 loc) · 8.59 KB
/
index.tsx
File metadata and controls
256 lines (247 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import React, { useMemo } from "react";
import { hexaStringToBuffer } from "@ledgerhq/device-management-kit";
import {
type GetAddressDAError,
type GetAddressDAIntermediateValue,
type GetAddressDAOutput,
type SignDelegationAuthorizationDAError,
type SignDelegationAuthorizationDAIntermediateValue,
type SignDelegationAuthorizationDAOutput,
type SignPersonalMessageDAError,
type SignPersonalMessageDAIntermediateValue,
type SignPersonalMessageDAOutput,
type SignTransactionDAError,
type SignTransactionDAIntermediateValue,
type SignTransactionDAOutput,
type SignTypedDataDAError,
type SignTypedDataDAIntermediateValue,
type SignTypedDataDAOutput,
type TypedData,
} from "@ledgerhq/device-signer-kit-ethereum";
import { ethers } from "ethers";
import { DeviceActionsList } from "@/components/DeviceActionsView/DeviceActionsList";
import { type DeviceActionProps } from "@/components/DeviceActionsView/DeviceActionTester";
import { useDmk } from "@/providers/DeviceManagementKitProvider";
import { useSignerEth } from "@/providers/SignerEthProvider";
export const SignerEthView: React.FC<{ sessionId: string }> = ({
sessionId,
}) => {
const dmk = useDmk();
const signer = useSignerEth();
const deviceModelId = dmk.getConnectedDevice({
sessionId,
}).modelId;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const deviceActions: DeviceActionProps<any, any, any, any>[] = useMemo(
() => [
{
title: "Get address",
description:
"Perform all the actions necessary to get an ethereum address from the device",
executeDeviceAction: ({
derivationPath,
checkOnDevice,
returnChainCode,
skipOpenApp,
}) => {
if (!signer) {
throw new Error("Signer not initialized");
}
return signer.getAddress(derivationPath, {
checkOnDevice,
returnChainCode,
skipOpenApp,
});
},
initialValues: {
derivationPath: "44'/60'/0'/0/0",
checkOnDevice: false,
returnChainCode: false,
skipOpenApp: false,
},
deviceModelId,
} satisfies DeviceActionProps<
GetAddressDAOutput,
{
derivationPath: string;
checkOnDevice?: boolean;
returnChainCode?: boolean;
skipOpenApp?: boolean;
},
GetAddressDAError,
GetAddressDAIntermediateValue
>,
{
title: "Sign message",
description:
"Perform all the actions necessary to sign a message with the device",
executeDeviceAction: ({ derivationPath, message, skipOpenApp }) => {
if (!signer) {
throw new Error("Signer not initialized");
}
return signer.signMessage(derivationPath, message, { skipOpenApp });
},
initialValues: {
derivationPath: "44'/60'/0'/0/0",
message: "Hello World",
skipOpenApp: false,
},
deviceModelId,
} satisfies DeviceActionProps<
SignPersonalMessageDAOutput,
{
derivationPath: string;
message: string;
skipOpenApp?: boolean;
},
SignPersonalMessageDAError,
SignPersonalMessageDAIntermediateValue
>,
{
title: "Sign transaction",
description:
"Perform all the actions necessary to sign a transaction with the device (JSON or serialized raw TX)",
executeDeviceAction: ({
derivationPath,
transaction,
recipientDomain,
skipOpenApp,
}) => {
if (!signer) {
throw new Error("Signer not initialized");
}
// If it's a json transaction, try to parse it
try {
const jsonTx = JSON.parse(transaction);
// Remove "from" if present in the json transaction because it make no sense
// in an unsigned ethereum tx, and ethers will reject it
if (jsonTx.from) {
delete jsonTx.from;
}
transaction = ethers.Transaction.from(jsonTx).unsignedSerialized;
} catch (_: unknown) {
// Not a JSON
}
// Convert the serialized rawTx into a buffer
const tx = hexaStringToBuffer(transaction);
if (!tx) {
throw new Error("Invalid transaction format");
}
return signer.signTransaction(derivationPath, tx, {
domain: recipientDomain,
skipOpenApp,
});
},
initialValues: {
derivationPath: "44'/60'/0'/0/0",
transaction: "",
recipientDomain: "",
skipOpenApp: false,
},
deviceModelId,
} satisfies DeviceActionProps<
SignTransactionDAOutput,
{
derivationPath: string;
transaction: string;
recipientDomain: string;
skipOpenApp?: boolean;
},
SignTransactionDAError,
SignTransactionDAIntermediateValue
>,
{
title: "Sign typed message",
description:
"Perform all the actions necessary to sign a typed message on the device",
executeDeviceAction: ({ derivationPath, message, skipOpenApp }) => {
const typedData = JSON.parse(message) as TypedData;
if (!signer) {
throw new Error("Signer not initialized");
}
return signer.signTypedData(derivationPath, typedData, {
skipOpenApp,
});
},
initialValues: {
derivationPath: "44'/60'/0'/0/0",
message: `{"domain":{"name":"USD Coin","verifyingContract":"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","chainId":1,"version":"2"},"primaryType":"Permit","message":{"deadline":1718992051,"nonce":0,"spender":"0x111111125421ca6dc452d289314280a0f8842a65","owner":"0x6cbcd73cd8e8a42844662f0a0e76d7f79afd933d","value":"115792089237316195423570985008687907853269984665640564039457584007913129639935"},"types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Permit":[{"name":"owner","type":"address"},{"name":"spender","type":"address"},{"name":"value","type":"uint256"},{"name":"nonce","type":"uint256"},{"name":"deadline","type":"uint256"}]}}`,
skipOpenApp: false,
},
validateValues: ({ message }) => {
try {
const parsedData = JSON.parse(message);
if (
typeof parsedData !== "object" ||
typeof parsedData.domain !== "object" ||
typeof parsedData.types !== "object" ||
typeof parsedData.primaryType !== "string" ||
typeof parsedData.message !== "object"
) {
console.log(
`Invalid typed message format: should conform to EIP712 specification`,
);
return false;
}
} catch (error) {
console.log(`Invalid typed message format (${error})`);
return false;
}
return true;
},
deviceModelId,
} satisfies DeviceActionProps<
SignTypedDataDAOutput,
{
derivationPath: string;
message: string;
skipOpenApp?: boolean;
},
SignTypedDataDAError,
SignTypedDataDAIntermediateValue
>,
{
title: "Sign Delegation Authorization",
description:
"Perform all the actions necessary to sign an EIP 7702 Delegation Authorization with the device",
executeDeviceAction: ({
derivationPath,
nonce,
contractAddress,
chainId,
}) => {
if (!signer) {
throw new Error("Signer not initialized");
}
return signer.signDelegationAuthorization(
derivationPath,
chainId,
contractAddress,
nonce,
);
},
initialValues: {
derivationPath: "44'/60'/0'/0/0",
nonce: 0,
contractAddress: "0x",
chainId: 1,
},
deviceModelId,
} satisfies DeviceActionProps<
SignDelegationAuthorizationDAOutput,
{
derivationPath: string;
nonce: number;
contractAddress: string;
chainId: number;
},
SignDelegationAuthorizationDAError,
SignDelegationAuthorizationDAIntermediateValue
>,
],
[deviceModelId, signer],
);
return (
<DeviceActionsList title="Signer Ethereum" deviceActions={deviceActions} />
);
};