/ test / contracts / basic_auth.aes
basic_auth.aes
 1  // Contract replicating "normal" Aeternity authentication
 2  contract BasicAuth =
 3    record state = { nonce : int, owner : address }
 4  
 5    entrypoint init() = { nonce = 1, owner = Call.caller }
 6  
 7    stateful entrypoint authorize(n : int, s : signature) : bool =
 8      _require(n >= state.nonce, "Nonce too low")
 9      _require(n =< state.nonce, "Nonce too high")
10      put(state{ nonce = n + 1 })
11      switch(Auth.tx_hash)
12        None          => abort("Not in Auth context")
13        Some(tx_hash) => Crypto.verify_sig(to_sign(tx_hash, n), state.owner, s)
14  
15    entrypoint to_sign(h : hash, n : int) =
16      Crypto.blake2b((h, n))
17  
18    entrypoint get_auth_tx_hash() = Auth.tx_hash
19  
20    // Not using builtin require because Sophia version handling in ga test
21    // suites is a bit broken (TODO).
22    function _require(b : bool, err : string) =
23      if(!b) abort(err)