github.com/DapperCollectives/CAST/backend@v0.0.0-20230921221157-1350c8be7c96/main/cadence/float/FLOATVerifiers.cdc (about)

     1  import FLOAT from 0xf8d6e0586b0a20c7
     2  import FungibleToken from 0xee82856bf20e2aa6 
     3  import FlowToken from 0x0ae53cb6e3f42a79
     4  
     5  pub contract FLOATVerifiers {
     6  
     7      // The "verifiers" to be used
     8      
     9      //
    10      // Timelock
    11      //
    12      // Specifies a time range in which the 
    13      // FLOAT from an event can be claimed
    14      pub struct Timelock: FLOAT.IVerifier {
    15          // An automatic switch handled by the contract
    16          // to stop people from claiming after a certain time.
    17          pub let dateStart: UFix64
    18          pub let dateEnding: UFix64
    19  
    20          pub fun verify(_ params: {String: AnyStruct}) {
    21              assert(
    22                  getCurrentBlock().timestamp >= self.dateStart,
    23                  message: "This FLOAT Event has not started yet."
    24              )
    25              assert(
    26                  getCurrentBlock().timestamp <= self.dateEnding,
    27                  message: "Sorry! The time has run out to mint this FLOAT."
    28              )
    29          }
    30  
    31          init(_dateStart: UFix64, _timePeriod: UFix64) {
    32              self.dateStart = _dateStart
    33              self.dateEnding = self.dateStart + _timePeriod
    34          }
    35      }
    36  
    37      //
    38      // Secret
    39      //
    40      // Specifies a secret code in order
    41      // to claim a FLOAT (not very secure, but cool feature)
    42      pub struct Secret: FLOAT.IVerifier {
    43          // The secret code, set by the owner of this event.
    44          access(self) let secretPhrase: String
    45  
    46          pub fun verify(_ params: {String: AnyStruct}) {
    47              let secretPhrase = params["secretPhrase"]! as! String
    48              assert(
    49                  self.secretPhrase == secretPhrase, 
    50                  message: "You did not input the correct secret phrase."
    51              )
    52          }
    53  
    54          init(_secretPhrase: String) {
    55              self.secretPhrase = _secretPhrase
    56          }
    57      }
    58  
    59      //
    60      // Limited
    61      //
    62      // Specifies a limit for the amount of people
    63      // who can CLAIM. Not to be confused with how many currently
    64      // hold a FLOAT from this event, since users can
    65      // delete their FLOATs.
    66      pub struct Limited: FLOAT.IVerifier {
    67          pub var capacity: UInt64
    68  
    69          pub fun verify(_ params: {String: AnyStruct}) {
    70              let event = params["event"]! as! &FLOAT.FLOATEvent{FLOAT.FLOATEventPublic}
    71              let currentCapacity = event.totalSupply
    72              assert(
    73                  currentCapacity < self.capacity,
    74                  message: "This FLOAT Event is at capacity."
    75              )
    76          }
    77  
    78          init(_capacity: UInt64) {
    79              self.capacity = _capacity
    80          }
    81      }
    82  
    83      //
    84      // MultipleSecret
    85      //
    86      // Allows for Multiple Secret codes
    87      // Everytime a secret gets used, it gets removed
    88      // from the list.
    89      pub struct MultipleSecret: FLOAT.IVerifier {
    90          access(self) let secrets: {String: Bool}
    91  
    92          pub fun verify(_ params: {String: AnyStruct}) {
    93              let secretPhrase = params["secretPhrase"]! as! String
    94              assert(
    95                  self.secrets[secretPhrase] != nil, 
    96                  message: "You did not input a correct secret phrase."
    97              )
    98              self.secrets.remove(key: secretPhrase)
    99          }
   100  
   101          init(_secrets: [String]) {
   102              self.secrets = {}
   103              for secret in _secrets {
   104                  self.secrets[secret] = true
   105              }
   106          }
   107      }
   108  
   109      //
   110      // SecretV2
   111      //
   112      // Much more secure than Secret
   113      pub struct SecretV2: FLOAT.IVerifier {
   114          pub let publicKey: String
   115  
   116          pub fun verify(_ params: {String: AnyStruct}) {
   117              let data: [UInt8] = (params["claimee"]! as! Address).toString().utf8
   118              let sig: [UInt8] = (params["secretSig"]! as! String).decodeHex()
   119              let publicKey = PublicKey(publicKey: self.publicKey.decodeHex(), signatureAlgorithm: SignatureAlgorithm.ECDSA_P256)
   120              let valid = publicKey.verify(signature: sig, signedData: data, domainSeparationTag: "FLOW-V0.0-user", hashAlgorithm: HashAlgorithm.SHA3_256)
   121              
   122              assert(
   123                  valid, 
   124                  message: "You did not input the correct secret phrase."
   125              )
   126          }
   127  
   128          init(_publicKey: String) {
   129              self.publicKey = _publicKey
   130          }
   131      }
   132  
   133      //
   134      // MinimumBalance
   135      //
   136      // Requires a minimum Flow Balance to succeed
   137      pub struct MinimumBalance: FLOAT.IVerifier {
   138          pub let amount: UFix64
   139  
   140          pub fun verify(_ params: {String: AnyStruct}) {
   141              let claimee: Address = params["claimee"]! as! Address
   142              let flowVault = getAccount(claimee).getCapability(/public/flowTokenBalance)
   143                                  .borrow<&FlowToken.Vault{FungibleToken.Balance}>()
   144                                  ?? panic("Could not borrow the Flow Token Vault")
   145              
   146              assert(
   147                  flowVault.balance >= self.amount, 
   148                  message: "You do not meet the minimum required Flow Token balance."
   149              )
   150          }
   151  
   152          init(_amount: UFix64) {
   153              self.amount = _amount
   154          }
   155      }
   156  }