|
| 1 | +import hre from "hardhat"; |
| 2 | +import assert from "node:assert"; |
| 3 | + |
| 4 | +const BALANCE_TO_SEND = 1000000000000000000n; |
| 5 | + |
| 6 | +const { provider, ethers } = await hre.network.connect("edrOp"); |
| 7 | + |
| 8 | +const signers = await ethers.getSigners(); |
| 9 | + |
| 10 | +const hardhatSigner = signers[0]; |
| 11 | +const hardhatAddress = hardhatSigner.address; |
| 12 | +// Ledger accounts are returned at the end |
| 13 | +const ledgerSigner = signers[signers.length - 1]; |
| 14 | +const ledgerAddress = ledgerSigner.address; |
| 15 | + |
| 16 | +// Be sure that the ledger account has some ETH |
| 17 | +await hardhatSigner.sendTransaction({ |
| 18 | + to: ledgerAddress, |
| 19 | + value: BALANCE_TO_SEND, |
| 20 | +}); |
| 21 | + |
| 22 | +// Uncomment the RPC method you want to test |
| 23 | + |
| 24 | +await ethSendTransaction(); |
| 25 | + |
| 26 | +// await ethSign(); |
| 27 | + |
| 28 | +// await personalSign(); |
| 29 | + |
| 30 | +// await ethSignTypedDataV4(); |
| 31 | + |
| 32 | +async function ethSendTransaction() { |
| 33 | + const txParams = { |
| 34 | + // Shared properties |
| 35 | + to: hardhatAddress, |
| 36 | + value: 10000000n, |
| 37 | + gas: 310000n, |
| 38 | + |
| 39 | + // Enable the properties that you need for a specific transaction type |
| 40 | + // and comment out the ones that you don't need |
| 41 | + |
| 42 | + // eip1559 |
| 43 | + maxFeePerGas: 1000000000n, |
| 44 | + maxPriorityFeePerGas: 1000000000n, |
| 45 | + |
| 46 | + // eip2930 |
| 47 | + //gasPrice: 999999345n, |
| 48 | + //accessList: [], |
| 49 | + |
| 50 | + // legacy |
| 51 | + // gasPrice: 999999345n, |
| 52 | + }; |
| 53 | + |
| 54 | + await ledgerSigner.sendTransaction(txParams); |
| 55 | +} |
| 56 | + |
| 57 | +async function ethSign() { |
| 58 | + const msg = ethers.toUtf8Bytes("Hello eth_sign"); |
| 59 | + const hexMsg = ethers.hexlify(msg); |
| 60 | + |
| 61 | + const sig = await provider.request({ |
| 62 | + method: "eth_sign", |
| 63 | + params: [ledgerAddress, hexMsg], |
| 64 | + }); |
| 65 | + |
| 66 | + const verifiedAddress = ethers.verifyMessage("Hello eth_sign", sig); |
| 67 | + |
| 68 | + assert.equal(verifiedAddress, ledgerAddress); |
| 69 | +} |
| 70 | + |
| 71 | +async function personalSign() { |
| 72 | + const message = "Hello personal_sign"; |
| 73 | + |
| 74 | + const sig = await provider.request({ |
| 75 | + method: "personal_sign", |
| 76 | + params: [ethers.hexlify(ethers.toUtf8Bytes(message)), ledgerAddress], |
| 77 | + }); |
| 78 | + |
| 79 | + const recovered = ethers.verifyMessage(message, sig); |
| 80 | + |
| 81 | + assert.equal(recovered, ledgerAddress); |
| 82 | +} |
| 83 | + |
| 84 | +async function ethSignTypedDataV4() { |
| 85 | + const chainId = Number((await ethers.provider.getNetwork()).chainId); |
| 86 | + |
| 87 | + // EIP‑712 domain separator |
| 88 | + const domain = { |
| 89 | + name: "MyDApp", |
| 90 | + version: "1", |
| 91 | + chainId, |
| 92 | + verifyingContract: "0x0000000000000000000000000000000000000000", |
| 93 | + }; |
| 94 | + |
| 95 | + const mailTypes = { |
| 96 | + Mail: [ |
| 97 | + { name: "from", type: "address" }, |
| 98 | + { name: "to", type: "address" }, |
| 99 | + { name: "contents", type: "string" }, |
| 100 | + ], |
| 101 | + }; |
| 102 | + |
| 103 | + const types = { |
| 104 | + EIP712Domain: [ |
| 105 | + { name: "name", type: "string" }, |
| 106 | + { name: "version", type: "string" }, |
| 107 | + { name: "chainId", type: "uint256" }, |
| 108 | + { name: "verifyingContract", type: "address" }, |
| 109 | + ], |
| 110 | + ...mailTypes, |
| 111 | + }; |
| 112 | + |
| 113 | + const message = { |
| 114 | + from: ledgerAddress, |
| 115 | + to: hardhatAddress, |
| 116 | + contents: "Hello typed data", |
| 117 | + }; |
| 118 | + |
| 119 | + const json = JSON.stringify({ |
| 120 | + domain, |
| 121 | + types, |
| 122 | + primaryType: "Mail", |
| 123 | + message, |
| 124 | + }); |
| 125 | + |
| 126 | + const sig = await provider.request({ |
| 127 | + method: "eth_signTypedData_v4", |
| 128 | + params: [ledgerAddress, json], |
| 129 | + }); |
| 130 | + |
| 131 | + const digest = ethers.TypedDataEncoder.hash(domain, mailTypes, message); |
| 132 | + const recovered = ethers.recoverAddress(digest, sig); |
| 133 | + |
| 134 | + assert.equal(recovered, ledgerAddress); |
| 135 | +} |
0 commit comments