-
Notifications
You must be signed in to change notification settings - Fork 20
/
ReadmeExampleTest.kt
103 lines (80 loc) · 3.49 KB
/
ReadmeExampleTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.luxoft.blockchainlab.corda.hyperledger.indy
import com.luxoft.blockchainlab.corda.hyperledger.indy.flow.*
import net.corda.core.identity.CordaX500Name
import net.corda.node.internal.StartedNode
import net.corda.testing.core.singleIdentity
import net.corda.testing.node.internal.InternalMockNetwork
import net.corda.testing.node.internal.InternalMockNetwork.MockNode
import org.junit.Before
import org.junit.Test
import java.time.LocalDateTime
import java.util.*
class ReadmeExampleTest : CordaTestBase() {
private lateinit var trustee: StartedNode<MockNode>
private lateinit var issuer: StartedNode<MockNode>
private lateinit var alice: StartedNode<MockNode>
private lateinit var bob: StartedNode<MockNode>
@Before
fun setup() {
trustee = createPartyNode(CordaX500Name("Trustee", "London", "GB"))
issuer = createPartyNode(CordaX500Name("Issuer", "London", "GB"))
alice = createPartyNode(CordaX500Name("Alice", "London", "GB"))
bob = createPartyNode(CordaX500Name("Bob", "London", "GB"))
setPermissions(issuer, trustee)
}
@Test
fun `grocery store example`() {
val ministry: StartedNode<InternalMockNetwork.MockNode> = issuer
val alice: StartedNode<*> = alice
val store: StartedNode<*> = bob
// Each Corda node has a X500 name:
val ministryX500 = ministry.info.singleIdentity().name
val aliceX500 = alice.info.singleIdentity().name
// And each Indy node has a DID, a.k.a Decentralized ID:
val ministryDID = store.services.startFlow(
GetDidFlow.Initiator(ministryX500)
).resultFuture.get()
// To allow customers and shops to communicate, Ministry issues a shopping scheme:
val schemaId = ministry.services.startFlow(
CreateSchemaFlow.Authority(
"shopping scheme",
"1.0",
listOf("NAME", "BORN")
)
).resultFuture.get()
// Ministry creates a credential definition for the shopping scheme:
val credentialDefinitionId = ministry.services.startFlow(
CreateCredentialDefinitionFlow.Authority(schemaId)
).resultFuture.get()
// Ministry verifies Alice's legal status and issues her a shopping credential:
val credentialProposal = """
{
"NAME":{"raw":"Alice", "encoded":"119191919"},
"BORN":{"raw":"2000", "encoded":"2000"}
}
"""
ministry.services.startFlow(
IssueCredentialFlow.Issuer(
UUID.randomUUID().toString(),
credentialProposal,
credentialDefinitionId,
aliceX500
)
).resultFuture.get()
// When Alice comes to grocery store, the store asks Alice to verify that she is legally allowed to buy drinks:
// Alice.BORN >= currentYear - 18
val eighteenYearsAgo = LocalDateTime.now().minusYears(18).year
val legalAgePredicate =
VerifyCredentialFlow.ProofPredicate(schemaId, credentialDefinitionId, "BORN", eighteenYearsAgo)
val verified = store.services.startFlow(
VerifyCredentialFlow.Verifier(
UUID.randomUUID().toString(),
emptyList(),
listOf(legalAgePredicate),
aliceX500
)
).resultFuture.get()
// If the verification succeeds, the store can be sure that Alice's age is above 18.
println("You can buy drinks: $verified")
}
}