Skip to content

Commit

Permalink
updated to v007 of p4 (bitcoin-sv#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
tigh-latte authored Nov 30, 2021
1 parent 6e31852 commit 73ff0f1
Show file tree
Hide file tree
Showing 30 changed files with 142 additions and 140 deletions.
10 changes: 5 additions & 5 deletions data/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"io/ioutil"
"net/http"

"github.com/libsv/go-p4"
server "github.com/libsv/p4-server"
"github.com/pkg/errors"
validator "github.com/theflyingcodr/govalidator"
"github.com/theflyingcodr/lathos/errs"
Expand Down Expand Up @@ -67,7 +67,7 @@ func (c *client) Do(ctx context.Context, method, endpoint string, expStatus int,

func (c *client) handleErr(resp *http.Response, expStatus int) error {
if resp.StatusCode == http.StatusBadRequest {
brErr := p4.BadRequestError{
brErr := server.BadRequestError{
Errors: make(validator.ErrValidation),
}
if err := json.NewDecoder(resp.Body).Decode(&brErr); err != nil {
Expand All @@ -78,19 +78,19 @@ func (c *client) handleErr(resp *http.Response, expStatus int) error {

switch resp.StatusCode {
case http.StatusNotFound:
var msg p4.ClientError
var msg server.ClientError
if err := json.NewDecoder(resp.Body).Decode(&msg); err != nil {
return errors.WithStack(err)
}
return errs.NewErrNotFound(msg.Code, msg.Message)
case http.StatusConflict:
var msg p4.ClientError
var msg server.ClientError
if err := json.NewDecoder(resp.Body).Decode(&msg); err != nil {
return errors.WithStack(err)
}
return errs.NewErrDuplicate(msg.Code, msg.Message)
case http.StatusUnprocessableEntity:
var msg p4.ClientError
var msg server.ClientError
if err := json.NewDecoder(resp.Body).Decode(&msg); err != nil {
return errors.WithStack(err)
}
Expand Down
8 changes: 4 additions & 4 deletions data/noop/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewNoOp(l log.Logger) *noop {
// PaymentCreate will post a request to payd to validate and add the txos to the wallet.
//
// If invalid a non 204 status code is returned.
func (n *noop) PaymentCreate(ctx context.Context, args p4.PaymentCreateArgs, req p4.PaymentCreate) (*p4.PaymentACK, error) {
func (n *noop) PaymentCreate(ctx context.Context, args p4.PaymentCreateArgs, req p4.Payment) (*p4.PaymentACK, error) {
n.l.Info("hit noop.PaymentCreate")
return &p4.PaymentACK{}, nil
}
Expand All @@ -33,11 +33,11 @@ func (n *noop) PaymentCreate(ctx context.Context, args p4.PaymentCreateArgs, req
//
// In this example, the payd wallet has no auth, in proper implementations auth would
// be enabled and a cookie / oauth / bearer token etc would be passed down.
func (n *noop) Owner(ctx context.Context) (*p4.MerchantData, error) {
func (n *noop) Owner(ctx context.Context) (*p4.Merchant, error) {
n.l.Info("hit noop.Owner")
return &p4.MerchantData{
return &p4.Merchant{
AvatarURL: "noop",
MerchantName: "noop",
Name: "noop",
Email: "noop",
Address: "noop",
ExtendedData: nil,
Expand Down
6 changes: 3 additions & 3 deletions data/payd/payd.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewPayD(cfg *config.PayD, client data.HTTPClient) *payd {
// PaymentCreate will post a request to payd to validate and add the txos to the wallet.
//
// If invalid a non 204 status code is returned.
func (p *payd) PaymentCreate(ctx context.Context, args p4.PaymentCreateArgs, req p4.PaymentCreate) (*p4.PaymentACK, error) {
func (p *payd) PaymentCreate(ctx context.Context, args p4.PaymentCreateArgs, req p4.Payment) (*p4.PaymentACK, error) {
paymentReq := models.PayDPaymentRequest{
RawTX: req.RawTX,
SPVEnvelope: req.SPVEnvelope,
Expand All @@ -60,8 +60,8 @@ func (p *payd) PaymentCreate(ctx context.Context, args p4.PaymentCreateArgs, req
//
// In this example, the payd wallet has no auth, in proper implementations auth would
// be enabled and a cookie / oauth / bearer token etc would be passed down.
func (p *payd) Owner(ctx context.Context) (*p4.MerchantData, error) {
var owner *p4.MerchantData
func (p *payd) Owner(ctx context.Context) (*p4.Merchant, error) {
var owner *p4.Merchant
if err := p.client.Do(ctx, http.MethodGet, fmt.Sprintf(urlOwner, p.baseURL()), http.StatusOK, nil, &owner); err != nil {
return nil, errors.WithStack(err)
}
Expand Down
8 changes: 4 additions & 4 deletions data/payd/payd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestPayd_PaymentCreate(t *testing.T) {
tests := map[string]struct {
doFunc func(context.Context, string, string, int, interface{}, interface{}) error
args p4.PaymentCreateArgs
req p4.PaymentCreate
req p4.Payment
cfg *config.PayD
expURL string
expReq models.PayDPaymentRequest
Expand All @@ -34,7 +34,7 @@ func TestPayd_PaymentCreate(t *testing.T) {
args: p4.PaymentCreateArgs{
PaymentID: "qwe123",
},
req: p4.PaymentCreate{
req: p4.Payment{
RawTX: func() *string { s := "rawrawraw"; return &s }(),
SPVEnvelope: &spv.Envelope{},
ProofCallbacks: map[string]p4.ProofCallback{
Expand All @@ -61,7 +61,7 @@ func TestPayd_PaymentCreate(t *testing.T) {
args: p4.PaymentCreateArgs{
PaymentID: "qwe123",
},
req: p4.PaymentCreate{
req: p4.Payment{
RawTX: func() *string { s := "rawrawraw"; return &s }(),
SPVEnvelope: &spv.Envelope{},
ProofCallbacks: map[string]p4.ProofCallback{
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestPayd_PaymentCreate(t *testing.T) {
args: p4.PaymentCreateArgs{
PaymentID: "qwe123",
},
req: p4.PaymentCreate{
req: p4.Payment{
RawTX: func() *string { s := "rawrawraw"; return &s }(),
SPVEnvelope: &spv.Envelope{},
ProofCallbacks: map[string]p4.ProofCallback{
Expand Down
2 changes: 1 addition & 1 deletion data/sockets/payd.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (p *payd) PaymentRequest(ctx context.Context, args p4.PaymentRequestArgs) (
}

// PaymentCreate will send a request to payd to create and process the payment.
func (p *payd) PaymentCreate(ctx context.Context, args p4.PaymentCreateArgs, req p4.PaymentCreate) (*p4.PaymentACK, error) {
func (p *payd) PaymentCreate(ctx context.Context, args p4.PaymentCreateArgs, req p4.Payment) (*p4.PaymentACK, error) {
msg := sockets.NewMessage(RoutePayment, "", args.PaymentID)
msg.AppID = "p4"
msg.CorrelationID = uuid.NewString()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ require (
github.com/labstack/echo/v4 v4.6.1
github.com/libsv/go-bc v0.1.7
github.com/libsv/go-bk v0.1.5
github.com/libsv/go-bt/v2 v2.1.0-beta.1
github.com/libsv/go-p4 v0.0.7-0.20211129151249-0811fc553403
github.com/libsv/go-bt/v2 v2.0.0-beta.13
github.com/libsv/go-p4 v0.0.7
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.0
github.com/rs/zerolog v1.26.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,14 @@ github.com/libsv/go-bk v0.1.5 h1:fqbWy8nwVM/ayM8Nxe+lM7fN0FaUMMD1w5MCpwit7XQ=
github.com/libsv/go-bk v0.1.5/go.mod h1:xbDkeFFpP0uyFaPLnP6TwaLpAsHaslZ0LftTdWlB6HI=
github.com/libsv/go-bt/v2 v2.0.0-beta.7/go.mod h1:6Vk1qMlMoFJFNm7rR7NmFx4VwHNMCi4+V9WBqTd37rQ=
github.com/libsv/go-bt/v2 v2.0.0-beta.12/go.mod h1:Ict9mJuFxjzNszELtCTtAHH5tQtQd2fnXUKG1n1ldP0=
github.com/libsv/go-bt/v2 v2.0.0-beta.13 h1:kedGtn9OX6k+OpZF21Wh7EKnce782nYn2NuYVloWr4U=
github.com/libsv/go-bt/v2 v2.0.0-beta.13/go.mod h1:u5g3GmVLffBV8sWvMqHR3JekC51OR9XYvmQp1h/XoiQ=
github.com/libsv/go-bt/v2 v2.1.0-beta.1 h1:3nSvhUnrCOM4Cel+zVlLjCoAITCJ7dbYJuPGj6x8958=
github.com/libsv/go-bt/v2 v2.1.0-beta.1/go.mod h1:u5g3GmVLffBV8sWvMqHR3JekC51OR9XYvmQp1h/XoiQ=
github.com/libsv/go-p4 v0.0.7-0.20211129151249-0811fc553403 h1:CAleggVLG1gbSratpcG9VhwEf7c6+DVxABnDO/7RdtQ=
github.com/libsv/go-p4 v0.0.7-0.20211129151249-0811fc553403/go.mod h1:RpUraQaHbPZG6vp4HPhBvS2J9vL/GKMdr8J+VUpOvkk=
github.com/libsv/go-p4 v0.0.7 h1:zKrNUNQ1zL6T5prxWu7LkxXQ5ysBHDL1RH8Ziu6f9kQ=
github.com/libsv/go-p4 v0.0.7/go.mod h1:neXEtCQkbdYbB35sMZ8BBA1NK4/uENZ+MhfiM1qzeBM=
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
Expand Down
5 changes: 0 additions & 5 deletions mocks/mocks.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
package mocks

//go:generate moq -pkg mocks -out payment_writer.go ../ PaymentWriter
//go:generate moq -pkg mocks -out payment_service.go ../ PaymentService
//go:generate moq -pkg mocks -out payment_request_service.go ../ PaymentRequestService
//go:generate moq -pkg mocks -out merchant_reader.go ../ MerchantReader
//go:generate moq -pkg mocks -out destination_reader.go ../ DestinationReader
//go:generate moq -pkg mocks -out http_client.go ../data HTTPClient
15 changes: 15 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
// Package server is needed for swagger doc generation. Do not delete.
package server

import validator "github.com/theflyingcodr/govalidator"

// ClientError defines an error type that can be returned to handle client errors.
type ClientError struct {
ID string `json:"id" example:"e97970bf-2a88-4bc8-90e6-2f597a80b93d"`
Code string `json:"code" example:"N01"`
Title string `json:"title" example:"not found"`
Message string `json:"message" example:"unable to find foo when loading bar"`
}

// BadRequestError defines an error type to handle validation errors.
type BadRequestError struct {
Errors validator.ErrValidation `json:"errors" swaggertype:"validator.ErrValidation"`
}
2 changes: 1 addition & 1 deletion service/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewPayment(l log.Logger, paymentWtr p4.PaymentWriter) *payment {
}

// PaymentCreate will setup a new payment and return the result.
func (p *payment) PaymentCreate(ctx context.Context, args p4.PaymentCreateArgs, req p4.PaymentCreate) (*p4.PaymentACK, error) {
func (p *payment) PaymentCreate(ctx context.Context, args p4.PaymentCreateArgs, req p4.Payment) (*p4.PaymentACK, error) {
if err := args.Validate(); err != nil {
return nil, err
}
Expand Down
32 changes: 16 additions & 16 deletions service/payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ import (

"github.com/libsv/go-bc/spv"
"github.com/libsv/go-p4"
p4mocks "github.com/libsv/go-p4/mocks"
"github.com/libsv/p4-server/log"
"github.com/libsv/p4-server/mocks"
"github.com/libsv/p4-server/service"
"github.com/stretchr/testify/assert"
)

func TestPayment_Create(t *testing.T) {
tests := map[string]struct {
paymentCreateFn func(context.Context, p4.PaymentCreateArgs, p4.PaymentCreate) (*p4.PaymentACK, error)
paymentCreateFn func(context.Context, p4.PaymentCreateArgs, p4.Payment) (*p4.PaymentACK, error)
args p4.PaymentCreateArgs
req p4.PaymentCreate
req p4.Payment
expErr error
}{
"successful payment create": {
paymentCreateFn: func(context.Context, p4.PaymentCreateArgs, p4.PaymentCreate) (*p4.PaymentACK, error) {
paymentCreateFn: func(context.Context, p4.PaymentCreateArgs, p4.Payment) (*p4.PaymentACK, error) {
return &p4.PaymentACK{}, nil
},
req: p4.PaymentCreate{
req: p4.Payment{
SPVEnvelope: &spv.Envelope{
RawTx: "01000000000000000000",
TxID: "d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43",
},
MerchantData: p4.MerchantData{
MerchantData: p4.Merchant{
ExtendedData: map[string]interface{}{"paymentReference": "omgwow"},
},
},
Expand All @@ -38,48 +38,48 @@ func TestPayment_Create(t *testing.T) {
},
},
"invalid args errors": {
paymentCreateFn: func(context.Context, p4.PaymentCreateArgs, p4.PaymentCreate) (*p4.PaymentACK, error) {
paymentCreateFn: func(context.Context, p4.PaymentCreateArgs, p4.Payment) (*p4.PaymentACK, error) {
return &p4.PaymentACK{}, nil
},
args: p4.PaymentCreateArgs{},
req: p4.PaymentCreate{
req: p4.Payment{
SPVEnvelope: &spv.Envelope{
RawTx: "01000000000000000000",
TxID: "d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43",
},
MerchantData: p4.MerchantData{
MerchantData: p4.Merchant{
ExtendedData: map[string]interface{}{"paymentReference": "omgwow"},
},
},
expErr: errors.New("[paymentID: value cannot be empty]"),
},
"missing raw tx errors": {
paymentCreateFn: func(context.Context, p4.PaymentCreateArgs, p4.PaymentCreate) (*p4.PaymentACK, error) {
paymentCreateFn: func(context.Context, p4.PaymentCreateArgs, p4.Payment) (*p4.PaymentACK, error) {
return &p4.PaymentACK{}, nil
},
args: p4.PaymentCreateArgs{
PaymentID: "abc123",
},
req: p4.PaymentCreate{
MerchantData: p4.MerchantData{
req: p4.Payment{
MerchantData: p4.Merchant{
ExtendedData: map[string]interface{}{"paymentReference": "omgwow"},
},
},
expErr: errors.New("[spvEnvelope/rawTx: either an SPVEnvelope or a rawTX are required]"),
},
"error on payment create is handled": {
paymentCreateFn: func(context.Context, p4.PaymentCreateArgs, p4.PaymentCreate) (*p4.PaymentACK, error) {
paymentCreateFn: func(context.Context, p4.PaymentCreateArgs, p4.Payment) (*p4.PaymentACK, error) {
return nil, errors.New("lol oh boi")
},
args: p4.PaymentCreateArgs{
PaymentID: "abc123",
},
req: p4.PaymentCreate{
req: p4.Payment{
SPVEnvelope: &spv.Envelope{
RawTx: "01000000000000000000",
TxID: "d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43",
},
MerchantData: p4.MerchantData{
MerchantData: p4.Merchant{
ExtendedData: map[string]interface{}{"paymentReference": "omgwow"},
},
},
Expand All @@ -91,7 +91,7 @@ func TestPayment_Create(t *testing.T) {
t.Run(name, func(t *testing.T) {
svc := service.NewPayment(
log.Noop{},
&mocks.PaymentWriterMock{
&p4mocks.PaymentWriterMock{
PaymentCreateFunc: test.paymentCreateFn,
})

Expand Down
4 changes: 2 additions & 2 deletions service/paymentrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func (p *paymentRequest) PaymentRequest(ctx context.Context, args p4.PaymentRequ
ExpirationTimestamp: dests.ExpiresAt,
PaymentURL: fmt.Sprintf("http://%s/api/v1/payment/%s", p.walletCfg.FQDN, args.PaymentID),
Memo: fmt.Sprintf("invoice %s", args.PaymentID),
MerchantData: &p4.MerchantData{
MerchantData: &p4.Merchant{
AvatarURL: merchant.AvatarURL,
MerchantName: merchant.MerchantName,
Name: merchant.Name,
Email: merchant.Email,
Address: merchant.Address,
ExtendedData: merchant.ExtendedData,
Expand Down
Loading

0 comments on commit 73ff0f1

Please sign in to comment.