-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore.go
51 lines (40 loc) · 1.17 KB
/
restore.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
package wallets
import (
"context"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/lugondev/wallet-signer-manager/src/auth/entities"
"github.com/lugondev/wallet-signer-manager/pkg/errors"
"github.com/lugondev/wallet-signer-manager/src/stores/database"
)
func (c Connector) Restore(ctx context.Context, pubkey string) error {
logger := c.logger.With("pubkey", pubkey)
logger.Debug("restoring wallet")
err := c.authorizator.CheckPermission(&entities.Operation{Action: entities.ActionDelete, Resource: entities.ResourceWallets})
if err != nil {
return err
}
_, err = c.Get(ctx, pubkey)
if err == nil {
return nil
}
acc, err := c.db.GetDeleted(ctx, pubkey)
if err != nil {
return err
}
err = c.db.RunInTransaction(ctx, func(dbtx database.Wallets) error {
err = dbtx.Restore(ctx, pubkey)
if err != nil {
return err
}
err = c.store.Restore(ctx, hexutil.Encode(acc.CompressedPublicKey))
if err != nil && !errors.IsNotSupportedError(err) { // If the underlying store does not support restoring, we only restore in DB
return err
}
return nil
})
if err != nil {
return err
}
logger.Info("wallet restored successfully")
return nil
}