|
| 1 | +import { ContractBuilder } from './contract'; |
| 2 | +import type { Contract } from './contract'; |
| 3 | +import { printContract } from './print'; |
| 4 | +import { defaults as commonDefaults, withCommonDefaults, type CommonOptions } from './common-options'; |
| 5 | +import { defineFunctions } from './utils/define-functions'; |
| 6 | + |
| 7 | +export const defaults: Required<ERC7579Options> = { |
| 8 | + ...commonDefaults, |
| 9 | + name: 'MyERC7579Module', |
| 10 | + validator: undefined, |
| 11 | + executor: undefined, |
| 12 | + hook: false, |
| 13 | + fallback: false, |
| 14 | + access: commonDefaults.access, |
| 15 | +} as const; |
| 16 | + |
| 17 | +export type ERC7579MultisigType = { |
| 18 | + weighted: boolean; |
| 19 | + confirmation: boolean; |
| 20 | +}; |
| 21 | + |
| 22 | +export type ERC7579ValidatorType = { |
| 23 | + signature: boolean; |
| 24 | + multisig: ERC7579MultisigType; |
| 25 | +}; |
| 26 | + |
| 27 | +export type ERC7579ExecutorType = { |
| 28 | + delayed: boolean; |
| 29 | +}; |
| 30 | + |
| 31 | +export interface ERC7579Options extends CommonOptions { |
| 32 | + name: string; |
| 33 | + validator: ERC7579ValidatorType | undefined; |
| 34 | + executor: ERC7579ExecutorType | undefined; |
| 35 | + hook: boolean; |
| 36 | + fallback: boolean; |
| 37 | +} |
| 38 | + |
| 39 | +function withDefaults(opts: ERC7579Options): Required<ERC7579Options> { |
| 40 | + return { |
| 41 | + ...withCommonDefaults(opts), |
| 42 | + name: opts.name ?? defaults.name, |
| 43 | + validator: opts.validator ?? defaults.validator, |
| 44 | + executor: opts.executor ?? defaults.executor, |
| 45 | + hook: opts.hook ?? defaults.hook, |
| 46 | + fallback: opts.fallback ?? defaults.fallback, |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +export function printERC7579(opts: ERC7579Options = defaults): string { |
| 51 | + return printContract(buildERC7579(opts)); |
| 52 | +} |
| 53 | + |
| 54 | +export function buildERC7579(opts: ERC7579Options): Contract { |
| 55 | + const allOpts = withDefaults(opts); |
| 56 | + |
| 57 | + const c = new ContractBuilder(allOpts.name); |
| 58 | + |
| 59 | + addParents(c, allOpts); |
| 60 | + addMultisig(c, allOpts); |
| 61 | + |
| 62 | + return c; |
| 63 | +} |
| 64 | + |
| 65 | +function addParents(c: ContractBuilder, opts: ERC7579Options): void { |
| 66 | + c.addParent({ |
| 67 | + name: 'IERC7579Module', |
| 68 | + path: '@openzeppelin/contracts/interfaces/draft-IERC7579.sol', |
| 69 | + }); |
| 70 | + |
| 71 | + if (opts.executor) { |
| 72 | + c.addParent({ |
| 73 | + name: 'ERC7579Executor', |
| 74 | + path: '@openzeppelin/community-contracts/account/modules/ERC7579Executor.sol', |
| 75 | + }); |
| 76 | + |
| 77 | + if (opts.executor.delayed) { |
| 78 | + c.addParent({ |
| 79 | + name: 'ERC7579DelayedExecutor', |
| 80 | + path: '@openzeppelin/community-contracts/account/modules/ERC7579DelayedExecutor.sol', |
| 81 | + }); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (opts.validator) { |
| 86 | + c.addParent({ |
| 87 | + name: 'ERC7579Validator', |
| 88 | + path: '@openzeppelin/community-contracts/account/modules/ERC7579Validator.sol', |
| 89 | + }); |
| 90 | + |
| 91 | + if (opts.validator.multisig) { |
| 92 | + c.addParent({ |
| 93 | + name: 'ERC7579Multisig', |
| 94 | + path: '@openzeppelin/community-contracts/account/modules/ERC7579Multisig.sol', |
| 95 | + }); |
| 96 | + |
| 97 | + if (opts.validator.multisig.weighted) { |
| 98 | + c.addParent({ |
| 99 | + name: 'ERC7579MultisigWeighted', |
| 100 | + path: '@openzeppelin/community-contracts/account/modules/ERC7579MultisigWeighted.sol', |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + if (opts.validator.multisig.confirmation) { |
| 105 | + c.addParent({ |
| 106 | + name: 'ERC7579MultisigConfirmation', |
| 107 | + path: '@openzeppelin/community-contracts/account/modules/ERC7579MultisigConfirmation.sol', |
| 108 | + }); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (opts.hook) { |
| 114 | + c.addParent({ |
| 115 | + name: 'IERC7579Hook', |
| 116 | + path: '@openzeppelin/contracts/interfaces/draft-IERC7579.sol', |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + if (opts.fallback) { |
| 121 | + // NO OP |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +function addMultisig(c: ContractBuilder, opts: ERC7579Options): void { |
| 126 | + if (opts.executor) { |
| 127 | + const fn = functions._validateExecution; |
| 128 | + c.addOverride(c, fn); |
| 129 | + if (opts.validator) { |
| 130 | + // _rawERC7579Validation available |
| 131 | + c.setFunctionBody( |
| 132 | + [ |
| 133 | + `uint16 executionCalldataLength = uint16(uint256(bytes32(${fn.args[3]!.name}[0:2]))); // First 2 bytes are the length`, |
| 134 | + `bytes calldata executionCalldata = ${fn.args[3]!.name}[2:2 + executionCalldataLength]; // Next bytes are the calldata`, |
| 135 | + `bytes32 typeHash = _getExecuteTypeHash(${fn.args[0]!.name}, salt, mode, executionCalldata);`, |
| 136 | + `require(_rawERC7579Validation(${fn.args[0]!.name}, typeHash, ${fn.args[3]!.name}[2 + executionCalldataLength:])); // Remaining bytes are the signature`, |
| 137 | + `return executionCalldata;`, |
| 138 | + ], |
| 139 | + fn, |
| 140 | + ); |
| 141 | + } else { |
| 142 | + c.setFunctionBody( |
| 143 | + [ |
| 144 | + `// Slice \`${fn.args[3]!.name}\` to build custom authorization based on calldata`, |
| 145 | + `return ${fn.args[3]!.name}; // Use raw ${fn.args[3]!.name} as execution calldata`, |
| 146 | + ], |
| 147 | + fn, |
| 148 | + ); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +const functions = { |
| 154 | + ...defineFunctions({ |
| 155 | + _validateExecution: { |
| 156 | + kind: 'internal' as const, |
| 157 | + args: [ |
| 158 | + { name: 'account', type: 'address' }, |
| 159 | + { name: 'hash', type: 'bytes32' }, |
| 160 | + { name: 'mode', type: 'bytes32' }, |
| 161 | + { name: 'data', type: 'bytes calldata' }, |
| 162 | + ], |
| 163 | + returns: ['bytes calldata'], |
| 164 | + }, |
| 165 | + }), |
| 166 | +}; |
0 commit comments