From aee8020a65ca5cfebb2ca479357a535bbf07269b Mon Sep 17 00:00:00 2001 From: yonada Date: Fri, 12 Jan 2024 15:30:09 +0000 Subject: [PATCH] fix(world): check namespace exists before balance transfer [L-03] (#2095) Co-authored-by: alvarius Co-authored-by: Kevin Ingersoll --- .changeset/seven-carpets-develop.md | 5 ++++ .../implementations/BalanceTransferSystem.sol | 4 +++ packages/world/test/WorldBalance.t.sol | 26 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 .changeset/seven-carpets-develop.md diff --git a/.changeset/seven-carpets-develop.md b/.changeset/seven-carpets-develop.md new file mode 100644 index 0000000000..c1ab18bf56 --- /dev/null +++ b/.changeset/seven-carpets-develop.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/world": patch +--- + +Namespace balances can no longer be transferred to non-existent namespaces. diff --git a/packages/world/src/modules/core/implementations/BalanceTransferSystem.sol b/packages/world/src/modules/core/implementations/BalanceTransferSystem.sol index 085e98db7f..53b7c88fc0 100644 --- a/packages/world/src/modules/core/implementations/BalanceTransferSystem.sol +++ b/packages/world/src/modules/core/implementations/BalanceTransferSystem.sol @@ -2,6 +2,7 @@ pragma solidity >=0.8.21; import { ResourceId, ResourceIdInstance } from "@latticexyz/store/src/ResourceId.sol"; +import { ResourceIds } from "@latticexyz/store/src/codegen/tables/ResourceIds.sol"; import { System } from "../../../System.sol"; import { revertWithBytes } from "../../../revertWithBytes.sol"; @@ -37,6 +38,9 @@ contract BalanceTransferSystem is System, IWorldErrors { revert World_InvalidResourceType(RESOURCE_NAMESPACE, toNamespaceId, toNamespaceId.toString()); } + // Require the namespace to exist + AccessControl.requireExistence(toNamespaceId); + // Require caller to have access to the namespace AccessControl.requireAccess(fromNamespaceId, _msgSender()); diff --git a/packages/world/test/WorldBalance.t.sol b/packages/world/test/WorldBalance.t.sol index 10c44dfd45..3e92b2b4bf 100644 --- a/packages/world/test/WorldBalance.t.sol +++ b/packages/world/test/WorldBalance.t.sol @@ -276,6 +276,32 @@ contract WorldBalanceTest is Test, GasReporter { assertEq(Balances.get(invalidNamespace), 0); } + function testTransferBalanceToNamespaceRevertResourceNotFound() public { + bytes14 toNamespace = "not_registered"; + ResourceId toNamespaceId = WorldResourceIdLib.encodeNamespace(toNamespace); + + uint256 value = 1 ether; + + // Expect the root namespace to have no balance + assertEq(Balances.get(ROOT_NAMESPACE_ID), 0); + + // Send balance to root namespace + vm.deal(caller, value); + vm.prank(caller); + (bool success, bytes memory data) = address(world).call{ value: value }(""); + assertTrue(success); + assertEq(data.length, 0); + + // Expect the root namespace to have the value as balance + assertEq(Balances.get(ROOT_NAMESPACE_ID), value); + + // Expect revert when attempting to transfer to a non-existent namespace + vm.expectRevert( + abi.encodeWithSelector(IWorldErrors.World_ResourceNotFound.selector, toNamespaceId, toNamespaceId.toString()) + ); + world.transferBalanceToNamespace(ROOT_NAMESPACE_ID, toNamespaceId, value); + } + function testTransferBalanceToAddress() public { uint256 value = 1 ether;