-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(world): prevent namespace from ending with underscore [M-05] (#2182)
Co-authored-by: Kevin Ingersoll <[email protected]> Co-authored-by: yonada <[email protected]>
- Loading branch information
1 parent
a35c05e
commit 17f9872
Showing
10 changed files
with
93 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@latticexyz/world": patch | ||
--- | ||
|
||
Added a check to prevent namespaces from ending with an underscore (which could cause problems with world function signatures). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.21; | ||
|
||
import { ResourceId, WorldResourceIdInstance } from "./WorldResourceId.sol"; | ||
import { RESOURCE_NAMESPACE } from "./worldResourceTypes.sol"; | ||
import { IWorldErrors } from "./IWorldErrors.sol"; | ||
|
||
using WorldResourceIdInstance for ResourceId; | ||
|
||
/** | ||
* @notice Checks if a given `resourceId` is a namespace. | ||
* @dev Reverts with `IWorldErrors.World_InvalidResourceType` if the ID does not have the correct components. | ||
* @param resourceId The resource ID to check. | ||
*/ | ||
function requireNamespace(ResourceId resourceId) pure { | ||
// Require the resourceId to have the namespace type and the root name | ||
// (the resource ID is identical to the resource ID of its namespace) | ||
if (ResourceId.unwrap(resourceId) != ResourceId.unwrap(resourceId.getNamespaceId())) { | ||
revert IWorldErrors.World_InvalidResourceType(RESOURCE_NAMESPACE, resourceId, resourceId.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.21; | ||
|
||
import { Bytes } from "@latticexyz/store/src/Bytes.sol"; | ||
|
||
import { ResourceId, WorldResourceIdInstance, WorldResourceIdLib } from "./WorldResourceId.sol"; | ||
import { IWorldErrors } from "./IWorldErrors.sol"; | ||
|
||
using WorldResourceIdInstance for ResourceId; | ||
|
||
/** | ||
* @notice Checks if a given `resourceId` is a valid namespace. | ||
* @dev Reverts with `IWorldErrors.World_InvalidNamespace` if the namespace includes the reserved `__` separator string or ends with `_`. | ||
* @param resourceId The resource ID to validate. | ||
*/ | ||
function requireValidNamespace(ResourceId resourceId) pure { | ||
// Require the namespace to not include the reserved separator | ||
bytes14 namespace = resourceId.getNamespace(); | ||
string memory trimmedNamespace = WorldResourceIdLib.toTrimmedString(namespace); | ||
uint256 trimmedNamespaceLength = bytes(trimmedNamespace).length; | ||
|
||
if (trimmedNamespaceLength > 0) { | ||
if (Bytes.slice1(bytes(trimmedNamespace), trimmedNamespaceLength - 1) == "_") { | ||
revert IWorldErrors.World_InvalidNamespace(namespace); | ||
} | ||
|
||
for (uint256 i; i < trimmedNamespaceLength - 1; i++) { | ||
if (Bytes.slice1(bytes(trimmedNamespace), i) == "_" && Bytes.slice1(bytes(trimmedNamespace), i + 1) == "_") { | ||
revert IWorldErrors.World_InvalidNamespace(namespace); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters