Subtransaction Privacy Demonstrated with Daml (Code Walkthrough)
Nothing is loaded from YouTube until you press play, and then without tracking cookies.
About this video
š To get started with Daml, visit bit.ly/trydaml š Check out our blog daml.com/blog/engineering
Watch this Daml code walkthrough to learn how we demonstrate subtransaction privacy.
Subtransaction Privacy Intro: Ā Ā Ā ā¢Ā SubtransactionĀ PrivacyĀ inĀ 7Ā Minutes
Canton Technical Primer: Ā Ā Ā ā¢Ā DamlĀ 2.0Ā CantonĀ TechnicalĀ PrimerĀ [2022]
00:00 Intro 00:12 Subtransaction Privacy Recap / Example 01:31 Using Daml Navigator to Illustrate SubTx Privacy 03:40 Code Walkthrough: Setup 04:40 Repository Intro 04:53 Code Walkthrough: daml.yaml 06:00 Code Walkthrough: README.md, notes 07:32 Code Walkthrough: Iou template 12:29 Code Walkthrough: IouTransfer Request template 16:13 Code Walkthrough: Share template 19:05 Code Walkthrough: ShareTransfer template 21:36 Code Walkthrough: Trade template, atomic swap 24:40 Code Walkthrough: Accepting the swap, atomic swap 29:40 Code Walkthrough: Creating test scripts 41:00 Start Daml Navigator
Transcript
6,064 wordsCaptions as YouTube publishes them, usually machine-generated, so names such as Daml and Canton can be misheard.
- 0:00
in this video we will demonstrate sub-transaction privacy through demo smart contracts first let's walk through a quick scenario to review what sub-transaction privacy is to illustrate sub-transaction privacy let's take a look at the following example here we have a scenario where Alice wants to buy some shares from Bob in reality there will be many intermediaries facilitating this
- 0:24
transaction but for simplicity's sake we will limit the participants to Alice Bob the bank and the share industry when Alice makes a transaction with Bob Alice is swapping some of her money for some of Bob's shares from the bank's perspective the only thing that the bank should see is a transfer of money from Alice to Bob the bank should have no knowledge of the share transfer
- 0:48
similarly from the view of the share registry the only thing that the share registry should see is a transfer of ownership of shares from Bob to Alice and no knowledge of the money being transferred meanwhile Bob and Alice would have a full view of the transaction this means that they can see the money transfer as well as the share transfer the visibility of data on a need to know basis from the involved stakeholders is
- 1:12
what we call sub-transaction privacy if you'd like to watch a more detailed video on the topic of sub-transaction privacy you can take a look at the linked resource in the description before we dive into the code I'm going to show you the final result using the download Navigator the demo Navigator is a tool that allows us to view and interact with demo contracts we'll be viewing the state after the transaction between Alice and Bob to take place
- 1:38
let's log in with the bank first we'll see contracts that the bank is able to see at first glance we can see an IOU contract we use an IOU to represent money if we click into it and look under contract details we can see that the issuers the bank as expected and the owner is Bob at the beginning Bob didn't have any iOS it was Alice that wanted to buy some shares therefore at the end of the transaction Bob now owns the ious which were previously owned by Alice
- 2:11
now if I go back and check include archived you can see the only contracts that are visible are the ones related to IOU transfers we don't see any contract related to share transfer the bank has no knowledge of a share transfer taking place next let's log in as the share industry again if we click into the only active contract here see the contract details this shows that Alice is now the owner of the shares let's go back and click on include
- 2:49
archived now if we examine these rows we can see share transfer and shares and nothing related to the IOU transfer therefore we can see that the share registry has no knowledge of any IOU transfers taking place we can say that sub-transaction privacy has been achieved let me log back out and take a look from Alice's point of view let me click on include archived and now you can see the swap proposal the share the share transfer Alice is able to see
- 3:24
all of the transactions that are involved in this swap this would apply for Bob as well now let's see how we can implement this in demo code the following coding portion is done on a Mac to get started go to a terminal and CD into directory where you want the project to be located in my case I have the folder named projects type Daniel new followed by the project name in my case I will use sub-transaction privacy example and then we need to add the flag
- 4:04
template equals skeleton this will give us the most basic setup which does not include a react front-end UI so then CD into our project so CD sub transaction privacy example and next run demo Studio will open up the IDE of our project in your project if we go to this demo directory we'll see a main.daml file now this main.dano file is the boilerplate code that we don't need at
- 4:45
the moment we're going to create our own templates so we can ignore this for now let's take a look at the demo.yaml file in line seven we're just going to trade change this main into trade because we'll have a startup script or an init script in a trade dot IOU file in a moment as we go through this walkthrough so the next thing we want to also do is add in Navigator options so we can just go anywhere here and type in Navigator
- 5:18
options and what we want is for us to use the feature user management set to false so let's go ahead and do that there we go and this allows you to log into the Navigator with the parties directly so I'm just going to go ahead and save this first now before we dive into the coding it's helpful to think who might be the involved parties and what the involved templates are so let me just create a markdown file here first we can just call it
- 5:59
readme.md and let's have a header here let's just call it parties this is just for our own note to work off of so we'll need to have Alice involved as the parties right there's Alice and there's Bob we're also going to have the bank and as well as the share registry next for the templates we need an IOU template so let's go ahead here and write down the templates that are needed and we're going to have an IOU template
- 6:31
this represents the money issued by the bank we'll need an IOU transfer request and this is part of the propose and accept pattern where in order to transfer an asset to someone the recipient needs to agree to receive it and that's what we use the IOU transfer request for we'll also need a share template and a share transfer request this also serves as part of the proposed and accept pattern in order for us to
- 7:04
transfer shares and finally we're going to need a swap contract and this will be responsible for creating the sub transactions let me go ahead and fix this so now that we have everything let's create our IOU template on the left hand side here let's create a new file and we'll call it iou.aml then at the very top here we're going to call the module IOU so we would just use the module keyword how you where
- 7:39
then at the very top we'll space out one line here and we'll Define the template by typing template IOU we'll Define the template by the fields that it should have as well so this IOU is going to have several Fields such as the issuer the owner the amount so we do that with the with keyword and we'll indent and we're going to be typing in the issuer of type party and in this case it's going to be the bank the bank is going to be the
- 8:17
issuer of the IOU the owner is also going to be of type party the amount is going to be of type decimal and this defines how much the IOU is worth then we'll also have a public field and this is just going to be a list of parties and what the public field does is that it can involve anyone who is going to be involved in the transaction so that they're able to see this IOU contract now that we have these fields defined we
- 8:48
also need to tell the contract who the signatories of the contract are that means with whose authority do we need to create the contract and this is done with a where block followed by a signatory keyword so we can go down here align the where with the with above you can see that there's alignment there and using the signatory keyword we're going to say it's the issuer as well as the owner so therefore in order to create this IOU there needs to be the authority the authorization from the
- 9:20
issuer as well as the owner and we use the Observer keyword to make the contract visible to whoever we Define it in this case we're going to be defining it as public so anyone in the list of parties here at line 8 will be able to see this IOU contract now that we have these fields on IOU we have the basic ingredients to create it however as the owner of the IOU will need to be able to do things with it and in this case we need to be able to have choices on this IU that the owner can
- 9:54
exercise such as transferring it to someone else with that in mind let's create a choice and we'll call it IOU underscore transfer request so let's just use the choice keyword let's call it transfer request and it's going to return a contract ID of type what exactly it's going to be the IOU transfer request now we haven't defined what the IOU transfer request template is yet but we'll be doing that after we Define the
- 10:29
IOU template now we can specify the argument that goes into this Choice by using the with keyword so if we drop down the line and we'll indent with new owner of type party what this means is whoever exercises this choice and in this case we want the person who exercises it to be the owner right so it should be controller the controller keyword defines who has the right to exercise
- 10:59
this choice and in this case it is the owner so the owner can exercise his choice and pass in a new owner and what this does is that it will use the new owner field in the next section of the block that it basically will use this line 14 argument in what we'll be defining underline 16. so what happens in this choice and let's go ahead and Define what happens in this Choice after someone exercises it and to do that we have to use the do keyword
- 11:35
do and what we want to do is create that IOU transfer request so we can just say create IOU transfer request with and these two dots basically means let's use the above fields in this new template so we're creating a transfer request and the reason why these are underlined in Red is because this IOU transfer request template doesn't exist so we're going to have to write that next but this right here is the IOU template and just to recap it's the owner that
- 12:09
can exercise the choice IO you transfer and when I'm exercising this IU transfer request I need to pass in the recipient or who the new owner of this new IU it's is going to be so let's drop down here and create the IOU transfer request similar to the top we're going to say template IOU transfer request and again there's going to be Fields associated with this template and it's going to be we're going to use the with keyword
- 12:47
followed by the fields so it's going to be an issue issuer of type party owner of type party is going to be same as the top amount of type decimal and public which is going to be a list of parties and finally we have the new owner which is also going to be of type party now whose authority do we need in this case we're going to need the issuer and the owner so we can just use the where keyword followed by the signatory keyword issuer
- 13:22
owner and who gets to see this contract well we can use the Observer keyword to disclose who we want to see who who gets to see the contract and in this case we're going to be using the party the parties in the public field because it's a list right there now what can the recipient of this IOU transfer request do so if someone received it they should be able to accept and reject this and for this example's sake we'll just type out or just code out the acceptance of this
- 13:54
choice so again using the choice keyword we'll just call it IOU accept and that returns a contract ID of type IOU and again who gets to control this choice in this case is going to be the new owner so you can use the controller keyword new owner and then we'll Define what happens once this choice is exercised with the do keyword and we're defining okay let's create an IOU with the owner
- 14:29
set as the new owner and fill in the remaining fields so let's clean this up a little bit now we've defined the trans the IOU transfer request with the choice value you accept and what happens is we're creating an IOU with the owner set as the new owner notice that in front of choice it's just Choice there is no keyword non-consuming which that would potentially work non-consuming just
- 15:03
means that once a choice is exercised this contract does not get archived by default when we exercise a choice the contract gets archived so in this example when we exercise IOU transfer request we are archiving an existing IOU and we are generating an IOU transfer request contract which is this template right here and then by exercising accepting how you accept we archive this template and we
- 15:37
generate a new IOU with the owner as the new owner a demo file should actually be in the demo directory so I'm just going to drag it in here and we'll move it okay now let's move on to the share.daml file so again on the left hand side we are going to create a share.daml file and in here we're going to be creating our shared templates and the share like I said earlier represents the company
- 16:10
shares so at the top we will say module share where and then we can Define our template share and again we'll need to think about what fields are associated with a company share so using the with keyword we can Define the fields similar to it's going to be similar to the IOU so we'll have an issuer of type party we'll have an owner of type party we can also add a ticker of type text so every share has a ticker and then again we'll add a public field of a list of
- 16:50
parties and this will just Define who is involved in the transaction who will be able to see this share contract we're going to add a where keyword and Define the signatories using the signatory keyword and the signatory just means with whose authorization we need to create this contract and in our case we need the issuer and we need the owner The Observer keyword allows us to Define who gets to see this contract and in this case it'll be everyone in
- 17:25
line 8 public field once we have that we need to think once I have the share what can I do with it and as the owner of the share I should be able to transfer the share to someone else whoever I want and that is a choice that exists on this share so again using the choice keyword followed by the name we're just going to call it share underscore transfer request and the transfer request is going to
- 17:57
return a contract ID of type what it will return a transfer request however the transfer request template has not been created it's similar to the IOU request it's going to be somewhat similar so we have defined the choice here but we need to exercise the choice with an argument right who who is going to be the new owner of this share so using the with keyword we can define a an argument name we're going to call it new owner a type party
- 18:36
and the controller of this choice is going to be the current owner right as the current owner I can Define I can choose who I want to transfer this share to then we're going to have a do block which just defines what happens in this code so it's going to create a transfer request with the above Fields again the red lines are here because the transfer request template has not been created so let's go ahead under in a new line we're going to create a template and we're
- 19:11
going to name it transfer request with the fields similar to the ones above issuer of type party owner of type party the new owner also of type party and then we have the ticker and finally the public which is a list of parties again who has again who is who authorizes the transfer request that is defined with a where block and a signatory keyword where we can say issuer and the owner so does the issuer
- 19:59
and owner they both agree to create this transfer request and we'll add an observer to disclose who is involved in this transfer request so that they can both see the contract now as the person receiving this transfer request I can accept it or reject it and again I'll just code out the acceptance choice using the choice keyword followed by the name of that choice we're going to call it share accept and it's going to return
- 20:29
a contract ID of type share which is just the one above and it is the controller I'm going to indent that it is the controller new owner so the new owner is able to accept it what happens after we exercise it we actually have to Define what happens in the code in a do block so we can just say do create share with owner set as new owner and we're passing in everything else right so the Dubach creates the share with the new owners with the owner set
- 21:10
as the new owner so what happens is the previous share contract gets archived and a new one is created with the new owner as the owner that's it for the IOU template and the shared template we've pretty much completed all the templates there will just be one more template to create that will facilitate the atomic swap of the share and the ious this template is also very interesting because you'll see that it will produce all the properties of sub
- 21:45
transaction privacy but we don't actually have to explicitly implement it so let's create our final demo file and we'll call it trade.damel so in the demo directory we're going to call it trade dot dowel and similarly to everything and all the other templates that we've done we'll call this a module trade where and this template or this module will specifically use the share and the IOU templates so we can first go ahead and
- 22:16
import the share and we can also import IOU we'll also will also need to import the demo.scripts and this will help us test and interact with our demo contracts so let's go ahead and import demo.script so we have everything that we need we can start on our swap file so at the top we're going to Define our template and we'll call it swap proposal with fields initiator so the person who
- 22:55
is initiating the swap and the recipient of the swap is also going to be of type party we'll have the share CID because the person initiating the swap is going to be the person with the shares so I will have my contract ID of type share and the amount so how much am I willing to swap my shares for right I can say Okay I want to have it for a thousand dollars now we need to Define with whose authorization we need to create this swap proposal so we'll use the signatory
- 23:33
keyword and it will be the initiator in this case and then we'll have our Observer which is basically who is able to see this swap proposal and of course it will be the recipient of this contract finally the recipient is going to be able to accept or reject the swap proposal so let's go ahead and code out the choice using the choice keyword we'll call this Choice name accept and it doesn't return to anything and it's going to take an argument and
- 24:08
this argument is going to be defined using the with keyword followed by the argument name and in this case we're using the IOU CID so the person initiating or the person or the recipient when I accept this swap proposal I need to provide the ious to the initiator so this iOS ucid is going to be of type contract ID IOU and we mentioned earlier it is the controller recipient it is the recipient
- 24:37
who can control this choice now we get to the interesting part the do block and in this do block we are going to implement the workflow and this workflow that we illustrate it will already have sub-transaction privacy baked into it as a developer I don't need to explicitly code that out now let's see what we do here we can say IOU fetch iouscid this line right here represents the recipient getting their IOU
- 25:10
contracts right I need to get my IOU contracts and then what I can do is I need to transfer my ious to the initiator so we will use an IOU request CID to say as the variable returned from exercising our IOU transfer request so let's just do exercise iucid and the choice name is going to be IOU transfer request so we are exercising the IOU transfer request with new owner set as the initiator right so the initiator is the one who gets the the ious
- 25:59
I believe that this red squiggly line here is simply because we haven't finished typing it so the last statement do look must be an expression okay we can at the end of it actually we can just do return because it returns nothing so we have our exercise IOU CID that's correct now let's have our share request CID and this is a result of exercising the share CID and the choice that we're exercising is called share transfer
- 26:43
with the new owner as the recipient right so the recipient becomes the owner of these shares let me see why there's a red line here so the red line is simply because I had an indentation error it's important to note that in damil everything is white space sensitive so let me go ahead and indent that and now that error is gone now that we have completed the transfer steps each participant or each party needs to accept their shares and their
- 27:24
ilus so we can just say exercise and again it's the contract that we need to do so IOU request ID oops allow you request ID let's go ahead and copy and paste that and what is that choice that we're going to be exercising and in this case it's going to be the IOU accept and then we also have our share acceptance that we need to do so we can say exercise share request CID with the choice share underscore
- 28:11
accept now we've created our trade template now let's review this portion again IOU fetch iouscid we are fetching that contract and we have it we save it here and who is doing the fetching it is the recipient who is doing the fetching right it's the recipient that accepts and passes in their IOU CID which is the contract ID and then as the recipient I need to transfer my IOU to the new owner alright so this is in this case it would
- 28:50
be Bob transferring no it would be Alice transferring the ious to Bob right in line 23 it's going to be Bob transferring the shares to Atlas so the new owner is going to be Alice I'm exercising the choice share transfer request now in this case Bob accepts the IOU so it's Bob accepting the IOU now the ownership of the IOU is now Bob on the flip side Alice here accepts the shares and once all these lines are done
- 29:26
the atomic swap has been completed all the data the sub transaction privacy is going to be reflected in the result here it will be shown in the test scripts below see that there is a yellow squiggly line here and that's because we haven't used the import for demoscript so next we're going to create that scenario with demo scripts where Alice and Bob are going to initiate in a trade so let's go ahead and do that first what we want to do is
- 29:55
just Define our setup it's going to be of type script that doesn't return anything and the setup has to do something and what it's going to do is everything we're going to Define underneath the do block so the first thing is we're going to need to allocate parties the parties that we had mentioned earlier are the bank the share industry Alice and Bob so let's go ahead and do that we're going to allocate
- 30:19
party with hint bank and we're just going to provide a party ID hint they like to be the same and there's a red squiggly line here because the script needs to return nothing explicitly so at the bottom we can just say pure and now there's a typo here allocate party so we've allocated the bank now we're going to have to do the same thing with the share registry so let's go ahead and do that
- 30:49
I will just change the names here to share industry to symbolize that and we'll have to do the same with Alice and Bob so let's go ahead and change it we'll do Alice and then we'll also do fob and let's go okay so now that we have the now we have our parties allocated let's do the first thing which is to initialize the io the IOU setup so in the very first instance it's the bank that needs to issue that IOU to itself first so we can do that by saying
- 31:28
okay submit bank that's the person that's the party that's doing the submitting of the creation of the IOU contract so you can say create using the create command we're creating a contract with some fields and what are those fields it's the issuer as the party Bank the owner as also the party bank and we also have an amount which we can Define as a thousand and finally we have public which is going to be Alice and Bob these are the parties involved in the transaction but
- 32:01
we would not do this in a production grade application we would usually have an extra step that explicitly adds either blob or Alice as an observer to the contract but for Simplicity Stakes sake of this example we will initialize the IOU like this so now that we have the IOU City you can actually explore the active contracts looking at the script results so if you just click here script results on the right hand side you can see that a IOU contract has been created this is
- 32:33
an active contract if we click on show archive there won't be anything because nothing has been archived yet but as I said nothing has been archived yet as we interact with the choices and as we exercise the choices various contracts will be archived and they will show up if we happy show archive to checked so once the bank has issued the IOU as minimize that Bank needs to transfer to the IOU to
- 33:03
let's say Alice first because our setup our scenario is Alice wants to buy shares from Bob so Alice is the one who starts out with ious so let's go ahead and write a transfer first right so transfer CID it's the bank that does this right so submit Bank do and the bank is exercising a choice on what on this ious CID and the name of the choice that it's going to exercise is called IOU transfer request and we're passing in a field right with
- 33:42
the new owner as Alice so look at that on the right hand side this IOU has been archived now it's archived because a choice was exercised on it that choice that we exercised was the IOU transfer Quest and therefore an IU transfer request contract has been created and you can see the columns are the various fields and it's visible to these parties right here so now that we have the transfer request
- 34:13
sent out Alice needs to accept this transfer right so what we can do is again iucib let Alice exercising the command and it's going to exercise the transfer CID it's going to exercise that contract and the choice name that it exercises it's called IOU accept now this this new IOU has been created right and the owner is now Alice the bank has transferred the IOU to Alice and you can see that the IOU transfer request on the
- 34:53
right hand side has been archived so now we've created the scenario where Alice starts out with the ious let's do something similar with um Bob and the shares so we can say share CID and it's going to be the share industry now that does the submissions so it's going to create a share with the issuer as itself and the owner as itself now the ticker we can specify we can just say is going to be share that's all and the
- 35:32
public parties which are the people involved in this transaction will be Alice involved and as mentioned we will not be doing this in a production grade scenario there will be extra steps where you need to explicitly add these parties as Observers now once we've done this you can see a share that's been created on the right hand side of our script exam layer you can see it's active the issuer and the owner now the share registry needs to send
- 36:04
this or transfer this to Bob so we're going to say transfer CID it's going to be the shared strength that exercises on top of the share CID contract it exercises the choice name so share transfer request with a new owner who is going through the new owner while we're transferring it to Bob so the new owner is going to be Bob once we've written that you can see that this share contract has been archived and we had
- 36:41
created now a share request with the new owner Bob so now it's the new owner Bob that needs to accept this shared transfer request so let's go ahead and write the next slide kind of share CID and it's going to be Bob that does the exercising so we say exercise command it's a typo it would be easier if I just copy and paste here and we are exercising on top of the transfer CID and the specific Choice
- 37:16
name is share except we are going to be accepting this so we have initialized the main setup Alice has accepted the IOU and Bob has accepted the shares so Alice has the ious and Bob has the shares now we need to initialize the Swap all right Bob needs to initialize the swap because he has a shared so let's just call it swap proposal CID and it's going to be Bob that's going to create this this swap proposal
- 37:51
with an initializer the initializer is Bob himself right so before the initiator sorry not the initializer the recipient is going to be Alice and then we have also the share CID which is what Bob has so we say share CID is equal to the share CID and it's referencing the share CID created above and we also have the amount so Bob is willing to trade these shares for a thousand all right so now a swap proposal has
- 38:28
been created and we can see the swap proposal contract has been created right here it's visible between Alice and Bob all right now what happens now Alice needs to accept this swap proposal and we can do that again using our scripts let me start a new line here we can just say it's because this script doesn't return anything we can just say submit Alice do exercising on the swap proposal CID except we are accepting it and we are accepting it with the ious
- 39:08
that Alice has and there is an issue with the casing here so let me just copy and paste that over here so we have accepting it with the ious that that are available here so once we've done this let's see is there anything else that we need to add here script educate execution failed let's see what happens here it says attempt to exercise a consumed contract I can assume that we are exercising it
- 39:39
right here I use CID and it looks like there's another typo here so let's just remove that and there we go everything works out so Alice accepts the swap Proposal with her ioucids and you can see that in the beginning where we started out with actually the owner of the ious being Alice it is now swapped to Bob and the share in the beginning the IOU belong the share belonged to Bob and now it belongs to Atlas and we looked at look at all the archived
- 40:19
contracts we can actually see this so in the beginning when the bank issues the IOU the owner is the bank right then the bank sent it to Alice and at the end of the scenario Bob owns the IOU now let's look at the shares right the share registry issues it to itself it first sends it to Bob and then Bobs transfers it to Alice so at the end of the day the ownership of the IOU and the ownership of the shares
- 40:52
have been swapped and the talk about sub-transaction privacy we are going full circle to the demo Navigator so let's see how we can do that we can open up the terminal here and we can just run demo start let's go ahead and do that and after you we run down will start it's going to open up the Navigator let's hit we'll start and what happens is that we are compiling these demo files into a Dar
- 41:25
file let's give it a few more moments okay now the Navigator is starting and we can navigate in our browser to set Port 7550 and we will be able to see the Navigator and I have it open here now as you can see we have the drop down just like I showed you in the beginning and we can log in with the bank and from the bank's perspective we're going to include the archive contracts
- 42:14
and you can see in the archive contracts everything to do with only the ious are seen here there is no share transfer visible to the bank and if I log back out and log in with the share registry and we look into the archive contracts we can only see the shares and the share transfers and nothing to do with the IOU and the IU transfers so with our demo code and our demo smart contracts
- 42:46
we were able to achieve Atomic swap as well as sub-transaction privacy