/
mongodb.go
140 lines (130 loc) · 3.73 KB
/
mongodb.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
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
package mongodb_storage
import (
"context"
"fmt"
"github.com/thavlik/bvs/components/commissioner/pkg/storage"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
type mongoStorage struct {
client *mongo.Client
db *mongo.Database
elections *mongo.Collection
minters *mongo.Collection
}
type mongoElection struct {
ID string `bson:"_id"`
SigningKey string `bson:"signingKey"`
VerificationKey string `bson:"verificationKey"`
PolicyID string `bson:"policyId"`
MintingScript string `bson:"mintingScript"`
InvalidHereafter int `bson:"invalidHereafter"`
Protocol string `bson:"protocol"`
}
type mongoMinter struct {
ID string `bson:"_id"`
SigningKey string `bson:"signingKey"`
VerificationKey string `bson:"verificationKey"`
Address string `bson:"address"`
}
func NewMongoDBStorage(
username,
password,
host string,
port int,
database,
cacertPath string,
) (storage.Storage, error) {
if port != 27017 {
return nil, fmt.Errorf("by virtue of a limitation in mongodb, only port 27017 is supported")
}
uri := fmt.Sprintf(
"mongodb+srv://%s:%s@%s/%s?tls=true&tlsCAFile=%s",
username,
password,
host,
database,
cacertPath,
)
client, err := mongo.Connect(
context.TODO(),
options.Client().ApplyURI(uri))
if err != nil {
return nil, fmt.Errorf("connect: %v", err)
}
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
return nil, fmt.Errorf("ping: %v", err)
}
db := client.Database(database)
return &mongoStorage{
client,
db,
db.Collection("elections"),
db.Collection("minters"),
}, nil
}
func (s *mongoStorage) StoreElection(v *storage.Election) error {
if _, err := s.elections.InsertOne(context.TODO(), &mongoElection{
ID: v.ID,
SigningKey: v.SigningKey,
VerificationKey: v.VerificationKey,
PolicyID: v.PolicyID,
MintingScript: v.MintingScript,
InvalidHereafter: v.InvalidHereafter,
Protocol: v.Protocol,
}); err != nil {
return fmt.Errorf("mongo insert: %v", err)
}
return nil
}
func (s *mongoStorage) RetrieveElection(id string) (*storage.Election, error) {
result := s.elections.FindOne(context.TODO(), map[string]interface{}{
"_id": id,
})
if err := result.Err(); err != nil {
return nil, fmt.Errorf("mongo find: %v", err)
}
v := &mongoElection{}
if err := result.Decode(v); err != nil {
return nil, fmt.Errorf("decode: %v", err)
}
return &storage.Election{
ID: v.ID,
SigningKey: v.SigningKey,
VerificationKey: v.VerificationKey,
PolicyID: v.PolicyID,
MintingScript: v.MintingScript,
InvalidHereafter: v.InvalidHereafter,
Protocol: v.Protocol,
}, nil
}
func (s *mongoStorage) StoreMinter(v *storage.Minter) error {
if _, err := s.minters.InsertOne(context.TODO(), &mongoMinter{
ID: v.ID,
SigningKey: v.SigningKey,
VerificationKey: v.VerificationKey,
Address: v.Address,
}); err != nil {
return fmt.Errorf("mongo insert: %v", err)
}
return nil
}
func (s *mongoStorage) RetrieveMinter(id string) (*storage.Minter, error) {
result := s.minters.FindOne(context.TODO(), map[string]interface{}{
"_id": id,
})
if err := result.Err(); err != nil {
return nil, fmt.Errorf("mongo find: %v", err)
}
v := &mongoMinter{}
if err := result.Decode(v); err != nil {
return nil, fmt.Errorf("decode: %v", err)
}
return &storage.Minter{
ID: v.ID,
SigningKey: v.SigningKey,
VerificationKey: v.VerificationKey,
Address: v.Address,
}, nil
}