Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix eth_sign
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmurdoch committed Sep 15, 2023
1 parent 0e3405a commit aba5c6d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/ethereum/ethereum/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
17 changes: 16 additions & 1 deletion packages/ethereum/ethereum/tests/api/eth/sign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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, 2),
"1b", // 27 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);
Expand All @@ -59,7 +67,14 @@ describe("api", () => {

let sgn = await provider.send("eth_sign", [accounts[0], msgHex]);

assert.strictEqual(
sgn.substring(sgn.length - 2, 2),
"1b", // 27 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);
Expand Down

0 comments on commit aba5c6d

Please sign in to comment.