-
Notifications
You must be signed in to change notification settings - Fork 384
/
vc_issuer.go
112 lines (93 loc) · 2.58 KB
/
vc_issuer.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
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"context"
"crypto/ed25519"
"encoding/base64"
"flag"
"fmt"
"net"
"net/http"
"github.com/oklog/run"
"github.com/peterbourgon/ff/v3/ffcli"
"berty.tech/berty/v2/go/pkg/bertyvcissuer"
"berty.tech/berty/v2/go/pkg/verifiablecredstypes"
)
func vcIssuerCommand() *ffcli.Command {
listenerFlag := ""
serverRootURL := ""
vcIssuerPrivKey := ""
fsBuilder := func() (*flag.FlagSet, error) {
fs := flag.NewFlagSet("berty vc-issuer", flag.ExitOnError)
fs.StringVar(&listenerFlag, "http.listener", listenerFlag, "http listener")
fs.StringVar(&serverRootURL, "http.server-root", serverRootURL, "http server root")
fs.StringVar(&vcIssuerPrivKey, "vc-sk", vcIssuerPrivKey, "Verifiable Credentials Issuer private key (base64 encoded)")
return fs, nil
}
return &ffcli.Command{
Name: "vc-issuer",
ShortUsage: "berty [global flags] vc-issuer [flags]",
ShortHelp: "start a verified credentials issuer service",
Options: ffSubcommandOptions(),
FlagSetBuilder: fsBuilder,
UsageFunc: usageFunc,
Exec: func(ctx context.Context, args []string) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if listenerFlag == "" {
return fmt.Errorf("missing -http.listener flag")
}
if serverRootURL == "" {
return fmt.Errorf("missing -http.server-root flag")
}
if vcIssuerPrivKey == "" {
return fmt.Errorf("missing -vc-sk flag")
}
g := run.Group{}
g.Add(func() error {
<-ctx.Done()
return ctx.Err()
}, func(error) {
cancel()
})
logger, err := manager.GetLogger()
if err != nil {
return err
}
skBytes, err := base64.RawStdEncoding.DecodeString(vcIssuerPrivKey)
if err != nil {
return err
}
if len(skBytes) != ed25519.SeedSize {
return fmt.Errorf("invalid sk size")
}
issuerPrivateKey := &[32]byte{}
copy(issuerPrivateKey[:], skBytes)
l, err := net.Listen("tcp", listenerFlag)
if err != nil {
return err
}
vcConfig := &bertyvcissuer.Config{
ServerRootURL: serverRootURL,
IssuerSignKey: issuerPrivateKey,
Flow: &bertyvcissuer.FlowConfig{
Type: verifiablecredstypes.FlowType_FlowTypeCode, // TODO
CodeGenerator: bertyvcissuer.CodeGeneratorZero, // TODO
CodeSenderClient: &bertyvcissuer.PhoneCodeSenderMockService{
Logger: logger,
}, // TODO
},
Logger: logger,
}
server, err := bertyvcissuer.New(vcConfig)
if err != nil {
return err
}
g.Add(func() error {
return http.Serve(l, server)
}, func(err error) {
l.Close()
})
return g.Run()
},
}
}