From 26501a51a17ee6bdc51bdcd3c4f4f7c07eaa39d4 Mon Sep 17 00:00:00 2001 From: wbt Date: Tue, 5 Jul 2022 14:41:00 -0400 Subject: [PATCH 1/7] Use newer ethereumjs-util Hopefully fixes #5260 --- packages/hdwallet-provider/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hdwallet-provider/package.json b/packages/hdwallet-provider/package.json index 17f6f87f888..e623d4a4b27 100644 --- a/packages/hdwallet-provider/package.json +++ b/packages/hdwallet-provider/package.json @@ -26,7 +26,7 @@ "@metamask/eth-sig-util": "4.0.1", "ethereum-cryptography": "1.0.3", "ethereum-protocol": "^1.0.1", - "ethereumjs-util": "^6.1.0", + "ethereumjs-util": "^7.1.5", "ethereumjs-wallet": "^1.0.1", "web3-provider-engine": "16.0.3" }, From b50a0deca333a386a3202ba8067568ca3e232225 Mon Sep 17 00:00:00 2001 From: eggplantzzz Date: Wed, 6 Jul 2022 10:39:24 -0400 Subject: [PATCH 2/7] update yarn.lock --- yarn.lock | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/yarn.lock b/yarn.lock index d784c8b47ad..00ec879e26c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14253,7 +14253,7 @@ ethereumjs-util@^5.0.0, ethereumjs-util@^5.1.1, ethereumjs-util@^5.1.2, ethereum safe-buffer "^5.1.1" secp256k1 "^3.0.1" -ethereumjs-util@^6.0.0, ethereumjs-util@^6.1.0: +ethereumjs-util@^6.0.0: version "6.2.0" resolved "https://registry.yarnpkg.com/ethereumjs-util/-/ethereumjs-util-6.2.0.tgz#23ec79b2488a7d041242f01e25f24e5ad0357960" integrity sha512-vb0XN9J2QGdZGIEKG2vXM+kUdEivUfU6Wmi5y0cg+LRhDYKnXIZ/Lz7XjFbHRR9VIKq2lVGLzGBkA++y2nOdOQ== @@ -14326,6 +14326,17 @@ ethereumjs-util@^7.1.1, ethereumjs-util@^7.1.3, ethereumjs-util@^7.1.4: ethereum-cryptography "^0.1.3" rlp "^2.2.4" +ethereumjs-util@^7.1.5: + version "7.1.5" + resolved "https://registry.yarnpkg.com/ethereumjs-util/-/ethereumjs-util-7.1.5.tgz#9ecf04861e4fbbeed7465ece5f23317ad1129181" + integrity sha512-SDl5kKrQAudFBUe5OJM9Ac6WmMyYmXX/6sTmLZ3ffG2eY6ZIGBes3pEDxNN6V72WyOw4CPD5RomKdsa8DAAwLg== + dependencies: + "@types/bn.js" "^5.1.0" + bn.js "^5.1.2" + create-hash "^1.1.2" + ethereum-cryptography "^0.1.3" + rlp "^2.2.4" + ethereumjs-vm@^2.3.4: version "2.6.0" resolved "https://registry.yarnpkg.com/ethereumjs-vm/-/ethereumjs-vm-2.6.0.tgz#76243ed8de031b408793ac33907fb3407fe400c6" From 0dabf569d52d239e5db54af7009a50c0c01f7b36 Mon Sep 17 00:00:00 2001 From: eggplantzzz Date: Wed, 6 Jul 2022 14:05:34 -0400 Subject: [PATCH 3/7] get rid of redundant mnemonic validation that was causing problems --- .../src/constructor/getOptions.ts | 25 +++++++++---------- .../hdwallet-provider/test/provider.test.ts | 4 +-- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/packages/hdwallet-provider/src/constructor/getOptions.ts b/packages/hdwallet-provider/src/constructor/getOptions.ts index 4e0f12f9a74..391d90157b8 100644 --- a/packages/hdwallet-provider/src/constructor/getOptions.ts +++ b/packages/hdwallet-provider/src/constructor/getOptions.ts @@ -2,14 +2,11 @@ import type { MnemonicPhrase, PrivateKey } from "./types"; import type { ConstructorArguments } from "./ConstructorArguments"; import type * as Constructor from "./Constructor"; import type * as LegacyConstructor from "./LegacyConstructor"; -import { validateMnemonic } from "ethereum-cryptography/bip39"; -import { wordlist } from "ethereum-cryptography/bip39/wordlists/english"; // check that the first argument is a mnemonic phrase const isMnemonicPhrase = ( credentials: LegacyConstructor.Credentials -): credentials is MnemonicPhrase => - typeof credentials === "string" && validateMnemonic(credentials, wordlist); +): credentials is MnemonicPhrase => typeof credentials === "string"; // check that the first argument is a list of private keys const isPrivateKeys = ( @@ -20,29 +17,31 @@ const isPrivateKeys = ( const isPrivateKey = ( credentials: LegacyConstructor.Credentials ): credentials is PrivateKey => - !isPrivateKeys(credentials) && !isMnemonicPhrase(credentials); + typeof credentials === "string" && + credentials.length === 64 && + // this is added since parseInt(mnemonic) should equal NaN and private keys + // should parse into a valid number + parseInt(credentials) !== NaN; // turn polymorphic first argument into { mnemonic } or { privateKeys } const getSigningAuthorityOptions = ( credentials: LegacyConstructor.Credentials ): Constructor.SigningAuthority => { - if (isMnemonicPhrase(credentials)) { + if (isPrivateKey(credentials)) { return { - mnemonic: { - phrase: credentials - } + privateKeys: [credentials] }; } else if (isPrivateKeys(credentials)) { return { privateKeys: credentials }; - } else if (isPrivateKey(credentials)) { - // if(...) included for explicitness + } else if (isMnemonicPhrase(credentials)) { return { - privateKeys: [credentials] + mnemonic: { + phrase: credentials + } }; } else { - // this won't be reached until/unless we validate private key(s) throw new Error( `First argument to new HDWalletProvider() must be a mnemonic phrase, a ` + `single private key, or a list of private keys. ` + diff --git a/packages/hdwallet-provider/test/provider.test.ts b/packages/hdwallet-provider/test/provider.test.ts index aef7e67b7ff..062d733d4bc 100644 --- a/packages/hdwallet-provider/test/provider.test.ts +++ b/packages/hdwallet-provider/test/provider.test.ts @@ -1,4 +1,4 @@ -import assert from "assert"; +import { assert } from "chai"; import Ganache from "ganache"; import * as EthUtil from "ethereumjs-util"; import Web3 from "web3"; @@ -66,7 +66,7 @@ describe("HD Wallet Provider", function () { ); assert.fail("Should throw on invalid mnemonic"); } catch (e) { - assert(e.message.includes("Could not create addresses")); + assert(e.message.includes("Mnemonic invalid or undefined")); } }); From 5a1f42eb7590724bb704f5070caabe6e9ddda7b6 Mon Sep 17 00:00:00 2001 From: eggplantzzz Date: Fri, 8 Jul 2022 10:45:15 -0400 Subject: [PATCH 4/7] update private key and mnemonic check --- .../hdwallet-provider/src/constructor/getOptions.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/hdwallet-provider/src/constructor/getOptions.ts b/packages/hdwallet-provider/src/constructor/getOptions.ts index 391d90157b8..abcbea33dee 100644 --- a/packages/hdwallet-provider/src/constructor/getOptions.ts +++ b/packages/hdwallet-provider/src/constructor/getOptions.ts @@ -6,7 +6,9 @@ import type * as LegacyConstructor from "./LegacyConstructor"; // check that the first argument is a mnemonic phrase const isMnemonicPhrase = ( credentials: LegacyConstructor.Credentials -): credentials is MnemonicPhrase => typeof credentials === "string"; +): credentials is MnemonicPhrase => { + return typeof credentials === "string" && credentials.includes(" "); +}; // check that the first argument is a list of private keys const isPrivateKeys = ( @@ -19,9 +21,11 @@ const isPrivateKey = ( ): credentials is PrivateKey => typeof credentials === "string" && credentials.length === 64 && - // this is added since parseInt(mnemonic) should equal NaN and private keys - // should parse into a valid number - parseInt(credentials) !== NaN; + // this is added since parseInt(mnemonic) should equal NaN (unless it starts + // with a-f) and private keys should parse into a valid number - this will + // also parse with the largest hex value, namely "f" * 64 + parseInt(credentials, 16) !== NaN && + !credentials.includes(" "); // turn polymorphic first argument into { mnemonic } or { privateKeys } const getSigningAuthorityOptions = ( From ea6f45343dab36a16d5ddb03082ce321372d0111 Mon Sep 17 00:00:00 2001 From: eggplantzzz Date: Fri, 8 Jul 2022 14:03:45 -0400 Subject: [PATCH 5/7] add mnemonic to hdwallet tests for providers --- packages/hdwallet-provider/test/urlValidation.test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/hdwallet-provider/test/urlValidation.test.ts b/packages/hdwallet-provider/test/urlValidation.test.ts index bd55bfc4509..869b706a959 100644 --- a/packages/hdwallet-provider/test/urlValidation.test.ts +++ b/packages/hdwallet-provider/test/urlValidation.test.ts @@ -4,6 +4,8 @@ import Ganache from "ganache"; import { describe, it } from "mocha"; const { isValidProvider } = WalletProvider; +const mnemonic = + "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat"; describe("HD Wallet Provider Validator", () => { it("fails missing protocol", () => { @@ -17,7 +19,7 @@ describe("HD Wallet Provider Validator", () => { it("throws a meaningful error", () => { const badUrl = "localhost/v1/badbeef"; try { - new WalletProvider("", badUrl, 0, 100); + new WalletProvider(mnemonic, badUrl, 0, 100); assert.fail("did not throw!"); } catch (e) { const expectedMessage = [ @@ -32,7 +34,7 @@ describe("HD Wallet Provider Validator", () => { it("throws a meaningful error when url is without slashes or slash", () => { const badUrl = "http:localhost/v1/badbeef"; try { - new WalletProvider("", badUrl, 0, 100); + new WalletProvider(mnemonic, badUrl, 0, 100); assert.fail("did not throw!"); } catch (e) { const expectedMessage = [ From dd804ce13a10e99ddae9f8fc34d6490bd0ada0dd Mon Sep 17 00:00:00 2001 From: eggplantzzz Date: Fri, 8 Jul 2022 16:29:52 -0400 Subject: [PATCH 6/7] update method names for testing mneomic-likeness and private key-likeness --- packages/hdwallet-provider/src/constructor/getOptions.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/hdwallet-provider/src/constructor/getOptions.ts b/packages/hdwallet-provider/src/constructor/getOptions.ts index abcbea33dee..2394099e171 100644 --- a/packages/hdwallet-provider/src/constructor/getOptions.ts +++ b/packages/hdwallet-provider/src/constructor/getOptions.ts @@ -4,7 +4,7 @@ import type * as Constructor from "./Constructor"; import type * as LegacyConstructor from "./LegacyConstructor"; // check that the first argument is a mnemonic phrase -const isMnemonicPhrase = ( +const isMnemonicLike = ( credentials: LegacyConstructor.Credentials ): credentials is MnemonicPhrase => { return typeof credentials === "string" && credentials.includes(" "); @@ -16,7 +16,7 @@ const isPrivateKeys = ( ): credentials is PrivateKey[] => credentials instanceof Array; // check that the first argument is a single private key (default case for invalid mnemonics) -const isPrivateKey = ( +const isPrivateKeyLike = ( credentials: LegacyConstructor.Credentials ): credentials is PrivateKey => typeof credentials === "string" && @@ -31,7 +31,7 @@ const isPrivateKey = ( const getSigningAuthorityOptions = ( credentials: LegacyConstructor.Credentials ): Constructor.SigningAuthority => { - if (isPrivateKey(credentials)) { + if (isPrivateKeyLike(credentials)) { return { privateKeys: [credentials] }; @@ -39,7 +39,7 @@ const getSigningAuthorityOptions = ( return { privateKeys: credentials }; - } else if (isMnemonicPhrase(credentials)) { + } else if (isMnemonicLike(credentials)) { return { mnemonic: { phrase: credentials From 024536d5bea9a164f8ce9b5b024aeed9b27313aa Mon Sep 17 00:00:00 2001 From: eggplantzzz Date: Fri, 8 Jul 2022 16:40:29 -0400 Subject: [PATCH 7/7] update one more function name --- packages/hdwallet-provider/src/constructor/getOptions.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/hdwallet-provider/src/constructor/getOptions.ts b/packages/hdwallet-provider/src/constructor/getOptions.ts index 2394099e171..726b0fc6ff4 100644 --- a/packages/hdwallet-provider/src/constructor/getOptions.ts +++ b/packages/hdwallet-provider/src/constructor/getOptions.ts @@ -11,7 +11,7 @@ const isMnemonicLike = ( }; // check that the first argument is a list of private keys -const isPrivateKeys = ( +const isPrivateKeysLike = ( credentials: LegacyConstructor.Credentials ): credentials is PrivateKey[] => credentials instanceof Array; @@ -35,7 +35,7 @@ const getSigningAuthorityOptions = ( return { privateKeys: [credentials] }; - } else if (isPrivateKeys(credentials)) { + } else if (isPrivateKeysLike(credentials)) { return { privateKeys: credentials };