Repository navigation
Expand file tree
/
Copy pathOKAccount.sol
More file actions
199 lines (179 loc) · 9.45 KB
/
Copy pathOKAccount.sol
File metadata and controls
199 lines (179 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// SPDX-License-Identifier: BUSL-1.1
// Copyright: OKcontract Pte. Ltd.
pragma solidity ^0.8.36;
import {IMiniScript} from "@chainwall/interfaces/IMiniScript.sol";
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {ChainwallUUPSUpgradeable} from "./ChainwallUpgradeable.sol";
import {InteractionFlags} from "./InteractionFlags.sol";
import {MiniScriptEngine} from "./MiniScriptEngine.sol";
import {ProtocolCodehashes} from "./ProtocolCodehashes.sol";
import {IRegistry} from "./registry/RegistryShared.sol";
interface IAccountInteract {
function registry() external view returns (IRegistry);
}
/// @title OKAccount
/// @notice Minimal tenant-native account that executes registry interactions only through `Interact`.
/// @dev `OKAccount` is the account-mode counterpart to a guarded Safe:
/// - it is deployed as an atomically initialized ERC-1967 proxy bound to one
/// `Interact`, one registry, and one `MiniScriptEngine`
/// - it accepts ETH directly so it can hold execution balances
/// - it refuses all execution unless the caller is the bound `Interact`
/// - its stable vault address can be upgraded only by that tenant Registry
///
/// Example:
/// 1. A user deploys an account through `AccountFactory`.
/// 2. The user proposes or directly executes an interaction through `Interact`.
/// 3. `Interact` calls {executeInteraction} on the account.
/// 4. The account loads the script from the registry and runs it through the
/// shared engine with `address(this)` as the execution vault.
contract OKAccount is Initializable, ChainwallUUPSUpgradeable {
bytes32 public constant CHAINWALL_COMPONENT_ID = keccak256("Chainwall.OKAccount");
/// @notice `Interact` instance that is allowed to trigger execution.
address public INTERACT;
/// @notice Registry used to resolve interaction scripts.
address public REGISTRY;
/// @notice Shared MiniScript engine used through delegatecall.
address public ENGINE;
/// @notice Thrown when anyone other than the bound `Interact` calls {executeInteraction}.
error InteractOnly();
/// @notice Thrown when the registry address is zero during initialization.
error RegistryZero();
/// @notice Thrown when the engine address is zero during initialization.
error EngineZero();
/// @notice Thrown when the interact address is zero during initialization.
error InteractZero();
/// @notice Thrown when the requested interaction is deactivated in the registry.
error InteractionInactive(bytes32 interactionId);
/// @notice Thrown when the interact address has no deployed code during initialization.
error InteractNotContract(address interact);
/// @notice Thrown when the registry address has no deployed code during initialization.
error RegistryNotContract(address registry);
/// @notice Thrown when the engine address has no deployed code during initialization.
error EngineNotContract(address engine);
/// @notice Thrown when the engine runtime is not a canonical MiniScript engine.
error EngineNotCanonical(address engine);
/// @notice Thrown when the supplied `Interact` points at a different registry.
error InteractRegistryMismatch(address expectedRegistry, address actualRegistry);
/// @notice Thrown when an implementation change is attempted outside the tenant Registry.
error OnlyRegistryUpgrade();
event EngineChanged(address indexed previousEngine, address indexed newEngine);
constructor() {
_disableInitializers();
}
/// @notice Initialize an account proxy bound to a single tenant execution environment.
/// @param interact `Interact` instance allowed to trigger execution.
/// @param registry Registry used for script lookup.
/// @param engine Shared MiniScript engine.
function initialize(address interact, address registry, address engine) external initializer {
if (interact == address(0)) revert InteractZero();
if (registry == address(0)) revert RegistryZero();
if (engine == address(0)) revert EngineZero();
if (interact.code.length == 0) revert InteractNotContract(interact);
if (registry.code.length == 0) revert RegistryNotContract(registry);
if (engine.code.length == 0) revert EngineNotContract(engine);
if (!ProtocolCodehashes.isAcceptedMiniScriptDeployment(engine)) {
revert EngineNotCanonical(engine);
}
address interactRegistry = address(IAccountInteract(interact).registry());
if (interactRegistry != registry) {
revert InteractRegistryMismatch(registry, interactRegistry);
}
INTERACT = interact;
REGISTRY = registry;
ENGINE = engine;
__UUPSUpgradeable_init();
}
/// @notice Stable logical type used by Registry upgrade verification.
function chainwallComponentId() external pure override returns (bytes32) {
return CHAINWALL_COMPONENT_ID;
}
/// @notice Upgrade entry point reserved for the tenant Registry timelock.
function timelockUpgrade(address newImplementation, bytes calldata data) external {
if (msg.sender != REGISTRY) revert OnlyRegistryUpgrade();
_upgradePreservingBindings(newImplementation, data);
}
/// @notice Disable the ordinary UUPS surface so callers must use {timelockUpgrade}.
function upgradeToAndCall(address, bytes memory) public payable override {
revert("OKAccount: use timelockUpgrade");
}
function _authorizeUpgrade(address) internal view override {
if (msg.sender != REGISTRY) revert OnlyRegistryUpgrade();
}
/// @dev These three pointers define the only execution path for account-held assets. Engine replacement is a
/// separate Registry action; implementation migration data may not change any tenant trust root.
function _chainwallSecurityBindingsHash() internal view override returns (bytes32) {
return _hashSecurityBindings(abi.encode(INTERACT, REGISTRY, ENGINE));
}
/// @notice Return the engine currently executing this vault's interactions.
function chainwallEngine() external view returns (address) {
return ENGINE;
}
/// @notice Adopt a canonical engine through the separately scoped Registry action.
function setChainwallEngine(address newEngine) external {
if (msg.sender != REGISTRY) revert OnlyRegistryUpgrade();
if (newEngine == address(0)) revert EngineZero();
if (newEngine.code.length == 0) revert EngineNotContract(newEngine);
if (!ProtocolCodehashes.isAcceptedMiniScriptDeployment(newEngine)) {
revert EngineNotCanonical(newEngine);
}
address previous = ENGINE;
ENGINE = newEngine;
emit EngineChanged(previous, newEngine);
}
/// @notice Accept ETH so the account can fund native-value executions.
receive() external payable {}
/// @notice Canonical host authentication surface used by linked execution.
/// @return engine Canonical immutable MiniScript engine.
/// @return registry Registry whose catalog is authoritative for this account.
function miniScriptHostConfig() external view returns (address engine, address registry) {
return (ENGINE, REGISTRY);
}
/// @notice Execute an interaction by id in the account context.
/// @dev The account itself becomes the execution vault and caller context
/// seen by downstream Safe/account-aware scripts.
/// @param interactionId Registry interaction identifier.
/// @param args Initial stack arguments forwarded to MiniScript.
/// @param maxSteps Interpreter step cap enforced by the engine.
/// @param maxNativeValue Maximum native value the script is allowed to spend.
/// @param actor Authenticated policy actor exposed through subject context.
/// @param policyId Policy selected for this execution.
/// @return stackTop Final value left on top of the MiniScript stack.
function executeInteraction(
bytes32 interactionId,
uint256[] calldata args,
uint256 maxSteps,
uint256 maxNativeValue,
address actor,
bytes32 policyId
) external returns (uint256 stackTop) {
if (msg.sender != INTERACT) revert InteractOnly();
uint256 flags = IRegistry(REGISTRY).getInteractionFlags(interactionId);
if (InteractionFlags.isInactive(flags)) {
revert InteractionInactive(interactionId);
}
bytes memory script = IRegistry(REGISTRY).getInteractionScript(interactionId);
require(script.length != 0, "interaction:not-found");
IMiniScript.SubjectContext memory subject = IMiniScript.SubjectContext({
version: 2,
interactionId: interactionId,
nativeValue: maxNativeValue,
actor: actor,
vault: address(this),
policyId: policyId,
interactionArgs: args
});
bytes memory data = abi.encodeWithSelector(
MiniScriptEngine.runSubject.selector, script, args, maxSteps, REGISTRY, subject, maxNativeValue, ENGINE
);
// Only INTERACT can execute; initialization and Registry-only engine updates require the canonical
// deployment.
// forge-lint: disable-next-line(controlled-delegatecall)
(bool ok, bytes memory ret) = ENGINE.delegatecall(data);
if (!ok) {
assembly {
revert(add(ret, 0x20), mload(ret))
}
}
stackTop = abi.decode(ret, (uint256));
}
}