Skip to content

Commit

Permalink
chore(SPV-789): rename struct, add comments, refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed May 20, 2024
1 parent bbb5381 commit a79ce3a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/server/run_server/demo_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ func (d *demoServiceProvider) CreatePikeDestinationResponse(
alias, domain string,
satoshis uint64,
metaData *server.RequestMetadata,
) (*paymail.PikePaymentDestinationsResponse, error) {
) (*paymail.PikePaymentOutputsResponse, error) {
return nil, nil
}
15 changes: 10 additions & 5 deletions pike.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ import (
"time"
)

// PikeContactRequestResponse is PIKE wrapper for StandardResponse
type PikeContactRequestResponse struct {
StandardResponse
}

// PikeContactRequestPayload is a payload used to request a contact
type PikeContactRequestPayload struct {
FullName string `json:"fullName"`
Paymail string `json:"paymail"`
}

// PikePaymentOutputsPayload is a payload needed to get payment outputs
// TODO: check if everything is needed after whole PIKE implementation
type PikePaymentDestinationsRequest struct {
type PikePaymentOutputsPayload struct {
SenderName string `json:"senderName"`
SenderPaymail string `json:"senderPaymail"`
Amount uint64 `json:"amount"`
Expand All @@ -28,12 +31,14 @@ type PikePaymentDestinationsRequest struct {
Signature string `json:"signature"`
}

type PikePaymentDestinationsResponse struct {
Outputs []PikePaymentDestination `json:"outputs"`
Reference string `json:"reference"`
// PikePaymentOutputsResponse is a response which contain output templates
type PikePaymentOutputsResponse struct {
Outputs []PikePaymentOutput `json:"outputs"`
Reference string `json:"reference"`
}

type PikePaymentDestination struct {
// PikePaymentOutput is a single output template with satoshis
type PikePaymentOutput struct {
Script string `json:"script"`
Satoshis int `json:"satoshis"`
}
Expand Down
6 changes: 5 additions & 1 deletion server/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ func (c *Configuration) EnrichCapabilities(host string) (*paymail.CapabilitiesPa
for key, cap := range c.nestedCapabilities {
payload.Capabilities[key] = make(map[string]interface{})
for nestedKey, nestedCap := range cap {
payload.Capabilities[key].(map[string]interface{})[nestedKey] = serviceUrl + nestedCap.Path
nestedObj, ok := payload.Capabilities[key].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("failed to cast nested capabilities")
}
nestedObj[nestedKey] = serviceUrl + nestedCap.Path
}
}
return payload, nil
Expand Down
2 changes: 1 addition & 1 deletion server/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ type PikePaymentServiceProvider interface {
alias, domain string,
satoshis uint64,
metaData *RequestMetadata,
) (*paymail.PikePaymentDestinationsResponse, error)
) (*paymail.PikePaymentOutputsResponse, error)
}
2 changes: 1 addition & 1 deletion server/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ func (m *mockServiceProvider) AddContact(ctx context.Context, requesterPaymail s
return nil
}

func (m *mockServiceProvider) CreatePikeDestinationResponse(ctx context.Context, alias, domain string, satoshis uint64, metaData *RequestMetadata) (*paymail.PikePaymentDestinationsResponse, error) {
func (m *mockServiceProvider) CreatePikeDestinationResponse(ctx context.Context, alias, domain string, satoshis uint64, metaData *RequestMetadata) (*paymail.PikePaymentOutputsResponse, error) {
return nil, nil
}
7 changes: 5 additions & 2 deletions server/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
)

// GetPaymailAndCreateMetadata is a helper function to get the paymail from the request, check it in database and create the metadata based on that.
func (c *Configuration) GetPaymailAndCreateMetadata(context *gin.Context, satoshis uint64) (alias, domain string, md *RequestMetadata, ok bool) {
incomingPaymail := context.Param(PaymailAddressParamName)

Expand All @@ -14,7 +15,8 @@ func (c *Configuration) GetPaymailAndCreateMetadata(context *gin.Context, satosh
if len(paymailAddress) == 0 {
ErrorResponse(context, ErrorInvalidParameter, "invalid paymail: "+incomingPaymail, http.StatusBadRequest)
return
} else if !c.IsAllowedDomain(domain) {
}
if !c.IsAllowedDomain(domain) {
ErrorResponse(context, ErrorUnknownDomain, "domain unknown: "+domain, http.StatusBadRequest)
return
}
Expand All @@ -39,7 +41,8 @@ func (c *Configuration) GetPaymailAndCreateMetadata(context *gin.Context, satosh
if err != nil {
ErrorResponse(context, ErrorFindingPaymail, err.Error(), http.StatusExpectationFailed)
return
} else if foundPaymail == nil {
}
if foundPaymail == nil {
ErrorResponse(context, ErrorPaymailNotFound, "paymail not found", http.StatusNotFound)
return
}
Expand Down
7 changes: 5 additions & 2 deletions server/pike.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ func (c *Configuration) pikeNewContact(rc *gin.Context) {
}

func (c *Configuration) pikeGetPaymentDestinations(rc *gin.Context) {
var paymentDestinationRequest paymail.PikePaymentDestinationsRequest
var paymentDestinationRequest paymail.PikePaymentOutputsPayload
err := json.NewDecoder(rc.Request.Body).Decode(&paymentDestinationRequest)
defer func() {
_ = rc.Request.Body.Close()
}()
if err != nil {
ErrorResponse(rc, ErrorInvalidParameter, err.Error(), http.StatusBadRequest)
return
Expand All @@ -39,7 +42,7 @@ func (c *Configuration) pikeGetPaymentDestinations(rc *gin.Context) {
return
}

var response *paymail.PikePaymentDestinationsResponse
var response *paymail.PikePaymentOutputsResponse
if response, err = c.pikePaymentActions.CreatePikeDestinationResponse(
rc.Request.Context(), alias, domain, paymentDestinationRequest.Amount, md,
); err != nil {
Expand Down

0 comments on commit a79ce3a

Please sign in to comment.