# Grant Application: Authenticated Hooks

**URL:** <https://forum.cow.fi/t/grant-application-authenticated-hooks/2408>\
**Category:** CoW Grants Program\
**Tags:** complete, grant-funded\
**Created:** [April 29, 2024, 12:42pm UTC](https://forum.cow.fi/t/grant-application-authenticated-hooks/2408 "2024-04-29T12:42:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![meetm](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@meetm](https://forum.cow.fi/u/meetm)\
**Post date:** [April 29, 2024, 12:42pm UTC](https://forum.cow.fi/t/grant-application-authenticated-hooks/2408/1 "2024-04-29T12:42:30Z")

</div>

# CoW Grant – Authenticated Hooks

## Author

I am [meetmangukiya](https://github.com/meetmangukiya). I am a software developer, have been in DeFi and web3 space for over 3 years now. I also have familiarity with CoW protocol technicals.

I have also completed the following CoW grants:

1. Automated fee collection - [https://forum.cow.fi/t/cow-grant-automated-fee-withdrawal/](https://forum.cow.fi/t/cow-grant-automated-fee-withdrawal/)

## Grant Category

Other/Misc.

## Grant Description

All hooks, irrespective of the order, are executed from the context of `HooksTrampoline`. This is a requirement as otherwise if executed from `GPv2Settlement`, this would place accumulated protocol fees in direct jeopardy.

This setup works fine when executing hooks that are _permissionless_ (such as `claim` for an airdrop that’s not authenticated on `msg.sender`), and also works for hooks that have self-contained authorisation (e.g. `permit`, where a user signs the permit, in order to be able to set allowances).

This setup is limited in scope to protocols / standards that _already provide some inherent type of signed authentication_, or are _completely permissionless_. This prohibits integrations such CDP management, vault savings strategies etc.

## Grant goals and impact

This grant introduces a solution to this by standardizing a way to sign the hooks calldata such that it will be authenticated on chain and user-specific user-owned accounts will be called with those hooks data.

It also acts a solution to the problem of post hooks not being atomic. So even if the post hook fails, the assets will still be under the custody of the user-owned proxy, and not lie in the HooksTrampoline contract for anyone to steal.

### Scope

To implement a solution that allows for the use of deterministic addresses to be associated with an order owner so that the owner may set the `receiver` to an address that hasn’t be deployed yet, but which they are guaranteed to have control over.

To this end, implement an [EIP-1967](https://eips.ethereum.org/EIPS/eip-1967) proxy factory. The factory should use `CREATE2` to create the proxies with `msg.sender` as the `salt`. E.g.:

The factory should adhere to the interface:

```auto
function executeHooks(Call3[] calldata calls, bytes32 nonce, bytes32 r, bytes32 s, uint8 v);

```

The type of order owner is determined by reference to `v`. If `v == 0`, it is assumed to be a smart contract, otherwise an EOA.

#### EOAs

The hook’s owner is determined by `ecrecover` of `rsv`

#### Smart contracts

The hook’s owner is determined by assuming that `r` is set to the owner’s address. The smart contract is assumed to support [ERC-1271](https://eips.ethereum.org/EIPS/eip-1271).

#### Deterministic proxy deployment

Within the proxy factory, an internal function is used for deployment:

```auto
function _create(address owner) internal {
    bytes32 salt = bytes32(bytes20(owner));
    Proxy proxy = new Proxy{salt: salt}(implementation);
}

```

#### Execution of hooks

When running `executeHooks` in the proxy factory, the proxy factory:

1. Determine’s the order’s owner
2. With (1), calculates the deterministic address for which their proxy should have been deployed to
3. If (2) has a code length of 0, deploy the deterministic contract, with a delegate call to the `implementation`, for `trustedExecuteHooks`.
4. Otherwise, call the proxy address with `trustedExecuteHooks`.

#### Implementation

`implementation` would be a singleton that adheres to the interface:

```auto
interface CoWAuthHook {
    function executeHooks(Call3[] calldata calls, bytes32 nonce, bytes32 r, bytes32 s, uint8 v);
    function trustedExecuteHooks(Call3[] calldata calls, bytes32 nonce);
    function updateTrustedExecutor(address who, bool authorized);
}

```

**NOTE** : `trustedExecuteHooks` should ensure that `msg.sender` is the ProxyFactory as this can then ensure that signature verification has taken place, and is authenticated (no need to do the signature verification again).

#### State handling

Any state contained within the proxy **MUST** be stored using the [Diamond Storage](https://eips.ethereum.org/EIPS/eip-2535#storage) methodology to avoid any potential state conflicts on upgrades by a user.

#### ENS resolver

Provide a forward resolver that will resolve any `<address>` subname of `cowhooks.eth`(example) to the deterministic proxy address for given `<address>`. So, if any user wants to see what is their proxy, they can just search etherscan for `<address>.cowhooks.eth` and get the address they can use as `receiver` for their swap orders, etc.

Also, provide a reverse resolution of `<address>.cowhooks.eth` to `<proxy-address>`. One thing to note is both the forward and reverse resolution won’t happen until the said proxy is deployed, since this logic will be encapsulated in the proxy deployment/initialization logic.

#### Tools

When implementing this solution, the following must be provided:

1. Smart contracts for the `ProxyFactory`, the `implementation` (singleton) and the ENS forward/reverse resolvers.
2. Comprehensive test suite for the smart contracts
3. SDK / Typescript for:  
a. Calculating the deterministic proxy address when deployed via the `ProxyFactory`.  
b. Signing of the `Call3[]` and `nonce` EIP-712 type.  
c. Encoding of the hooks calldata

#### Example to be provided

Use a prehook to borrow DAI on behalf of the user and send it to the user. The order will then pull it from the user’s EOA during the settlement.

### Milestones

1. Contracts and tests - 2 weeks
2. SDK and usage example - 1 weeks

### Funding request

1. Upfront - 5000 xDAI
2. Upon completion - 5000 xDAI
3. Upon completion - 10000 COW, vesting over 12 months.

All of the budget going towards development.

### Gnosis Chain Address

`gno:0xd33d055ea52ec5a685995d40f19fa98d0744df0c`

### Terms and Conditions

By submitting this grant application, I acknowledge and agree to be bound by the CoW DAO Participation Agreement and the CoW Grant Terms and Conditions.

---

<div class="post-metadata">

**Author:** ![netrunner.eth](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/netrunner.eth/32/252_2.png) [@netrunner.eth](https://forum.cow.fi/u/netrunner.eth)\
**Post date:** [April 30, 2024, 12:48pm UTC](https://forum.cow.fi/t/grant-application-authenticated-hooks/2408/2 "2024-04-30T12:48:30Z")

</div>

Thank you for submitting your grant proposal. This will require technical review to determine impact and value.

---

<div class="post-metadata">

**Author:** ![mfw78](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/mfw78/32/1598_2.png) [@mfw78](https://forum.cow.fi/u/mfw78)\
**Post date:** [April 30, 2024, 1:10pm UTC](https://forum.cow.fi/t/grant-application-authenticated-hooks/2408/3 "2024-04-30T13:10:20Z")

</div>

This grant would be great for smoothing out the developer experience and reasoning required when authoring hooks. The main amendment I would seek is to have additional examples:

1. Swap and bridge (Ethereum → Gnosis Chain and Gnosis Chain → Ethereum), i.e. 2 examples here showing both ways. This would be to showcase use with deterministic receivers and post-hooks.
2. Prehook for leveraging a DAI position (showcasing authorisation functionality of the deterministically deployed proxy).

With this scope included, I’m generally in favour of this grant.

---

<div class="post-metadata">

**Author:** ![meetm](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@meetm](https://forum.cow.fi/u/meetm)\
**Post date:** [April 30, 2024, 1:55pm UTC](https://forum.cow.fi/t/grant-application-authenticated-hooks/2408/4 "2024-04-30T13:55:06Z")

</div>

Prehook for leveraging DAI already included in the original post. Will add an additional example for swap and bridge too, but can’t edit the post.

---

<div class="post-metadata">

**Author:** ![sohkai](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/sohkai/32/362_2.png) [@sohkai](https://forum.cow.fi/u/sohkai)\
**Post date:** [April 30, 2024, 4:43pm UTC](https://forum.cow.fi/t/grant-application-authenticated-hooks/2408/5 "2024-04-30T16:43:28Z")

</div>

This looks great and an important addition to improve the possibilities achievable with hooks. Am in favour of this grant.

---

<div class="post-metadata">

**Author:** ![anxolin](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/anxolin/32/275_2.png) [@anxolin](https://forum.cow.fi/u/anxolin)\
**Post date:** [May 1, 2024, 1:20pm UTC](https://forum.cow.fi/t/grant-application-authenticated-hooks/2408/6 "2024-05-01T13:20:09Z")

</div>

I really vouch for this development. I believe that this framework and SDK would help with hook adoption.

We will also incorporate a twin feature soon for allowing third parties to extend CoW Swap with arbitrary DEFI interactions as pre/post hooks. I’m talking about the Hook Store. This framework together with the Hook Store could help the adoption of hooks so it’s easier to add more interesting DEFI interactions.

I’d love to see a sound SWAP+BRIDGE example with this framework!

---

<div class="post-metadata">

**Author:** ![mfw78](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/mfw78/32/1598_2.png) [@mfw78](https://forum.cow.fi/u/mfw78)\
**Post date:** [May 2, 2024, 7:42am UTC](https://forum.cow.fi/t/grant-application-authenticated-hooks/2408/7 "2024-05-02T07:42:08Z")

</div>

I think this grant can be moved to a snapshot vote.

---

<div class="post-metadata">

**Author:** ![meetm](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@meetm](https://forum.cow.fi/u/meetm)\
**Post date:** [May 30, 2024, 10:07am UTC](https://forum.cow.fi/t/grant-application-authenticated-hooks/2408/8 "2024-05-30T10:07:49Z")

</div>

Implemented at: [GitHub - cowdao-grants/cow-shed](https://github.com/cowdao-grants/cow-shed)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/system/32/1733_2.png) [@system](https://forum.cow.fi/u/system)\
**Post date:** [November 24, 2025, 1:54pm UTC](https://forum.cow.fi/t/grant-application-authenticated-hooks/2408/9 "2025-11-24T13:54:12Z")

</div>


