forked from ssvlabs/ssv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration_2_encrypt_shares.go
61 lines (55 loc) · 1.68 KB
/
migration_2_encrypt_shares.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
package migrations
import (
"context"
"crypto/sha256"
"crypto/x509"
"fmt"
"github.com/bloxapp/ssv/utils/rsaencryption"
"github.com/bloxapp/ssv/storage/basedb"
"go.uber.org/zap"
)
var migration_2_encrypt_shares = Migration{
Name: "migration_2_encrypt_shares",
Run: func(ctx context.Context, logger *zap.Logger, opt Options, key []byte, completed CompletedFunc) error {
return opt.Db.Update(func(txn basedb.Txn) error {
err := txn.Set(migrationsPrefix, key, migrationCompleted)
if err != nil {
return err
}
obj, found, err := txn.Get([]byte("operator/"), []byte("private-key"))
if err != nil {
return fmt.Errorf("failed to get private key: %w", err)
}
if !found {
return completed(txn)
}
operatorKey, err := rsaencryption.PemToPrivateKey(obj.Value)
if err != nil {
return fmt.Errorf("failed to get private key: %w", err)
}
signerStorage := opt.signerStorage(logger)
accounts, err := signerStorage.ListAccountsTxn(txn)
if err != nil {
return fmt.Errorf("failed to list accounts: %w", err)
}
keyBytes := x509.MarshalPKCS1PrivateKey(operatorKey)
hash := sha256.Sum256(keyBytes)
keyString := fmt.Sprintf("%x", hash)
err = signerStorage.SetEncryptionKey(keyString)
if err != nil {
return fmt.Errorf("failed to set encryption key: %w", err)
}
for _, account := range accounts {
err := signerStorage.SaveAccountTxn(txn, account)
if err != nil {
return fmt.Errorf("failed to save account %s: %w", account, err)
}
}
err = txn.Delete([]byte("operator/"), []byte("private-key"))
if err != nil {
return fmt.Errorf("failed to delete private key: %w", err)
}
return completed(txn)
})
},
}