diff --git a/examples/server/run_server/demo_interface.go b/examples/server/run_server/demo_interface.go index 0cca35b..2575949 100644 --- a/examples/server/run_server/demo_interface.go +++ b/examples/server/run_server/demo_interface.go @@ -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 } diff --git a/pike.go b/pike.go index 8fdbcdf..6e8f996 100644 --- a/pike.go +++ b/pike.go @@ -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"` @@ -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"` } diff --git a/server/capabilities.go b/server/capabilities.go index 4e33ed1..4176f59 100644 --- a/server/capabilities.go +++ b/server/capabilities.go @@ -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 diff --git a/server/interface.go b/server/interface.go index 40d33c0..1ed9dc3 100644 --- a/server/interface.go +++ b/server/interface.go @@ -97,5 +97,5 @@ type PikePaymentServiceProvider interface { alias, domain string, satoshis uint64, metaData *RequestMetadata, - ) (*paymail.PikePaymentDestinationsResponse, error) + ) (*paymail.PikePaymentOutputsResponse, error) } diff --git a/server/mock_test.go b/server/mock_test.go index f7fd164..c7a604e 100644 --- a/server/mock_test.go +++ b/server/mock_test.go @@ -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 } diff --git a/server/payment.go b/server/payment.go index e4580eb..ea9ac63 100644 --- a/server/payment.go +++ b/server/payment.go @@ -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) @@ -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 } @@ -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 } diff --git a/server/pike.go b/server/pike.go index 54f8795..e7f0cec 100644 --- a/server/pike.go +++ b/server/pike.go @@ -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 @@ -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 {