-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
142 lines (120 loc) · 3.76 KB
/
Copy pathmain.go
File metadata and controls
142 lines (120 loc) · 3.76 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
_ "embed"
"log"
"strings"
_ "github.com/0xPolygonID/refresh-service/logger"
"github.com/0xPolygonID/refresh-service/packagemanager"
"github.com/0xPolygonID/refresh-service/providers/flexiblehttp"
"github.com/0xPolygonID/refresh-service/server"
"github.com/0xPolygonID/refresh-service/service"
"github.com/iden3/go-schema-processor/v2/loaders"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/piprate/json-gold/ld"
"github.com/pkg/errors"
)
var (
w3cSchemaURL = "https://www.w3.org/2018/credentials/v1"
//go:embed w3cSchema.json
w3cSchemaBody []byte
)
type KVstring map[string]string
func (c *KVstring) Decode(value string) error {
const delimiter = ";"
value = strings.Trim(value, "\"")
if value == "" {
*c = make(map[string]string)
return nil
}
contracts := make(map[string]string)
pairs := strings.Split(value, delimiter)
for _, pair := range pairs {
kvpair := strings.Split(pair, "=")
if len(kvpair) != 2 {
return errors.Errorf("invalid map item: %q", pair)
}
contracts[kvpair[0]] = kvpair[1]
}
*c = KVstring(contracts)
return nil
}
type Config struct {
SupportedIssuers KVstring `envconfig:"SUPPORTED_ISSUERS" required:"true"`
IPFSGWURL string `envconfig:"IPFS_GATEWAY_URL" default:"https://ipfs.io"`
ServerHost string `envconfig:"SERVER_HOST" default:":8002"`
HTTPConfigPath string `envconfig:"HTTP_CONFIG_PATH" default:"config.yaml"`
SupportedRPC KVstring `envconfig:"SUPPORTED_RPC" required:"true"`
SupportedStateContracts KVstring `envconfig:"SUPPORTED_STATE_CONTRACTS" required:"true"`
CircuitsFolderPath string `envconfig:"CIRCUITS_FOLDER_PATH" default:"keys"`
SupportedIssuersBasicAuth KVstring `envconfig:"ISSUERS_BASIC_AUTH"`
SupportedCustomDIDMethods string `envconfig:"SUPPORTED_CUSTOM_DID_METHODS"`
}
func (c *Config) getServerHost() string {
return strings.TrimSuffix(c.ServerHost, "/")
}
func (c *Config) getSupportedIssuers() map[string]string {
var supportedIssuers = make(map[string]string, len(c.SupportedIssuers))
for k, v := range c.SupportedIssuers {
supportedIssuers[k] = strings.TrimSuffix(v, "/")
}
return supportedIssuers
}
func main() {
var cfg Config
if err := godotenv.Load(); err != nil {
log.Printf("Error loading .env file: %v", err)
}
if err := envconfig.Process("", &cfg); err != nil {
log.Fatalf("failed init config: %v", err)
}
packageManager, err := packagemanager.NewPackageManager(
cfg.SupportedRPC,
cfg.SupportedStateContracts,
packagemanager.WithVerificationKeyPath(cfg.CircuitsFolderPath),
packagemanager.WithCustomDIDMethods(cfg.SupportedCustomDIDMethods),
)
if err != nil {
log.Fatalf("failed init package manager: %v", err)
}
issuerService := service.NewIssuerService(
cfg.getSupportedIssuers(),
cfg.SupportedIssuersBasicAuth,
nil,
)
documentLoader, err := initDocumentLoaderWithCache(cfg.IPFSGWURL)
if err != nil {
log.Fatalf("failed init document loader: %v", err)
}
flexhttp, err := flexiblehttp.NewFactoryFlexibleHTTP(
cfg.HTTPConfigPath,
nil,
)
if err != nil {
log.Fatalf("failed init flexiblehttp: %v", err)
}
refreshService := service.NewRefreshService(
issuerService,
documentLoader,
flexhttp,
)
agentService := service.NewAgentService(
refreshService,
packageManager,
)
h := server.NewHandlers(
agentService,
)
log.Fatal(h.Run(cfg.getServerHost()))
}
func initDocumentLoaderWithCache(ipfsGW string) (ld.DocumentLoader, error) {
opts := loaders.WithEmbeddedDocumentBytes(
w3cSchemaURL, w3cSchemaBody,
)
memoryCacheEngine, err := loaders.NewMemoryCacheEngine(opts)
if err != nil {
return nil, err
}
l := loaders.NewDocumentLoader(nil, ipfsGW, loaders.WithCacheEngine(memoryCacheEngine))
return l, nil
}