generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fix-686-where-builder' of https://github.com/bitcoin-sv…
…/spv-wallet into fix-686-where-builder
- Loading branch information
Showing
36 changed files
with
1,192 additions
and
520 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.