Skip to content

Commit

Permalink
Feature: SPV Options (#48)
Browse files Browse the repository at this point in the history
* added spv options

* updated readme to include swagger host
  • Loading branch information
tigh-latte authored Sep 30, 2021
1 parent ff39a7b commit 7fe0979
Show file tree
Hide file tree
Showing 92 changed files with 3,327 additions and 188 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ USER appuser:appuser

EXPOSE 8443

CMD ["rest-server"]
CMD ["rest-server"]
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ Values can also be passed at build time to provide information such as build inf

### Server

| Key | Description | Default |
|------------------------|--------------------------------------------------------------------|----------|
| SERVER_PORT | Port which this server should use | :8443 |
| SERVER_HOST | Host name under which this server is found | payd:8443|
| SERVER_SWAGGER_ENABLED | If set to true we will expose an endpoint hosting the Swagger docs | true |
| Key | Description | Default |
|------------------------|--------------------------------------------------------------------|---------------|
| SERVER_PORT | Port which this server should use | :8443 |
| SERVER_HOST | Host name under which this server is found | payd:8443 |
| SERVER_SWAGGER_ENABLED | If set to true we will expose an endpoint hosting the Swagger docs | true |
| SERVER_SWAGGER_HOST | The host that swagger will point its api requests to | localhost:8443|

### Environment / Deployment Info

Expand Down
4 changes: 3 additions & 1 deletion cmd/rest-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/libsv/go-bc/spv"
docs "github.com/libsv/payd/docs"
"github.com/pkg/errors"
echoSwagger "github.com/swaggo/echo-swagger"
"github.com/tonicpow/go-minercraft"
Expand Down Expand Up @@ -87,6 +88,7 @@ func main() {
e.HideBanner = true
g := e.Group("/")
if cfg.Server.SwaggerEnabled {
docs.SwaggerInfo.Host = cfg.Server.SwaggerHost
e.GET("/swagger/*", echoSwagger.WrapHandler)
}
// Middleware
Expand Down Expand Up @@ -126,7 +128,7 @@ func main() {
// setup services
privKeySvc := service.NewPrivateKeys(sqlLiteStore, cfg.Wallet.Network == "mainnet")
destSvc := service.NewDestinationsService(privKeySvc, sqlLiteStore, sqlLiteStore, sqlLiteStore, mapiStore)
paymentSvc := service.NewPayments(spvv, sqlLiteStore, sqlLiteStore, sqlLiteStore, &paydSQL.Transacter{}, mapiStore, sqlLiteStore)
paymentSvc := service.NewPayments(spvv, sqlLiteStore, sqlLiteStore, sqlLiteStore, &paydSQL.Transacter{}, mapiStore, mapiStore, sqlLiteStore)

thttp.NewInvoice(service.NewInvoice(cfg.Server, cfg.Wallet, sqlLiteStore, destSvc, &paydSQL.Transacter{})).
RegisterRoutes(g)
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
EnvServerPort = "server.port"
EnvServerHost = "server.host"
EnvServerSwaggerEnabled = "server.swagger.enabled"
EnvServerSwaggerHost = "server.swagger.host"
EnvEnvironment = "env.environment"
EnvRegion = "env.region"
EnvVersion = "env.version"
Expand Down Expand Up @@ -104,6 +105,7 @@ type Server struct {
Hostname string
// SwaggerEnabled if true we will include an endpoint to serve swagger documents.
SwaggerEnabled bool
SwaggerHost string
}

// Db contains database information.
Expand Down
1 change: 1 addition & 0 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func SetupDefaults() {
viper.SetDefault(EnvServerPort, ":8443")
viper.SetDefault(EnvServerHost, "payd:8443")
viper.SetDefault(EnvServerSwaggerEnabled, true)
viper.SetDefault(EnvServerSwaggerHost, "localhost:8443")

// deployment
viper.SetDefault(EnvEnvironment, "dev")
Expand Down
1 change: 1 addition & 0 deletions config/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (c *Config) WithServer() *Config {
Port: viper.GetString(EnvServerPort),
Hostname: viper.GetString(EnvServerHost),
SwaggerEnabled: viper.GetBool(EnvServerSwaggerEnabled),
SwaggerHost: viper.GetString(EnvServerSwaggerHost),
}
return c
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/jmoiron/sqlx v1.3.4
github.com/labstack/echo/v4 v4.6.1
github.com/labstack/gommon v0.3.0
github.com/libsv/go-bc v0.1.4
github.com/libsv/go-bk v0.1.4
github.com/libsv/go-bt/v2 v2.0.0-beta.7
github.com/mailru/easyjson v0.7.7 // indirect
Expand Down Expand Up @@ -50,6 +49,7 @@ require (
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/libsv/go-bc v0.1.6
github.com/libsv/go-bt v1.0.1 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,14 @@ github.com/lib/pq v1.10.0 h1:Zx5DJFEYQXio93kgXnQ09fXNiUKsqv4OUEu2UtGcB1E=
github.com/lib/pq v1.10.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/libsv/go-bc v0.1.4 h1:p1ZT5OvXBDdKSgZaRwXd0JCrGuJ44gp05DiqUSCMdP8=
github.com/libsv/go-bc v0.1.4/go.mod h1:rrlTAokCx10eCQnutt6g++LqTQ+I1n+SPCwKDVK4sd8=
github.com/libsv/go-bc v0.1.5-0.20210929125350-a1fa4c6f5404 h1:vm0W01SdS4dpv2dRJGy/if0AudeS+VDrTX+nthzPjhQ=
github.com/libsv/go-bc v0.1.5-0.20210929125350-a1fa4c6f5404/go.mod h1:IZuBIf65nlLY1OR/oXGOW/AgVvO5hGhIhRZPiHAiVHU=
github.com/libsv/go-bc v0.1.5-0.20210929134219-ae16368dc22c h1:do7BgyzVmkRifffwfPMJyRPEinyAEwxZID41c84O7xo=
github.com/libsv/go-bc v0.1.5-0.20210929134219-ae16368dc22c/go.mod h1:IZuBIf65nlLY1OR/oXGOW/AgVvO5hGhIhRZPiHAiVHU=
github.com/libsv/go-bc v0.1.5 h1:PVgJbyT+BAkKexInOrXmUf8MW+69+t7YEUH4/c1eqXg=
github.com/libsv/go-bc v0.1.5/go.mod h1:rrlTAokCx10eCQnutt6g++LqTQ+I1n+SPCwKDVK4sd8=
github.com/libsv/go-bc v0.1.6 h1:jacgv/22R5TI1u3PInc7EG7TcvbdQWO2y3mA8be4v1w=
github.com/libsv/go-bc v0.1.6/go.mod h1:IZuBIf65nlLY1OR/oXGOW/AgVvO5hGhIhRZPiHAiVHU=
github.com/libsv/go-bk v0.0.0-20210430094342-ff08e691962b/go.mod h1:xbDkeFFpP0uyFaPLnP6TwaLpAsHaslZ0LftTdWlB6HI=
github.com/libsv/go-bk v0.1.4 h1:bTxlerGeibh8RRmyhFK03wSAEp6EAJxGR4vXuRT0LCE=
github.com/libsv/go-bk v0.1.4/go.mod h1:xbDkeFFpP0uyFaPLnP6TwaLpAsHaslZ0LftTdWlB6HI=
Expand Down
89 changes: 29 additions & 60 deletions service/payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"github.com/libsv/payd"
)

type paymentValidatorFunc func(ctx context.Context, req payd.PaymentCreate) (*bt.Tx, error)

type payments struct {
paymentVerify spv.PaymentVerifier
txWtr payd.TransactionWriter
Expand All @@ -24,25 +22,21 @@ type payments struct {
transacter payd.Transacter
callbackWtr payd.ProofCallbackWriter
broadcaster payd.BroadcastWriter
validator map[bool]paymentValidatorFunc
feeRdr payd.FeeReader
}

// NewPayments will setup and return a payments service.
func NewPayments(paymentVerify spv.PaymentVerifier, txWtr payd.TransactionWriter, invRdr payd.InvoiceReader, destRdr payd.DestinationsReader, transacter payd.Transacter, broadcaster payd.BroadcastWriter, callbackWtr payd.ProofCallbackWriter) *payments {
func NewPayments(paymentVerify spv.PaymentVerifier, txWtr payd.TransactionWriter, invRdr payd.InvoiceReader, destRdr payd.DestinationsReader, transacter payd.Transacter, broadcaster payd.BroadcastWriter, feeRdr payd.FeeReader, callbackWtr payd.ProofCallbackWriter) *payments {
svc := &payments{
paymentVerify: paymentVerify,
invRdr: invRdr,
destRdr: destRdr,
transacter: transacter,
txWtr: txWtr,
broadcaster: broadcaster,
feeRdr: feeRdr,
callbackWtr: callbackWtr,
validator: map[bool]paymentValidatorFunc{},
}
// setup validators for spv and rawTX
svc.validator[true] = svc.spvHandler
svc.validator[false] = svc.rawTxHandler

return svc
}

Expand All @@ -60,11 +54,29 @@ func (p *payments) PaymentCreate(ctx context.Context, req payd.PaymentCreate) er
if inv.State != payd.StateInvoicePending {
return lathos.NewErrDuplicate("D001", fmt.Sprintf("payment already received for invoice ID '%s'", req.InvoiceID))
}
// validate request tx or envelope and return tx.
tx, err := p.validator[inv.SPVRequired](ctx, req)

fq, err := p.feeRdr.Fees(ctx)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to read fees for payment with id %s", req.InvoiceID)
}

tx, err := p.paymentVerify.VerifyPayment(ctx, req.SPVEnvelope, p.paymentVerifyOpts(inv.SPVRequired, fq)...)
if err != nil {
if errors.Is(err, spv.ErrFeePaidNotEnough) {
return validator.ErrValidation{
"fees": {
err.Error(),
},
}
}
// map error to a validation error
return validator.ErrValidation{
"spvEnvelope": {
err.Error(),
},
}
}

// get destinations
oo, err := p.destRdr.Destinations(ctx, payd.DestinationsArgs{InvoiceID: req.InvoiceID})
if err != nil {
Expand Down Expand Up @@ -154,53 +166,10 @@ func (p *payments) PaymentCreate(ctx context.Context, req payd.PaymentCreate) er
return p.transacter.Commit(ctx)
}

func (p *payments) spvHandler(ctx context.Context, req payd.PaymentCreate) (*bt.Tx, error) {
if err := req.Validate(true); err != nil {
return nil, err
}
ok, err := p.paymentVerify.VerifyPayment(ctx, req.SPVEnvelope)
if err != nil {
// map error to a validation error
return nil, validator.ErrValidation{
"spvEnvelope": {
err.Error(),
},
}
}
if !ok {
// map error to a validation error
return nil, validator.ErrValidation{
"spvEnvelope": {
"payment envelope is not valid",
},
}
}
// validate outputs match invoice
// ensure tx pays enough fees.
tx, err := bt.NewTxFromString(req.SPVEnvelope.RawTx)
if err != nil {
// convert to validation error
if err := validator.New().Validate("rawTx", func() error {
return errors.Wrap(err, "invalid transaction received")
}).Err(); err != nil {
return nil, err
}
}
return tx, nil
}

func (p *payments) rawTxHandler(ctx context.Context, req payd.PaymentCreate) (*bt.Tx, error) {
if err := validator.New().Validate("rawTx", validator.NotEmpty(req.RawTX.ValueOrZero())).Err(); err != nil {
return nil, err
}
tx, err := bt.NewTxFromString(req.RawTX.ValueOrZero())
if err != nil {
// convert to validation error
if err := validator.New().Validate("rawTx", func() error {
return errors.Wrap(err, "invalid transaction received")
}).Err(); err != nil {
return nil, err
}
func (p *payments) paymentVerifyOpts(verifySPV bool, fq *bt.FeeQuote) []spv.VerifyOpt {
opts := []spv.VerifyOpt{spv.VerifyFees(fq)}
if !verifySPV {
opts = append(opts, spv.NoVerifySPV())
}
return tx, nil
return opts
}
1 change: 1 addition & 0 deletions vendor/github.com/boombuler/barcode/go.mod

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

7 changes: 7 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/go.mod

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

2 changes: 2 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/go.sum

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

9 changes: 9 additions & 0 deletions vendor/github.com/go-openapi/jsonpointer/go.mod

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

24 changes: 24 additions & 0 deletions vendor/github.com/go-openapi/jsonpointer/go.sum

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

11 changes: 11 additions & 0 deletions vendor/github.com/go-openapi/jsonreference/go.mod

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

37 changes: 37 additions & 0 deletions vendor/github.com/go-openapi/jsonreference/go.sum

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

13 changes: 13 additions & 0 deletions vendor/github.com/go-openapi/spec/go.mod

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

Loading

0 comments on commit 7fe0979

Please sign in to comment.