Skip to content

Commit

Permalink
feat(store): add unwrap() function to ResourceIdInstance (#3249)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Ingersoll <[email protected]>
  • Loading branch information
vdrg and holic authored Oct 3, 2024
1 parent 97c86a3 commit 13e5689
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
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

0 comments on commit 13e5689

Please sign in to comment.