Skip to content

Commit

Permalink
feat(SPV-1420): make totp work (#123)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
dzolt-4chain and github-actions[bot] authored Feb 7, 2025
1 parent 6c2bc35 commit cded5a2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 27 deletions.
4 changes: 2 additions & 2 deletions domain/contacts/contacts_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func NewContactsService(adminWalletClient users.AdminWalletClient, walletClientF
}

// UpsertContact creates or updates a contact
func (s *Service) UpsertContact(ctx context.Context, accessKey, paymail, fullName, requesterPaymail string, metadata map[string]any) (*models.Contact, error) {
userWalletClient, err := s.walletClientFactory.CreateWithAccessKey(accessKey)
func (s *Service) UpsertContact(ctx context.Context, xPriv, paymail, fullName, requesterPaymail string, metadata map[string]any) (*models.Contact, error) {
userWalletClient, err := s.walletClientFactory.CreateWithXpriv(xPriv)
if err != nil {
return nil, spverrors.ErrUpsertContact.Wrap(err)
}
Expand Down
5 changes: 4 additions & 1 deletion transports/http/endpoints/api/contacts/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/bitcoin-sv/spv-wallet-web-backend/domain"
"github.com/bitcoin-sv/spv-wallet-web-backend/domain/contacts"
"github.com/bitcoin-sv/spv-wallet-web-backend/domain/users"
"github.com/bitcoin-sv/spv-wallet-web-backend/spverrors"
"github.com/bitcoin-sv/spv-wallet-web-backend/transports/http/auth"
router "github.com/bitcoin-sv/spv-wallet-web-backend/transports/http/endpoints/routes"
Expand All @@ -15,13 +16,15 @@ import (
)

type handler struct {
uService users.UserService
cService contacts.Service
log *zerolog.Logger
}

// NewHandler creates new endpoint handler.
func NewHandler(s *domain.Services, log *zerolog.Logger) router.APIEndpoints {
return &handler{
uService: *s.UsersService,
cService: *s.ContactsService,
log: log,
}
Expand Down Expand Up @@ -81,7 +84,7 @@ func (h *handler) upsertContact(c *gin.Context) {
return
}

_, err := h.cService.UpsertContact(c.Request.Context(), c.GetString(auth.SessionAccessKey), paymail, req.FullName, c.GetString(auth.SessionUserPaymail), req.Metadata)
_, err := h.cService.UpsertContact(c.Request.Context(), c.GetString(auth.SessionXPriv), paymail, req.FullName, c.GetString(auth.SessionUserPaymail), req.Metadata)
if err != nil {
spverrors.ErrorResponse(c, err, h.log)
return
Expand Down
72 changes: 48 additions & 24 deletions transports/spvwallet/user_client_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,20 +248,28 @@ func (u *userClientAdapter) ConfirmContact(ctx context.Context, contact *models.
func (u *userClientAdapter) GetContacts(ctx context.Context, conditions *filter.ContactFilter, metadata map[string]any, queryParams *filter.QueryParams) (*models.SearchContactsResponse, error) {
opts := []queries.QueryOption[filter.ContactFilter]{
queries.QueryWithMetadataFilter[filter.ContactFilter](metadata),
queries.QueryWithPageFilter[filter.ContactFilter](filter.Page{
Number: queryParams.Page,
Size: queryParams.PageSize,
Sort: queryParams.SortDirection,
SortBy: queryParams.OrderByField,
}),
queries.QueryWithFilter(filter.ContactFilter{
ModelFilter: conditions.ModelFilter,
ID: conditions.ID,
FullName: conditions.FullName,
Paymail: conditions.Paymail,
PubKey: conditions.PubKey,
Status: conditions.Status,
}),
}

if queryParams != nil {
opts = append(opts,
queries.QueryWithPageFilter[filter.ContactFilter](filter.Page{
Number: queryParams.Page,
Size: queryParams.PageSize,
Sort: queryParams.SortDirection,
SortBy: queryParams.OrderByField,
}))
}

if conditions != nil {
opts = append(opts,
queries.QueryWithFilter(filter.ContactFilter{
ModelFilter: conditions.ModelFilter,
ID: conditions.ID,
FullName: conditions.FullName,
Paymail: conditions.Paymail,
PubKey: conditions.PubKey,
Status: conditions.Status,
}))
}

res, err := u.api.Contacts(ctx, opts...)
Expand All @@ -270,17 +278,33 @@ func (u *userClientAdapter) GetContacts(ctx context.Context, conditions *filter.
return nil, errors.Wrap(err, "error while fetching contacts")
}

content := make([]*models.Contact, len(res.Content))
for i, c := range res.Content {
content[i] = &models.Contact{
Model: common.Model(c.Model),
FullName: c.FullName,
ID: c.ID,
Paymail: c.Paymail,
PubKey: c.PubKey,
Status: c.Status,
}
}

page := models.Page{
TotalElements: int64(res.Page.TotalElements),
TotalPages: res.Page.TotalPages,
Size: res.Page.Size,
Number: res.Page.Number,
}

if queryParams != nil {
page.OrderByField = &queryParams.OrderByField
page.SortDirection = &queryParams.SortDirection
}

return &models.SearchContactsResponse{
Content: []*models.Contact{},
Page: models.Page{
OrderByField: &queryParams.OrderByField,
SortDirection: &queryParams.SortDirection,
TotalElements: int64(res.Page.TotalElements),
TotalPages: res.Page.TotalPages,
Size: res.Page.Size,
Number: res.Page.Number,
},
}, nil
Content: content,
Page: page}, nil
}

func (u *userClientAdapter) GenerateTotpForContact(contact *models.Contact, period, digits uint) (string, error) {
Expand Down

0 comments on commit cded5a2

Please sign in to comment.