Skip to content

Commit

Permalink
Merge branch 'fix-686-where-builder' of https://github.com/bitcoin-sv…
Browse files Browse the repository at this point in the history
…/spv-wallet into fix-686-where-builder
  • Loading branch information
chris-4chain committed Mar 28, 2024
2 parents 9c283ca + 2491147 commit 8c61f24
Show file tree
Hide file tree
Showing 36 changed files with 1,192 additions and 520 deletions.
50 changes: 0 additions & 50 deletions actions/contacts/create.go

This file was deleted.

31 changes: 22 additions & 9 deletions actions/contacts/models.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
package contacts

import (
"errors"

"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/bitcoin-sv/spv-wallet/models"
)

// CreateContact is the model for creating a contact
type CreateContact struct {
// UpsertContact is the model for creating a contact
type UpsertContact struct {
// The complete name of the contact, including first name, middle name (if applicable), and last name.
FullName string `json:"fullName"`
// Accepts a JSON object for embedding custom metadata, enabling arbitrary additional information to be associated with the resource
Metadata engine.Metadata `json:"metadata" swaggertype:"object,string" example:"key:value,key2:value2"`
// Optional paymail address owned by the user to bind the contact to. It is required in case if user has multiple paymail addresses
RequesterPaymail string `json:"requesterPaymail"`
}

func (p *UpsertContact) validate() error {
if p.FullName == "" {
return errors.New("fullName is required")
}

return nil
}

// UpdateContact is the model for updating a contact
type UpdateContact struct {
XPubID string `json:"xpub_id"`
FullName string `json:"full_name"`
Paymail string `json:"paymail"`
PubKey string `json:"pubKey"`
Status models.ContactStatus `json:"status"`
Metadata engine.Metadata `json:"metadata"`
XPubID string `json:"xpub_id"`
FullName string `json:"full_name"`
Paymail string `json:"paymail"`
PubKey string `json:"pubKey"`
Status string `json:"status"`
Metadata engine.Metadata `json:"metadata"`
}
5 changes: 2 additions & 3 deletions actions/contacts/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ func NewHandler(appConfig *config.AppConfig, services *config.AppServices) route

apiEndpoints := routes.APIEndpointsFunc(func(router *gin.RouterGroup) {
contactGroup := router.Group("/contact")
contactGroup.POST("", action.create)
contactGroup.PATCH("", action.update)
contactGroup.PUT("/:paymail", action.upsert)

contactGroup.PATCH("", action.update)
contactsGroup := router.Group("/contacts")
contactsGroup.GET("", action.search)

})

return apiEndpoints
Expand Down
2 changes: 1 addition & 1 deletion actions/contacts/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (ts *TestSuite) TestContactsRegisterRoutes() {
method string
url string
}{
{"POST", "/" + config.APIVersion + "/contact"},
{"PUT", "/" + config.APIVersion + "/contact/:paymail"},
{"GET", "/" + config.APIVersion + "/contacts"},
}

Expand Down
61 changes: 61 additions & 0 deletions actions/contacts/upsert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package contacts

import (
"encoding/json"
"errors"
"net/http"

"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/server/auth"
"github.com/gin-gonic/gin"
)

// upsert will add a new contact or modify an existing one.
// Upsert contact godoc
// @Summary Upsert contact
// @Description Add or update contact. When adding a new contact, the system utilizes Paymail's PIKE capability to dispatch an invitation request, asking the counterparty to include the current user in their contacts.
// @Tags Contact
// @Produce json
// @Param paymail path string true "Paymail address of the contact the user wants to add/modify"
// @Param UpsertContact body contacts.UpsertContact true "Full name and metadata needed to add/modify contact"
// @Success 201
// @Router /v1/contact/{paymail} [PUT]
// @Security x-auth-xpub
func (a *Action) upsert(c *gin.Context) {
requesterPubKey := c.GetString(auth.ParamXPubKey)
cPaymail := c.Param("paymail")

var req UpsertContact
if err := json.NewDecoder(c.Request.Body).Decode(&req); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}

if err := req.validate(); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}

contact, err := a.Services.SpvWalletEngine.UpsertContact(
c.Request.Context(),
req.FullName, cPaymail,
requesterPubKey, req.RequesterPaymail,
engine.WithMetadatas(req.Metadata))

if err != nil && !errors.Is(err, engine.ErrAddingContactRequest) {
c.JSON(http.StatusInternalServerError, err.Error())
return
}

response := &models.CreateContactResponse{
Contact: mappings.MapToContactContract(contact),
}

if err != nil {
response.AddAdditionalInfo("warning", err.Error())
}

c.JSON(http.StatusOK, response)
}
4 changes: 4 additions & 0 deletions config/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ func loadPaymail(appConfig *AppConfig, options []engine.ClientOps) []engine.Clie
if pm.Beef.enabled() {
options = append(options, engine.WithPaymailBeefSupport(pm.Beef.BlockHeaderServiceHeaderValidationURL, pm.Beef.BlockHeaderServiceAuthToken))
}
if appConfig.ExperimentalFeatures.PikeEnabled {
options = append(options, engine.WithPaymailPikeSupport())
}

return options
}

Expand Down
111 changes: 61 additions & 50 deletions docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8c61f24

Please # to comment.