-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
95 lines (76 loc) · 2.13 KB
/
store.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
package postgres
import (
"context"
"database/sql"
"github.com/code-payments/code-server/pkg/database/query"
"github.com/code-payments/code-server/pkg/code/data/vault"
"github.com/jmoiron/sqlx"
)
type store struct {
db *sqlx.DB
}
func New(db *sql.DB) vault.Store {
return &store{
db: sqlx.NewDb(db, "pgx"),
}
}
// Count returns the total count of keys.
func (s *store) Count(ctx context.Context) (uint64, error) {
return dbGetCount(ctx, s.db)
}
// CountByState returns the total count of keys by state
func (s *store) CountByState(ctx context.Context, state vault.State) (uint64, error) {
return dbGetCountByState(ctx, s.db, state)
}
// Save creates or updates vault metadata in the store.
func (s *store) Save(ctx context.Context, record *vault.Record) error {
obj, err := toKeyModel(record)
if err != nil {
return err
}
ciphertext, err := vault.Encrypt(record.PrivateKey, record.PublicKey)
if err != nil {
return err
}
obj.PrivateKey = ciphertext
err = obj.dbSave(ctx, s.db)
if err != nil {
return err
}
obj.PrivateKey = record.PrivateKey
res := fromKeyModel(obj)
res.CopyTo(record)
return nil
}
// Get finds the vault record for a given pubkey.
func (s *store) Get(ctx context.Context, pubkey string) (*vault.Record, error) {
obj, err := dbGetKey(ctx, s.db, pubkey)
if err != nil {
return nil, err
}
plaintext, err := vault.Decrypt(obj.PrivateKey, obj.PublicKey)
if err != nil {
return nil, err
}
obj.PrivateKey = plaintext
return fromKeyModel(obj), nil
}
// GetAllByState returns all vault records for a given state.
//
// Returns ErrKeyNotFound if no records are found.
func (s *store) GetAllByState(ctx context.Context, state vault.State, cursor query.Cursor, limit uint64, direction query.Ordering) ([]*vault.Record, error) {
models, err := dbGetAllByState(ctx, s.db, state, cursor, limit, direction)
if err != nil {
return nil, err
}
keys := make([]*vault.Record, len(models))
for i, model := range models {
plaintext, err := vault.Decrypt(model.PrivateKey, model.PublicKey)
if err != nil {
return nil, err
}
model.PrivateKey = plaintext
keys[i] = fromKeyModel(model)
}
return keys, nil
}