From e2f81236375fa86c4fdb415f5b0954cd1cdf5cd3 Mon Sep 17 00:00:00 2001 From: David Murdoch <187813+davidmurdoch@users.noreply.github.com> Date: Fri, 15 Sep 2023 16:22:31 -0400 Subject: [PATCH] fix eth_sign --- packages/ethereum/ethereum/src/api.ts | 2 +- .../ethereum/tests/api/eth/sign.test.ts | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/ethereum/ethereum/src/api.ts b/packages/ethereum/ethereum/src/api.ts index 93d5674e8a..c4289b6825 100644 --- a/packages/ethereum/ethereum/src/api.ts +++ b/packages/ethereum/ethereum/src/api.ts @@ -2079,7 +2079,7 @@ export default class EthereumApi implements Api { const messageHash = hashPersonalMessage(Data.toBuffer(message)); const { v, r, s } = ecsign(messageHash, privateKey.toBuffer()); - return toRpcSig(v, r, s); + return toRpcSig(v + 27n, r, s); } /** diff --git a/packages/ethereum/ethereum/tests/api/eth/sign.test.ts b/packages/ethereum/ethereum/tests/api/eth/sign.test.ts index ecfab32d06..f57726ba4e 100644 --- a/packages/ethereum/ethereum/tests/api/eth/sign.test.ts +++ b/packages/ethereum/ethereum/tests/api/eth/sign.test.ts @@ -8,6 +8,7 @@ import { } from "@ethereumjs/util"; import getProvider from "../../helpers/getProvider"; import { Data, Quantity } from "@ganache/utils"; +import { sign } from "crypto"; describe("api", () => { describe("eth", () => { @@ -37,10 +38,17 @@ describe("api", () => { const msgHash = hashPersonalMessage(msg); const address = accounts[0]; - let sgn = await provider.send("eth_sign", [ + let sgn: string = await provider.send("eth_sign", [ address, Data.toString(msg) ]); + + assert.strictEqual( + sgn.substring(sgn.length - 2), + "1c", // 28 in hex + "eth_sign should produce a v value of 27 or 28" + ); + const { v, r, s } = fromRpcSig(sgn); const pub = ecrecover(msgHash, v, r, s); @@ -59,7 +67,14 @@ describe("api", () => { let sgn = await provider.send("eth_sign", [accounts[0], msgHex]); + assert.strictEqual( + sgn.substring(sgn.length - 2), + "1c", // 28 in hex + "eth_sign should produce a v value of 27 or 28" + ); + const { v, r, s } = fromRpcSig(sgn); + const pub = ecrecover(msgHash, v, r, s); const addr = fromSigned(pubToAddress(pub)); const strAddr = Data.toString(Quantity.toBuffer(addr), 20);