Skip to content

Commit

Permalink
Merge pull request #143 from libsv/fix/ancestry
Browse files Browse the repository at this point in the history
Feature: Transaction Ancestory
  • Loading branch information
roppa authored Apr 5, 2022
2 parents 73f8dee + 12df91b commit 17b3442
Show file tree
Hide file tree
Showing 20 changed files with 273 additions and 410 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
require (
github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.5.0
github.com/libsv/go-bc v0.1.8
github.com/libsv/go-bc v0.1.10
github.com/rs/zerolog v1.26.1
github.com/theflyingcodr/sockets v0.0.11-beta.0.20220225103542-c6eecb16f586
)
Expand Down Expand Up @@ -92,7 +92,7 @@ require (

require (
github.com/InVisionApp/go-health/v2 v2.1.2
github.com/libsv/go-dpp v0.0.9
github.com/libsv/go-dpp v0.1.11
github.com/libsv/go-spvchannels v0.0.2
)

Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,12 @@ 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.8 h1:sn0zz9nyaC0oRNYfRiNgt0ceCAjN4QeJXherfo1o7QI=
github.com/libsv/go-bc v0.1.8/go.mod h1:4YMpWv9xxg/tt+y1jP9HxIh+2fiZno4+UIh78wi1Vwk=
github.com/libsv/go-bc v0.1.9 h1:dTCGLUs3SiolKN5LpNPBTGCEl/HHaGB+hwZRkNfhDJE=
github.com/libsv/go-bc v0.1.9/go.mod h1:55OsjWtvaIEXy4w02icUi2lIdThuwqkAiSeF4GPU5tw=
github.com/libsv/go-bc v0.1.10-0.20220405091049-8313f23e03aa h1:pjp+AkTohM3CBrB0aL3xf09TliJu86EblV5AEum8maE=
github.com/libsv/go-bc v0.1.10-0.20220405091049-8313f23e03aa/go.mod h1:55OsjWtvaIEXy4w02icUi2lIdThuwqkAiSeF4GPU5tw=
github.com/libsv/go-bc v0.1.10 h1:pe3CriFVcHauoQSk8rJeC3/7BOgTqfp33LBb0M4SSi8=
github.com/libsv/go-bc v0.1.10/go.mod h1:55OsjWtvaIEXy4w02icUi2lIdThuwqkAiSeF4GPU5tw=
github.com/libsv/go-bk v0.1.5/go.mod h1:xbDkeFFpP0uyFaPLnP6TwaLpAsHaslZ0LftTdWlB6HI=
github.com/libsv/go-bk v0.1.6 h1:c9CiT5+64HRDbzxPl1v/oiFmbvWZTuUYqywCf+MBs/c=
github.com/libsv/go-bk v0.1.6/go.mod h1:khJboDoH18FPUaZlzRFKzlVN84d4YfdmlDtdX4LAjQA=
Expand All @@ -523,6 +529,12 @@ github.com/libsv/go-bt/v2 v2.1.0-beta.2 h1:oq6BQQtNeZiG/esfoY/7RyYF+dDj996xqNfvo
github.com/libsv/go-bt/v2 v2.1.0-beta.2/go.mod h1:u5g3GmVLffBV8sWvMqHR3JekC51OR9XYvmQp1h/XoiQ=
github.com/libsv/go-dpp v0.0.9 h1:Sm9udJ4n9x7/3WSGjdB0+R7SZKJBIltI6Qnji/VgISY=
github.com/libsv/go-dpp v0.0.9/go.mod h1:VZ4BSG12xv5KHeqdz4tPDYcwVkAFmfDXO4zJ2t9bsyI=
github.com/libsv/go-dpp v0.0.10-0.20220404121630-0c117ccbe02a h1:wRtlK0r2681PYRQJOCwb9YGmJXwmsAOU0pYLaVnX3HE=
github.com/libsv/go-dpp v0.0.10-0.20220404121630-0c117ccbe02a/go.mod h1:VZ4BSG12xv5KHeqdz4tPDYcwVkAFmfDXO4zJ2t9bsyI=
github.com/libsv/go-dpp v0.0.10 h1:rQcWKkVlWl8ud4s0uqOJ/hEyDG+oPxP/jbJZmISYdKw=
github.com/libsv/go-dpp v0.0.10/go.mod h1:nc9Lh987LXHc8+pE3k3ThMWvQQft9JSboFQF1elfRFs=
github.com/libsv/go-dpp v0.1.11 h1:K3fpykSin7L5s8SghkLSivACuimagdbCdHXHb5ZrrRQ=
github.com/libsv/go-dpp v0.1.11/go.mod h1:nc9Lh987LXHc8+pE3k3ThMWvQQft9JSboFQF1elfRFs=
github.com/libsv/go-spvchannels v0.0.2 h1:WkhJZmVQftNVSYFJjkXTf+78IC0jX6jBYKnWrVm57+E=
github.com/libsv/go-spvchannels v0.0.2/go.mod h1:J2uSn4X/Eq3BIEfmGtuhAHZUxyUIQydpxSZFyEIb8D0=
github.com/lyft/protoc-gen-star v0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w=
Expand Down
12 changes: 6 additions & 6 deletions mocks/p4.go → mocks/dpp.go

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

50 changes: 28 additions & 22 deletions mocks/payment_verifier.go

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

2 changes: 1 addition & 1 deletion payment_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type PaymentRequestResponse struct {
Memo string `json:"memo"`
MerchantData User `json:"merchantData"`
Fee *bt.FeeQuote `json:"fees"`
SPVRequired bool `json:"spvRequired" example:"true"`
AncestryRequired bool `json:"ancestryRequired" example:"true"`
}

// PaymentRequestService will create and return a paymentRequest using the args provided.
Expand Down
16 changes: 4 additions & 12 deletions service/envelopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,10 @@ func (e *envelopes) Envelope(ctx context.Context, args payd.EnvelopeArgs, req dp
return nil, errors.Wrapf(err, "failed to sign tx %s", tx.String())
}

spvEnvelope := &spv.Envelope{
RawTx: tx.String(),
TxID: tx.TxID(),
}

if req.SPVRequired {
// Create the spv envelope for the tx.
s, err := e.spvc.CreateEnvelope(ctx, tx)
if err != nil {
return nil, errors.Wrapf(err, "failed to create spv envelope for tx %s", tx.String())
}
spvEnvelope = s
// Create the spv envelope for the tx.
spvEnvelope, err := e.spvc.CreateEnvelope(ctx, tx)
if err != nil {
return nil, errors.Wrapf(err, "failed to create spv envelope for tx %s", tx.String())
}

txCreate := payd.TransactionCreate{
Expand Down
9 changes: 8 additions & 1 deletion service/pay.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"encoding/hex"
"fmt"

"github.com/libsv/go-bk/bip32"
Expand Down Expand Up @@ -91,9 +92,15 @@ func (p *pay) Pay(ctx context.Context, req payd.PayRequest) (*dpp.PaymentACK, er
if err != nil {
return nil, errors.Wrapf(err, "envelope creation failed for '%s'", req.PayToURL)
}
bb, err := env.Bytes()
if err != nil {
return nil, errors.Wrapf(err, "failed to convert ancestry into bytes for payment '%s'", payReq.PaymentURL)
}
ancestry := hex.EncodeToString(bb)
// Send the payment to the dpp proxy server.
ack, err := p.dpp.PaymentSend(ctx, req, dpp.Payment{
SPVEnvelope: env,
Ancestry: &ancestry,
RawTx: &env.RawTx,
ProofCallbacks: map[string]dpp.ProofCallback{
"https://" + p.svrCfg.Hostname + "/api/v1/proofs/" + env.TxID: {},
},
Expand Down
2 changes: 1 addition & 1 deletion service/payment_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (p *paymentRequest) PaymentRequest(ctx context.Context, args payd.PaymentRe
}
return &payd.PaymentRequestResponse{
Network: string(p.cfg.Network),
SPVRequired: dd.SPVRequired,
AncestryRequired: dd.SPVRequired,
Destinations: payd.DPPDestination{Outputs: oo},
Fee: fees,
CreationTimestamp: dd.CreatedAt,
Expand Down
20 changes: 16 additions & 4 deletions service/payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"encoding/hex"
"fmt"
"path"
"time"
Expand Down Expand Up @@ -80,7 +81,19 @@ func (p *payments) PaymentCreate(ctx context.Context, args payd.PaymentCreateArg
return nil, errs.NewErrUnprocessable("E001", "fee quote has expired, please make a new payment request")
}

tx, err := p.paymentVerify.VerifyPayment(ctx, req.SPVEnvelope, p.paymentVerifyOpts(inv.SPVRequired, fq)...)
tx, err := bt.NewTxFromString(*req.RawTx)
if err != nil {
return nil, errors.Wrap(err, "failed to parse tx")
}

ancestors := []byte{1}
if req.Ancestry != nil {
ancestors, err = hex.DecodeString(*req.Ancestry)
if err != nil {
return nil, errors.Wrap(err, "failed to decode ancestry")
}
}
tx, err = p.paymentVerify.VerifyPayment(ctx, tx, ancestors, p.paymentVerifyOpts(inv.SPVRequired, fq)...)
if err != nil {
if errors.Is(err, spv.ErrFeePaidNotEnough) {
return nil, validator.ErrValidation{
Expand All @@ -91,12 +104,11 @@ func (p *payments) PaymentCreate(ctx context.Context, args payd.PaymentCreateArg
}
// map error to a validation error
return nil, validator.ErrValidation{
"spvEnvelope": {
"ancestry": {
err.Error(),
},
}
}

// get destinations
oo, err := p.destRdr.Destinations(ctx, payd.DestinationsArgs{InvoiceID: args.InvoiceID})
if err != nil {
Expand Down Expand Up @@ -156,7 +168,7 @@ func (p *payments) PaymentCreate(ctx context.Context, args payd.PaymentCreateArg
InvoiceID: args.InvoiceID,
TxID: txID,
RefundTo: null.StringFromPtr(req.RefundTo),
TxHex: req.SPVEnvelope.RawTx,
TxHex: *req.RawTx,
Outputs: txos,
}); err != nil {
return nil, errors.Wrapf(err, "failed to store transaction for invoiceID '%s'", args.InvoiceID)
Expand Down
Loading

0 comments on commit 17b3442

Please sign in to comment.