forked from NomicFoundation/hardhat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/hardhat-core/src/internal/hardhat-network/provider/modules/personal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Address, toRpcSig } from "ethereumjs-util"; | ||
import { rpcAddress, rpcData } from "../../../core/jsonrpc/types/base-types"; | ||
import { validateParams } from "../../../core/jsonrpc/types/input/validation"; | ||
import { MethodNotFoundError } from "../../../core/providers/errors"; | ||
import { HardhatNode } from "../node"; | ||
|
||
/* eslint-disable @nomiclabs/hardhat-internal-rules/only-hardhat-error */ | ||
|
||
export class PersonalModule { | ||
constructor(private readonly _node: HardhatNode) {} | ||
|
||
public async processRequest( | ||
method: string, | ||
params: any[] = [] | ||
): Promise<any> { | ||
switch (method) { | ||
case "personal_sign": { | ||
return this._signAction(...this._signParams(params)); | ||
} | ||
} | ||
|
||
throw new MethodNotFoundError(`Method ${method} not found`); | ||
} | ||
|
||
// personal_sign | ||
|
||
private _signParams(params: any[]): [Buffer, Buffer] { | ||
return validateParams(params, rpcData, rpcAddress); | ||
} | ||
|
||
private async _signAction(data: Buffer, address: Buffer): Promise<string> { | ||
const signature = await this._node.signPersonalMessage( | ||
new Address(address), | ||
data | ||
); | ||
|
||
return toRpcSig(signature.v, signature.r, signature.s); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/hardhat-core/test/internal/hardhat-network/provider/modules/personal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { assert } from "chai"; | ||
|
||
import { workaroundWindowsCiFailures } from "../../../../utils/workaround-windows-ci-failures"; | ||
import { setCWD } from "../../helpers/cwd"; | ||
import { PROVIDERS } from "../../helpers/providers"; | ||
|
||
describe("Personal module", function () { | ||
PROVIDERS.forEach(({ name, useProvider, isFork }) => { | ||
if (isFork) { | ||
this.timeout(50000); | ||
} | ||
|
||
workaroundWindowsCiFailures.call(this, { isFork }); | ||
|
||
describe(`${name} provider`, function () { | ||
setCWD(); | ||
useProvider(); | ||
|
||
describe("personal_sign", async function () { | ||
it("Should be compatible with geth's implementation", async function () { | ||
// This test was created by using Geth 1.10.12-unstable and calling personal_sign | ||
const result = await this.provider.request({ | ||
method: "personal_sign", | ||
params: [ | ||
"0x5417aa2a18a44da0675524453ff108c545382f0d7e26605c56bba47c21b5e979", | ||
"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266", | ||
], | ||
}); | ||
|
||
assert.equal( | ||
result, | ||
"0x9c73dd4937a37eecab3abb54b74b6ec8e500080431d36afedb1726624587ee6710296e10c1194dded7376f13ff03ef6c9e797eb86bae16c20c57776fc69344271c" | ||
); | ||
}); | ||
|
||
it("Should be compatible with metamask's implementation", async function () { | ||
// This test was created by using Metamask 10.3.0 | ||
|
||
const result = (await this.provider.request({ | ||
method: "personal_sign", | ||
params: [ | ||
"0x7699f568ecd7753e6ddf75a42fa4c2cc86cbbdc704c9eb1a6b6d4b9d8b8d1519", | ||
"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266", | ||
], | ||
})) as string; | ||
|
||
assert.equal( | ||
result, | ||
"0x2875e4206c9fe3b229291c81f95cc4f421e2f4d3e023f5b4041daa56ab4000977010b47a3c01036ec8a6a0872aec2ab285150f003d01b0d8da60c1cceb9154181c" | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |