Repository: NomicFoundation/hardhat-ignition (ts)

Issue:
update default deployment id to `chain-<chainId>`
There is a concept of `network-id` in HH, this means that our default deployment id, the one we use if no deployment id is provided by the user, could be misleading.

To clarify, we are going to  change the default deployment id to be `chain-<CHAINID>`, where `<CHAINID>` is the chain id of the network being connected to.

Pull request:
feat: update the default deployment id template
The template changes from `network-<CHAINID>` to `chain-<CHAINID>`.

There is now also a validation test against the user provided deployment id to ensure it matches our rules. Specifically that it starts with an upper or lowercase letter, and then only includes alpha numerics, underscores or dashes.

Resolves #551.

Patch:
diff --git a/packages/core/test-integrations/fixture-projects/default-with-new-chain-id/ignition/deployments/network-31337/deployed_addresses.json b/packages/core/test-integrations/fixture-projects/default-with-new-chain-id/ignition/deployments/chain-31337/deployed_addresses.json
similarity index 100%
rename from packages/core/test-integrations/fixture-projects/default-with-new-chain-id/ignition/deployments/network-31337/deployed_addresses.json
rename to packages/core/test-integrations/fixture-projects/default-with-new-chain-id/ignition/deployments/chain-31337/deployed_addresses.json
diff --git a/packages/core/test-integrations/fixture-projects/default-with-new-chain-id/ignition/deployments/network-31337/journal.jsonl b/packages/core/test-integrations/fixture-projects/default-with-new-chain-id/ignition/deployments/chain-31337/journal.jsonl
similarity index 100%
rename from packages/core/test-integrations/fixture-projects/default-with-new-chain-id/ignition/deployments/network-31337/journal.jsonl
rename to packages/core/test-integrations/fixture-projects/default-with-new-chain-id/ignition/deployments/chain-31337/journal.jsonl
diff --git a/packages/hardhat-plugin/src/index.ts b/packages/hardhat-plugin/src/index.ts
index 472fe289..b600241d 100644
--- a/packages/hardhat-plugin/src/index.ts
+++ b/packages/hardhat-plugin/src/index.ts
@@ -11,6 +11,7 @@ import path from "path";
 
 import "./type-extensions";
 import { calculateDeploymentStatusDisplay } from "./ui/helpers/calculate-deployment-status-display";
+import { resolveDeploymentId } from "./utils/resolve-deployment-id";
 
 /* ignition config defaults */
 const IGNITION_DIR = "ignition";
@@ -67,7 +68,7 @@ ignitionScope
       }: {
         modulePath: string;
         parameters?: string;
-        deploymentId: string;
+        deploymentId: string | undefined;
       },
       hre
     ) => {
@@ -128,7 +129,7 @@ ignitionScope
         method: "eth_accounts",
       })) as string[];
 
-      const deploymentId = givenDeploymentId ?? `network-${chainId}`;
+      const deploymentId = resolveDeploymentId(givenDeploymentId, chainId);
 
       const deploymentDir =
         hre.network.name === "hardhat"
diff --git a/packages/hardhat-plugin/src/utils/resolve-deployment-id.ts b/packages/hardhat-plugin/src/utils/resolve-deployment-id.ts
new file mode 100644
index 00000000..ad9aaf25
--- /dev/null
+++ b/packages/hardhat-plugin/src/utils/resolve-deployment-id.ts
@@ -0,0 +1,43 @@
+import { NomicLabsHardhatPluginError } from "hardhat/plugins";
+
+/**
+ * A regex that captures Ignitions rules for deployment-ids, specifically
+ * that they can only contain alphanumerics, dashes and underscores,
+ * and that they start with a letter.
+ */
+const ignitionDeploymentIdRegex = /^[a-zA-Z][a-zA-Z0-9_\-]*$/;
+
+/**
+ * Determine the deploymentId, using either the user provided id,
+ * throwing if it is invalid, or generating one from the chainId
+ * if none was provided.
+ *
+ * @param givenDeploymentId - the user provided deploymentId if
+ * they provided one undefined otherwise
+ * @param chainId - the chainId of the network being deployed to
+ *
+ * @returns the deploymentId
+ */
+export function resolveDeploymentId(
+  givenDeploymentId: string | undefined,
+  chainId: number
+) {
+  if (
+    givenDeploymentId !== undefined &&
+    !_isValidDeploymentIdentifier(givenDeploymentId)
+  ) {
+    throw new NomicLabsHardhatPluginError(
+      "hardhat-ignition",
+      `The deployment-id "${givenDeploymentId}" contains banned characters, ids can only contain alphanumerics, dashes or underscores`
+    );
+  }
+
+  return givenDeploymentId ?? `chain-${chainId}`;
+}
+
+/**
+ * Determine if the given identifier the rules for a valid deployment id.
+ * */
+export function _isValidDeploymentIdentifier(identifier: string): boolean {
+  return ignitionDeploymentIdRegex.test(identifier);
+}


Test patch:
diff --git a/packages/hardhat-plugin/test/utils/resolve-deployment-id.ts b/packages/hardhat-plugin/test/utils/resolve-deployment-id.ts
new file mode 100644
index 00000000..8532411a
--- /dev/null
+++ b/packages/hardhat-plugin/test/utils/resolve-deployment-id.ts
@@ -0,0 +1,28 @@
+import { assert } from "chai";
+
+import { resolveDeploymentId } from "../../src/utils/resolve-deployment-id";
+
+describe("deploy id rules", () => {
+  const exampleChainId = 31337;
+
+  it("should use the user provided id if one is provided", () => {
+    const deploymentId = resolveDeploymentId(
+      "my-deployment-id",
+      exampleChainId
+    );
+
+    assert.equal(deploymentId, "my-deployment-id");
+  });
+
+  it("should generate a default id based on the chainId if the user provided no deploymentId", () => {
+    const deploymentId = resolveDeploymentId(undefined, exampleChainId);
+
+    assert.equal(deploymentId, "chain-31337");
+  });
+
+  it("should throw if the user provided an invalid deploymentId", () => {
+    assert.throws(() => {
+      resolveDeploymentId("deployment/test", exampleChainId);
+    }, /The deployment-id "deployment\/test" contains banned characters, ids can only contain alphanumerics, dashes or underscores/);
+  });
+});