Repository: onflow/fcl-js
Pull request #1629 (2023-04-17 18:12:02)

PKG -- [config] Fix key warning
Addresses: https://github.com/onflow/fcl-js/issues/1625

changeset-bot[bot]: ###  ⚠️  No Changeset found

Latest commit: d800d7292602197650c96baf46e4990fa7c21159

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. **If these changes should result in a version bump, you need to add a changeset.**

<details><summary>This PR includes no changesets</summary>

  When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

</details>

[Click here to learn what changesets are, and how to add one](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add a changeset to this PR](https://github.com/onflow/fcl-js/new/chasefleming/key-warning-improvement?filename=.changeset/lovely-gifts-search.md&value=---%0A%22%40onflow%2Fconfig%22%3A%20patch%0A---%0A%0APKG%20--%20%5Bconfig%5D%20Fix%20key%20warning%0A)



Patch:
diff --git a/.changeset/metal-steaks-bake.md b/.changeset/metal-steaks-bake.md
new file mode 100644
index 00000000..e3a1e09f
--- /dev/null
+++ b/.changeset/metal-steaks-bake.md
@@ -0,0 +1,5 @@
+---
+"@onflow/config": patch
+---
+
+Fix key warning when key/file/location format is used in flow.json
diff --git a/packages/config/utils/utils.js b/packages/config/utils/utils.js
index 2866d1fe..818c0276 100644
--- a/packages/config/utils/utils.js
+++ b/packages/config/utils/utils.js
@@ -126,6 +126,17 @@ export const getContracts = (jsons, network) => {
   )(jsons)
 }
 
+/**
+ * @descriptions Checks if string is hexidecimal
+ * @param {string} str - String to check
+ * @returns {boolean} - Is hexidecimal status
+ */
+const isHexidecimal = str => {
+  // Check that it is a string
+  if (typeof str !== "string") return false
+  return /^[0-9A-Fa-f]+$/.test(str)
+}
+
 /**
  * @description Checks flow.json file for private keys
  * @param {object} flowJSON - Flow JSON
@@ -135,7 +146,7 @@ const hasPrivateKeys = flowJSON => {
   return Object.entries(flowJSON?.accounts).reduce(
     (hasPrivateKey, [key, value]) => {
       if (hasPrivateKey) return true
-      return value?.hasOwnProperty("key") && !value?.key?.startsWith("$")
+      return value?.hasOwnProperty("key") && isHexidecimal(value?.key)
     },
     false
   )


Test patch:
diff --git a/packages/config/utils/utils.test.js b/packages/config/utils/utils.test.js
index e821253d..74f2e59c 100644
--- a/packages/config/utils/utils.test.js
+++ b/packages/config/utils/utils.test.js
@@ -131,4 +131,39 @@ describe("anyHasPrivateKeys", () => {
     expect(anyHasPrivateKeys(flowJSON)).toBe(false)
     expect(anyHasPrivateKeys([flowJSON, flowJSONTwo])).toBe(false)
   })
+
+  test("it should return false if private key value is in separate file", () => {
+    const flowJSON = {
+      accounts: {
+        "emulator-account": {
+          address: "f8d6e0586b0a20c7",
+          key: {
+            type: "file",
+            location: "./emulator.key"
+          }
+        },
+      },
+    }
+
+    const flowJSONTwo = {
+      accounts: {
+        "emulator-account": {
+          address: "f8d6e0586b0a20c7",
+        },
+      },
+    }
+
+    const flowJSONThree = {
+      accounts: {
+        "emulator-account": {
+          address: "f8d6e0586b0a20c7",
+          key: "ba68d45a5acaa52f3cacf4ad3a64d9523e0ce0ae3addb1ee6805385b380b7646"
+        },
+      },
+    }
+
+    expect(anyHasPrivateKeys(flowJSON)).toBe(false)
+    expect(anyHasPrivateKeys([flowJSON, flowJSONTwo])).toBe(false)
+    expect(anyHasPrivateKeys(flowJSONThree)).toBe(true)
+  })
 })