@startuml
'https://plantuml.com/sequence-diagram

skinparam SequenceGroupBodyBackgroundColor #FFFFFF90

header Use case sequence diagram - %page% of %lastpage%
title Alice goes to Portugal

box "Glass Frontend" #FloralWhite
    actor Alice as a #DeepSkyBlue
    entity "Alice's Glass Wallet" as w #SteelBlue
end box
box "Backend" #Gainsboro
    entity "Data Schema\nTransformer" as dst #SeaGreen
    entity "Data Sharing\nFramework" as dsf #Teal
    database "Distributed Ledger\nTechnology" as dlt #Maroon
end box
box "Public Entities" #Cornsilk
    participant "Ministry of\nDigital Governance" as modj #PeachPuff
    participant "Ministry of\nJustice" as moj #RosyBrown
end box
box "Third Parties" #Lavender
    participant "Bank" as b #PapayaWhip
end box

newpage Initialization
    note over modj:  Starts with a DID

    group Ministry of Justice
        autonumber 1.1.1
        moj ->> modj: requests MoJ Wallet (including signed DID)
       activate modj #PeachPuff
            modj ->> modj: Creates Wallet (and DID))
            note right modj: The DID is signed by MoDJ's DID
            modj -->> dlt: Anchors Wallet and DID to DLT
           activate modj #PeachPuff
                activate dlt #Maroon
                    dlt -->> modj: Anchoring successful
                deactivate dlt
            deactivate modj
            modj -->> moj: returns wallet Key
            note over moj: MoJ now has a wallet, containing a DID signed by MoDJ
        deactivate modj
    end
    group Bank
        autonumber inc B
        b ->> modj: requests Bank Wallet (including signed DID)
       activate modj #PeachPuff
            modj ->> modj: Creates Wallet (and DID))
            note right modj: The DID is signed by MoDJ's DID
            modj -->> dlt: Anchors Wallet and DID to DLT
           activate modj #PeachPuff
                activate dlt #Maroon
                    dlt -->> modj: Anchoring successful
                deactivate dlt
            deactivate modj
            modj -->> b: returns wallet Key
            note over b: Bank now has a wallet, containing a DID signed by MoDJ
        deactivate modj
    end
    group Wallet [specifically Alice's]
        autonumber inc B
        a ->> w: Registers for a new Glass Wallet
       activate w #SteelBlue
            w ->> w: Creates Wallet (and DID))
            note right w: This DID is specific to the wallet, is not signed, and has no use so far
            w -->> dlt: Anchors Wallet and DID to DLT
            activate dlt #Maroon
               activate w #SteelBlue
                    dlt -->> w: Anchoring successful
                deactivate w
            deactivate dlt
            w -->> a: returns wallet Key
        deactivate w
        activate a #DeepSkyBlue
            a -->> w: Alice can now login to her Glass Wallet
        deactivate a
    end
    group eGovernance Install
        autonumber inc B
        a ->> modj: Alice identifies herself and requests her eGovernance Application to be issued
       activate modj #PeachPuff
            modj ->> modj: Validates identification
            loop for each embedded service - in this case only MoJ
                modj ->> moj: Request issuing of Service Wallet
                deactivate modj
               activate moj #RosyBrown
                    moj ->> dsf: requests to transform the user data from own to common format
                   activate dsf #Teal
                        dsf ->> dsf: transforms own data model to common format data model
                        dsf --> moj: returns data in common format
                    deactivate dsf
                    moj ->> moj: Creates service Wallet and DID (data in common format)
                    note right moj: The DID is signed by\nthe Ministry of Justice DID
                    moj ->> dlt: anchors Wallet and DID to DLT
                    activate dlt #Maroon
                       activate moj #RosyBrown
                            dlt -->> moj: Anchoring successful
                        deactivate moj
                    deactivate dlt
                    moj -->> modj: return wallet Key
                deactivate moj
           activate modj #PeachPuff
            end loop
                modj ->> modj: Creates eGovernance Wallet and DID
                note right modj: The DID is signed by\nthe Ministry of Digital Governance DID
                modj ->> dlt: anchors Wallet and DID to DLT
                activate dlt #Maroon
                   activate modj #PeachPuff
                        dlt -->> modj: Anchoring successful
                    deactivate modj
                deactivate dlt
                modj -->> a: Shows Installation code
            deactivate modj
        activate a #DeepSkyBlue
            a ->> w: Alice scans code with Wallet
        deactivate a
       activate w #SteelBlue
            w ->> w: Wallet reads code\n and install eGovernance Dapp
            w -->> a: Alice can now access her eGovernance dApp from her wallet
        deactivate w
    end

