|
1 | | -import { getChecksumAddress } from '@metamask/utils'; |
2 | | - |
3 | 1 | import { makeErc20TokenAllowanceDecoderConfig } from './caveats/erc20TokenAllowance'; |
4 | 2 | import { makeErc20TokenPeriodicDecoderConfig } from './caveats/erc20TokenPeriodic'; |
5 | 3 | import { makeErc20TokenStreamDecoderConfig } from './caveats/erc20TokenStream'; |
6 | 4 | import { makeNativeTokenAllowanceDecoderConfig } from './caveats/nativeTokenAllowance'; |
7 | 5 | import { makeNativeTokenPeriodicDecoderConfig } from './caveats/nativeTokenPeriodic'; |
8 | 6 | import { makeNativeTokenStreamDecoderConfig } from './caveats/nativeTokenStream'; |
9 | 7 | import { makeTokenApprovalRevocationDecoderConfig } from './caveats/tokenApprovalRevocation'; |
10 | | -import type { |
11 | | - Caveat, |
12 | | - DeployedContractsByName, |
13 | | - PermissionDecoder, |
14 | | - PermissionDecoderConfig, |
15 | | - ValidateAndDecodeResult, |
16 | | -} from './types'; |
17 | | -import type { Hex } from '../types'; |
| 8 | +import type { DeployedContractsByName, PermissionDecoderConfig } from './types'; |
18 | 9 | import { getChecksumEnforcersByChainId } from './utils'; |
19 | 10 |
|
20 | 11 | export type { |
@@ -78,103 +69,3 @@ export const makePermissionDecoderConfigs = ( |
78 | 69 | makeTokenApprovalRevocationDecoderConfig(contractAddresses), |
79 | 70 | ]; |
80 | 71 | }; |
81 | | - |
82 | | -/** |
83 | | - * Creates a runtime decoder from one permission decoder configuration. |
84 | | - * |
85 | | - * @param config - Permission decoder configuration. |
86 | | - * @returns Permission decoder. |
87 | | - */ |
88 | | -export const makePermissionDecoder = ( |
89 | | - config: PermissionDecoderConfig, |
90 | | -): PermissionDecoder => { |
91 | | - const requiredEnforcers = new Map<Hex, number>( |
92 | | - Object.entries(config.requiredEnforcers) as [Hex, number][], |
93 | | - ); |
94 | | - const optionalEnforcers = new Set(config.optionalEnforcers); |
95 | | - |
96 | | - const caveatAddressesMatch = (caveatAddresses: Hex[]): boolean => { |
97 | | - const counts = new Map<Hex, number>(); |
98 | | - |
99 | | - for (const address of caveatAddresses) { |
100 | | - counts.set(address, (counts.get(address) ?? 0) + 1); |
101 | | - } |
102 | | - for (const [address, count] of counts) { |
103 | | - const maxAllowedCount = |
104 | | - requiredEnforcers.get(address) ?? |
105 | | - (optionalEnforcers.has(address) ? 1 : 0); |
106 | | - if (maxAllowedCount === 0 || count > maxAllowedCount) { |
107 | | - return false; |
108 | | - } |
109 | | - } |
110 | | - |
111 | | - return true; |
112 | | - }; |
113 | | - |
114 | | - const validateAndDecodePermission = ( |
115 | | - caveats: Caveat[], |
116 | | - ): ValidateAndDecodeResult => { |
117 | | - try { |
118 | | - const normalizedCaveats = caveats.map((caveat) => ({ |
119 | | - ...caveat, |
120 | | - enforcer: getChecksumAddress(caveat.enforcer), |
121 | | - })); |
122 | | - |
123 | | - const caveatAddresses = normalizedCaveats.map( |
124 | | - (caveat) => caveat.enforcer, |
125 | | - ); |
126 | | - |
127 | | - if (!caveatAddressesMatch(caveatAddresses)) { |
128 | | - throw new Error('Invalid caveats'); |
129 | | - } |
130 | | - |
131 | | - const data = config.validateAndDecodeData( |
132 | | - normalizedCaveats, |
133 | | - config.contractAddresses, |
134 | | - ); |
135 | | - const rules = config.rules |
136 | | - .map((decodeRule) => |
137 | | - decodeRule({ |
138 | | - contractAddresses: config.contractAddresses, |
139 | | - caveats: normalizedCaveats, |
140 | | - requiredEnforcers, |
141 | | - }), |
142 | | - ) |
143 | | - .filter((rule) => rule !== null); |
144 | | - const expiryRule = rules.find((rule) => rule.type === 'expiry'); |
145 | | - |
146 | | - return { |
147 | | - isValid: true, |
148 | | - expiry: |
149 | | - expiryRule?.type === 'expiry' ? expiryRule.data.timestamp : null, |
150 | | - data, |
151 | | - rules: rules.length > 0 ? rules : undefined, |
152 | | - }; |
153 | | - } catch (error) { |
154 | | - return { |
155 | | - isValid: false, |
156 | | - error: error instanceof Error ? error : new Error('Invalid caveats'), |
157 | | - }; |
158 | | - } |
159 | | - }; |
160 | | - |
161 | | - return { |
162 | | - permissionType: config.permissionType, |
163 | | - requiredEnforcers, |
164 | | - optionalEnforcers, |
165 | | - caveatAddressesMatch, |
166 | | - validateAndDecodePermission, |
167 | | - }; |
168 | | -}; |
169 | | - |
170 | | -/** |
171 | | - * Creates permission decoders for all supported permission types. |
172 | | - * |
173 | | - * @param contracts - Deployed delegation framework contracts for one chain. |
174 | | - * @returns Runtime permission decoders. |
175 | | - */ |
176 | | -export const createPermissionDecodersForContracts = ( |
177 | | - contracts: DeployedContractsByName, |
178 | | -): PermissionDecoder[] => { |
179 | | - return makePermissionDecoderConfigs(contracts).map(makePermissionDecoder); |
180 | | -}; |
0 commit comments