@etherplay/delegation 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +135 -0
- package/contracts/Delegation.sol +465 -0
- package/contracts/IDelegation.sol +76 -0
- package/contracts/UsingDelegation.sol +163 -0
- package/contracts/utils/Payments.sol +63 -0
- package/contracts/utils/SignatureUtils.sol +107 -0
- package/contracts/utils/StringUtils.sol +66 -0
- package/dist/abi.d.ts +178 -0
- package/dist/abi.d.ts.map +1 -0
- package/dist/abi.js +143 -0
- package/dist/abi.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/message.d.ts +37 -0
- package/dist/message.d.ts.map +1 -0
- package/dist/message.js +52 -0
- package/dist/message.js.map +1 -0
- package/package.json +65 -2
- package/src/abi.ts +142 -0
- package/src/index.ts +13 -0
- package/src/message.ts +76 -0
- package/vectors.json +55 -0
package/README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# @etherplay/delegation
|
|
2
|
+
|
|
3
|
+
Onchain delegation: an account authorises other addresses to act in its name, **at one contract**.
|
|
4
|
+
|
|
5
|
+
The problem it solves: an app that acts on its user's behalf sends from a key of its own, so the address that signs a transaction is not the account the action belongs to. A contract that records `msg.sender` records the wrong one.
|
|
6
|
+
|
|
7
|
+
This package ships one feature with three faces, and they are here together on purpose:
|
|
8
|
+
|
|
9
|
+
| face | where | who consumes it |
|
|
10
|
+
| ------------------------------ | -------------------------------- | ------------------------------------------------------- |
|
|
11
|
+
| the Solidity library | `contracts/` | your contract, compiled in from `node_modules` |
|
|
12
|
+
| the TypeScript message builder | `src/message.ts` (package entry) | whatever produces the signature, e.g. a wallet |
|
|
13
|
+
| the ABI | `src/abi.ts` (package entry) | a client calling a contract it does not compile against |
|
|
14
|
+
|
|
15
|
+
They are together because what breaks is the agreement between them, and it breaks silently: a signature over a message that differs by one byte does not fail loudly, it recovers a different address. Co-located, the test that pins them runs on every change to either. That is `vectors.json`, read from both languages.
|
|
16
|
+
|
|
17
|
+
## There is nothing to deploy
|
|
18
|
+
|
|
19
|
+
**This package ships source.** Each adopter compiles the library into its own contract and owns its own delegations.
|
|
20
|
+
|
|
21
|
+
A shared `DelegationRegistry` that many contracts point at is not a variation on this design, it is its opposite. The verifying contract named in every signature would be that registry, so a credential granted for one game would be valid at every game on it, which is precisely the unbounded authority this design exists to remove.
|
|
22
|
+
|
|
23
|
+
## Solidity
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pnpm add -D @etherplay/delegation
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The standard shape, ready to inherit:
|
|
30
|
+
|
|
31
|
+
```solidity
|
|
32
|
+
import {UsingDelegation} from '@etherplay/delegation/contracts/UsingDelegation.sol';
|
|
33
|
+
|
|
34
|
+
contract MyGame is UsingDelegation {
|
|
35
|
+
mapping(address account => uint256) public scores;
|
|
36
|
+
|
|
37
|
+
function score(address onBehalfOf, uint256 points) external {
|
|
38
|
+
// where you would have written msg.sender
|
|
39
|
+
scores[_requireAccountForSender(onBehalfOf)] += points;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
That is the whole adoption. `UsingDelegation` declares **no storage of its own** (the library keeps its state in an ERC-7201 namespaced region), so it adds nothing to your layout and shifts nothing you already have, which is what makes it safe to add to a contract already live behind a proxy.
|
|
45
|
+
|
|
46
|
+
It gives your contract six external functions:
|
|
47
|
+
|
|
48
|
+
```solidity
|
|
49
|
+
function registerDelegate(address delegate, address payable payee) external payable;
|
|
50
|
+
function registerDelegateViaSignature(
|
|
51
|
+
address owner,
|
|
52
|
+
address delegate,
|
|
53
|
+
uint256 deadline,
|
|
54
|
+
bytes calldata signature
|
|
55
|
+
) external payable;
|
|
56
|
+
function revokeDelegate(address delegate) external;
|
|
57
|
+
function delegationStatus(address owner, address delegate) external view returns (bool allowed, bool withdrawn);
|
|
58
|
+
function delegationMessage(address delegate, uint256 deadline) external view returns (string memory);
|
|
59
|
+
function delegationDigest(address delegate, uint256 deadline) external view returns (bytes32);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
If you want fewer, or different ones, use `Delegation` (the library) directly and write your own; `UsingDelegation` is only those six written once. `IDelegation` is the same set as a type, for a router composing a selector list or a caller that does not compile against you.
|
|
63
|
+
|
|
64
|
+
**What you are handing over:** a delegate authorised at your contract may do anything at your contract that its owner could do through the paths you resolve with `_requireAccountForSender`. Not a scope, not one action. It can do nothing anywhere else. If that is more than you mean to grant, narrow it in the override.
|
|
65
|
+
|
|
66
|
+
**Enumeration is the event.** There is no onchain list of an account's delegates; `DelegationChanged(owner, delegate, allowed)` is indexed on both addresses, and the set is reconstructed by replaying it. Note that many public RPCs cap `eth_getLogs` ranges, so a browser reconstructing from genesis wants a stored deployment block and paging.
|
|
67
|
+
|
|
68
|
+
## TypeScript
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import {delegationMessage, delegationDigest, DELEGATION_ABI} from '@etherplay/delegation';
|
|
72
|
+
|
|
73
|
+
const message = delegationMessage({
|
|
74
|
+
delegate: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
|
|
75
|
+
contract: '0xe7f1725e7734ce288f8367e1bb143e90bb3f0512',
|
|
76
|
+
chainId: 31337,
|
|
77
|
+
deadline: 1767225600, // unix seconds; 0 means no expiry
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const signature = await owner.signMessage({message}); // EIP-191 personal_sign
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Anyone can then submit it, and pays for it:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
await client.writeContract({
|
|
87
|
+
address: contract,
|
|
88
|
+
abi: DELEGATION_ABI,
|
|
89
|
+
functionName: 'registerDelegateViaSignature',
|
|
90
|
+
args: [owner, delegate, deadline, signature],
|
|
91
|
+
value: fundingForTheDelegate,
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
An owner that can sign but cannot send, or holds nothing, can still delegate.
|
|
96
|
+
|
|
97
|
+
**The owner must be a key, not a contract.** `registerDelegateViaSignature` verifies with `ecrecover` and has no ERC-1271 `isValidSignature` fallback, so a smart account cannot register that way. It is not locked out: it calls `registerDelegate` directly, which proves who is asking by who is sending. The two paths suit opposite shapes, and the signature one exists for a signer that can sign and cannot send, which is precisely what a contract account is not.
|
|
98
|
+
|
|
99
|
+
## The message
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
IMPORTANT: Only sign this on a site you trust.
|
|
103
|
+
|
|
104
|
+
This authorizes another address to act in your name onchain, at one contract.
|
|
105
|
+
You can withdraw it at any time by revoking it there.
|
|
106
|
+
|
|
107
|
+
Delegate: 0x70997970c51812dc3a010c7d01b50e0d17dc79c8
|
|
108
|
+
Contract: 0xe7f1725e7734ce288f8367e1bb143e90bb3f0512
|
|
109
|
+
Chain ID: 31337
|
|
110
|
+
Expires: 1767225600
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Readable text rather than EIP-712, because this is about to be shown to a human. Prose first, so the first thing read is what is being agreed to; everything that matters in labelled fields, so a wallet has one extraction strategy rather than two. Both addresses lowercase. `Expires: never` for a deadline of zero, as a line rather than an omission.
|
|
114
|
+
|
|
115
|
+
The contract and the chain come from `address(this)` and `block.chainid`, never from the caller: they are the bound the design rests on. The deadline is passed in calldata because the contract cannot know it, which is safe because it is not trusted, only recovered against.
|
|
116
|
+
|
|
117
|
+
There is no origin and no version field. A wallet always knows the true origin of the page asking, and the delegate address already carries the origin binding by being a pure function of (account key, origin). Note that the absent leading `Origin:` line is deliberate: under the etherplay convention that prefix means "safe to sign without asking", which is exactly the property being removed here.
|
|
118
|
+
|
|
119
|
+
## `vectors.json`
|
|
120
|
+
|
|
121
|
+
Not a fixture. It is the specification: `(delegate, contract, chainId, deadline)` with the exact message and its EIP-191 digest, covering both deadline branches, chain ids of 1 and 31337, and checksummed inputs that must render lowercase.
|
|
122
|
+
|
|
123
|
+
Three implementations must agree on these bytes: `contracts/Delegation.sol`, `src/message.ts`, and whatever a third-party wallet writes. **Never change one without the others and the vectors, in the same commit.** A change here invalidates every signature ever generated.
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
pnpm test # hardhat: the Solidity suite and the TypeScript one, both against vectors.json
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Deadlines
|
|
130
|
+
|
|
131
|
+
Zero means no expiry, and is right for a credential a human was prompted for: refreshing one costs a popup and re-consent in the middle of a game.
|
|
132
|
+
|
|
133
|
+
A credential minted with **no human in the loop** should carry a real deadline. It is the only lever anyone has if the decision to mint it turns out to be wrong: removing whatever authorised it stops future minting and reaches nobody who already holds one.
|
|
134
|
+
|
|
135
|
+
The deadline bounds how long the credential may be **presented**, not how long the authority lasts. Once registered, a delegate stands until it is revoked.
|
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.4;
|
|
3
|
+
|
|
4
|
+
import {StringUtils} from "./utils/StringUtils.sol";
|
|
5
|
+
import {SignatureUtils} from "./utils/SignatureUtils.sol";
|
|
6
|
+
|
|
7
|
+
/// @title Delegation
|
|
8
|
+
/// @notice Lets an account authorise other addresses to act in its name, at
|
|
9
|
+
/// the contract that adopts this library and nowhere else.
|
|
10
|
+
///
|
|
11
|
+
/// The problem it solves: an app that acts on its user's behalf sends from a
|
|
12
|
+
/// key of its own, so the address that signs a transaction is not the account
|
|
13
|
+
/// the action belongs to. A contract that records `msg.sender` records the
|
|
14
|
+
/// wrong one, and the user sees one address in the app and another against
|
|
15
|
+
/// their actions, with nothing onchain connecting them.
|
|
16
|
+
///
|
|
17
|
+
/// THE UNIT OF AUTHORITY IS ONE CONTRACT: yours. A delegate authorised here may
|
|
18
|
+
/// act for its owner here, and an identical signature is worth nothing at any
|
|
19
|
+
/// other contract, because the contract's own address is part of what was
|
|
20
|
+
/// signed. There is no scope, no capability and no per-route key: this library
|
|
21
|
+
/// answers "whose action is this", which is identity, not permission. An
|
|
22
|
+
/// adopter that needs narrower authority than "the whole contract" has a seam
|
|
23
|
+
/// for it in {UsingDelegation-_requireAccountForSender}, which is more
|
|
24
|
+
/// expressive than a storage key and costs nothing when unused.
|
|
25
|
+
///
|
|
26
|
+
/// A SET, NOT A SLOT: an owner may have any number of delegates at once, so a
|
|
27
|
+
/// second front-end does not evict the first, and withdrawing one signer is not
|
|
28
|
+
/// entangled with replacing another. The membership check costs exactly what an
|
|
29
|
+
/// equality check did.
|
|
30
|
+
///
|
|
31
|
+
/// A LIBRARY, NOT A BASE CONTRACT, and deliberately. Inheriting would give a
|
|
32
|
+
/// contract external functions that appear in its ABI without appearing in its
|
|
33
|
+
/// source, which is the opposite of what a contract you are about to deploy
|
|
34
|
+
/// should do, and it would stop an adopter declining a path it does not want.
|
|
35
|
+
/// As a library neither applies: you write your own entry points, and you can
|
|
36
|
+
/// see all of them.
|
|
37
|
+
///
|
|
38
|
+
/// The price is that you write those entry points, and they are lines that say
|
|
39
|
+
/// exactly what your contract exposes. If the standard shape is what you want,
|
|
40
|
+
/// {UsingDelegation} is that same set written once and ready to inherit - read
|
|
41
|
+
/// it for the full set - and inheriting it is safe for the same reason this is
|
|
42
|
+
/// a library at all, since the namespaced storage below means it declares no
|
|
43
|
+
/// state and so shifts none of yours.
|
|
44
|
+
///
|
|
45
|
+
/// NOT A REGISTRY, and this is the one mistake that undoes everything above.
|
|
46
|
+
/// "A registry" is the obvious noun for what this looks like, and a shared
|
|
47
|
+
/// `DelegationRegistry` that many contracts point at is not a variation on this
|
|
48
|
+
/// design, it is its opposite: the verifying contract in every signature would
|
|
49
|
+
/// be that registry, so every credential granted for one game would be valid
|
|
50
|
+
/// at every game on it, which is exactly the unbounded authority the contract
|
|
51
|
+
/// field exists to remove. This package ships SOURCE. Each adopter compiles it
|
|
52
|
+
/// into its own contract and owns its own delegations.
|
|
53
|
+
///
|
|
54
|
+
/// STORAGE IS NAMESPACED (ERC-7201), so this owns a region nothing else can
|
|
55
|
+
/// collide with, at a slot derived from a name rather than from a position.
|
|
56
|
+
/// A contract already live behind a proxy can therefore adopt delegation on an
|
|
57
|
+
/// upgrade without disturbing the layout it already has.
|
|
58
|
+
///
|
|
59
|
+
/// Every function is `internal`, so they inline into your contract: no library
|
|
60
|
+
/// deployment, no linking, no `delegatecall`, and `msg.sender` is still the
|
|
61
|
+
/// caller you expect. It is also what makes `address(this)` in the signed text
|
|
62
|
+
/// below mean YOUR contract rather than some library address.
|
|
63
|
+
library Delegation {
|
|
64
|
+
/// @notice the standing of one (owner, delegate) pair.
|
|
65
|
+
///
|
|
66
|
+
/// The three states are exclusive, which is why one word per pair is
|
|
67
|
+
/// faithful rather than a compression of two flags: {revoke} sets
|
|
68
|
+
/// `Withdrawn` and removes authority in the same write, an owner-sent
|
|
69
|
+
/// {register} clears `Withdrawn` because it is a fresh decision, and the
|
|
70
|
+
/// signature path refuses to cross it.
|
|
71
|
+
///
|
|
72
|
+
/// `None` is the zero value, so an untouched pair reads as no authority
|
|
73
|
+
/// without anything ever having been written.
|
|
74
|
+
enum Status {
|
|
75
|
+
None,
|
|
76
|
+
Allowed,
|
|
77
|
+
Withdrawn
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/// @notice emitted whenever a (owner, delegate) pair changes standing.
|
|
81
|
+
///
|
|
82
|
+
/// THIS EVENT IS THE ENUMERATION API. There is no onchain list of an
|
|
83
|
+
/// account's delegates - a linked set was costed and rejected, since few
|
|
84
|
+
/// delegates are expected and signer management belongs to the application
|
|
85
|
+
/// rather than to the library - so reconstructing the set means replaying
|
|
86
|
+
/// these logs in order. Both addresses are indexed, which gives a
|
|
87
|
+
/// per-account `eth_getLogs` query with no indexer; the flag is not, since
|
|
88
|
+
/// nobody filters on it.
|
|
89
|
+
///
|
|
90
|
+
/// One event rather than an authorise/revoke pair, because a replay wants
|
|
91
|
+
/// both kinds in order and a single topic0 is one filter and one decode.
|
|
92
|
+
///
|
|
93
|
+
/// The wart, and it belongs to the app rather than to this library: many
|
|
94
|
+
/// public and wallet-supplied RPCs cap `eth_getLogs` ranges, so a fresh
|
|
95
|
+
/// browser reconstructing from genesis needs a stored deployment block and
|
|
96
|
+
/// paging.
|
|
97
|
+
///
|
|
98
|
+
/// @param owner the account being represented
|
|
99
|
+
/// @param delegate the address whose standing changed
|
|
100
|
+
/// @param allowed true when it may now act for `owner`, false when its
|
|
101
|
+
/// authorisation was withdrawn
|
|
102
|
+
event DelegationChanged(
|
|
103
|
+
address indexed owner,
|
|
104
|
+
address indexed delegate,
|
|
105
|
+
bool allowed
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
/// @notice `sender` is not a delegate of `owner` at this contract
|
|
109
|
+
error NotDelegate(address owner, address sender);
|
|
110
|
+
|
|
111
|
+
/// @notice the owner withdrew its authorisation for `delegate`; only the
|
|
112
|
+
/// owner itself can authorise that delegate again (see {revoke})
|
|
113
|
+
error DelegationWithdrawn(address owner, address delegate);
|
|
114
|
+
|
|
115
|
+
/// @notice the signature was not produced by `owner`, or was produced for a
|
|
116
|
+
/// different delegate, deadline, contract or chain
|
|
117
|
+
error InvalidSignature();
|
|
118
|
+
|
|
119
|
+
/// @notice a delegate must be a real address; use {revoke} to withdraw one
|
|
120
|
+
error InvalidDelegate();
|
|
121
|
+
|
|
122
|
+
/// @notice the signature named a deadline that has already passed
|
|
123
|
+
error SignatureExpired(uint256 deadline);
|
|
124
|
+
|
|
125
|
+
/// @custom:storage-location erc7201:etherplay.storage.Delegation
|
|
126
|
+
struct Layout {
|
|
127
|
+
/// owner => delegate => whether that delegate may act for that owner
|
|
128
|
+
/// here, has been withdrawn, or was never authorised.
|
|
129
|
+
///
|
|
130
|
+
/// Keyed by OWNER first, deliberately. The reverse mapping would let
|
|
131
|
+
/// anyone claim someone else's delegate address (which is public the
|
|
132
|
+
/// moment that delegate sends anything) and have that owner's actions
|
|
133
|
+
/// attributed to the claimer. Keyed this way, the worst an attacker
|
|
134
|
+
/// achieves is making their OWN account answer to an address they do
|
|
135
|
+
/// not control, which harms nobody.
|
|
136
|
+
///
|
|
137
|
+
/// The inner key is the DELEGATE, so withdrawing one signer says
|
|
138
|
+
/// nothing about any other: a registration signature is permanently
|
|
139
|
+
/// derivable by whatever produced it, and a per-owner withdrawal would
|
|
140
|
+
/// make it impossible to add a new signer by signature alone once any
|
|
141
|
+
/// old one had been withdrawn.
|
|
142
|
+
mapping(address owner => mapping(address delegate => Status)) status;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/// keccak256(abi.encode(uint256(keccak256("etherplay.storage.Delegation")) - 1)) & ~bytes32(uint256(0xff))
|
|
146
|
+
bytes32 internal constant STORAGE_LOCATION =
|
|
147
|
+
0xe3a6fc8979f365f798ce92a5c63aa15935638389b80320c807252f142f884200;
|
|
148
|
+
|
|
149
|
+
/// @dev this library's storage, inside the CALLING contract.
|
|
150
|
+
///
|
|
151
|
+
/// Internal library functions are inlined rather than `delegatecall`ed, so
|
|
152
|
+
/// the slot below resolves against the caller's own storage. That is what
|
|
153
|
+
/// lets a library own state without having any.
|
|
154
|
+
///
|
|
155
|
+
/// PRIVATE, so an adopting contract cannot reach past the functions below
|
|
156
|
+
/// and write the mapping directly. `internal` would hand every adopter a
|
|
157
|
+
/// route around the zero-delegate check, the withdrawn state and the event,
|
|
158
|
+
/// which is a wide hole to leave in something meant to be handed to people
|
|
159
|
+
/// writing their own contracts. Nothing outside this library needs it.
|
|
160
|
+
function layout() private pure returns (Layout storage $) {
|
|
161
|
+
assembly {
|
|
162
|
+
$.slot := STORAGE_LOCATION
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ------------------------------------------------------------------------
|
|
167
|
+
// READING
|
|
168
|
+
// ------------------------------------------------------------------------
|
|
169
|
+
|
|
170
|
+
/// @notice the standing of one (owner, delegate) pair, as two flags.
|
|
171
|
+
///
|
|
172
|
+
/// Two bools rather than the {Status} value itself, at the same single
|
|
173
|
+
/// SLOAD. It keeps an enum out of every interface, ABI and client that
|
|
174
|
+
/// would otherwise have to remember whether 2 means withdrawn, and both
|
|
175
|
+
/// questions an app has - may this signer act, and do I need a transaction
|
|
176
|
+
/// from the owner to fix it - are answered by one call.
|
|
177
|
+
///
|
|
178
|
+
/// @return allowed whether `delegate` may act for `owner` here
|
|
179
|
+
/// @return withdrawn whether `owner` withdrew `delegate`, which blocks
|
|
180
|
+
/// {registerViaSignature} for that delegate until `owner`
|
|
181
|
+
/// authorises it again itself
|
|
182
|
+
function statusOf(
|
|
183
|
+
address owner,
|
|
184
|
+
address delegate
|
|
185
|
+
) internal view returns (bool allowed, bool withdrawn) {
|
|
186
|
+
Status current = layout().status[owner][delegate];
|
|
187
|
+
return (current == Status.Allowed, current == Status.Withdrawn);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/// @notice whether `sender` may act as `onBehalfOf`.
|
|
191
|
+
///
|
|
192
|
+
/// Asks; does not enforce. For a view a UI can call before offering an
|
|
193
|
+
/// action, and for a call site that wants to do something other than revert.
|
|
194
|
+
/// Anyone may always act as themselves, and a zero `onBehalfOf` means
|
|
195
|
+
/// exactly that, so neither costs an SLOAD.
|
|
196
|
+
///
|
|
197
|
+
/// @param sender normally `msg.sender`, but see UsingDelegation for the
|
|
198
|
+
/// case where a contract resolves its effective sender differently
|
|
199
|
+
/// @param onBehalfOf the account being claimed, or zero for none
|
|
200
|
+
function canActFor(
|
|
201
|
+
address sender,
|
|
202
|
+
address onBehalfOf
|
|
203
|
+
) internal view returns (bool) {
|
|
204
|
+
if (onBehalfOf == address(0) || onBehalfOf == sender) {
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
return layout().status[onBehalfOf][sender] == Status.Allowed;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/// @notice the account this action belongs to, or revert.
|
|
211
|
+
///
|
|
212
|
+
/// THE FUNCTION YOUR CONTRACT ACTUALLY USES. Call it wherever you would
|
|
213
|
+
/// have taken `msg.sender` as the identity an action belongs to, and record
|
|
214
|
+
/// what it returns. A zero `onBehalfOf` means "acting for myself", so an
|
|
215
|
+
/// entry point can pass one through unconditionally and behave exactly as
|
|
216
|
+
/// it did before delegation existed.
|
|
217
|
+
///
|
|
218
|
+
/// NAMED FOR THE ENFORCEMENT, not just the lookup: it reverts on an
|
|
219
|
+
/// unauthorised claim rather than falling back to the sender. Somebody
|
|
220
|
+
/// trying to act as somebody else is not a request to act as themselves,
|
|
221
|
+
/// and quietly recording it that way would store an action nobody asked
|
|
222
|
+
/// for. Use {canActFor} when a question rather than a requirement is what
|
|
223
|
+
/// you want.
|
|
224
|
+
///
|
|
225
|
+
/// @param sender normally `msg.sender`, but see UsingDelegation for the
|
|
226
|
+
/// case where a contract resolves its effective sender differently
|
|
227
|
+
/// @param onBehalfOf the account being claimed, or zero for none
|
|
228
|
+
function requireAccountFor(
|
|
229
|
+
address sender,
|
|
230
|
+
address onBehalfOf
|
|
231
|
+
) internal view returns (address) {
|
|
232
|
+
if (!canActFor(sender, onBehalfOf)) {
|
|
233
|
+
revert NotDelegate(onBehalfOf, sender);
|
|
234
|
+
}
|
|
235
|
+
return onBehalfOf == address(0) ? sender : onBehalfOf;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// ------------------------------------------------------------------------
|
|
239
|
+
// GRANTING
|
|
240
|
+
// ------------------------------------------------------------------------
|
|
241
|
+
|
|
242
|
+
/// @notice authorise `delegate` to act for `owner` here, on `owner`'s own
|
|
243
|
+
/// say-so.
|
|
244
|
+
///
|
|
245
|
+
/// AUTHORISES NOTHING ITSELF: the adopting contract must already have
|
|
246
|
+
/// established that `owner` is the one asking, normally by passing
|
|
247
|
+
/// `msg.sender`. It also CLEARS a previous withdrawal of `delegate`, which
|
|
248
|
+
/// is only sound BECAUSE the owner is acting directly - that is a fresh
|
|
249
|
+
/// decision, not a signature presented again - so passing anything else
|
|
250
|
+
/// here would let one account undo another's revocation.
|
|
251
|
+
///
|
|
252
|
+
/// @param owner the account authorising, i.e. `msg.sender`
|
|
253
|
+
/// @param delegate the address to authorise; use {revoke} to withdraw it
|
|
254
|
+
function register(address owner, address delegate) internal {
|
|
255
|
+
if (delegate == address(0)) {
|
|
256
|
+
revert InvalidDelegate();
|
|
257
|
+
}
|
|
258
|
+
layout().status[owner][delegate] = Status.Allowed;
|
|
259
|
+
emit DelegationChanged(owner, delegate, true);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/// @notice authorise `delegate` to act for `owner` here, proven by
|
|
263
|
+
/// `owner`'s signature, whoever is sending the transaction.
|
|
264
|
+
///
|
|
265
|
+
/// The point: an owner that can sign but cannot send, or that holds no
|
|
266
|
+
/// funds, can still delegate. It signs, somebody else submits and pays.
|
|
267
|
+
///
|
|
268
|
+
/// The signature carries no nonce, on purpose. It grants a standing
|
|
269
|
+
/// authorisation to one named address at one named contract on one named
|
|
270
|
+
/// chain, so presenting it a second time only re-asserts what is already
|
|
271
|
+
/// true, at the submitter's expense. The one thing repetition could undo is
|
|
272
|
+
/// a revocation, which is why {revoke} leaves a state this refuses to cross
|
|
273
|
+
/// for THAT delegate only - a different delegate can still be authorised by
|
|
274
|
+
/// a fresh signature.
|
|
275
|
+
///
|
|
276
|
+
/// WHAT IS AND IS NOT CALLER-SUPPLIED. The contract and the chain come from
|
|
277
|
+
/// `address(this)` and `block.chainid` and can never be passed in, because
|
|
278
|
+
/// they are the bounds the whole design rests on and a caller-supplied
|
|
279
|
+
/// bound is no bound at all. The deadline HAS to be passed in: it was
|
|
280
|
+
/// chosen by whoever produced the signature and this contract has no other
|
|
281
|
+
/// way to learn it. That is safe because it is not trusted - a deadline
|
|
282
|
+
/// that does not match the one that was signed simply recovers a different
|
|
283
|
+
/// address, and lying about it in either direction fails, since a later
|
|
284
|
+
/// deadline breaks the signature and an earlier one only expires sooner.
|
|
285
|
+
///
|
|
286
|
+
/// THE OWNER MUST BE A KEY, not a contract. Verification is `ecrecover`, so
|
|
287
|
+
/// an ERC-1271 smart account cannot register this way and there is no
|
|
288
|
+
/// `isValidSignature` fallback. Such an account is not locked out, it takes
|
|
289
|
+
/// the other path: {register}, which proves who is asking by who is sending.
|
|
290
|
+
/// The two suit opposite shapes, and this one exists for a signer that can
|
|
291
|
+
/// sign and cannot send, which is the thing a contract account is not.
|
|
292
|
+
///
|
|
293
|
+
/// WHAT THE DEADLINE BOUNDS is how long the credential may be PRESENTED,
|
|
294
|
+
/// not how long the authority lasts. Once registered, a delegate stands
|
|
295
|
+
/// until it is revoked. That is the honest reading of a standing
|
|
296
|
+
/// authorisation, and it is why a deadline is worth having anyway: it is
|
|
297
|
+
/// the only bound on a credential that was minted without a human in the
|
|
298
|
+
/// loop and has since turned out to be a mistake.
|
|
299
|
+
///
|
|
300
|
+
/// @param owner the account being represented
|
|
301
|
+
/// @param delegate the address to authorise
|
|
302
|
+
/// @param deadline the unix time after which this signature stops being
|
|
303
|
+
/// accepted, or zero for no expiry
|
|
304
|
+
/// @param signature `owner`'s signature over {message}
|
|
305
|
+
function registerViaSignature(
|
|
306
|
+
address owner,
|
|
307
|
+
address delegate,
|
|
308
|
+
uint256 deadline,
|
|
309
|
+
bytes calldata signature
|
|
310
|
+
) internal {
|
|
311
|
+
if (delegate == address(0)) {
|
|
312
|
+
revert InvalidDelegate();
|
|
313
|
+
}
|
|
314
|
+
if (deadline != 0 && block.timestamp > deadline) {
|
|
315
|
+
revert SignatureExpired(deadline);
|
|
316
|
+
}
|
|
317
|
+
Layout storage $ = layout();
|
|
318
|
+
if ($.status[owner][delegate] == Status.Withdrawn) {
|
|
319
|
+
revert DelegationWithdrawn(owner, delegate);
|
|
320
|
+
}
|
|
321
|
+
if (
|
|
322
|
+
SignatureUtils.recover(digest(delegate, deadline), signature) !=
|
|
323
|
+
owner
|
|
324
|
+
) {
|
|
325
|
+
revert InvalidSignature();
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
$.status[owner][delegate] = Status.Allowed;
|
|
329
|
+
emit DelegationChanged(owner, delegate, true);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/// @notice withdraw `owner`'s authorisation of `delegate`: it can no longer
|
|
333
|
+
/// act here, and no signature can put THAT delegate back.
|
|
334
|
+
///
|
|
335
|
+
/// AUTHORISES NOTHING ITSELF: the adopting contract must already have
|
|
336
|
+
/// established that `owner` is the one asking, normally by passing
|
|
337
|
+
/// `msg.sender`.
|
|
338
|
+
///
|
|
339
|
+
/// Takes the delegate as an argument, because an owner may have several and
|
|
340
|
+
/// "withdraw everything" is not what someone replacing one signer means.
|
|
341
|
+
/// Withdrawing an address that was never authorised is allowed and is not a
|
|
342
|
+
/// no-op: it pre-empts a signature that may already exist for it, which is a
|
|
343
|
+
/// real case, since a credential is derivable by whatever produced it long
|
|
344
|
+
/// before anyone submits it.
|
|
345
|
+
///
|
|
346
|
+
/// The zero delegate is the one exception, and is refused. Neither register
|
|
347
|
+
/// path can ever have accepted it, so there is nothing to withdraw and no
|
|
348
|
+
/// signature to pre-empt: it can only be a caller passing an address it
|
|
349
|
+
/// failed to set. Refusing costs one comparison and keeps a meaningless
|
|
350
|
+
/// entry out of {DelegationChanged}, which matters more than it sounds,
|
|
351
|
+
/// because that log IS the enumeration API and an app replaying it would
|
|
352
|
+
/// have to know to ignore the entry.
|
|
353
|
+
///
|
|
354
|
+
/// One-way as far as signatures are concerned, deliberately. Re-authorising
|
|
355
|
+
/// the SAME delegate takes a transaction from the owner ({register}),
|
|
356
|
+
/// because a signature that carries no nonce can state a standing
|
|
357
|
+
/// authorisation but cannot express a decision to reverse one, and reading
|
|
358
|
+
/// it as such would let an old signature outrank a newer intent. A DIFFERENT
|
|
359
|
+
/// delegate can still be authorised by a fresh signature.
|
|
360
|
+
///
|
|
361
|
+
/// This is withdrawal of consent, not key rotation. If a delegate key leaks
|
|
362
|
+
/// there is nothing to rotate to that the same leak would not also expose,
|
|
363
|
+
/// so the useful response is to stop.
|
|
364
|
+
function revoke(address owner, address delegate) internal {
|
|
365
|
+
if (delegate == address(0)) {
|
|
366
|
+
revert InvalidDelegate();
|
|
367
|
+
}
|
|
368
|
+
layout().status[owner][delegate] = Status.Withdrawn;
|
|
369
|
+
emit DelegationChanged(owner, delegate, false);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// ------------------------------------------------------------------------
|
|
373
|
+
// THE SIGNED TEXT
|
|
374
|
+
// ------------------------------------------------------------------------
|
|
375
|
+
|
|
376
|
+
// Prose, then a block of labelled fields.
|
|
377
|
+
//
|
|
378
|
+
// Readable text rather than typed data, because the owner is being asked to
|
|
379
|
+
// hand another address authority over their account, and what they can read
|
|
380
|
+
// in the signing dialog matters more than what it costs to check here. The
|
|
381
|
+
// shape borrows from EIP-4361 without being it: 4361 is built around a
|
|
382
|
+
// domain, a URI and a nonce, all three deliberately absent here.
|
|
383
|
+
//
|
|
384
|
+
// The prose comes FIRST so the first thing a human sees is what they are
|
|
385
|
+
// agreeing to, and so the field block cannot be mistaken for the leading
|
|
386
|
+
// `Origin:` line of the etherplay convention, which tells a conforming
|
|
387
|
+
// wallet a message is safe to sign without asking. It is not: a wallet
|
|
388
|
+
// implementing only that rule must fall through to prompting.
|
|
389
|
+
//
|
|
390
|
+
// EVERYTHING THAT MATTERS IS A FIELD, none of it inline in the prose,
|
|
391
|
+
// because a wallet has to extract the delegate (to compare against the
|
|
392
|
+
// signer it derives) and the contract and chain (to check against whatever
|
|
393
|
+
// it auto-signs), and one extraction strategy is better than two.
|
|
394
|
+
//
|
|
395
|
+
// There is no origin in it. The wallet always knows the true origin of the
|
|
396
|
+
// page asking, so a claimed one tells it nothing, and the delegate address
|
|
397
|
+
// already carries the origin binding by being a pure function of (account
|
|
398
|
+
// key, origin). There is no version field either: any change to these bytes
|
|
399
|
+
// invalidates every signature regardless, so there is no negotiation to be
|
|
400
|
+
// had, and a wallet that cannot parse what it sees falls back to showing
|
|
401
|
+
// raw text and prompting.
|
|
402
|
+
//
|
|
403
|
+
// `Expires: never` stays a line rather than being omitted, since an absent
|
|
404
|
+
// line is easy for a human to miss and easy for a parser to treat as an
|
|
405
|
+
// unset default.
|
|
406
|
+
//
|
|
407
|
+
// WHATEVER PRODUCES THE SIGNATURE BUILDS THIS SAME STRING, so the wording,
|
|
408
|
+
// the field order and the address casing are consensus, not style. Changing
|
|
409
|
+
// any of it invalidates every signature ever generated, silently. The other
|
|
410
|
+
// implementations in this package are the TypeScript builder in `src/` and
|
|
411
|
+
// the ABI it is exercised through; all of them are pinned against
|
|
412
|
+
// `vectors.json`, and a change to one without the others is a change that
|
|
413
|
+
// will pass review and fail in production.
|
|
414
|
+
string internal constant MESSAGE_HEAD =
|
|
415
|
+
"IMPORTANT: Only sign this on a site you trust.\n\n"
|
|
416
|
+
"This authorizes another address to act in your name onchain, at one contract.\n"
|
|
417
|
+
"You can withdraw it at any time by revoking it there.\n\n"
|
|
418
|
+
"Delegate: ";
|
|
419
|
+
string internal constant MESSAGE_CONTRACT = "\nContract: ";
|
|
420
|
+
string internal constant MESSAGE_CHAIN_ID = "\nChain ID: ";
|
|
421
|
+
string internal constant MESSAGE_EXPIRES = "\nExpires: ";
|
|
422
|
+
string internal constant MESSAGE_NO_EXPIRY = "never";
|
|
423
|
+
|
|
424
|
+
/// @notice the exact text an owner signs to authorise `delegate` here.
|
|
425
|
+
///
|
|
426
|
+
/// Expose it, so a caller can display it and so whatever produces the
|
|
427
|
+
/// signature can be asserted byte-for-byte against it. The two have to
|
|
428
|
+
/// agree exactly or every signature is rejected, so the agreement is worth
|
|
429
|
+
/// being checkable from outside rather than by reading both sides.
|
|
430
|
+
///
|
|
431
|
+
/// `view` rather than `pure`: the contract and the chain are read from
|
|
432
|
+
/// `address(this)` and `block.chainid` rather than taken as arguments,
|
|
433
|
+
/// which is what stops a caller choosing them.
|
|
434
|
+
///
|
|
435
|
+
/// Both addresses are rendered lowercase; see {StringUtils-toHexString}.
|
|
436
|
+
/// The deadline is decimal unix seconds, or the word `never` for zero.
|
|
437
|
+
function message(
|
|
438
|
+
address delegate,
|
|
439
|
+
uint256 deadline
|
|
440
|
+
) internal view returns (string memory) {
|
|
441
|
+
return
|
|
442
|
+
string(
|
|
443
|
+
abi.encodePacked(
|
|
444
|
+
MESSAGE_HEAD,
|
|
445
|
+
StringUtils.toHexString(delegate),
|
|
446
|
+
MESSAGE_CONTRACT,
|
|
447
|
+
StringUtils.toHexString(address(this)),
|
|
448
|
+
MESSAGE_CHAIN_ID,
|
|
449
|
+
StringUtils.toString(block.chainid),
|
|
450
|
+
MESSAGE_EXPIRES,
|
|
451
|
+
deadline == 0
|
|
452
|
+
? MESSAGE_NO_EXPIRY
|
|
453
|
+
: StringUtils.toString(deadline)
|
|
454
|
+
)
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/// @notice the EIP-191 digest of {message}.
|
|
459
|
+
function digest(
|
|
460
|
+
address delegate,
|
|
461
|
+
uint256 deadline
|
|
462
|
+
) internal view returns (bytes32) {
|
|
463
|
+
return SignatureUtils.textDigest(bytes(message(delegate, deadline)));
|
|
464
|
+
}
|
|
465
|
+
}
|