/ test / contracts / bitcoin_auth.aes
bitcoin_auth.aes
 1  contract BitcoinAuth =
 2    record state = { nonce : int, owner : bytes(64) }
 3  
 4    entrypoint init(owner' : bytes(64)) = { nonce = 1, owner = owner' }
 5  
 6    stateful entrypoint authorize(n : int, s : signature) : 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_hash)
11        None          => abort("Not in Auth context")
12        Some(tx_hash) => Crypto.verify_sig_secp256k1(to_sign(tx_hash, n), state.owner, s)
13  
14    entrypoint to_sign(h : hash, n : int) : hash =
15      Crypto.blake2b((h, n))
16  
17    function nonce() : int =
18      state.nonce
19