-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(world-modules): register delegation with signature (#2480)
Co-authored-by: Karolis Ramanauskas <[email protected]> Co-authored-by: alvarius <[email protected]>
- Loading branch information
1 parent
bfd5a01
commit e86bd14
Showing
23 changed files
with
1,037 additions
and
27 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,9 @@ | ||
--- | ||
"@latticexyz/cli": patch | ||
"@latticexyz/world-modules": patch | ||
"@latticexyz/world": patch | ||
--- | ||
|
||
Added a new preview module, `Unstable_DelegationWithSignatureModule`, which allows registering delegations with a signature. | ||
|
||
Note: this module is marked as `Unstable`, because it will be removed and included in the default `World` deployment once it is audited. |
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
108 changes: 108 additions & 0 deletions
108
e2e/packages/sync-test/data/callRegisterDelegationWithSignature.ts
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,108 @@ | ||
import { Page } from "@playwright/test"; | ||
import { GetContractReturnType, PublicClient, WalletClient } from "viem"; | ||
import { AbiParametersToPrimitiveTypes, ExtractAbiFunction, ExtractAbiFunctionNames } from "abitype"; | ||
|
||
const DelegationAbi = [ | ||
{ | ||
type: "function", | ||
name: "registerDelegationWithSignature", | ||
inputs: [ | ||
{ | ||
name: "delegatee", | ||
type: "address", | ||
internalType: "address", | ||
}, | ||
{ | ||
name: "delegationControlId", | ||
type: "bytes32", | ||
internalType: "ResourceId", | ||
}, | ||
{ | ||
name: "initCallData", | ||
type: "bytes", | ||
internalType: "bytes", | ||
}, | ||
{ | ||
name: "delegator", | ||
type: "address", | ||
internalType: "address", | ||
}, | ||
{ | ||
name: "signature", | ||
type: "bytes", | ||
internalType: "bytes", | ||
}, | ||
], | ||
outputs: [], | ||
stateMutability: "nonpayable", | ||
}, | ||
] as const; | ||
|
||
type DelegationAbi = typeof DelegationAbi; | ||
|
||
type WorldContract = GetContractReturnType<DelegationAbi, PublicClient, WalletClient>; | ||
|
||
type WriteMethodName = ExtractAbiFunctionNames<DelegationAbi>; | ||
type WriteMethod<TMethod extends WriteMethodName> = ExtractAbiFunction<DelegationAbi, TMethod>; | ||
type WriteArgs<TMethod extends WriteMethodName> = AbiParametersToPrimitiveTypes<WriteMethod<TMethod>["inputs"]>; | ||
|
||
export function callRegisterDelegationWithSignature(page: Page, args?: WriteArgs<"registerDelegationWithSignature">) { | ||
return page.evaluate( | ||
([_args]) => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const walletClient = (window as any).walletClient as WalletClient; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const worldContract = (window as any).worldContract as WorldContract; | ||
|
||
return walletClient | ||
.writeContract({ | ||
address: worldContract.address, | ||
abi: [ | ||
{ | ||
type: "function", | ||
name: "registerDelegationWithSignature", | ||
inputs: [ | ||
{ | ||
name: "delegatee", | ||
type: "address", | ||
internalType: "address", | ||
}, | ||
{ | ||
name: "delegationControlId", | ||
type: "bytes32", | ||
internalType: "ResourceId", | ||
}, | ||
{ | ||
name: "initCallData", | ||
type: "bytes", | ||
internalType: "bytes", | ||
}, | ||
{ | ||
name: "delegator", | ||
type: "address", | ||
internalType: "address", | ||
}, | ||
{ | ||
name: "signature", | ||
type: "bytes", | ||
internalType: "bytes", | ||
}, | ||
], | ||
outputs: [], | ||
stateMutability: "nonpayable", | ||
}, | ||
], | ||
functionName: "registerDelegationWithSignature", | ||
args: _args, | ||
}) | ||
.then((tx) => window["waitForTransaction"](tx)) | ||
.catch((error) => { | ||
console.error(error); | ||
throw new Error( | ||
[`Error executing registerDelegationWithSignature with args:`, JSON.stringify(_args), error].join("\n\n"), | ||
); | ||
}); | ||
}, | ||
[args], | ||
); | ||
} |
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,15 @@ | ||
import { Page } from "@playwright/test"; | ||
import IWorldAbi from "../../contracts/out/IWorld.sol/IWorld.abi.json"; | ||
import { GetContractReturnType, PublicClient, WalletClient } from "viem"; | ||
|
||
type WorldAbi = typeof IWorldAbi; | ||
|
||
type WorldContract = GetContractReturnType<WorldAbi, PublicClient, WalletClient>; | ||
|
||
export function getWorld(page: Page) { | ||
return page.evaluate(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const worldContract = (window as any).worldContract as WorldContract; | ||
return worldContract; | ||
}, []); | ||
} |
110 changes: 110 additions & 0 deletions
110
e2e/packages/sync-test/registerDelegationWithSignature.test.ts
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,110 @@ | ||
import { afterEach, beforeEach, describe, expect, it } from "vitest"; | ||
import type { ViteDevServer } from "vite"; | ||
import { Browser, Page } from "@playwright/test"; | ||
import { createAsyncErrorHandler } from "./asyncErrors"; | ||
import { deployContracts, startViteServer, startBrowserAndPage, openClientWithRootAccount } from "./setup"; | ||
import { rpcHttpUrl } from "./setup/constants"; | ||
import { waitForInitialSync } from "./data/waitForInitialSync"; | ||
import { createBurnerAccount, resourceToHex, transportObserver } from "@latticexyz/common"; | ||
import { http, createWalletClient, ClientConfig } from "viem"; | ||
import { mudFoundry } from "@latticexyz/common/chains"; | ||
import { encodeEntity } from "@latticexyz/store-sync/recs"; | ||
import { callPageFunction } from "./data/callPageFunction"; | ||
import worldConfig from "@latticexyz/world/mud.config"; | ||
import { worldToV1 } from "@latticexyz/world/config/v2"; | ||
import { delegationWithSignatureTypes } from "@latticexyz/world/internal"; | ||
import { getWorld } from "./data/getWorld"; | ||
import { callRegisterDelegationWithSignature } from "./data/callRegisterDelegationWithSignature"; | ||
|
||
const DELEGATOR_PRIVATE_KEY = "0x67bbd1575ecc79b3247c7d7b87a5bc533ccb6a63955a9fefdfaf75853f7cd543"; | ||
|
||
const worldConfigV1 = worldToV1(worldConfig); | ||
|
||
describe("registerDelegationWithSignature", async () => { | ||
const asyncErrorHandler = createAsyncErrorHandler(); | ||
let webserver: ViteDevServer; | ||
let browser: Browser; | ||
let page: Page; | ||
|
||
beforeEach(async () => { | ||
asyncErrorHandler.resetErrors(); | ||
|
||
await deployContracts(rpcHttpUrl); | ||
|
||
// Start client and browser | ||
webserver = await startViteServer(); | ||
const browserAndPage = await startBrowserAndPage(asyncErrorHandler.reportError); | ||
browser = browserAndPage.browser; | ||
page = browserAndPage.page; | ||
}); | ||
|
||
afterEach(async () => { | ||
await browser.close(); | ||
await webserver.close(); | ||
}); | ||
|
||
it("can generate a signature and register a delegation", async () => { | ||
await openClientWithRootAccount(page); | ||
await waitForInitialSync(page); | ||
|
||
// Set up client | ||
const clientOptions = { | ||
chain: mudFoundry, | ||
transport: transportObserver(http(mudFoundry.rpcUrls.default.http[0] ?? undefined)), | ||
} as const satisfies ClientConfig; | ||
|
||
const delegator = createBurnerAccount(DELEGATOR_PRIVATE_KEY); | ||
const delegatorWalletClient = createWalletClient({ | ||
...clientOptions, | ||
account: delegator, | ||
}); | ||
|
||
const worldContract = await getWorld(page); | ||
|
||
// Declare delegation parameters | ||
const delegatee = "0x7203e7ADfDF38519e1ff4f8Da7DCdC969371f377"; | ||
const delegationControlId = resourceToHex({ type: "system", namespace: "", name: "unlimited" }); | ||
const initCallData = "0x"; | ||
const nonce = 0n; | ||
|
||
// Sign registration message | ||
const signature = await delegatorWalletClient.signTypedData({ | ||
domain: { | ||
chainId: delegatorWalletClient.chain.id, | ||
verifyingContract: worldContract.address, | ||
}, | ||
types: delegationWithSignatureTypes, | ||
primaryType: "Delegation", | ||
message: { | ||
delegatee, | ||
delegationControlId, | ||
initCallData, | ||
delegator: delegator.address, | ||
nonce, | ||
}, | ||
}); | ||
|
||
// Register the delegation | ||
await callRegisterDelegationWithSignature(page, [ | ||
delegatee, | ||
delegationControlId, | ||
initCallData, | ||
delegator.address, | ||
signature, | ||
]); | ||
|
||
// Expect delegation to have been created | ||
const value = await callPageFunction(page, "getComponentValue", [ | ||
"UserDelegationControl", | ||
encodeEntity(worldConfigV1.tables.UserDelegationControl.keySchema, { | ||
delegator: delegator.address, | ||
delegatee, | ||
}), | ||
]); | ||
|
||
expect(value).toMatchObject({ | ||
__staticData: delegationControlId, | ||
delegationControlId, | ||
}); | ||
}); | ||
}); |
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
23 changes: 23 additions & 0 deletions
23
packages/world-modules/src/interfaces/IUnstable_DelegationWithSignatureSystem.sol
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,23 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.24; | ||
|
||
/* Autogenerated file. Do not edit manually. */ | ||
|
||
import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; | ||
|
||
/** | ||
* @title IUnstable_DelegationWithSignatureSystem | ||
* @author MUD (https://mud.dev) by Lattice (https://lattice.xyz) | ||
* @dev This interface is automatically generated from the corresponding system contract. Do not edit manually. | ||
*/ | ||
interface IUnstable_DelegationWithSignatureSystem { | ||
error InvalidSignature(address signer); | ||
|
||
function registerDelegationWithSignature( | ||
address delegatee, | ||
ResourceId delegationControlId, | ||
bytes memory initCallData, | ||
address delegator, | ||
bytes memory signature | ||
) external; | ||
} |
Oops, something went wrong.