|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.20; |
| 3 | + |
| 4 | +import {TxAuthManagerBase} from "./TxAuthManagerBase.sol"; |
| 5 | +import {CrossStore} from "./CrossStore.sol"; |
| 6 | +import {Account, TxAuthState} from "../proto/cross/core/auth/Auth.sol"; |
| 7 | + |
| 8 | +abstract contract TxAuthManager is TxAuthManagerBase, CrossStore { |
| 9 | + function initAuthState(bytes32 txID, Account.Data[] memory signers) internal virtual override { |
| 10 | + CrossStore.AuthStorage storage s = _getAuthStorage(); |
| 11 | + if (s.authInitialized[txID]) revert AuthStateAlreadyInitialized(txID); |
| 12 | + _setStateFromRemainingList(s, txID, signers); |
| 13 | + s.authInitialized[txID] = true; |
| 14 | + } |
| 15 | + |
| 16 | + function isCompletedAuth(bytes32 txID) internal view virtual override returns (bool) { |
| 17 | + CrossStore.AuthStorage storage s = _getAuthStorage(); |
| 18 | + if (!s.authInitialized[txID]) revert IDNotFound(txID); |
| 19 | + return s.remainingCount[txID] == 0; |
| 20 | + } |
| 21 | + |
| 22 | + function sign(bytes32 txID, Account.Data[] memory signers) internal virtual override returns (bool) { |
| 23 | + CrossStore.AuthStorage storage s = _getAuthStorage(); |
| 24 | + if (!s.authInitialized[txID]) revert IDNotFound(txID); |
| 25 | + if (s.remainingCount[txID] == 0) revert AuthAlreadyCompleted(txID); |
| 26 | + |
| 27 | + for (uint256 i = 0; i < signers.length; ++i) { |
| 28 | + bytes32 key = _accountKey(signers[i]); |
| 29 | + if (s.remaining[txID][key]) { |
| 30 | + s.remaining[txID][key] = false; |
| 31 | + --s.remainingCount[txID]; |
| 32 | + if (s.remainingCount[txID] == 0) return true; |
| 33 | + } |
| 34 | + } |
| 35 | + return s.remainingCount[txID] == 0; |
| 36 | + } |
| 37 | + |
| 38 | + function getAuthState(bytes32 txID) internal view virtual override returns (TxAuthState.Data memory) { |
| 39 | + CrossStore.AuthStorage storage s = _getAuthStorage(); |
| 40 | + if (!s.authInitialized[txID]) revert IDNotFound(txID); |
| 41 | + Account.Data[] memory remains = _getRemainingSigners(s, txID); |
| 42 | + return TxAuthState.Data({remaining_signers: remains}); |
| 43 | + } |
| 44 | + |
| 45 | + function _getRemainingSigners(CrossStore.AuthStorage storage s, bytes32 txID) |
| 46 | + internal |
| 47 | + view |
| 48 | + returns (Account.Data[] memory out) |
| 49 | + { |
| 50 | + uint256 total = s.requiredAccounts[txID].length; |
| 51 | + uint256 need = s.remainingCount[txID]; |
| 52 | + out = new Account.Data[](need); |
| 53 | + uint256 p = 0; |
| 54 | + for (uint256 i = 0; i < total; ++i) { |
| 55 | + Account.Data memory acc = s.requiredAccounts[txID][i]; |
| 56 | + if (s.remaining[txID][_accountKey(acc)]) { |
| 57 | + out[p] = acc; |
| 58 | + ++p; |
| 59 | + if (p == need) break; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + function _setStateFromRemainingList(CrossStore.AuthStorage storage s, bytes32 txID, Account.Data[] memory list) |
| 65 | + internal |
| 66 | + { |
| 67 | + for (uint256 i = 0; i < list.length; ++i) { |
| 68 | + bytes32 key = _accountKey(list[i]); |
| 69 | + if (!s.remaining[txID][key]) { |
| 70 | + s.remaining[txID][key] = true; |
| 71 | + ++s.remainingCount[txID]; |
| 72 | + s.requiredAccounts[txID].push(list[i]); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + function _accountKey(Account.Data memory a) internal pure returns (bytes32) { |
| 78 | + return keccak256(abi.encode(a.id, a.auth_type)); |
| 79 | + } |
| 80 | +} |
0 commit comments