Skip to content

Commit 7f3502d

Browse files
update manual tests
1 parent fbc2152 commit 7f3502d

1 file changed

Lines changed: 131 additions & 84 deletions

File tree

‎packages/example-project/scripts/ledger-2.ts‎

Lines changed: 131 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
*
4141
* - The origin token: the plugin has no setting for it (an open question in
4242
* the doc), so there is nothing to run. Switching the network module off
43-
* is now a setting, `useRemoteLedgerServices`, and the two scenarios
44-
* around TEST NR 15 cover both of its positions.
43+
* is now a setting, `useRemoteLedgerServices`, and TEST NR 15 covers both
44+
* of its positions, read from `hardhat.config.ts`.
4545
*/
4646
import type { TypedDataDomain, TypedDataField } from "ethers";
4747
import type { NetworkConnectionParams } from "hardhat/types/network";
@@ -196,10 +196,9 @@ if (LOG_REQUESTS_TO_LEDGER_SERVERS) {
196196
// own: the plugin releases the device session at the end of every request.
197197
// await run(forgetToClose);
198198

199-
// TEST NR 9 (approve on the device): a field named only with digits, which the
200-
// DMK encodes in the wrong order: after your approval the plugin refuses the
201-
// signature with ETH_SIGN_TYPED_DATA_V4_SIGNATURE_MISMATCH, because it does not
202-
// recover to your address.
199+
// TEST NR 9: a field named only with digits, which the DMK would encode in the
200+
// wrong order, is refused with ETH_SIGN_TYPED_DATA_V4_INVALID_DATA_PARAM
201+
// before the device is asked for anything.
203202
// await run(typedDataDigitsFieldName);
204203

205204
// TEST NR 10: nine malformed inputs (an integer above 2^53 as a JSON number, a
@@ -213,6 +212,13 @@ if (LOG_REQUESTS_TO_LEDGER_SERVERS) {
213212
// "Device not connected" retry message, then a successful retry.
214213
// await run(deviceNotPluggedIn);
215214

215+
// TEST NR 35 (numbered last so that the numbers above do not move, but worth
216+
// running with the rest of priority 1): three transactions in a row on one
217+
// connection, the ordinary deployment-script case now that every request
218+
// releases its device session: three approvals, no reconnection between them,
219+
// all three mined.
220+
// await run(threeTransactionsInSequence);
221+
216222
// ===========================================================================
217223
// PRIORITY 2: run if time allows. Claims of the design doc that depend on the
218224
// device or on Ledger's servers.
@@ -232,18 +238,19 @@ if (LOG_REQUESTS_TO_LEDGER_SERVERS) {
232238
// and amount instead of raw calldata.
233239
// await run(erc20TransferIsClearSigned);
234240

235-
// TEST NR 15 (enable Blind signing in the Ethereum app's settings first, or
236-
// the device refuses the last two signatures): with the default config, where
237-
// `useRemoteLedgerServices` is off, nothing at all is sent to Ledger: no
238-
// [ledger-http] line for any of personal_sign, two transactions and typed
239-
// data, and the device shows everything raw.
240-
// await run(noRequestsWithRemoteServicesOff);
241+
// TEST NR 15 (run it twice, and enable Blind signing in the Ethereum app's
242+
// settings first, or the device refuses the last two signatures of the "off"
243+
// run): the setting is read from `hardhat.config.ts`, the way a user sets it.
244+
// Run it once as the config ships, with `networks.edrOp.ledgerOptions` unset:
245+
// nothing at all is sent to Ledger, and the device shows everything raw. Then
246+
// add `ledgerOptions: { useRemoteLedgerServices: true }` to `networks.edrOp`
247+
// and run it again: every request the DMK makes to Ledger's servers is
248+
// printed, none for personal_sign, metadata plus telemetry for the two
249+
// transactions and for typed data, all without an origin token.
250+
// await run(remoteLedgerServicesFromConfig);
241251

242-
// TEST NR 16: the same run with `useRemoteLedgerServices` on, to compare it
243-
// against: every request the DMK makes to Ledger's servers is printed, none
244-
// for personal_sign, metadata plus telemetry for the transactions and for
245-
// typed data, all without an origin token.
246-
// await run(requestsWithRemoteServicesOn);
252+
// TEST NR 16: merged into TEST NR 15, which now reads the config instead of
253+
// overriding the setting on its connection. The numbers below are unchanged.
247254

248255
// TEST NR 17: a contract call with no descriptor (a local Counter) is shown
249256
// raw, with the blind-signing warning, exactly as today.
@@ -267,7 +274,7 @@ if (LOG_REQUESTS_TO_LEDGER_SERVERS) {
267274
// TEST NR 22: two concurrent requests run one at a time, each on its own
268275
// device session: a single "Connecting to Ledger..." line (only the first
269276
// connection is announced), two approvals, two valid signatures.
270-
// await run(concurrentRequestsShareOneSession);
277+
// await run(concurrentRequestsRunOneAtATime);
271278

272279
// TEST NR 23 (forks mainnet for the second half): a recipient with an ENS name
273280
// is shown as the raw address on chain 10 and as "nick.eth" on chain 1.
@@ -300,9 +307,9 @@ if (LOG_REQUESTS_TO_LEDGER_SERVERS) {
300307
// TEST NR 29: typed data passed as an object instead of a JSON string.
301308
// await run(typedDataAsObject);
302309

303-
// TEST NR 30 (approve on the device): a field named __proto__, whose value the
304-
// DMK loses: after your approval the plugin refuses the signature with
305-
// ETH_SIGN_TYPED_DATA_V4_SIGNATURE_MISMATCH.
310+
// TEST NR 30: a field named __proto__, whose value the DMK would lose, is
311+
// refused with ETH_SIGN_TYPED_DATA_V4_INVALID_DATA_PARAM before the device is
312+
// asked for anything.
306313
// await run(typedDataProtoFieldName);
307314

308315
// TEST NR 31: an ERC-20 approve is clear-signed like the transfer.
@@ -433,7 +440,41 @@ async function unplugAndReplugBetweenRequests(): Promise<void> {
433440
});
434441
}
435442

436-
async function concurrentRequestsShareOneSession(): Promise<void> {
443+
/**
444+
* The ordinary case for a deployment script: several Ledger-signed
445+
* transactions in a row, each opening and releasing its own device session.
446+
*/
447+
async function threeTransactionsInSequence(): Promise<void> {
448+
expected(
449+
"'[hardhat-ledger] Connecting to Ledger...' and 'Connection successful' " +
450+
"printed once, before the first transaction: the requests after it " +
451+
"open their own device session silently, since only the first " +
452+
"connection is announced. Three approvals on the device, one after " +
453+
"the other, with no 'Reconnecting to Ledger...' and no retry message " +
454+
"between them. All three are mined, so the account's nonce grows by " +
455+
"three, and the process exits.",
456+
);
457+
458+
await withConnection(async (ctx) => {
459+
const nonceBefore = await ctx.ethers.provider.getTransactionCount(
460+
ctx.ledgerAddress,
461+
);
462+
463+
for (const index of [1, 2, 3]) {
464+
step(`Transaction ${index} of 3. Approve it on the device.`);
465+
466+
await sendEth(ctx, ctx.hardhatAddress, BigInt(index) * 10n ** 12n);
467+
}
468+
469+
assert.equal(
470+
await ctx.ethers.provider.getTransactionCount(ctx.ledgerAddress),
471+
nonceBefore + 3,
472+
"The three transactions did not all land",
473+
);
474+
});
475+
}
476+
477+
async function concurrentRequestsRunOneAtATime(): Promise<void> {
437478
expected(
438479
"'[hardhat-ledger] Connecting to Ledger...' printed once: the second " +
439480
"request waits for the first to finish, then opens its own session " +
@@ -621,44 +662,41 @@ async function twoConnectionsAtOnce(): Promise<void> {
621662
// Behaviors to be aware of: what the device shows, what goes to Ledger
622663
// ---------------------------------------------------------------------------
623664

624-
async function noRequestsWithRemoteServicesOff(): Promise<void> {
665+
/**
666+
* Reads `useRemoteLedgerServices` from `hardhat.config.ts`, the way a user
667+
* sets it, instead of overriding it on the connection. Run it once with the
668+
* setting absent and once with it on, and compare the two logs.
669+
*/
670+
async function remoteLedgerServicesFromConfig(): Promise<void> {
671+
const enabled =
672+
hre.config.networks.edrOp.ledgerOptions?.useRemoteLedgerServices ?? false;
673+
625674
expected(
626-
"not one [ledger-http] line, from connecting to the last signature: " +
627-
"with `useRemoteLedgerServices` off, which is the default, the plugin " +
628-
"never calls Ledger. The device showed raw data throughout: the ETH " +
629-
"transfer without the 'Optimism' network name and with no Transaction " +
630-
"Check prompt, the USDC transfer as raw calldata behind a " +
631-
"blind-signing warning instead of 'Send 100 USDC', and the typed data " +
632-
"as a single hash instead of its fields. The scenario fails if any " +
633-
"request was made; `requestsWithRemoteServicesOn` is the same run with " +
634-
"the setting on, to compare it against.",
675+
enabled
676+
? "[ledger-http] lines for the two transactions and the typed data, " +
677+
"and none for connecting, opening the app, deriving the path or " +
678+
"personal_sign. The transactions fetched display metadata " +
679+
"(Ledger's crypto-assets service, nft.api.live.ledger.com, " +
680+
"metadata.api.live.ledger.com...), so the device showed 'Optimism' " +
681+
"and 'Send 100 USDC', and each signature ended with a POST to " +
682+
"blind-signing.api.ledger.com/ingest, the telemetry. Typed data " +
683+
"did the same for the domain's chain id and verifying contract. " +
684+
"Every line says 'no origin token': the plugin sends none. The " +
685+
"scenario fails if nothing was sent."
686+
: "not one [ledger-http] line, from connecting to the last signature: " +
687+
"with `useRemoteLedgerServices` unset, which is the default, the " +
688+
"plugin never calls Ledger. The device showed raw data " +
689+
"throughout: the ETH transfer without the 'Optimism' network name " +
690+
"and with no Transaction Check prompt, the USDC transfer as raw " +
691+
"calldata behind a blind-signing warning instead of 'Send 100 " +
692+
"USDC', and the typed data as a single hash instead of its " +
693+
"fields. The scenario fails if any request was made.",
635694
);
636695

637-
recordRequestsToLedgerServers();
638-
639-
await withConnection(async (ctx) => {
640-
await signOnEveryPath(ctx);
641-
642-
assert.deepEqual(
643-
requestsToLedger,
644-
[],
645-
"The plugin contacted Ledger with useRemoteLedgerServices off",
646-
);
647-
});
648-
}
649-
650-
async function requestsWithRemoteServicesOn(): Promise<void> {
651-
expected(
652-
"[ledger-http] lines for the two transactions and the typed data, and " +
653-
"none for connecting, opening the app, deriving the path or " +
654-
"personal_sign. The transactions fetched display metadata (Ledger's " +
655-
"crypto-assets service, nft.api.live.ledger.com, " +
656-
"metadata.api.live.ledger.com...), so the device showed 'Optimism' " +
657-
"and 'Send 100 USDC', and each signature ended with a POST to " +
658-
"blind-signing.api.ledger.com/ingest, the telemetry. Typed data did " +
659-
"the same for the domain's chain id and verifying contract. Every " +
660-
"line says 'no origin token': the plugin sends none. The scenario " +
661-
"fails if nothing was sent.",
696+
step(
697+
enabled
698+
? "`networks.edrOp.ledgerOptions.useRemoteLedgerServices` is true in hardhat.config.ts."
699+
: "`networks.edrOp.ledgerOptions` is unset in hardhat.config.ts, so the services are off.",
662700
);
663701

664702
recordRequestsToLedgerServers();
@@ -668,11 +706,19 @@ async function requestsWithRemoteServicesOn(): Promise<void> {
668706

669707
console.log(`\n>>> ${requestsToLedger.length} request(s) to Ledger`);
670708

671-
assert.ok(
672-
requestsToLedger.length > 0,
673-
"The plugin contacted nobody with useRemoteLedgerServices on",
674-
);
675-
}, withRemoteLedgerServices());
709+
if (enabled) {
710+
assert.ok(
711+
requestsToLedger.length > 0,
712+
"The plugin contacted nobody with useRemoteLedgerServices on",
713+
);
714+
} else {
715+
assert.deepEqual(
716+
requestsToLedger,
717+
[],
718+
"The plugin contacted Ledger with useRemoteLedgerServices off",
719+
);
720+
}
721+
});
676722
}
677723

678724
async function plainTransfer(): Promise<void> {
@@ -682,8 +728,8 @@ async function plainTransfer(): Promise<void> {
682728
"off and nothing is fetched from Ledger: check whether the network " +
683729
"name, 'Optimism', and its icon on Stax, Flex and Nano Gen5 still " +
684730
"appear without the network descriptor, and expect no Transaction " +
685-
"Check prompt. `requestsWithRemoteServicesOn` is the same transfer " +
686-
"with the setting on, where the name and the icon do appear and a " +
731+
"Check prompt. TEST NR 15, run with the setting on in the config, is " +
732+
"the same transfer, where the name and the icon do appear and a " +
687733
"touchscreen device may first ask whether to enable Transaction " +
688734
"Check, printed as '[hardhat-ledger] Answer the Web3 Checks prompt on " +
689735
"your Ledger device'; accepting it changes nothing either way, " +
@@ -1145,11 +1191,11 @@ async function typedDataRefusedBeforeDevice(): Promise<void> {
11451191
}
11461192

11471193
// ---------------------------------------------------------------------------
1148-
// Typed data: refused after you approve on the device
1194+
// Typed data: field names the signer kit cannot carry
11491195
// ---------------------------------------------------------------------------
11501196

11511197
async function typedDataDigitsFieldName(): Promise<void> {
1152-
await refusedAfterApproval(
1198+
await refusedBeforeDevice(
11531199
"A field named `123`",
11541200
(ctx) =>
11551201
mail(ctx, {
@@ -1166,14 +1212,14 @@ async function typedDataDigitsFieldName(): Promise<void> {
11661212
contents: undefined,
11671213
},
11681214
}),
1169-
"JavaScript orders digit-only keys first, so the DMK encodes the fields " +
1170-
"in a different order than declared, the device signs that, and the " +
1171-
"recovered address is not yours.",
1215+
"JavaScript orders digit-only keys first, so the DMK would encode the " +
1216+
"fields in a different order than declared, and the device would sign " +
1217+
"that.",
11721218
);
11731219
}
11741220

11751221
async function typedDataProtoFieldName(): Promise<void> {
1176-
await refusedAfterApproval(
1222+
await refusedBeforeDevice(
11771223
"A field named `__proto__`",
11781224
(ctx) =>
11791225
JSON.stringify(
@@ -1184,8 +1230,9 @@ async function typedDataProtoFieldName(): Promise<void> {
11841230
message: { PROTO: "not an own property once parsed by the DMK" },
11851231
}),
11861232
).replaceAll('"PROTO"', '"__proto__"'),
1187-
"the DMK loses the value on the way to the device (a `__proto__` key sets " +
1188-
"a prototype instead of a property), so the device signs something else.",
1233+
"the DMK would lose the value on the way to the device (a `__proto__` " +
1234+
"key sets a prototype instead of a property), so the device would sign " +
1235+
"something else.",
11891236
);
11901237
}
11911238

@@ -1579,30 +1626,30 @@ function recoverMail(
15791626
}
15801627

15811628
/**
1582-
* Signs typed data the DMK is known to mis-encode and checks that the plugin
1583-
* refuses the signature after the device produced it.
1629+
* Signs typed data whose field names the signer kit cannot carry, and checks
1630+
* that the plugin refuses it before the device is asked for anything.
15841631
*/
1585-
async function refusedAfterApproval(
1632+
async function refusedBeforeDevice(
15861633
label: string,
15871634
build: (ctx: Ctx) => string | object,
15881635
why: string,
15891636
): Promise<void> {
15901637
expected(
1591-
"'[hardhat-ledger] Review and approve the typed data on your Ledger " +
1592-
"device', your approval, '[hardhat-ledger] Confirmation success', " +
1593-
"and only then ETH_SIGN_TYPED_DATA_V4_SIGNATURE_MISMATCH ('The " +
1594-
"Ledger device signed different typed data than requested'): the " +
1595-
"signature was checked against the data you sent and did not " +
1596-
"recover to your address. Why: " +
1597-
why,
1638+
"ETH_SIGN_TYPED_DATA_V4_INVALID_DATA_PARAM without a '[hardhat-ledger] " +
1639+
"Review and approve the typed data on your Ledger device' line: the " +
1640+
"device was connected but never asked to sign, so there is nothing to " +
1641+
"approve. Why: " +
1642+
why +
1643+
" The plugin would only notice once you had approved it, so it checks " +
1644+
"the field names first.",
15981645
);
15991646

1600-
step(`${label}. Approve it on the device when asked.`);
1647+
step(label);
16011648

16021649
await withConnection(async (ctx) => {
16031650
await expectLedgerError(
16041651
signTypedData(ctx, build(ctx)),
1605-
"ETH_SIGN_TYPED_DATA_V4_SIGNATURE_MISMATCH",
1652+
"ETH_SIGN_TYPED_DATA_V4_INVALID_DATA_PARAM",
16061653
);
16071654
});
16081655
}

0 commit comments

Comments
 (0)