diff --git a/packages/hardhat-core/src/internal/core/errors-list.ts b/packages/hardhat-core/src/internal/core/errors-list.ts index 15bfe4474a..a77466ad82 100644 --- a/packages/hardhat-core/src/internal/core/errors-list.ts +++ b/packages/hardhat-core/src/internal/core/errors-list.ts @@ -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: { diff --git a/packages/hardhat-core/test/internal/core/config/config-validation.ts b/packages/hardhat-core/test/internal/core/config/config-validation.ts index df1386c3a7..41992bf8cb 100644 --- a/packages/hardhat-core/test/internal/core/config/config-validation.ts +++ b/packages/hardhat-core/test/internal/core/config/config-validation.ts @@ -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(