-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
99 lines (81 loc) · 2.38 KB
/
main.go
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
package main
import (
"encoding/json"
"fmt"
"os"
)
const (
vcSpec = "https://www.w3.org/2018/credentials/v1"
issuerID = "https://oxford.com/issuers/1" // This is a fake URL.
issuerName = "The Marvelous University of Oxford"
)
var vcContext = []string{vcSpec}
func main() {
// Part I: Create the issuer, the subject, and the verifier.
issuer, err := CreateIssuer(issuerID, issuerName)
if err != nil {
panic(err)
}
subject, err := CreateSubject()
if err != nil {
panic(err)
}
verifier := CreateVerifier()
// Part II: The Issuer issues credentials on the Subject.
credentials, err := part2(issuer, subject)
if err != nil {
panic(err)
}
// Part III: The Verifier (any third party) can check the claim of the
// Subject that it holds the credentials
part3(subject, verifier, credentials)
}
func part2(issuer Issuer, subject Subject) (Credential, error) {
// Step 1: Create a Subject and a claim to sign about this subject.
// The claim is created jointly by the Subject and the Issuer. How they come
// to agree on the claim to sign is out of scope here.
claim := Claim{
Age: 24,
UniversityName: "Oxford",
Degree: "Bachelor of Science",
}
nicePrint(claim, "Claim")
// Step 2: The Issuer signs the claim about this subject.
credentials, err := issuer.SignCredential(claim, subject.GetID())
if err != nil {
err = fmt.Errorf("Issuer couldn't sign credentials: %w", err)
return credentials, err
}
nicePrint(credentials, "Credential")
return credentials, err
}
func part3(subject Subject, verifier Verifier, credentials Credential) {
// Step 1: The verifier creates a challenge/nonce to be included in the
// presentation which will be signed bby the subject.
nonce, err := verifier.MakeNonce()
if err != nil {
panic(err)
}
// Step 2: The subject creates the presentation and signs it.
presentation, err := subject.SignPresentation(
credentials,
nonce,
)
if err != nil {
panic(err)
}
nicePrint(presentation, "Presentation")
// Step 3: The verifier checks that the signature of the presentation is
// correct.
err = verifier.VerifiesPresentation(presentation)
if err != nil {
panic(fmt.Errorf("Verificiation failed: %w", err))
}
fmt.Println("\n!!! Verification succeeded !!!")
}
func nicePrint(i interface{}, name string) {
e := json.NewEncoder(os.Stdout)
e.SetIndent("", " ")
fmt.Printf("\n***** %s *****\n\n", name)
e.Encode(i)
}