Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(store): add unwrap() function to ResourceIdInstance #3249

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sour-bats-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/store": patch
---

Added an `unwrap` function to the `ResourceIdInstance` library to make it easier to unwrap a `ResourceId` with `resourceId.unwrap()`.
20 changes: 20 additions & 0 deletions docs/pages/store/reference/misc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,26 @@ function getResourceName(ResourceId resourceId) internal pure returns (bytes30);
| -------- | --------- | --------------- |
| `<none>` | `bytes30` | A 30-byte name. |

#### unwrap

Unwrap a resource ID into a bytes32.

```solidity
function unwrap(ResourceId resourceId) internal pure returns (bytes32);
```

**Parameters**

| Name | Type | Description |
| ------------ | ------------ | ---------------- |
| `resourceId` | `ResourceId` | The resource ID. |

**Returns**

| Name | Type | Description |
| -------- | --------- | ----------------------- |
| `<none>` | `bytes32` | The underlying bytes32. |

## ResourceIdLib

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/ResourceId.sol)
Expand Down
9 changes: 9 additions & 0 deletions packages/store/src/ResourceId.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@ library ResourceIdInstance {
function getResourceName(ResourceId resourceId) internal pure returns (bytes30) {
return bytes30(ResourceId.unwrap(resourceId) << (TYPE_BITS));
}

/**
* @notice Unwrap a resource ID into a bytes32.
* @param resourceId The resource ID.
* @return The underlying bytes32.
*/
function unwrap(ResourceId resourceId) internal pure returns (bytes32) {
return ResourceId.unwrap(resourceId);
}
}
6 changes: 6 additions & 0 deletions packages/store/test/ResourceId.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ contract ResourceIdTest is Test, GasReporter {
assertEq(resourceType, RESOURCE_TABLE);
}

function testUnwrap() public {
bytes32 underlying = keccak256("test");
ResourceId id = ResourceId.wrap(underlying);
assertEq(id.unwrap(), ResourceId.unwrap(id));
}

function testFuzz(bytes30 name, bytes2 resourceType) public {
ResourceId tableId = ResourceIdLib.encode({ typeId: resourceType, name: name });
assertEq(tableId.getType(), resourceType);
Expand Down
Loading