package Ġcom . template . flows Ċ Ċ import Ġco . parallel universe . fib ers . Suspend able Ċ import Ġcom . template . contracts . R upe e Contract Ċ import Ġcom . template . states . R upe e Change Ċ import Ġcom . template . states . R upe e State Ċ import Ġnet . cord a . core . contracts . Command Ċ import Ġnet . cord a . core . contracts . State And Ref Ċ import Ġnet . cord a . core . contracts . require That Ċ import Ġnet . cord a . core . flows .* Ċ import Ġnet . cord a . core . identity . Party Ċ import Ġnet . cord a . core . transactions . Signed Transaction Ċ import Ġnet . cord a . core . transactions . Transaction Builder Ċ import Ġnet . cord a . core . identity . C ord a X 5 0 0 Name Ċ import Ġnet . cord a . core . node . services . query By Ċ import Ġnet . cord a . core . utilities . Progress Tracker ĊĊ Ċ object ĠR upe e Flow { ĊĊĠĠĠ Ġ@ Start able By RPC ĊĠĠĠ Ġ@ Initi ating Flow ĊĠĠĠ Ġclass ĠIssue ( private Ġval Ġamount Ġ: ĠInt , Ġprivate Ġval Ġowner Ġ: ĠParty ): ĠFlow Logic < Unit > (){ ĊĊĠĠĠĠĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġobject ĠTX _ BUILD ING Ġ: ĠProgress Tracker . Step (" Building ĠTransaction ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġobject ĠTX _ VER IFICATION Ġ: ĠProgress Tracker . Step (" Verify ing ĠTransaction ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġobject ĠTX _ SIGN ING Ġ: ĠProgress Tracker . Step (" Signing ĠTransaction ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġobject ĠTX _ FINAL ISING Ġ: ĠProgress Tracker . Step (" Sending Ġto ĠNot ary Ġand ĠFinal ising ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġfun Ġtrack or () Ġ= ĠProgress Tracker ( TX _ BUILD ING , ĠTX _ VER IFICATION , ĠTX _ SIGN ING , ĠTX _ FINAL ISING ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġoverride Ġval Ġprogress Tracker Ġ= Ġtrack or () ĊĊĠĠĠĠĠĠĠ Ġ@ Suspend able ĊĠĠĠĠĠĠĠ Ġoverride Ġfun Ġcall () Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġprogress Tracker . current Step Ġ= ĠTX _ BUILD ING ĊĠĠĠĠĠĠĠĠĠĠ Ġval Ġnot ary Ġ= Ġservice Hub . network Map Cache . not ary Id entities [ 0 ] ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġru pe e Output Ġ= ĠR upe e State ( amount , our Identity , owner ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġcommand Ġ= ĠCommand ( R upe e Contract . Commands . issue (), ĠlistOf ( our Identity . own ing Key , owner . own ing Key )) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġtx Builder Ġ= ĠTransaction Builder ( not ary ). add Output State ( ru pe e Output , ĠR upe e Contract . ID ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. add Command ( command ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġprogress Tracker . current Step Ġ= ĠTX _ VER IFICATION ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtx Builder . verify ( service Hub ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġprogress Tracker . current Step Ġ= ĠTX _ SIGN ING ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġpartially Signed Tx Ġ= Ġservice Hub . sign Initial Transaction ( tx Builder ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġcounter Party Ġ= Ġinitiate Flow ( owner ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġfully Signed Tx Ġ= Ġsub Flow ( Collect Signatures Flow ( part ially Signed Tx , ĠlistOf ( counter Party ))) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġprogress Tracker . current Step Ġ= ĠTX _ FINAL ISING ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsub Flow ( Final ity Flow ( fully Signed Tx )) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Initi ated By ( Issue :: class ) ĊĠĠĠ Ġclass ĠIssue Responder ( val Ġcounter Party Session : ĠFlow Session ) Ġ: ĠFlow Logic < Unit > (){ ĊĊĠĠĠĠĠĠĠ Ġ@ Suspend able ĊĠĠĠĠĠĠĠ Ġoverride Ġfun Ġcall () Ġ{ ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġcentral Bank Ġ= Ġservice Hub . network Map Cache . get Peer By Legal Name ( C ord a X 5 0 0 Name (" Central ĠBank "," P une "," IN ")) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġsign Transaction Flow Ġ= Ġobject Ġ: ĠSign Transaction Flow ( counter Party Session ){ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġfun Ġcheck Transaction ( st x : ĠSigned Transaction ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġrequire That Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" Only ĠCentral ĠBank Ġcan Ġissue Ġthe Ġru pe e " Ġusing Ġ( counter Party Session . counter party Ġ== Ġcentral Bank ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" Bank Ġmust Ġsign Ġtx Ġfirst " Ġusing Ġ( st x . sigs . any Ġ{ Ġsig Ġ-> Ġsig . by Ġ== Ġcounter Party Session . counter party . own ing Key }) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġsub Flow ( sign Transaction Flow ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Start able By RPC ĊĠĠĠ Ġ@ Initi ating Flow ĊĠĠĠ Ġclass ĠTransfer ( private Ġval Ġamount : ĠInt , Ġprivate Ġval Ġnew Owner : ĠParty ) Ġ: ĠFlow Logic < Unit > (){ ĊĊĠĠĠĠĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġobject ĠTX _ BUILD ING Ġ: ĠProgress Tracker . Step (" Building ĠTransaction ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġobject ĠTX _ VER IFICATION Ġ: ĠProgress Tracker . Step (" Verify ing ĠTransaction ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġobject ĠTX _ SIGN ING Ġ: ĠProgress Tracker . Step (" Signing ĠTransaction ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġobject ĠTX _ FINAL ISING Ġ: ĠProgress Tracker . Step (" Sending Ġto ĠNot ary Ġand ĠFinal ising ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġfun Ġtrack or () Ġ= ĠProgress Tracker ( TX _ BUILD ING , ĠTX _ VER IFICATION , ĠTX _ SIGN ING , ĠTX _ FINAL ISING ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġoverride Ġval Ġprogress Tracker Ġ= Ġtrack or () ĊĊĊĠĠĠĠĠĠĠ Ġ@ Suspend able ĊĠĠĠĠĠĠĠ Ġoverride Ġfun Ġcall () Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġnot ary Ġ= Ġservice Hub . network Map Cache . not ary Id entities [ 0 ] ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġtx Builder Ġ= ĠTransaction Builder ( not ary ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġru pe e Inputs Ġ= Ġservice Hub . vault Service . query By < R upe e State >(). states ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġru pe es Input Ġ= Ġcalculate States ( amount , Ġru pe e Inputs ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġru pe es Input . forEach Ġ{ Ġtx Builder . add Input State ( it )} ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġfinal States Ġ= Ġcalculate Change ( ru pe es Input . map Ġ{ Ġit . state . data Ġ}, Ġamount , Ġnew Owner ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġcommand Ġ= ĠCommand ( R upe e Contract . Commands . transfer (), ĠlistOf ( our Identity . own ing Key , final States . other Party R upe e . owner . own ing Key )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtx Builder . add Output State ( final States . other Party R upe e , R upe e Contract . ID Ġ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. add Command ( command ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( final States . has Change ()) Ġtx Builder . add Output State ( final States . my R upe e , ĠR upe e Contract . ID ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġtx Builder . verify ( service Hub ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġpartially Signed Tx Ġ= Ġservice Hub . sign Initial Transaction ( tx Builder ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġfully Signed Tx Ġ= Ġsub Flow ( Collect Signatures Flow ( part ially Signed Tx , ĠlistOf ( init iate Flow ( new Owner )))) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsub Flow ( Final ity Flow ( fully Signed Tx )) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġ@ Initi ated By ( Transfer :: class ) ĊĠĠĠ Ġclass ĠTransfer Responder ( val Ġsession : ĠFlow Session ) Ġ: ĠFlow Logic < Unit > (){ ĊĊĠĠĠĠĠĠĠ Ġ@ Suspend able ĊĠĠĠĠĠĠĠ Ġoverride Ġfun Ġcall () Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġsign Transaction Flow Ġ= Ġobject Ġ: ĠSign Transaction Flow ( session ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġfun Ġcheck Transaction ( st x : ĠSigned Transaction ) Ġ= Ġrequire That Ġ{ ĊĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" Owner Ġmust Ġsign Ġthe Ġtx Ġfirst " Ġusing Ġ( st x . sigs . any Ġ{ Ġsig Ġ-> Ġsig . by Ġ== Ġsession . counter party . own ing Key Ġ}) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġsub Flow ( sign Transaction Flow ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġcalculate States ( amount Ġ: ĠInt , Ġstates : ĠList < State And Ref < R upe e State >>) Ġ: ĠList < State And Ref < R upe e State >> { ĊĊĠĠĠĠĠĠĠ Ġvar Ġtotal Ġ= Ġ 0 ĊĠĠĠĠĠĠĠ Ġvar Ġinputs Ġ= ĠlistOf < State And Ref < R upe e State >>() ĊĠĠĠĠĠĠ Ġfor Ġ( ru pe e Ġin Ġstates ){ ĊĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( total Ġ>= Ġamount ) Ġbreak ĊĠĠĠĠĠĠĠĠĠĠ Ġtotal Ġ+= Ġru pe e . state . data . amount ĊĠĠĠĠĠĠĠĠĠĠ Ġinputs Ġ+= Ġru pe e ĊĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġrequire That Ġ{ Ġ" Party Ġdoes Ġnot Ġhave Ġenough ĠR upe e " Ġusing Ġ( total Ġ>= Ġamount ) Ġ} ĊĠĠĠĠĠĠĠ Ġreturn Ġinputs ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġcalculate Change ( ru pe es : ĠList < R upe e State >, Ġunit Ġ: ĠInt , Ġnew Owner : ĠParty ) Ġ: ĠR upe e Change Ġ{ ĊĊĠĠĠĠĠĠĠ Ġval Ġtotal Ġ= Ġru pe es . map Ġ{ Ġit . amount Ġ}. sum () ĊĠĠĠĠĠĠĠ Ġval Ġtotal R upe e Ġ= Ġru pe es . first (). copy ( amount Ġ= Ġtotal ) ĊĠĠĠĠĠĠĠ Ġreturn ĠR upe e Change ( total R upe e . copy ( amount Ġ= Ġunit , Ġowner Ġ= Ġnew Owner ), total R upe e . copy ( amount Ġ= Ġtotal - unit )) ĊĠĠĠ Ġ} Ċ Ċ }