newpage Demonstrator 1
    group Alice gets a Social Security Card
        autonumber inc A
        a ->> moj: Alice requests a Social Security Card
       activate moj #RosyBrown
            note over moj: To issue a Social Security Card,\nMoj needs an ID and a Birth Certificate
            moj ->> w: Issues request for Evidences under \"egov.*.id\" and\n \"egov.*.health.birthCertificate\" with conditions
        deactivate moj
       activate w #SteelBlue
            w ->> a: Wallet requests user authorization to share those evidences
           activate w #SteelBlue
                activate a #DeepSkyBlue
                    a -->> w: Alice authorizes sharing those Specific evidences,\nfor the requested conditions
                deactivate a
            deactivate w
            alt eGovernance Application is Locked
                w ->> a: Requests Alice to unlock to eGovernance dApp
                a -->> w: Alice unlocks the eGovernance dApp
            end
            w ->> w: wallet crawls the eGovernance dApp for the evidences:\n- egov.*.id\n- egov.*.health.birthCertificate
           activate w #SteelBlue
            w ->> w: wallet retrieves the keys to the selected evidences
            w ->> dsf: calls Data Sharing Framework to embedded 'conditions'
           activate dsf #Teal
                dsf -->> w: Returns DSF links for evidence access\n (with embedded conditions)
            deactivate dsf
            deactivate w
            w -->> moj: sends signed message with DSF links
        deactivate w
       activate moj #RosyBrown
            moj ->> dsf: MoJ requests link resolution
           activate dsf #Teal
                dsf -->> moj: Resolves links according\n to conditions and returns evidences keys
            deactivate dsf
            moj ->> moj: MoJ resolves Evidence Keys
            moj ->> dst: request converting common format evidences to own (MoJ's) format
           activate dst #SeaGreen
                dst -->> moj: returns models in Moj's own format
            deactivate dst
            note over moj: Ministry of Justice now has the required evidences
            moj -->> moj: Issues Social Security Card
            moj ->> dlt: Anchors Social Security Card
            activate dlt #Maroon
                dlt -->> moj: Anchoring successful
            deactivate dlt #Maroon
            moj -->> w: MoJ send StoreEvidenceRequest to wallet with Evidence Key
        deactivate moj
       activate w #SteelBlue
            w ->> a: Requests user Authorization to Store evidence
            activate a #DeepSkyBlue
                a -->> w: Alice authorizes
            deactivate a
            w ->> w: Stores evidence key under 'egov.pt.social-security' for instance
            w --> a: Alice now has a Social Security Card
        deactivate w
    end

newpage Demonstrator 2
    group Alice gets Portuguese Residency
        autonumber inc B
        a ->> moj: Alice requests Portuguese Residency
       activate moj #RosyBrown
            note over moj: To issue a Portuguese Residency Card,\nMoj needs an ID and a Work Contract
            moj ->> w: Issues request for Evidences under 'egov.*.id' and 'egov.*.tax.work-contract?valid=true' with conditions
        deactivate moj
       activate w #SteelBlue
            w ->> a: Wallet requests user authorization to share those evidences
           activate w #SteelBlue
                activate a #DeepSkyBlue
                    a -->> w: Alice authorizes sharing those Specific evidences,\nfor the requested conditions
                deactivate a
            deactivate w
            alt eGovernance Application is Locked
                w ->> a: Requests Alice to unlock to eGovernance dApp
                a -->> w: Alice unlocks the eGovernance dApp
            end
            w ->> w: wallet crawls the eGovernance dApp for the evidences:\n- egov.*.id\n- egov.*.tax.work-contract?valid=true
           activate w #SteelBlue
            w ->> w: wallet retrieves the keys to the selected evidences
            w ->> dsf: calls Data Sharing Framework to embedded 'conditions'
           activate dsf #Teal
                dsf -->> w: Returns DSF links for evidence \n (with embedded conditions)
            deactivate dsf
            deactivate w
            w -->> moj: sends signed message with DSF links
        deactivate w
       activate moj #RosyBrown
            moj ->> dsf: MoJ requests link resolution
           activate dsf #Teal
                dsf -->> moj: Resolves links according\n to conditions and returns evidences keys
            deactivate dsf
            moj ->> moj: MoJ resolves Evidence Keys
            moj ->> dst: request converting common format evidences to own (MoJ's) format
           activate dst #SeaGreen
                dst -->> moj: returns models in Moj's own format
            deactivate dst
            note over moj: Ministry of Justice now has the required evidences
            moj -->> moj: Issues Portuguese Residency Card
            moj ->> dlt: Anchors Portuguese Residency Card
            activate dlt #Maroon
                dlt -->> moj: Anchoring successful
            deactivate dlt #Maroon
            moj -->> w: MoJ send StoreEvidenceRequest to wallet with Evidence Key
        deactivate moj
       activate w #SteelBlue
            w ->> a: Requests user Authorization to Store evidence
            activate a #DeepSkyBlue
                a -->> w: Alice authorizes
            deactivate a
            w ->> w: Stores evidence key under 'egov.pt.residency' for instance
            w --> a: Alice now has a Portuguese Residency Card
        deactivate w
    end

newpage Demonstrator 3
    group Alice Opens a Bank Account
        autonumber inc B
        a ->> b **: Alice asks to open Bank Account
       activate b #PapayaWhip
            note over b: To open a Bank Account,\nThe bank needs an ID and valid tax record
            b ->> w: Issues request for Evidences under \"egov.*.id\"\n and \"egov.*.tax.record?valid=true\" with conditions
        deactivate b
       activate w #SteelBlue
            w ->> a: Wallet requests user authorization to share those evidences
           activate w #SteelBlue
                activate a #DeepSkyBlue
                    a -->> w: Alice authorizes sharing those Specific evidences,\nfor the requested conditions
                deactivate a
            deactivate w
            alt eGovernance Application is Locked
                w ->> a: Requests Alice to unlock to eGovernance dApp
                a -->> w: Alice unlocks the eGovernance dApp
            end
            w ->> w: wallet crawls the eGovernance dApp for the evidences:\n- egov.*.id\n- egov.*.tax.record?valid=true
           activate w #SteelBlue
            w ->> w: wallet retrieves the keys to the selected evidences
            w ->> dsf: calls Data Sharing Framework to embedded 'conditions'
           activate dsf #Teal
                dsf -->> w: Returns DSF links for evidence access\n (with embedded conditions)
            deactivate dsf
            deactivate w
            w -->> b: sends signed message with DSF links
        deactivate w
       activate b #PapayaWhip
            b ->> dsf: Bank requests link resolution
           activate dsf #Teal
                dsf -->> b: Resolves links according\n to conditions and returns evidences keys
            deactivate dsf
            b ->> b: Bank resolves Evidence Keys
            b ->> dst: request converting common format evidences to own (Bank's) format
           activate dst #SeaGreen
                dst -->> b: returns models in Bank's own format
            deactivate dst
            note over b: Bank now has the required evidences
            b -->> b: Opens Bank Account
            alt User does not have Bank dApp
                b ->> b: Creates Bank dApp and DID (data in common format)
                note right b: The DID is signed by\nthe bank's DID\nand includes the account details
                b ->> dlt: anchors dApp and DID to DLT
                activate dlt #Maroon
                   activate b #PapayaWhip
                        dlt -->> b: Anchoring successful
                    deactivate b
                deactivate dlt #Maroon
                b -->> w: send dApp install request
               activate w #SteelBlue
                w ->> a: request permission to install dApp
                a -->> w: approves dApp installation
                w ->> w: installs Bank dApp (which contains all info)
                w -->> a: Alice now has the new bank account in her new Back dApp
                deactivate w
            else User already has Bank App
                b -->> b: bank updates dApp data with new account
                    b ---> w: bank's dApp within wallet is automatically updated
                    deactivate b
               activate w #SteelBlue
                    w -->> a: Alice now has a new Bank dApp with her new account information
                deactivate w
            end
    end

footer Use case sequence diagram - %page% of %lastpage%
@enduml