@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
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.4;
|
|
3
|
+
|
|
4
|
+
/// @title IDelegation
|
|
5
|
+
/// @notice The delegation entry points as a type, for code that has to NAME
|
|
6
|
+
/// them rather than inherit them.
|
|
7
|
+
///
|
|
8
|
+
/// {UsingDelegation} already writes these functions, so a contract that
|
|
9
|
+
/// inherits it needs nothing from here. This exists for the cases where the
|
|
10
|
+
/// implementation is not the thing being described:
|
|
11
|
+
///
|
|
12
|
+
/// - A contract behind a ROUTER cannot say "these selectors are mine" by
|
|
13
|
+
/// inheriting an abstract contract: the router needs a list of selectors,
|
|
14
|
+
/// composed from interfaces, while the implementation lives elsewhere.
|
|
15
|
+
/// Without a shared declaration every router restates the same six
|
|
16
|
+
/// signatures, and a restatement is a copy that can drift.
|
|
17
|
+
/// - A client, a script or a test that calls into a contract it does not
|
|
18
|
+
/// compile against - `IDelegation(target).delegationStatus(owner, delegate)`
|
|
19
|
+
/// - which is also the shape the TypeScript side of this package exports as
|
|
20
|
+
/// `DELEGATION_ABI`, checked against this file by a test.
|
|
21
|
+
///
|
|
22
|
+
/// DO NOT WRITE `is IDelegation, UsingDelegation`. It does not compile without
|
|
23
|
+
/// restating all six functions as `override`, which is exactly the boilerplate
|
|
24
|
+
/// {UsingDelegation} exists to spare an adopter. Inherit the implementation
|
|
25
|
+
/// alone and use this interface where a TYPE is wanted; the two agreeing is
|
|
26
|
+
/// covered by a test (test/solidity/IDelegation.t.sol), and on a router the
|
|
27
|
+
/// thing that actually breaks is a selector that does not route - a missing
|
|
28
|
+
/// route in the deploy script, or two routes claiming the same selector - which
|
|
29
|
+
/// no compiler check would have caught.
|
|
30
|
+
///
|
|
31
|
+
/// The whole external surface, in the order {UsingDelegation} declares it, so
|
|
32
|
+
/// the two can be read side by side.
|
|
33
|
+
///
|
|
34
|
+
/// There is no `delegateOf`. An account may have several delegates, so no
|
|
35
|
+
/// single address could be returned without being a lie, and the set is
|
|
36
|
+
/// reconstructed from `DelegationChanged` logs rather than read. Every caller
|
|
37
|
+
/// only ever asked about a pair anyway.
|
|
38
|
+
interface IDelegation {
|
|
39
|
+
/// @notice authorise `delegate` to act for you here, and optionally fund it.
|
|
40
|
+
function registerDelegate(
|
|
41
|
+
address delegate,
|
|
42
|
+
address payable payee
|
|
43
|
+
) external payable;
|
|
44
|
+
|
|
45
|
+
/// @notice authorise `delegate` to act for `owner` here, proven by `owner`'s
|
|
46
|
+
/// signature, and fund that delegate with whatever value is sent.
|
|
47
|
+
function registerDelegateViaSignature(
|
|
48
|
+
address owner,
|
|
49
|
+
address delegate,
|
|
50
|
+
uint256 deadline,
|
|
51
|
+
bytes calldata signature
|
|
52
|
+
) external payable;
|
|
53
|
+
|
|
54
|
+
/// @notice withdraw your authorisation of `delegate`; no signature can put
|
|
55
|
+
/// that one back.
|
|
56
|
+
function revokeDelegate(address delegate) external;
|
|
57
|
+
|
|
58
|
+
/// @notice whether `delegate` may act for `owner` here, and whether `owner`
|
|
59
|
+
/// withdrew it.
|
|
60
|
+
function delegationStatus(
|
|
61
|
+
address owner,
|
|
62
|
+
address delegate
|
|
63
|
+
) external view returns (bool allowed, bool withdrawn);
|
|
64
|
+
|
|
65
|
+
/// @notice the exact text an owner signs to authorise `delegate` here.
|
|
66
|
+
function delegationMessage(
|
|
67
|
+
address delegate,
|
|
68
|
+
uint256 deadline
|
|
69
|
+
) external view returns (string memory);
|
|
70
|
+
|
|
71
|
+
/// @notice the EIP-191 digest of {delegationMessage}.
|
|
72
|
+
function delegationDigest(
|
|
73
|
+
address delegate,
|
|
74
|
+
uint256 deadline
|
|
75
|
+
) external view returns (bytes32);
|
|
76
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.4;
|
|
3
|
+
|
|
4
|
+
import {Delegation} from "./Delegation.sol";
|
|
5
|
+
import {Payments} from "./utils/Payments.sol";
|
|
6
|
+
|
|
7
|
+
/// @title Using Delegation
|
|
8
|
+
/// @notice The standard delegation entry points, ready to inherit.
|
|
9
|
+
///
|
|
10
|
+
/// {Delegation} is a library, so a contract adopting it writes its own external
|
|
11
|
+
/// functions. That is the right default: it keeps a contract's ABI and its
|
|
12
|
+
/// source in agreement, and lets it expose only what it means to. But it is
|
|
13
|
+
/// about seventy lines that are the same in every adopter, and getting them
|
|
14
|
+
/// wrong is not hypothetical - omit {revokeDelegate} and users are left unable
|
|
15
|
+
/// to withdraw an authorisation they have already given.
|
|
16
|
+
///
|
|
17
|
+
/// So this is the same set, written once. Inherit it when the standard shape is
|
|
18
|
+
/// what you want, and use the library directly when it is not.
|
|
19
|
+
///
|
|
20
|
+
/// WHAT YOU ARE HANDING OVER, in one sentence, because an adopter should not
|
|
21
|
+
/// have to infer it: a delegate authorised at your contract may do anything at
|
|
22
|
+
/// YOUR CONTRACT that its owner could do through the paths you resolve with
|
|
23
|
+
/// {_requireAccountForSender}. Not a scope, not one action - the whole contract.
|
|
24
|
+
/// It can do nothing anywhere else, since the signature names this contract and
|
|
25
|
+
/// this chain. If that is more than you mean to grant, narrow it in the
|
|
26
|
+
/// override rather than expecting the library to do it.
|
|
27
|
+
///
|
|
28
|
+
/// SAFE TO INHERIT, and specifically because {Delegation} keeps its state in a
|
|
29
|
+
/// namespaced region (ERC-7201) rather than in state variables. This contract
|
|
30
|
+
/// declares NO storage of its own, so it adds nothing to your layout and shifts
|
|
31
|
+
/// nothing you already have. The usual reason to be wary of a base contract -
|
|
32
|
+
/// that its storage silently precedes yours - does not apply, and there is a
|
|
33
|
+
/// test asserting exactly that. Keep it that way: adding a state variable here
|
|
34
|
+
/// would break every contract that already inherits this.
|
|
35
|
+
///
|
|
36
|
+
/// Everything is `virtual`, so an adopter can wrap one to add access control,
|
|
37
|
+
/// pin the payee, or refuse a path it does not want.
|
|
38
|
+
///
|
|
39
|
+
/// There are three ways to resolve WHO is acting, in increasing order of
|
|
40
|
+
/// effort, and you want the first that fits:
|
|
41
|
+
///
|
|
42
|
+
/// 1. Inherit this and call {_requireAccountForSender}. The sender is
|
|
43
|
+
/// `msg.sender`, which is right for a contract users call directly.
|
|
44
|
+
/// 2. Inherit this and override {_requireAccountForSender}, for relayed or
|
|
45
|
+
/// signature-based execution where the sender is recovered rather than
|
|
46
|
+
/// observed. One override, every call site follows.
|
|
47
|
+
/// 3. Skip this contract and call {Delegation} directly, passing whatever
|
|
48
|
+
/// address you like. The library never reads `msg.sender`.
|
|
49
|
+
abstract contract UsingDelegation {
|
|
50
|
+
/// @notice authorise `delegate` to act for you here, and optionally fund it.
|
|
51
|
+
///
|
|
52
|
+
/// The funding is why `payee` is here rather than left to a separate
|
|
53
|
+
/// transfer: a newly authorised address may hold nothing, and an address
|
|
54
|
+
/// that cannot pay for gas cannot do the thing it was just authorised to
|
|
55
|
+
/// do. Doing both at once removes the state in between.
|
|
56
|
+
///
|
|
57
|
+
/// `payee` is free rather than forced to `delegate` because this is your
|
|
58
|
+
/// own money and you may reasonably direct it elsewhere. The signature
|
|
59
|
+
/// variant does force it: there the money comes from a third party, who has
|
|
60
|
+
/// no business choosing a destination you never named.
|
|
61
|
+
///
|
|
62
|
+
/// No deadline here, and none is missing: a deadline bounds how long a
|
|
63
|
+
/// pre-signed credential may be presented, and you are present.
|
|
64
|
+
function registerDelegate(
|
|
65
|
+
address delegate,
|
|
66
|
+
address payable payee
|
|
67
|
+
) external payable virtual {
|
|
68
|
+
Delegation.register(msg.sender, delegate);
|
|
69
|
+
Payments.forward(payee, msg.value);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// @notice authorise `delegate` to act for `owner` here, proven by `owner`'s
|
|
73
|
+
/// signature, and fund that delegate with whatever value is sent.
|
|
74
|
+
///
|
|
75
|
+
/// Anyone may submit this and the submitter pays, which is the point: an
|
|
76
|
+
/// owner that can sign but cannot send can still delegate.
|
|
77
|
+
///
|
|
78
|
+
/// `deadline` is in calldata because only whoever produced the signature
|
|
79
|
+
/// knows it; the contract and the chain are NOT, because they are the bounds
|
|
80
|
+
/// this design rests on. See {Delegation-registerViaSignature}.
|
|
81
|
+
function registerDelegateViaSignature(
|
|
82
|
+
address owner,
|
|
83
|
+
address delegate,
|
|
84
|
+
uint256 deadline,
|
|
85
|
+
bytes calldata signature
|
|
86
|
+
) external payable virtual {
|
|
87
|
+
Delegation.registerViaSignature(owner, delegate, deadline, signature);
|
|
88
|
+
Payments.forward(payable(delegate), msg.value);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// @notice withdraw your authorisation of `delegate`; no signature can put
|
|
92
|
+
/// that one back.
|
|
93
|
+
///
|
|
94
|
+
/// DO NOT DROP THIS ONE if you override the set. An authorisation that
|
|
95
|
+
/// cannot be withdrawn is the failure this whole mechanism has to avoid.
|
|
96
|
+
function revokeDelegate(address delegate) external virtual {
|
|
97
|
+
Delegation.revoke(msg.sender, delegate);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/// @notice whether `delegate` may act for `owner` here, and whether `owner`
|
|
101
|
+
/// withdrew it.
|
|
102
|
+
///
|
|
103
|
+
/// The one read an app needs: `allowed` answers "can my signer act", and
|
|
104
|
+
/// `withdrawn` answers "can a signature fix that, or does it take a
|
|
105
|
+
/// transaction from the owner".
|
|
106
|
+
function delegationStatus(
|
|
107
|
+
address owner,
|
|
108
|
+
address delegate
|
|
109
|
+
) external view virtual returns (bool allowed, bool withdrawn) {
|
|
110
|
+
return Delegation.statusOf(owner, delegate);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/// @notice the exact text an owner signs to authorise `delegate` here.
|
|
114
|
+
function delegationMessage(
|
|
115
|
+
address delegate,
|
|
116
|
+
uint256 deadline
|
|
117
|
+
) external view virtual returns (string memory) {
|
|
118
|
+
return Delegation.message(delegate, deadline);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/// @notice the EIP-191 digest of {delegationMessage}.
|
|
122
|
+
function delegationDigest(
|
|
123
|
+
address delegate,
|
|
124
|
+
uint256 deadline
|
|
125
|
+
) external view virtual returns (bytes32) {
|
|
126
|
+
return Delegation.digest(delegate, deadline);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/// @notice the account this call belongs to, or revert.
|
|
130
|
+
///
|
|
131
|
+
/// THE FUNCTION YOUR OWN CODE USES. Call it wherever you would have taken
|
|
132
|
+
/// `msg.sender` as the identity an action belongs to, and record what it
|
|
133
|
+
/// returns. Zero means "acting for myself", so an entry point can pass one
|
|
134
|
+
/// through unconditionally.
|
|
135
|
+
///
|
|
136
|
+
/// It ENFORCES rather than looks up, reverting on an unauthorised claim
|
|
137
|
+
/// instead of falling back; and it resolves against the SENDER, which this
|
|
138
|
+
/// contract binds to `msg.sender` while {Delegation} leaves it a parameter.
|
|
139
|
+
///
|
|
140
|
+
/// OVERRIDE THIS for relayed or signature-based execution, where the
|
|
141
|
+
/// address that sent the transaction is not the address whose authority is
|
|
142
|
+
/// being used:
|
|
143
|
+
///
|
|
144
|
+
/// function _requireAccountForSender(address onBehalfOf)
|
|
145
|
+
/// internal view override returns (address)
|
|
146
|
+
/// {
|
|
147
|
+
/// return Delegation.requireAccountFor(_recoveredSender(), onBehalfOf);
|
|
148
|
+
/// }
|
|
149
|
+
///
|
|
150
|
+
/// It is also the seam for narrowing authority: an adopter that wants a
|
|
151
|
+
/// delegate to reach some of its functions and not others overrides this
|
|
152
|
+
/// (or calls {Delegation-canActFor} directly at the sites that differ)
|
|
153
|
+
/// rather than looking for a scope in the library, which deliberately has
|
|
154
|
+
/// none.
|
|
155
|
+
///
|
|
156
|
+
/// See {Delegation-requireAccountFor} for the substance, and
|
|
157
|
+
/// {Delegation-canActFor} for the question rather than the requirement.
|
|
158
|
+
function _requireAccountForSender(
|
|
159
|
+
address onBehalfOf
|
|
160
|
+
) internal view virtual returns (address) {
|
|
161
|
+
return Delegation.requireAccountFor(msg.sender, onBehalfOf);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.4;
|
|
3
|
+
|
|
4
|
+
/// @title Payments
|
|
5
|
+
/// @notice Sending value on to somebody else, from inside a call that was doing
|
|
6
|
+
/// something else anyway.
|
|
7
|
+
///
|
|
8
|
+
/// The pattern this exists for: an entry point takes a `payee` alongside its
|
|
9
|
+
/// real arguments and forwards whatever it was sent. It lets an app fund an
|
|
10
|
+
/// address that has to pay its own gas without a second transaction, which
|
|
11
|
+
/// matters most for an address that starts empty and therefore cannot send the
|
|
12
|
+
/// first thing it is meant to send. Worth putting on any entry point a funded
|
|
13
|
+
/// account calls, not only on registration, so the funding rides along with
|
|
14
|
+
/// whatever the user was already doing.
|
|
15
|
+
///
|
|
16
|
+
/// A library of `internal` functions, so it inlines into its callers and costs
|
|
17
|
+
/// nothing to deploy. It holds no state.
|
|
18
|
+
library Payments {
|
|
19
|
+
/// @notice the recipient rejected the value, or ran out of gas taking it
|
|
20
|
+
error TransferFailed(address payee);
|
|
21
|
+
|
|
22
|
+
/// @notice value was sent with nowhere to forward it to
|
|
23
|
+
error ValueWithNoPayee(uint256 amount);
|
|
24
|
+
|
|
25
|
+
/// @notice forward `amount` to `payee`, if there is anything to forward.
|
|
26
|
+
///
|
|
27
|
+
/// CALL THIS LAST, after the caller's own state is written. It hands control
|
|
28
|
+
/// to `payee`, which may be a contract, and only an already-settled state is
|
|
29
|
+
/// safe to be re-entered against.
|
|
30
|
+
///
|
|
31
|
+
/// `call` with all the gas rather than `transfer` with its 2300 stipend: the
|
|
32
|
+
/// stipend silently refuses any payee that is a smart account, which is an
|
|
33
|
+
/// increasing share of them and none of the caller's business.
|
|
34
|
+
///
|
|
35
|
+
/// Reverts rather than swallowing a failure. The point of forwarding is that
|
|
36
|
+
/// the recipient ends up funded, so a transfer that vanished has not
|
|
37
|
+
/// half-worked, it has failed, and the caller's own state change is
|
|
38
|
+
/// worthless without it.
|
|
39
|
+
///
|
|
40
|
+
/// A zero `amount` is a no-op, so a caller can pass one through
|
|
41
|
+
/// unconditionally instead of guarding at every site. A zero `payee` is
|
|
42
|
+
/// likewise fine WHEN there is nothing to forward, which is how an entry
|
|
43
|
+
/// point says "no funding this time".
|
|
44
|
+
///
|
|
45
|
+
/// But value with no payee REVERTS, rather than being quietly kept. The
|
|
46
|
+
/// contract calling this has no reason to hold the money and typically no
|
|
47
|
+
/// way to release it, so a no-op there would take a caller's mistake -
|
|
48
|
+
/// value attached to a call that names no recipient - and turn it into
|
|
49
|
+
/// funds nobody can ever recover. Failing the transaction leaves the money
|
|
50
|
+
/// where it started.
|
|
51
|
+
function forward(address payable payee, uint256 amount) internal {
|
|
52
|
+
if (amount == 0) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (payee == address(0)) {
|
|
56
|
+
revert ValueWithNoPayee(amount);
|
|
57
|
+
}
|
|
58
|
+
(bool success, ) = payee.call{value: amount}("");
|
|
59
|
+
if (!success) {
|
|
60
|
+
revert TransferFailed(payee);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.4;
|
|
3
|
+
|
|
4
|
+
import {StringUtils} from "./StringUtils.sol";
|
|
5
|
+
|
|
6
|
+
/// @title Signature Utils
|
|
7
|
+
/// @notice Recovering the address behind a signature, with the checks
|
|
8
|
+
/// `ecrecover` does not do for you.
|
|
9
|
+
///
|
|
10
|
+
/// A library of `internal` functions, so it inlines into its callers and costs
|
|
11
|
+
/// nothing to deploy. It holds no state.
|
|
12
|
+
library SignatureUtils {
|
|
13
|
+
/// @notice the signature is not 65 bytes, or carries values no honest
|
|
14
|
+
/// signer produces
|
|
15
|
+
error MalformedSignature();
|
|
16
|
+
|
|
17
|
+
/// @notice the signature is well-formed but recovers to nobody
|
|
18
|
+
error UnrecoverableSignature();
|
|
19
|
+
|
|
20
|
+
/// @notice the address that produced `signature` over `digest`.
|
|
21
|
+
///
|
|
22
|
+
/// `ecrecover` alone is not enough, and each of the three checks here exists
|
|
23
|
+
/// for a distinct reason:
|
|
24
|
+
///
|
|
25
|
+
/// - A length other than 65 means the caller passed something that is not a
|
|
26
|
+
/// signature at all, and reading past it would recover an address from
|
|
27
|
+
/// whatever happened to be in memory.
|
|
28
|
+
/// - The curve is symmetric, so `(r, s)` and `(r, -s)` both recover the same
|
|
29
|
+
/// address. Accepting either turns one authorisation into two distinct
|
|
30
|
+
/// valid signatures, which breaks anything that identifies a signature by
|
|
31
|
+
/// its bytes. Only the low half is accepted.
|
|
32
|
+
/// - `ecrecover` returns the zero address on failure rather than reverting,
|
|
33
|
+
/// so an unchecked result compares equal to an uninitialised mapping entry
|
|
34
|
+
/// and quietly authorises the wrong thing.
|
|
35
|
+
///
|
|
36
|
+
/// `v` IS DELIBERATELY NOT CANONICALISED, and that qualifies the second
|
|
37
|
+
/// point above rather than extending it. Both encodings in circulation are
|
|
38
|
+
/// accepted - the 27/28 of `eth_sign` and the raw 0/1 recovery bit - because
|
|
39
|
+
/// signers genuinely differ on which they emit, and refusing half of them
|
|
40
|
+
/// buys nothing. So one authorisation does still have two accepted byte
|
|
41
|
+
/// strings, differing in the last byte. That is safe HERE because nothing in
|
|
42
|
+
/// this package identifies a signature by its bytes: the withdrawn state is
|
|
43
|
+
/// keyed by (owner, delegate), and a credential presented twice only
|
|
44
|
+
/// re-asserts what is already true. A caller that DOES key on signature
|
|
45
|
+
/// bytes has to normalise `v` itself before doing so.
|
|
46
|
+
///
|
|
47
|
+
/// @param digest what was signed
|
|
48
|
+
/// @param signature 65 bytes, `r || s || v`
|
|
49
|
+
/// @return the recovered address, never the zero address
|
|
50
|
+
function recover(
|
|
51
|
+
bytes32 digest,
|
|
52
|
+
bytes calldata signature
|
|
53
|
+
) internal pure returns (address) {
|
|
54
|
+
if (signature.length != 65) {
|
|
55
|
+
revert MalformedSignature();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
bytes32 r;
|
|
59
|
+
bytes32 s;
|
|
60
|
+
uint8 v;
|
|
61
|
+
assembly {
|
|
62
|
+
r := calldataload(signature.offset)
|
|
63
|
+
s := calldataload(add(signature.offset, 32))
|
|
64
|
+
v := byte(0, calldataload(add(signature.offset, 64)))
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// secp256k1n / 2
|
|
68
|
+
if (
|
|
69
|
+
uint256(s) >
|
|
70
|
+
0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0
|
|
71
|
+
) {
|
|
72
|
+
revert MalformedSignature();
|
|
73
|
+
}
|
|
74
|
+
if (v < 27) {
|
|
75
|
+
v += 27;
|
|
76
|
+
}
|
|
77
|
+
if (v != 27 && v != 28) {
|
|
78
|
+
revert MalformedSignature();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
address signer = ecrecover(digest, v, r, s);
|
|
82
|
+
if (signer == address(0)) {
|
|
83
|
+
revert UnrecoverableSignature();
|
|
84
|
+
}
|
|
85
|
+
return signer;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/// @notice the EIP-191 `personal_sign` digest of a text message.
|
|
89
|
+
///
|
|
90
|
+
/// What a wallet actually signs when asked to sign a string: the text with
|
|
91
|
+
/// a fixed prefix and its own byte length in front, so a signature over a
|
|
92
|
+
/// message can never be replayed as a signature over a transaction.
|
|
93
|
+
///
|
|
94
|
+
/// The length is decimal ASCII, which is why this needs {StringUtils}.
|
|
95
|
+
///
|
|
96
|
+
/// @param message the exact bytes shown to the signer
|
|
97
|
+
function textDigest(bytes memory message) internal pure returns (bytes32) {
|
|
98
|
+
return
|
|
99
|
+
keccak256(
|
|
100
|
+
abi.encodePacked(
|
|
101
|
+
"\x19Ethereum Signed Message:\n",
|
|
102
|
+
StringUtils.toString(message.length),
|
|
103
|
+
message
|
|
104
|
+
)
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.4;
|
|
3
|
+
|
|
4
|
+
/// @title String Utils
|
|
5
|
+
/// @notice Rendering values as text.
|
|
6
|
+
///
|
|
7
|
+
/// Needed wherever a contract has to reproduce a string that was built
|
|
8
|
+
/// somewhere else, which in practice means verifying a signature over
|
|
9
|
+
/// human-readable text: the signed bytes only match if both sides spell the
|
|
10
|
+
/// values the same way.
|
|
11
|
+
///
|
|
12
|
+
/// A library of `internal` functions, so it is inlined into its callers and
|
|
13
|
+
/// costs nothing to deploy. It holds no state and knows nothing about who uses
|
|
14
|
+
/// it.
|
|
15
|
+
library StringUtils {
|
|
16
|
+
bytes internal constant HEX_DIGITS = "0123456789abcdef";
|
|
17
|
+
|
|
18
|
+
/// @notice `0x`-prefixed, LOWERCASE hex form of an address.
|
|
19
|
+
///
|
|
20
|
+
/// Lowercase rather than EIP-55 checksummed, deliberately. Checksumming
|
|
21
|
+
/// costs a hash of the lowercase form plus a pass over every character, and
|
|
22
|
+
/// settles nothing that matters: whoever produces a signature and whoever
|
|
23
|
+
/// verifies it must agree on one spelling regardless, so the useful property
|
|
24
|
+
/// is that the spelling is unambiguous and cheap, not that it carries a
|
|
25
|
+
/// typo-detecting checksum nobody in this path is reading.
|
|
26
|
+
///
|
|
27
|
+
/// The other implementations of the delegation message therefore have to
|
|
28
|
+
/// lowercase too, and the vectors file carries checksummed inputs precisely
|
|
29
|
+
/// to catch one that forgot.
|
|
30
|
+
///
|
|
31
|
+
/// @param value the address to render
|
|
32
|
+
/// @return the 42-character lowercase representation
|
|
33
|
+
function toHexString(address value) internal pure returns (string memory) {
|
|
34
|
+
bytes20 raw = bytes20(value);
|
|
35
|
+
bytes memory result = new bytes(42);
|
|
36
|
+
result[0] = "0";
|
|
37
|
+
result[1] = "x";
|
|
38
|
+
for (uint256 i = 0; i < 20; i++) {
|
|
39
|
+
result[2 + i * 2] = HEX_DIGITS[uint8(raw[i]) >> 4];
|
|
40
|
+
result[3 + i * 2] = HEX_DIGITS[uint8(raw[i]) & 0x0f];
|
|
41
|
+
}
|
|
42
|
+
return string(result);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// @notice decimal form of a number.
|
|
46
|
+
///
|
|
47
|
+
/// @param value the number to render
|
|
48
|
+
/// @return the shortest representation, with no leading zero except for
|
|
49
|
+
/// zero itself
|
|
50
|
+
function toString(uint256 value) internal pure returns (string memory) {
|
|
51
|
+
if (value == 0) {
|
|
52
|
+
return "0";
|
|
53
|
+
}
|
|
54
|
+
uint256 digits;
|
|
55
|
+
for (uint256 rest = value; rest != 0; rest /= 10) {
|
|
56
|
+
digits++;
|
|
57
|
+
}
|
|
58
|
+
bytes memory buffer = new bytes(digits);
|
|
59
|
+
while (value != 0) {
|
|
60
|
+
digits--;
|
|
61
|
+
buffer[digits] = bytes1(uint8(48 + (value % 10)));
|
|
62
|
+
value /= 10;
|
|
63
|
+
}
|
|
64
|
+
return string(buffer);
|
|
65
|
+
}
|
|
66
|
+
}
|
package/dist/abi.d.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The delegation surface as an ABI, for a client that calls a contract it does not compile against.
|
|
3
|
+
*
|
|
4
|
+
* The same six functions {@link file://../contracts/IDelegation.sol} declares, plus the event and
|
|
5
|
+
* the errors an adopter of `UsingDelegation` actually exposes - the event because it IS the
|
|
6
|
+
* enumeration API (there is no onchain list of an account's delegates, so the set is reconstructed
|
|
7
|
+
* by replaying these logs), and the errors so a revert decodes into something an app can act on
|
|
8
|
+
* rather than a hex blob.
|
|
9
|
+
*
|
|
10
|
+
* Hand-written rather than generated, so it reads as the contract's surface and ships without a
|
|
11
|
+
* build artifact. Drift is caught instead of prevented: a test compiles the Solidity and asserts
|
|
12
|
+
* this is exactly its ABI, so adding a parameter on one side and not the other fails in CI.
|
|
13
|
+
*
|
|
14
|
+
* `as const`, so viem infers argument and return types from it.
|
|
15
|
+
*/
|
|
16
|
+
export declare const DELEGATION_ABI: readonly [{
|
|
17
|
+
readonly type: "function";
|
|
18
|
+
readonly name: "registerDelegate";
|
|
19
|
+
readonly stateMutability: "payable";
|
|
20
|
+
readonly inputs: readonly [{
|
|
21
|
+
readonly name: "delegate";
|
|
22
|
+
readonly type: "address";
|
|
23
|
+
}, {
|
|
24
|
+
readonly name: "payee";
|
|
25
|
+
readonly type: "address";
|
|
26
|
+
}];
|
|
27
|
+
readonly outputs: readonly [];
|
|
28
|
+
}, {
|
|
29
|
+
readonly type: "function";
|
|
30
|
+
readonly name: "registerDelegateViaSignature";
|
|
31
|
+
readonly stateMutability: "payable";
|
|
32
|
+
readonly inputs: readonly [{
|
|
33
|
+
readonly name: "owner";
|
|
34
|
+
readonly type: "address";
|
|
35
|
+
}, {
|
|
36
|
+
readonly name: "delegate";
|
|
37
|
+
readonly type: "address";
|
|
38
|
+
}, {
|
|
39
|
+
readonly name: "deadline";
|
|
40
|
+
readonly type: "uint256";
|
|
41
|
+
}, {
|
|
42
|
+
readonly name: "signature";
|
|
43
|
+
readonly type: "bytes";
|
|
44
|
+
}];
|
|
45
|
+
readonly outputs: readonly [];
|
|
46
|
+
}, {
|
|
47
|
+
readonly type: "function";
|
|
48
|
+
readonly name: "revokeDelegate";
|
|
49
|
+
readonly stateMutability: "nonpayable";
|
|
50
|
+
readonly inputs: readonly [{
|
|
51
|
+
readonly name: "delegate";
|
|
52
|
+
readonly type: "address";
|
|
53
|
+
}];
|
|
54
|
+
readonly outputs: readonly [];
|
|
55
|
+
}, {
|
|
56
|
+
readonly type: "function";
|
|
57
|
+
readonly name: "delegationStatus";
|
|
58
|
+
readonly stateMutability: "view";
|
|
59
|
+
readonly inputs: readonly [{
|
|
60
|
+
readonly name: "owner";
|
|
61
|
+
readonly type: "address";
|
|
62
|
+
}, {
|
|
63
|
+
readonly name: "delegate";
|
|
64
|
+
readonly type: "address";
|
|
65
|
+
}];
|
|
66
|
+
readonly outputs: readonly [{
|
|
67
|
+
readonly name: "allowed";
|
|
68
|
+
readonly type: "bool";
|
|
69
|
+
}, {
|
|
70
|
+
readonly name: "withdrawn";
|
|
71
|
+
readonly type: "bool";
|
|
72
|
+
}];
|
|
73
|
+
}, {
|
|
74
|
+
readonly type: "function";
|
|
75
|
+
readonly name: "delegationMessage";
|
|
76
|
+
readonly stateMutability: "view";
|
|
77
|
+
readonly inputs: readonly [{
|
|
78
|
+
readonly name: "delegate";
|
|
79
|
+
readonly type: "address";
|
|
80
|
+
}, {
|
|
81
|
+
readonly name: "deadline";
|
|
82
|
+
readonly type: "uint256";
|
|
83
|
+
}];
|
|
84
|
+
readonly outputs: readonly [{
|
|
85
|
+
readonly name: "";
|
|
86
|
+
readonly type: "string";
|
|
87
|
+
}];
|
|
88
|
+
}, {
|
|
89
|
+
readonly type: "function";
|
|
90
|
+
readonly name: "delegationDigest";
|
|
91
|
+
readonly stateMutability: "view";
|
|
92
|
+
readonly inputs: readonly [{
|
|
93
|
+
readonly name: "delegate";
|
|
94
|
+
readonly type: "address";
|
|
95
|
+
}, {
|
|
96
|
+
readonly name: "deadline";
|
|
97
|
+
readonly type: "uint256";
|
|
98
|
+
}];
|
|
99
|
+
readonly outputs: readonly [{
|
|
100
|
+
readonly name: "";
|
|
101
|
+
readonly type: "bytes32";
|
|
102
|
+
}];
|
|
103
|
+
}, {
|
|
104
|
+
readonly type: "event";
|
|
105
|
+
readonly name: "DelegationChanged";
|
|
106
|
+
readonly anonymous: false;
|
|
107
|
+
readonly inputs: readonly [{
|
|
108
|
+
readonly name: "owner";
|
|
109
|
+
readonly type: "address";
|
|
110
|
+
readonly indexed: true;
|
|
111
|
+
}, {
|
|
112
|
+
readonly name: "delegate";
|
|
113
|
+
readonly type: "address";
|
|
114
|
+
readonly indexed: true;
|
|
115
|
+
}, {
|
|
116
|
+
readonly name: "allowed";
|
|
117
|
+
readonly type: "bool";
|
|
118
|
+
readonly indexed: false;
|
|
119
|
+
}];
|
|
120
|
+
}, {
|
|
121
|
+
readonly type: "error";
|
|
122
|
+
readonly name: "NotDelegate";
|
|
123
|
+
readonly inputs: readonly [{
|
|
124
|
+
readonly name: "owner";
|
|
125
|
+
readonly type: "address";
|
|
126
|
+
}, {
|
|
127
|
+
readonly name: "sender";
|
|
128
|
+
readonly type: "address";
|
|
129
|
+
}];
|
|
130
|
+
}, {
|
|
131
|
+
readonly type: "error";
|
|
132
|
+
readonly name: "DelegationWithdrawn";
|
|
133
|
+
readonly inputs: readonly [{
|
|
134
|
+
readonly name: "owner";
|
|
135
|
+
readonly type: "address";
|
|
136
|
+
}, {
|
|
137
|
+
readonly name: "delegate";
|
|
138
|
+
readonly type: "address";
|
|
139
|
+
}];
|
|
140
|
+
}, {
|
|
141
|
+
readonly type: "error";
|
|
142
|
+
readonly name: "InvalidSignature";
|
|
143
|
+
readonly inputs: readonly [];
|
|
144
|
+
}, {
|
|
145
|
+
readonly type: "error";
|
|
146
|
+
readonly name: "InvalidDelegate";
|
|
147
|
+
readonly inputs: readonly [];
|
|
148
|
+
}, {
|
|
149
|
+
readonly type: "error";
|
|
150
|
+
readonly name: "SignatureExpired";
|
|
151
|
+
readonly inputs: readonly [{
|
|
152
|
+
readonly name: "deadline";
|
|
153
|
+
readonly type: "uint256";
|
|
154
|
+
}];
|
|
155
|
+
}, {
|
|
156
|
+
readonly type: "error";
|
|
157
|
+
readonly name: "MalformedSignature";
|
|
158
|
+
readonly inputs: readonly [];
|
|
159
|
+
}, {
|
|
160
|
+
readonly type: "error";
|
|
161
|
+
readonly name: "UnrecoverableSignature";
|
|
162
|
+
readonly inputs: readonly [];
|
|
163
|
+
}, {
|
|
164
|
+
readonly type: "error";
|
|
165
|
+
readonly name: "TransferFailed";
|
|
166
|
+
readonly inputs: readonly [{
|
|
167
|
+
readonly name: "payee";
|
|
168
|
+
readonly type: "address";
|
|
169
|
+
}];
|
|
170
|
+
}, {
|
|
171
|
+
readonly type: "error";
|
|
172
|
+
readonly name: "ValueWithNoPayee";
|
|
173
|
+
readonly inputs: readonly [{
|
|
174
|
+
readonly name: "amount";
|
|
175
|
+
readonly type: "uint256";
|
|
176
|
+
}];
|
|
177
|
+
}];
|
|
178
|
+
//# sourceMappingURL=abi.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abi.d.ts","sourceRoot":"","sources":["../src/abi.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA8HjB,CAAC"}
|