/ test / contracts / sophia_2 / basic_auth.aes
basic_auth.aes
 1  // Contract replicating "normal" Aeternity authentication
 2  contract BasicAuth =
 3    record state = { nonce : int, owner : address }
 4  
 5    function init() = { nonce = 1, owner = Call.caller }
 6  
 7    stateful function 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.ecverify(to_sign(tx_hash, n), state.owner, s)
14  
15    function to_sign(h : hash, n : int) =
16      Crypto.blake2b((h, n))
17  
18    // Not using builtin require because Sophia version handling in ga test
19    // suites is a bit broken (TODO).
20    private function _require(b : bool, err : string) =
21      if(!b) abort(err)