Skip to content

Commit

Permalink
extra tests for privkey validation
Browse files Browse the repository at this point in the history
  • Loading branch information
zoeyTM committed Nov 4, 2021
1 parent 2783718 commit 771fef4
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/hardhat-core/src/internal/core/errors-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,19 @@ Please run: npm install --save-dev typescript`,
Please run this and try again: \`npm install --save-dev typescript\``,
shouldBeReported: false,
},
INVALID_PRIVATE_KEY: {
number: 15,
message: `There's one or more invalid private keys in your config file:
%keys%
To learn more about Hardhat's configuration, please go to https://hardhat.org/config/`,
title: "Invalid private key",
description: `You have one or more errors in your config file.
Check the error message for details, or go to the [documentation](https://hardhat.org/config/) to learn more.`,
shouldBeReported: false,
},
},
NETWORK: {
CONFIG_NOT_FOUND: {
Expand Down
195 changes: 195 additions & 0 deletions packages/hardhat-core/test/internal/core/config/config-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,201 @@ describe("Config validation", function () {
});
});

/**
* networkConfig.accounts
if string => must === "remote"
if object => ~
if array =>
if network === hardhat =>
each => isValidHexString(each.privateKey)
&& isValidBalance?(each.balance)
else (network === http) =>
each => isValidHexString(each)
*/

/**
* This describe block will encompass all private key tests
* for both Hardhat and HTTP networks
*/
describe("Private key config", function () {
describe("HTTP network accounts", function () {
it("Should allow an array of valid private keys", function () {
validateConfig({
networks: {
custom: {
url: "http://localhost",
accounts: [
"0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
],
},
},
});
});

it("Should allow valid private keys with missing hex prefix", function () {
validateConfig({
networks: {
custom: {
url: "http://localhost",
accounts: [
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
],
},
},
});
});

it("Should not allow hex literals", function () {
expectHardhatError(
() =>
validateConfig({
networks: {
custom: {
url: "http://localhost",
accounts: [
0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
],
},
},
}),
ERRORS.GENERAL.INVALID_PRIVATE_KEY
);
});

it("Should not allow private keys of incorrect length", function () {
expectHardhatError(
() =>
validateConfig({
networks: {
custom: {
url: "http://localhost",
accounts: ["0xaaaa"],
},
},
}),
ERRORS.GENERAL.INVALID_PRIVATE_KEY
);

expectHardhatError(
() =>
validateConfig({
networks: {
custom: {
url: "http://localhost",
accounts: [
"0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabb",
],
},
},
}),
ERRORS.GENERAL.INVALID_PRIVATE_KEY
);
});
});

describe("Hardhat Network accounts", function () {
it("Should allow an array of account objects with valid private keys", function () {
validateConfig({
networks: {
[HARDHAT_NETWORK_NAME]: {
accounts: [
{
balance: "123",
privateKey:
"0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
},
{
balance: "123",
privateKey:
"0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
},
{
balance: "123",
privateKey:
"0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
},
],
},
},
});
});

it("Should allow valid private keys with missing hex prefix", function () {
validateConfig({
networks: {
[HARDHAT_NETWORK_NAME]: {
accounts: [
{
balance: "123",
privateKey:
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
},
],
},
},
});
});

it("Should not allow hex literals", function () {
expectHardhatError(
() =>
validateConfig({
networks: {
[HARDHAT_NETWORK_NAME]: {
accounts: [
{
balance: "123",
privateKey: 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
},
],
},
},
}),
ERRORS.GENERAL.INVALID_PRIVATE_KEY
);
});

it("Should not allow private keys of incorrect length", function () {
expectHardhatError(
() =>
validateConfig({
networks: {
[HARDHAT_NETWORK_NAME]: {
accounts: [
{
balance: "123",
privateKey: "0xaaaa",
},
],
},
},
}),
ERRORS.GENERAL.INVALID_PRIVATE_KEY
);

expectHardhatError(
() =>
validateConfig({
networks: {
[HARDHAT_NETWORK_NAME]: {
accounts: [
{
balance: "123",
privateKey:
"0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbb",
},
],
},
},
}),
ERRORS.GENERAL.INVALID_PRIVATE_KEY
);
});
});
});

describe("Hardhat Network config", function () {
it("Should fail with invalid types", function () {
expectHardhatError(
Expand Down

0 comments on commit 771fef4

Please sign in to comment.