/ test / contracts / tx_auth.aes
tx_auth.aes
  1  contract TxAuth =
  2    record state = { nonce : int }
  3  
  4    entrypoint init() = { nonce = 1 }
  5  
  6    stateful entrypoint authorize(n : int) : bool =
  7      require(n >= state.nonce, "Nonce too low")
  8      require(n =< state.nonce, "Nonce too high")
  9      put(state{ nonce = n + 1 })
 10      switch(Auth.tx)
 11        None      => abort("Not in Auth context")
 12        Some(tx0) =>
 13          switch(tx0.tx)
 14            Chain.SpendTx(_, amount, _)  => amount > 400
 15            Chain.ContractCreateTx(_)    => true
 16            Chain.ContractCallTx(_, _)   => true
 17            _                            => false
 18  
 19    stateful entrypoint authorize_named_call(n : int, addr : address) : bool =
 20      require(n >= state.nonce, "Nonce too low")
 21      require(n =< state.nonce, "Nonce too high")
 22      put(state{ nonce = n + 1 })
 23      switch(Auth.tx)
 24        None      => abort("Not in Auth context")
 25        Some(tx0) =>
 26          switch(tx0.tx)
 27            Chain.ContractCreateTx(_)    => true
 28            Chain.ContractCallTx(ct, _)  => ct == addr
 29            _                            => false
 30  
 31  
 32    entrypoint test_no_auth_tx() =
 33      None == Auth.tx
 34  
 35    entrypoint test_auth_tx_fee(limit : int) =
 36      let Some({fee = fee}) = Auth.tx
 37      fee > limit
 38  
 39    entrypoint test_auth_tx_actor(addr : address) =
 40      let Some({actor = actor}) = Auth.tx
 41      actor == addr
 42  
 43    entrypoint test_auth_tx_ttl(limit : int) =
 44      let Some({ttl = ttl}) = Auth.tx
 45      ttl > limit
 46  
 47    entrypoint test_auth_tx_no_paying_for() =
 48      let Some({paying_for = pf }) = Auth.tx
 49      None == pf
 50  
 51    entrypoint test_auth_tx_paying_for(p : address, f : int) =
 52      let Some({paying_for = Some(Chain.PayingForTx(px, fx)) }) = Auth.tx
 53      p == px && f == fx
 54  
 55    entrypoint test_auth_tx_no_ga_metas() =
 56      let Some({ga_metas = gms }) = Auth.tx
 57      [] == gms
 58  
 59    entrypoint test_auth_tx_one_ga_metas(gm : address, f : int) =
 60      let Some({ga_metas = [Chain.GAMetaTx(gmx, fx)] }) = Auth.tx
 61      gm == gmx && f == fx
 62  
 63    entrypoint test_auth_tx_two_ga_metas(gm1 : address, f1 : int, gm2 : address, f2 : int) =
 64      let Some({ga_metas = [Chain.GAMetaTx(gmx1, fx1), Chain.GAMetaTx(gmx2, fx2)] }) = Auth.tx
 65      gm1 == gmx1 && f1 == fx1 && gm2 == gmx2 && f2 == fx2
 66  
 67    entrypoint test_auth_tx_spend(recv : address, amt : int, payload : string) =
 68      let Some({tx = Chain.SpendTx(recvx, amtx, payloadx)}) = Auth.tx
 69      recv == recvx && amt == amtx && payload == payloadx
 70  
 71    entrypoint test_auth_tx_oracle_register() =
 72      let Some({tx = Chain.OracleRegisterTx}) = Auth.tx
 73      true
 74  
 75    entrypoint test_auth_tx_oracle_query() =
 76      let Some({tx = Chain.OracleQueryTx}) = Auth.tx
 77      true
 78  
 79    entrypoint test_auth_tx_oracle_response() =
 80      let Some({tx = Chain.OracleResponseTx}) = Auth.tx
 81      true
 82  
 83    entrypoint test_auth_tx_oracle_extend() =
 84      let Some({tx = Chain.OracleExtendTx}) = Auth.tx
 85      true
 86  
 87    entrypoint test_auth_tx_name_preclaim() =
 88      let Some({tx = Chain.NamePreclaimTx}) = Auth.tx
 89      true
 90  
 91    entrypoint test_auth_tx_name_claim(n : string) =
 92      let Some({tx = Chain.NameClaimTx(nx)}) = Auth.tx
 93      n == nx
 94  
 95    entrypoint test_auth_tx_name_update(nh : hash) =
 96      let Some({tx = Chain.NameUpdateTx(nhx)}) = Auth.tx
 97      nh == nhx
 98  
 99    entrypoint test_auth_tx_name_revoke(nh : hash) =
100      let Some({tx = Chain.NameRevokeTx(nhx)}) = Auth.tx
101      nh == nhx
102  
103    entrypoint test_auth_tx_name_transfer(to : address, nh : hash) =
104      let Some({tx = Chain.NameTransferTx(tox, nhx)}) = Auth.tx
105      to == tox && nh == nhx
106  
107    entrypoint test_auth_tx_channel_create(o : address) =
108      let Some({tx = Chain.ChannelCreateTx(ox)}) = Auth.tx
109      o == ox
110  
111    entrypoint test_auth_tx_channel_deposit(ch : address, amt : int) =
112      let Some({tx = Chain.ChannelDepositTx(chx, amtx)}) = Auth.tx
113      ch == chx && amt == amtx
114  
115    entrypoint test_auth_tx_channel_withdraw(ch : address, amt : int) =
116      let Some({tx = Chain.ChannelWithdrawTx(chx, amtx)}) = Auth.tx
117      ch == chx && amt == amtx
118  
119    entrypoint test_auth_tx_channel_force_progress(ch : address) =
120      let Some({tx = Chain.ChannelForceProgressTx(chx)}) = Auth.tx
121      ch == chx
122  
123    entrypoint test_auth_tx_channel_close_mutual(ch : address) =
124      let Some({tx = Chain.ChannelCloseMutualTx(chx)}) = Auth.tx
125      ch == chx
126  
127    entrypoint test_auth_tx_channel_close_solo(ch : address) =
128      let Some({tx = Chain.ChannelCloseSoloTx(chx)}) = Auth.tx
129      ch == chx
130  
131    entrypoint test_auth_tx_channel_slash(ch : address) =
132      let Some({tx = Chain.ChannelSlashTx(chx)}) = Auth.tx
133      ch == chx
134  
135    entrypoint test_auth_tx_channel_settle(ch : address) =
136      let Some({tx = Chain.ChannelSettleTx(chx)}) = Auth.tx
137      ch == chx
138  
139    entrypoint test_auth_tx_channel_snapshot_solo(ch : address) =
140      let Some({tx = Chain.ChannelSnapshotSoloTx(chx)}) = Auth.tx
141      ch == chx
142  
143    entrypoint test_auth_tx_contract_create(amt : int) =
144      let Some({tx = Chain.ContractCreateTx(amtx)}) = Auth.tx
145      amt == amtx
146  
147    entrypoint test_auth_tx_contract_call(ct : address, amt : int) =
148      let Some({tx = Chain.ContractCallTx(ctx, amtx)}) = Auth.tx
149      ct == ctx && amt == amtx
150  
151    entrypoint test_auth_tx_ga_attach() =
152      let Some({tx = Chain.GAAttachTx}) = Auth.tx
153      true
154