# Key assertions

**URL:** <https://forum.canton.network/t/key-assertions/5910>\
**Category:** App Development\
**Created:** [January 4, 2023, 6:06pm UTC](https://forum.canton.network/t/key-assertions/5910 "2023-01-04T18:06:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neelam\_Dwivedi](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/neelam_dwivedi/32/4719_2.png) [@Neelam\_Dwivedi](https://forum.canton.network/u/Neelam_Dwivedi)\
**Post date:** [January 4, 2023, 6:06pm UTC](https://forum.canton.network/t/key-assertions/5910/1 "2023-01-04T18:06:00Z")

</div>

The docs [here](https://docs.daml.com/concepts/ledger-model/ledger-structure.html) says that ‘Key assertions’ is one of the four actions in a transaction tree. However, there is no one ‘key assertion’ function or keyword in Daml. So what does this refer to? Is it any assertion involving keys, or is it a set of statements such as exerciseByKey, lookupByKey, NoSuchKey… etc?

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [January 4, 2023, 6:51pm UTC](https://forum.canton.network/t/key-assertions/5910/2 "2023-01-04T18:51:59Z")

</div>

Key assertion here refers to a negative lookupByKey. A positive key lookup is implicitly recorded as part of a fetch or exercise of a contract with the given key but we need a special primitive to record that there is no contract with a given key.

However, in practice, this is a case where the ledger model is slightly out of sync with the implementation. In the implementation, we don’t use NoSuchKey assertions but instead we have a separate LookupByKey node which can result both negative and positive assertions (fetchByKey and exerciseByKey are still recorded through regular fetch/exercise). We track that mismatch in [Align Ledger Model docs to implementation · Issue #7113 · digital-asset/daml · GitHub](https://github.com/digital-asset/daml/issues/7113).

---

<div class="post-metadata">

**Author:** ![Neelam\_Dwivedi](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/neelam_dwivedi/32/4719_2.png) [@Neelam\_Dwivedi](https://forum.canton.network/u/Neelam_Dwivedi)\
**Post date:** [January 4, 2023, 6:59pm UTC](https://forum.canton.network/t/key-assertions/5910/3 "2023-01-04T18:59:48Z")

</div>

So is it correct to say that the key assertion here means lookupByKey, and just that?

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [January 4, 2023, 7:00pm UTC](https://forum.canton.network/t/key-assertions/5910/4 "2023-01-04T19:00:41Z")

</div>

In the ledger model, it’s even more restrictive. It’s only a negative lookupByKey so one that returns None or in other words a NoSuchKey assertion.

---

<div class="post-metadata">

**Author:** ![Neelam\_Dwivedi](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/neelam_dwivedi/32/4719_2.png) [@Neelam\_Dwivedi](https://forum.canton.network/u/Neelam_Dwivedi)\
**Post date:** [January 4, 2023, 7:17pm UTC](https://forum.canton.network/t/key-assertions/5910/5 "2023-01-04T19:17:55Z")

</div>

Thanks @cocreature! For some reason, I am still having a hard time wrapping my head around this. Can you please point me to some code that uses this concept of ‘key assertion’?

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [January 4, 2023, 8:05pm UTC](https://forum.canton.network/t/key-assertions/5910/6 "2023-01-04T20:05:20Z")

</div>

Consider this Daml choice:

```haskell
template T
  with
    …
  choice C : ()
    with
       k : SomeKeyType
  do None <- lookupByKey @SomeOtherTemplate k
     pure ()  

```

And now let’s say we do something like `exercise cid (C yourkey)`.

The resulting transaction in the ledger model will look something like this:

```haskell
exercise cid (C yourkey) -- That’s the exercise node as the root
  - NoSuchKey @SomeTemplate yourkey -- That’s the key assertion which is a child node of the exercise

```

Another way of looking at this is to take a look at the transaction graph in Daml studio. As I mentioned above the implementation uses LookupByKey nodes rather than “NoSuchKey” assertions but if you mentally replace a key lookup with “not found” by “NoSuchKey” you will see the same structure:

![Screenshot from 2023-01-04 21-03-48](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/6/6dab827c4dfc15f97883491b341c50b03bdf157f.png)

```haskell
module Main where

import Daml.Script

data SomeKeyType = SomeKeyType 
  with
    p : Party
  deriving (Eq, Show)

template SomeTemplate
  with
    p : Party
  where
    signatory p
    key (SomeKeyType p) : SomeKeyType
    maintainer key.p

template T
  with
    p : Party
  where
    signatory p
    choice C : ()
      with
        k : SomeKeyType
      controller p
      do None <- lookupByKey @SomeTemplate k
         pure ()

script = do
  p <- allocateParty "p"
  c <- submit p $ createCmd (T p)
  submit p $ exerciseCmd c (C (SomeKeyType p))

```

---

<div class="post-metadata">

**Author:** ![Neelam\_Dwivedi](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/neelam_dwivedi/32/4719_2.png) [@Neelam\_Dwivedi](https://forum.canton.network/u/Neelam_Dwivedi)\
**Post date:** [January 4, 2023, 8:08pm UTC](https://forum.canton.network/t/key-assertions/5910/7 "2023-01-04T20:08:49Z")

</div>

Wow! Got it! Thanks a ton!
