|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity 0.8.25; |
| 3 | + |
| 4 | +import { |
| 5 | + AccessControlDefaultAdminRulesUpgradeable |
| 6 | +} from "@openzeppelin/contracts-upgradeable/access/extensions/AccessControlDefaultAdminRulesUpgradeable.sol"; |
| 7 | +import {BtcUtils} from "@rsksmart/btc-transaction-solidity-helper/contracts/BtcUtils.sol"; |
| 8 | +import {OpCodes} from "@rsksmart/btc-transaction-solidity-helper/contracts/OpCodes.sol"; |
| 9 | +import {EmergencyPause} from "./EmergencyPause/EmergencyPause.sol"; |
| 10 | +import {IBridge} from "./interfaces/IBridge.sol"; |
| 11 | +import {IPauseRegistry} from "./interfaces/IPauseRegistry.sol"; |
| 12 | +import {IPegInAddressRegistry} from "./interfaces/IPegInAddressRegistry.sol"; |
| 13 | +import {Flyover} from "./libraries/Flyover.sol"; |
| 14 | + |
| 15 | +/// @title PegInAddressRegistry |
| 16 | +/// @notice Maintains registered Flyover peg-in Rootstock addresses and derives the |
| 17 | +/// corresponding Bitcoin deposit addresses for the current powpeg composition |
| 18 | +contract PegInAddressRegistry is |
| 19 | + AccessControlDefaultAdminRulesUpgradeable, |
| 20 | + EmergencyPause, |
| 21 | + IPegInAddressRegistry |
| 22 | +{ |
| 23 | + struct Registration { |
| 24 | + uint256 blockNumber; |
| 25 | + address registrant; |
| 26 | + } |
| 27 | + |
| 28 | + /// @notice The version of the contract |
| 29 | + string public constant VERSION = "1.0.0"; |
| 30 | + |
| 31 | + IBridge private _bridge; |
| 32 | + bool private _mainnet; |
| 33 | + |
| 34 | + bytes32 private _registrationRoot; |
| 35 | + uint256 private _registrationCount; |
| 36 | + mapping(address => Registration) private _registrations; |
| 37 | + |
| 38 | + /// @notice Emitted when an address is already registered |
| 39 | + /// @param addr The Rootstock address that was already registered |
| 40 | + error AlreadyRegistered(address addr); |
| 41 | + |
| 42 | + /// @custom:oz-upgrades-unsafe-allow constructor |
| 43 | + constructor() { |
| 44 | + _disableInitializers(); |
| 45 | + } |
| 46 | + |
| 47 | + /// @notice Initializes the contract |
| 48 | + /// @param defaultAdmin The default admin of the contract |
| 49 | + /// @param bridge The Rootstock bridge contract |
| 50 | + /// @param mainnet Whether the contract derives mainnet or testnet BTC addresses |
| 51 | + /// @param pauseRegistry The central PauseRegistry for pause state |
| 52 | + // solhint-disable-next-line comprehensive-interface |
| 53 | + function initialize( |
| 54 | + address defaultAdmin, |
| 55 | + address payable bridge, |
| 56 | + bool mainnet, |
| 57 | + address pauseRegistry |
| 58 | + ) external initializer { |
| 59 | + if (address(pauseRegistry).code.length == 0) { |
| 60 | + revert Flyover.NoContract(address(pauseRegistry)); |
| 61 | + } |
| 62 | + __AccessControlDefaultAdminRules_init(0, defaultAdmin); |
| 63 | + __EmergencyPause_init(IPauseRegistry(pauseRegistry)); |
| 64 | + _bridge = IBridge(bridge); |
| 65 | + _mainnet = mainnet; |
| 66 | + } |
| 67 | + |
| 68 | + /// @inheritdoc IPegInAddressRegistry |
| 69 | + function registerAddress(address addr) external override whenNotSoftPaused { |
| 70 | + if (addr == address(0)) revert Flyover.InvalidAddress(addr); |
| 71 | + if (_isRegistered(addr)) revert AlreadyRegistered(addr); |
| 72 | + |
| 73 | + _registrations[addr] = Registration({ |
| 74 | + blockNumber: block.number, |
| 75 | + registrant: msg.sender |
| 76 | + }); |
| 77 | + ++_registrationCount; |
| 78 | + _registrationRoot = keccak256(abi.encodePacked(_registrationRoot, addr)); |
| 79 | + |
| 80 | + emit AddressRegistered(addr, msg.sender, _registrationRoot); |
| 81 | + } |
| 82 | + |
| 83 | + /// @inheritdoc IPegInAddressRegistry |
| 84 | + function getPegInAddress( |
| 85 | + address addr |
| 86 | + ) external view override returns (bytes memory derivationAddress, Encoding encoding) { |
| 87 | + return (_derivePegInAddress(addr), Encoding.BASE58); |
| 88 | + } |
| 89 | + |
| 90 | + /// @inheritdoc IPegInAddressRegistry |
| 91 | + function getPegInAddresses( |
| 92 | + address[] calldata addrs |
| 93 | + ) external view override returns (bytes[] memory derivationAddresses, Encoding encoding) { |
| 94 | + uint addressCount = addrs.length; |
| 95 | + derivationAddresses = new bytes[](addressCount); |
| 96 | + for (uint256 i = 0; i < addressCount; ++i) { |
| 97 | + derivationAddresses[i] = _derivePegInAddress(addrs[i]); |
| 98 | + } |
| 99 | + return (derivationAddresses, Encoding.BASE58); |
| 100 | + } |
| 101 | + |
| 102 | + /// @inheritdoc IPegInAddressRegistry |
| 103 | + function getRegistrationRoot() external view override returns (bytes32) { |
| 104 | + return _registrationRoot; |
| 105 | + } |
| 106 | + |
| 107 | + /// @inheritdoc IPegInAddressRegistry |
| 108 | + function isRegistered(address addr) external view override returns (bool) { |
| 109 | + return _isRegistered(addr); |
| 110 | + } |
| 111 | + |
| 112 | + /// @inheritdoc IPegInAddressRegistry |
| 113 | + function getRegistrationBlock(address addr) external view override returns (uint256) { |
| 114 | + return _registrations[addr].blockNumber; |
| 115 | + } |
| 116 | + |
| 117 | + /// @inheritdoc IPegInAddressRegistry |
| 118 | + function getRegistrant(address addr) external view override returns (address) { |
| 119 | + return _registrations[addr].registrant; |
| 120 | + } |
| 121 | + |
| 122 | + /// @inheritdoc IPegInAddressRegistry |
| 123 | + function getRegistrationCount() external view override returns (uint256) { |
| 124 | + return _registrationCount; |
| 125 | + } |
| 126 | + |
| 127 | + function _isRegistered(address addr) private view returns (bool) { |
| 128 | + return _registrations[addr].blockNumber != 0; |
| 129 | + } |
| 130 | + |
| 131 | + /// @notice Derives the Bitcoin P2SH deposit address for a Rootstock address |
| 132 | + /// @dev Builds the Flyover redeem script from the address hash and the active powpeg |
| 133 | + /// redeem script, then wraps it in a P2WSH-in-P2SH script as in `PegInContract`. |
| 134 | + function _derivePegInAddress(address addr) private view returns (bytes memory) { |
| 135 | + bytes32 derivationValue = keccak256(abi.encodePacked(addr)); |
| 136 | + bytes memory flyoverRedeemScript = bytes.concat( |
| 137 | + OpCodes.OP_PUSHBYTES_32, |
| 138 | + derivationValue, |
| 139 | + OpCodes.OP_DROP, |
| 140 | + _bridge.getActivePowpegRedeemScript() |
| 141 | + ); |
| 142 | + bytes memory segwitScript = bytes.concat( |
| 143 | + OpCodes.OP_0, |
| 144 | + OpCodes.OP_PUSHBYTES_32, |
| 145 | + sha256(flyoverRedeemScript) |
| 146 | + ); |
| 147 | + return BtcUtils.getP2SHAddressFromScript(segwitScript, _mainnet); |
| 148 | + } |
| 149 | +} |
0 commit comments