github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/precompile_example/KeyHelper.sol (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  pragma solidity >=0.5.0 <0.9.0;
     3  pragma experimental ABIEncoderV2;
     4  
     5  // This file was copied from github.com/hashgraph/hedera-smart-contracts on Sep 27 2023
     6  
     7  import "./HederaTokenService.sol";
     8  
     9  abstract contract KeyHelper {
    10      using Bits for uint256;
    11      address supplyContract;
    12  
    13      mapping(KeyType => uint256) keyTypes;
    14  
    15      enum KeyType {
    16          ADMIN,
    17          KYC,
    18          FREEZE,
    19          WIPE,
    20          SUPPLY,
    21          FEE,
    22          PAUSE
    23      }
    24      enum KeyValueType {
    25          INHERIT_ACCOUNT_KEY,
    26          CONTRACT_ID,
    27          ED25519,
    28          SECP256K1,
    29          DELEGETABLE_CONTRACT_ID
    30      }
    31  
    32      constructor() {
    33          keyTypes[KeyType.ADMIN] = 1;
    34          keyTypes[KeyType.KYC] = 2;
    35          keyTypes[KeyType.FREEZE] = 4;
    36          keyTypes[KeyType.WIPE] = 8;
    37          keyTypes[KeyType.SUPPLY] = 16;
    38          keyTypes[KeyType.FEE] = 32;
    39          keyTypes[KeyType.PAUSE] = 64;
    40      }
    41  
    42      function getDefaultKeys() internal view returns (IHederaTokenService.TokenKey[] memory keys) {
    43          keys = new IHederaTokenService.TokenKey[](2);
    44          keys[0] = getSingleKey(KeyType.KYC, KeyValueType.CONTRACT_ID, '');
    45          keys[1] = IHederaTokenService.TokenKey(
    46              getDuplexKeyType(KeyType.SUPPLY, KeyType.PAUSE),
    47              getKeyValueType(KeyValueType.CONTRACT_ID, '')
    48          );
    49      }
    50  
    51      function getAllTypeKeys(KeyValueType keyValueType, bytes memory key)
    52          internal
    53          view
    54          returns (IHederaTokenService.TokenKey[] memory keys)
    55      {
    56          keys = new IHederaTokenService.TokenKey[](1);
    57          keys[0] = IHederaTokenService.TokenKey(getAllKeyTypes(), getKeyValueType(keyValueType, key));
    58      }
    59  
    60      function getCustomSingleTypeKeys(
    61          KeyType keyType,
    62          KeyValueType keyValueType,
    63          bytes memory key
    64      ) internal view returns (IHederaTokenService.TokenKey[] memory keys) {
    65          keys = new IHederaTokenService.TokenKey[](1);
    66          keys[0] = IHederaTokenService.TokenKey(getKeyType(keyType), getKeyValueType(keyValueType, key));
    67      }
    68  
    69      function getCustomDuplexTypeKeys(
    70          KeyType firstType,
    71          KeyType secondType,
    72          KeyValueType keyValueType,
    73          bytes memory key
    74      ) internal view returns (IHederaTokenService.TokenKey[] memory keys) {
    75          keys = new IHederaTokenService.TokenKey[](1);
    76          keys[0] = IHederaTokenService.TokenKey(
    77              getDuplexKeyType(firstType, secondType),
    78              getKeyValueType(keyValueType, key)
    79          );
    80      }
    81  
    82      function getSingleKey(
    83          KeyType keyType,
    84          KeyValueType keyValueType,
    85          bytes memory key
    86      ) internal view returns (IHederaTokenService.TokenKey memory tokenKey) {
    87          tokenKey = IHederaTokenService.TokenKey(getKeyType(keyType), getKeyValueType(keyValueType, key));
    88      }
    89  
    90      function getSingleKey(
    91          KeyType keyType,
    92          KeyValueType keyValueType,
    93          address key
    94      ) internal view returns (IHederaTokenService.TokenKey memory tokenKey) {
    95          tokenKey = IHederaTokenService.TokenKey(getKeyType(keyType), getKeyValueType(keyValueType, key));
    96      }
    97  
    98      function getSingleKey(
    99          KeyType firstType,
   100          KeyType secondType,
   101          KeyValueType keyValueType,
   102          bytes memory key
   103      ) internal view returns (IHederaTokenService.TokenKey memory tokenKey) {
   104          tokenKey = IHederaTokenService.TokenKey(
   105              getDuplexKeyType(firstType, secondType),
   106              getKeyValueType(keyValueType, key)
   107          );
   108      }
   109  
   110      function getDuplexKeyType(KeyType firstType, KeyType secondType) internal pure returns (uint256 keyType) {
   111          keyType = keyType.setBit(uint8(firstType));
   112          keyType = keyType.setBit(uint8(secondType));
   113      }
   114  
   115      function getAllKeyTypes() internal pure returns (uint256 keyType) {
   116          keyType = keyType.setBit(uint8(KeyType.ADMIN));
   117          keyType = keyType.setBit(uint8(KeyType.KYC));
   118          keyType = keyType.setBit(uint8(KeyType.FREEZE));
   119          keyType = keyType.setBit(uint8(KeyType.WIPE));
   120          keyType = keyType.setBit(uint8(KeyType.SUPPLY));
   121          keyType = keyType.setBit(uint8(KeyType.FEE));
   122          keyType = keyType.setBit(uint8(KeyType.PAUSE));
   123      }
   124  
   125      function getKeyType(KeyType keyType) internal view returns (uint256) {
   126          return keyTypes[keyType];
   127      }
   128  
   129      function getKeyValueType(KeyValueType keyValueType, bytes memory key)
   130          internal
   131          view
   132          returns (IHederaTokenService.KeyValue memory keyValue)
   133      {
   134          if (keyValueType == KeyValueType.INHERIT_ACCOUNT_KEY) {
   135              keyValue.inheritAccountKey = true;
   136          } else if (keyValueType == KeyValueType.CONTRACT_ID) {
   137              keyValue.contractId = supplyContract;
   138          } else if (keyValueType == KeyValueType.ED25519) {
   139              keyValue.ed25519 = key;
   140          } else if (keyValueType == KeyValueType.SECP256K1) {
   141              keyValue.ECDSA_secp256k1 = key;
   142          } else if (keyValueType == KeyValueType.DELEGETABLE_CONTRACT_ID) {
   143              keyValue.delegatableContractId = supplyContract;
   144          }
   145      }
   146  
   147      function getKeyValueType(KeyValueType keyValueType, address keyAddress)
   148          internal
   149          pure
   150          returns (IHederaTokenService.KeyValue memory keyValue)
   151      {
   152          if (keyValueType == KeyValueType.CONTRACT_ID) {
   153              keyValue.contractId = keyAddress;
   154          } else if (keyValueType == KeyValueType.DELEGETABLE_CONTRACT_ID) {
   155              keyValue.delegatableContractId = keyAddress;
   156          }
   157      }
   158  }
   159  
   160  library Bits {
   161      uint256 internal constant ONE = uint256(1);
   162  
   163      // Sets the bit at the given 'index' in 'self' to '1'.
   164      // Returns the modified value.
   165      function setBit(uint256 self, uint8 index) internal pure returns (uint256) {
   166          return self | (ONE << index);
   167      }
   168  }