|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; |
| 5 | +import "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol"; |
| 6 | +import "./PassportBuilderScore.sol"; |
| 7 | +import "@openzeppelin/contracts/access/Ownable.sol"; |
| 8 | + |
| 9 | +contract TalentBuilderScore is Ownable { |
| 10 | + using ECDSA for bytes32; |
| 11 | + |
| 12 | + address public trustedSigner; |
| 13 | + address public feeReceiver; |
| 14 | + PassportBuilderScore public passportBuilderScore; |
| 15 | + PassportRegistry public passportRegistry; |
| 16 | + uint256 public cost = 0.0001 ether; |
| 17 | + |
| 18 | + event BuilderScoreSet(address indexed user, uint256 score, uint256 talentId); |
| 19 | + |
| 20 | + bool public enabled; |
| 21 | + |
| 22 | + constructor( |
| 23 | + address _trustedSigner, |
| 24 | + address _passportBuilderScoreAddress, |
| 25 | + address _passportRegistryAddress, |
| 26 | + address _feeReceiver |
| 27 | + ) Ownable(_trustedSigner) { |
| 28 | + trustedSigner = _trustedSigner; |
| 29 | + passportBuilderScore = PassportBuilderScore(_passportBuilderScoreAddress); |
| 30 | + passportRegistry = PassportRegistry(_passportRegistryAddress); |
| 31 | + feeReceiver = _feeReceiver; |
| 32 | + enabled = true; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @notice Changes the owner of passport registry. |
| 37 | + * @param _newOwner The new owner of passport registry. |
| 38 | + * @dev Can only be called by the owner. |
| 39 | + */ |
| 40 | + function setPassportRegistryOwner(address _newOwner) public onlyOwner { |
| 41 | + passportRegistry.transferOwnership(_newOwner); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @notice Enables or disables the SmartBuilderScore contract. |
| 46 | + * @param _enabled Whether the SmartBuilderScore contract should be enabled. |
| 47 | + * @dev Can only be called by the owner. |
| 48 | + */ |
| 49 | + function setEnabled(bool _enabled) public onlyOwner { |
| 50 | + enabled = _enabled; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @notice Disables the SmartBuilderScore contract. |
| 55 | + * @dev Can only be called by the owner. |
| 56 | + */ |
| 57 | + function setDisabled() public onlyOwner { |
| 58 | + enabled = false; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @notice Sets the cost of adding a score. |
| 63 | + * @param _cost The cost of adding a score. |
| 64 | + * @dev Can only be called by the owner. |
| 65 | + */ |
| 66 | + function setCost(uint256 _cost) public onlyOwner { |
| 67 | + cost = _cost; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @notice Updates the fee receiver address. |
| 72 | + * @param _feeReceiver The new fee receiver address. |
| 73 | + * @dev Can only be called by the owner. |
| 74 | + */ |
| 75 | + function updateReceiver(address _feeReceiver) public onlyOwner { |
| 76 | + feeReceiver = _feeReceiver; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @notice Creates an attestation if the provided number is signed by the trusted signer. |
| 81 | + * @param score The number to be attested. |
| 82 | + * @param talentId The number of the talent profile to receive the attestation. |
| 83 | + * @param wallet The wallet to receive the attestation. |
| 84 | + * @param signature The signature of the trusted signer. |
| 85 | + */ |
| 86 | + function addScore(uint256 score, uint256 talentId, address wallet, bytes memory signature) public payable { |
| 87 | + require(enabled, "Setting the Builder Score is disabled for this contract"); |
| 88 | + // Ensure the caller has paid the required fee |
| 89 | + require(msg.value >= cost, "Insufficient payment"); |
| 90 | + // Hash the number |
| 91 | + bytes32 numberHash = keccak256(abi.encodePacked(score, talentId, wallet)); |
| 92 | + |
| 93 | + // Recover the address that signed the hash |
| 94 | + address signer = MessageHashUtils.toEthSignedMessageHash(numberHash).recover(signature); |
| 95 | + |
| 96 | + // Ensure the signer is the trusted signer |
| 97 | + require(signer == trustedSigner, "Invalid signature"); |
| 98 | + |
| 99 | + // Transfer fee to fee receiver |
| 100 | + payable(feeReceiver).transfer(msg.value); |
| 101 | + |
| 102 | + // Create passport if it does not exist |
| 103 | + if(passportRegistry.idPassport(talentId) == address(0)) { |
| 104 | + passportRegistry.adminCreate("talent_builder_score", wallet, talentId); |
| 105 | + } |
| 106 | + |
| 107 | + // Emit event |
| 108 | + require(passportBuilderScore.setScore(talentId, score), "Failed to set score"); |
| 109 | + emit BuilderScoreSet(wallet, score, talentId); |
| 110 | + } |
| 111 | +} |
0 commit comments