-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
39 lines (30 loc) · 1.1 KB
/
create.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
package wallets
import (
"context"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/lugondev/wallet-signer-manager/src/stores/database/models"
"github.com/lugondev/wallet-signer-manager/pkg/errors"
authentities "github.com/lugondev/wallet-signer-manager/src/auth/entities"
"github.com/lugondev/wallet-signer-manager/src/stores/entities"
)
func (c Connector) Create(ctx context.Context, id string, attr *entities.Attributes) (*entities.Wallet, error) {
logger := c.logger.With("id", id)
logger.Debug("creating wallet")
err := c.authorizator.CheckPermission(&authentities.Operation{Action: authentities.ActionWrite, Resource: authentities.ResourceWallets})
if err != nil {
return nil, err
}
wallet, err := c.store.Create(ctx, id, attr)
if err != nil && errors.IsAlreadyExistsError(err) {
wallet, err = c.store.Get(ctx, id)
}
if err != nil {
return nil, err
}
acc, err := c.db.Add(ctx, models.NewWalletFromKey(wallet, attr))
if err != nil {
return nil, err
}
logger.With("pubkey", hexutil.Encode(acc.CompressedPublicKey)).Info("wallet created successfully")
return acc, nil
}