digital-asset / daml-finance

Apache License 2.0
17 stars 16 forks source link

Generation of `KeyTable` keys off-ledger #999

Open lucianojoublanc-da opened 1 year ago

lucianojoublanc-da commented 1 year ago

At present, the version of Daml Finance in the snapshot-vnext branch relies on the following code to generate keys:

https://github.com/digital-asset/daml-finance/blob/484dcab2b66a5366636353bae351deae23b0b6f8/src/main/daml/Daml/Finance/Interface/Util/KeyTable.daml#L67C33-L67C33

The use of show particularly is problematic; it relies on the internal representation of Show, which is often derived i.e. deriving (Eq, Show), and to provide a keyTable off-ledger, we need to generate this manually.

We haven't run into this problem yet as our tests are run using Daml script, which provides access to the show instance.

One workaround would be to provide a manual instance of show e.g.

show = DA.Text.intercalate "|" $ show <$> [hodling.id, holding.registrar, holding.depository]
-- "Account 1|GS123|JPM234"

But this is also quite clunky (what happens if a customer uses | in their account keys?)

A less problematic issue is also the use of a hash here. What happens if we have a clash? (e.g. two keys that have the same string such as "1" (numbered accounts and clients)).

cc @johan-da

lucianojoublanc-da commented 1 year ago

A suggestion is to add a constraint to the HasSyntheticKey class HasField "id" Id t, i.e. that the type has a field called id of type Id (which is itself a newtype Text). This would make things much easier off-ledger, and keys would be more meaningful. On the other hand, this would make key clashes more likely (e.g. two users with a Savings account).