-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
orm.go
80 lines (70 loc) · 1.99 KB
/
orm.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
package keystore
import (
"database/sql"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"github.com/pkg/errors"
"github.com/smartcontractkit/sqlx"
)
func NewORM(db *sqlx.DB, lggr logger.Logger, cfg pg.QConfig) ksORM {
namedLogger := lggr.Named("KeystoreORM")
return ksORM{
q: pg.NewQ(db, namedLogger, cfg),
lggr: namedLogger,
}
}
type ksORM struct {
q pg.Q
lggr logger.Logger
}
func (orm ksORM) isEmpty() (bool, error) {
var count int64
err := orm.q.QueryRow("SELECT count(*) FROM encrypted_key_rings").Scan(&count)
if err != nil {
return false, err
}
return count == 0, nil
}
func (orm ksORM) saveEncryptedKeyRing(kr *encryptedKeyRing, callbacks ...func(pg.Queryer) error) error {
return orm.q.Transaction(func(tx pg.Queryer) error {
_, err := tx.Exec(`
UPDATE encrypted_key_rings
SET encrypted_keys = $1
`, kr.EncryptedKeys)
if err != nil {
return errors.Wrap(err, "while saving keyring")
}
for _, callback := range callbacks {
err = callback(tx)
if err != nil {
return err
}
}
return nil
})
}
func (orm ksORM) getEncryptedKeyRing() (kr encryptedKeyRing, err error) {
err = orm.q.Get(&kr, `SELECT * FROM encrypted_key_rings LIMIT 1`)
if errors.Is(err, sql.ErrNoRows) {
sql := `INSERT INTO encrypted_key_rings (encrypted_keys, updated_at) VALUES (NULL, NOW()) RETURNING *;`
err2 := orm.q.Get(&kr, sql)
if err2 != nil {
return kr, err2
}
} else if err != nil {
return kr, err
}
return kr, nil
}
func (orm ksORM) loadKeyStates() (*keyStates, error) {
ks := newKeyStates()
var ethkeystates []*ethkey.State
if err := orm.q.Select(ðkeystates, `SELECT id, address, evm_chain_id, disabled, created_at, updated_at FROM evm.key_states`); err != nil {
return ks, errors.Wrap(err, "error loading evm.key_states from DB")
}
for _, state := range ethkeystates {
ks.add(state)
}
return ks, nil
}